blob: 81401f386c9ce06d64a8fdab2c54f26f075ac8a6 [file] [log] [blame]
Gerd Hoffmannab315232010-12-14 18:40:46 +00001/******************************************************************************
2 * gntdev.c
3 *
4 * Device for accessing (in user-space) pages that have been granted by other
5 * domains.
6 *
7 * Copyright (c) 2006-2007, D G Murray.
8 * (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03009 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
Gerd Hoffmannab315232010-12-14 18:40:46 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#undef DEBUG
22
Joe Perches283c0972013-06-28 03:21:41 -070023#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
24
Rob Herringee7f5222019-10-08 14:41:55 -050025#include <linux/dma-mapping.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/miscdevice.h>
30#include <linux/fs.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000031#include <linux/uaccess.h>
32#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010033#include <linux/sched/mm.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000034#include <linux/spinlock.h>
35#include <linux/slab.h>
Daniel De Graafaab8f112011-02-03 12:19:02 -050036#include <linux/highmem.h>
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +020037#include <linux/refcount.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000038
39#include <xen/xen.h>
40#include <xen/grant_table.h>
Daniel De Graafca47cea2011-03-09 18:07:34 -050041#include <xen/balloon.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000042#include <xen/gntdev.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050043#include <xen/events.h>
Julien Gralla9fd60e2015-06-17 15:28:02 +010044#include <xen/page.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000045#include <asm/xen/hypervisor.h>
46#include <asm/xen/hypercall.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000047
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030048#include "gntdev-common.h"
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030049#ifdef CONFIG_XEN_GNTDEV_DMABUF
50#include "gntdev-dmabuf.h"
51#endif
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030052
Gerd Hoffmannab315232010-12-14 18:40:46 +000053MODULE_LICENSE("GPL");
54MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
55 "Gerd Hoffmann <kraxel@redhat.com>");
56MODULE_DESCRIPTION("User-space granted page access driver");
57
Daniel De Graafef910822011-02-03 12:18:59 -050058static int limit = 1024*1024;
Gerd Hoffmannab315232010-12-14 18:40:46 +000059module_param(limit, int, 0644);
Daniel De Graafef910822011-02-03 12:18:59 -050060MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
61 "the gntdev device");
62
63static atomic_t pages_mapped = ATOMIC_INIT(0);
Gerd Hoffmannab315232010-12-14 18:40:46 +000064
Daniel De Graafaab8f112011-02-03 12:19:02 -050065static int use_ptemod;
Daniel De Graaf16a1d022013-01-02 22:57:12 +000066#define populate_freeable_maps use_ptemod
Daniel De Graafaab8f112011-02-03 12:19:02 -050067
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030068static int unmap_grant_pages(struct gntdev_grant_map *map,
69 int offset, int pages);
Daniel De Graafaab8f112011-02-03 12:19:02 -050070
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030071static struct miscdevice gntdev_miscdev;
72
Gerd Hoffmannab315232010-12-14 18:40:46 +000073/* ------------------------------------------------------------------ */
74
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030075bool gntdev_account_mapped_pages(int count)
76{
77 return atomic_add_return(count, &pages_mapped) > limit;
78}
79
Gerd Hoffmannab315232010-12-14 18:40:46 +000080static void gntdev_print_maps(struct gntdev_priv *priv,
81 char *text, int text_index)
82{
83#ifdef DEBUG
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030084 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +000085
Daniel De Graafef910822011-02-03 12:18:59 -050086 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +000087 list_for_each_entry(map, &priv->maps, next)
88 pr_debug(" index %2d, count %2d %s\n",
89 map->index, map->count,
90 map->index == text_index && text ? text : "");
91#endif
92}
93
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +030094static void gntdev_free_map(struct gntdev_grant_map *map)
David Vrabela67baeb72012-10-24 12:39:02 +010095{
96 if (map == NULL)
97 return;
98
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +030099#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
100 if (map->dma_vaddr) {
101 struct gnttab_dma_alloc_args args;
102
103 args.dev = map->dma_dev;
104 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
105 args.nr_pages = map->count;
106 args.pages = map->pages;
107 args.frames = map->frames;
108 args.vaddr = map->dma_vaddr;
109 args.dev_bus_addr = map->dma_bus_addr;
110
111 gnttab_dma_free_pages(&args);
112 } else
113#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100114 if (map->pages)
David Vrabelff4b1562015-01-08 18:06:01 +0000115 gnttab_free_pages(map->count, map->pages);
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300116
117#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
118 kfree(map->frames);
119#endif
David Vrabela67baeb72012-10-24 12:39:02 +0100120 kfree(map->pages);
121 kfree(map->grants);
122 kfree(map->map_ops);
123 kfree(map->unmap_ops);
124 kfree(map->kmap_ops);
David Vrabel853d0282015-01-05 14:13:41 +0000125 kfree(map->kunmap_ops);
David Vrabela67baeb72012-10-24 12:39:02 +0100126 kfree(map);
127}
128
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300129struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300130 int dma_flags)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000131{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300132 struct gntdev_grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000133 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000134
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300135 add = kzalloc(sizeof(*add), GFP_KERNEL);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000136 if (NULL == add)
137 return NULL;
138
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300139 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
140 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
141 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
142 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
David Vrabel853d0282015-01-05 14:13:41 +0000143 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
Dan Carpenterfc6e0c32011-11-04 21:23:32 +0300144 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000145 if (NULL == add->grants ||
146 NULL == add->map_ops ||
147 NULL == add->unmap_ops ||
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100148 NULL == add->kmap_ops ||
David Vrabel853d0282015-01-05 14:13:41 +0000149 NULL == add->kunmap_ops ||
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000150 NULL == add->pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000151 goto err;
152
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300153#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
154 add->dma_flags = dma_flags;
155
156 /*
157 * Check if this mapping is requested to be backed
158 * by a DMA buffer.
159 */
160 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
161 struct gnttab_dma_alloc_args args;
162
163 add->frames = kcalloc(count, sizeof(add->frames[0]),
164 GFP_KERNEL);
165 if (!add->frames)
166 goto err;
167
168 /* Remember the device, so we can free DMA memory. */
169 add->dma_dev = priv->dma_dev;
170
171 args.dev = priv->dma_dev;
172 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
173 args.nr_pages = count;
174 args.pages = add->pages;
175 args.frames = add->frames;
176
177 if (gnttab_dma_alloc_pages(&args))
178 goto err;
179
180 add->dma_vaddr = args.vaddr;
181 add->dma_bus_addr = args.dev_bus_addr;
182 } else
183#endif
David Vrabelff4b1562015-01-08 18:06:01 +0000184 if (gnttab_alloc_pages(count, add->pages))
Daniel De Graafca47cea2011-03-09 18:07:34 -0500185 goto err;
186
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000187 for (i = 0; i < count; i++) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500188 add->map_ops[i].handle = -1;
189 add->unmap_ops[i].handle = -1;
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100190 add->kmap_ops[i].handle = -1;
David Vrabel853d0282015-01-05 14:13:41 +0000191 add->kunmap_ops[i].handle = -1;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000192 }
193
Gerd Hoffmannab315232010-12-14 18:40:46 +0000194 add->index = 0;
195 add->count = count;
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200196 refcount_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000197
Gerd Hoffmannab315232010-12-14 18:40:46 +0000198 return add;
199
200err:
David Vrabela67baeb72012-10-24 12:39:02 +0100201 gntdev_free_map(add);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000202 return NULL;
203}
204
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300205void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000206{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300207 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000208
209 list_for_each_entry(map, &priv->maps, next) {
210 if (add->index + add->count < map->index) {
211 list_add_tail(&add->next, &map->next);
212 goto done;
213 }
214 add->index = map->index + map->count;
215 }
216 list_add_tail(&add->next, &priv->maps);
217
218done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000219 gntdev_print_maps(priv, "[new]", add->index);
220}
221
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300222static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
223 int index, int count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000224{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300225 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000226
227 list_for_each_entry(map, &priv->maps, next) {
228 if (map->index != index)
229 continue;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500230 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000231 continue;
232 return map;
233 }
234 return NULL;
235}
236
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300237void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000238{
239 if (!map)
240 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000241
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200242 if (!refcount_dec_and_test(&map->users))
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500243 return;
244
245 atomic_sub(map->count, &pages_mapped);
246
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400247 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500248 notify_remote_via_evtchn(map->notify.event);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400249 evtchn_put(map->notify.event);
250 }
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500251
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000252 if (populate_freeable_maps && priv) {
David Vrabel1401c002015-01-09 18:06:12 +0000253 mutex_lock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000254 list_del(&map->next);
David Vrabel1401c002015-01-09 18:06:12 +0000255 mutex_unlock(&priv->lock);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000256 }
257
David Vrabela67baeb72012-10-24 12:39:02 +0100258 if (map->pages && !use_ptemod)
259 unmap_grant_pages(map, 0, map->count);
260 gntdev_free_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000261}
262
263/* ------------------------------------------------------------------ */
264
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700265static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000266{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300267 struct gntdev_grant_map *map = data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000268 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500269 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000270 u64 pte_maddr;
271
272 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800273 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
274
David Vrabel923b2912014-12-18 14:56:54 +0000275 /*
276 * Set the PTE as special to force get_user_pages_fast() fall
277 * back to the slow path. If this is not supported as part of
278 * the grant map, it will be done afterwards.
279 */
280 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
281 flags |= (1 << _GNTMAP_guest_avail0);
282
Daniel De Graafaab8f112011-02-03 12:19:02 -0500283 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000284 map->grants[pgnr].ref,
285 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500286 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500287 -1 /* handle */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000288 return 0;
289}
290
David Vrabel923b2912014-12-18 14:56:54 +0000291#ifdef CONFIG_X86
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -0700292static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
David Vrabel923b2912014-12-18 14:56:54 +0000293{
294 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
295 return 0;
296}
297#endif
298
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300299int gntdev_map_grant_pages(struct gntdev_grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000300{
301 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500302
303 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500304 /* Note: it could already be mapped */
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500305 if (map->map_ops[0].handle != -1)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500306 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500307 for (i = 0; i < map->count; i++) {
Ian Campbell38eaeb02011-03-08 16:56:43 +0000308 unsigned long addr = (unsigned long)
Daniel De Graafaab8f112011-02-03 12:19:02 -0500309 pfn_to_kaddr(page_to_pfn(map->pages[i]));
310 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
311 map->grants[i].ref,
312 map->grants[i].domid);
313 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500314 map->flags, -1 /* handle */);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500315 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100316 } else {
317 /*
318 * Setup the map_ops corresponding to the pte entries pointing
319 * to the kernel linear addresses of the struct pages.
320 * These ptes are completely different from the user ptes dealt
321 * with find_grant_ptes.
322 */
323 for (i = 0; i < map->count; i++) {
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100324 unsigned long address = (unsigned long)
325 pfn_to_kaddr(page_to_pfn(map->pages[i]));
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100326 BUG_ON(PageHighMem(map->pages[i]));
327
Stefano Stabelliniee072642013-07-23 17:23:54 +0000328 gnttab_set_map_op(&map->kmap_ops[i], address,
329 map->flags | GNTMAP_host_map,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100330 map->grants[i].ref,
331 map->grants[i].domid);
David Vrabel853d0282015-01-05 14:13:41 +0000332 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
333 map->flags | GNTMAP_host_map, -1);
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100334 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500335 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000336
337 pr_debug("map %d+%d\n", map->index, map->count);
Konrad Rzeszutek Wilke85fc982014-02-03 06:43:59 -0500338 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
339 map->pages, map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000340 if (err)
341 return err;
342
343 for (i = 0; i < map->count; i++) {
David Vrabel853d0282015-01-05 14:13:41 +0000344 if (map->map_ops[i].status) {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000345 err = -EINVAL;
David Vrabel853d0282015-01-05 14:13:41 +0000346 continue;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500347 }
David Vrabel853d0282015-01-05 14:13:41 +0000348
349 map->unmap_ops[i].handle = map->map_ops[i].handle;
350 if (use_ptemod)
351 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300352#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
353 else if (map->dma_vaddr) {
354 unsigned long bfn;
355
356 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
357 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
358 }
359#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000360 }
361 return err;
362}
363
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300364static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
365 int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000366{
367 int i, err = 0;
Jennifer Herbert74528222015-01-05 15:07:46 +0000368 struct gntab_unmap_queue_data unmap_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000369
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500370 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
371 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf1affa982013-01-02 17:57:13 -0500372 if (pgno >= offset && pgno < offset + pages) {
373 /* No need for kmap, pages are in lowmem */
374 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500375 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500376 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
377 }
378 }
379
Jennifer Herbert74528222015-01-05 15:07:46 +0000380 unmap_data.unmap_ops = map->unmap_ops + offset;
381 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
382 unmap_data.pages = map->pages + offset;
383 unmap_data.count = pages;
384
Bob Liub44166c2015-04-03 14:42:59 +0800385 err = gnttab_unmap_refs_sync(&unmap_data);
386 if (err)
387 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000388
389 for (i = 0; i < pages; i++) {
390 if (map->unmap_ops[offset+i].status)
391 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500392 pr_debug("unmap handle=%d st=%d\n",
393 map->unmap_ops[offset+i].handle,
394 map->unmap_ops[offset+i].status);
395 map->unmap_ops[offset+i].handle = -1;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000396 }
397 return err;
398}
399
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300400static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
401 int pages)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500402{
403 int range, err = 0;
404
405 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
406
407 /* It is possible the requested range will have a "hole" where we
408 * already unmapped some of the grants. Only unmap valid ranges.
409 */
410 while (pages && !err) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500411 while (pages && map->unmap_ops[offset].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500412 offset++;
413 pages--;
414 }
415 range = 0;
416 while (range < pages) {
Ross Lagerwall951a0102018-01-09 12:10:21 +0000417 if (map->unmap_ops[offset+range].handle == -1)
Daniel De Graafb57c1862011-02-09 15:12:00 -0500418 break;
Daniel De Graafb57c1862011-02-09 15:12:00 -0500419 range++;
420 }
421 err = __unmap_grant_pages(map, offset, range);
422 offset += range;
423 pages -= range;
424 }
425
426 return err;
427}
428
Gerd Hoffmannab315232010-12-14 18:40:46 +0000429/* ------------------------------------------------------------------ */
430
Daniel De Graafd79647a2011-03-07 15:18:57 -0500431static void gntdev_vma_open(struct vm_area_struct *vma)
432{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300433 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graafd79647a2011-03-07 15:18:57 -0500434
435 pr_debug("gntdev_vma_open %p\n", vma);
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +0200436 refcount_inc(&map->users);
Daniel De Graafd79647a2011-03-07 15:18:57 -0500437}
438
Gerd Hoffmannab315232010-12-14 18:40:46 +0000439static void gntdev_vma_close(struct vm_area_struct *vma)
440{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300441 struct gntdev_grant_map *map = vma->vm_private_data;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000442 struct file *file = vma->vm_file;
443 struct gntdev_priv *priv = file->private_data;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000444
Daniel De Graafd79647a2011-03-07 15:18:57 -0500445 pr_debug("gntdev_vma_close %p\n", vma);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000446 if (use_ptemod) {
Daniel De Graaf2512f292013-01-02 22:57:11 +0000447 /* It is possible that an mmu notifier could be running
448 * concurrently, so take priv->lock to ensure that the vma won't
449 * vanishing during the unmap_grant_pages call, since we will
450 * spin here until that completes. Such a concurrent call will
451 * not do any unmapping, since that has been done prior to
452 * closing the vma, but it may still iterate the unmap_ops list.
453 */
David Vrabel1401c002015-01-09 18:06:12 +0000454 mutex_lock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000455 map->vma = NULL;
David Vrabel1401c002015-01-09 18:06:12 +0000456 mutex_unlock(&priv->lock);
Daniel De Graaf2512f292013-01-02 22:57:11 +0000457 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000458 vma->vm_private_data = NULL;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000459 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000460}
461
David Vrabeldab069c2014-12-18 14:59:07 +0000462static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
463 unsigned long addr)
464{
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300465 struct gntdev_grant_map *map = vma->vm_private_data;
David Vrabeldab069c2014-12-18 14:59:07 +0000466
467 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
468}
469
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700470static const struct vm_operations_struct gntdev_vmops = {
Daniel De Graafd79647a2011-03-07 15:18:57 -0500471 .open = gntdev_vma_open,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000472 .close = gntdev_vma_close,
David Vrabeldab069c2014-12-18 14:59:07 +0000473 .find_special_page = gntdev_vma_find_special_page,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000474};
475
476/* ------------------------------------------------------------------ */
477
Michal Hocko93065ac2018-08-21 21:52:33 -0700478static bool in_range(struct gntdev_grant_map *map,
479 unsigned long start, unsigned long end)
480{
481 if (!map->vma)
482 return false;
483 if (map->vma->vm_start >= end)
484 return false;
485 if (map->vma->vm_end <= start)
486 return false;
487
488 return true;
489}
490
Michal Hocko58a57562018-09-05 09:21:39 +1000491static int unmap_if_in_range(struct gntdev_grant_map *map,
492 unsigned long start, unsigned long end,
493 bool blockable)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000494{
495 unsigned long mstart, mend;
496 int err;
497
Michal Hocko58a57562018-09-05 09:21:39 +1000498 if (!in_range(map, start, end))
499 return 0;
500
501 if (!blockable)
502 return -EAGAIN;
503
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000504 mstart = max(start, map->vma->vm_start);
505 mend = min(end, map->vma->vm_end);
506 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
507 map->index, map->count,
508 map->vma->vm_start, map->vma->vm_end,
509 start, end, mstart, mend);
510 err = unmap_grant_pages(map,
511 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
512 (mend - mstart) >> PAGE_SHIFT);
513 WARN_ON(err);
Michal Hocko58a57562018-09-05 09:21:39 +1000514
515 return 0;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000516}
517
Michal Hocko93065ac2018-08-21 21:52:33 -0700518static int mn_invl_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800519 const struct mmu_notifier_range *range)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000520{
521 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300522 struct gntdev_grant_map *map;
Michal Hocko93065ac2018-08-21 21:52:33 -0700523 int ret = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000524
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700525 if (mmu_notifier_range_blockable(range))
Michal Hocko93065ac2018-08-21 21:52:33 -0700526 mutex_lock(&priv->lock);
527 else if (!mutex_trylock(&priv->lock))
528 return -EAGAIN;
529
Gerd Hoffmannab315232010-12-14 18:40:46 +0000530 list_for_each_entry(map, &priv->maps, next) {
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800531 ret = unmap_if_in_range(map, range->start, range->end,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700532 mmu_notifier_range_blockable(range));
Michal Hocko58a57562018-09-05 09:21:39 +1000533 if (ret)
Michal Hocko93065ac2018-08-21 21:52:33 -0700534 goto out_unlock;
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000535 }
536 list_for_each_entry(map, &priv->freeable_maps, next) {
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800537 ret = unmap_if_in_range(map, range->start, range->end,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700538 mmu_notifier_range_blockable(range));
Michal Hocko58a57562018-09-05 09:21:39 +1000539 if (ret)
Michal Hocko93065ac2018-08-21 21:52:33 -0700540 goto out_unlock;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000541 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700542
543out_unlock:
David Vrabel1401c002015-01-09 18:06:12 +0000544 mutex_unlock(&priv->lock);
Michal Hocko93065ac2018-08-21 21:52:33 -0700545
546 return ret;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000547}
548
Gerd Hoffmannab315232010-12-14 18:40:46 +0000549static void mn_release(struct mmu_notifier *mn,
550 struct mm_struct *mm)
551{
552 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300553 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000554 int err;
555
David Vrabel1401c002015-01-09 18:06:12 +0000556 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000557 list_for_each_entry(map, &priv->maps, next) {
558 if (!map->vma)
559 continue;
560 pr_debug("map %d+%d (%lx %lx)\n",
561 map->index, map->count,
562 map->vma->vm_start, map->vma->vm_end);
563 err = unmap_grant_pages(map, /* offset */ 0, map->count);
564 WARN_ON(err);
565 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000566 list_for_each_entry(map, &priv->freeable_maps, next) {
567 if (!map->vma)
568 continue;
569 pr_debug("map %d+%d (%lx %lx)\n",
570 map->index, map->count,
571 map->vma->vm_start, map->vma->vm_end);
572 err = unmap_grant_pages(map, /* offset */ 0, map->count);
573 WARN_ON(err);
574 }
David Vrabel1401c002015-01-09 18:06:12 +0000575 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000576}
577
Julia Lawallb9c0a922015-11-29 23:02:49 +0100578static const struct mmu_notifier_ops gntdev_mmu_ops = {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000579 .release = mn_release,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000580 .invalidate_range_start = mn_invl_range_start,
581};
582
583/* ------------------------------------------------------------------ */
584
585static int gntdev_open(struct inode *inode, struct file *flip)
586{
587 struct gntdev_priv *priv;
588 int ret = 0;
589
590 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
591 if (!priv)
592 return -ENOMEM;
593
594 INIT_LIST_HEAD(&priv->maps);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000595 INIT_LIST_HEAD(&priv->freeable_maps);
David Vrabel1401c002015-01-09 18:06:12 +0000596 mutex_init(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000597
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300598#ifdef CONFIG_XEN_GNTDEV_DMABUF
Oleksandr Andrushchenkofa13e662019-02-14 16:23:20 +0200599 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300600 if (IS_ERR(priv->dmabuf_priv)) {
601 ret = PTR_ERR(priv->dmabuf_priv);
602 kfree(priv);
603 return ret;
604 }
605#endif
606
Daniel De Graafaab8f112011-02-03 12:19:02 -0500607 if (use_ptemod) {
608 priv->mm = get_task_mm(current);
609 if (!priv->mm) {
610 kfree(priv);
611 return -ENOMEM;
612 }
613 priv->mn.ops = &gntdev_mmu_ops;
614 ret = mmu_notifier_register(&priv->mn, priv->mm);
615 mmput(priv->mm);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000616 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000617
618 if (ret) {
619 kfree(priv);
620 return ret;
621 }
622
623 flip->private_data = priv;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300624#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
625 priv->dma_dev = gntdev_miscdev.this_device;
Rob Herringee7f5222019-10-08 14:41:55 -0500626 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300627#endif
Gerd Hoffmannab315232010-12-14 18:40:46 +0000628 pr_debug("priv %p\n", priv);
629
630 return 0;
631}
632
633static int gntdev_release(struct inode *inode, struct file *flip)
634{
635 struct gntdev_priv *priv = flip->private_data;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300636 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000637
638 pr_debug("priv %p\n", priv);
639
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200640 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000641 while (!list_empty(&priv->maps)) {
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300642 map = list_entry(priv->maps.next,
643 struct gntdev_grant_map, next);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500644 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000645 gntdev_put_map(NULL /* already removed */, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000646 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000647 WARN_ON(!list_empty(&priv->freeable_maps));
Marek Marczykowski-Górecki30b03d02015-06-26 03:28:24 +0200648 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000649
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300650#ifdef CONFIG_XEN_GNTDEV_DMABUF
651 gntdev_dmabuf_fini(priv->dmabuf_priv);
652#endif
653
Daniel De Graafaab8f112011-02-03 12:19:02 -0500654 if (use_ptemod)
655 mmu_notifier_unregister(&priv->mn, priv->mm);
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300656
Gerd Hoffmannab315232010-12-14 18:40:46 +0000657 kfree(priv);
658 return 0;
659}
660
661static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
662 struct ioctl_gntdev_map_grant_ref __user *u)
663{
664 struct ioctl_gntdev_map_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300665 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000666 int err;
667
668 if (copy_from_user(&op, u, sizeof(op)) != 0)
669 return -EFAULT;
670 pr_debug("priv %p, add %d\n", priv, op.count);
671 if (unlikely(op.count <= 0))
672 return -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000673
674 err = -ENOMEM;
Oleksandr Andrushchenko975ef7f2018-07-20 12:01:46 +0300675 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000676 if (!map)
677 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500678
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300679 if (unlikely(gntdev_account_mapped_pages(op.count))) {
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500680 pr_debug("can't map: over limit\n");
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000681 gntdev_put_map(NULL, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000682 return err;
683 }
684
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500685 if (copy_from_user(map->grants, &u->refs,
686 sizeof(map->grants[0]) * op.count) != 0) {
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000687 gntdev_put_map(NULL, map);
688 return -EFAULT;
Daniel De Graafef910822011-02-03 12:18:59 -0500689 }
690
David Vrabel1401c002015-01-09 18:06:12 +0000691 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000692 gntdev_add_map(priv, map);
693 op.index = map->index << PAGE_SHIFT;
David Vrabel1401c002015-01-09 18:06:12 +0000694 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000695
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500696 if (copy_to_user(u, &op, sizeof(op)) != 0)
697 return -EFAULT;
698
Gerd Hoffmannab315232010-12-14 18:40:46 +0000699 return 0;
700}
701
702static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
703 struct ioctl_gntdev_unmap_grant_ref __user *u)
704{
705 struct ioctl_gntdev_unmap_grant_ref op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300706 struct gntdev_grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000707 int err = -ENOENT;
708
709 if (copy_from_user(&op, u, sizeof(op)) != 0)
710 return -EFAULT;
711 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
712
David Vrabel1401c002015-01-09 18:06:12 +0000713 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000714 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500715 if (map) {
716 list_del(&map->next);
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000717 if (populate_freeable_maps)
718 list_add_tail(&map->next, &priv->freeable_maps);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500719 err = 0;
720 }
David Vrabel1401c002015-01-09 18:06:12 +0000721 mutex_unlock(&priv->lock);
Daniel De Graaf1f1503b2011-10-11 15:16:06 -0400722 if (map)
Daniel De Graaf16a1d022013-01-02 22:57:12 +0000723 gntdev_put_map(priv, map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000724 return err;
725}
726
727static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
728 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
729{
730 struct ioctl_gntdev_get_offset_for_vaddr op;
Daniel De Graafa8792112011-02-03 12:19:00 -0500731 struct vm_area_struct *vma;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300732 struct gntdev_grant_map *map;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000733 int rv = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000734
735 if (copy_from_user(&op, u, sizeof(op)) != 0)
736 return -EFAULT;
737 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
738
Daniel De Graaf2512f292013-01-02 22:57:11 +0000739 down_read(&current->mm->mmap_sem);
Daniel De Graafa8792112011-02-03 12:19:00 -0500740 vma = find_vma(current->mm, op.vaddr);
741 if (!vma || vma->vm_ops != &gntdev_vmops)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000742 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500743
744 map = vma->vm_private_data;
745 if (!map)
Daniel De Graaf2512f292013-01-02 22:57:11 +0000746 goto out_unlock;
Daniel De Graafa8792112011-02-03 12:19:00 -0500747
Gerd Hoffmannab315232010-12-14 18:40:46 +0000748 op.offset = map->index << PAGE_SHIFT;
749 op.count = map->count;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000750 rv = 0;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000751
Daniel De Graaf2512f292013-01-02 22:57:11 +0000752 out_unlock:
753 up_read(&current->mm->mmap_sem);
754
755 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000756 return -EFAULT;
Daniel De Graaf2512f292013-01-02 22:57:11 +0000757 return rv;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000758}
759
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500760static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
761{
762 struct ioctl_gntdev_unmap_notify op;
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +0300763 struct gntdev_grant_map *map;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500764 int rc;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400765 int out_flags;
766 unsigned int out_event;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500767
768 if (copy_from_user(&op, u, sizeof(op)))
769 return -EFAULT;
770
771 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
772 return -EINVAL;
773
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400774 /* We need to grab a reference to the event channel we are going to use
775 * to send the notify before releasing the reference we may already have
776 * (if someone has called this ioctl twice). This is required so that
777 * it is possible to change the clear_byte part of the notification
778 * without disturbing the event channel part, which may now be the last
779 * reference to that event channel.
780 */
781 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
782 if (evtchn_get(op.event_channel_port))
783 return -EINVAL;
784 }
785
786 out_flags = op.action;
787 out_event = op.event_channel_port;
788
David Vrabel1401c002015-01-09 18:06:12 +0000789 mutex_lock(&priv->lock);
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500790
791 list_for_each_entry(map, &priv->maps, next) {
792 uint64_t begin = map->index << PAGE_SHIFT;
793 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
794 if (op.index >= begin && op.index < end)
795 goto found;
796 }
797 rc = -ENOENT;
798 goto unlock_out;
799
800 found:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500801 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
802 (map->flags & GNTMAP_readonly)) {
803 rc = -EINVAL;
804 goto unlock_out;
805 }
806
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400807 out_flags = map->notify.flags;
808 out_event = map->notify.event;
809
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500810 map->notify.flags = op.action;
811 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
812 map->notify.event = op.event_channel_port;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400813
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500814 rc = 0;
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400815
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500816 unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +0000817 mutex_unlock(&priv->lock);
Daniel De Graaf0cc678f2011-10-27 17:58:49 -0400818
819 /* Drop the reference to the event channel we did not save in the map */
820 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
821 evtchn_put(out_event);
822
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500823 return rc;
824}
825
David Vrabel36ae2202016-05-09 10:59:48 +0100826#define GNTDEV_COPY_BATCH 16
David Vrabela4cdb552014-12-02 16:13:26 +0000827
828struct gntdev_copy_batch {
829 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
830 struct page *pages[GNTDEV_COPY_BATCH];
831 s16 __user *status[GNTDEV_COPY_BATCH];
832 unsigned int nr_ops;
833 unsigned int nr_pages;
834};
835
836static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
837 bool writeable, unsigned long *gfn)
838{
839 unsigned long addr = (unsigned long)virt;
840 struct page *page;
841 unsigned long xen_pfn;
842 int ret;
843
Ira Weiny73b01402019-05-13 17:17:11 -0700844 ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
David Vrabela4cdb552014-12-02 16:13:26 +0000845 if (ret < 0)
846 return ret;
847
848 batch->pages[batch->nr_pages++] = page;
849
850 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
851 *gfn = pfn_to_gfn(xen_pfn);
852
853 return 0;
854}
855
856static void gntdev_put_pages(struct gntdev_copy_batch *batch)
857{
858 unsigned int i;
859
860 for (i = 0; i < batch->nr_pages; i++)
861 put_page(batch->pages[i]);
862 batch->nr_pages = 0;
863}
864
865static int gntdev_copy(struct gntdev_copy_batch *batch)
866{
867 unsigned int i;
868
869 gnttab_batch_copy(batch->ops, batch->nr_ops);
870 gntdev_put_pages(batch);
871
872 /*
873 * For each completed op, update the status if the op failed
874 * and all previous ops for the segment were successful.
875 */
876 for (i = 0; i < batch->nr_ops; i++) {
877 s16 status = batch->ops[i].status;
878 s16 old_status;
879
880 if (status == GNTST_okay)
881 continue;
882
883 if (__get_user(old_status, batch->status[i]))
884 return -EFAULT;
885
886 if (old_status != GNTST_okay)
887 continue;
888
889 if (__put_user(status, batch->status[i]))
890 return -EFAULT;
891 }
892
893 batch->nr_ops = 0;
894 return 0;
895}
896
897static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
898 struct gntdev_grant_copy_segment *seg,
899 s16 __user *status)
900{
901 uint16_t copied = 0;
902
903 /*
904 * Disallow local -> local copies since there is only space in
905 * batch->pages for one page per-op and this would be a very
906 * expensive memcpy().
907 */
908 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
909 return -EINVAL;
910
911 /* Can't cross page if source/dest is a grant ref. */
912 if (seg->flags & GNTCOPY_source_gref) {
913 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
914 return -EINVAL;
915 }
916 if (seg->flags & GNTCOPY_dest_gref) {
917 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
918 return -EINVAL;
919 }
920
921 if (put_user(GNTST_okay, status))
922 return -EFAULT;
923
924 while (copied < seg->len) {
925 struct gnttab_copy *op;
926 void __user *virt;
927 size_t len, off;
928 unsigned long gfn;
929 int ret;
930
931 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
932 ret = gntdev_copy(batch);
933 if (ret < 0)
934 return ret;
935 }
936
937 len = seg->len - copied;
938
939 op = &batch->ops[batch->nr_ops];
940 op->flags = 0;
941
942 if (seg->flags & GNTCOPY_source_gref) {
943 op->source.u.ref = seg->source.foreign.ref;
944 op->source.domid = seg->source.foreign.domid;
945 op->source.offset = seg->source.foreign.offset + copied;
946 op->flags |= GNTCOPY_source_gref;
947 } else {
948 virt = seg->source.virt + copied;
949 off = (unsigned long)virt & ~XEN_PAGE_MASK;
950 len = min(len, (size_t)XEN_PAGE_SIZE - off);
951
952 ret = gntdev_get_page(batch, virt, false, &gfn);
953 if (ret < 0)
954 return ret;
955
956 op->source.u.gmfn = gfn;
957 op->source.domid = DOMID_SELF;
958 op->source.offset = off;
959 }
960
961 if (seg->flags & GNTCOPY_dest_gref) {
962 op->dest.u.ref = seg->dest.foreign.ref;
963 op->dest.domid = seg->dest.foreign.domid;
964 op->dest.offset = seg->dest.foreign.offset + copied;
965 op->flags |= GNTCOPY_dest_gref;
966 } else {
967 virt = seg->dest.virt + copied;
968 off = (unsigned long)virt & ~XEN_PAGE_MASK;
969 len = min(len, (size_t)XEN_PAGE_SIZE - off);
970
971 ret = gntdev_get_page(batch, virt, true, &gfn);
972 if (ret < 0)
973 return ret;
974
975 op->dest.u.gmfn = gfn;
976 op->dest.domid = DOMID_SELF;
977 op->dest.offset = off;
978 }
979
980 op->len = len;
981 copied += len;
982
983 batch->status[batch->nr_ops] = status;
984 batch->nr_ops++;
985 }
986
987 return 0;
988}
989
990static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
991{
992 struct ioctl_gntdev_grant_copy copy;
993 struct gntdev_copy_batch batch;
994 unsigned int i;
995 int ret = 0;
996
997 if (copy_from_user(&copy, u, sizeof(copy)))
998 return -EFAULT;
999
1000 batch.nr_ops = 0;
1001 batch.nr_pages = 0;
1002
1003 for (i = 0; i < copy.count; i++) {
1004 struct gntdev_grant_copy_segment seg;
1005
1006 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
1007 ret = -EFAULT;
1008 goto out;
1009 }
1010
1011 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
1012 if (ret < 0)
1013 goto out;
1014
1015 cond_resched();
1016 }
1017 if (batch.nr_ops)
1018 ret = gntdev_copy(&batch);
1019 return ret;
1020
1021 out:
1022 gntdev_put_pages(&batch);
1023 return ret;
1024}
1025
Gerd Hoffmannab315232010-12-14 18:40:46 +00001026static long gntdev_ioctl(struct file *flip,
1027 unsigned int cmd, unsigned long arg)
1028{
1029 struct gntdev_priv *priv = flip->private_data;
1030 void __user *ptr = (void __user *)arg;
1031
1032 switch (cmd) {
1033 case IOCTL_GNTDEV_MAP_GRANT_REF:
1034 return gntdev_ioctl_map_grant_ref(priv, ptr);
1035
1036 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1037 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1038
1039 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1040 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1041
Daniel De Graafbdc612d2011-02-03 12:19:04 -05001042 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1043 return gntdev_ioctl_notify(priv, ptr);
1044
David Vrabela4cdb552014-12-02 16:13:26 +00001045 case IOCTL_GNTDEV_GRANT_COPY:
1046 return gntdev_ioctl_grant_copy(priv, ptr);
1047
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +03001048#ifdef CONFIG_XEN_GNTDEV_DMABUF
1049 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1050 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1051
1052 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1053 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1054
1055 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1056 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1057
1058 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1059 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1060#endif
1061
Gerd Hoffmannab315232010-12-14 18:40:46 +00001062 default:
1063 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1064 return -ENOIOCTLCMD;
1065 }
1066
1067 return 0;
1068}
1069
1070static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1071{
1072 struct gntdev_priv *priv = flip->private_data;
1073 int index = vma->vm_pgoff;
Muhammad Falak R Wanic7ebf9d2016-05-24 05:34:32 +05301074 int count = vma_pages(vma);
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001075 struct gntdev_grant_map *map;
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001076 int err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001077
1078 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1079 return -EINVAL;
1080
1081 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1082 index, count, vma->vm_start, vma->vm_pgoff);
1083
David Vrabel1401c002015-01-09 18:06:12 +00001084 mutex_lock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001085 map = gntdev_find_map_index(priv, index, count);
1086 if (!map)
1087 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001088 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +00001089 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001090 if (use_ptemod && priv->mm != vma->vm_mm) {
Joe Perches283c0972013-06-28 03:21:41 -07001091 pr_warn("Huh? Other mm?\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001092 goto unlock_out;
1093 }
1094
Elena Reshetovac5f7c5a2017-03-06 16:21:16 +02001095 refcount_inc(&map->users);
Daniel De Graaf68b025c2011-02-03 12:19:01 -05001096
Gerd Hoffmannab315232010-12-14 18:40:46 +00001097 vma->vm_ops = &gntdev_vmops;
1098
Boris Ostrovsky30faaaf2016-11-21 09:56:06 -05001099 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
Daniel De Graafd79647a2011-03-07 15:18:57 -05001100
1101 if (use_ptemod)
Stefano Stabellinie8e937b2012-04-03 18:05:47 +01001102 vma->vm_flags |= VM_DONTCOPY;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001103
1104 vma->vm_private_data = map;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001105
Daniel De Graafaab8f112011-02-03 12:19:02 -05001106 if (use_ptemod)
1107 map->vma = vma;
1108
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001109 if (map->flags) {
1110 if ((vma->vm_flags & VM_WRITE) &&
1111 (map->flags & GNTMAP_readonly))
Dan Carpentera93e20a2011-03-19 08:45:43 +03001112 goto out_unlock_put;
Daniel De Graaf12996fc2011-02-09 16:11:32 -05001113 } else {
1114 map->flags = GNTMAP_host_map;
1115 if (!(vma->vm_flags & VM_WRITE))
1116 map->flags |= GNTMAP_readonly;
1117 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001118
David Vrabel1401c002015-01-09 18:06:12 +00001119 mutex_unlock(&priv->lock);
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001120
Daniel De Graafaab8f112011-02-03 12:19:02 -05001121 if (use_ptemod) {
Juergen Gross298d2752017-10-25 17:08:07 +02001122 map->pages_vm_start = vma->vm_start;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001123 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1124 vma->vm_end - vma->vm_start,
1125 find_grant_ptes, map);
1126 if (err) {
Joe Perches283c0972013-06-28 03:21:41 -07001127 pr_warn("find_grant_ptes() failure.\n");
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001128 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -05001129 }
Gerd Hoffmannab315232010-12-14 18:40:46 +00001130 }
1131
Oleksandr Andrushchenko1d314562018-07-20 12:01:47 +03001132 err = gntdev_map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001133 if (err)
1134 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001135
Daniel De Graafaab8f112011-02-03 12:19:02 -05001136 if (!use_ptemod) {
Souptick Joarder8d1502f2019-07-31 00:04:56 +05301137 err = vm_map_pages_zero(vma, map->pages, map->count);
Souptick Joarderdf9bde02019-05-13 17:22:23 -07001138 if (err)
1139 goto out_put_map;
David Vrabel923b2912014-12-18 14:56:54 +00001140 } else {
1141#ifdef CONFIG_X86
1142 /*
1143 * If the PTEs were not made special by the grant map
1144 * hypercall, do so here.
1145 *
1146 * This is racy since the mapping is already visible
1147 * to userspace but userspace should be well-behaved
1148 * enough to not touch it until the mmap() call
1149 * returns.
1150 */
1151 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1152 apply_to_page_range(vma->vm_mm, vma->vm_start,
1153 vma->vm_end - vma->vm_start,
1154 set_grant_ptes_as_special, NULL);
1155 }
1156#endif
Daniel De Graafaab8f112011-02-03 12:19:02 -05001157 }
1158
Daniel De Graaff0a70c82011-01-07 11:51:47 +00001159 return 0;
1160
Gerd Hoffmannab315232010-12-14 18:40:46 +00001161unlock_out:
David Vrabel1401c002015-01-09 18:06:12 +00001162 mutex_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +00001163 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001164
Dan Carpentera93e20a2011-03-19 08:45:43 +03001165out_unlock_put:
David Vrabel1401c002015-01-09 18:06:12 +00001166 mutex_unlock(&priv->lock);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001167out_put_map:
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001168 if (use_ptemod) {
Daniel De Graaf84e40752011-02-09 15:11:59 -05001169 map->vma = NULL;
Ross Lagerwallcf2acf62018-01-09 12:10:22 +00001170 unmap_grant_pages(map, 0, map->count);
1171 }
Daniel De Graaf16a1d022013-01-02 22:57:12 +00001172 gntdev_put_map(priv, map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -05001173 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +00001174}
1175
1176static const struct file_operations gntdev_fops = {
1177 .owner = THIS_MODULE,
1178 .open = gntdev_open,
1179 .release = gntdev_release,
1180 .mmap = gntdev_mmap,
1181 .unlocked_ioctl = gntdev_ioctl
1182};
1183
1184static struct miscdevice gntdev_miscdev = {
1185 .minor = MISC_DYNAMIC_MINOR,
1186 .name = "xen/gntdev",
1187 .fops = &gntdev_fops,
1188};
1189
1190/* ------------------------------------------------------------------ */
1191
1192static int __init gntdev_init(void)
1193{
1194 int err;
1195
1196 if (!xen_domain())
1197 return -ENODEV;
1198
Konrad Rzeszutek Wilk6926f6d2014-01-03 10:20:18 -05001199 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
Daniel De Graafaab8f112011-02-03 12:19:02 -05001200
Gerd Hoffmannab315232010-12-14 18:40:46 +00001201 err = misc_register(&gntdev_miscdev);
1202 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -07001203 pr_err("Could not register gntdev device\n");
Gerd Hoffmannab315232010-12-14 18:40:46 +00001204 return err;
1205 }
1206 return 0;
1207}
1208
1209static void __exit gntdev_exit(void)
1210{
1211 misc_deregister(&gntdev_miscdev);
1212}
1213
1214module_init(gntdev_init);
1215module_exit(gntdev_exit);
1216
1217/* ------------------------------------------------------------------ */