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