blob: 4c339c7e66e50d79312e00eee4c82492be5accab [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
Gerd Hoffmannab315232010-12-14 18:40:46 +000025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/miscdevice.h>
29#include <linux/fs.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000030#include <linux/uaccess.h>
31#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010032#include <linux/sched/mm.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000033#include <linux/spinlock.h>
34#include <linux/slab.h>
Daniel De Graafaab8f112011-02-03 12:19:02 -050035#include <linux/highmem.h>
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +020036#include <linux/refcount.h>
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030037#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
38#include <linux/of_device.h>
39#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +000040
41#include <xen/xen.h>
42#include <xen/grant_table.h>
Daniel De Graafca47cea2011-03-09 18:07:34 -050043#include <xen/balloon.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000044#include <xen/gntdev.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050045#include <xen/events.h>
Julien Gralla9fd60e2015-06-17 15:28:02 +010046#include <xen/page.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000047#include <asm/xen/hypervisor.h>
48#include <asm/xen/hypercall.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000049
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030050#include "gntdev-common.h"
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030051#ifdef CONFIG_XEN_GNTDEV_DMABUF
52#include "gntdev-dmabuf.h"
53#endif
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030054
Gerd Hoffmannab315232010-12-14 18:40:46 +000055MODULE_LICENSE("GPL");
56MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
57 "Gerd Hoffmann <kraxel@redhat.com>");
58MODULE_DESCRIPTION("User-space granted page access driver");
59
Daniel De Graafef910822011-02-03 12:18:59 -050060static int limit = 1024*1024;
Gerd Hoffmannab315232010-12-14 18:40:46 +000061module_param(limit, int, 0644);
Daniel De Graafef910822011-02-03 12:18:59 -050062MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
63 "the gntdev device");
64
65static atomic_t pages_mapped = ATOMIC_INIT(0);
Gerd Hoffmannab315232010-12-14 18:40:46 +000066
Daniel De Graafaab8f112011-02-03 12:19:02 -050067static int use_ptemod;
Daniel De Graaf16a1d022013-01-02 22:57:12 +000068#define populate_freeable_maps use_ptemod
Daniel De Graafaab8f112011-02-03 12:19:02 -050069
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030070static int unmap_grant_pages(struct gntdev_grant_map *map,
71 int offset, int pages);
Daniel De Graafaab8f112011-02-03 12:19:02 -050072
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030073static struct miscdevice gntdev_miscdev;
74
Gerd Hoffmannab315232010-12-14 18:40:46 +000075/* ------------------------------------------------------------------ */
76
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030077bool gntdev_account_mapped_pages(int count)
78{
79 return atomic_add_return(count, &pages_mapped) > limit;
80}
81
Gerd Hoffmannab315232010-12-14 18:40:46 +000082static void gntdev_print_maps(struct gntdev_priv *priv,
83 char *text, int text_index)
84{
85#ifdef DEBUG
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030086 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +000087
Daniel De Graafef910822011-02-03 12:18:59 -050088 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +000089 list_for_each_entry(map, &priv->maps, next)
90 pr_debug(" index %2d, count %2d %s\n",
91 map->index, map->count,
92 map->index == text_index && text ? text : "");
93#endif
94}
95
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030096static void gntdev_free_map(struct gntdev_grant_map *map)
David Vrabela67baeb72012-10-24 12:39:02 +010097{
98 if (map == NULL)
99 return;
100
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300101#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
102 if (map->dma_vaddr) {
103 struct gnttab_dma_alloc_args args;
104
105 args.dev = map->dma_dev;
106 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
107 args.nr_pages = map->count;
108 args.pages = map->pages;
109 args.frames = map->frames;
110 args.vaddr = map->dma_vaddr;
111 args.dev_bus_addr = map->dma_bus_addr;
112
113 gnttab_dma_free_pages(&args);
114 } else
115#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100116 if (map->pages)
David Vrabelff4b1562015-01-08 18:06:01 +0000117 gnttab_free_pages(map->count, map->pages);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300118
119#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
120 kfree(map->frames);
121#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100122 kfree(map->pages);
123 kfree(map->grants);
124 kfree(map->map_ops);
125 kfree(map->unmap_ops);
126 kfree(map->kmap_ops);
David Vrabel853d0282015-01-05 14:13:41 +0000127 kfree(map->kunmap_ops);
David Vrabela67baeb72012-10-24 12:39:02 +0100128 kfree(map);
129}
130
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300131struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300132 int dma_flags)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000133{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300134 struct gntdev_grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000135 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000136
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300137 add = kzalloc(sizeof(*add), GFP_KERNEL);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000138 if (NULL == add)
139 return NULL;
140
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300141 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
142 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
143 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
144 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
David Vrabel853d0282015-01-05 14:13:41 +0000145 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300146 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000147 if (NULL == add->grants ||
148 NULL == add->map_ops ||
149 NULL == add->unmap_ops ||
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100150 NULL == add->kmap_ops ||
David Vrabel853d0282015-01-05 14:13:41 +0000151 NULL == add->kunmap_ops ||
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000152 NULL == add->pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000153 goto err;
154
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300155#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
156 add->dma_flags = dma_flags;
157
158 /*
159 * Check if this mapping is requested to be backed
160 * by a DMA buffer.
161 */
162 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
163 struct gnttab_dma_alloc_args args;
164
165 add->frames = kcalloc(count, sizeof(add->frames[0]),
166 GFP_KERNEL);
167 if (!add->frames)
168 goto err;
169
170 /* Remember the device, so we can free DMA memory. */
171 add->dma_dev = priv->dma_dev;
172
173 args.dev = priv->dma_dev;
174 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
175 args.nr_pages = count;
176 args.pages = add->pages;
177 args.frames = add->frames;
178
179 if (gnttab_dma_alloc_pages(&args))
180 goto err;
181
182 add->dma_vaddr = args.vaddr;
183 add->dma_bus_addr = args.dev_bus_addr;
184 } else
185#endif
David Vrabelff4b1562015-01-08 18:06:01 +0000186 if (gnttab_alloc_pages(count, add->pages))
Daniel De Graafca47cea2011-03-09 18:07:34 -0500187 goto err;
188
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000189 for (i = 0; i < count; i++) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500190 add->map_ops[i].handle = -1;
191 add->unmap_ops[i].handle = -1;
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100192 add->kmap_ops[i].handle = -1;
David Vrabel853d0282015-01-05 14:13:41 +0000193 add->kunmap_ops[i].handle = -1;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000194 }
195
Gerd Hoffmannab315232010-12-14 18:40:46 +0000196 add->index = 0;
197 add->count = count;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200198 refcount_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000199
Gerd Hoffmannab315232010-12-14 18:40:46 +0000200 return add;
201
202err:
David Vrabela67baeb72012-10-24 12:39:02 +0100203 gntdev_free_map(add);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000204 return NULL;
205}
206
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300207void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000208{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300209 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000210
211 list_for_each_entry(map, &priv->maps, next) {
212 if (add->index + add->count < map->index) {
213 list_add_tail(&add->next, &map->next);
214 goto done;
215 }
216 add->index = map->index + map->count;
217 }
218 list_add_tail(&add->next, &priv->maps);
219
220done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000221 gntdev_print_maps(priv, "[new]", add->index);
222}
223
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300224static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
225 int index, int count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000226{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300227 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000228
229 list_for_each_entry(map, &priv->maps, next) {
230 if (map->index != index)
231 continue;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500232 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000233 continue;
234 return map;
235 }
236 return NULL;
237}
238
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300239void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000240{
241 if (!map)
242 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000243
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200244 if (!refcount_dec_and_test(&map->users))
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500245 return;
246
247 atomic_sub(map->count, &pages_mapped);
248
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400249 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500250 notify_remote_via_evtchn(map->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400251 evtchn_put(map->notify.event);
252 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500253
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000254 if (populate_freeable_maps && priv) {
David Vrabel1401c002015-01-09 18:06:12 +0000255 mutex_lock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000256 list_del(&map->next);
David Vrabel1401c002015-01-09 18:06:12 +0000257 mutex_unlock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000258 }
259
David Vrabela67baeb72012-10-24 12:39:02 +0100260 if (map->pages && !use_ptemod)
261 unmap_grant_pages(map, 0, map->count);
262 gntdev_free_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000263}
264
265/* ------------------------------------------------------------------ */
266
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700267static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000268{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300269 struct gntdev_grant_map *map = data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000270 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500271 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000272 u64 pte_maddr;
273
274 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800275 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
276
David Vrabel923b2912014-12-18 14:56:54 +0000277 /*
278 * Set the PTE as special to force get_user_pages_fast() fall
279 * back to the slow path. If this is not supported as part of
280 * the grant map, it will be done afterwards.
281 */
282 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
283 flags |= (1 << _GNTMAP_guest_avail0);
284
Daniel De Graafaab8f112011-02-03 12:19:02 -0500285 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000286 map->grants[pgnr].ref,
287 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500288 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500289 -1 /* handle */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000290 return 0;
291}
292
David Vrabel923b2912014-12-18 14:56:54 +0000293#ifdef CONFIG_X86
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700294static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
David Vrabel923b2912014-12-18 14:56:54 +0000295{
296 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
297 return 0;
298}
299#endif
300
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300301int gntdev_map_grant_pages(struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000302{
303 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500304
305 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500306 /* Note: it could already be mapped */
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500307 if (map->map_ops[0].handle != -1)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500308 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500309 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000310 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500311 pfn_to_kaddr(page_to_pfn(map->pages[i]));
312 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
313 map->grants[i].ref,
314 map->grants[i].domid);
315 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500316 map->flags, -1 /* handle */);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500317 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100318 } else {
319 /*
320 * Setup the map_ops corresponding to the pte entries pointing
321 * to the kernel linear addresses of the struct pages.
322 * These ptes are completely different from the user ptes dealt
323 * with find_grant_ptes.
324 */
325 for (i = 0; i < map->count; i++) {
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100326 unsigned long address = (unsigned long)
327 pfn_to_kaddr(page_to_pfn(map->pages[i]));
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100328 BUG_ON(PageHighMem(map->pages[i]));
329
Stefano Stabelliniee072642013-07-23 17:23:54 +0000330 gnttab_set_map_op(&map->kmap_ops[i], address,
331 map->flags | GNTMAP_host_map,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100332 map->grants[i].ref,
333 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000334 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
335 map->flags | GNTMAP_host_map, -1);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100336 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500337 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000338
339 pr_debug("map %d+%d\n", map->index, map->count);
Konrad Rzeszutek Wilke85fc982014-02-03 06:43:59 -0500340 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
341 map->pages, map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000342 if (err)
343 return err;
344
345 for (i = 0; i < map->count; i++) {
David Vrabel853d0282015-01-05 14:13:41 +0000346 if (map->map_ops[i].status) {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000347 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000348 continue;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500349 }
David Vrabel853d0282015-01-05 14:13:41 +0000350
351 map->unmap_ops[i].handle = map->map_ops[i].handle;
352 if (use_ptemod)
353 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300354#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
355 else if (map->dma_vaddr) {
356 unsigned long bfn;
357
358 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
359 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
360 }
361#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000362 }
363 return err;
364}
365
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300366static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
367 int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000368{
369 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000370 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000371
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500372 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
373 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500374 if (pgno >= offset && pgno < offset + pages) {
375 /* No need for kmap, pages are in lowmem */
376 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500377 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500378 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
379 }
380 }
381
Jennifer Herbert74528222015-01-05 15:07:46 +0000382 unmap_data.unmap_ops = map->unmap_ops + offset;
383 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
384 unmap_data.pages = map->pages + offset;
385 unmap_data.count = pages;
386
Bob Liub44166c2015-04-03 14:42:59 +0800387 err = gnttab_unmap_refs_sync(&unmap_data);
388 if (err)
389 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000390
391 for (i = 0; i < pages; i++) {
392 if (map->unmap_ops[offset+i].status)
393 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500394 pr_debug("unmap handle=%d st=%d\n",
395 map->unmap_ops[offset+i].handle,
396 map->unmap_ops[offset+i].status);
397 map->unmap_ops[offset+i].handle = -1;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000398 }
399 return err;
400}
401
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300402static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
403 int pages)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500404{
405 int range, err = 0;
406
407 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
408
409 /* It is possible the requested range will have a "hole" where we
410 * already unmapped some of the grants. Only unmap valid ranges.
411 */
412 while (pages && !err) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500413 while (pages && map->unmap_ops[offset].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500414 offset++;
415 pages--;
416 }
417 range = 0;
418 while (range < pages) {
Ross Lagerwall951a0102018-01-09 12:10:21 +0000419 if (map->unmap_ops[offset+range].handle == -1)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500420 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500421 range++;
422 }
423 err = __unmap_grant_pages(map, offset, range);
424 offset += range;
425 pages -= range;
426 }
427
428 return err;
429}
430
Gerd Hoffmannab315232010-12-14 18:40:46 +0000431/* ------------------------------------------------------------------ */
432
Daniel De Graafd79647a2011-03-07 15:18:57 -0500433static void gntdev_vma_open(struct vm_area_struct *vma)
434{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300435 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500436
437 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200438 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500439}
440
Gerd Hoffmannab315232010-12-14 18:40:46 +0000441static void gntdev_vma_close(struct vm_area_struct *vma)
442{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300443 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000444 struct file *file = vma->vm_file;
445 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000446
Daniel De Graafd79647a2011-03-07 15:18:57 -0500447 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000448 if (use_ptemod) {
Daniel De Graaf2512f292013-01-02 22:57:11 +0000449 /* It is possible that an mmu notifier could be running
450 * concurrently, so take priv->lock to ensure that the vma won't
451 * vanishing during the unmap_grant_pages call, since we will
452 * spin here until that completes. Such a concurrent call will
453 * not do any unmapping, since that has been done prior to
454 * closing the vma, but it may still iterate the unmap_ops list.
455 */
David Vrabel1401c002015-01-09 18:06:12 +0000456 mutex_lock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000457 map->vma = NULL;
David Vrabel1401c002015-01-09 18:06:12 +0000458 mutex_unlock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000459 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000460 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000461 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000462}
463
David Vrabeldab069c2014-12-18 14:59:07 +0000464static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
465 unsigned long addr)
466{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300467 struct gntdev_grant_map *map = vma->vm_private_data;
David Vrabeldab069c2014-12-18 14:59:07 +0000468
469 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
470}
471
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700472static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500473 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000474 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000475 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000476};
477
478/* ------------------------------------------------------------------ */
479
Michal Hocko93065ac2018-08-21 21:52:33 -0700480static bool in_range(struct gntdev_grant_map *map,
481 unsigned long start, unsigned long end)
482{
483 if (!map->vma)
484 return false;
485 if (map->vma->vm_start >= end)
486 return false;
487 if (map->vma->vm_end <= start)
488 return false;
489
490 return true;
491}
492
Michal Hocko58a57562018-09-05 09:21:39 +1000493static int unmap_if_in_range(struct gntdev_grant_map *map,
494 unsigned long start, unsigned long end,
495 bool blockable)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000496{
497 unsigned long mstart, mend;
498 int err;
499
Michal Hocko58a57562018-09-05 09:21:39 +1000500 if (!in_range(map, start, end))
501 return 0;
502
503 if (!blockable)
504 return -EAGAIN;
505
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000506 mstart = max(start, map->vma->vm_start);
507 mend = min(end, map->vma->vm_end);
508 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
509 map->index, map->count,
510 map->vma->vm_start, map->vma->vm_end,
511 start, end, mstart, mend);
512 err = unmap_grant_pages(map,
513 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
514 (mend - mstart) >> PAGE_SHIFT);
515 WARN_ON(err);
Michal Hocko58a57562018-09-05 09:21:39 +1000516
517 return 0;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000518}
519
Michal Hocko93065ac2018-08-21 21:52:33 -0700520static int mn_invl_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800521 const struct mmu_notifier_range *range)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000522{
523 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300524 struct gntdev_grant_map *map;
Michal Hocko93065ac2018-08-21 21:52:33 -0700525 int ret = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000526
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700527 if (mmu_notifier_range_blockable(range))
Michal Hocko93065ac2018-08-21 21:52:33 -0700528 mutex_lock(&priv->lock);
529 else if (!mutex_trylock(&priv->lock))
530 return -EAGAIN;
531
Gerd Hoffmannab315232010-12-14 18:40:46 +0000532 list_for_each_entry(map, &priv->maps, next) {
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800533 ret = unmap_if_in_range(map, range->start, range->end,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700534 mmu_notifier_range_blockable(range));
Michal Hocko58a57562018-09-05 09:21:39 +1000535 if (ret)
Michal Hocko93065ac2018-08-21 21:52:33 -0700536 goto out_unlock;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000537 }
538 list_for_each_entry(map, &priv->freeable_maps, next) {
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800539 ret = unmap_if_in_range(map, range->start, range->end,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700540 mmu_notifier_range_blockable(range));
Michal Hocko58a57562018-09-05 09:21:39 +1000541 if (ret)
Michal Hocko93065ac2018-08-21 21:52:33 -0700542 goto out_unlock;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000543 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700544
545out_unlock:
David Vrabel1401c002015-01-09 18:06:12 +0000546 mutex_unlock(&priv->lock);
Michal Hocko93065ac2018-08-21 21:52:33 -0700547
548 return ret;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000549}
550
Gerd Hoffmannab315232010-12-14 18:40:46 +0000551static void mn_release(struct mmu_notifier *mn,
552 struct mm_struct *mm)
553{
554 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300555 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000556 int err;
557
David Vrabel1401c002015-01-09 18:06:12 +0000558 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000559 list_for_each_entry(map, &priv->maps, next) {
560 if (!map->vma)
561 continue;
562 pr_debug("map %d+%d (%lx %lx)\n",
563 map->index, map->count,
564 map->vma->vm_start, map->vma->vm_end);
565 err = unmap_grant_pages(map, /* offset */ 0, map->count);
566 WARN_ON(err);
567 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000568 list_for_each_entry(map, &priv->freeable_maps, next) {
569 if (!map->vma)
570 continue;
571 pr_debug("map %d+%d (%lx %lx)\n",
572 map->index, map->count,
573 map->vma->vm_start, map->vma->vm_end);
574 err = unmap_grant_pages(map, /* offset */ 0, map->count);
575 WARN_ON(err);
576 }
David Vrabel1401c002015-01-09 18:06:12 +0000577 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000578}
579
Julia Lawallb9c0a922015-11-29 23:02:49 +0100580static const struct mmu_notifier_ops gntdev_mmu_ops = {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000581 .release = mn_release,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000582 .invalidate_range_start = mn_invl_range_start,
583};
584
585/* ------------------------------------------------------------------ */
586
587static int gntdev_open(struct inode *inode, struct file *flip)
588{
589 struct gntdev_priv *priv;
590 int ret = 0;
591
592 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
593 if (!priv)
594 return -ENOMEM;
595
596 INIT_LIST_HEAD(&priv->maps);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000597 INIT_LIST_HEAD(&priv->freeable_maps);
David Vrabel1401c002015-01-09 18:06:12 +0000598 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000599
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300600#ifdef CONFIG_XEN_GNTDEV_DMABUF
Oleksandr Andrushchenkofa13e662019-02-14 16:23:20 +0200601 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300602 if (IS_ERR(priv->dmabuf_priv)) {
603 ret = PTR_ERR(priv->dmabuf_priv);
604 kfree(priv);
605 return ret;
606 }
607#endif
608
Daniel De Graafaab8f112011-02-03 12:19:02 -0500609 if (use_ptemod) {
610 priv->mm = get_task_mm(current);
611 if (!priv->mm) {
612 kfree(priv);
613 return -ENOMEM;
614 }
615 priv->mn.ops = &gntdev_mmu_ops;
616 ret = mmu_notifier_register(&priv->mn, priv->mm);
617 mmput(priv->mm);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000618 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000619
620 if (ret) {
621 kfree(priv);
622 return ret;
623 }
624
625 flip->private_data = priv;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300626#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
627 priv->dma_dev = gntdev_miscdev.this_device;
628
629 /*
630 * The device is not spawn from a device tree, so arch_setup_dma_ops
631 * is not called, thus leaving the device with dummy DMA ops.
632 * Fix this by calling of_dma_configure() with a NULL node to set
633 * default DMA ops.
634 */
635 of_dma_configure(priv->dma_dev, NULL, true);
636#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000637 pr_debug("priv %p\n", priv);
638
639 return 0;
640}
641
642static int gntdev_release(struct inode *inode, struct file *flip)
643{
644 struct gntdev_priv *priv = flip->private_data;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300645 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000646
647 pr_debug("priv %p\n", priv);
648
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200649 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000650 while (!list_empty(&priv->maps)) {
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300651 map = list_entry(priv->maps.next,
652 struct gntdev_grant_map, next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500653 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000654 gntdev_put_map(NULL /* already removed */, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000655 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000656 WARN_ON(!list_empty(&priv->freeable_maps));
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200657 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000658
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300659#ifdef CONFIG_XEN_GNTDEV_DMABUF
660 gntdev_dmabuf_fini(priv->dmabuf_priv);
661#endif
662
Daniel De Graafaab8f112011-02-03 12:19:02 -0500663 if (use_ptemod)
664 mmu_notifier_unregister(&priv->mn, priv->mm);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300665
Gerd Hoffmannab315232010-12-14 18:40:46 +0000666 kfree(priv);
667 return 0;
668}
669
670static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
671 struct ioctl_gntdev_map_grant_ref __user *u)
672{
673 struct ioctl_gntdev_map_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300674 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000675 int err;
676
677 if (copy_from_user(&op, u, sizeof(op)) != 0)
678 return -EFAULT;
679 pr_debug("priv %p, add %d\n", priv, op.count);
680 if (unlikely(op.count <= 0))
681 return -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000682
683 err = -ENOMEM;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300684 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000685 if (!map)
686 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500687
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300688 if (unlikely(gntdev_account_mapped_pages(op.count))) {
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500689 pr_debug("can't map: over limit\n");
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000690 gntdev_put_map(NULL, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000691 return err;
692 }
693
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500694 if (copy_from_user(map->grants, &u->refs,
695 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000696 gntdev_put_map(NULL, map);
697 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500698 }
699
David Vrabel1401c002015-01-09 18:06:12 +0000700 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000701 gntdev_add_map(priv, map);
702 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000703 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000704
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500705 if (copy_to_user(u, &op, sizeof(op)) != 0)
706 return -EFAULT;
707
Gerd Hoffmannab315232010-12-14 18:40:46 +0000708 return 0;
709}
710
711static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
712 struct ioctl_gntdev_unmap_grant_ref __user *u)
713{
714 struct ioctl_gntdev_unmap_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300715 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000716 int err = -ENOENT;
717
718 if (copy_from_user(&op, u, sizeof(op)) != 0)
719 return -EFAULT;
720 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
721
David Vrabel1401c002015-01-09 18:06:12 +0000722 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000723 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500724 if (map) {
725 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000726 if (populate_freeable_maps)
727 list_add_tail(&map->next, &priv->freeable_maps);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500728 err = 0;
729 }
David Vrabel1401c002015-01-09 18:06:12 +0000730 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400731 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000732 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000733 return err;
734}
735
736static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
737 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
738{
739 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500740 struct vm_area_struct *vma;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300741 struct gntdev_grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000742 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000743
744 if (copy_from_user(&op, u, sizeof(op)) != 0)
745 return -EFAULT;
746 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
747
Daniel De Graaf2512f292013-01-02 22:57:11 +0000748 down_read(&current->mm->mmap_sem);
Daniel De Graafa8792112011-02-03 12:19:00 -0500749 vma = find_vma(current->mm, op.vaddr);
750 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000751 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500752
753 map = vma->vm_private_data;
754 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000755 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500756
Gerd Hoffmannab315232010-12-14 18:40:46 +0000757 op.offset = map->index << PAGE_SHIFT;
758 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000759 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000760
Daniel De Graaf2512f292013-01-02 22:57:11 +0000761 out_unlock:
762 up_read(&current->mm->mmap_sem);
763
764 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000765 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000766 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000767}
768
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500769static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
770{
771 struct ioctl_gntdev_unmap_notify op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300772 struct gntdev_grant_map *map;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500773 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400774 int out_flags;
775 unsigned int out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500776
777 if (copy_from_user(&op, u, sizeof(op)))
778 return -EFAULT;
779
780 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
781 return -EINVAL;
782
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400783 /* We need to grab a reference to the event channel we are going to use
784 * to send the notify before releasing the reference we may already have
785 * (if someone has called this ioctl twice). This is required so that
786 * it is possible to change the clear_byte part of the notification
787 * without disturbing the event channel part, which may now be the last
788 * reference to that event channel.
789 */
790 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
791 if (evtchn_get(op.event_channel_port))
792 return -EINVAL;
793 }
794
795 out_flags = op.action;
796 out_event = op.event_channel_port;
797
David Vrabel1401c002015-01-09 18:06:12 +0000798 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500799
800 list_for_each_entry(map, &priv->maps, next) {
801 uint64_t begin = map->index << PAGE_SHIFT;
802 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
803 if (op.index >= begin && op.index < end)
804 goto found;
805 }
806 rc = -ENOENT;
807 goto unlock_out;
808
809 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500810 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
811 (map->flags & GNTMAP_readonly)) {
812 rc = -EINVAL;
813 goto unlock_out;
814 }
815
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400816 out_flags = map->notify.flags;
817 out_event = map->notify.event;
818
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500819 map->notify.flags = op.action;
820 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
821 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400822
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500823 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400824
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500825 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000826 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400827
828 /* Drop the reference to the event channel we did not save in the map */
829 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
830 evtchn_put(out_event);
831
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500832 return rc;
833}
834
David Vrabel36ae2202016-05-09 10:59:48 +0100835#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000836
837struct gntdev_copy_batch {
838 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
839 struct page *pages[GNTDEV_COPY_BATCH];
840 s16 __user *status[GNTDEV_COPY_BATCH];
841 unsigned int nr_ops;
842 unsigned int nr_pages;
843};
844
845static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
846 bool writeable, unsigned long *gfn)
847{
848 unsigned long addr = (unsigned long)virt;
849 struct page *page;
850 unsigned long xen_pfn;
851 int ret;
852
Ira Weiny73b01402019-05-13 17:17:11 -0700853 ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
David Vrabela4cdb552014-12-02 16:13:26 +0000854 if (ret < 0)
855 return ret;
856
857 batch->pages[batch->nr_pages++] = page;
858
859 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
860 *gfn = pfn_to_gfn(xen_pfn);
861
862 return 0;
863}
864
865static void gntdev_put_pages(struct gntdev_copy_batch *batch)
866{
867 unsigned int i;
868
869 for (i = 0; i < batch->nr_pages; i++)
870 put_page(batch->pages[i]);
871 batch->nr_pages = 0;
872}
873
874static int gntdev_copy(struct gntdev_copy_batch *batch)
875{
876 unsigned int i;
877
878 gnttab_batch_copy(batch->ops, batch->nr_ops);
879 gntdev_put_pages(batch);
880
881 /*
882 * For each completed op, update the status if the op failed
883 * and all previous ops for the segment were successful.
884 */
885 for (i = 0; i < batch->nr_ops; i++) {
886 s16 status = batch->ops[i].status;
887 s16 old_status;
888
889 if (status == GNTST_okay)
890 continue;
891
892 if (__get_user(old_status, batch->status[i]))
893 return -EFAULT;
894
895 if (old_status != GNTST_okay)
896 continue;
897
898 if (__put_user(status, batch->status[i]))
899 return -EFAULT;
900 }
901
902 batch->nr_ops = 0;
903 return 0;
904}
905
906static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
907 struct gntdev_grant_copy_segment *seg,
908 s16 __user *status)
909{
910 uint16_t copied = 0;
911
912 /*
913 * Disallow local -> local copies since there is only space in
914 * batch->pages for one page per-op and this would be a very
915 * expensive memcpy().
916 */
917 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
918 return -EINVAL;
919
920 /* Can't cross page if source/dest is a grant ref. */
921 if (seg->flags & GNTCOPY_source_gref) {
922 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
923 return -EINVAL;
924 }
925 if (seg->flags & GNTCOPY_dest_gref) {
926 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
927 return -EINVAL;
928 }
929
930 if (put_user(GNTST_okay, status))
931 return -EFAULT;
932
933 while (copied < seg->len) {
934 struct gnttab_copy *op;
935 void __user *virt;
936 size_t len, off;
937 unsigned long gfn;
938 int ret;
939
940 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
941 ret = gntdev_copy(batch);
942 if (ret < 0)
943 return ret;
944 }
945
946 len = seg->len - copied;
947
948 op = &batch->ops[batch->nr_ops];
949 op->flags = 0;
950
951 if (seg->flags & GNTCOPY_source_gref) {
952 op->source.u.ref = seg->source.foreign.ref;
953 op->source.domid = seg->source.foreign.domid;
954 op->source.offset = seg->source.foreign.offset + copied;
955 op->flags |= GNTCOPY_source_gref;
956 } else {
957 virt = seg->source.virt + copied;
958 off = (unsigned long)virt & ~XEN_PAGE_MASK;
959 len = min(len, (size_t)XEN_PAGE_SIZE - off);
960
961 ret = gntdev_get_page(batch, virt, false, &gfn);
962 if (ret < 0)
963 return ret;
964
965 op->source.u.gmfn = gfn;
966 op->source.domid = DOMID_SELF;
967 op->source.offset = off;
968 }
969
970 if (seg->flags & GNTCOPY_dest_gref) {
971 op->dest.u.ref = seg->dest.foreign.ref;
972 op->dest.domid = seg->dest.foreign.domid;
973 op->dest.offset = seg->dest.foreign.offset + copied;
974 op->flags |= GNTCOPY_dest_gref;
975 } else {
976 virt = seg->dest.virt + copied;
977 off = (unsigned long)virt & ~XEN_PAGE_MASK;
978 len = min(len, (size_t)XEN_PAGE_SIZE - off);
979
980 ret = gntdev_get_page(batch, virt, true, &gfn);
981 if (ret < 0)
982 return ret;
983
984 op->dest.u.gmfn = gfn;
985 op->dest.domid = DOMID_SELF;
986 op->dest.offset = off;
987 }
988
989 op->len = len;
990 copied += len;
991
992 batch->status[batch->nr_ops] = status;
993 batch->nr_ops++;
994 }
995
996 return 0;
997}
998
999static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
1000{
1001 struct ioctl_gntdev_grant_copy copy;
1002 struct gntdev_copy_batch batch;
1003 unsigned int i;
1004 int ret = 0;
1005
1006 if (copy_from_user(&copy, u, sizeof(copy)))
1007 return -EFAULT;
1008
1009 batch.nr_ops = 0;
1010 batch.nr_pages = 0;
1011
1012 for (i = 0; i < copy.count; i++) {
1013 struct gntdev_grant_copy_segment seg;
1014
1015 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
1016 ret = -EFAULT;
1017 goto out;
1018 }
1019
1020 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
1021 if (ret < 0)
1022 goto out;
1023
1024 cond_resched();
1025 }
1026 if (batch.nr_ops)
1027 ret = gntdev_copy(&batch);
1028 return ret;
1029
1030 out:
1031 gntdev_put_pages(&batch);
1032 return ret;
1033}
1034
Gerd Hoffmannab315232010-12-14 18:40:46 +00001035static long gntdev_ioctl(struct file *flip,
1036 unsigned int cmd, unsigned long arg)
1037{
1038 struct gntdev_priv *priv = flip->private_data;
1039 void __user *ptr = (void __user *)arg;
1040
1041 switch (cmd) {
1042 case IOCTL_GNTDEV_MAP_GRANT_REF:
1043 return gntdev_ioctl_map_grant_ref(priv, ptr);
1044
1045 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1046 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1047
1048 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1049 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1050
Daniel De Graafbdc612d2011-02-03 12:19:04 -05001051 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1052 return gntdev_ioctl_notify(priv, ptr);
1053
David Vrabela4cdb552014-12-02 16:13:26 +00001054 case IOCTL_GNTDEV_GRANT_COPY:
1055 return gntdev_ioctl_grant_copy(priv, ptr);
1056
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +03001057#ifdef CONFIG_XEN_GNTDEV_DMABUF
1058 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1059 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1060
1061 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1062 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1063
1064 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1065 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1066
1067 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1068 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1069#endif
1070
Gerd Hoffmannab315232010-12-14 18:40:46 +00001071 default:
1072 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1073 return -ENOIOCTLCMD;
1074 }
1075
1076 return 0;
1077}
1078
1079static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1080{
1081 struct gntdev_priv *priv = flip->private_data;
1082 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +05301083 int count = vma_pages(vma);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001084 struct gntdev_grant_map *map;
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001085 int err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001086
1087 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1088 return -EINVAL;
1089
1090 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1091 index, count, vma->vm_start, vma->vm_pgoff);
1092
David Vrabel1401c002015-01-09 18:06:12 +00001093 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001094 map = gntdev_find_map_index(priv, index, count);
1095 if (!map)
1096 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001097 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +00001098 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001099 if (use_ptemod && priv->mm != vma->vm_mm) {
Joe Perches283c0972013-06-28 03:21:41 -07001100 pr_warn("Huh? Other mm?\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001101 goto unlock_out;
1102 }
1103
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +02001104 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -05001105
Gerd Hoffmannab315232010-12-14 18:40:46 +00001106 vma->vm_ops = &gntdev_vmops;
1107
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -05001108 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -05001109
1110 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +01001111 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001112
1113 vma->vm_private_data = map;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001114
Daniel De Graafaab8f112011-02-03 12:19:02 -05001115 if (use_ptemod)
1116 map->vma = vma;
1117
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001118 if (map->flags) {
1119 if ((vma->vm_flags & VM_WRITE) &&
1120 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001121 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001122 } else {
1123 map->flags = GNTMAP_host_map;
1124 if (!(vma->vm_flags & VM_WRITE))
1125 map->flags |= GNTMAP_readonly;
1126 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001127
David Vrabel1401c002015-01-09 18:06:12 +00001128 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001129
Daniel De Graafaab8f112011-02-03 12:19:02 -05001130 if (use_ptemod) {
Juergen Gross298d2752017-10-25 17:08:07 +02001131 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001132 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1133 vma->vm_end - vma->vm_start,
1134 find_grant_ptes, map);
1135 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001136 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001137 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001138 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001139 }
1140
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001141 err = gntdev_map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001142 if (err)
1143 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001144
Daniel De Graafaab8f112011-02-03 12:19:02 -05001145 if (!use_ptemod) {
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001146 err = vm_map_pages(vma, map->pages, map->count);
1147 if (err)
1148 goto out_put_map;
David Vrabel923b2912014-12-18 14:56:54 +00001149 } else {
1150#ifdef CONFIG_X86
1151 /*
1152 * If the PTEs were not made special by the grant map
1153 * hypercall, do so here.
1154 *
1155 * This is racy since the mapping is already visible
1156 * to userspace but userspace should be well-behaved
1157 * enough to not touch it until the mmap() call
1158 * returns.
1159 */
1160 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1161 apply_to_page_range(vma->vm_mm, vma->vm_start,
1162 vma->vm_end - vma->vm_start,
1163 set_grant_ptes_as_special, NULL);
1164 }
1165#endif
Daniel De Graafaab8f112011-02-03 12:19:02 -05001166 }
1167
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001168 return 0;
1169
Gerd Hoffmannab315232010-12-14 18:40:46 +00001170unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001171 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001172 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001173
Dan Carpentera93e20a2011-03-19 08:45:43 +03001174out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001175 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001176out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001177 if (use_ptemod) {
Daniel De Graaf84e40752011-02-09 15:11:59 -05001178 map->vma = NULL;
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001179 unmap_grant_pages(map, 0, map->count);
1180 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001181 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001182 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001183}
1184
1185static const struct file_operations gntdev_fops = {
1186 .owner = THIS_MODULE,
1187 .open = gntdev_open,
1188 .release = gntdev_release,
1189 .mmap = gntdev_mmap,
1190 .unlocked_ioctl = gntdev_ioctl
1191};
1192
1193static struct miscdevice gntdev_miscdev = {
1194 .minor = MISC_DYNAMIC_MINOR,
1195 .name = "xen/gntdev",
1196 .fops = &gntdev_fops,
1197};
1198
1199/* ------------------------------------------------------------------ */
1200
1201static int __init gntdev_init(void)
1202{
1203 int err;
1204
1205 if (!xen_domain())
1206 return -ENODEV;
1207
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001208 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001209
Gerd Hoffmannab315232010-12-14 18:40:46 +00001210 err = misc_register(&gntdev_miscdev);
1211 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001212 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001213 return err;
1214 }
1215 return 0;
1216}
1217
1218static void __exit gntdev_exit(void)
1219{
1220 misc_deregister(&gntdev_miscdev);
1221}
1222
1223module_init(gntdev_init);
1224module_exit(gntdev_exit);
1225
1226/* ------------------------------------------------------------------ */