blob: fec1b6537166502e5b6dfedf058f83ffcd4e2e63 [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;
Juergen Gross30dcc562021-07-30 09:18:04 +0200269 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
270 (1 << _GNTMAP_guest_avail0);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000271 u64 pte_maddr;
272
273 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800274 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
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,
Jan Beulichbce21a22021-03-10 11:45:26 +0100280 INVALID_GRANT_HANDLE);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000281 return 0;
282}
283
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300284int gntdev_map_grant_pages(struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000285{
286 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500287
288 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500289 /* Note: it could already be mapped */
Jan Beulichbce21a22021-03-10 11:45:26 +0100290 if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500291 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500292 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000293 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500294 pfn_to_kaddr(page_to_pfn(map->pages[i]));
295 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
296 map->grants[i].ref,
297 map->grants[i].domid);
298 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Jan Beulichbce21a22021-03-10 11:45:26 +0100299 map->flags, INVALID_GRANT_HANDLE);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500300 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100301 } else {
302 /*
303 * Setup the map_ops corresponding to the pte entries pointing
304 * to the kernel linear addresses of the struct pages.
305 * These ptes are completely different from the user ptes dealt
306 * with find_grant_ptes.
Jan Beulichdbe52832021-02-15 08:51:07 +0100307 * Note that GNTMAP_device_map isn't needed here: The
308 * dev_bus_addr output field gets consumed only from ->map_ops,
309 * and by not requesting it when mapping we also avoid needing
310 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
311 * reference to the page in the hypervisor).
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100312 */
Jan Beulichdbe52832021-02-15 08:51:07 +0100313 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
314 GNTMAP_host_map;
315
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100316 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
Jan Beulichdbe52832021-02-15 08:51:07 +0100321 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100322 map->grants[i].ref,
323 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000324 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
Jan Beulichbce21a22021-03-10 11:45:26 +0100325 flags, INVALID_GRANT_HANDLE);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100326 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500327 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000328
329 pr_debug("map %d+%d\n", map->index, map->count);
Jan Beulich36caa3fe2021-03-10 11:45:00 +0100330 err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
331 map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000332
333 for (i = 0; i < map->count; i++) {
Jan Beulichebee0ea2021-02-15 08:52:27 +0100334 if (map->map_ops[i].status == GNTST_okay)
335 map->unmap_ops[i].handle = map->map_ops[i].handle;
336 else if (!err)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000337 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000338
Jan Beulichdbe52832021-02-15 08:51:07 +0100339 if (map->flags & GNTMAP_device_map)
340 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
341
Jan Beulichebee0ea2021-02-15 08:52:27 +0100342 if (use_ptemod) {
343 if (map->kmap_ops[i].status == GNTST_okay)
344 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
345 else if (!err)
346 err = -EINVAL;
347 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000348 }
349 return err;
350}
351
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300352static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
353 int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000354{
355 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000356 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000357
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500358 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
359 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500360 if (pgno >= offset && pgno < offset + pages) {
361 /* No need for kmap, pages are in lowmem */
362 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500363 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500364 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
365 }
366 }
367
Jennifer Herbert74528222015-01-05 15:07:46 +0000368 unmap_data.unmap_ops = map->unmap_ops + offset;
369 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
370 unmap_data.pages = map->pages + offset;
371 unmap_data.count = pages;
372
Bob Liub44166c2015-04-03 14:42:59 +0800373 err = gnttab_unmap_refs_sync(&unmap_data);
374 if (err)
375 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000376
377 for (i = 0; i < pages; i++) {
378 if (map->unmap_ops[offset+i].status)
379 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500380 pr_debug("unmap handle=%d st=%d\n",
381 map->unmap_ops[offset+i].handle,
382 map->unmap_ops[offset+i].status);
Jan Beulichbce21a22021-03-10 11:45:26 +0100383 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
Jan Beulichf28347c2021-09-17 08:13:08 +0200384 if (use_ptemod) {
385 if (map->kunmap_ops[offset+i].status)
386 err = -EINVAL;
387 pr_debug("kunmap handle=%u st=%d\n",
388 map->kunmap_ops[offset+i].handle,
389 map->kunmap_ops[offset+i].status);
390 map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
391 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000392 }
393 return err;
394}
395
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300396static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
397 int pages)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500398{
399 int range, err = 0;
400
401 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
402
403 /* It is possible the requested range will have a "hole" where we
404 * already unmapped some of the grants. Only unmap valid ranges.
405 */
406 while (pages && !err) {
Jan Beulichbce21a22021-03-10 11:45:26 +0100407 while (pages &&
408 map->unmap_ops[offset].handle == INVALID_GRANT_HANDLE) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500409 offset++;
410 pages--;
411 }
412 range = 0;
413 while (range < pages) {
Jan Beulichbce21a22021-03-10 11:45:26 +0100414 if (map->unmap_ops[offset + range].handle ==
415 INVALID_GRANT_HANDLE)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500416 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500417 range++;
418 }
419 err = __unmap_grant_pages(map, offset, range);
420 offset += range;
421 pages -= range;
422 }
423
424 return err;
425}
426
Gerd Hoffmannab315232010-12-14 18:40:46 +0000427/* ------------------------------------------------------------------ */
428
Daniel De Graafd79647a2011-03-07 15:18:57 -0500429static void gntdev_vma_open(struct vm_area_struct *vma)
430{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300431 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500432
433 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200434 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500435}
436
Gerd Hoffmannab315232010-12-14 18:40:46 +0000437static void gntdev_vma_close(struct vm_area_struct *vma)
438{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300439 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000440 struct file *file = vma->vm_file;
441 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000442
Daniel De Graafd79647a2011-03-07 15:18:57 -0500443 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000444 if (use_ptemod) {
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400445 WARN_ON(map->vma != vma);
446 mmu_interval_notifier_remove(&map->notifier);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000447 map->vma = NULL;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000448 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000449 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000450 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000451}
452
David Vrabeldab069c2014-12-18 14:59:07 +0000453static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
454 unsigned long addr)
455{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300456 struct gntdev_grant_map *map = vma->vm_private_data;
David Vrabeldab069c2014-12-18 14:59:07 +0000457
458 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
459}
460
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700461static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500462 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000463 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000464 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000465};
466
467/* ------------------------------------------------------------------ */
468
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400469static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
470 const struct mmu_notifier_range *range,
471 unsigned long cur_seq)
Michal Hocko93065ac2018-08-21 21:52:33 -0700472{
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400473 struct gntdev_grant_map *map =
474 container_of(mn, struct gntdev_grant_map, notifier);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000475 unsigned long mstart, mend;
476 int err;
477
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400478 if (!mmu_notifier_range_blockable(range))
479 return false;
Michal Hocko58a57562018-09-05 09:21:39 +1000480
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400481 /*
482 * If the VMA is split or otherwise changed the notifier is not
483 * updated, but we don't want to process VA's outside the modified
484 * VMA. FIXME: It would be much more understandable to just prevent
485 * modifying the VMA in the first place.
486 */
487 if (map->vma->vm_start >= range->end ||
488 map->vma->vm_end <= range->start)
489 return true;
Michal Hocko58a57562018-09-05 09:21:39 +1000490
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400491 mstart = max(range->start, map->vma->vm_start);
492 mend = min(range->end, map->vma->vm_end);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000493 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
494 map->index, map->count,
495 map->vma->vm_start, map->vma->vm_end,
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400496 range->start, range->end, mstart, mend);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000497 err = unmap_grant_pages(map,
498 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
499 (mend - mstart) >> PAGE_SHIFT);
500 WARN_ON(err);
Michal Hocko58a57562018-09-05 09:21:39 +1000501
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400502 return true;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000503}
504
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -0400505static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
506 .invalidate = gntdev_invalidate,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000507};
508
509/* ------------------------------------------------------------------ */
510
511static int gntdev_open(struct inode *inode, struct file *flip)
512{
513 struct gntdev_priv *priv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000514
515 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
516 if (!priv)
517 return -ENOMEM;
518
519 INIT_LIST_HEAD(&priv->maps);
David Vrabel1401c002015-01-09 18:06:12 +0000520 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000521
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300522#ifdef CONFIG_XEN_GNTDEV_DMABUF
Oleksandr Andrushchenkofa13e662019-02-14 16:23:20 +0200523 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300524 if (IS_ERR(priv->dmabuf_priv)) {
Colin Ian Kingd41b26d2019-11-11 12:20:09 +0000525 int ret = PTR_ERR(priv->dmabuf_priv);
526
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300527 kfree(priv);
528 return ret;
529 }
530#endif
531
Gerd Hoffmannab315232010-12-14 18:40:46 +0000532 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);
Juergen Gross3b06ac62019-11-07 12:15:45 +0100576 if (unlikely(gntdev_test_page_count(op.count)))
Gerd Hoffmannab315232010-12-14 18:40:46 +0000577 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
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500584 if (copy_from_user(map->grants, &u->refs,
585 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000586 gntdev_put_map(NULL, map);
587 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500588 }
589
David Vrabel1401c002015-01-09 18:06:12 +0000590 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000591 gntdev_add_map(priv, map);
592 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000593 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000594
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500595 if (copy_to_user(u, &op, sizeof(op)) != 0)
596 return -EFAULT;
597
Gerd Hoffmannab315232010-12-14 18:40:46 +0000598 return 0;
599}
600
601static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
602 struct ioctl_gntdev_unmap_grant_ref __user *u)
603{
604 struct ioctl_gntdev_unmap_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300605 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000606 int err = -ENOENT;
607
608 if (copy_from_user(&op, u, sizeof(op)) != 0)
609 return -EFAULT;
610 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
611
David Vrabel1401c002015-01-09 18:06:12 +0000612 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000613 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500614 if (map) {
615 list_del(&map->next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500616 err = 0;
617 }
David Vrabel1401c002015-01-09 18:06:12 +0000618 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400619 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000620 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000621 return err;
622}
623
624static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
625 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
626{
627 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500628 struct vm_area_struct *vma;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300629 struct gntdev_grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000630 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000631
632 if (copy_from_user(&op, u, sizeof(op)) != 0)
633 return -EFAULT;
634 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
635
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700636 mmap_read_lock(current->mm);
Daniel De Graafa8792112011-02-03 12:19:00 -0500637 vma = find_vma(current->mm, op.vaddr);
638 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000639 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500640
641 map = vma->vm_private_data;
642 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000643 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500644
Gerd Hoffmannab315232010-12-14 18:40:46 +0000645 op.offset = map->index << PAGE_SHIFT;
646 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000647 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000648
Daniel De Graaf2512f292013-01-02 22:57:11 +0000649 out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700650 mmap_read_unlock(current->mm);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000651
652 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000653 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000654 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000655}
656
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500657static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
658{
659 struct ioctl_gntdev_unmap_notify op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300660 struct gntdev_grant_map *map;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500661 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400662 int out_flags;
Yan Yankovskyi0102e4e2020-03-23 18:15:11 +0200663 evtchn_port_t out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500664
665 if (copy_from_user(&op, u, sizeof(op)))
666 return -EFAULT;
667
668 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
669 return -EINVAL;
670
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400671 /* We need to grab a reference to the event channel we are going to use
672 * to send the notify before releasing the reference we may already have
673 * (if someone has called this ioctl twice). This is required so that
674 * it is possible to change the clear_byte part of the notification
675 * without disturbing the event channel part, which may now be the last
676 * reference to that event channel.
677 */
678 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
679 if (evtchn_get(op.event_channel_port))
680 return -EINVAL;
681 }
682
683 out_flags = op.action;
684 out_event = op.event_channel_port;
685
David Vrabel1401c002015-01-09 18:06:12 +0000686 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500687
688 list_for_each_entry(map, &priv->maps, next) {
689 uint64_t begin = map->index << PAGE_SHIFT;
690 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
691 if (op.index >= begin && op.index < end)
692 goto found;
693 }
694 rc = -ENOENT;
695 goto unlock_out;
696
697 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500698 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
699 (map->flags & GNTMAP_readonly)) {
700 rc = -EINVAL;
701 goto unlock_out;
702 }
703
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400704 out_flags = map->notify.flags;
705 out_event = map->notify.event;
706
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500707 map->notify.flags = op.action;
708 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
709 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400710
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500711 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400712
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500713 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000714 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400715
716 /* Drop the reference to the event channel we did not save in the map */
717 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
718 evtchn_put(out_event);
719
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500720 return rc;
721}
722
David Vrabel36ae2202016-05-09 10:59:48 +0100723#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000724
725struct gntdev_copy_batch {
726 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
727 struct page *pages[GNTDEV_COPY_BATCH];
728 s16 __user *status[GNTDEV_COPY_BATCH];
729 unsigned int nr_ops;
730 unsigned int nr_pages;
Souptick Joarder77905582020-09-06 12:21:53 +0530731 bool writeable;
David Vrabela4cdb552014-12-02 16:13:26 +0000732};
733
734static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
Souptick Joarder77905582020-09-06 12:21:53 +0530735 unsigned long *gfn)
David Vrabela4cdb552014-12-02 16:13:26 +0000736{
737 unsigned long addr = (unsigned long)virt;
738 struct page *page;
739 unsigned long xen_pfn;
740 int ret;
741
Souptick Joarderd6bbc2f2020-09-06 12:21:54 +0530742 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
David Vrabela4cdb552014-12-02 16:13:26 +0000743 if (ret < 0)
744 return ret;
745
746 batch->pages[batch->nr_pages++] = page;
747
748 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
749 *gfn = pfn_to_gfn(xen_pfn);
750
751 return 0;
752}
753
754static void gntdev_put_pages(struct gntdev_copy_batch *batch)
755{
Souptick Joarderd6bbc2f2020-09-06 12:21:54 +0530756 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
David Vrabela4cdb552014-12-02 16:13:26 +0000757 batch->nr_pages = 0;
Souptick Joarder77905582020-09-06 12:21:53 +0530758 batch->writeable = false;
David Vrabela4cdb552014-12-02 16:13:26 +0000759}
760
761static int gntdev_copy(struct gntdev_copy_batch *batch)
762{
763 unsigned int i;
764
765 gnttab_batch_copy(batch->ops, batch->nr_ops);
766 gntdev_put_pages(batch);
767
768 /*
769 * For each completed op, update the status if the op failed
770 * and all previous ops for the segment were successful.
771 */
772 for (i = 0; i < batch->nr_ops; i++) {
773 s16 status = batch->ops[i].status;
774 s16 old_status;
775
776 if (status == GNTST_okay)
777 continue;
778
779 if (__get_user(old_status, batch->status[i]))
780 return -EFAULT;
781
782 if (old_status != GNTST_okay)
783 continue;
784
785 if (__put_user(status, batch->status[i]))
786 return -EFAULT;
787 }
788
789 batch->nr_ops = 0;
790 return 0;
791}
792
793static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
794 struct gntdev_grant_copy_segment *seg,
795 s16 __user *status)
796{
797 uint16_t copied = 0;
798
799 /*
800 * Disallow local -> local copies since there is only space in
801 * batch->pages for one page per-op and this would be a very
802 * expensive memcpy().
803 */
804 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
805 return -EINVAL;
806
807 /* Can't cross page if source/dest is a grant ref. */
808 if (seg->flags & GNTCOPY_source_gref) {
809 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
810 return -EINVAL;
811 }
812 if (seg->flags & GNTCOPY_dest_gref) {
813 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
814 return -EINVAL;
815 }
816
817 if (put_user(GNTST_okay, status))
818 return -EFAULT;
819
820 while (copied < seg->len) {
821 struct gnttab_copy *op;
822 void __user *virt;
823 size_t len, off;
824 unsigned long gfn;
825 int ret;
826
827 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
828 ret = gntdev_copy(batch);
829 if (ret < 0)
830 return ret;
831 }
832
833 len = seg->len - copied;
834
835 op = &batch->ops[batch->nr_ops];
836 op->flags = 0;
837
838 if (seg->flags & GNTCOPY_source_gref) {
839 op->source.u.ref = seg->source.foreign.ref;
840 op->source.domid = seg->source.foreign.domid;
841 op->source.offset = seg->source.foreign.offset + copied;
842 op->flags |= GNTCOPY_source_gref;
843 } else {
844 virt = seg->source.virt + copied;
845 off = (unsigned long)virt & ~XEN_PAGE_MASK;
846 len = min(len, (size_t)XEN_PAGE_SIZE - off);
Souptick Joarder77905582020-09-06 12:21:53 +0530847 batch->writeable = false;
David Vrabela4cdb552014-12-02 16:13:26 +0000848
Souptick Joarder77905582020-09-06 12:21:53 +0530849 ret = gntdev_get_page(batch, virt, &gfn);
David Vrabela4cdb552014-12-02 16:13:26 +0000850 if (ret < 0)
851 return ret;
852
853 op->source.u.gmfn = gfn;
854 op->source.domid = DOMID_SELF;
855 op->source.offset = off;
856 }
857
858 if (seg->flags & GNTCOPY_dest_gref) {
859 op->dest.u.ref = seg->dest.foreign.ref;
860 op->dest.domid = seg->dest.foreign.domid;
861 op->dest.offset = seg->dest.foreign.offset + copied;
862 op->flags |= GNTCOPY_dest_gref;
863 } else {
864 virt = seg->dest.virt + copied;
865 off = (unsigned long)virt & ~XEN_PAGE_MASK;
866 len = min(len, (size_t)XEN_PAGE_SIZE - off);
Souptick Joarder77905582020-09-06 12:21:53 +0530867 batch->writeable = true;
David Vrabela4cdb552014-12-02 16:13:26 +0000868
Souptick Joarder77905582020-09-06 12:21:53 +0530869 ret = gntdev_get_page(batch, virt, &gfn);
David Vrabela4cdb552014-12-02 16:13:26 +0000870 if (ret < 0)
871 return ret;
872
873 op->dest.u.gmfn = gfn;
874 op->dest.domid = DOMID_SELF;
875 op->dest.offset = off;
876 }
877
878 op->len = len;
879 copied += len;
880
881 batch->status[batch->nr_ops] = status;
882 batch->nr_ops++;
883 }
884
885 return 0;
886}
887
888static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
889{
890 struct ioctl_gntdev_grant_copy copy;
891 struct gntdev_copy_batch batch;
892 unsigned int i;
893 int ret = 0;
894
895 if (copy_from_user(&copy, u, sizeof(copy)))
896 return -EFAULT;
897
898 batch.nr_ops = 0;
899 batch.nr_pages = 0;
900
901 for (i = 0; i < copy.count; i++) {
902 struct gntdev_grant_copy_segment seg;
903
904 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
905 ret = -EFAULT;
906 goto out;
907 }
908
909 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
910 if (ret < 0)
911 goto out;
912
913 cond_resched();
914 }
915 if (batch.nr_ops)
916 ret = gntdev_copy(&batch);
917 return ret;
918
919 out:
920 gntdev_put_pages(&batch);
921 return ret;
922}
923
Gerd Hoffmannab315232010-12-14 18:40:46 +0000924static long gntdev_ioctl(struct file *flip,
925 unsigned int cmd, unsigned long arg)
926{
927 struct gntdev_priv *priv = flip->private_data;
928 void __user *ptr = (void __user *)arg;
929
930 switch (cmd) {
931 case IOCTL_GNTDEV_MAP_GRANT_REF:
932 return gntdev_ioctl_map_grant_ref(priv, ptr);
933
934 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
935 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
936
937 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
938 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
939
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500940 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
941 return gntdev_ioctl_notify(priv, ptr);
942
David Vrabela4cdb552014-12-02 16:13:26 +0000943 case IOCTL_GNTDEV_GRANT_COPY:
944 return gntdev_ioctl_grant_copy(priv, ptr);
945
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300946#ifdef CONFIG_XEN_GNTDEV_DMABUF
947 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
948 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
949
950 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
951 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
952
953 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
954 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
955
956 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
957 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
958#endif
959
Gerd Hoffmannab315232010-12-14 18:40:46 +0000960 default:
961 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
962 return -ENOIOCTLCMD;
963 }
964
965 return 0;
966}
967
968static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
969{
970 struct gntdev_priv *priv = flip->private_data;
971 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +0530972 int count = vma_pages(vma);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300973 struct gntdev_grant_map *map;
Souptick Joarderdf9bde02019-05-13 17:22:23 -0700974 int err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000975
976 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
977 return -EINVAL;
978
979 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
980 index, count, vma->vm_start, vma->vm_pgoff);
981
David Vrabel1401c002015-01-09 18:06:12 +0000982 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000983 map = gntdev_find_map_index(priv, index, count);
984 if (!map)
985 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500986 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000987 goto unlock_out;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200988 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500989
Gerd Hoffmannab315232010-12-14 18:40:46 +0000990 vma->vm_ops = &gntdev_vmops;
991
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -0500992 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500993
994 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +0100995 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000996
997 vma->vm_private_data = map;
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500998 if (map->flags) {
999 if ((vma->vm_flags & VM_WRITE) &&
1000 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001001 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001002 } else {
1003 map->flags = GNTMAP_host_map;
1004 if (!(vma->vm_flags & VM_WRITE))
1005 map->flags |= GNTMAP_readonly;
1006 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001007
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001008 if (use_ptemod) {
1009 map->vma = vma;
1010 err = mmu_interval_notifier_insert_locked(
1011 &map->notifier, vma->vm_mm, vma->vm_start,
1012 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
Juergen Gross970655a2021-04-23 07:40:38 +02001013 if (err) {
1014 map->vma = NULL;
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001015 goto out_unlock_put;
Juergen Gross970655a2021-04-23 07:40:38 +02001016 }
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001017 }
David Vrabel1401c002015-01-09 18:06:12 +00001018 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001019
Daniel De Graafaab8f112011-02-03 12:19:02 -05001020 if (use_ptemod) {
Boris Ostrovsky92937242020-01-28 10:31:26 -05001021 /*
1022 * gntdev takes the address of the PTE in find_grant_ptes() and
1023 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1024 * purpose of the notifier is to prevent the hypervisor pointer
1025 * to the PTE from going stale.
1026 *
1027 * Since this vma's mappings can't be touched without the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001028 * mmap_lock, and we are holding it now, there is no need for
Boris Ostrovsky92937242020-01-28 10:31:26 -05001029 * the notifier_range locking pattern.
1030 */
1031 mmu_interval_read_begin(&map->notifier);
1032
Juergen Gross298d2752017-10-25 17:08:07 +02001033 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001034 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1035 vma->vm_end - vma->vm_start,
1036 find_grant_ptes, map);
1037 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001038 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001039 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001040 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001041 }
1042
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001043 err = gntdev_map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001044 if (err)
1045 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001046
Daniel De Graafaab8f112011-02-03 12:19:02 -05001047 if (!use_ptemod) {
Souptick Joarder8d1502f2019-07-31 00:04:56 +05301048 err = vm_map_pages_zero(vma, map->pages, map->count);
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001049 if (err)
1050 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001051 }
1052
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001053 return 0;
1054
Gerd Hoffmannab315232010-12-14 18:40:46 +00001055unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001056 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001057 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001058
Dan Carpentera93e20a2011-03-19 08:45:43 +03001059out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001060 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001061out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001062 if (use_ptemod) {
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001063 unmap_grant_pages(map, 0, map->count);
Jason Gunthorped3eeb1d2019-11-12 16:22:31 -04001064 if (map->vma) {
1065 mmu_interval_notifier_remove(&map->notifier);
1066 map->vma = NULL;
1067 }
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001068 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001069 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001070 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001071}
1072
1073static const struct file_operations gntdev_fops = {
1074 .owner = THIS_MODULE,
1075 .open = gntdev_open,
1076 .release = gntdev_release,
1077 .mmap = gntdev_mmap,
1078 .unlocked_ioctl = gntdev_ioctl
1079};
1080
1081static struct miscdevice gntdev_miscdev = {
1082 .minor = MISC_DYNAMIC_MINOR,
1083 .name = "xen/gntdev",
1084 .fops = &gntdev_fops,
1085};
1086
1087/* ------------------------------------------------------------------ */
1088
1089static int __init gntdev_init(void)
1090{
1091 int err;
1092
1093 if (!xen_domain())
1094 return -ENODEV;
1095
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001096 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001097
Gerd Hoffmannab315232010-12-14 18:40:46 +00001098 err = misc_register(&gntdev_miscdev);
1099 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001100 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001101 return err;
1102 }
1103 return 0;
1104}
1105
1106static void __exit gntdev_exit(void)
1107{
1108 misc_deregister(&gntdev_miscdev);
1109}
1110
1111module_init(gntdev_init);
1112module_exit(gntdev_exit);
1113
1114/* ------------------------------------------------------------------ */