blob: f01d58c7a042ed747ce2c9227af0c5429a78546e [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
Juergen Gross3b06ac62019-11-07 12:15:45 +010058static unsigned int limit = 64*1024;
59module_param(limit, uint, 0644);
60MODULE_PARM_DESC(limit,
61 "Maximum number of grants that may be mapped by one mapping request");
Gerd Hoffmannab315232010-12-14 18:40:46 +000062
Daniel De Graafaab8f112011-02-03 12:19:02 -050063static int use_ptemod;
64
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030065static int unmap_grant_pages(struct gntdev_grant_map *map,
66 int offset, int pages);
Daniel De Graafaab8f112011-02-03 12:19:02 -050067
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030068static struct miscdevice gntdev_miscdev;
69
Gerd Hoffmannab315232010-12-14 18:40:46 +000070/* ------------------------------------------------------------------ */
71
Juergen Gross3b06ac62019-11-07 12:15:45 +010072bool gntdev_test_page_count(unsigned int count)
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030073{
Juergen Gross3b06ac62019-11-07 12:15:45 +010074 return !count || count > limit;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030075}
76
Gerd Hoffmannab315232010-12-14 18:40:46 +000077static void gntdev_print_maps(struct gntdev_priv *priv,
78 char *text, int text_index)
79{
80#ifdef DEBUG
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030081 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +000082
Daniel De Graafef910822011-02-03 12:18:59 -050083 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +000084 list_for_each_entry(map, &priv->maps, next)
85 pr_debug(" index %2d, count %2d %s\n",
86 map->index, map->count,
87 map->index == text_index && text ? text : "");
88#endif
89}
90
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030091static void gntdev_free_map(struct gntdev_grant_map *map)
David Vrabela67baeb72012-10-24 12:39:02 +010092{
93 if (map == NULL)
94 return;
95
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030096#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
97 if (map->dma_vaddr) {
98 struct gnttab_dma_alloc_args args;
99
100 args.dev = map->dma_dev;
101 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
102 args.nr_pages = map->count;
103 args.pages = map->pages;
104 args.frames = map->frames;
105 args.vaddr = map->dma_vaddr;
106 args.dev_bus_addr = map->dma_bus_addr;
107
108 gnttab_dma_free_pages(&args);
109 } else
110#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100111 if (map->pages)
David Vrabelff4b1562015-01-08 18:06:01 +0000112 gnttab_free_pages(map->count, map->pages);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300113
114#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
Juergen Grossb3f79312019-11-07 12:15:46 +0100115 kvfree(map->frames);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300116#endif
Juergen Grossb3f79312019-11-07 12:15:46 +0100117 kvfree(map->pages);
118 kvfree(map->grants);
119 kvfree(map->map_ops);
120 kvfree(map->unmap_ops);
121 kvfree(map->kmap_ops);
122 kvfree(map->kunmap_ops);
David Vrabela67baeb72012-10-24 12:39:02 +0100123 kfree(map);
124}
125
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300126struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300127 int dma_flags)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000128{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300129 struct gntdev_grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000130 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000131
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300132 add = kzalloc(sizeof(*add), GFP_KERNEL);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000133 if (NULL == add)
134 return NULL;
135
Jan Beulichf1d20d82021-03-10 11:46:13 +0100136 add->grants = kvmalloc_array(count, sizeof(add->grants[0]),
137 GFP_KERNEL);
138 add->map_ops = kvmalloc_array(count, sizeof(add->map_ops[0]),
139 GFP_KERNEL);
140 add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]),
141 GFP_KERNEL);
Juergen Grossb3f79312019-11-07 12:15:46 +0100142 add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000143 if (NULL == add->grants ||
144 NULL == add->map_ops ||
145 NULL == add->unmap_ops ||
146 NULL == add->pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000147 goto err;
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100148 if (use_ptemod) {
Jan Beulichf1d20d82021-03-10 11:46:13 +0100149 add->kmap_ops = kvmalloc_array(count, sizeof(add->kmap_ops[0]),
150 GFP_KERNEL);
151 add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]),
152 GFP_KERNEL);
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100153 if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
154 goto err;
155 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000156
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300157#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
158 add->dma_flags = dma_flags;
159
160 /*
161 * Check if this mapping is requested to be backed
162 * by a DMA buffer.
163 */
164 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
165 struct gnttab_dma_alloc_args args;
166
Juergen Grossb3f79312019-11-07 12:15:46 +0100167 add->frames = kvcalloc(count, sizeof(add->frames[0]),
168 GFP_KERNEL);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300169 if (!add->frames)
170 goto err;
171
172 /* Remember the device, so we can free DMA memory. */
173 add->dma_dev = priv->dma_dev;
174
175 args.dev = priv->dma_dev;
176 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
177 args.nr_pages = count;
178 args.pages = add->pages;
179 args.frames = add->frames;
180
181 if (gnttab_dma_alloc_pages(&args))
182 goto err;
183
184 add->dma_vaddr = args.vaddr;
185 add->dma_bus_addr = args.dev_bus_addr;
186 } else
187#endif
David Vrabelff4b1562015-01-08 18:06:01 +0000188 if (gnttab_alloc_pages(count, add->pages))
Daniel De Graafca47cea2011-03-09 18:07:34 -0500189 goto err;
190
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000191 for (i = 0; i < count; i++) {
Jan Beulichf1d20d82021-03-10 11:46:13 +0100192 add->grants[i].domid = DOMID_INVALID;
193 add->grants[i].ref = INVALID_GRANT_REF;
Jan Beulichbce21a22021-03-10 11:45:26 +0100194 add->map_ops[i].handle = INVALID_GRANT_HANDLE;
195 add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100196 if (use_ptemod) {
Jan Beulichbce21a22021-03-10 11:45:26 +0100197 add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
198 add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100199 }
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000200 }
201
Gerd Hoffmannab315232010-12-14 18:40:46 +0000202 add->index = 0;
203 add->count = count;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200204 refcount_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000205
Gerd Hoffmannab315232010-12-14 18:40:46 +0000206 return add;
207
208err:
David Vrabela67baeb72012-10-24 12:39:02 +0100209 gntdev_free_map(add);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000210 return NULL;
211}
212
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300213void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000214{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300215 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000216
217 list_for_each_entry(map, &priv->maps, next) {
218 if (add->index + add->count < map->index) {
219 list_add_tail(&add->next, &map->next);
220 goto done;
221 }
222 add->index = map->index + map->count;
223 }
224 list_add_tail(&add->next, &priv->maps);
225
226done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000227 gntdev_print_maps(priv, "[new]", add->index);
228}
229
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300230static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
231 int index, int count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000232{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300233 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000234
235 list_for_each_entry(map, &priv->maps, next) {
236 if (map->index != index)
237 continue;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500238 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000239 continue;
240 return map;
241 }
242 return NULL;
243}
244
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300245void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000246{
247 if (!map)
248 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000249
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200250 if (!refcount_dec_and_test(&map->users))
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500251 return;
252
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400253 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500254 notify_remote_via_evtchn(map->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400255 evtchn_put(map->notify.event);
256 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500257
David Vrabela67baeb72012-10-24 12:39:02 +0100258 if (map->pages && !use_ptemod)
259 unmap_grant_pages(map, 0, map->count);
260 gntdev_free_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000261}
262
263/* ------------------------------------------------------------------ */
264
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700265static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000266{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300267 struct gntdev_grant_map *map = data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000268 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500269 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000270 u64 pte_maddr;
271
272 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800273 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
274
David Vrabel923b2912014-12-18 14:56:54 +0000275 /*
276 * Set the PTE as special to force get_user_pages_fast() fall
277 * back to the slow path. If this is not supported as part of
278 * the grant map, it will be done afterwards.
279 */
280 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
281 flags |= (1 << _GNTMAP_guest_avail0);
282
Daniel De Graafaab8f112011-02-03 12:19:02 -0500283 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000284 map->grants[pgnr].ref,
285 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500286 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Jan Beulichbce21a22021-03-10 11:45:26 +0100287 INVALID_GRANT_HANDLE);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000288 return 0;
289}
290
David Vrabel923b2912014-12-18 14:56:54 +0000291#ifdef CONFIG_X86
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700292static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
David Vrabel923b2912014-12-18 14:56:54 +0000293{
294 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
295 return 0;
296}
297#endif
298
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300299int gntdev_map_grant_pages(struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000300{
301 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500302
303 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500304 /* Note: it could already be mapped */
Jan Beulichbce21a22021-03-10 11:45:26 +0100305 if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500306 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500307 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000308 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500309 pfn_to_kaddr(page_to_pfn(map->pages[i]));
310 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
311 map->grants[i].ref,
312 map->grants[i].domid);
313 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Jan Beulichbce21a22021-03-10 11:45:26 +0100314 map->flags, INVALID_GRANT_HANDLE);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500315 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100316 } else {
317 /*
318 * Setup the map_ops corresponding to the pte entries pointing
319 * to the kernel linear addresses of the struct pages.
320 * These ptes are completely different from the user ptes dealt
321 * with find_grant_ptes.
Jan Beulichdbe52832021-02-15 08:51:07 +0100322 * Note that GNTMAP_device_map isn't needed here: The
323 * dev_bus_addr output field gets consumed only from ->map_ops,
324 * and by not requesting it when mapping we also avoid needing
325 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
326 * reference to the page in the hypervisor).
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100327 */
Jan Beulichdbe52832021-02-15 08:51:07 +0100328 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
329 GNTMAP_host_map;
330
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100331 for (i = 0; i < map->count; i++) {
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100332 unsigned long address = (unsigned long)
333 pfn_to_kaddr(page_to_pfn(map->pages[i]));
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100334 BUG_ON(PageHighMem(map->pages[i]));
335
Jan Beulichdbe52832021-02-15 08:51:07 +0100336 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100337 map->grants[i].ref,
338 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000339 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
Jan Beulichbce21a22021-03-10 11:45:26 +0100340 flags, INVALID_GRANT_HANDLE);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100341 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500342 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000343
344 pr_debug("map %d+%d\n", map->index, map->count);
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100345 err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
346 map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000347
348 for (i = 0; i < map->count; i++) {
Jan Beulichebee0ea2021-02-15 08:52:27 +0100349 if (map->map_ops[i].status == GNTST_okay)
350 map->unmap_ops[i].handle = map->map_ops[i].handle;
351 else if (!err)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000352 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000353
Jan Beulichdbe52832021-02-15 08:51:07 +0100354 if (map->flags & GNTMAP_device_map)
355 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
356
Jan Beulichebee0ea2021-02-15 08:52:27 +0100357 if (use_ptemod) {
358 if (map->kmap_ops[i].status == GNTST_okay)
359 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
360 else if (!err)
361 err = -EINVAL;
362 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000363 }
364 return err;
365}
366
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300367static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
368 int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000369{
370 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000371 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000372
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500373 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
374 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500375 if (pgno >= offset && pgno < offset + pages) {
376 /* No need for kmap, pages are in lowmem */
377 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500378 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500379 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
380 }
381 }
382
Jennifer Herbert74528222015-01-05 15:07:46 +0000383 unmap_data.unmap_ops = map->unmap_ops + offset;
384 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
385 unmap_data.pages = map->pages + offset;
386 unmap_data.count = pages;
387
Bob Liub44166c2015-04-03 14:42:59 +0800388 err = gnttab_unmap_refs_sync(&unmap_data);
389 if (err)
390 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000391
392 for (i = 0; i < pages; i++) {
393 if (map->unmap_ops[offset+i].status)
394 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500395 pr_debug("unmap handle=%d st=%d\n",
396 map->unmap_ops[offset+i].handle,
397 map->unmap_ops[offset+i].status);
Jan Beulichbce21a22021-03-10 11:45:26 +0100398 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000399 }
400 return err;
401}
402
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300403static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
404 int pages)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500405{
406 int range, err = 0;
407
408 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
409
410 /* It is possible the requested range will have a "hole" where we
411 * already unmapped some of the grants. Only unmap valid ranges.
412 */
413 while (pages && !err) {
Jan Beulichbce21a22021-03-10 11:45:26 +0100414 while (pages &&
415 map->unmap_ops[offset].handle == INVALID_GRANT_HANDLE) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500416 offset++;
417 pages--;
418 }
419 range = 0;
420 while (range < pages) {
Jan Beulichbce21a22021-03-10 11:45:26 +0100421 if (map->unmap_ops[offset + range].handle ==
422 INVALID_GRANT_HANDLE)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500423 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500424 range++;
425 }
426 err = __unmap_grant_pages(map, offset, range);
427 offset += range;
428 pages -= range;
429 }
430
431 return err;
432}
433
Gerd Hoffmannab315232010-12-14 18:40:46 +0000434/* ------------------------------------------------------------------ */
435
Daniel De Graafd79647a2011-03-07 15:18:57 -0500436static void gntdev_vma_open(struct vm_area_struct *vma)
437{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300438 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500439
440 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200441 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500442}
443
Gerd Hoffmannab315232010-12-14 18:40:46 +0000444static void gntdev_vma_close(struct vm_area_struct *vma)
445{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300446 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000447 struct file *file = vma->vm_file;
448 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000449
Daniel De Graafd79647a2011-03-07 15:18:57 -0500450 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000451 if (use_ptemod) {
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400452 WARN_ON(map->vma != vma);
453 mmu_interval_notifier_remove(&map->notifier);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000454 map->vma = NULL;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000455 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000456 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000457 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000458}
459
David Vrabeldab069c2014-12-18 14:59:07 +0000460static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
461 unsigned long addr)
462{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300463 struct gntdev_grant_map *map = vma->vm_private_data;
David Vrabeldab069c2014-12-18 14:59:07 +0000464
465 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
466}
467
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700468static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500469 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000470 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000471 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000472};
473
474/* ------------------------------------------------------------------ */
475
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400476static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
477 const struct mmu_notifier_range *range,
478 unsigned long cur_seq)
Michal Hocko93065ac2018-08-21 21:52:33 -0700479{
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400480 struct gntdev_grant_map *map =
481 container_of(mn, struct gntdev_grant_map, notifier);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000482 unsigned long mstart, mend;
483 int err;
484
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400485 if (!mmu_notifier_range_blockable(range))
486 return false;
Michal Hocko58a57562018-09-05 09:21:39 +1000487
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400488 /*
489 * If the VMA is split or otherwise changed the notifier is not
490 * updated, but we don't want to process VA's outside the modified
491 * VMA. FIXME: It would be much more understandable to just prevent
492 * modifying the VMA in the first place.
493 */
494 if (map->vma->vm_start >= range->end ||
495 map->vma->vm_end <= range->start)
496 return true;
Michal Hocko58a57562018-09-05 09:21:39 +1000497
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400498 mstart = max(range->start, map->vma->vm_start);
499 mend = min(range->end, map->vma->vm_end);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000500 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
501 map->index, map->count,
502 map->vma->vm_start, map->vma->vm_end,
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400503 range->start, range->end, mstart, mend);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000504 err = unmap_grant_pages(map,
505 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
506 (mend - mstart) >> PAGE_SHIFT);
507 WARN_ON(err);
Michal Hocko58a57562018-09-05 09:21:39 +1000508
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400509 return true;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000510}
511
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400512static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
513 .invalidate = gntdev_invalidate,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000514};
515
516/* ------------------------------------------------------------------ */
517
518static int gntdev_open(struct inode *inode, struct file *flip)
519{
520 struct gntdev_priv *priv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000521
522 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
523 if (!priv)
524 return -ENOMEM;
525
526 INIT_LIST_HEAD(&priv->maps);
David Vrabel1401c002015-01-09 18:06:12 +0000527 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000528
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300529#ifdef CONFIG_XEN_GNTDEV_DMABUF
Oleksandr Andrushchenkofa13e662019-02-14 16:23:20 +0200530 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300531 if (IS_ERR(priv->dmabuf_priv)) {
Colin Ian Kingd41b26d2019-11-11 12:20:09 +0000532 int ret = PTR_ERR(priv->dmabuf_priv);
533
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300534 kfree(priv);
535 return ret;
536 }
537#endif
538
Gerd Hoffmannab315232010-12-14 18:40:46 +0000539 flip->private_data = priv;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300540#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
541 priv->dma_dev = gntdev_miscdev.this_device;
Rob Herringee7f5222019-10-08 14:41:55 -0500542 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300543#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000544 pr_debug("priv %p\n", priv);
545
546 return 0;
547}
548
549static int gntdev_release(struct inode *inode, struct file *flip)
550{
551 struct gntdev_priv *priv = flip->private_data;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300552 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000553
554 pr_debug("priv %p\n", priv);
555
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200556 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000557 while (!list_empty(&priv->maps)) {
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300558 map = list_entry(priv->maps.next,
559 struct gntdev_grant_map, next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500560 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000561 gntdev_put_map(NULL /* already removed */, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000562 }
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200563 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000564
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300565#ifdef CONFIG_XEN_GNTDEV_DMABUF
566 gntdev_dmabuf_fini(priv->dmabuf_priv);
567#endif
568
Gerd Hoffmannab315232010-12-14 18:40:46 +0000569 kfree(priv);
570 return 0;
571}
572
573static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
574 struct ioctl_gntdev_map_grant_ref __user *u)
575{
576 struct ioctl_gntdev_map_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300577 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000578 int err;
579
580 if (copy_from_user(&op, u, sizeof(op)) != 0)
581 return -EFAULT;
582 pr_debug("priv %p, add %d\n", priv, op.count);
Juergen Gross3b06ac62019-11-07 12:15:45 +0100583 if (unlikely(gntdev_test_page_count(op.count)))
Gerd Hoffmannab315232010-12-14 18:40:46 +0000584 return -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000585
586 err = -ENOMEM;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300587 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000588 if (!map)
589 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500590
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500591 if (copy_from_user(map->grants, &u->refs,
592 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000593 gntdev_put_map(NULL, map);
594 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500595 }
596
David Vrabel1401c002015-01-09 18:06:12 +0000597 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000598 gntdev_add_map(priv, map);
599 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000600 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000601
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500602 if (copy_to_user(u, &op, sizeof(op)) != 0)
603 return -EFAULT;
604
Gerd Hoffmannab315232010-12-14 18:40:46 +0000605 return 0;
606}
607
608static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
609 struct ioctl_gntdev_unmap_grant_ref __user *u)
610{
611 struct ioctl_gntdev_unmap_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300612 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000613 int err = -ENOENT;
614
615 if (copy_from_user(&op, u, sizeof(op)) != 0)
616 return -EFAULT;
617 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
618
David Vrabel1401c002015-01-09 18:06:12 +0000619 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000620 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500621 if (map) {
622 list_del(&map->next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500623 err = 0;
624 }
David Vrabel1401c002015-01-09 18:06:12 +0000625 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400626 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000627 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000628 return err;
629}
630
631static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
632 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
633{
634 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500635 struct vm_area_struct *vma;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300636 struct gntdev_grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000637 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000638
639 if (copy_from_user(&op, u, sizeof(op)) != 0)
640 return -EFAULT;
641 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
642
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700643 mmap_read_lock(current->mm);
Daniel De Graafa8792112011-02-03 12:19:00 -0500644 vma = find_vma(current->mm, op.vaddr);
645 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000646 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500647
648 map = vma->vm_private_data;
649 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000650 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500651
Gerd Hoffmannab315232010-12-14 18:40:46 +0000652 op.offset = map->index << PAGE_SHIFT;
653 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000654 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000655
Daniel De Graaf2512f292013-01-02 22:57:11 +0000656 out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700657 mmap_read_unlock(current->mm);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000658
659 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000660 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000661 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000662}
663
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500664static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
665{
666 struct ioctl_gntdev_unmap_notify op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300667 struct gntdev_grant_map *map;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500668 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400669 int out_flags;
Yan Yankovskyi0102e4e2020-03-23 18:15:11 +0200670 evtchn_port_t out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500671
672 if (copy_from_user(&op, u, sizeof(op)))
673 return -EFAULT;
674
675 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
676 return -EINVAL;
677
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400678 /* We need to grab a reference to the event channel we are going to use
679 * to send the notify before releasing the reference we may already have
680 * (if someone has called this ioctl twice). This is required so that
681 * it is possible to change the clear_byte part of the notification
682 * without disturbing the event channel part, which may now be the last
683 * reference to that event channel.
684 */
685 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
686 if (evtchn_get(op.event_channel_port))
687 return -EINVAL;
688 }
689
690 out_flags = op.action;
691 out_event = op.event_channel_port;
692
David Vrabel1401c002015-01-09 18:06:12 +0000693 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500694
695 list_for_each_entry(map, &priv->maps, next) {
696 uint64_t begin = map->index << PAGE_SHIFT;
697 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
698 if (op.index >= begin && op.index < end)
699 goto found;
700 }
701 rc = -ENOENT;
702 goto unlock_out;
703
704 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500705 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
706 (map->flags & GNTMAP_readonly)) {
707 rc = -EINVAL;
708 goto unlock_out;
709 }
710
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400711 out_flags = map->notify.flags;
712 out_event = map->notify.event;
713
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500714 map->notify.flags = op.action;
715 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
716 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400717
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500718 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400719
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500720 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000721 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400722
723 /* Drop the reference to the event channel we did not save in the map */
724 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
725 evtchn_put(out_event);
726
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500727 return rc;
728}
729
David Vrabel36ae2202016-05-09 10:59:48 +0100730#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000731
732struct gntdev_copy_batch {
733 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
734 struct page *pages[GNTDEV_COPY_BATCH];
735 s16 __user *status[GNTDEV_COPY_BATCH];
736 unsigned int nr_ops;
737 unsigned int nr_pages;
Souptick Joarder77905582020-09-06 12:21:53 +0530738 bool writeable;
David Vrabela4cdb552014-12-02 16:13:26 +0000739};
740
741static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
Souptick Joarder77905582020-09-06 12:21:53 +0530742 unsigned long *gfn)
David Vrabela4cdb552014-12-02 16:13:26 +0000743{
744 unsigned long addr = (unsigned long)virt;
745 struct page *page;
746 unsigned long xen_pfn;
747 int ret;
748
Souptick Joarderd6bbc2f2020-09-06 12:21:54 +0530749 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
David Vrabela4cdb552014-12-02 16:13:26 +0000750 if (ret < 0)
751 return ret;
752
753 batch->pages[batch->nr_pages++] = page;
754
755 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
756 *gfn = pfn_to_gfn(xen_pfn);
757
758 return 0;
759}
760
761static void gntdev_put_pages(struct gntdev_copy_batch *batch)
762{
Souptick Joarderd6bbc2f2020-09-06 12:21:54 +0530763 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
David Vrabela4cdb552014-12-02 16:13:26 +0000764 batch->nr_pages = 0;
Souptick Joarder77905582020-09-06 12:21:53 +0530765 batch->writeable = false;
David Vrabela4cdb552014-12-02 16:13:26 +0000766}
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);
Souptick Joarder77905582020-09-06 12:21:53 +0530854 batch->writeable = false;
David Vrabela4cdb552014-12-02 16:13:26 +0000855
Souptick Joarder77905582020-09-06 12:21:53 +0530856 ret = gntdev_get_page(batch, virt, &gfn);
David Vrabela4cdb552014-12-02 16:13:26 +0000857 if (ret < 0)
858 return ret;
859
860 op->source.u.gmfn = gfn;
861 op->source.domid = DOMID_SELF;
862 op->source.offset = off;
863 }
864
865 if (seg->flags & GNTCOPY_dest_gref) {
866 op->dest.u.ref = seg->dest.foreign.ref;
867 op->dest.domid = seg->dest.foreign.domid;
868 op->dest.offset = seg->dest.foreign.offset + copied;
869 op->flags |= GNTCOPY_dest_gref;
870 } else {
871 virt = seg->dest.virt + copied;
872 off = (unsigned long)virt & ~XEN_PAGE_MASK;
873 len = min(len, (size_t)XEN_PAGE_SIZE - off);
Souptick Joarder77905582020-09-06 12:21:53 +0530874 batch->writeable = true;
David Vrabela4cdb552014-12-02 16:13:26 +0000875
Souptick Joarder77905582020-09-06 12:21:53 +0530876 ret = gntdev_get_page(batch, virt, &gfn);
David Vrabela4cdb552014-12-02 16:13:26 +0000877 if (ret < 0)
878 return ret;
879
880 op->dest.u.gmfn = gfn;
881 op->dest.domid = DOMID_SELF;
882 op->dest.offset = off;
883 }
884
885 op->len = len;
886 copied += len;
887
888 batch->status[batch->nr_ops] = status;
889 batch->nr_ops++;
890 }
891
892 return 0;
893}
894
895static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
896{
897 struct ioctl_gntdev_grant_copy copy;
898 struct gntdev_copy_batch batch;
899 unsigned int i;
900 int ret = 0;
901
902 if (copy_from_user(&copy, u, sizeof(copy)))
903 return -EFAULT;
904
905 batch.nr_ops = 0;
906 batch.nr_pages = 0;
907
908 for (i = 0; i < copy.count; i++) {
909 struct gntdev_grant_copy_segment seg;
910
911 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
912 ret = -EFAULT;
913 goto out;
914 }
915
916 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
917 if (ret < 0)
918 goto out;
919
920 cond_resched();
921 }
922 if (batch.nr_ops)
923 ret = gntdev_copy(&batch);
924 return ret;
925
926 out:
927 gntdev_put_pages(&batch);
928 return ret;
929}
930
Gerd Hoffmannab315232010-12-14 18:40:46 +0000931static long gntdev_ioctl(struct file *flip,
932 unsigned int cmd, unsigned long arg)
933{
934 struct gntdev_priv *priv = flip->private_data;
935 void __user *ptr = (void __user *)arg;
936
937 switch (cmd) {
938 case IOCTL_GNTDEV_MAP_GRANT_REF:
939 return gntdev_ioctl_map_grant_ref(priv, ptr);
940
941 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
942 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
943
944 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
945 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
946
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500947 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
948 return gntdev_ioctl_notify(priv, ptr);
949
David Vrabela4cdb552014-12-02 16:13:26 +0000950 case IOCTL_GNTDEV_GRANT_COPY:
951 return gntdev_ioctl_grant_copy(priv, ptr);
952
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300953#ifdef CONFIG_XEN_GNTDEV_DMABUF
954 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
955 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
956
957 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
958 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
959
960 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
961 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
962
963 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
964 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
965#endif
966
Gerd Hoffmannab315232010-12-14 18:40:46 +0000967 default:
968 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
969 return -ENOIOCTLCMD;
970 }
971
972 return 0;
973}
974
975static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
976{
977 struct gntdev_priv *priv = flip->private_data;
978 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +0530979 int count = vma_pages(vma);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300980 struct gntdev_grant_map *map;
Souptick Joarderdf9bde02019-05-13 17:22:23 -0700981 int err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000982
983 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
984 return -EINVAL;
985
986 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
987 index, count, vma->vm_start, vma->vm_pgoff);
988
David Vrabel1401c002015-01-09 18:06:12 +0000989 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000990 map = gntdev_find_map_index(priv, index, count);
991 if (!map)
992 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500993 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000994 goto unlock_out;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200995 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500996
Gerd Hoffmannab315232010-12-14 18:40:46 +0000997 vma->vm_ops = &gntdev_vmops;
998
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -0500999 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -05001000
1001 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +01001002 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001003
1004 vma->vm_private_data = map;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001005 if (map->flags) {
1006 if ((vma->vm_flags & VM_WRITE) &&
1007 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001008 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001009 } else {
1010 map->flags = GNTMAP_host_map;
1011 if (!(vma->vm_flags & VM_WRITE))
1012 map->flags |= GNTMAP_readonly;
1013 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001014
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001015 if (use_ptemod) {
1016 map->vma = vma;
1017 err = mmu_interval_notifier_insert_locked(
1018 &map->notifier, vma->vm_mm, vma->vm_start,
1019 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1020 if (err)
1021 goto out_unlock_put;
1022 }
David Vrabel1401c002015-01-09 18:06:12 +00001023 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001024
Daniel De Graafaab8f112011-02-03 12:19:02 -05001025 if (use_ptemod) {
Boris Ostrovsky92937242020-01-28 10:31:26 -05001026 /*
1027 * gntdev takes the address of the PTE in find_grant_ptes() and
1028 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1029 * purpose of the notifier is to prevent the hypervisor pointer
1030 * to the PTE from going stale.
1031 *
1032 * Since this vma's mappings can't be touched without the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001033 * mmap_lock, and we are holding it now, there is no need for
Boris Ostrovsky92937242020-01-28 10:31:26 -05001034 * the notifier_range locking pattern.
1035 */
1036 mmu_interval_read_begin(&map->notifier);
1037
Juergen Gross298d2752017-10-25 17:08:07 +02001038 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001039 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1040 vma->vm_end - vma->vm_start,
1041 find_grant_ptes, map);
1042 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001043 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001044 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001045 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001046 }
1047
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001048 err = gntdev_map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001049 if (err)
1050 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001051
Daniel De Graafaab8f112011-02-03 12:19:02 -05001052 if (!use_ptemod) {
Souptick Joarder8d1502f2019-07-31 00:04:56 +05301053 err = vm_map_pages_zero(vma, map->pages, map->count);
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001054 if (err)
1055 goto out_put_map;
David Vrabel923b2912014-12-18 14:56:54 +00001056 } else {
1057#ifdef CONFIG_X86
1058 /*
1059 * If the PTEs were not made special by the grant map
1060 * hypercall, do so here.
1061 *
1062 * This is racy since the mapping is already visible
1063 * to userspace but userspace should be well-behaved
1064 * enough to not touch it until the mmap() call
1065 * returns.
1066 */
1067 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1068 apply_to_page_range(vma->vm_mm, vma->vm_start,
1069 vma->vm_end - vma->vm_start,
1070 set_grant_ptes_as_special, NULL);
1071 }
1072#endif
Daniel De Graafaab8f112011-02-03 12:19:02 -05001073 }
1074
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001075 return 0;
1076
Gerd Hoffmannab315232010-12-14 18:40:46 +00001077unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001078 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001079 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001080
Dan Carpentera93e20a2011-03-19 08:45:43 +03001081out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001082 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001083out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001084 if (use_ptemod) {
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001085 unmap_grant_pages(map, 0, map->count);
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001086 if (map->vma) {
1087 mmu_interval_notifier_remove(&map->notifier);
1088 map->vma = NULL;
1089 }
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001090 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001091 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001092 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001093}
1094
1095static const struct file_operations gntdev_fops = {
1096 .owner = THIS_MODULE,
1097 .open = gntdev_open,
1098 .release = gntdev_release,
1099 .mmap = gntdev_mmap,
1100 .unlocked_ioctl = gntdev_ioctl
1101};
1102
1103static struct miscdevice gntdev_miscdev = {
1104 .minor = MISC_DYNAMIC_MINOR,
1105 .name = "xen/gntdev",
1106 .fops = &gntdev_fops,
1107};
1108
1109/* ------------------------------------------------------------------ */
1110
1111static int __init gntdev_init(void)
1112{
1113 int err;
1114
1115 if (!xen_domain())
1116 return -ENODEV;
1117
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001118 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001119
Gerd Hoffmannab315232010-12-14 18:40:46 +00001120 err = misc_register(&gntdev_miscdev);
1121 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001122 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001123 return err;
1124 }
1125 return 0;
1126}
1127
1128static void __exit gntdev_exit(void)
1129{
1130 misc_deregister(&gntdev_miscdev);
1131}
1132
1133module_init(gntdev_init);
1134module_exit(gntdev_exit);
1135
1136/* ------------------------------------------------------------------ */