xen-blkback: safely unmap grants in case they are still in use
[linux-2.6-block.git] / drivers / xen / gntdev.c
CommitLineData
ab31523c
GH
1/******************************************************************************
2 * gntdev.c
3 *
4 * Device for accessing (in user-space) pages that have been granted by other
5 * domains.
6 *
7 * Copyright (c) 2006-2007, D G Murray.
8 * (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#undef DEBUG
21
283c0972
JP
22#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
23
ab31523c
GH
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/miscdevice.h>
28#include <linux/fs.h>
29#include <linux/mm.h>
30#include <linux/mman.h>
31#include <linux/mmu_notifier.h>
32#include <linux/types.h>
33#include <linux/uaccess.h>
34#include <linux/sched.h>
35#include <linux/spinlock.h>
36#include <linux/slab.h>
aab8f11a 37#include <linux/highmem.h>
ab31523c
GH
38
39#include <xen/xen.h>
40#include <xen/grant_table.h>
ca47ceaa 41#include <xen/balloon.h>
ab31523c 42#include <xen/gntdev.h>
bdc612dc 43#include <xen/events.h>
ab31523c
GH
44#include <asm/xen/hypervisor.h>
45#include <asm/xen/hypercall.h>
46#include <asm/xen/page.h>
47
48MODULE_LICENSE("GPL");
49MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
50 "Gerd Hoffmann <kraxel@redhat.com>");
51MODULE_DESCRIPTION("User-space granted page access driver");
52
ef91082e 53static int limit = 1024*1024;
ab31523c 54module_param(limit, int, 0644);
ef91082e
DDG
55MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
56 "the gntdev device");
57
58static atomic_t pages_mapped = ATOMIC_INIT(0);
ab31523c 59
aab8f11a 60static int use_ptemod;
16a1d022 61#define populate_freeable_maps use_ptemod
aab8f11a 62
ab31523c 63struct gntdev_priv {
16a1d022 64 /* maps with visible offsets in the file descriptor */
ab31523c 65 struct list_head maps;
16a1d022
DDG
66 /* maps that are not visible; will be freed on munmap.
67 * Only populated if populate_freeable_maps == 1 */
68 struct list_head freeable_maps;
69 /* lock protects maps and freeable_maps */
1401c00e 70 struct mutex lock;
ab31523c
GH
71 struct mm_struct *mm;
72 struct mmu_notifier mn;
73};
74
bdc612dc
DDG
75struct unmap_notify {
76 int flags;
77 /* Address relative to the start of the grant_map */
78 int addr;
79 int event;
80};
81
ab31523c
GH
82struct grant_map {
83 struct list_head next;
ab31523c
GH
84 struct vm_area_struct *vma;
85 int index;
86 int count;
87 int flags;
68b025c8 88 atomic_t users;
bdc612dc 89 struct unmap_notify notify;
ab31523c
GH
90 struct ioctl_gntdev_grant_ref *grants;
91 struct gnttab_map_grant_ref *map_ops;
92 struct gnttab_unmap_grant_ref *unmap_ops;
0930bba6 93 struct gnttab_map_grant_ref *kmap_ops;
853d0289 94 struct gnttab_unmap_grant_ref *kunmap_ops;
a12b4eb3 95 struct page **pages;
ab31523c
GH
96};
97
aab8f11a
DDG
98static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
99
ab31523c
GH
100/* ------------------------------------------------------------------ */
101
102static void gntdev_print_maps(struct gntdev_priv *priv,
103 char *text, int text_index)
104{
105#ifdef DEBUG
106 struct grant_map *map;
107
ef91082e 108 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
ab31523c
GH
109 list_for_each_entry(map, &priv->maps, next)
110 pr_debug(" index %2d, count %2d %s\n",
111 map->index, map->count,
112 map->index == text_index && text ? text : "");
113#endif
114}
115
a67baeb7
DV
116static void gntdev_free_map(struct grant_map *map)
117{
118 if (map == NULL)
119 return;
120
121 if (map->pages)
ff4b156f 122 gnttab_free_pages(map->count, map->pages);
a67baeb7
DV
123 kfree(map->pages);
124 kfree(map->grants);
125 kfree(map->map_ops);
126 kfree(map->unmap_ops);
127 kfree(map->kmap_ops);
853d0289 128 kfree(map->kunmap_ops);
a67baeb7
DV
129 kfree(map);
130}
131
ab31523c
GH
132static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
133{
134 struct grant_map *add;
a12b4eb3 135 int i;
ab31523c
GH
136
137 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
138 if (NULL == add)
139 return NULL;
140
fc6e0c3b
DC
141 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
142 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
143 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
144 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
853d0289 145 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
fc6e0c3b 146 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
a12b4eb3
SS
147 if (NULL == add->grants ||
148 NULL == add->map_ops ||
149 NULL == add->unmap_ops ||
0930bba6 150 NULL == add->kmap_ops ||
853d0289 151 NULL == add->kunmap_ops ||
a12b4eb3 152 NULL == add->pages)
ab31523c
GH
153 goto err;
154
ff4b156f 155 if (gnttab_alloc_pages(count, add->pages))
ca47ceaa
DDG
156 goto err;
157
a12b4eb3 158 for (i = 0; i < count; i++) {
77c35acb
DDG
159 add->map_ops[i].handle = -1;
160 add->unmap_ops[i].handle = -1;
0930bba6 161 add->kmap_ops[i].handle = -1;
853d0289 162 add->kunmap_ops[i].handle = -1;
a12b4eb3
SS
163 }
164
ab31523c
GH
165 add->index = 0;
166 add->count = count;
68b025c8 167 atomic_set(&add->users, 1);
ab31523c 168
ab31523c
GH
169 return add;
170
171err:
a67baeb7 172 gntdev_free_map(add);
ab31523c
GH
173 return NULL;
174}
175
176static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
177{
178 struct grant_map *map;
179
180 list_for_each_entry(map, &priv->maps, next) {
181 if (add->index + add->count < map->index) {
182 list_add_tail(&add->next, &map->next);
183 goto done;
184 }
185 add->index = map->index + map->count;
186 }
187 list_add_tail(&add->next, &priv->maps);
188
189done:
ab31523c
GH
190 gntdev_print_maps(priv, "[new]", add->index);
191}
192
193static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
194 int index, int count)
195{
196 struct grant_map *map;
197
198 list_for_each_entry(map, &priv->maps, next) {
199 if (map->index != index)
200 continue;
bdc612dc 201 if (count && map->count != count)
ab31523c
GH
202 continue;
203 return map;
204 }
205 return NULL;
206}
207
16a1d022 208static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
ab31523c
GH
209{
210 if (!map)
211 return;
a12b4eb3 212
68b025c8
DDG
213 if (!atomic_dec_and_test(&map->users))
214 return;
215
216 atomic_sub(map->count, &pages_mapped);
217
0cc678f8 218 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
bdc612dc 219 notify_remote_via_evtchn(map->notify.event);
0cc678f8
DDG
220 evtchn_put(map->notify.event);
221 }
bdc612dc 222
16a1d022 223 if (populate_freeable_maps && priv) {
1401c00e 224 mutex_lock(&priv->lock);
16a1d022 225 list_del(&map->next);
1401c00e 226 mutex_unlock(&priv->lock);
16a1d022
DDG
227 }
228
a67baeb7
DV
229 if (map->pages && !use_ptemod)
230 unmap_grant_pages(map, 0, map->count);
231 gntdev_free_map(map);
ab31523c
GH
232}
233
234/* ------------------------------------------------------------------ */
235
236static int find_grant_ptes(pte_t *pte, pgtable_t token,
237 unsigned long addr, void *data)
238{
239 struct grant_map *map = data;
240 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
aab8f11a 241 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
ab31523c
GH
242 u64 pte_maddr;
243
244 BUG_ON(pgnr >= map->count);
ba5d1012
JF
245 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
246
aab8f11a 247 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
ab31523c
GH
248 map->grants[pgnr].ref,
249 map->grants[pgnr].domid);
aab8f11a 250 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
77c35acb 251 -1 /* handle */);
ab31523c
GH
252 return 0;
253}
254
255static int map_grant_pages(struct grant_map *map)
256{
257 int i, err = 0;
aab8f11a
DDG
258
259 if (!use_ptemod) {
12996fc3 260 /* Note: it could already be mapped */
77c35acb 261 if (map->map_ops[0].handle != -1)
12996fc3 262 return 0;
aab8f11a 263 for (i = 0; i < map->count; i++) {
38eaeb0f 264 unsigned long addr = (unsigned long)
aab8f11a
DDG
265 pfn_to_kaddr(page_to_pfn(map->pages[i]));
266 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
267 map->grants[i].ref,
268 map->grants[i].domid);
269 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
77c35acb 270 map->flags, -1 /* handle */);
aab8f11a 271 }
0930bba6
SS
272 } else {
273 /*
274 * Setup the map_ops corresponding to the pte entries pointing
275 * to the kernel linear addresses of the struct pages.
276 * These ptes are completely different from the user ptes dealt
277 * with find_grant_ptes.
278 */
279 for (i = 0; i < map->count; i++) {
0930bba6
SS
280 unsigned long address = (unsigned long)
281 pfn_to_kaddr(page_to_pfn(map->pages[i]));
0930bba6
SS
282 BUG_ON(PageHighMem(map->pages[i]));
283
ee072640
SS
284 gnttab_set_map_op(&map->kmap_ops[i], address,
285 map->flags | GNTMAP_host_map,
0930bba6
SS
286 map->grants[i].ref,
287 map->grants[i].domid);
853d0289
DV
288 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
289 map->flags | GNTMAP_host_map, -1);
0930bba6 290 }
aab8f11a 291 }
ab31523c
GH
292
293 pr_debug("map %d+%d\n", map->index, map->count);
e85fc980
KRW
294 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
295 map->pages, map->count);
ab31523c
GH
296 if (err)
297 return err;
298
299 for (i = 0; i < map->count; i++) {
853d0289 300 if (map->map_ops[i].status) {
ab31523c 301 err = -EINVAL;
853d0289 302 continue;
77c35acb 303 }
853d0289
DV
304
305 map->unmap_ops[i].handle = map->map_ops[i].handle;
306 if (use_ptemod)
307 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
ab31523c
GH
308 }
309 return err;
310}
311
74528225
JH
312struct unmap_grant_pages_callback_data
313{
314 struct completion completion;
315 int result;
316};
317
318static void unmap_grant_callback(int result,
319 struct gntab_unmap_queue_data *data)
320{
321 struct unmap_grant_pages_callback_data* d = data->data;
322
323 d->result = result;
324 complete(&d->completion);
325}
326
b57c1869 327static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
ab31523c
GH
328{
329 int i, err = 0;
74528225
JH
330 struct gntab_unmap_queue_data unmap_data;
331 struct unmap_grant_pages_callback_data data;
332
333 init_completion(&data.completion);
334 unmap_data.data = &data;
335 unmap_data.done= &unmap_grant_callback;
ab31523c 336
bdc612dc
DDG
337 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
338 int pgno = (map->notify.addr >> PAGE_SHIFT);
1affa98d
DDG
339 if (pgno >= offset && pgno < offset + pages) {
340 /* No need for kmap, pages are in lowmem */
341 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
bdc612dc 342 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
bdc612dc
DDG
343 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
344 }
345 }
346
74528225
JH
347 unmap_data.unmap_ops = map->unmap_ops + offset;
348 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
349 unmap_data.pages = map->pages + offset;
350 unmap_data.count = pages;
351
352 gnttab_unmap_refs_async(&unmap_data);
353
354 wait_for_completion(&data.completion);
355 if (data.result)
356 return data.result;
ab31523c
GH
357
358 for (i = 0; i < pages; i++) {
359 if (map->unmap_ops[offset+i].status)
360 err = -EINVAL;
77c35acb
DDG
361 pr_debug("unmap handle=%d st=%d\n",
362 map->unmap_ops[offset+i].handle,
363 map->unmap_ops[offset+i].status);
364 map->unmap_ops[offset+i].handle = -1;
ab31523c
GH
365 }
366 return err;
367}
368
b57c1869
DDG
369static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
370{
371 int range, err = 0;
372
373 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
374
375 /* It is possible the requested range will have a "hole" where we
376 * already unmapped some of the grants. Only unmap valid ranges.
377 */
378 while (pages && !err) {
77c35acb 379 while (pages && map->unmap_ops[offset].handle == -1) {
b57c1869
DDG
380 offset++;
381 pages--;
382 }
383 range = 0;
384 while (range < pages) {
77c35acb 385 if (map->unmap_ops[offset+range].handle == -1) {
b57c1869
DDG
386 range--;
387 break;
388 }
389 range++;
390 }
391 err = __unmap_grant_pages(map, offset, range);
392 offset += range;
393 pages -= range;
394 }
395
396 return err;
397}
398
ab31523c
GH
399/* ------------------------------------------------------------------ */
400
d79647ae
DDG
401static void gntdev_vma_open(struct vm_area_struct *vma)
402{
403 struct grant_map *map = vma->vm_private_data;
404
405 pr_debug("gntdev_vma_open %p\n", vma);
406 atomic_inc(&map->users);
407}
408
ab31523c
GH
409static void gntdev_vma_close(struct vm_area_struct *vma)
410{
411 struct grant_map *map = vma->vm_private_data;
16a1d022
DDG
412 struct file *file = vma->vm_file;
413 struct gntdev_priv *priv = file->private_data;
ab31523c 414
d79647ae 415 pr_debug("gntdev_vma_close %p\n", vma);
2512f298 416 if (use_ptemod) {
2512f298
DDG
417 /* It is possible that an mmu notifier could be running
418 * concurrently, so take priv->lock to ensure that the vma won't
419 * vanishing during the unmap_grant_pages call, since we will
420 * spin here until that completes. Such a concurrent call will
421 * not do any unmapping, since that has been done prior to
422 * closing the vma, but it may still iterate the unmap_ops list.
423 */
1401c00e 424 mutex_lock(&priv->lock);
2512f298 425 map->vma = NULL;
1401c00e 426 mutex_unlock(&priv->lock);
2512f298 427 }
ab31523c 428 vma->vm_private_data = NULL;
16a1d022 429 gntdev_put_map(priv, map);
ab31523c
GH
430}
431
ab31523c 432static struct vm_operations_struct gntdev_vmops = {
d79647ae 433 .open = gntdev_vma_open,
ab31523c 434 .close = gntdev_vma_close,
ab31523c
GH
435};
436
437/* ------------------------------------------------------------------ */
438
16a1d022
DDG
439static void unmap_if_in_range(struct grant_map *map,
440 unsigned long start, unsigned long end)
441{
442 unsigned long mstart, mend;
443 int err;
444
445 if (!map->vma)
446 return;
447 if (map->vma->vm_start >= end)
448 return;
449 if (map->vma->vm_end <= start)
450 return;
451 mstart = max(start, map->vma->vm_start);
452 mend = min(end, map->vma->vm_end);
453 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
454 map->index, map->count,
455 map->vma->vm_start, map->vma->vm_end,
456 start, end, mstart, mend);
457 err = unmap_grant_pages(map,
458 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
459 (mend - mstart) >> PAGE_SHIFT);
460 WARN_ON(err);
461}
462
ab31523c
GH
463static void mn_invl_range_start(struct mmu_notifier *mn,
464 struct mm_struct *mm,
465 unsigned long start, unsigned long end)
466{
467 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
468 struct grant_map *map;
ab31523c 469
1401c00e 470 mutex_lock(&priv->lock);
ab31523c 471 list_for_each_entry(map, &priv->maps, next) {
16a1d022
DDG
472 unmap_if_in_range(map, start, end);
473 }
474 list_for_each_entry(map, &priv->freeable_maps, next) {
475 unmap_if_in_range(map, start, end);
ab31523c 476 }
1401c00e 477 mutex_unlock(&priv->lock);
ab31523c
GH
478}
479
480static void mn_invl_page(struct mmu_notifier *mn,
481 struct mm_struct *mm,
482 unsigned long address)
483{
484 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
485}
486
487static void mn_release(struct mmu_notifier *mn,
488 struct mm_struct *mm)
489{
490 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
491 struct grant_map *map;
492 int err;
493
1401c00e 494 mutex_lock(&priv->lock);
ab31523c
GH
495 list_for_each_entry(map, &priv->maps, next) {
496 if (!map->vma)
497 continue;
498 pr_debug("map %d+%d (%lx %lx)\n",
499 map->index, map->count,
500 map->vma->vm_start, map->vma->vm_end);
501 err = unmap_grant_pages(map, /* offset */ 0, map->count);
502 WARN_ON(err);
503 }
16a1d022
DDG
504 list_for_each_entry(map, &priv->freeable_maps, next) {
505 if (!map->vma)
506 continue;
507 pr_debug("map %d+%d (%lx %lx)\n",
508 map->index, map->count,
509 map->vma->vm_start, map->vma->vm_end);
510 err = unmap_grant_pages(map, /* offset */ 0, map->count);
511 WARN_ON(err);
512 }
1401c00e 513 mutex_unlock(&priv->lock);
ab31523c
GH
514}
515
b8b0f559 516static struct mmu_notifier_ops gntdev_mmu_ops = {
ab31523c
GH
517 .release = mn_release,
518 .invalidate_page = mn_invl_page,
519 .invalidate_range_start = mn_invl_range_start,
520};
521
522/* ------------------------------------------------------------------ */
523
524static int gntdev_open(struct inode *inode, struct file *flip)
525{
526 struct gntdev_priv *priv;
527 int ret = 0;
528
529 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
530 if (!priv)
531 return -ENOMEM;
532
533 INIT_LIST_HEAD(&priv->maps);
16a1d022 534 INIT_LIST_HEAD(&priv->freeable_maps);
1401c00e 535 mutex_init(&priv->lock);
ab31523c 536
aab8f11a
DDG
537 if (use_ptemod) {
538 priv->mm = get_task_mm(current);
539 if (!priv->mm) {
540 kfree(priv);
541 return -ENOMEM;
542 }
543 priv->mn.ops = &gntdev_mmu_ops;
544 ret = mmu_notifier_register(&priv->mn, priv->mm);
545 mmput(priv->mm);
ab31523c 546 }
ab31523c
GH
547
548 if (ret) {
549 kfree(priv);
550 return ret;
551 }
552
553 flip->private_data = priv;
554 pr_debug("priv %p\n", priv);
555
556 return 0;
557}
558
559static int gntdev_release(struct inode *inode, struct file *flip)
560{
561 struct gntdev_priv *priv = flip->private_data;
562 struct grant_map *map;
ab31523c
GH
563
564 pr_debug("priv %p\n", priv);
565
ab31523c
GH
566 while (!list_empty(&priv->maps)) {
567 map = list_entry(priv->maps.next, struct grant_map, next);
68b025c8 568 list_del(&map->next);
16a1d022 569 gntdev_put_map(NULL /* already removed */, map);
ab31523c 570 }
16a1d022 571 WARN_ON(!list_empty(&priv->freeable_maps));
ab31523c 572
aab8f11a
DDG
573 if (use_ptemod)
574 mmu_notifier_unregister(&priv->mn, priv->mm);
ab31523c
GH
575 kfree(priv);
576 return 0;
577}
578
579static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
580 struct ioctl_gntdev_map_grant_ref __user *u)
581{
582 struct ioctl_gntdev_map_grant_ref op;
583 struct grant_map *map;
584 int err;
585
586 if (copy_from_user(&op, u, sizeof(op)) != 0)
587 return -EFAULT;
588 pr_debug("priv %p, add %d\n", priv, op.count);
589 if (unlikely(op.count <= 0))
590 return -EINVAL;
ab31523c
GH
591
592 err = -ENOMEM;
593 map = gntdev_alloc_map(priv, op.count);
594 if (!map)
595 return err;
ef91082e 596
68b025c8
DDG
597 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
598 pr_debug("can't map: over limit\n");
16a1d022 599 gntdev_put_map(NULL, map);
ab31523c
GH
600 return err;
601 }
602
68b025c8
DDG
603 if (copy_from_user(map->grants, &u->refs,
604 sizeof(map->grants[0]) * op.count) != 0) {
16a1d022
DDG
605 gntdev_put_map(NULL, map);
606 return -EFAULT;
ef91082e
DDG
607 }
608
1401c00e 609 mutex_lock(&priv->lock);
ab31523c
GH
610 gntdev_add_map(priv, map);
611 op.index = map->index << PAGE_SHIFT;
1401c00e 612 mutex_unlock(&priv->lock);
ab31523c 613
68b025c8
DDG
614 if (copy_to_user(u, &op, sizeof(op)) != 0)
615 return -EFAULT;
616
ab31523c
GH
617 return 0;
618}
619
620static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
621 struct ioctl_gntdev_unmap_grant_ref __user *u)
622{
623 struct ioctl_gntdev_unmap_grant_ref op;
624 struct grant_map *map;
625 int err = -ENOENT;
626
627 if (copy_from_user(&op, u, sizeof(op)) != 0)
628 return -EFAULT;
629 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
630
1401c00e 631 mutex_lock(&priv->lock);
ab31523c 632 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
68b025c8
DDG
633 if (map) {
634 list_del(&map->next);
16a1d022
DDG
635 if (populate_freeable_maps)
636 list_add_tail(&map->next, &priv->freeable_maps);
68b025c8
DDG
637 err = 0;
638 }
1401c00e 639 mutex_unlock(&priv->lock);
1f1503ba 640 if (map)
16a1d022 641 gntdev_put_map(priv, map);
ab31523c
GH
642 return err;
643}
644
645static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
646 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
647{
648 struct ioctl_gntdev_get_offset_for_vaddr op;
a879211b 649 struct vm_area_struct *vma;
ab31523c 650 struct grant_map *map;
2512f298 651 int rv = -EINVAL;
ab31523c
GH
652
653 if (copy_from_user(&op, u, sizeof(op)) != 0)
654 return -EFAULT;
655 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
656
2512f298 657 down_read(&current->mm->mmap_sem);
a879211b
DDG
658 vma = find_vma(current->mm, op.vaddr);
659 if (!vma || vma->vm_ops != &gntdev_vmops)
2512f298 660 goto out_unlock;
a879211b
DDG
661
662 map = vma->vm_private_data;
663 if (!map)
2512f298 664 goto out_unlock;
a879211b 665
ab31523c
GH
666 op.offset = map->index << PAGE_SHIFT;
667 op.count = map->count;
2512f298 668 rv = 0;
ab31523c 669
2512f298
DDG
670 out_unlock:
671 up_read(&current->mm->mmap_sem);
672
673 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
ab31523c 674 return -EFAULT;
2512f298 675 return rv;
ab31523c
GH
676}
677
bdc612dc
DDG
678static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
679{
680 struct ioctl_gntdev_unmap_notify op;
681 struct grant_map *map;
682 int rc;
0cc678f8
DDG
683 int out_flags;
684 unsigned int out_event;
bdc612dc
DDG
685
686 if (copy_from_user(&op, u, sizeof(op)))
687 return -EFAULT;
688
689 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
690 return -EINVAL;
691
0cc678f8
DDG
692 /* We need to grab a reference to the event channel we are going to use
693 * to send the notify before releasing the reference we may already have
694 * (if someone has called this ioctl twice). This is required so that
695 * it is possible to change the clear_byte part of the notification
696 * without disturbing the event channel part, which may now be the last
697 * reference to that event channel.
698 */
699 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
700 if (evtchn_get(op.event_channel_port))
701 return -EINVAL;
702 }
703
704 out_flags = op.action;
705 out_event = op.event_channel_port;
706
1401c00e 707 mutex_lock(&priv->lock);
bdc612dc
DDG
708
709 list_for_each_entry(map, &priv->maps, next) {
710 uint64_t begin = map->index << PAGE_SHIFT;
711 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
712 if (op.index >= begin && op.index < end)
713 goto found;
714 }
715 rc = -ENOENT;
716 goto unlock_out;
717
718 found:
9960be97
DDG
719 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
720 (map->flags & GNTMAP_readonly)) {
721 rc = -EINVAL;
722 goto unlock_out;
723 }
724
0cc678f8
DDG
725 out_flags = map->notify.flags;
726 out_event = map->notify.event;
727
bdc612dc
DDG
728 map->notify.flags = op.action;
729 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
730 map->notify.event = op.event_channel_port;
0cc678f8 731
bdc612dc 732 rc = 0;
0cc678f8 733
bdc612dc 734 unlock_out:
1401c00e 735 mutex_unlock(&priv->lock);
0cc678f8
DDG
736
737 /* Drop the reference to the event channel we did not save in the map */
738 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
739 evtchn_put(out_event);
740
bdc612dc
DDG
741 return rc;
742}
743
ab31523c
GH
744static long gntdev_ioctl(struct file *flip,
745 unsigned int cmd, unsigned long arg)
746{
747 struct gntdev_priv *priv = flip->private_data;
748 void __user *ptr = (void __user *)arg;
749
750 switch (cmd) {
751 case IOCTL_GNTDEV_MAP_GRANT_REF:
752 return gntdev_ioctl_map_grant_ref(priv, ptr);
753
754 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
755 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
756
757 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
758 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
759
bdc612dc
DDG
760 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
761 return gntdev_ioctl_notify(priv, ptr);
762
ab31523c
GH
763 default:
764 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
765 return -ENOIOCTLCMD;
766 }
767
768 return 0;
769}
770
771static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
772{
773 struct gntdev_priv *priv = flip->private_data;
774 int index = vma->vm_pgoff;
775 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
776 struct grant_map *map;
aab8f11a 777 int i, err = -EINVAL;
ab31523c
GH
778
779 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
780 return -EINVAL;
781
782 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
783 index, count, vma->vm_start, vma->vm_pgoff);
784
1401c00e 785 mutex_lock(&priv->lock);
ab31523c
GH
786 map = gntdev_find_map_index(priv, index, count);
787 if (!map)
788 goto unlock_out;
aab8f11a 789 if (use_ptemod && map->vma)
ab31523c 790 goto unlock_out;
aab8f11a 791 if (use_ptemod && priv->mm != vma->vm_mm) {
283c0972 792 pr_warn("Huh? Other mm?\n");
ab31523c
GH
793 goto unlock_out;
794 }
795
68b025c8
DDG
796 atomic_inc(&map->users);
797
ab31523c
GH
798 vma->vm_ops = &gntdev_vmops;
799
314e51b9 800 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
d79647ae
DDG
801
802 if (use_ptemod)
e8e937be 803 vma->vm_flags |= VM_DONTCOPY;
ab31523c
GH
804
805 vma->vm_private_data = map;
ab31523c 806
aab8f11a
DDG
807 if (use_ptemod)
808 map->vma = vma;
809
12996fc3
DDG
810 if (map->flags) {
811 if ((vma->vm_flags & VM_WRITE) &&
812 (map->flags & GNTMAP_readonly))
a93e20a8 813 goto out_unlock_put;
12996fc3
DDG
814 } else {
815 map->flags = GNTMAP_host_map;
816 if (!(vma->vm_flags & VM_WRITE))
817 map->flags |= GNTMAP_readonly;
818 }
ab31523c 819
1401c00e 820 mutex_unlock(&priv->lock);
f0a70c88 821
aab8f11a
DDG
822 if (use_ptemod) {
823 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
824 vma->vm_end - vma->vm_start,
825 find_grant_ptes, map);
826 if (err) {
283c0972 827 pr_warn("find_grant_ptes() failure.\n");
90b6f305 828 goto out_put_map;
aab8f11a 829 }
ab31523c
GH
830 }
831
832 err = map_grant_pages(map);
90b6f305
DDG
833 if (err)
834 goto out_put_map;
f0a70c88 835
aab8f11a
DDG
836 if (!use_ptemod) {
837 for (i = 0; i < count; i++) {
838 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
839 map->pages[i]);
840 if (err)
90b6f305 841 goto out_put_map;
aab8f11a
DDG
842 }
843 }
844
f0a70c88
DDG
845 return 0;
846
ab31523c 847unlock_out:
1401c00e 848 mutex_unlock(&priv->lock);
ab31523c 849 return err;
90b6f305 850
a93e20a8 851out_unlock_put:
1401c00e 852 mutex_unlock(&priv->lock);
90b6f305 853out_put_map:
84e4075d
DDG
854 if (use_ptemod)
855 map->vma = NULL;
16a1d022 856 gntdev_put_map(priv, map);
90b6f305 857 return err;
ab31523c
GH
858}
859
860static const struct file_operations gntdev_fops = {
861 .owner = THIS_MODULE,
862 .open = gntdev_open,
863 .release = gntdev_release,
864 .mmap = gntdev_mmap,
865 .unlocked_ioctl = gntdev_ioctl
866};
867
868static struct miscdevice gntdev_miscdev = {
869 .minor = MISC_DYNAMIC_MINOR,
870 .name = "xen/gntdev",
871 .fops = &gntdev_fops,
872};
873
874/* ------------------------------------------------------------------ */
875
876static int __init gntdev_init(void)
877{
878 int err;
879
880 if (!xen_domain())
881 return -ENODEV;
882
6926f6d6 883 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
aab8f11a 884
ab31523c
GH
885 err = misc_register(&gntdev_miscdev);
886 if (err != 0) {
283c0972 887 pr_err("Could not register gntdev device\n");
ab31523c
GH
888 return err;
889 }
890 return 0;
891}
892
893static void __exit gntdev_exit(void)
894{
895 misc_deregister(&gntdev_miscdev);
896}
897
898module_init(gntdev_init);
899module_exit(gntdev_exit);
900
901/* ------------------------------------------------------------------ */