blob: e53fe191738cfe8ce7c7cfe6202c2651258ff952 [file] [log] [blame]
Daniel De Graafdd314052011-02-07 17:23:05 -05001/******************************************************************************
2 * gntalloc.c
3 *
4 * Device for creating grant references (in user-space) that may be shared
5 * with other domains.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17/*
18 * This driver exists to allow userspace programs in Linux to allocate kernel
19 * memory that will later be shared with another domain. Without this device,
20 * Linux userspace programs cannot create grant references.
21 *
22 * How this stuff works:
23 * X -> granting a page to Y
24 * Y -> mapping the grant from X
25 *
26 * 1. X uses the gntalloc device to allocate a page of kernel memory, P.
27 * 2. X creates an entry in the grant table that says domid(Y) can access P.
28 * This is done without a hypercall unless the grant table needs expansion.
29 * 3. X gives the grant reference identifier, GREF, to Y.
30 * 4. Y maps the page, either directly into kernel memory for use in a backend
31 * driver, or via a the gntdev device to map into the address space of an
32 * application running in Y. This is the first point at which Xen does any
33 * tracking of the page.
34 * 5. A program in X mmap()s a segment of the gntalloc device that corresponds
35 * to the shared page, and can now communicate with Y over the shared page.
36 *
37 *
38 * NOTE TO USERSPACE LIBRARIES:
39 * The grant allocation and mmap()ing are, naturally, two separate operations.
40 * You set up the sharing by calling the create ioctl() and then the mmap().
41 * Teardown requires munmap() and either close() or ioctl().
42 *
43 * WARNING: Since Xen does not allow a guest to forcibly end the use of a grant
44 * reference, this device can be used to consume kernel memory by leaving grant
45 * references mapped by another domain when an application exits. Therefore,
46 * there is a global limit on the number of pages that can be allocated. When
47 * all references to the page are unmapped, it will be freed during the next
48 * grant operation.
49 */
50
Joe Perches283c0972013-06-28 03:21:41 -070051#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
52
Daniel De Graafdd314052011-02-07 17:23:05 -050053#include <linux/atomic.h>
54#include <linux/module.h>
55#include <linux/miscdevice.h>
56#include <linux/kernel.h>
57#include <linux/init.h>
58#include <linux/slab.h>
59#include <linux/fs.h>
60#include <linux/device.h>
61#include <linux/mm.h>
62#include <linux/uaccess.h>
63#include <linux/types.h>
64#include <linux/list.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050065#include <linux/highmem.h>
Daniel De Graafdd314052011-02-07 17:23:05 -050066
67#include <xen/xen.h>
68#include <xen/page.h>
69#include <xen/grant_table.h>
70#include <xen/gntalloc.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050071#include <xen/events.h>
Daniel De Graafdd314052011-02-07 17:23:05 -050072
73static int limit = 1024;
74module_param(limit, int, 0644);
75MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
76 "the gntalloc device");
77
78static LIST_HEAD(gref_list);
Daniel De Graaf8ca19a82011-10-27 17:58:48 -040079static DEFINE_MUTEX(gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -050080static int gref_size;
81
Daniel De Graafbdc612d2011-02-03 12:19:04 -050082struct notify_info {
83 uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */
84 uint16_t flags:2; /* Bits 12-13: Unmap notification flags */
85 int event; /* Port (event channel) to notify */
86};
87
Daniel De Graafdd314052011-02-07 17:23:05 -050088/* Metadata on a grant reference. */
89struct gntalloc_gref {
90 struct list_head next_gref; /* list entry gref_list */
91 struct list_head next_file; /* list entry file->list, if open */
92 struct page *page; /* The shared page */
93 uint64_t file_index; /* File offset for mmap() */
94 unsigned int users; /* Use count - when zero, waiting on Xen */
95 grant_ref_t gref_id; /* The grant reference number */
Daniel De Graafbdc612d2011-02-03 12:19:04 -050096 struct notify_info notify; /* Unmap notification */
Daniel De Graafdd314052011-02-07 17:23:05 -050097};
98
99struct gntalloc_file_private_data {
100 struct list_head list;
101 uint64_t index;
102};
103
Daniel De Graaf243082e2011-11-28 11:49:11 -0500104struct gntalloc_vma_private_data {
105 struct gntalloc_gref *gref;
106 int users;
107 int count;
108};
109
Daniel De Graafdd314052011-02-07 17:23:05 -0500110static void __del_gref(struct gntalloc_gref *gref);
111
112static void do_cleanup(void)
113{
114 struct gntalloc_gref *gref, *n;
115 list_for_each_entry_safe(gref, n, &gref_list, next_gref) {
116 if (!gref->users)
117 __del_gref(gref);
118 }
119}
120
121static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
122 uint32_t *gref_ids, struct gntalloc_file_private_data *priv)
123{
124 int i, rc, readonly;
125 LIST_HEAD(queue_gref);
126 LIST_HEAD(queue_file);
David Vrabel5903c6b2014-09-02 15:21:30 +0100127 struct gntalloc_gref *gref, *next;
Daniel De Graafdd314052011-02-07 17:23:05 -0500128
129 readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
130 rc = -ENOMEM;
131 for (i = 0; i < op->count; i++) {
132 gref = kzalloc(sizeof(*gref), GFP_KERNEL);
133 if (!gref)
134 goto undo;
135 list_add_tail(&gref->next_gref, &queue_gref);
136 list_add_tail(&gref->next_file, &queue_file);
137 gref->users = 1;
138 gref->file_index = op->index + i * PAGE_SIZE;
139 gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
140 if (!gref->page)
141 goto undo;
142
143 /* Grant foreign access to the page. */
David Vrabele9de2e52014-09-02 15:21:29 +0100144 rc = gnttab_grant_foreign_access(op->domid,
Daniel De Graafdd314052011-02-07 17:23:05 -0500145 pfn_to_mfn(page_to_pfn(gref->page)), readonly);
David Vrabele9de2e52014-09-02 15:21:29 +0100146 if (rc < 0)
Daniel De Graafdd314052011-02-07 17:23:05 -0500147 goto undo;
David Vrabele9de2e52014-09-02 15:21:29 +0100148 gref_ids[i] = gref->gref_id = rc;
Daniel De Graafdd314052011-02-07 17:23:05 -0500149 }
150
151 /* Add to gref lists. */
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400152 mutex_lock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500153 list_splice_tail(&queue_gref, &gref_list);
154 list_splice_tail(&queue_file, &priv->list);
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400155 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500156
157 return 0;
158
159undo:
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400160 mutex_lock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500161 gref_size -= (op->count - i);
162
David Vrabel5903c6b2014-09-02 15:21:30 +0100163 list_for_each_entry_safe(gref, next, &queue_file, next_file) {
164 list_del(&gref->next_file);
Daniel De Graafdd314052011-02-07 17:23:05 -0500165 __del_gref(gref);
166 }
167
168 /* It's possible for the target domain to map the just-allocated grant
169 * references by blindly guessing their IDs; if this is done, then
170 * __del_gref will leave them in the queue_gref list. They need to be
171 * added to the global list so that we can free them when they are no
172 * longer referenced.
173 */
174 if (unlikely(!list_empty(&queue_gref)))
175 list_splice_tail(&queue_gref, &gref_list);
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400176 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500177 return rc;
178}
179
180static void __del_gref(struct gntalloc_gref *gref)
181{
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500182 if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
183 uint8_t *tmp = kmap(gref->page);
184 tmp[gref->notify.pgoff] = 0;
185 kunmap(gref->page);
186 }
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400187 if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500188 notify_remote_via_evtchn(gref->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400189 evtchn_put(gref->notify.event);
190 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500191
192 gref->notify.flags = 0;
193
David Vrabele9de2e52014-09-02 15:21:29 +0100194 if (gref->gref_id) {
Daniel De Graafdd314052011-02-07 17:23:05 -0500195 if (gnttab_query_foreign_access(gref->gref_id))
196 return;
197
198 if (!gnttab_end_foreign_access_ref(gref->gref_id, 0))
199 return;
Daniel De Graaf0105d2b2011-11-28 11:49:10 -0500200
201 gnttab_free_grant_reference(gref->gref_id);
Daniel De Graafdd314052011-02-07 17:23:05 -0500202 }
203
204 gref_size--;
205 list_del(&gref->next_gref);
206
207 if (gref->page)
208 __free_page(gref->page);
209
210 kfree(gref);
211}
212
213/* finds contiguous grant references in a file, returns the first */
214static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
215 uint64_t index, uint32_t count)
216{
217 struct gntalloc_gref *rv = NULL, *gref;
218 list_for_each_entry(gref, &priv->list, next_file) {
219 if (gref->file_index == index && !rv)
220 rv = gref;
221 if (rv) {
222 if (gref->file_index != index)
223 return NULL;
224 index += PAGE_SIZE;
225 count--;
226 if (count == 0)
227 return rv;
228 }
229 }
230 return NULL;
231}
232
233/*
234 * -------------------------------------
235 * File operations.
236 * -------------------------------------
237 */
238static int gntalloc_open(struct inode *inode, struct file *filp)
239{
240 struct gntalloc_file_private_data *priv;
241
242 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
243 if (!priv)
244 goto out_nomem;
245 INIT_LIST_HEAD(&priv->list);
246
247 filp->private_data = priv;
248
249 pr_debug("%s: priv %p\n", __func__, priv);
250
251 return 0;
252
253out_nomem:
254 return -ENOMEM;
255}
256
257static int gntalloc_release(struct inode *inode, struct file *filp)
258{
259 struct gntalloc_file_private_data *priv = filp->private_data;
260 struct gntalloc_gref *gref;
261
262 pr_debug("%s: priv %p\n", __func__, priv);
263
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400264 mutex_lock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500265 while (!list_empty(&priv->list)) {
266 gref = list_entry(priv->list.next,
267 struct gntalloc_gref, next_file);
268 list_del(&gref->next_file);
269 gref->users--;
270 if (gref->users == 0)
271 __del_gref(gref);
272 }
273 kfree(priv);
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400274 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500275
276 return 0;
277}
278
279static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
280 struct ioctl_gntalloc_alloc_gref __user *arg)
281{
282 int rc = 0;
283 struct ioctl_gntalloc_alloc_gref op;
284 uint32_t *gref_ids;
285
286 pr_debug("%s: priv %p\n", __func__, priv);
287
288 if (copy_from_user(&op, arg, sizeof(op))) {
289 rc = -EFAULT;
290 goto out;
291 }
292
Dan Carpenter21643e62011-11-04 21:24:08 +0300293 gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY);
Daniel De Graafdd314052011-02-07 17:23:05 -0500294 if (!gref_ids) {
295 rc = -ENOMEM;
296 goto out;
297 }
298
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400299 mutex_lock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500300 /* Clean up pages that were at zero (local) users but were still mapped
301 * by remote domains. Since those pages count towards the limit that we
302 * are about to enforce, removing them here is a good idea.
303 */
304 do_cleanup();
305 if (gref_size + op.count > limit) {
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400306 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500307 rc = -ENOSPC;
308 goto out_free;
309 }
310 gref_size += op.count;
311 op.index = priv->index;
312 priv->index += op.count * PAGE_SIZE;
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400313 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500314
315 rc = add_grefs(&op, gref_ids, priv);
316 if (rc < 0)
317 goto out_free;
318
319 /* Once we finish add_grefs, it is unsafe to touch the new reference,
320 * since it is possible for a concurrent ioctl to remove it (by guessing
321 * its index). If the userspace application doesn't provide valid memory
322 * to write the IDs to, then it will need to close the file in order to
323 * release - which it will do by segfaulting when it tries to access the
324 * IDs to close them.
325 */
326 if (copy_to_user(arg, &op, sizeof(op))) {
327 rc = -EFAULT;
328 goto out_free;
329 }
330 if (copy_to_user(arg->gref_ids, gref_ids,
331 sizeof(gref_ids[0]) * op.count)) {
332 rc = -EFAULT;
333 goto out_free;
334 }
335
336out_free:
337 kfree(gref_ids);
338out:
339 return rc;
340}
341
342static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
343 void __user *arg)
344{
345 int i, rc = 0;
346 struct ioctl_gntalloc_dealloc_gref op;
347 struct gntalloc_gref *gref, *n;
348
349 pr_debug("%s: priv %p\n", __func__, priv);
350
351 if (copy_from_user(&op, arg, sizeof(op))) {
352 rc = -EFAULT;
353 goto dealloc_grant_out;
354 }
355
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400356 mutex_lock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500357 gref = find_grefs(priv, op.index, op.count);
358 if (gref) {
359 /* Remove from the file list only, and decrease reference count.
360 * The later call to do_cleanup() will remove from gref_list and
361 * free the memory if the pages aren't mapped anywhere.
362 */
363 for (i = 0; i < op.count; i++) {
364 n = list_entry(gref->next_file.next,
365 struct gntalloc_gref, next_file);
366 list_del(&gref->next_file);
367 gref->users--;
368 gref = n;
369 }
370 } else {
371 rc = -EINVAL;
372 }
373
374 do_cleanup();
375
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400376 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500377dealloc_grant_out:
378 return rc;
379}
380
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500381static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
382 void __user *arg)
383{
384 struct ioctl_gntalloc_unmap_notify op;
385 struct gntalloc_gref *gref;
386 uint64_t index;
387 int pgoff;
388 int rc;
389
390 if (copy_from_user(&op, arg, sizeof(op)))
391 return -EFAULT;
392
393 index = op.index & ~(PAGE_SIZE - 1);
394 pgoff = op.index & (PAGE_SIZE - 1);
395
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400396 mutex_lock(&gref_mutex);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500397
398 gref = find_grefs(priv, index, 1);
399 if (!gref) {
400 rc = -ENOENT;
401 goto unlock_out;
402 }
403
404 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
405 rc = -EINVAL;
406 goto unlock_out;
407 }
408
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400409 /* We need to grab a reference to the event channel we are going to use
410 * to send the notify before releasing the reference we may already have
411 * (if someone has called this ioctl twice). This is required so that
412 * it is possible to change the clear_byte part of the notification
413 * without disturbing the event channel part, which may now be the last
414 * reference to that event channel.
415 */
416 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
417 if (evtchn_get(op.event_channel_port)) {
418 rc = -EINVAL;
419 goto unlock_out;
420 }
421 }
422
423 if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
424 evtchn_put(gref->notify.event);
425
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500426 gref->notify.flags = op.action;
427 gref->notify.pgoff = pgoff;
428 gref->notify.event = op.event_channel_port;
429 rc = 0;
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400430
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500431 unlock_out:
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400432 mutex_unlock(&gref_mutex);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500433 return rc;
434}
435
Daniel De Graafdd314052011-02-07 17:23:05 -0500436static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
437 unsigned long arg)
438{
439 struct gntalloc_file_private_data *priv = filp->private_data;
440
441 switch (cmd) {
442 case IOCTL_GNTALLOC_ALLOC_GREF:
443 return gntalloc_ioctl_alloc(priv, (void __user *)arg);
444
445 case IOCTL_GNTALLOC_DEALLOC_GREF:
446 return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
447
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500448 case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
449 return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
450
Daniel De Graafdd314052011-02-07 17:23:05 -0500451 default:
452 return -ENOIOCTLCMD;
453 }
454
455 return 0;
456}
457
Daniel De Graafd79647a2011-03-07 15:18:57 -0500458static void gntalloc_vma_open(struct vm_area_struct *vma)
459{
Daniel De Graaf243082e2011-11-28 11:49:11 -0500460 struct gntalloc_vma_private_data *priv = vma->vm_private_data;
461
462 if (!priv)
Daniel De Graafd79647a2011-03-07 15:18:57 -0500463 return;
464
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400465 mutex_lock(&gref_mutex);
Daniel De Graaf243082e2011-11-28 11:49:11 -0500466 priv->users++;
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400467 mutex_unlock(&gref_mutex);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500468}
469
Daniel De Graafdd314052011-02-07 17:23:05 -0500470static void gntalloc_vma_close(struct vm_area_struct *vma)
471{
Daniel De Graaf243082e2011-11-28 11:49:11 -0500472 struct gntalloc_vma_private_data *priv = vma->vm_private_data;
473 struct gntalloc_gref *gref, *next;
474 int i;
475
476 if (!priv)
Daniel De Graafdd314052011-02-07 17:23:05 -0500477 return;
478
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400479 mutex_lock(&gref_mutex);
Daniel De Graaf243082e2011-11-28 11:49:11 -0500480 priv->users--;
481 if (priv->users == 0) {
482 gref = priv->gref;
483 for (i = 0; i < priv->count; i++) {
484 gref->users--;
485 next = list_entry(gref->next_gref.next,
486 struct gntalloc_gref, next_gref);
487 if (gref->users == 0)
488 __del_gref(gref);
489 gref = next;
490 }
491 kfree(priv);
492 }
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400493 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500494}
495
496static struct vm_operations_struct gntalloc_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500497 .open = gntalloc_vma_open,
Daniel De Graafdd314052011-02-07 17:23:05 -0500498 .close = gntalloc_vma_close,
499};
500
501static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
502{
503 struct gntalloc_file_private_data *priv = filp->private_data;
Daniel De Graaf243082e2011-11-28 11:49:11 -0500504 struct gntalloc_vma_private_data *vm_priv;
Daniel De Graafdd314052011-02-07 17:23:05 -0500505 struct gntalloc_gref *gref;
506 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
507 int rv, i;
508
Daniel De Graafdd314052011-02-07 17:23:05 -0500509 if (!(vma->vm_flags & VM_SHARED)) {
Joe Perches283c0972013-06-28 03:21:41 -0700510 pr_err("%s: Mapping must be shared\n", __func__);
Daniel De Graafdd314052011-02-07 17:23:05 -0500511 return -EINVAL;
512 }
513
Daniel De Graaf243082e2011-11-28 11:49:11 -0500514 vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL);
515 if (!vm_priv)
516 return -ENOMEM;
517
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400518 mutex_lock(&gref_mutex);
Daniel De Graaf243082e2011-11-28 11:49:11 -0500519
520 pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__,
521 priv, vm_priv, vma->vm_pgoff, count);
522
Daniel De Graafdd314052011-02-07 17:23:05 -0500523 gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
524 if (gref == NULL) {
525 rv = -ENOENT;
526 pr_debug("%s: Could not find grant reference",
527 __func__);
Julia Lawall2e163412011-12-23 18:39:29 +0100528 kfree(vm_priv);
Daniel De Graafdd314052011-02-07 17:23:05 -0500529 goto out_unlock;
530 }
531
Daniel De Graaf243082e2011-11-28 11:49:11 -0500532 vm_priv->gref = gref;
533 vm_priv->users = 1;
534 vm_priv->count = count;
Daniel De Graafdd314052011-02-07 17:23:05 -0500535
Daniel De Graaf243082e2011-11-28 11:49:11 -0500536 vma->vm_private_data = vm_priv;
537
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700538 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Daniel De Graafdd314052011-02-07 17:23:05 -0500539
540 vma->vm_ops = &gntalloc_vmops;
541
542 for (i = 0; i < count; i++) {
543 gref->users++;
544 rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
545 gref->page);
546 if (rv)
547 goto out_unlock;
548
549 gref = list_entry(gref->next_file.next,
550 struct gntalloc_gref, next_file);
551 }
552 rv = 0;
553
554out_unlock:
Daniel De Graaf8ca19a82011-10-27 17:58:48 -0400555 mutex_unlock(&gref_mutex);
Daniel De Graafdd314052011-02-07 17:23:05 -0500556 return rv;
557}
558
559static const struct file_operations gntalloc_fops = {
560 .owner = THIS_MODULE,
561 .open = gntalloc_open,
562 .release = gntalloc_release,
563 .unlocked_ioctl = gntalloc_ioctl,
564 .mmap = gntalloc_mmap
565};
566
567/*
568 * -------------------------------------
569 * Module creation/destruction.
570 * -------------------------------------
571 */
572static struct miscdevice gntalloc_miscdev = {
573 .minor = MISC_DYNAMIC_MINOR,
574 .name = "xen/gntalloc",
575 .fops = &gntalloc_fops,
576};
577
578static int __init gntalloc_init(void)
579{
580 int err;
581
582 if (!xen_domain())
583 return -ENODEV;
584
585 err = misc_register(&gntalloc_miscdev);
586 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -0700587 pr_err("Could not register misc gntalloc device\n");
Daniel De Graafdd314052011-02-07 17:23:05 -0500588 return err;
589 }
590
591 pr_debug("Created grant allocation device at %d,%d\n",
592 MISC_MAJOR, gntalloc_miscdev.minor);
593
594 return 0;
595}
596
597static void __exit gntalloc_exit(void)
598{
599 misc_deregister(&gntalloc_miscdev);
600}
601
602module_init(gntalloc_init);
603module_exit(gntalloc_exit);
604
605MODULE_LICENSE("GPL");
606MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
607 "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
608MODULE_DESCRIPTION("User-space grant reference allocator driver");