blob: a04ddf2a68afa539a278073dc1b218ecb1db8ca8 [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>
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03009 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
Gerd Hoffmannab315232010-12-14 18:40:46 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#undef DEBUG
22
Joe Perches283c0972013-06-28 03:21:41 -070023#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
24
Rob Herringee7f5222019-10-08 14:41:55 -050025#include <linux/dma-mapping.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/miscdevice.h>
30#include <linux/fs.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000031#include <linux/uaccess.h>
32#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010033#include <linux/sched/mm.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000034#include <linux/spinlock.h>
35#include <linux/slab.h>
Daniel De Graafaab8f112011-02-03 12:19:02 -050036#include <linux/highmem.h>
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +020037#include <linux/refcount.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000038
39#include <xen/xen.h>
40#include <xen/grant_table.h>
Daniel De Graafca47cea2011-03-09 18:07:34 -050041#include <xen/balloon.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000042#include <xen/gntdev.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050043#include <xen/events.h>
Julien Gralla9fd60e2015-06-17 15:28:02 +010044#include <xen/page.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000045#include <asm/xen/hypervisor.h>
46#include <asm/xen/hypercall.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000047
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030048#include "gntdev-common.h"
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030049#ifdef CONFIG_XEN_GNTDEV_DMABUF
50#include "gntdev-dmabuf.h"
51#endif
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030052
Gerd Hoffmannab315232010-12-14 18:40:46 +000053MODULE_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;
66
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030067static int unmap_grant_pages(struct gntdev_grant_map *map,
68 int offset, int pages);
Daniel De Graafaab8f112011-02-03 12:19:02 -050069
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030070static struct miscdevice gntdev_miscdev;
71
Gerd Hoffmannab315232010-12-14 18:40:46 +000072/* ------------------------------------------------------------------ */
73
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030074bool gntdev_account_mapped_pages(int count)
75{
76 return atomic_add_return(count, &pages_mapped) > limit;
77}
78
Gerd Hoffmannab315232010-12-14 18:40:46 +000079static void gntdev_print_maps(struct gntdev_priv *priv,
80 char *text, int text_index)
81{
82#ifdef DEBUG
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030083 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +000084
Daniel De Graafef910822011-02-03 12:18:59 -050085 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +000086 list_for_each_entry(map, &priv->maps, next)
87 pr_debug(" index %2d, count %2d %s\n",
88 map->index, map->count,
89 map->index == text_index && text ? text : "");
90#endif
91}
92
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030093static void gntdev_free_map(struct gntdev_grant_map *map)
David Vrabela67baeb72012-10-24 12:39:02 +010094{
95 if (map == NULL)
96 return;
97
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030098#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
99 if (map->dma_vaddr) {
100 struct gnttab_dma_alloc_args args;
101
102 args.dev = map->dma_dev;
103 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
104 args.nr_pages = map->count;
105 args.pages = map->pages;
106 args.frames = map->frames;
107 args.vaddr = map->dma_vaddr;
108 args.dev_bus_addr = map->dma_bus_addr;
109
110 gnttab_dma_free_pages(&args);
111 } else
112#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100113 if (map->pages)
David Vrabelff4b1562015-01-08 18:06:01 +0000114 gnttab_free_pages(map->count, map->pages);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300115
116#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
117 kfree(map->frames);
118#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100119 kfree(map->pages);
120 kfree(map->grants);
121 kfree(map->map_ops);
122 kfree(map->unmap_ops);
123 kfree(map->kmap_ops);
David Vrabel853d0282015-01-05 14:13:41 +0000124 kfree(map->kunmap_ops);
David Vrabela67baeb72012-10-24 12:39:02 +0100125 kfree(map);
126}
127
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300128struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300129 int dma_flags)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000130{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300131 struct gntdev_grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000132 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000133
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300134 add = kzalloc(sizeof(*add), GFP_KERNEL);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000135 if (NULL == add)
136 return NULL;
137
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300138 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
139 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
140 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
141 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
David Vrabel853d0282015-01-05 14:13:41 +0000142 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300143 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000144 if (NULL == add->grants ||
145 NULL == add->map_ops ||
146 NULL == add->unmap_ops ||
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100147 NULL == add->kmap_ops ||
David Vrabel853d0282015-01-05 14:13:41 +0000148 NULL == add->kunmap_ops ||
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000149 NULL == add->pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000150 goto err;
151
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300152#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
153 add->dma_flags = dma_flags;
154
155 /*
156 * Check if this mapping is requested to be backed
157 * by a DMA buffer.
158 */
159 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
160 struct gnttab_dma_alloc_args args;
161
162 add->frames = kcalloc(count, sizeof(add->frames[0]),
163 GFP_KERNEL);
164 if (!add->frames)
165 goto err;
166
167 /* Remember the device, so we can free DMA memory. */
168 add->dma_dev = priv->dma_dev;
169
170 args.dev = priv->dma_dev;
171 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
172 args.nr_pages = count;
173 args.pages = add->pages;
174 args.frames = add->frames;
175
176 if (gnttab_dma_alloc_pages(&args))
177 goto err;
178
179 add->dma_vaddr = args.vaddr;
180 add->dma_bus_addr = args.dev_bus_addr;
181 } else
182#endif
David Vrabelff4b1562015-01-08 18:06:01 +0000183 if (gnttab_alloc_pages(count, add->pages))
Daniel De Graafca47cea2011-03-09 18:07:34 -0500184 goto err;
185
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000186 for (i = 0; i < count; i++) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500187 add->map_ops[i].handle = -1;
188 add->unmap_ops[i].handle = -1;
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100189 add->kmap_ops[i].handle = -1;
David Vrabel853d0282015-01-05 14:13:41 +0000190 add->kunmap_ops[i].handle = -1;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000191 }
192
Gerd Hoffmannab315232010-12-14 18:40:46 +0000193 add->index = 0;
194 add->count = count;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200195 refcount_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000196
Gerd Hoffmannab315232010-12-14 18:40:46 +0000197 return add;
198
199err:
David Vrabela67baeb72012-10-24 12:39:02 +0100200 gntdev_free_map(add);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000201 return NULL;
202}
203
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300204void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000205{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300206 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000207
208 list_for_each_entry(map, &priv->maps, next) {
209 if (add->index + add->count < map->index) {
210 list_add_tail(&add->next, &map->next);
211 goto done;
212 }
213 add->index = map->index + map->count;
214 }
215 list_add_tail(&add->next, &priv->maps);
216
217done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000218 gntdev_print_maps(priv, "[new]", add->index);
219}
220
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300221static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
222 int index, int count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000223{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300224 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000225
226 list_for_each_entry(map, &priv->maps, next) {
227 if (map->index != index)
228 continue;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500229 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000230 continue;
231 return map;
232 }
233 return NULL;
234}
235
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300236void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000237{
238 if (!map)
239 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000240
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200241 if (!refcount_dec_and_test(&map->users))
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500242 return;
243
244 atomic_sub(map->count, &pages_mapped);
245
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400246 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500247 notify_remote_via_evtchn(map->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400248 evtchn_put(map->notify.event);
249 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500250
David Vrabela67baeb72012-10-24 12:39:02 +0100251 if (map->pages && !use_ptemod)
252 unmap_grant_pages(map, 0, map->count);
253 gntdev_free_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000254}
255
256/* ------------------------------------------------------------------ */
257
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700258static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000259{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300260 struct gntdev_grant_map *map = data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000261 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500262 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000263 u64 pte_maddr;
264
265 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800266 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
267
David Vrabel923b2912014-12-18 14:56:54 +0000268 /*
269 * Set the PTE as special to force get_user_pages_fast() fall
270 * back to the slow path. If this is not supported as part of
271 * the grant map, it will be done afterwards.
272 */
273 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
274 flags |= (1 << _GNTMAP_guest_avail0);
275
Daniel De Graafaab8f112011-02-03 12:19:02 -0500276 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000277 map->grants[pgnr].ref,
278 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500279 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500280 -1 /* handle */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000281 return 0;
282}
283
David Vrabel923b2912014-12-18 14:56:54 +0000284#ifdef CONFIG_X86
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700285static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
David Vrabel923b2912014-12-18 14:56:54 +0000286{
287 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
288 return 0;
289}
290#endif
291
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300292int gntdev_map_grant_pages(struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000293{
294 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500295
296 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500297 /* Note: it could already be mapped */
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500298 if (map->map_ops[0].handle != -1)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500299 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500300 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000301 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500302 pfn_to_kaddr(page_to_pfn(map->pages[i]));
303 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
304 map->grants[i].ref,
305 map->grants[i].domid);
306 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500307 map->flags, -1 /* handle */);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500308 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100309 } else {
310 /*
311 * Setup the map_ops corresponding to the pte entries pointing
312 * to the kernel linear addresses of the struct pages.
313 * These ptes are completely different from the user ptes dealt
314 * with find_grant_ptes.
315 */
316 for (i = 0; i < map->count; i++) {
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100317 unsigned long address = (unsigned long)
318 pfn_to_kaddr(page_to_pfn(map->pages[i]));
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100319 BUG_ON(PageHighMem(map->pages[i]));
320
Stefano Stabelliniee072642013-07-23 17:23:54 +0000321 gnttab_set_map_op(&map->kmap_ops[i], address,
322 map->flags | GNTMAP_host_map,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100323 map->grants[i].ref,
324 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000325 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
326 map->flags | GNTMAP_host_map, -1);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100327 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500328 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000329
330 pr_debug("map %d+%d\n", map->index, map->count);
Konrad Rzeszutek Wilke85fc982014-02-03 06:43:59 -0500331 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
332 map->pages, map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000333 if (err)
334 return err;
335
336 for (i = 0; i < map->count; i++) {
David Vrabel853d0282015-01-05 14:13:41 +0000337 if (map->map_ops[i].status) {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000338 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000339 continue;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500340 }
David Vrabel853d0282015-01-05 14:13:41 +0000341
342 map->unmap_ops[i].handle = map->map_ops[i].handle;
343 if (use_ptemod)
344 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300345#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
346 else if (map->dma_vaddr) {
347 unsigned long bfn;
348
349 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
350 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
351 }
352#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000353 }
354 return err;
355}
356
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300357static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
358 int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000359{
360 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000361 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000362
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500363 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
364 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500365 if (pgno >= offset && pgno < offset + pages) {
366 /* No need for kmap, pages are in lowmem */
367 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500368 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500369 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
370 }
371 }
372
Jennifer Herbert74528222015-01-05 15:07:46 +0000373 unmap_data.unmap_ops = map->unmap_ops + offset;
374 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
375 unmap_data.pages = map->pages + offset;
376 unmap_data.count = pages;
377
Bob Liub44166c2015-04-03 14:42:59 +0800378 err = gnttab_unmap_refs_sync(&unmap_data);
379 if (err)
380 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000381
382 for (i = 0; i < pages; i++) {
383 if (map->unmap_ops[offset+i].status)
384 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500385 pr_debug("unmap handle=%d st=%d\n",
386 map->unmap_ops[offset+i].handle,
387 map->unmap_ops[offset+i].status);
388 map->unmap_ops[offset+i].handle = -1;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000389 }
390 return err;
391}
392
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300393static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
394 int pages)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500395{
396 int range, err = 0;
397
398 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
399
400 /* It is possible the requested range will have a "hole" where we
401 * already unmapped some of the grants. Only unmap valid ranges.
402 */
403 while (pages && !err) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500404 while (pages && map->unmap_ops[offset].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500405 offset++;
406 pages--;
407 }
408 range = 0;
409 while (range < pages) {
Ross Lagerwall951a0102018-01-09 12:10:21 +0000410 if (map->unmap_ops[offset+range].handle == -1)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500411 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500412 range++;
413 }
414 err = __unmap_grant_pages(map, offset, range);
415 offset += range;
416 pages -= range;
417 }
418
419 return err;
420}
421
Gerd Hoffmannab315232010-12-14 18:40:46 +0000422/* ------------------------------------------------------------------ */
423
Daniel De Graafd79647a2011-03-07 15:18:57 -0500424static void gntdev_vma_open(struct vm_area_struct *vma)
425{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300426 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500427
428 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200429 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500430}
431
Gerd Hoffmannab315232010-12-14 18:40:46 +0000432static void gntdev_vma_close(struct vm_area_struct *vma)
433{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300434 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000435 struct file *file = vma->vm_file;
436 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000437
Daniel De Graafd79647a2011-03-07 15:18:57 -0500438 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000439 if (use_ptemod) {
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400440 WARN_ON(map->vma != vma);
441 mmu_interval_notifier_remove(&map->notifier);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000442 map->vma = NULL;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000443 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000444 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000445 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000446}
447
David Vrabeldab069c2014-12-18 14:59:07 +0000448static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
449 unsigned long addr)
450{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300451 struct gntdev_grant_map *map = vma->vm_private_data;
David Vrabeldab069c2014-12-18 14:59:07 +0000452
453 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
454}
455
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700456static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500457 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000458 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000459 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000460};
461
462/* ------------------------------------------------------------------ */
463
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400464static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
465 const struct mmu_notifier_range *range,
466 unsigned long cur_seq)
Michal Hocko93065ac2018-08-21 21:52:33 -0700467{
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400468 struct gntdev_grant_map *map =
469 container_of(mn, struct gntdev_grant_map, notifier);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000470 unsigned long mstart, mend;
471 int err;
472
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400473 if (!mmu_notifier_range_blockable(range))
474 return false;
Michal Hocko58a57562018-09-05 09:21:39 +1000475
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400476 /*
477 * If the VMA is split or otherwise changed the notifier is not
478 * updated, but we don't want to process VA's outside the modified
479 * VMA. FIXME: It would be much more understandable to just prevent
480 * modifying the VMA in the first place.
481 */
482 if (map->vma->vm_start >= range->end ||
483 map->vma->vm_end <= range->start)
484 return true;
Michal Hocko58a57562018-09-05 09:21:39 +1000485
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400486 mstart = max(range->start, map->vma->vm_start);
487 mend = min(range->end, map->vma->vm_end);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000488 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
489 map->index, map->count,
490 map->vma->vm_start, map->vma->vm_end,
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400491 range->start, range->end, mstart, mend);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000492 err = unmap_grant_pages(map,
493 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
494 (mend - mstart) >> PAGE_SHIFT);
495 WARN_ON(err);
Michal Hocko58a57562018-09-05 09:21:39 +1000496
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400497 return true;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000498}
499
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400500static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
501 .invalidate = gntdev_invalidate,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000502};
503
504/* ------------------------------------------------------------------ */
505
506static int gntdev_open(struct inode *inode, struct file *flip)
507{
508 struct gntdev_priv *priv;
509 int ret = 0;
510
511 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
512 if (!priv)
513 return -ENOMEM;
514
515 INIT_LIST_HEAD(&priv->maps);
David Vrabel1401c002015-01-09 18:06:12 +0000516 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000517
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300518#ifdef CONFIG_XEN_GNTDEV_DMABUF
Oleksandr Andrushchenkofa13e662019-02-14 16:23:20 +0200519 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300520 if (IS_ERR(priv->dmabuf_priv)) {
521 ret = PTR_ERR(priv->dmabuf_priv);
522 kfree(priv);
523 return ret;
524 }
525#endif
526
Gerd Hoffmannab315232010-12-14 18:40:46 +0000527 if (ret) {
528 kfree(priv);
529 return ret;
530 }
531
532 flip->private_data = priv;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300533#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
534 priv->dma_dev = gntdev_miscdev.this_device;
Rob Herringee7f5222019-10-08 14:41:55 -0500535 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300536#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000537 pr_debug("priv %p\n", priv);
538
539 return 0;
540}
541
542static int gntdev_release(struct inode *inode, struct file *flip)
543{
544 struct gntdev_priv *priv = flip->private_data;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300545 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000546
547 pr_debug("priv %p\n", priv);
548
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200549 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000550 while (!list_empty(&priv->maps)) {
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300551 map = list_entry(priv->maps.next,
552 struct gntdev_grant_map, next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500553 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000554 gntdev_put_map(NULL /* already removed */, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000555 }
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200556 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000557
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300558#ifdef CONFIG_XEN_GNTDEV_DMABUF
559 gntdev_dmabuf_fini(priv->dmabuf_priv);
560#endif
561
Gerd Hoffmannab315232010-12-14 18:40:46 +0000562 kfree(priv);
563 return 0;
564}
565
566static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
567 struct ioctl_gntdev_map_grant_ref __user *u)
568{
569 struct ioctl_gntdev_map_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300570 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000571 int err;
572
573 if (copy_from_user(&op, u, sizeof(op)) != 0)
574 return -EFAULT;
575 pr_debug("priv %p, add %d\n", priv, op.count);
576 if (unlikely(op.count <= 0))
577 return -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000578
579 err = -ENOMEM;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300580 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000581 if (!map)
582 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500583
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300584 if (unlikely(gntdev_account_mapped_pages(op.count))) {
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500585 pr_debug("can't map: over limit\n");
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000586 gntdev_put_map(NULL, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000587 return err;
588 }
589
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500590 if (copy_from_user(map->grants, &u->refs,
591 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000592 gntdev_put_map(NULL, map);
593 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500594 }
595
David Vrabel1401c002015-01-09 18:06:12 +0000596 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000597 gntdev_add_map(priv, map);
598 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000599 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000600
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500601 if (copy_to_user(u, &op, sizeof(op)) != 0)
602 return -EFAULT;
603
Gerd Hoffmannab315232010-12-14 18:40:46 +0000604 return 0;
605}
606
607static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
608 struct ioctl_gntdev_unmap_grant_ref __user *u)
609{
610 struct ioctl_gntdev_unmap_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300611 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000612 int err = -ENOENT;
613
614 if (copy_from_user(&op, u, sizeof(op)) != 0)
615 return -EFAULT;
616 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
617
David Vrabel1401c002015-01-09 18:06:12 +0000618 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000619 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500620 if (map) {
621 list_del(&map->next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500622 err = 0;
623 }
David Vrabel1401c002015-01-09 18:06:12 +0000624 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400625 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000626 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000627 return err;
628}
629
630static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
631 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
632{
633 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500634 struct vm_area_struct *vma;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300635 struct gntdev_grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000636 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000637
638 if (copy_from_user(&op, u, sizeof(op)) != 0)
639 return -EFAULT;
640 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
641
Daniel De Graaf2512f292013-01-02 22:57:11 +0000642 down_read(&current->mm->mmap_sem);
Daniel De Graafa8792112011-02-03 12:19:00 -0500643 vma = find_vma(current->mm, op.vaddr);
644 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000645 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500646
647 map = vma->vm_private_data;
648 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000649 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500650
Gerd Hoffmannab315232010-12-14 18:40:46 +0000651 op.offset = map->index << PAGE_SHIFT;
652 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000653 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000654
Daniel De Graaf2512f292013-01-02 22:57:11 +0000655 out_unlock:
656 up_read(&current->mm->mmap_sem);
657
658 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000659 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000660 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000661}
662
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500663static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
664{
665 struct ioctl_gntdev_unmap_notify op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300666 struct gntdev_grant_map *map;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500667 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400668 int out_flags;
669 unsigned int out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500670
671 if (copy_from_user(&op, u, sizeof(op)))
672 return -EFAULT;
673
674 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
675 return -EINVAL;
676
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400677 /* We need to grab a reference to the event channel we are going to use
678 * to send the notify before releasing the reference we may already have
679 * (if someone has called this ioctl twice). This is required so that
680 * it is possible to change the clear_byte part of the notification
681 * without disturbing the event channel part, which may now be the last
682 * reference to that event channel.
683 */
684 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
685 if (evtchn_get(op.event_channel_port))
686 return -EINVAL;
687 }
688
689 out_flags = op.action;
690 out_event = op.event_channel_port;
691
David Vrabel1401c002015-01-09 18:06:12 +0000692 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500693
694 list_for_each_entry(map, &priv->maps, next) {
695 uint64_t begin = map->index << PAGE_SHIFT;
696 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
697 if (op.index >= begin && op.index < end)
698 goto found;
699 }
700 rc = -ENOENT;
701 goto unlock_out;
702
703 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500704 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
705 (map->flags & GNTMAP_readonly)) {
706 rc = -EINVAL;
707 goto unlock_out;
708 }
709
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400710 out_flags = map->notify.flags;
711 out_event = map->notify.event;
712
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500713 map->notify.flags = op.action;
714 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
715 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400716
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500717 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400718
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500719 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000720 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400721
722 /* Drop the reference to the event channel we did not save in the map */
723 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
724 evtchn_put(out_event);
725
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500726 return rc;
727}
728
David Vrabel36ae2202016-05-09 10:59:48 +0100729#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000730
731struct gntdev_copy_batch {
732 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
733 struct page *pages[GNTDEV_COPY_BATCH];
734 s16 __user *status[GNTDEV_COPY_BATCH];
735 unsigned int nr_ops;
736 unsigned int nr_pages;
737};
738
739static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
740 bool writeable, unsigned long *gfn)
741{
742 unsigned long addr = (unsigned long)virt;
743 struct page *page;
744 unsigned long xen_pfn;
745 int ret;
746
Ira Weiny73b01402019-05-13 17:17:11 -0700747 ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
David Vrabela4cdb552014-12-02 16:13:26 +0000748 if (ret < 0)
749 return ret;
750
751 batch->pages[batch->nr_pages++] = page;
752
753 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
754 *gfn = pfn_to_gfn(xen_pfn);
755
756 return 0;
757}
758
759static void gntdev_put_pages(struct gntdev_copy_batch *batch)
760{
761 unsigned int i;
762
763 for (i = 0; i < batch->nr_pages; i++)
764 put_page(batch->pages[i]);
765 batch->nr_pages = 0;
766}
767
768static int gntdev_copy(struct gntdev_copy_batch *batch)
769{
770 unsigned int i;
771
772 gnttab_batch_copy(batch->ops, batch->nr_ops);
773 gntdev_put_pages(batch);
774
775 /*
776 * For each completed op, update the status if the op failed
777 * and all previous ops for the segment were successful.
778 */
779 for (i = 0; i < batch->nr_ops; i++) {
780 s16 status = batch->ops[i].status;
781 s16 old_status;
782
783 if (status == GNTST_okay)
784 continue;
785
786 if (__get_user(old_status, batch->status[i]))
787 return -EFAULT;
788
789 if (old_status != GNTST_okay)
790 continue;
791
792 if (__put_user(status, batch->status[i]))
793 return -EFAULT;
794 }
795
796 batch->nr_ops = 0;
797 return 0;
798}
799
800static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
801 struct gntdev_grant_copy_segment *seg,
802 s16 __user *status)
803{
804 uint16_t copied = 0;
805
806 /*
807 * Disallow local -> local copies since there is only space in
808 * batch->pages for one page per-op and this would be a very
809 * expensive memcpy().
810 */
811 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
812 return -EINVAL;
813
814 /* Can't cross page if source/dest is a grant ref. */
815 if (seg->flags & GNTCOPY_source_gref) {
816 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
817 return -EINVAL;
818 }
819 if (seg->flags & GNTCOPY_dest_gref) {
820 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
821 return -EINVAL;
822 }
823
824 if (put_user(GNTST_okay, status))
825 return -EFAULT;
826
827 while (copied < seg->len) {
828 struct gnttab_copy *op;
829 void __user *virt;
830 size_t len, off;
831 unsigned long gfn;
832 int ret;
833
834 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
835 ret = gntdev_copy(batch);
836 if (ret < 0)
837 return ret;
838 }
839
840 len = seg->len - copied;
841
842 op = &batch->ops[batch->nr_ops];
843 op->flags = 0;
844
845 if (seg->flags & GNTCOPY_source_gref) {
846 op->source.u.ref = seg->source.foreign.ref;
847 op->source.domid = seg->source.foreign.domid;
848 op->source.offset = seg->source.foreign.offset + copied;
849 op->flags |= GNTCOPY_source_gref;
850 } else {
851 virt = seg->source.virt + copied;
852 off = (unsigned long)virt & ~XEN_PAGE_MASK;
853 len = min(len, (size_t)XEN_PAGE_SIZE - off);
854
855 ret = gntdev_get_page(batch, virt, false, &gfn);
856 if (ret < 0)
857 return ret;
858
859 op->source.u.gmfn = gfn;
860 op->source.domid = DOMID_SELF;
861 op->source.offset = off;
862 }
863
864 if (seg->flags & GNTCOPY_dest_gref) {
865 op->dest.u.ref = seg->dest.foreign.ref;
866 op->dest.domid = seg->dest.foreign.domid;
867 op->dest.offset = seg->dest.foreign.offset + copied;
868 op->flags |= GNTCOPY_dest_gref;
869 } else {
870 virt = seg->dest.virt + copied;
871 off = (unsigned long)virt & ~XEN_PAGE_MASK;
872 len = min(len, (size_t)XEN_PAGE_SIZE - off);
873
874 ret = gntdev_get_page(batch, virt, true, &gfn);
875 if (ret < 0)
876 return ret;
877
878 op->dest.u.gmfn = gfn;
879 op->dest.domid = DOMID_SELF;
880 op->dest.offset = off;
881 }
882
883 op->len = len;
884 copied += len;
885
886 batch->status[batch->nr_ops] = status;
887 batch->nr_ops++;
888 }
889
890 return 0;
891}
892
893static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
894{
895 struct ioctl_gntdev_grant_copy copy;
896 struct gntdev_copy_batch batch;
897 unsigned int i;
898 int ret = 0;
899
900 if (copy_from_user(&copy, u, sizeof(copy)))
901 return -EFAULT;
902
903 batch.nr_ops = 0;
904 batch.nr_pages = 0;
905
906 for (i = 0; i < copy.count; i++) {
907 struct gntdev_grant_copy_segment seg;
908
909 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
910 ret = -EFAULT;
911 goto out;
912 }
913
914 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
915 if (ret < 0)
916 goto out;
917
918 cond_resched();
919 }
920 if (batch.nr_ops)
921 ret = gntdev_copy(&batch);
922 return ret;
923
924 out:
925 gntdev_put_pages(&batch);
926 return ret;
927}
928
Gerd Hoffmannab315232010-12-14 18:40:46 +0000929static long gntdev_ioctl(struct file *flip,
930 unsigned int cmd, unsigned long arg)
931{
932 struct gntdev_priv *priv = flip->private_data;
933 void __user *ptr = (void __user *)arg;
934
935 switch (cmd) {
936 case IOCTL_GNTDEV_MAP_GRANT_REF:
937 return gntdev_ioctl_map_grant_ref(priv, ptr);
938
939 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
940 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
941
942 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
943 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
944
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500945 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
946 return gntdev_ioctl_notify(priv, ptr);
947
David Vrabela4cdb552014-12-02 16:13:26 +0000948 case IOCTL_GNTDEV_GRANT_COPY:
949 return gntdev_ioctl_grant_copy(priv, ptr);
950
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300951#ifdef CONFIG_XEN_GNTDEV_DMABUF
952 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
953 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
954
955 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
956 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
957
958 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
959 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
960
961 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
962 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
963#endif
964
Gerd Hoffmannab315232010-12-14 18:40:46 +0000965 default:
966 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
967 return -ENOIOCTLCMD;
968 }
969
970 return 0;
971}
972
973static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
974{
975 struct gntdev_priv *priv = flip->private_data;
976 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +0530977 int count = vma_pages(vma);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300978 struct gntdev_grant_map *map;
Souptick Joarderdf9bde02019-05-13 17:22:23 -0700979 int err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000980
981 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
982 return -EINVAL;
983
984 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
985 index, count, vma->vm_start, vma->vm_pgoff);
986
David Vrabel1401c002015-01-09 18:06:12 +0000987 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000988 map = gntdev_find_map_index(priv, index, count);
989 if (!map)
990 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500991 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000992 goto unlock_out;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200993 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500994
Gerd Hoffmannab315232010-12-14 18:40:46 +0000995 vma->vm_ops = &gntdev_vmops;
996
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -0500997 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500998
999 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +01001000 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001001
1002 vma->vm_private_data = map;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001003 if (map->flags) {
1004 if ((vma->vm_flags & VM_WRITE) &&
1005 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001006 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001007 } else {
1008 map->flags = GNTMAP_host_map;
1009 if (!(vma->vm_flags & VM_WRITE))
1010 map->flags |= GNTMAP_readonly;
1011 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001012
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001013 if (use_ptemod) {
1014 map->vma = vma;
1015 err = mmu_interval_notifier_insert_locked(
1016 &map->notifier, vma->vm_mm, vma->vm_start,
1017 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1018 if (err)
1019 goto out_unlock_put;
1020 }
David Vrabel1401c002015-01-09 18:06:12 +00001021 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001022
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001023 /*
1024 * gntdev takes the address of the PTE in find_grant_ptes() and passes
1025 * it to the hypervisor in gntdev_map_grant_pages(). The purpose of
1026 * the notifier is to prevent the hypervisor pointer to the PTE from
1027 * going stale.
1028 *
1029 * Since this vma's mappings can't be touched without the mmap_sem,
1030 * and we are holding it now, there is no need for the notifier_range
1031 * locking pattern.
1032 */
1033 mmu_interval_read_begin(&map->notifier);
1034
Daniel De Graafaab8f112011-02-03 12:19:02 -05001035 if (use_ptemod) {
Juergen Gross298d2752017-10-25 17:08:07 +02001036 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001037 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1038 vma->vm_end - vma->vm_start,
1039 find_grant_ptes, map);
1040 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001041 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001042 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001043 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001044 }
1045
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001046 err = gntdev_map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001047 if (err)
1048 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001049
Daniel De Graafaab8f112011-02-03 12:19:02 -05001050 if (!use_ptemod) {
Souptick Joarder8d1502f2019-07-31 00:04:56 +05301051 err = vm_map_pages_zero(vma, map->pages, map->count);
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001052 if (err)
1053 goto out_put_map;
David Vrabel923b2912014-12-18 14:56:54 +00001054 } else {
1055#ifdef CONFIG_X86
1056 /*
1057 * If the PTEs were not made special by the grant map
1058 * hypercall, do so here.
1059 *
1060 * This is racy since the mapping is already visible
1061 * to userspace but userspace should be well-behaved
1062 * enough to not touch it until the mmap() call
1063 * returns.
1064 */
1065 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1066 apply_to_page_range(vma->vm_mm, vma->vm_start,
1067 vma->vm_end - vma->vm_start,
1068 set_grant_ptes_as_special, NULL);
1069 }
1070#endif
Daniel De Graafaab8f112011-02-03 12:19:02 -05001071 }
1072
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001073 return 0;
1074
Gerd Hoffmannab315232010-12-14 18:40:46 +00001075unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001076 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001077 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001078
Dan Carpentera93e20a2011-03-19 08:45:43 +03001079out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001080 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001081out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001082 if (use_ptemod) {
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001083 unmap_grant_pages(map, 0, map->count);
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001084 if (map->vma) {
1085 mmu_interval_notifier_remove(&map->notifier);
1086 map->vma = NULL;
1087 }
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001088 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001089 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001090 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001091}
1092
1093static const struct file_operations gntdev_fops = {
1094 .owner = THIS_MODULE,
1095 .open = gntdev_open,
1096 .release = gntdev_release,
1097 .mmap = gntdev_mmap,
1098 .unlocked_ioctl = gntdev_ioctl
1099};
1100
1101static struct miscdevice gntdev_miscdev = {
1102 .minor = MISC_DYNAMIC_MINOR,
1103 .name = "xen/gntdev",
1104 .fops = &gntdev_fops,
1105};
1106
1107/* ------------------------------------------------------------------ */
1108
1109static int __init gntdev_init(void)
1110{
1111 int err;
1112
1113 if (!xen_domain())
1114 return -ENODEV;
1115
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001116 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001117
Gerd Hoffmannab315232010-12-14 18:40:46 +00001118 err = misc_register(&gntdev_miscdev);
1119 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001120 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001121 return err;
1122 }
1123 return 0;
1124}
1125
1126static void __exit gntdev_exit(void)
1127{
1128 misc_deregister(&gntdev_miscdev);
1129}
1130
1131module_init(gntdev_init);
1132module_exit(gntdev_exit);
1133
1134/* ------------------------------------------------------------------ */