1184299Snwhitehorn/******************************************************************************
2184299Snwhitehorn * gntdev.c
3184299Snwhitehorn *
4184299Snwhitehorn * Device for accessing (in user-space) pages that have been granted by other
5184299Snwhitehorn * domains.
6184299Snwhitehorn *
7184299Snwhitehorn * Copyright (c) 2006-2007, D G Murray.
8184299Snwhitehorn *           (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
9184299Snwhitehorn *           (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
10184299Snwhitehorn *
11184299Snwhitehorn * This program is distributed in the hope that it will be useful,
12184299Snwhitehorn * but WITHOUT ANY WARRANTY; without even the implied warranty of
13184299Snwhitehorn * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14184299Snwhitehorn * GNU General Public License for more details.
15184299Snwhitehorn *
16184299Snwhitehorn * You should have received a copy of the GNU General Public License
17184299Snwhitehorn * along with this program; if not, write to the Free Software
18184299Snwhitehorn * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19184299Snwhitehorn */
20184299Snwhitehorn
21184299Snwhitehorn#undef DEBUG
22184299Snwhitehorn
23184299Snwhitehorn#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
24184299Snwhitehorn
25184299Snwhitehorn#include <linux/dma-mapping.h>
26184299Snwhitehorn#include <linux/module.h>
27184299Snwhitehorn#include <linux/kernel.h>
28184299Snwhitehorn#include <linux/init.h>
29184299Snwhitehorn#include <linux/miscdevice.h>
30184299Snwhitehorn#include <linux/fs.h>
31184299Snwhitehorn#include <linux/uaccess.h>
32184299Snwhitehorn#include <linux/sched.h>
33184299Snwhitehorn#include <linux/sched/mm.h>
34184299Snwhitehorn#include <linux/spinlock.h>
35184299Snwhitehorn#include <linux/slab.h>
36184299Snwhitehorn#include <linux/highmem.h>
37184299Snwhitehorn#include <linux/refcount.h>
38205506Snwhitehorn#include <linux/workqueue.h>
39212054Snwhitehorn
40185727Snwhitehorn#include <xen/xen.h>
41184299Snwhitehorn#include <xen/grant_table.h>
42184299Snwhitehorn#include <xen/balloon.h>
43184299Snwhitehorn#include <xen/gntdev.h>
44185782Snwhitehorn#include <xen/events.h>
45184299Snwhitehorn#include <xen/page.h>
46184299Snwhitehorn#include <asm/xen/hypervisor.h>
47184299Snwhitehorn#include <asm/xen/hypercall.h>
48184299Snwhitehorn
49184299Snwhitehorn#include "gntdev-common.h"
50184299Snwhitehorn#ifdef CONFIG_XEN_GNTDEV_DMABUF
51184299Snwhitehorn#include "gntdev-dmabuf.h"
52184299Snwhitehorn#endif
53184299Snwhitehorn
54184299SnwhitehornMODULE_LICENSE("GPL");
55184299SnwhitehornMODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
56184299Snwhitehorn	      "Gerd Hoffmann <kraxel@redhat.com>");
57184299SnwhitehornMODULE_DESCRIPTION("User-space granted page access driver");
58184299Snwhitehorn
59205506Snwhitehornstatic unsigned int limit = 64*1024;
60184299Snwhitehornmodule_param(limit, uint, 0644);
61184299SnwhitehornMODULE_PARM_DESC(limit,
62184299Snwhitehorn	"Maximum number of grants that may be mapped by one mapping request");
63184299Snwhitehorn
64205506Snwhitehorn/* True in PV mode, false otherwise */
65184299Snwhitehornstatic int use_ptemod;
66184299Snwhitehorn
67184299Snwhitehornstatic void unmap_grant_pages(struct gntdev_grant_map *map,
68184299Snwhitehorn			      int offset, int pages);
69184299Snwhitehorn
70205506Snwhitehornstatic struct miscdevice gntdev_miscdev;
71205506Snwhitehorn
72205506Snwhitehorn/* ------------------------------------------------------------------ */
73205506Snwhitehorn
74205506Snwhitehornbool gntdev_test_page_count(unsigned int count)
75205506Snwhitehorn{
76205506Snwhitehorn	return !count || count > limit;
77205506Snwhitehorn}
78205506Snwhitehorn
79205506Snwhitehornstatic void gntdev_print_maps(struct gntdev_priv *priv,
80185727Snwhitehorn			      char *text, int text_index)
81185754Snwhitehorn{
82185727Snwhitehorn#ifdef DEBUG
83194027Savg	struct gntdev_grant_map *map;
84185754Snwhitehorn
85212054Snwhitehorn	pr_debug("%s: maps list (priv %p)\n", __func__, priv);
86212054Snwhitehorn	list_for_each_entry(map, &priv->maps, next)
87212054Snwhitehorn		pr_debug("  index %2d, count %2d %s\n",
88212054Snwhitehorn		       map->index, map->count,
89212054Snwhitehorn		       map->index == text_index && text ? text : "");
90185782Snwhitehorn#endif
91185727Snwhitehorn}
92193159Snwhitehorn
93185754Snwhitehornstatic void gntdev_free_map(struct gntdev_grant_map *map)
94185754Snwhitehorn{
95185754Snwhitehorn	if (map == NULL)
96184299Snwhitehorn		return;
97185754Snwhitehorn
98185754Snwhitehorn#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
99185754Snwhitehorn	if (map->dma_vaddr) {
100185754Snwhitehorn		struct gnttab_dma_alloc_args args;
101185754Snwhitehorn
102185754Snwhitehorn		args.dev = map->dma_dev;
103185754Snwhitehorn		args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
104185754Snwhitehorn		args.nr_pages = map->count;
105185754Snwhitehorn		args.pages = map->pages;
106185754Snwhitehorn		args.frames = map->frames;
107185754Snwhitehorn		args.vaddr = map->dma_vaddr;
108185754Snwhitehorn		args.dev_bus_addr = map->dma_bus_addr;
109185754Snwhitehorn
110185754Snwhitehorn		gnttab_dma_free_pages(&args);
111185754Snwhitehorn	} else
112184299Snwhitehorn#endif
113184299Snwhitehorn	if (map->pages)
114184299Snwhitehorn		gnttab_free_pages(map->count, map->pages);
115184299Snwhitehorn
116184299Snwhitehorn#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
117184299Snwhitehorn	kvfree(map->frames);
118184299Snwhitehorn#endif
119184299Snwhitehorn	kvfree(map->pages);
120184299Snwhitehorn	kvfree(map->grants);
121184299Snwhitehorn	kvfree(map->map_ops);
122184299Snwhitehorn	kvfree(map->unmap_ops);
123184299Snwhitehorn	kvfree(map->kmap_ops);
124184299Snwhitehorn	kvfree(map->kunmap_ops);
125184299Snwhitehorn	kvfree(map->being_removed);
126184299Snwhitehorn	kfree(map);
127184299Snwhitehorn}
128184299Snwhitehorn
129184299Snwhitehornstruct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
130205506Snwhitehorn					  int dma_flags)
131205506Snwhitehorn{
132205506Snwhitehorn	struct gntdev_grant_map *add;
133205506Snwhitehorn	int i;
134184299Snwhitehorn
135184299Snwhitehorn	add = kzalloc(sizeof(*add), GFP_KERNEL);
136184299Snwhitehorn	if (NULL == add)
137184299Snwhitehorn		return NULL;
138184299Snwhitehorn
139184299Snwhitehorn	add->grants    = kvmalloc_array(count, sizeof(add->grants[0]),
140184299Snwhitehorn					GFP_KERNEL);
141184299Snwhitehorn	add->map_ops   = kvmalloc_array(count, sizeof(add->map_ops[0]),
142184299Snwhitehorn					GFP_KERNEL);
143184299Snwhitehorn	add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]),
144184299Snwhitehorn					GFP_KERNEL);
145184299Snwhitehorn	add->pages     = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
146184299Snwhitehorn	add->being_removed =
147184299Snwhitehorn		kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL);
148184299Snwhitehorn	if (NULL == add->grants    ||
149184299Snwhitehorn	    NULL == add->map_ops   ||
150184299Snwhitehorn	    NULL == add->unmap_ops ||
151184299Snwhitehorn	    NULL == add->pages     ||
152184299Snwhitehorn	    NULL == add->being_removed)
153184299Snwhitehorn		goto err;
154184299Snwhitehorn	if (use_ptemod) {
155184299Snwhitehorn		add->kmap_ops   = kvmalloc_array(count, sizeof(add->kmap_ops[0]),
156184299Snwhitehorn						 GFP_KERNEL);
157184299Snwhitehorn		add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]),
158184299Snwhitehorn						 GFP_KERNEL);
159184299Snwhitehorn		if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
160184299Snwhitehorn			goto err;
161184299Snwhitehorn	}
162184299Snwhitehorn
163184299Snwhitehorn#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
164184299Snwhitehorn	add->dma_flags = dma_flags;
165184299Snwhitehorn
166184299Snwhitehorn	/*
167184299Snwhitehorn	 * Check if this mapping is requested to be backed
168184299Snwhitehorn	 * by a DMA buffer.
169184299Snwhitehorn	 */
170184299Snwhitehorn	if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
171184299Snwhitehorn		struct gnttab_dma_alloc_args args;
172184299Snwhitehorn
173184299Snwhitehorn		add->frames = kvcalloc(count, sizeof(add->frames[0]),
174184299Snwhitehorn				       GFP_KERNEL);
175184299Snwhitehorn		if (!add->frames)
176184299Snwhitehorn			goto err;
177184299Snwhitehorn
178184299Snwhitehorn		/* Remember the device, so we can free DMA memory. */
179184299Snwhitehorn		add->dma_dev = priv->dma_dev;
180184299Snwhitehorn
181184299Snwhitehorn		args.dev = priv->dma_dev;
182184299Snwhitehorn		args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
183184299Snwhitehorn		args.nr_pages = count;
184184299Snwhitehorn		args.pages = add->pages;
185184299Snwhitehorn		args.frames = add->frames;
186184299Snwhitehorn
187184299Snwhitehorn		if (gnttab_dma_alloc_pages(&args))
188184299Snwhitehorn			goto err;
189184299Snwhitehorn
190184299Snwhitehorn		add->dma_vaddr = args.vaddr;
191184299Snwhitehorn		add->dma_bus_addr = args.dev_bus_addr;
192184299Snwhitehorn	} else
193184299Snwhitehorn#endif
194184299Snwhitehorn	if (gnttab_alloc_pages(count, add->pages))
195184299Snwhitehorn		goto err;
196184299Snwhitehorn
197184299Snwhitehorn	for (i = 0; i < count; i++) {
198184299Snwhitehorn		add->grants[i].domid = DOMID_INVALID;
199184299Snwhitehorn		add->grants[i].ref = INVALID_GRANT_REF;
200184299Snwhitehorn		add->map_ops[i].handle = INVALID_GRANT_HANDLE;
201184299Snwhitehorn		add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
202184299Snwhitehorn		if (use_ptemod) {
203184299Snwhitehorn			add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
204184299Snwhitehorn			add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
205184299Snwhitehorn		}
206184299Snwhitehorn	}
207184299Snwhitehorn
208184299Snwhitehorn	add->index = 0;
209184299Snwhitehorn	add->count = count;
210184299Snwhitehorn	refcount_set(&add->users, 1);
211184299Snwhitehorn
212184299Snwhitehorn	return add;
213184299Snwhitehorn
214184299Snwhitehornerr:
215184299Snwhitehorn	gntdev_free_map(add);
216184299Snwhitehorn	return NULL;
217184299Snwhitehorn}
218184299Snwhitehorn
219184299Snwhitehornvoid gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
220184299Snwhitehorn{
221184299Snwhitehorn	struct gntdev_grant_map *map;
222184299Snwhitehorn
223184299Snwhitehorn	list_for_each_entry(map, &priv->maps, next) {
224184299Snwhitehorn		if (add->index + add->count < map->index) {
225184299Snwhitehorn			list_add_tail(&add->next, &map->next);
226184299Snwhitehorn			goto done;
227184299Snwhitehorn		}
228184299Snwhitehorn		add->index = map->index + map->count;
229184299Snwhitehorn	}
230184299Snwhitehorn	list_add_tail(&add->next, &priv->maps);
231184299Snwhitehorn
232184299Snwhitehorndone:
233184299Snwhitehorn	gntdev_print_maps(priv, "[new]", add->index);
234184299Snwhitehorn}
235184299Snwhitehorn
236184299Snwhitehornstatic struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
237184299Snwhitehorn						      int index, int count)
238184299Snwhitehorn{
239184299Snwhitehorn	struct gntdev_grant_map *map;
240184299Snwhitehorn
241184299Snwhitehorn	list_for_each_entry(map, &priv->maps, next) {
242184299Snwhitehorn		if (map->index != index)
243184299Snwhitehorn			continue;
244184299Snwhitehorn		if (count && map->count != count)
245184299Snwhitehorn			continue;
246184299Snwhitehorn		return map;
247184299Snwhitehorn	}
248184299Snwhitehorn	return NULL;
249184299Snwhitehorn}
250184299Snwhitehorn
251184299Snwhitehornvoid gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
252184299Snwhitehorn{
253184299Snwhitehorn	if (!map)
254184299Snwhitehorn		return;
255184299Snwhitehorn
256184299Snwhitehorn	if (!refcount_dec_and_test(&map->users))
257184299Snwhitehorn		return;
258184299Snwhitehorn
259184299Snwhitehorn	if (map->pages && !use_ptemod) {
260184299Snwhitehorn		/*
261184299Snwhitehorn		 * Increment the reference count.  This ensures that the
262184299Snwhitehorn		 * subsequent call to unmap_grant_pages() will not wind up
263184299Snwhitehorn		 * re-entering itself.  It *can* wind up calling
264184299Snwhitehorn		 * gntdev_put_map() recursively, but such calls will be with a
265184299Snwhitehorn		 * reference count greater than 1, so they will return before
266184299Snwhitehorn		 * this code is reached.  The recursion depth is thus limited to
267184299Snwhitehorn		 * 1.  Do NOT use refcount_inc() here, as it will detect that
268184299Snwhitehorn		 * the reference count is zero and WARN().
269184299Snwhitehorn		 */
270184299Snwhitehorn		refcount_set(&map->users, 1);
271184299Snwhitehorn
272184299Snwhitehorn		/*
273184299Snwhitehorn		 * Unmap the grants.  This may or may not be asynchronous, so it
274184299Snwhitehorn		 * is possible that the reference count is 1 on return, but it
275184299Snwhitehorn		 * could also be greater than 1.
276184299Snwhitehorn		 */
277184299Snwhitehorn		unmap_grant_pages(map, 0, map->count);
278184299Snwhitehorn
279184299Snwhitehorn		/* Check if the memory now needs to be freed */
280184299Snwhitehorn		if (!refcount_dec_and_test(&map->users))
281184299Snwhitehorn			return;
282184299Snwhitehorn
283184299Snwhitehorn		/*
284184299Snwhitehorn		 * All pages have been returned to the hypervisor, so free the
285184299Snwhitehorn		 * map.
286184299Snwhitehorn		 */
287184299Snwhitehorn	}
288184299Snwhitehorn
289184299Snwhitehorn	if (use_ptemod && map->notifier_init)
290184299Snwhitehorn		mmu_interval_notifier_remove(&map->notifier);
291184299Snwhitehorn
292184299Snwhitehorn	if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
293184299Snwhitehorn		notify_remote_via_evtchn(map->notify.event);
294184299Snwhitehorn		evtchn_put(map->notify.event);
295184299Snwhitehorn	}
296184299Snwhitehorn	gntdev_free_map(map);
297184299Snwhitehorn}
298184299Snwhitehorn
299184299Snwhitehorn/* ------------------------------------------------------------------ */
300184299Snwhitehorn
301184299Snwhitehornstatic int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
302184299Snwhitehorn{
303184299Snwhitehorn	struct gntdev_grant_map *map = data;
304184299Snwhitehorn	unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT;
305184299Snwhitehorn	int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
306184299Snwhitehorn		    (1 << _GNTMAP_guest_avail0);
307184299Snwhitehorn	u64 pte_maddr;
308184299Snwhitehorn
309184299Snwhitehorn	BUG_ON(pgnr >= map->count);
310184299Snwhitehorn	pte_maddr = arbitrary_virt_to_machine(pte).maddr;
311184299Snwhitehorn
312184299Snwhitehorn	gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
313184299Snwhitehorn			  map->grants[pgnr].ref,
314184299Snwhitehorn			  map->grants[pgnr].domid);
315184299Snwhitehorn	gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
316184299Snwhitehorn			    INVALID_GRANT_HANDLE);
317184299Snwhitehorn	return 0;
318184299Snwhitehorn}
319184299Snwhitehorn
320184299Snwhitehornint gntdev_map_grant_pages(struct gntdev_grant_map *map)
321184299Snwhitehorn{
322184299Snwhitehorn	size_t alloced = 0;
323184299Snwhitehorn	int i, err = 0;
324185754Snwhitehorn
325184299Snwhitehorn	if (!use_ptemod) {
326184299Snwhitehorn		/* Note: it could already be mapped */
327184299Snwhitehorn		if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
328184299Snwhitehorn			return 0;
329185727Snwhitehorn		for (i = 0; i < map->count; i++) {
330185727Snwhitehorn			unsigned long addr = (unsigned long)
331184299Snwhitehorn				pfn_to_kaddr(page_to_pfn(map->pages[i]));
332184299Snwhitehorn			gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
333184299Snwhitehorn				map->grants[i].ref,
334184299Snwhitehorn				map->grants[i].domid);
335184299Snwhitehorn			gnttab_set_unmap_op(&map->unmap_ops[i], addr,
336184299Snwhitehorn				map->flags, INVALID_GRANT_HANDLE);
337184299Snwhitehorn		}
338184299Snwhitehorn	} else {
339184299Snwhitehorn		/*
340184299Snwhitehorn		 * Setup the map_ops corresponding to the pte entries pointing
341184299Snwhitehorn		 * to the kernel linear addresses of the struct pages.
342184299Snwhitehorn		 * These ptes are completely different from the user ptes dealt
343184299Snwhitehorn		 * with find_grant_ptes.
344184299Snwhitehorn		 * Note that GNTMAP_device_map isn't needed here: The
345184299Snwhitehorn		 * dev_bus_addr output field gets consumed only from ->map_ops,
346184299Snwhitehorn		 * and by not requesting it when mapping we also avoid needing
347184299Snwhitehorn		 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
348184299Snwhitehorn		 * reference to the page in the hypervisor).
349184299Snwhitehorn		 */
350184299Snwhitehorn		unsigned int flags = (map->flags & ~GNTMAP_device_map) |
351184299Snwhitehorn				     GNTMAP_host_map;
352184299Snwhitehorn
353184299Snwhitehorn		for (i = 0; i < map->count; i++) {
354184299Snwhitehorn			unsigned long address = (unsigned long)
355184299Snwhitehorn				pfn_to_kaddr(page_to_pfn(map->pages[i]));
356184299Snwhitehorn			BUG_ON(PageHighMem(map->pages[i]));
357184299Snwhitehorn
358185782Snwhitehorn			gnttab_set_map_op(&map->kmap_ops[i], address, flags,
359185782Snwhitehorn				map->grants[i].ref,
360185782Snwhitehorn				map->grants[i].domid);
361184299Snwhitehorn			gnttab_set_unmap_op(&map->kunmap_ops[i], address,
362184299Snwhitehorn				flags, INVALID_GRANT_HANDLE);
363184299Snwhitehorn		}
364184299Snwhitehorn	}
365184299Snwhitehorn
366184299Snwhitehorn	pr_debug("map %d+%d\n", map->index, map->count);
367184299Snwhitehorn	err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
368184299Snwhitehorn			map->count);
369184299Snwhitehorn
370184299Snwhitehorn	for (i = 0; i < map->count; i++) {
371184299Snwhitehorn		if (map->map_ops[i].status == GNTST_okay) {
372184299Snwhitehorn			map->unmap_ops[i].handle = map->map_ops[i].handle;
373184299Snwhitehorn			alloced++;
374184299Snwhitehorn		} else if (!err)
375184299Snwhitehorn			err = -EINVAL;
376184299Snwhitehorn
377184299Snwhitehorn		if (map->flags & GNTMAP_device_map)
378184299Snwhitehorn			map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
379184299Snwhitehorn
380184299Snwhitehorn		if (use_ptemod) {
381184299Snwhitehorn			if (map->kmap_ops[i].status == GNTST_okay) {
382184299Snwhitehorn				alloced++;
383184299Snwhitehorn				map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
384184299Snwhitehorn			} else if (!err)
385184299Snwhitehorn				err = -EINVAL;
386184299Snwhitehorn		}
387184299Snwhitehorn	}
388184299Snwhitehorn	atomic_add(alloced, &map->live_grants);
389185754Snwhitehorn	return err;
390185754Snwhitehorn}
391185754Snwhitehorn
392185754Snwhitehornstatic void __unmap_grant_pages_done(int result,
393185754Snwhitehorn		struct gntab_unmap_queue_data *data)
394185754Snwhitehorn{
395185754Snwhitehorn	unsigned int i;
396185754Snwhitehorn	struct gntdev_grant_map *map = data->data;
397185754Snwhitehorn	unsigned int offset = data->unmap_ops - map->unmap_ops;
398185754Snwhitehorn	int successful_unmaps = 0;
399185754Snwhitehorn	int live_grants;
400185754Snwhitehorn
401184299Snwhitehorn	for (i = 0; i < data->count; i++) {
402184299Snwhitehorn		if (map->unmap_ops[offset + i].status == GNTST_okay &&
403185727Snwhitehorn		    map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
404185727Snwhitehorn			successful_unmaps++;
405185727Snwhitehorn
406185727Snwhitehorn		WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay &&
407185727Snwhitehorn			map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
408185727Snwhitehorn		pr_debug("unmap handle=%d st=%d\n",
409185727Snwhitehorn			map->unmap_ops[offset+i].handle,
410185727Snwhitehorn			map->unmap_ops[offset+i].status);
411185754Snwhitehorn		map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
412185754Snwhitehorn		if (use_ptemod) {
413185727Snwhitehorn			if (map->kunmap_ops[offset + i].status == GNTST_okay &&
414185754Snwhitehorn			    map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
415185754Snwhitehorn				successful_unmaps++;
416185754Snwhitehorn
417185754Snwhitehorn			WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay &&
418193159Snwhitehorn				map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
419193159Snwhitehorn			pr_debug("kunmap handle=%u st=%d\n",
420193159Snwhitehorn				 map->kunmap_ops[offset+i].handle,
421193159Snwhitehorn				 map->kunmap_ops[offset+i].status);
422185754Snwhitehorn			map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
423185754Snwhitehorn		}
424185754Snwhitehorn	}
425185754Snwhitehorn
426185754Snwhitehorn	/*
427185754Snwhitehorn	 * Decrease the live-grant counter.  This must happen after the loop to
428185754Snwhitehorn	 * prevent premature reuse of the grants by gnttab_mmap().
429185754Snwhitehorn	 */
430185754Snwhitehorn	live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
431185754Snwhitehorn	if (WARN_ON(live_grants < 0))
432185754Snwhitehorn		pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
433185754Snwhitehorn		       __func__, live_grants, successful_unmaps);
434185754Snwhitehorn
435185754Snwhitehorn	/* Release reference taken by __unmap_grant_pages */
436185754Snwhitehorn	gntdev_put_map(NULL, map);
437185754Snwhitehorn}
438185754Snwhitehorn
439185754Snwhitehornstatic void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
440185754Snwhitehorn			       int pages)
441185754Snwhitehorn{
442185754Snwhitehorn	if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
443185754Snwhitehorn		int pgno = (map->notify.addr >> PAGE_SHIFT);
444185754Snwhitehorn
445185754Snwhitehorn		if (pgno >= offset && pgno < offset + pages) {
446185754Snwhitehorn			/* No need for kmap, pages are in lowmem */
447185754Snwhitehorn			uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
448185754Snwhitehorn
449185754Snwhitehorn			tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
450185754Snwhitehorn			map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
451185754Snwhitehorn		}
452185754Snwhitehorn	}
453185754Snwhitehorn
454185754Snwhitehorn	map->unmap_data.unmap_ops = map->unmap_ops + offset;
455185754Snwhitehorn	map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
456185754Snwhitehorn	map->unmap_data.pages = map->pages + offset;
457185754Snwhitehorn	map->unmap_data.count = pages;
458185754Snwhitehorn	map->unmap_data.done = __unmap_grant_pages_done;
459185754Snwhitehorn	map->unmap_data.data = map;
460185754Snwhitehorn	refcount_inc(&map->users); /* to keep map alive during async call below */
461185754Snwhitehorn
462185754Snwhitehorn	gnttab_unmap_refs_async(&map->unmap_data);
463185754Snwhitehorn}
464185754Snwhitehorn
465185754Snwhitehornstatic void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
466185754Snwhitehorn			      int pages)
467185754Snwhitehorn{
468185754Snwhitehorn	int range;
469185754Snwhitehorn
470185754Snwhitehorn	if (atomic_read(&map->live_grants) == 0)
471185782Snwhitehorn		return; /* Nothing to do */
472185782Snwhitehorn
473185782Snwhitehorn	pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
474185782Snwhitehorn
475185782Snwhitehorn	/* It is possible the requested range will have a "hole" where we
476185782Snwhitehorn	 * already unmapped some of the grants. Only unmap valid ranges.
477205506Snwhitehorn	 */
478205506Snwhitehorn	while (pages) {
479205506Snwhitehorn		while (pages && map->being_removed[offset]) {
480205506Snwhitehorn			offset++;
481205506Snwhitehorn			pages--;
482205506Snwhitehorn		}
483212054Snwhitehorn		range = 0;
484212054Snwhitehorn		while (range < pages) {
485212054Snwhitehorn			if (map->being_removed[offset + range])
486212054Snwhitehorn				break;
487212054Snwhitehorn			map->being_removed[offset + range] = true;
488212054Snwhitehorn			range++;
489184299Snwhitehorn		}
490184299Snwhitehorn		if (range)
491184299Snwhitehorn			__unmap_grant_pages(map, offset, range);
492184299Snwhitehorn		offset += range;
493184299Snwhitehorn		pages -= range;
494184299Snwhitehorn	}
495184299Snwhitehorn}
496184299Snwhitehorn
497184299Snwhitehorn/* ------------------------------------------------------------------ */
498184299Snwhitehorn
499185782Snwhitehornstatic void gntdev_vma_open(struct vm_area_struct *vma)
500185782Snwhitehorn{
501185782Snwhitehorn	struct gntdev_grant_map *map = vma->vm_private_data;
502184299Snwhitehorn
503184299Snwhitehorn	pr_debug("gntdev_vma_open %p\n", vma);
504184299Snwhitehorn	refcount_inc(&map->users);
505184299Snwhitehorn}
506184299Snwhitehorn
507184299Snwhitehornstatic void gntdev_vma_close(struct vm_area_struct *vma)
508184299Snwhitehorn{
509184299Snwhitehorn	struct gntdev_grant_map *map = vma->vm_private_data;
510184299Snwhitehorn	struct file *file = vma->vm_file;
511184299Snwhitehorn	struct gntdev_priv *priv = file->private_data;
512184299Snwhitehorn
513184299Snwhitehorn	pr_debug("gntdev_vma_close %p\n", vma);
514184299Snwhitehorn
515184299Snwhitehorn	vma->vm_private_data = NULL;
516184299Snwhitehorn	gntdev_put_map(priv, map);
517184299Snwhitehorn}
518184299Snwhitehorn
519184299Snwhitehornstatic struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
520184299Snwhitehorn						 unsigned long addr)
521184299Snwhitehorn{
522184299Snwhitehorn	struct gntdev_grant_map *map = vma->vm_private_data;
523184299Snwhitehorn
524184299Snwhitehorn	return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
525184299Snwhitehorn}
526184299Snwhitehorn
527184299Snwhitehornstatic const struct vm_operations_struct gntdev_vmops = {
528184299Snwhitehorn	.open = gntdev_vma_open,
529184299Snwhitehorn	.close = gntdev_vma_close,
530184299Snwhitehorn	.find_special_page = gntdev_vma_find_special_page,
531184299Snwhitehorn};
532184299Snwhitehorn
533184299Snwhitehorn/* ------------------------------------------------------------------ */
534184299Snwhitehorn
535184299Snwhitehornstatic bool gntdev_invalidate(struct mmu_interval_notifier *mn,
536184299Snwhitehorn			      const struct mmu_notifier_range *range,
537184299Snwhitehorn			      unsigned long cur_seq)
538184299Snwhitehorn{
539184299Snwhitehorn	struct gntdev_grant_map *map =
540184299Snwhitehorn		container_of(mn, struct gntdev_grant_map, notifier);
541184299Snwhitehorn	unsigned long mstart, mend;
542184299Snwhitehorn	unsigned long map_start, map_end;
543184299Snwhitehorn
544184299Snwhitehorn	if (!mmu_notifier_range_blockable(range))
545184299Snwhitehorn		return false;
546184299Snwhitehorn
547184299Snwhitehorn	map_start = map->pages_vm_start;
548184299Snwhitehorn	map_end = map->pages_vm_start + (map->count << PAGE_SHIFT);
549184299Snwhitehorn
550184299Snwhitehorn	/*
551184299Snwhitehorn	 * If the VMA is split or otherwise changed the notifier is not
552184299Snwhitehorn	 * updated, but we don't want to process VA's outside the modified
553184299Snwhitehorn	 * VMA. FIXME: It would be much more understandable to just prevent
554184299Snwhitehorn	 * modifying the VMA in the first place.
555184299Snwhitehorn	 */
556184299Snwhitehorn	if (map_start >= range->end || map_end <= range->start)
557184299Snwhitehorn		return true;
558184299Snwhitehorn
559184299Snwhitehorn	mstart = max(range->start, map_start);
560184299Snwhitehorn	mend = min(range->end, map_end);
561184299Snwhitehorn	pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
562184299Snwhitehorn		 map->index, map->count, map_start, map_end,
563184299Snwhitehorn		 range->start, range->end, mstart, mend);
564184299Snwhitehorn	unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT,
565184299Snwhitehorn			  (mend - mstart) >> PAGE_SHIFT);
566184299Snwhitehorn
567184299Snwhitehorn	return true;
568184299Snwhitehorn}
569184299Snwhitehorn
570184299Snwhitehornstatic const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
571184299Snwhitehorn	.invalidate = gntdev_invalidate,
572184299Snwhitehorn};
573184299Snwhitehorn
574184299Snwhitehorn/* ------------------------------------------------------------------ */
575184299Snwhitehorn
576184299Snwhitehornstatic int gntdev_open(struct inode *inode, struct file *flip)
577184299Snwhitehorn{
578184299Snwhitehorn	struct gntdev_priv *priv;
579184299Snwhitehorn
580184299Snwhitehorn	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
581184299Snwhitehorn	if (!priv)
582184299Snwhitehorn		return -ENOMEM;
583184299Snwhitehorn
584184299Snwhitehorn	INIT_LIST_HEAD(&priv->maps);
585184299Snwhitehorn	mutex_init(&priv->lock);
586184299Snwhitehorn
587184299Snwhitehorn#ifdef CONFIG_XEN_GNTDEV_DMABUF
588184299Snwhitehorn	priv->dmabuf_priv = gntdev_dmabuf_init(flip);
589184299Snwhitehorn	if (IS_ERR(priv->dmabuf_priv)) {
590184299Snwhitehorn		int ret = PTR_ERR(priv->dmabuf_priv);
591184299Snwhitehorn
592184299Snwhitehorn		kfree(priv);
593184299Snwhitehorn		return ret;
594184299Snwhitehorn	}
595184299Snwhitehorn#endif
596184299Snwhitehorn
597184299Snwhitehorn	flip->private_data = priv;
598184299Snwhitehorn#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
599184299Snwhitehorn	priv->dma_dev = gntdev_miscdev.this_device;
600184299Snwhitehorn	dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
601184299Snwhitehorn#endif
602184299Snwhitehorn	pr_debug("priv %p\n", priv);
603184299Snwhitehorn
604184299Snwhitehorn	return 0;
605184299Snwhitehorn}
606184299Snwhitehorn
607184299Snwhitehornstatic int gntdev_release(struct inode *inode, struct file *flip)
608184299Snwhitehorn{
609184299Snwhitehorn	struct gntdev_priv *priv = flip->private_data;
610184299Snwhitehorn	struct gntdev_grant_map *map;
611194027Savg
612184299Snwhitehorn	pr_debug("priv %p\n", priv);
613184299Snwhitehorn
614184299Snwhitehorn	mutex_lock(&priv->lock);
615194027Savg	while (!list_empty(&priv->maps)) {
616184299Snwhitehorn		map = list_entry(priv->maps.next,
617184299Snwhitehorn				 struct gntdev_grant_map, next);
618184299Snwhitehorn		list_del(&map->next);
619184299Snwhitehorn		gntdev_put_map(NULL /* already removed */, map);
620184299Snwhitehorn	}
621184299Snwhitehorn	mutex_unlock(&priv->lock);
622184299Snwhitehorn
623184299Snwhitehorn#ifdef CONFIG_XEN_GNTDEV_DMABUF
624184299Snwhitehorn	gntdev_dmabuf_fini(priv->dmabuf_priv);
625184299Snwhitehorn#endif
626184299Snwhitehorn
627184299Snwhitehorn	kfree(priv);
628184299Snwhitehorn	return 0;
629184299Snwhitehorn}
630184299Snwhitehorn
631184299Snwhitehornstatic long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
632184299Snwhitehorn				       struct ioctl_gntdev_map_grant_ref __user *u)
633184299Snwhitehorn{
634184299Snwhitehorn	struct ioctl_gntdev_map_grant_ref op;
635184299Snwhitehorn	struct gntdev_grant_map *map;
636184299Snwhitehorn	int err;
637184299Snwhitehorn
638184299Snwhitehorn	if (copy_from_user(&op, u, sizeof(op)) != 0)
639184299Snwhitehorn		return -EFAULT;
640184299Snwhitehorn	pr_debug("priv %p, add %d\n", priv, op.count);
641184299Snwhitehorn	if (unlikely(gntdev_test_page_count(op.count)))
642184299Snwhitehorn		return -EINVAL;
643184299Snwhitehorn
644184299Snwhitehorn	err = -ENOMEM;
645184299Snwhitehorn	map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
646184299Snwhitehorn	if (!map)
647184299Snwhitehorn		return err;
648184299Snwhitehorn
649184299Snwhitehorn	if (copy_from_user(map->grants, &u->refs,
650184299Snwhitehorn			   sizeof(map->grants[0]) * op.count) != 0) {
651184299Snwhitehorn		gntdev_put_map(NULL, map);
652184299Snwhitehorn		return -EFAULT;
653184299Snwhitehorn	}
654184299Snwhitehorn
655184299Snwhitehorn	mutex_lock(&priv->lock);
656184299Snwhitehorn	gntdev_add_map(priv, map);
657184299Snwhitehorn	op.index = map->index << PAGE_SHIFT;
658184299Snwhitehorn	mutex_unlock(&priv->lock);
659184299Snwhitehorn
660184299Snwhitehorn	if (copy_to_user(u, &op, sizeof(op)) != 0)
661184299Snwhitehorn		return -EFAULT;
662184299Snwhitehorn
663184299Snwhitehorn	return 0;
664184299Snwhitehorn}
665184299Snwhitehorn
666184299Snwhitehornstatic long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
667184299Snwhitehorn					 struct ioctl_gntdev_unmap_grant_ref __user *u)
668184299Snwhitehorn{
669184299Snwhitehorn	struct ioctl_gntdev_unmap_grant_ref op;
670184299Snwhitehorn	struct gntdev_grant_map *map;
671184299Snwhitehorn	int err = -ENOENT;
672184299Snwhitehorn
673184299Snwhitehorn	if (copy_from_user(&op, u, sizeof(op)) != 0)
674184299Snwhitehorn		return -EFAULT;
675184299Snwhitehorn	pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
676184299Snwhitehorn
677184299Snwhitehorn	mutex_lock(&priv->lock);
678184299Snwhitehorn	map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
679184299Snwhitehorn	if (map) {
680184299Snwhitehorn		list_del(&map->next);
681184299Snwhitehorn		err = 0;
682184299Snwhitehorn	}
683184299Snwhitehorn	mutex_unlock(&priv->lock);
684184299Snwhitehorn	if (map)
685184299Snwhitehorn		gntdev_put_map(priv, map);
686184299Snwhitehorn	return err;
687184299Snwhitehorn}
688184299Snwhitehorn
689184299Snwhitehornstatic long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
690184299Snwhitehorn					      struct ioctl_gntdev_get_offset_for_vaddr __user *u)
691184299Snwhitehorn{
692184299Snwhitehorn	struct ioctl_gntdev_get_offset_for_vaddr op;
693184299Snwhitehorn	struct vm_area_struct *vma;
694184299Snwhitehorn	struct gntdev_grant_map *map;
695184299Snwhitehorn	int rv = -EINVAL;
696184299Snwhitehorn
697184299Snwhitehorn	if (copy_from_user(&op, u, sizeof(op)) != 0)
698184299Snwhitehorn		return -EFAULT;
699184299Snwhitehorn	pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
700184299Snwhitehorn
701184299Snwhitehorn	mmap_read_lock(current->mm);
702184299Snwhitehorn	vma = find_vma(current->mm, op.vaddr);
703184299Snwhitehorn	if (!vma || vma->vm_ops != &gntdev_vmops)
704184299Snwhitehorn		goto out_unlock;
705184299Snwhitehorn
706184299Snwhitehorn	map = vma->vm_private_data;
707184299Snwhitehorn	if (!map)
708184299Snwhitehorn		goto out_unlock;
709184299Snwhitehorn
710184299Snwhitehorn	op.offset = map->index << PAGE_SHIFT;
711184299Snwhitehorn	op.count = map->count;
712184299Snwhitehorn	rv = 0;
713184299Snwhitehorn
714184299Snwhitehorn out_unlock:
715184299Snwhitehorn	mmap_read_unlock(current->mm);
716184299Snwhitehorn
717184299Snwhitehorn	if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
718184299Snwhitehorn		return -EFAULT;
719184299Snwhitehorn	return rv;
720184299Snwhitehorn}
721184299Snwhitehorn
722184299Snwhitehornstatic long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
723184299Snwhitehorn{
724184299Snwhitehorn	struct ioctl_gntdev_unmap_notify op;
725184299Snwhitehorn	struct gntdev_grant_map *map;
726184299Snwhitehorn	int rc;
727184299Snwhitehorn	int out_flags;
728184299Snwhitehorn	evtchn_port_t out_event;
729184299Snwhitehorn
730184299Snwhitehorn	if (copy_from_user(&op, u, sizeof(op)))
731184299Snwhitehorn		return -EFAULT;
732184299Snwhitehorn
733184299Snwhitehorn	if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
734184299Snwhitehorn		return -EINVAL;
735184299Snwhitehorn
736184299Snwhitehorn	/* We need to grab a reference to the event channel we are going to use
737184299Snwhitehorn	 * to send the notify before releasing the reference we may already have
738184299Snwhitehorn	 * (if someone has called this ioctl twice). This is required so that
739184299Snwhitehorn	 * it is possible to change the clear_byte part of the notification
740184299Snwhitehorn	 * without disturbing the event channel part, which may now be the last
741184299Snwhitehorn	 * reference to that event channel.
742184299Snwhitehorn	 */
743184299Snwhitehorn	if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
744184299Snwhitehorn		if (evtchn_get(op.event_channel_port))
745184299Snwhitehorn			return -EINVAL;
746184299Snwhitehorn	}
747184299Snwhitehorn
748184299Snwhitehorn	out_flags = op.action;
749184299Snwhitehorn	out_event = op.event_channel_port;
750184299Snwhitehorn
751184299Snwhitehorn	mutex_lock(&priv->lock);
752184299Snwhitehorn
753184299Snwhitehorn	list_for_each_entry(map, &priv->maps, next) {
754184299Snwhitehorn		uint64_t begin = map->index << PAGE_SHIFT;
755184299Snwhitehorn		uint64_t end = (map->index + map->count) << PAGE_SHIFT;
756184299Snwhitehorn		if (op.index >= begin && op.index < end)
757184299Snwhitehorn			goto found;
758184299Snwhitehorn	}
759184299Snwhitehorn	rc = -ENOENT;
760184299Snwhitehorn	goto unlock_out;
761184299Snwhitehorn
762184299Snwhitehorn found:
763184299Snwhitehorn	if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
764185727Snwhitehorn			(map->flags & GNTMAP_readonly)) {
765185782Snwhitehorn		rc = -EINVAL;
766212054Snwhitehorn		goto unlock_out;
767212054Snwhitehorn	}
768212054Snwhitehorn
769212054Snwhitehorn	out_flags = map->notify.flags;
770212054Snwhitehorn	out_event = map->notify.event;
771212054Snwhitehorn
772212054Snwhitehorn	map->notify.flags = op.action;
773212054Snwhitehorn	map->notify.addr = op.index - (map->index << PAGE_SHIFT);
774212054Snwhitehorn	map->notify.event = op.event_channel_port;
775212054Snwhitehorn
776212054Snwhitehorn	rc = 0;
777212054Snwhitehorn
778212054Snwhitehorn unlock_out:
779212054Snwhitehorn	mutex_unlock(&priv->lock);
780185782Snwhitehorn
781185782Snwhitehorn	/* Drop the reference to the event channel we did not save in the map */
782185782Snwhitehorn	if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
783185782Snwhitehorn		evtchn_put(out_event);
784185782Snwhitehorn
785185782Snwhitehorn	return rc;
786185782Snwhitehorn}
787185782Snwhitehorn
788185782Snwhitehorn#define GNTDEV_COPY_BATCH 16
789185782Snwhitehorn
790185782Snwhitehornstruct gntdev_copy_batch {
791185782Snwhitehorn	struct gnttab_copy ops[GNTDEV_COPY_BATCH];
792185727Snwhitehorn	struct page *pages[GNTDEV_COPY_BATCH];
793185727Snwhitehorn	s16 __user *status[GNTDEV_COPY_BATCH];
794185727Snwhitehorn	unsigned int nr_ops;
795185727Snwhitehorn	unsigned int nr_pages;
796185727Snwhitehorn	bool writeable;
797185727Snwhitehorn};
798185727Snwhitehorn
799185727Snwhitehornstatic int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
800185727Snwhitehorn				unsigned long *gfn)
801185727Snwhitehorn{
802185727Snwhitehorn	unsigned long addr = (unsigned long)virt;
803185727Snwhitehorn	struct page *page;
804185727Snwhitehorn	unsigned long xen_pfn;
805185727Snwhitehorn	int ret;
806185727Snwhitehorn
807185727Snwhitehorn	ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
808185727Snwhitehorn	if (ret < 0)
809185727Snwhitehorn		return ret;
810185727Snwhitehorn
811185727Snwhitehorn	batch->pages[batch->nr_pages++] = page;
812185727Snwhitehorn
813185727Snwhitehorn	xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
814185727Snwhitehorn	*gfn = pfn_to_gfn(xen_pfn);
815185727Snwhitehorn
816185727Snwhitehorn	return 0;
817185727Snwhitehorn}
818185727Snwhitehorn
819185727Snwhitehornstatic void gntdev_put_pages(struct gntdev_copy_batch *batch)
820185727Snwhitehorn{
821185727Snwhitehorn	unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
822185727Snwhitehorn	batch->nr_pages = 0;
823185727Snwhitehorn	batch->writeable = false;
824185727Snwhitehorn}
825185727Snwhitehorn
826185727Snwhitehornstatic int gntdev_copy(struct gntdev_copy_batch *batch)
827185727Snwhitehorn{
828185727Snwhitehorn	unsigned int i;
829185727Snwhitehorn
830185727Snwhitehorn	gnttab_batch_copy(batch->ops, batch->nr_ops);
831185727Snwhitehorn	gntdev_put_pages(batch);
832185727Snwhitehorn
833185727Snwhitehorn	/*
834185754Snwhitehorn	 * For each completed op, update the status if the op failed
835185754Snwhitehorn	 * and all previous ops for the segment were successful.
836185754Snwhitehorn	 */
837185754Snwhitehorn	for (i = 0; i < batch->nr_ops; i++) {
838185754Snwhitehorn		s16 status = batch->ops[i].status;
839185754Snwhitehorn		s16 old_status;
840185754Snwhitehorn
841185754Snwhitehorn		if (status == GNTST_okay)
842185754Snwhitehorn			continue;
843185754Snwhitehorn
844185754Snwhitehorn		if (__get_user(old_status, batch->status[i]))
845185754Snwhitehorn			return -EFAULT;
846185754Snwhitehorn
847185754Snwhitehorn		if (old_status != GNTST_okay)
848185754Snwhitehorn			continue;
849185754Snwhitehorn
850185754Snwhitehorn		if (__put_user(status, batch->status[i]))
851185754Snwhitehorn			return -EFAULT;
852185754Snwhitehorn	}
853185754Snwhitehorn
854185754Snwhitehorn	batch->nr_ops = 0;
855185754Snwhitehorn	return 0;
856185754Snwhitehorn}
857185754Snwhitehorn
858185754Snwhitehornstatic int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
859185754Snwhitehorn				 struct gntdev_grant_copy_segment *seg,
860185754Snwhitehorn				 s16 __user *status)
861185754Snwhitehorn{
862185754Snwhitehorn	uint16_t copied = 0;
863185754Snwhitehorn
864185754Snwhitehorn	/*
865185754Snwhitehorn	 * Disallow local -> local copies since there is only space in
866185754Snwhitehorn	 * batch->pages for one page per-op and this would be a very
867185754Snwhitehorn	 * expensive memcpy().
868185754Snwhitehorn	 */
869185754Snwhitehorn	if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
870185754Snwhitehorn		return -EINVAL;
871185754Snwhitehorn
872185754Snwhitehorn	/* Can't cross page if source/dest is a grant ref. */
873185754Snwhitehorn	if (seg->flags & GNTCOPY_source_gref) {
874185754Snwhitehorn		if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
875185754Snwhitehorn			return -EINVAL;
876185754Snwhitehorn	}
877185754Snwhitehorn	if (seg->flags & GNTCOPY_dest_gref) {
878185754Snwhitehorn		if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
879185754Snwhitehorn			return -EINVAL;
880185754Snwhitehorn	}
881185754Snwhitehorn
882185754Snwhitehorn	if (put_user(GNTST_okay, status))
883185754Snwhitehorn		return -EFAULT;
884185754Snwhitehorn
885185754Snwhitehorn	while (copied < seg->len) {
886185754Snwhitehorn		struct gnttab_copy *op;
887185754Snwhitehorn		void __user *virt;
888185754Snwhitehorn		size_t len, off;
889185754Snwhitehorn		unsigned long gfn;
890185754Snwhitehorn		int ret;
891185754Snwhitehorn
892185754Snwhitehorn		if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
893185754Snwhitehorn			ret = gntdev_copy(batch);
894185754Snwhitehorn			if (ret < 0)
895185754Snwhitehorn				return ret;
896185754Snwhitehorn		}
897185754Snwhitehorn
898185754Snwhitehorn		len = seg->len - copied;
899193159Snwhitehorn
900193159Snwhitehorn		op = &batch->ops[batch->nr_ops];
901193159Snwhitehorn		op->flags = 0;
902193159Snwhitehorn
903193159Snwhitehorn		if (seg->flags & GNTCOPY_source_gref) {
904193159Snwhitehorn			op->source.u.ref = seg->source.foreign.ref;
905193159Snwhitehorn			op->source.domid = seg->source.foreign.domid;
906193159Snwhitehorn			op->source.offset = seg->source.foreign.offset + copied;
907193159Snwhitehorn			op->flags |= GNTCOPY_source_gref;
908193159Snwhitehorn		} else {
909193159Snwhitehorn			virt = seg->source.virt + copied;
910193159Snwhitehorn			off = (unsigned long)virt & ~XEN_PAGE_MASK;
911193159Snwhitehorn			len = min(len, (size_t)XEN_PAGE_SIZE - off);
912193159Snwhitehorn			batch->writeable = false;
913193159Snwhitehorn
914193159Snwhitehorn			ret = gntdev_get_page(batch, virt, &gfn);
915193159Snwhitehorn			if (ret < 0)
916193159Snwhitehorn				return ret;
917193159Snwhitehorn
918193159Snwhitehorn			op->source.u.gmfn = gfn;
919193159Snwhitehorn			op->source.domid = DOMID_SELF;
920185754Snwhitehorn			op->source.offset = off;
921185754Snwhitehorn		}
922185754Snwhitehorn
923185754Snwhitehorn		if (seg->flags & GNTCOPY_dest_gref) {
924185754Snwhitehorn			op->dest.u.ref = seg->dest.foreign.ref;
925185754Snwhitehorn			op->dest.domid = seg->dest.foreign.domid;
926185754Snwhitehorn			op->dest.offset = seg->dest.foreign.offset + copied;
927185754Snwhitehorn			op->flags |= GNTCOPY_dest_gref;
928185754Snwhitehorn		} else {
929185754Snwhitehorn			virt = seg->dest.virt + copied;
930185754Snwhitehorn			off = (unsigned long)virt & ~XEN_PAGE_MASK;
931185754Snwhitehorn			len = min(len, (size_t)XEN_PAGE_SIZE - off);
932185754Snwhitehorn			batch->writeable = true;
933185754Snwhitehorn
934185754Snwhitehorn			ret = gntdev_get_page(batch, virt, &gfn);
935185754Snwhitehorn			if (ret < 0)
936185754Snwhitehorn				return ret;
937185754Snwhitehorn
938185754Snwhitehorn			op->dest.u.gmfn = gfn;
939185754Snwhitehorn			op->dest.domid = DOMID_SELF;
940185754Snwhitehorn			op->dest.offset = off;
941185754Snwhitehorn		}
942185754Snwhitehorn
943185754Snwhitehorn		op->len = len;
944185754Snwhitehorn		copied += len;
945185754Snwhitehorn
946185754Snwhitehorn		batch->status[batch->nr_ops] = status;
947185754Snwhitehorn		batch->nr_ops++;
948185754Snwhitehorn	}
949185754Snwhitehorn
950185754Snwhitehorn	return 0;
951185754Snwhitehorn}
952185754Snwhitehorn
953185754Snwhitehornstatic long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
954185754Snwhitehorn{
955185754Snwhitehorn	struct ioctl_gntdev_grant_copy copy;
956185754Snwhitehorn	struct gntdev_copy_batch batch;
957185754Snwhitehorn	unsigned int i;
958185754Snwhitehorn	int ret = 0;
959185754Snwhitehorn
960185754Snwhitehorn	if (copy_from_user(&copy, u, sizeof(copy)))
961185754Snwhitehorn		return -EFAULT;
962185754Snwhitehorn
963185754Snwhitehorn	batch.nr_ops = 0;
964185754Snwhitehorn	batch.nr_pages = 0;
965185754Snwhitehorn
966185754Snwhitehorn	for (i = 0; i < copy.count; i++) {
967185754Snwhitehorn		struct gntdev_grant_copy_segment seg;
968185754Snwhitehorn
969185754Snwhitehorn		if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
970185754Snwhitehorn			ret = -EFAULT;
971185754Snwhitehorn			goto out;
972185754Snwhitehorn		}
973185754Snwhitehorn
974185754Snwhitehorn		ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
975185754Snwhitehorn		if (ret < 0)
976205506Snwhitehorn			goto out;
977205506Snwhitehorn
978205506Snwhitehorn		cond_resched();
979205506Snwhitehorn	}
980205506Snwhitehorn	if (batch.nr_ops)
981205506Snwhitehorn		ret = gntdev_copy(&batch);
982205506Snwhitehorn	return ret;
983205506Snwhitehorn
984205506Snwhitehorn  out:
985205506Snwhitehorn	gntdev_put_pages(&batch);
986205506Snwhitehorn	return ret;
987205506Snwhitehorn}
988205506Snwhitehorn
989205506Snwhitehornstatic long gntdev_ioctl(struct file *flip,
990205506Snwhitehorn			 unsigned int cmd, unsigned long arg)
991205506Snwhitehorn{
992205506Snwhitehorn	struct gntdev_priv *priv = flip->private_data;
993205506Snwhitehorn	void __user *ptr = (void __user *)arg;
994205506Snwhitehorn
995205506Snwhitehorn	switch (cmd) {
996205506Snwhitehorn	case IOCTL_GNTDEV_MAP_GRANT_REF:
997205506Snwhitehorn		return gntdev_ioctl_map_grant_ref(priv, ptr);
998205506Snwhitehorn
999205506Snwhitehorn	case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1000205506Snwhitehorn		return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1001205506Snwhitehorn
1002205506Snwhitehorn	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1003205506Snwhitehorn		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1004205506Snwhitehorn
1005205506Snwhitehorn	case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1006205506Snwhitehorn		return gntdev_ioctl_notify(priv, ptr);
1007205506Snwhitehorn
1008205506Snwhitehorn	case IOCTL_GNTDEV_GRANT_COPY:
1009205506Snwhitehorn		return gntdev_ioctl_grant_copy(priv, ptr);
1010205506Snwhitehorn
1011#ifdef CONFIG_XEN_GNTDEV_DMABUF
1012	case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1013		return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1014
1015	case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1016		return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1017
1018	case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1019		return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1020
1021	case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1022		return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1023#endif
1024
1025	default:
1026		pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1027		return -ENOIOCTLCMD;
1028	}
1029
1030	return 0;
1031}
1032
1033static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1034{
1035	struct gntdev_priv *priv = flip->private_data;
1036	int index = vma->vm_pgoff;
1037	int count = vma_pages(vma);
1038	struct gntdev_grant_map *map;
1039	int err = -EINVAL;
1040
1041	if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1042		return -EINVAL;
1043
1044	pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1045		 index, count, vma->vm_start, vma->vm_pgoff);
1046
1047	mutex_lock(&priv->lock);
1048	map = gntdev_find_map_index(priv, index, count);
1049	if (!map)
1050		goto unlock_out;
1051	if (!atomic_add_unless(&map->in_use, 1, 1))
1052		goto unlock_out;
1053
1054	refcount_inc(&map->users);
1055
1056	vma->vm_ops = &gntdev_vmops;
1057
1058	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP);
1059
1060	if (use_ptemod)
1061		vm_flags_set(vma, VM_DONTCOPY);
1062
1063	vma->vm_private_data = map;
1064	if (map->flags) {
1065		if ((vma->vm_flags & VM_WRITE) &&
1066				(map->flags & GNTMAP_readonly))
1067			goto out_unlock_put;
1068	} else {
1069		map->flags = GNTMAP_host_map;
1070		if (!(vma->vm_flags & VM_WRITE))
1071			map->flags |= GNTMAP_readonly;
1072	}
1073
1074	map->pages_vm_start = vma->vm_start;
1075
1076	if (use_ptemod) {
1077		err = mmu_interval_notifier_insert_locked(
1078			&map->notifier, vma->vm_mm, vma->vm_start,
1079			vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1080		if (err)
1081			goto out_unlock_put;
1082
1083		map->notifier_init = true;
1084	}
1085	mutex_unlock(&priv->lock);
1086
1087	if (use_ptemod) {
1088		/*
1089		 * gntdev takes the address of the PTE in find_grant_ptes() and
1090		 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1091		 * purpose of the notifier is to prevent the hypervisor pointer
1092		 * to the PTE from going stale.
1093		 *
1094		 * Since this vma's mappings can't be touched without the
1095		 * mmap_lock, and we are holding it now, there is no need for
1096		 * the notifier_range locking pattern.
1097		 */
1098		mmu_interval_read_begin(&map->notifier);
1099
1100		err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1101					  vma->vm_end - vma->vm_start,
1102					  find_grant_ptes, map);
1103		if (err) {
1104			pr_warn("find_grant_ptes() failure.\n");
1105			goto out_put_map;
1106		}
1107	}
1108
1109	err = gntdev_map_grant_pages(map);
1110	if (err)
1111		goto out_put_map;
1112
1113	if (!use_ptemod) {
1114		err = vm_map_pages_zero(vma, map->pages, map->count);
1115		if (err)
1116			goto out_put_map;
1117	}
1118
1119	return 0;
1120
1121unlock_out:
1122	mutex_unlock(&priv->lock);
1123	return err;
1124
1125out_unlock_put:
1126	mutex_unlock(&priv->lock);
1127out_put_map:
1128	if (use_ptemod)
1129		unmap_grant_pages(map, 0, map->count);
1130	gntdev_put_map(priv, map);
1131	return err;
1132}
1133
1134static const struct file_operations gntdev_fops = {
1135	.owner = THIS_MODULE,
1136	.open = gntdev_open,
1137	.release = gntdev_release,
1138	.mmap = gntdev_mmap,
1139	.unlocked_ioctl = gntdev_ioctl
1140};
1141
1142static struct miscdevice gntdev_miscdev = {
1143	.minor        = MISC_DYNAMIC_MINOR,
1144	.name         = "xen/gntdev",
1145	.fops         = &gntdev_fops,
1146};
1147
1148/* ------------------------------------------------------------------ */
1149
1150static int __init gntdev_init(void)
1151{
1152	int err;
1153
1154	if (!xen_domain())
1155		return -ENODEV;
1156
1157	use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1158
1159	err = misc_register(&gntdev_miscdev);
1160	if (err != 0) {
1161		pr_err("Could not register gntdev device\n");
1162		return err;
1163	}
1164	return 0;
1165}
1166
1167static void __exit gntdev_exit(void)
1168{
1169	misc_deregister(&gntdev_miscdev);
1170}
1171
1172module_init(gntdev_init);
1173module_exit(gntdev_exit);
1174
1175/* ------------------------------------------------------------------ */
1176