1 /******************************************************************************
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
51 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
52
53 #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>
65 #include <linux/highmem.h>
66
67 #include <xen/xen.h>
68 #include <xen/page.h>
69 #include <xen/grant_table.h>
70 #include <xen/gntalloc.h>
71 #include <xen/events.h>
72
73 static unsigned int limit = 1024;
74 module_param(limit, uint, 0644);
75 MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
76 "the gntalloc device");
77
78 static LIST_HEAD(gref_list);
79 static DEFINE_MUTEX(gref_mutex);
80 static unsigned int gref_size;
81
82 struct 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
88 /* Metadata on a grant reference. */
89 struct 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 */
96 struct notify_info notify; /* Unmap notification */
97 };
98
99 struct gntalloc_file_private_data {
100 struct list_head list;
101 uint64_t index;
102 };
103
104 struct gntalloc_vma_private_data {
105 struct gntalloc_gref *gref;
106 int users;
107 int count;
108 };
109
110 static void __del_gref(struct gntalloc_gref *gref);
111
do_cleanup(void)112 static 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
add_grefs(struct ioctl_gntalloc_alloc_gref * op,uint32_t * gref_ids,struct gntalloc_file_private_data * priv)121 static 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);
127 struct gntalloc_gref *gref, *next;
128
129 readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
130 for (i = 0; i < op->count; i++) {
131 gref = kzalloc_obj(*gref);
132 if (!gref) {
133 rc = -ENOMEM;
134 goto undo;
135 }
136 list_add_tail(&gref->next_gref, &queue_gref);
137 list_add_tail(&gref->next_file, &queue_file);
138 gref->users = 1;
139 gref->file_index = op->index + i * PAGE_SIZE;
140 gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
141 if (!gref->page) {
142 rc = -ENOMEM;
143 goto undo;
144 }
145
146 /* Grant foreign access to the page. */
147 rc = gnttab_grant_foreign_access(op->domid,
148 xen_page_to_gfn(gref->page),
149 readonly);
150 if (rc < 0)
151 goto undo;
152 gref_ids[i] = gref->gref_id = rc;
153 }
154
155 /* Add to gref lists. */
156 mutex_lock(&gref_mutex);
157 list_splice_tail(&queue_gref, &gref_list);
158 list_splice_tail(&queue_file, &priv->list);
159 mutex_unlock(&gref_mutex);
160
161 return 0;
162
163 undo:
164 mutex_lock(&gref_mutex);
165 gref_size -= (op->count - i);
166
167 list_for_each_entry_safe(gref, next, &queue_file, next_file) {
168 list_del(&gref->next_file);
169 __del_gref(gref);
170 }
171
172 mutex_unlock(&gref_mutex);
173 return rc;
174 }
175
__del_gref(struct gntalloc_gref * gref)176 static void __del_gref(struct gntalloc_gref *gref)
177 {
178 if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
179 uint8_t *tmp = kmap_local_page(gref->page);
180 tmp[gref->notify.pgoff] = 0;
181 kunmap_local(tmp);
182 }
183 if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
184 notify_remote_via_evtchn(gref->notify.event);
185 evtchn_put(gref->notify.event);
186 }
187
188 gref->notify.flags = 0;
189
190 if (gref->gref_id) {
191 if (gref->page)
192 gnttab_end_foreign_access(gref->gref_id, gref->page);
193 else
194 gnttab_free_grant_reference(gref->gref_id);
195 }
196
197 gref_size--;
198 list_del(&gref->next_gref);
199
200 kfree(gref);
201 }
202
203 /* finds contiguous grant references in a file, returns the first */
find_grefs(struct gntalloc_file_private_data * priv,uint64_t index,uint32_t count)204 static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
205 uint64_t index, uint32_t count)
206 {
207 struct gntalloc_gref *rv = NULL, *gref;
208 list_for_each_entry(gref, &priv->list, next_file) {
209 if (gref->file_index == index && !rv)
210 rv = gref;
211 if (rv) {
212 if (gref->file_index != index)
213 return NULL;
214 index += PAGE_SIZE;
215 count--;
216 if (count == 0)
217 return rv;
218 }
219 }
220 return NULL;
221 }
222
223 /*
224 * -------------------------------------
225 * File operations.
226 * -------------------------------------
227 */
gntalloc_open(struct inode * inode,struct file * filp)228 static int gntalloc_open(struct inode *inode, struct file *filp)
229 {
230 struct gntalloc_file_private_data *priv;
231
232 priv = kzalloc_obj(*priv);
233 if (!priv)
234 goto out_nomem;
235 INIT_LIST_HEAD(&priv->list);
236
237 filp->private_data = priv;
238
239 pr_debug("%s: priv %p\n", __func__, priv);
240
241 return 0;
242
243 out_nomem:
244 return -ENOMEM;
245 }
246
gntalloc_release(struct inode * inode,struct file * filp)247 static int gntalloc_release(struct inode *inode, struct file *filp)
248 {
249 struct gntalloc_file_private_data *priv = filp->private_data;
250 struct gntalloc_gref *gref;
251
252 pr_debug("%s: priv %p\n", __func__, priv);
253
254 mutex_lock(&gref_mutex);
255 while (!list_empty(&priv->list)) {
256 gref = list_entry(priv->list.next,
257 struct gntalloc_gref, next_file);
258 list_del(&gref->next_file);
259 gref->users--;
260 if (gref->users == 0)
261 __del_gref(gref);
262 }
263 kfree(priv);
264 mutex_unlock(&gref_mutex);
265
266 return 0;
267 }
268
gntalloc_ioctl_alloc(struct gntalloc_file_private_data * priv,struct ioctl_gntalloc_alloc_gref __user * arg)269 static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
270 struct ioctl_gntalloc_alloc_gref __user *arg)
271 {
272 int rc = 0;
273 struct ioctl_gntalloc_alloc_gref op;
274 uint32_t *gref_ids;
275 unsigned int limit_snapshot;
276
277 pr_debug("%s: priv %p\n", __func__, priv);
278
279 if (copy_from_user(&op, arg, sizeof(op))) {
280 rc = -EFAULT;
281 goto out;
282 }
283
284 limit_snapshot = READ_ONCE(limit);
285 if (op.count > limit_snapshot) {
286 rc = -ENOSPC;
287 goto out;
288 }
289
290 gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_KERNEL);
291 if (!gref_ids) {
292 rc = -ENOMEM;
293 goto out;
294 }
295
296 mutex_lock(&gref_mutex);
297 /* Clean up pages that were at zero (local) users but were still mapped
298 * by remote domains. Since those pages count towards the limit that we
299 * are about to enforce, removing them here is a good idea.
300 */
301 do_cleanup();
302 limit_snapshot = READ_ONCE(limit);
303 if (gref_size > limit_snapshot ||
304 op.count > limit_snapshot - gref_size) {
305 mutex_unlock(&gref_mutex);
306 rc = -ENOSPC;
307 goto out_free;
308 }
309 gref_size += op.count;
310 op.index = priv->index;
311 priv->index += (uint64_t)op.count * PAGE_SIZE;
312 mutex_unlock(&gref_mutex);
313
314 rc = add_grefs(&op, gref_ids, priv);
315 if (rc < 0)
316 goto out_free;
317
318 /* Once we finish add_grefs, it is unsafe to touch the new reference,
319 * since it is possible for a concurrent ioctl to remove it (by guessing
320 * its index). If the userspace application doesn't provide valid memory
321 * to write the IDs to, then it will need to close the file in order to
322 * release - which it will do by segfaulting when it tries to access the
323 * IDs to close them.
324 */
325 if (copy_to_user(arg, &op, sizeof(op))) {
326 rc = -EFAULT;
327 goto out_free;
328 }
329 if (copy_to_user(arg->gref_ids_flex, gref_ids,
330 sizeof(gref_ids[0]) * op.count)) {
331 rc = -EFAULT;
332 goto out_free;
333 }
334
335 out_free:
336 kfree(gref_ids);
337 out:
338 return rc;
339 }
340
gntalloc_ioctl_dealloc(struct gntalloc_file_private_data * priv,void __user * arg)341 static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
342 void __user *arg)
343 {
344 int i, rc = 0;
345 struct ioctl_gntalloc_dealloc_gref op;
346 struct gntalloc_gref *gref, *n;
347
348 pr_debug("%s: priv %p\n", __func__, priv);
349
350 if (copy_from_user(&op, arg, sizeof(op))) {
351 rc = -EFAULT;
352 goto dealloc_grant_out;
353 }
354
355 mutex_lock(&gref_mutex);
356 gref = find_grefs(priv, op.index, op.count);
357 if (gref) {
358 /* Remove from the file list only, and decrease reference count.
359 * The later call to do_cleanup() will remove from gref_list and
360 * free the memory if the pages aren't mapped anywhere.
361 */
362 for (i = 0; i < op.count; i++) {
363 n = list_entry(gref->next_file.next,
364 struct gntalloc_gref, next_file);
365 list_del(&gref->next_file);
366 gref->users--;
367 gref = n;
368 }
369 } else {
370 rc = -EINVAL;
371 }
372
373 do_cleanup();
374
375 mutex_unlock(&gref_mutex);
376 dealloc_grant_out:
377 return rc;
378 }
379
gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data * priv,void __user * arg)380 static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
381 void __user *arg)
382 {
383 struct ioctl_gntalloc_unmap_notify op;
384 struct gntalloc_gref *gref;
385 uint64_t index;
386 int pgoff;
387 int rc;
388
389 if (copy_from_user(&op, arg, sizeof(op)))
390 return -EFAULT;
391
392 index = op.index & ~(PAGE_SIZE - 1);
393 pgoff = op.index & (PAGE_SIZE - 1);
394
395 mutex_lock(&gref_mutex);
396
397 gref = find_grefs(priv, index, 1);
398 if (!gref) {
399 rc = -ENOENT;
400 goto unlock_out;
401 }
402
403 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
404 rc = -EINVAL;
405 goto unlock_out;
406 }
407
408 /* We need to grab a reference to the event channel we are going to use
409 * to send the notify before releasing the reference we may already have
410 * (if someone has called this ioctl twice). This is required so that
411 * it is possible to change the clear_byte part of the notification
412 * without disturbing the event channel part, which may now be the last
413 * reference to that event channel.
414 */
415 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
416 if (evtchn_get(op.event_channel_port)) {
417 rc = -EINVAL;
418 goto unlock_out;
419 }
420 }
421
422 if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
423 evtchn_put(gref->notify.event);
424
425 gref->notify.flags = op.action;
426 gref->notify.pgoff = pgoff;
427 gref->notify.event = op.event_channel_port;
428 rc = 0;
429
430 unlock_out:
431 mutex_unlock(&gref_mutex);
432 return rc;
433 }
434
gntalloc_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)435 static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
436 unsigned long arg)
437 {
438 struct gntalloc_file_private_data *priv = filp->private_data;
439
440 switch (cmd) {
441 case IOCTL_GNTALLOC_ALLOC_GREF:
442 return gntalloc_ioctl_alloc(priv, (void __user *)arg);
443
444 case IOCTL_GNTALLOC_DEALLOC_GREF:
445 return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
446
447 case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
448 return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
449
450 default:
451 return -ENOIOCTLCMD;
452 }
453
454 return 0;
455 }
456
gntalloc_vma_open(struct vm_area_struct * vma)457 static void gntalloc_vma_open(struct vm_area_struct *vma)
458 {
459 struct gntalloc_vma_private_data *priv = vma->vm_private_data;
460
461 if (!priv)
462 return;
463
464 mutex_lock(&gref_mutex);
465 priv->users++;
466 mutex_unlock(&gref_mutex);
467 }
468
gntalloc_vma_close(struct vm_area_struct * vma)469 static void gntalloc_vma_close(struct vm_area_struct *vma)
470 {
471 struct gntalloc_vma_private_data *priv = vma->vm_private_data;
472 struct gntalloc_gref *gref, *next;
473 int i;
474
475 if (!priv)
476 return;
477
478 mutex_lock(&gref_mutex);
479 priv->users--;
480 if (priv->users == 0) {
481 gref = priv->gref;
482 for (i = 0; i < priv->count; i++) {
483 gref->users--;
484 next = list_entry(gref->next_gref.next,
485 struct gntalloc_gref, next_gref);
486 if (gref->users == 0)
487 __del_gref(gref);
488 gref = next;
489 }
490 kfree(priv);
491 }
492 mutex_unlock(&gref_mutex);
493 }
494
495 static const struct vm_operations_struct gntalloc_vmops = {
496 .open = gntalloc_vma_open,
497 .close = gntalloc_vma_close,
498 };
499
gntalloc_mmap(struct file * filp,struct vm_area_struct * vma)500 static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
501 {
502 struct gntalloc_file_private_data *priv = filp->private_data;
503 struct gntalloc_vma_private_data *vm_priv;
504 struct gntalloc_gref *gref;
505 int count = vma_pages(vma);
506 int rv, i;
507
508 if (!(vma->vm_flags & VM_SHARED)) {
509 pr_err("%s: Mapping must be shared\n", __func__);
510 return -EINVAL;
511 }
512
513 vm_priv = kmalloc_obj(*vm_priv);
514 if (!vm_priv)
515 return -ENOMEM;
516
517 mutex_lock(&gref_mutex);
518
519 pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__,
520 priv, vm_priv, vma->vm_pgoff, count);
521
522 gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
523 if (gref == NULL) {
524 rv = -ENOENT;
525 pr_debug("%s: Could not find grant reference",
526 __func__);
527 kfree(vm_priv);
528 goto out_unlock;
529 }
530
531 vm_priv->gref = gref;
532 vm_priv->users = 1;
533 vm_priv->count = count;
534
535 vma->vm_private_data = vm_priv;
536
537 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
538
539 vma->vm_ops = &gntalloc_vmops;
540
541 for (i = 0; i < count; i++) {
542 gref->users++;
543 rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
544 gref->page);
545 if (rv)
546 goto out_unlock;
547
548 gref = list_entry(gref->next_file.next,
549 struct gntalloc_gref, next_file);
550 }
551 rv = 0;
552
553 out_unlock:
554 mutex_unlock(&gref_mutex);
555 return rv;
556 }
557
558 static const struct file_operations gntalloc_fops = {
559 .owner = THIS_MODULE,
560 .open = gntalloc_open,
561 .release = gntalloc_release,
562 .unlocked_ioctl = gntalloc_ioctl,
563 .mmap = gntalloc_mmap
564 };
565
566 /*
567 * -------------------------------------
568 * Module creation/destruction.
569 * -------------------------------------
570 */
571 static struct miscdevice gntalloc_miscdev = {
572 .minor = MISC_DYNAMIC_MINOR,
573 .name = "xen/gntalloc",
574 .fops = &gntalloc_fops,
575 };
576
gntalloc_init(void)577 static int __init gntalloc_init(void)
578 {
579 int err;
580
581 if (!xen_domain())
582 return -ENODEV;
583
584 err = misc_register(&gntalloc_miscdev);
585 if (err != 0) {
586 pr_err("Could not register misc gntalloc device\n");
587 return err;
588 }
589
590 pr_debug("Created grant allocation device at %d,%d\n",
591 MISC_MAJOR, gntalloc_miscdev.minor);
592
593 return 0;
594 }
595
gntalloc_exit(void)596 static void __exit gntalloc_exit(void)
597 {
598 misc_deregister(&gntalloc_miscdev);
599 }
600
601 module_init(gntalloc_init);
602 module_exit(gntalloc_exit);
603
604 MODULE_LICENSE("GPL");
605 MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
606 "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
607 MODULE_DESCRIPTION("User-space grant reference allocator driver");
608