blob: 173332f439d8a6f0b6fb388f49f057bad283b33b [file] [log] [blame]
Gerd Hoffmannab315232010-12-14 18:40:46 +00001/******************************************************************************
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
Joe Perches283c0972013-06-28 03:21:41 -070022#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
23
Gerd Hoffmannab315232010-12-14 18:40:46 +000024#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>
Ingo Molnar6e84f312017-02-08 18:51:29 +010035#include <linux/sched/mm.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000036#include <linux/spinlock.h>
37#include <linux/slab.h>
Daniel De Graafaab8f112011-02-03 12:19:02 -050038#include <linux/highmem.h>
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +020039#include <linux/refcount.h>
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030040#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
41#include <linux/of_device.h>
42#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +000043
44#include <xen/xen.h>
45#include <xen/grant_table.h>
Daniel De Graafca47cea2011-03-09 18:07:34 -050046#include <xen/balloon.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000047#include <xen/gntdev.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050048#include <xen/events.h>
Julien Gralla9fd60e2015-06-17 15:28:02 +010049#include <xen/page.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000050#include <asm/xen/hypervisor.h>
51#include <asm/xen/hypercall.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000052
53MODULE_LICENSE("GPL");
54MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
55 "Gerd Hoffmann <kraxel@redhat.com>");
56MODULE_DESCRIPTION("User-space granted page access driver");
57
Daniel De Graafef910822011-02-03 12:18:59 -050058static int limit = 1024*1024;
Gerd Hoffmannab315232010-12-14 18:40:46 +000059module_param(limit, int, 0644);
Daniel De Graafef910822011-02-03 12:18:59 -050060MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
61 "the gntdev device");
62
63static atomic_t pages_mapped = ATOMIC_INIT(0);
Gerd Hoffmannab315232010-12-14 18:40:46 +000064
Daniel De Graafaab8f112011-02-03 12:19:02 -050065static int use_ptemod;
Daniel De Graaf16a1d022013-01-02 22:57:12 +000066#define populate_freeable_maps use_ptemod
Daniel De Graafaab8f112011-02-03 12:19:02 -050067
Gerd Hoffmannab315232010-12-14 18:40:46 +000068struct gntdev_priv {
Daniel De Graaf16a1d022013-01-02 22:57:12 +000069 /* maps with visible offsets in the file descriptor */
Gerd Hoffmannab315232010-12-14 18:40:46 +000070 struct list_head maps;
Daniel De Graaf16a1d022013-01-02 22:57:12 +000071 /* maps that are not visible; will be freed on munmap.
72 * Only populated if populate_freeable_maps == 1 */
73 struct list_head freeable_maps;
74 /* lock protects maps and freeable_maps */
David Vrabel1401c002015-01-09 18:06:12 +000075 struct mutex lock;
Gerd Hoffmannab315232010-12-14 18:40:46 +000076 struct mm_struct *mm;
77 struct mmu_notifier mn;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030078
79#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
80 /* Device for which DMA memory is allocated. */
81 struct device *dma_dev;
82#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +000083};
84
Daniel De Graafbdc612d2011-02-03 12:19:04 -050085struct unmap_notify {
86 int flags;
87 /* Address relative to the start of the grant_map */
88 int addr;
89 int event;
90};
91
Gerd Hoffmannab315232010-12-14 18:40:46 +000092struct grant_map {
93 struct list_head next;
Gerd Hoffmannab315232010-12-14 18:40:46 +000094 struct vm_area_struct *vma;
95 int index;
96 int count;
97 int flags;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +020098 refcount_t users;
Daniel De Graafbdc612d2011-02-03 12:19:04 -050099 struct unmap_notify notify;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000100 struct ioctl_gntdev_grant_ref *grants;
101 struct gnttab_map_grant_ref *map_ops;
102 struct gnttab_unmap_grant_ref *unmap_ops;
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100103 struct gnttab_map_grant_ref *kmap_ops;
David Vrabel853d0282015-01-05 14:13:41 +0000104 struct gnttab_unmap_grant_ref *kunmap_ops;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000105 struct page **pages;
David Vrabeldab069c2014-12-18 14:59:07 +0000106 unsigned long pages_vm_start;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300107
108#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
109 /*
110 * If dmabuf_vaddr is not NULL then this mapping is backed by DMA
111 * capable memory.
112 */
113
114 struct device *dma_dev;
115 /* Flags used to create this DMA buffer: GNTDEV_DMA_FLAG_XXX. */
116 int dma_flags;
117 void *dma_vaddr;
118 dma_addr_t dma_bus_addr;
119 /* Needed to avoid allocation in gnttab_dma_free_pages(). */
120 xen_pfn_t *frames;
121#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000122};
123
Daniel De Graafaab8f112011-02-03 12:19:02 -0500124static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
125
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300126static struct miscdevice gntdev_miscdev;
127
Gerd Hoffmannab315232010-12-14 18:40:46 +0000128/* ------------------------------------------------------------------ */
129
130static void gntdev_print_maps(struct gntdev_priv *priv,
131 char *text, int text_index)
132{
133#ifdef DEBUG
134 struct grant_map *map;
135
Daniel De Graafef910822011-02-03 12:18:59 -0500136 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000137 list_for_each_entry(map, &priv->maps, next)
138 pr_debug(" index %2d, count %2d %s\n",
139 map->index, map->count,
140 map->index == text_index && text ? text : "");
141#endif
142}
143
David Vrabela67baeb72012-10-24 12:39:02 +0100144static void gntdev_free_map(struct grant_map *map)
145{
146 if (map == NULL)
147 return;
148
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300149#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
150 if (map->dma_vaddr) {
151 struct gnttab_dma_alloc_args args;
152
153 args.dev = map->dma_dev;
154 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
155 args.nr_pages = map->count;
156 args.pages = map->pages;
157 args.frames = map->frames;
158 args.vaddr = map->dma_vaddr;
159 args.dev_bus_addr = map->dma_bus_addr;
160
161 gnttab_dma_free_pages(&args);
162 } else
163#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100164 if (map->pages)
David Vrabelff4b1562015-01-08 18:06:01 +0000165 gnttab_free_pages(map->count, map->pages);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300166
167#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
168 kfree(map->frames);
169#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100170 kfree(map->pages);
171 kfree(map->grants);
172 kfree(map->map_ops);
173 kfree(map->unmap_ops);
174 kfree(map->kmap_ops);
David Vrabel853d0282015-01-05 14:13:41 +0000175 kfree(map->kunmap_ops);
David Vrabela67baeb72012-10-24 12:39:02 +0100176 kfree(map);
177}
178
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300179static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
180 int dma_flags)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000181{
182 struct grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000183 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000184
185 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
186 if (NULL == add)
187 return NULL;
188
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300189 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
190 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
191 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
192 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
David Vrabel853d0282015-01-05 14:13:41 +0000193 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300194 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000195 if (NULL == add->grants ||
196 NULL == add->map_ops ||
197 NULL == add->unmap_ops ||
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100198 NULL == add->kmap_ops ||
David Vrabel853d0282015-01-05 14:13:41 +0000199 NULL == add->kunmap_ops ||
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000200 NULL == add->pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000201 goto err;
202
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300203#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
204 add->dma_flags = dma_flags;
205
206 /*
207 * Check if this mapping is requested to be backed
208 * by a DMA buffer.
209 */
210 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
211 struct gnttab_dma_alloc_args args;
212
213 add->frames = kcalloc(count, sizeof(add->frames[0]),
214 GFP_KERNEL);
215 if (!add->frames)
216 goto err;
217
218 /* Remember the device, so we can free DMA memory. */
219 add->dma_dev = priv->dma_dev;
220
221 args.dev = priv->dma_dev;
222 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
223 args.nr_pages = count;
224 args.pages = add->pages;
225 args.frames = add->frames;
226
227 if (gnttab_dma_alloc_pages(&args))
228 goto err;
229
230 add->dma_vaddr = args.vaddr;
231 add->dma_bus_addr = args.dev_bus_addr;
232 } else
233#endif
David Vrabelff4b1562015-01-08 18:06:01 +0000234 if (gnttab_alloc_pages(count, add->pages))
Daniel De Graafca47cea2011-03-09 18:07:34 -0500235 goto err;
236
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000237 for (i = 0; i < count; i++) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500238 add->map_ops[i].handle = -1;
239 add->unmap_ops[i].handle = -1;
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100240 add->kmap_ops[i].handle = -1;
David Vrabel853d0282015-01-05 14:13:41 +0000241 add->kunmap_ops[i].handle = -1;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000242 }
243
Gerd Hoffmannab315232010-12-14 18:40:46 +0000244 add->index = 0;
245 add->count = count;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200246 refcount_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000247
Gerd Hoffmannab315232010-12-14 18:40:46 +0000248 return add;
249
250err:
David Vrabela67baeb72012-10-24 12:39:02 +0100251 gntdev_free_map(add);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000252 return NULL;
253}
254
255static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
256{
257 struct grant_map *map;
258
259 list_for_each_entry(map, &priv->maps, next) {
260 if (add->index + add->count < map->index) {
261 list_add_tail(&add->next, &map->next);
262 goto done;
263 }
264 add->index = map->index + map->count;
265 }
266 list_add_tail(&add->next, &priv->maps);
267
268done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000269 gntdev_print_maps(priv, "[new]", add->index);
270}
271
272static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
273 int index, int count)
274{
275 struct grant_map *map;
276
277 list_for_each_entry(map, &priv->maps, next) {
278 if (map->index != index)
279 continue;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500280 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000281 continue;
282 return map;
283 }
284 return NULL;
285}
286
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000287static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000288{
289 if (!map)
290 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000291
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200292 if (!refcount_dec_and_test(&map->users))
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500293 return;
294
295 atomic_sub(map->count, &pages_mapped);
296
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400297 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500298 notify_remote_via_evtchn(map->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400299 evtchn_put(map->notify.event);
300 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500301
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000302 if (populate_freeable_maps && priv) {
David Vrabel1401c002015-01-09 18:06:12 +0000303 mutex_lock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000304 list_del(&map->next);
David Vrabel1401c002015-01-09 18:06:12 +0000305 mutex_unlock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000306 }
307
David Vrabela67baeb72012-10-24 12:39:02 +0100308 if (map->pages && !use_ptemod)
309 unmap_grant_pages(map, 0, map->count);
310 gntdev_free_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000311}
312
313/* ------------------------------------------------------------------ */
314
315static int find_grant_ptes(pte_t *pte, pgtable_t token,
316 unsigned long addr, void *data)
317{
318 struct grant_map *map = data;
319 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500320 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000321 u64 pte_maddr;
322
323 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800324 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
325
David Vrabel923b2912014-12-18 14:56:54 +0000326 /*
327 * Set the PTE as special to force get_user_pages_fast() fall
328 * back to the slow path. If this is not supported as part of
329 * the grant map, it will be done afterwards.
330 */
331 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
332 flags |= (1 << _GNTMAP_guest_avail0);
333
Daniel De Graafaab8f112011-02-03 12:19:02 -0500334 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000335 map->grants[pgnr].ref,
336 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500337 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500338 -1 /* handle */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000339 return 0;
340}
341
David Vrabel923b2912014-12-18 14:56:54 +0000342#ifdef CONFIG_X86
343static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
344 unsigned long addr, void *data)
345{
346 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
347 return 0;
348}
349#endif
350
Gerd Hoffmannab315232010-12-14 18:40:46 +0000351static int map_grant_pages(struct grant_map *map)
352{
353 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500354
355 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500356 /* Note: it could already be mapped */
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500357 if (map->map_ops[0].handle != -1)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500358 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500359 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000360 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500361 pfn_to_kaddr(page_to_pfn(map->pages[i]));
362 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
363 map->grants[i].ref,
364 map->grants[i].domid);
365 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500366 map->flags, -1 /* handle */);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500367 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100368 } else {
369 /*
370 * Setup the map_ops corresponding to the pte entries pointing
371 * to the kernel linear addresses of the struct pages.
372 * These ptes are completely different from the user ptes dealt
373 * with find_grant_ptes.
374 */
375 for (i = 0; i < map->count; i++) {
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100376 unsigned long address = (unsigned long)
377 pfn_to_kaddr(page_to_pfn(map->pages[i]));
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100378 BUG_ON(PageHighMem(map->pages[i]));
379
Stefano Stabelliniee072642013-07-23 17:23:54 +0000380 gnttab_set_map_op(&map->kmap_ops[i], address,
381 map->flags | GNTMAP_host_map,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100382 map->grants[i].ref,
383 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000384 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
385 map->flags | GNTMAP_host_map, -1);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100386 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500387 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000388
389 pr_debug("map %d+%d\n", map->index, map->count);
Konrad Rzeszutek Wilke85fc982014-02-03 06:43:59 -0500390 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
391 map->pages, map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000392 if (err)
393 return err;
394
395 for (i = 0; i < map->count; i++) {
David Vrabel853d0282015-01-05 14:13:41 +0000396 if (map->map_ops[i].status) {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000397 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000398 continue;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500399 }
David Vrabel853d0282015-01-05 14:13:41 +0000400
401 map->unmap_ops[i].handle = map->map_ops[i].handle;
402 if (use_ptemod)
403 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300404#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
405 else if (map->dma_vaddr) {
406 unsigned long bfn;
407
408 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
409 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
410 }
411#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000412 }
413 return err;
414}
415
Daniel De Graafb57c1862011-02-09 15:12:00 -0500416static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000417{
418 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000419 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000420
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500421 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
422 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500423 if (pgno >= offset && pgno < offset + pages) {
424 /* No need for kmap, pages are in lowmem */
425 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500426 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500427 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
428 }
429 }
430
Jennifer Herbert74528222015-01-05 15:07:46 +0000431 unmap_data.unmap_ops = map->unmap_ops + offset;
432 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
433 unmap_data.pages = map->pages + offset;
434 unmap_data.count = pages;
435
Bob Liub44166c2015-04-03 14:42:59 +0800436 err = gnttab_unmap_refs_sync(&unmap_data);
437 if (err)
438 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000439
440 for (i = 0; i < pages; i++) {
441 if (map->unmap_ops[offset+i].status)
442 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500443 pr_debug("unmap handle=%d st=%d\n",
444 map->unmap_ops[offset+i].handle,
445 map->unmap_ops[offset+i].status);
446 map->unmap_ops[offset+i].handle = -1;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000447 }
448 return err;
449}
450
Daniel De Graafb57c1862011-02-09 15:12:00 -0500451static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
452{
453 int range, err = 0;
454
455 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
456
457 /* It is possible the requested range will have a "hole" where we
458 * already unmapped some of the grants. Only unmap valid ranges.
459 */
460 while (pages && !err) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500461 while (pages && map->unmap_ops[offset].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500462 offset++;
463 pages--;
464 }
465 range = 0;
466 while (range < pages) {
Ross Lagerwall951a0102018-01-09 12:10:21 +0000467 if (map->unmap_ops[offset+range].handle == -1)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500468 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500469 range++;
470 }
471 err = __unmap_grant_pages(map, offset, range);
472 offset += range;
473 pages -= range;
474 }
475
476 return err;
477}
478
Gerd Hoffmannab315232010-12-14 18:40:46 +0000479/* ------------------------------------------------------------------ */
480
Daniel De Graafd79647a2011-03-07 15:18:57 -0500481static void gntdev_vma_open(struct vm_area_struct *vma)
482{
483 struct grant_map *map = vma->vm_private_data;
484
485 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200486 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500487}
488
Gerd Hoffmannab315232010-12-14 18:40:46 +0000489static void gntdev_vma_close(struct vm_area_struct *vma)
490{
491 struct grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000492 struct file *file = vma->vm_file;
493 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000494
Daniel De Graafd79647a2011-03-07 15:18:57 -0500495 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000496 if (use_ptemod) {
Daniel De Graaf2512f292013-01-02 22:57:11 +0000497 /* It is possible that an mmu notifier could be running
498 * concurrently, so take priv->lock to ensure that the vma won't
499 * vanishing during the unmap_grant_pages call, since we will
500 * spin here until that completes. Such a concurrent call will
501 * not do any unmapping, since that has been done prior to
502 * closing the vma, but it may still iterate the unmap_ops list.
503 */
David Vrabel1401c002015-01-09 18:06:12 +0000504 mutex_lock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000505 map->vma = NULL;
David Vrabel1401c002015-01-09 18:06:12 +0000506 mutex_unlock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000507 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000508 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000509 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000510}
511
David Vrabeldab069c2014-12-18 14:59:07 +0000512static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
513 unsigned long addr)
514{
515 struct grant_map *map = vma->vm_private_data;
516
517 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
518}
519
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700520static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500521 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000522 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000523 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000524};
525
526/* ------------------------------------------------------------------ */
527
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000528static void unmap_if_in_range(struct grant_map *map,
529 unsigned long start, unsigned long end)
530{
531 unsigned long mstart, mend;
532 int err;
533
534 if (!map->vma)
535 return;
536 if (map->vma->vm_start >= end)
537 return;
538 if (map->vma->vm_end <= start)
539 return;
540 mstart = max(start, map->vma->vm_start);
541 mend = min(end, map->vma->vm_end);
542 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
543 map->index, map->count,
544 map->vma->vm_start, map->vma->vm_end,
545 start, end, mstart, mend);
546 err = unmap_grant_pages(map,
547 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
548 (mend - mstart) >> PAGE_SHIFT);
549 WARN_ON(err);
550}
551
Gerd Hoffmannab315232010-12-14 18:40:46 +0000552static void mn_invl_range_start(struct mmu_notifier *mn,
553 struct mm_struct *mm,
554 unsigned long start, unsigned long end)
555{
556 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
557 struct grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000558
David Vrabel1401c002015-01-09 18:06:12 +0000559 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000560 list_for_each_entry(map, &priv->maps, next) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000561 unmap_if_in_range(map, start, end);
562 }
563 list_for_each_entry(map, &priv->freeable_maps, next) {
564 unmap_if_in_range(map, start, end);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000565 }
David Vrabel1401c002015-01-09 18:06:12 +0000566 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000567}
568
Gerd Hoffmannab315232010-12-14 18:40:46 +0000569static void mn_release(struct mmu_notifier *mn,
570 struct mm_struct *mm)
571{
572 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
573 struct grant_map *map;
574 int err;
575
David Vrabel1401c002015-01-09 18:06:12 +0000576 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000577 list_for_each_entry(map, &priv->maps, next) {
578 if (!map->vma)
579 continue;
580 pr_debug("map %d+%d (%lx %lx)\n",
581 map->index, map->count,
582 map->vma->vm_start, map->vma->vm_end);
583 err = unmap_grant_pages(map, /* offset */ 0, map->count);
584 WARN_ON(err);
585 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000586 list_for_each_entry(map, &priv->freeable_maps, next) {
587 if (!map->vma)
588 continue;
589 pr_debug("map %d+%d (%lx %lx)\n",
590 map->index, map->count,
591 map->vma->vm_start, map->vma->vm_end);
592 err = unmap_grant_pages(map, /* offset */ 0, map->count);
593 WARN_ON(err);
594 }
David Vrabel1401c002015-01-09 18:06:12 +0000595 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000596}
597
Julia Lawallb9c0a922015-11-29 23:02:49 +0100598static const struct mmu_notifier_ops gntdev_mmu_ops = {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000599 .release = mn_release,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000600 .invalidate_range_start = mn_invl_range_start,
601};
602
603/* ------------------------------------------------------------------ */
604
605static int gntdev_open(struct inode *inode, struct file *flip)
606{
607 struct gntdev_priv *priv;
608 int ret = 0;
609
610 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
611 if (!priv)
612 return -ENOMEM;
613
614 INIT_LIST_HEAD(&priv->maps);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000615 INIT_LIST_HEAD(&priv->freeable_maps);
David Vrabel1401c002015-01-09 18:06:12 +0000616 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000617
Daniel De Graafaab8f112011-02-03 12:19:02 -0500618 if (use_ptemod) {
619 priv->mm = get_task_mm(current);
620 if (!priv->mm) {
621 kfree(priv);
622 return -ENOMEM;
623 }
624 priv->mn.ops = &gntdev_mmu_ops;
625 ret = mmu_notifier_register(&priv->mn, priv->mm);
626 mmput(priv->mm);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000627 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000628
629 if (ret) {
630 kfree(priv);
631 return ret;
632 }
633
634 flip->private_data = priv;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300635#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
636 priv->dma_dev = gntdev_miscdev.this_device;
637
638 /*
639 * The device is not spawn from a device tree, so arch_setup_dma_ops
640 * is not called, thus leaving the device with dummy DMA ops.
641 * Fix this by calling of_dma_configure() with a NULL node to set
642 * default DMA ops.
643 */
644 of_dma_configure(priv->dma_dev, NULL, true);
645#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000646 pr_debug("priv %p\n", priv);
647
648 return 0;
649}
650
651static int gntdev_release(struct inode *inode, struct file *flip)
652{
653 struct gntdev_priv *priv = flip->private_data;
654 struct grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000655
656 pr_debug("priv %p\n", priv);
657
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200658 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000659 while (!list_empty(&priv->maps)) {
660 map = list_entry(priv->maps.next, struct grant_map, next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500661 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000662 gntdev_put_map(NULL /* already removed */, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000663 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000664 WARN_ON(!list_empty(&priv->freeable_maps));
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200665 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000666
Daniel De Graafaab8f112011-02-03 12:19:02 -0500667 if (use_ptemod)
668 mmu_notifier_unregister(&priv->mn, priv->mm);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000669 kfree(priv);
670 return 0;
671}
672
673static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
674 struct ioctl_gntdev_map_grant_ref __user *u)
675{
676 struct ioctl_gntdev_map_grant_ref op;
677 struct grant_map *map;
678 int err;
679
680 if (copy_from_user(&op, u, sizeof(op)) != 0)
681 return -EFAULT;
682 pr_debug("priv %p, add %d\n", priv, op.count);
683 if (unlikely(op.count <= 0))
684 return -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000685
686 err = -ENOMEM;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300687 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000688 if (!map)
689 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500690
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500691 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
692 pr_debug("can't map: over limit\n");
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000693 gntdev_put_map(NULL, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000694 return err;
695 }
696
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500697 if (copy_from_user(map->grants, &u->refs,
698 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000699 gntdev_put_map(NULL, map);
700 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500701 }
702
David Vrabel1401c002015-01-09 18:06:12 +0000703 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000704 gntdev_add_map(priv, map);
705 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000706 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000707
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500708 if (copy_to_user(u, &op, sizeof(op)) != 0)
709 return -EFAULT;
710
Gerd Hoffmannab315232010-12-14 18:40:46 +0000711 return 0;
712}
713
714static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
715 struct ioctl_gntdev_unmap_grant_ref __user *u)
716{
717 struct ioctl_gntdev_unmap_grant_ref op;
718 struct grant_map *map;
719 int err = -ENOENT;
720
721 if (copy_from_user(&op, u, sizeof(op)) != 0)
722 return -EFAULT;
723 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
724
David Vrabel1401c002015-01-09 18:06:12 +0000725 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000726 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500727 if (map) {
728 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000729 if (populate_freeable_maps)
730 list_add_tail(&map->next, &priv->freeable_maps);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500731 err = 0;
732 }
David Vrabel1401c002015-01-09 18:06:12 +0000733 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400734 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000735 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000736 return err;
737}
738
739static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
740 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
741{
742 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500743 struct vm_area_struct *vma;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000744 struct grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000745 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000746
747 if (copy_from_user(&op, u, sizeof(op)) != 0)
748 return -EFAULT;
749 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
750
Daniel De Graaf2512f292013-01-02 22:57:11 +0000751 down_read(&current->mm->mmap_sem);
Daniel De Graafa8792112011-02-03 12:19:00 -0500752 vma = find_vma(current->mm, op.vaddr);
753 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000754 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500755
756 map = vma->vm_private_data;
757 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000758 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500759
Gerd Hoffmannab315232010-12-14 18:40:46 +0000760 op.offset = map->index << PAGE_SHIFT;
761 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000762 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000763
Daniel De Graaf2512f292013-01-02 22:57:11 +0000764 out_unlock:
765 up_read(&current->mm->mmap_sem);
766
767 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000768 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000769 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000770}
771
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500772static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
773{
774 struct ioctl_gntdev_unmap_notify op;
775 struct grant_map *map;
776 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400777 int out_flags;
778 unsigned int out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500779
780 if (copy_from_user(&op, u, sizeof(op)))
781 return -EFAULT;
782
783 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
784 return -EINVAL;
785
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400786 /* We need to grab a reference to the event channel we are going to use
787 * to send the notify before releasing the reference we may already have
788 * (if someone has called this ioctl twice). This is required so that
789 * it is possible to change the clear_byte part of the notification
790 * without disturbing the event channel part, which may now be the last
791 * reference to that event channel.
792 */
793 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
794 if (evtchn_get(op.event_channel_port))
795 return -EINVAL;
796 }
797
798 out_flags = op.action;
799 out_event = op.event_channel_port;
800
David Vrabel1401c002015-01-09 18:06:12 +0000801 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500802
803 list_for_each_entry(map, &priv->maps, next) {
804 uint64_t begin = map->index << PAGE_SHIFT;
805 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
806 if (op.index >= begin && op.index < end)
807 goto found;
808 }
809 rc = -ENOENT;
810 goto unlock_out;
811
812 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500813 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
814 (map->flags & GNTMAP_readonly)) {
815 rc = -EINVAL;
816 goto unlock_out;
817 }
818
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400819 out_flags = map->notify.flags;
820 out_event = map->notify.event;
821
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500822 map->notify.flags = op.action;
823 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
824 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400825
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500826 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400827
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500828 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000829 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400830
831 /* Drop the reference to the event channel we did not save in the map */
832 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
833 evtchn_put(out_event);
834
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500835 return rc;
836}
837
David Vrabel36ae2202016-05-09 10:59:48 +0100838#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000839
840struct gntdev_copy_batch {
841 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
842 struct page *pages[GNTDEV_COPY_BATCH];
843 s16 __user *status[GNTDEV_COPY_BATCH];
844 unsigned int nr_ops;
845 unsigned int nr_pages;
846};
847
848static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
849 bool writeable, unsigned long *gfn)
850{
851 unsigned long addr = (unsigned long)virt;
852 struct page *page;
853 unsigned long xen_pfn;
854 int ret;
855
856 ret = get_user_pages_fast(addr, 1, writeable, &page);
857 if (ret < 0)
858 return ret;
859
860 batch->pages[batch->nr_pages++] = page;
861
862 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
863 *gfn = pfn_to_gfn(xen_pfn);
864
865 return 0;
866}
867
868static void gntdev_put_pages(struct gntdev_copy_batch *batch)
869{
870 unsigned int i;
871
872 for (i = 0; i < batch->nr_pages; i++)
873 put_page(batch->pages[i]);
874 batch->nr_pages = 0;
875}
876
877static int gntdev_copy(struct gntdev_copy_batch *batch)
878{
879 unsigned int i;
880
881 gnttab_batch_copy(batch->ops, batch->nr_ops);
882 gntdev_put_pages(batch);
883
884 /*
885 * For each completed op, update the status if the op failed
886 * and all previous ops for the segment were successful.
887 */
888 for (i = 0; i < batch->nr_ops; i++) {
889 s16 status = batch->ops[i].status;
890 s16 old_status;
891
892 if (status == GNTST_okay)
893 continue;
894
895 if (__get_user(old_status, batch->status[i]))
896 return -EFAULT;
897
898 if (old_status != GNTST_okay)
899 continue;
900
901 if (__put_user(status, batch->status[i]))
902 return -EFAULT;
903 }
904
905 batch->nr_ops = 0;
906 return 0;
907}
908
909static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
910 struct gntdev_grant_copy_segment *seg,
911 s16 __user *status)
912{
913 uint16_t copied = 0;
914
915 /*
916 * Disallow local -> local copies since there is only space in
917 * batch->pages for one page per-op and this would be a very
918 * expensive memcpy().
919 */
920 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
921 return -EINVAL;
922
923 /* Can't cross page if source/dest is a grant ref. */
924 if (seg->flags & GNTCOPY_source_gref) {
925 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
926 return -EINVAL;
927 }
928 if (seg->flags & GNTCOPY_dest_gref) {
929 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
930 return -EINVAL;
931 }
932
933 if (put_user(GNTST_okay, status))
934 return -EFAULT;
935
936 while (copied < seg->len) {
937 struct gnttab_copy *op;
938 void __user *virt;
939 size_t len, off;
940 unsigned long gfn;
941 int ret;
942
943 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
944 ret = gntdev_copy(batch);
945 if (ret < 0)
946 return ret;
947 }
948
949 len = seg->len - copied;
950
951 op = &batch->ops[batch->nr_ops];
952 op->flags = 0;
953
954 if (seg->flags & GNTCOPY_source_gref) {
955 op->source.u.ref = seg->source.foreign.ref;
956 op->source.domid = seg->source.foreign.domid;
957 op->source.offset = seg->source.foreign.offset + copied;
958 op->flags |= GNTCOPY_source_gref;
959 } else {
960 virt = seg->source.virt + copied;
961 off = (unsigned long)virt & ~XEN_PAGE_MASK;
962 len = min(len, (size_t)XEN_PAGE_SIZE - off);
963
964 ret = gntdev_get_page(batch, virt, false, &gfn);
965 if (ret < 0)
966 return ret;
967
968 op->source.u.gmfn = gfn;
969 op->source.domid = DOMID_SELF;
970 op->source.offset = off;
971 }
972
973 if (seg->flags & GNTCOPY_dest_gref) {
974 op->dest.u.ref = seg->dest.foreign.ref;
975 op->dest.domid = seg->dest.foreign.domid;
976 op->dest.offset = seg->dest.foreign.offset + copied;
977 op->flags |= GNTCOPY_dest_gref;
978 } else {
979 virt = seg->dest.virt + copied;
980 off = (unsigned long)virt & ~XEN_PAGE_MASK;
981 len = min(len, (size_t)XEN_PAGE_SIZE - off);
982
983 ret = gntdev_get_page(batch, virt, true, &gfn);
984 if (ret < 0)
985 return ret;
986
987 op->dest.u.gmfn = gfn;
988 op->dest.domid = DOMID_SELF;
989 op->dest.offset = off;
990 }
991
992 op->len = len;
993 copied += len;
994
995 batch->status[batch->nr_ops] = status;
996 batch->nr_ops++;
997 }
998
999 return 0;
1000}
1001
1002static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
1003{
1004 struct ioctl_gntdev_grant_copy copy;
1005 struct gntdev_copy_batch batch;
1006 unsigned int i;
1007 int ret = 0;
1008
1009 if (copy_from_user(&copy, u, sizeof(copy)))
1010 return -EFAULT;
1011
1012 batch.nr_ops = 0;
1013 batch.nr_pages = 0;
1014
1015 for (i = 0; i < copy.count; i++) {
1016 struct gntdev_grant_copy_segment seg;
1017
1018 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
1019 ret = -EFAULT;
1020 goto out;
1021 }
1022
1023 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
1024 if (ret < 0)
1025 goto out;
1026
1027 cond_resched();
1028 }
1029 if (batch.nr_ops)
1030 ret = gntdev_copy(&batch);
1031 return ret;
1032
1033 out:
1034 gntdev_put_pages(&batch);
1035 return ret;
1036}
1037
Gerd Hoffmannab315232010-12-14 18:40:46 +00001038static long gntdev_ioctl(struct file *flip,
1039 unsigned int cmd, unsigned long arg)
1040{
1041 struct gntdev_priv *priv = flip->private_data;
1042 void __user *ptr = (void __user *)arg;
1043
1044 switch (cmd) {
1045 case IOCTL_GNTDEV_MAP_GRANT_REF:
1046 return gntdev_ioctl_map_grant_ref(priv, ptr);
1047
1048 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1049 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1050
1051 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1052 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1053
Daniel De Graafbdc612d2011-02-03 12:19:04 -05001054 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1055 return gntdev_ioctl_notify(priv, ptr);
1056
David Vrabela4cdb552014-12-02 16:13:26 +00001057 case IOCTL_GNTDEV_GRANT_COPY:
1058 return gntdev_ioctl_grant_copy(priv, ptr);
1059
Gerd Hoffmannab315232010-12-14 18:40:46 +00001060 default:
1061 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1062 return -ENOIOCTLCMD;
1063 }
1064
1065 return 0;
1066}
1067
1068static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1069{
1070 struct gntdev_priv *priv = flip->private_data;
1071 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +05301072 int count = vma_pages(vma);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001073 struct grant_map *map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001074 int i, err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001075
1076 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1077 return -EINVAL;
1078
1079 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1080 index, count, vma->vm_start, vma->vm_pgoff);
1081
David Vrabel1401c002015-01-09 18:06:12 +00001082 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001083 map = gntdev_find_map_index(priv, index, count);
1084 if (!map)
1085 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001086 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +00001087 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001088 if (use_ptemod && priv->mm != vma->vm_mm) {
Joe Perches283c0972013-06-28 03:21:41 -07001089 pr_warn("Huh? Other mm?\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001090 goto unlock_out;
1091 }
1092
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +02001093 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -05001094
Gerd Hoffmannab315232010-12-14 18:40:46 +00001095 vma->vm_ops = &gntdev_vmops;
1096
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -05001097 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -05001098
1099 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +01001100 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001101
1102 vma->vm_private_data = map;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001103
Daniel De Graafaab8f112011-02-03 12:19:02 -05001104 if (use_ptemod)
1105 map->vma = vma;
1106
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001107 if (map->flags) {
1108 if ((vma->vm_flags & VM_WRITE) &&
1109 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001110 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001111 } else {
1112 map->flags = GNTMAP_host_map;
1113 if (!(vma->vm_flags & VM_WRITE))
1114 map->flags |= GNTMAP_readonly;
1115 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001116
David Vrabel1401c002015-01-09 18:06:12 +00001117 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001118
Daniel De Graafaab8f112011-02-03 12:19:02 -05001119 if (use_ptemod) {
Juergen Gross298d2752017-10-25 17:08:07 +02001120 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001121 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1122 vma->vm_end - vma->vm_start,
1123 find_grant_ptes, map);
1124 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001125 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001126 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001127 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001128 }
1129
1130 err = map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001131 if (err)
1132 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001133
Daniel De Graafaab8f112011-02-03 12:19:02 -05001134 if (!use_ptemod) {
1135 for (i = 0; i < count; i++) {
1136 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1137 map->pages[i]);
1138 if (err)
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001139 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001140 }
David Vrabel923b2912014-12-18 14:56:54 +00001141 } else {
1142#ifdef CONFIG_X86
1143 /*
1144 * If the PTEs were not made special by the grant map
1145 * hypercall, do so here.
1146 *
1147 * This is racy since the mapping is already visible
1148 * to userspace but userspace should be well-behaved
1149 * enough to not touch it until the mmap() call
1150 * returns.
1151 */
1152 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1153 apply_to_page_range(vma->vm_mm, vma->vm_start,
1154 vma->vm_end - vma->vm_start,
1155 set_grant_ptes_as_special, NULL);
1156 }
1157#endif
Daniel De Graafaab8f112011-02-03 12:19:02 -05001158 }
1159
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001160 return 0;
1161
Gerd Hoffmannab315232010-12-14 18:40:46 +00001162unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001163 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001164 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001165
Dan Carpentera93e20a2011-03-19 08:45:43 +03001166out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001167 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001168out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001169 if (use_ptemod) {
Daniel De Graaf84e40752011-02-09 15:11:59 -05001170 map->vma = NULL;
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001171 unmap_grant_pages(map, 0, map->count);
1172 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001173 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001174 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001175}
1176
1177static const struct file_operations gntdev_fops = {
1178 .owner = THIS_MODULE,
1179 .open = gntdev_open,
1180 .release = gntdev_release,
1181 .mmap = gntdev_mmap,
1182 .unlocked_ioctl = gntdev_ioctl
1183};
1184
1185static struct miscdevice gntdev_miscdev = {
1186 .minor = MISC_DYNAMIC_MINOR,
1187 .name = "xen/gntdev",
1188 .fops = &gntdev_fops,
1189};
1190
1191/* ------------------------------------------------------------------ */
1192
1193static int __init gntdev_init(void)
1194{
1195 int err;
1196
1197 if (!xen_domain())
1198 return -ENODEV;
1199
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001200 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001201
Gerd Hoffmannab315232010-12-14 18:40:46 +00001202 err = misc_register(&gntdev_miscdev);
1203 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001204 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001205 return err;
1206 }
1207 return 0;
1208}
1209
1210static void __exit gntdev_exit(void)
1211{
1212 misc_deregister(&gntdev_miscdev);
1213}
1214
1215module_init(gntdev_init);
1216module_exit(gntdev_exit);
1217
1218/* ------------------------------------------------------------------ */