gntdev.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /******************************************************************************
  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>
  9. * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
  10. *
  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. #undef DEBUG
  21. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  22. #include <linux/dma-mapping.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/fs.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/sched.h>
  30. #include <linux/sched/mm.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/slab.h>
  33. #include <linux/highmem.h>
  34. #include <linux/refcount.h>
  35. #include <linux/workqueue.h>
  36. #include <xen/xen.h>
  37. #include <xen/grant_table.h>
  38. #include <xen/balloon.h>
  39. #include <xen/gntdev.h>
  40. #include <xen/events.h>
  41. #include <xen/page.h>
  42. #include <asm/xen/hypervisor.h>
  43. #include <asm/xen/hypercall.h>
  44. #include "gntdev-common.h"
  45. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  46. #include "gntdev-dmabuf.h"
  47. #endif
  48. MODULE_LICENSE("GPL");
  49. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  50. "Gerd Hoffmann <kraxel@redhat.com>");
  51. MODULE_DESCRIPTION("User-space granted page access driver");
  52. #define GNTDEV_COPY_BATCH 16
  53. struct gntdev_copy_batch {
  54. struct gnttab_copy ops[GNTDEV_COPY_BATCH];
  55. struct page *pages[GNTDEV_COPY_BATCH];
  56. s16 __user *status[GNTDEV_COPY_BATCH];
  57. unsigned int nr_ops;
  58. unsigned int nr_pages;
  59. bool writeable;
  60. struct gntdev_copy_batch *next;
  61. };
  62. static unsigned int limit = 64*1024;
  63. module_param(limit, uint, 0644);
  64. MODULE_PARM_DESC(limit,
  65. "Maximum number of grants that may be mapped by one mapping request");
  66. /* True in PV mode, false otherwise */
  67. static int use_ptemod;
  68. static void unmap_grant_pages(struct gntdev_grant_map *map,
  69. int offset, int pages);
  70. static struct miscdevice gntdev_miscdev;
  71. /* ------------------------------------------------------------------ */
  72. bool gntdev_test_page_count(unsigned int count)
  73. {
  74. return !count || count > limit;
  75. }
  76. static void gntdev_print_maps(struct gntdev_priv *priv,
  77. char *text, int text_index)
  78. {
  79. #ifdef DEBUG
  80. struct gntdev_grant_map *map;
  81. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  82. list_for_each_entry(map, &priv->maps, next)
  83. pr_debug(" index %2d, count %2d %s\n",
  84. map->index, map->count,
  85. map->index == text_index && text ? text : "");
  86. #endif
  87. }
  88. static void gntdev_free_map(struct gntdev_grant_map *map)
  89. {
  90. if (map == NULL)
  91. return;
  92. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  93. if (map->dma_vaddr) {
  94. struct gnttab_dma_alloc_args args;
  95. args.dev = map->dma_dev;
  96. args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
  97. args.nr_pages = map->count;
  98. args.pages = map->pages;
  99. args.frames = map->frames;
  100. args.vaddr = map->dma_vaddr;
  101. args.dev_bus_addr = map->dma_bus_addr;
  102. gnttab_dma_free_pages(&args);
  103. } else
  104. #endif
  105. if (map->pages)
  106. gnttab_free_pages(map->count, map->pages);
  107. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  108. kvfree(map->frames);
  109. #endif
  110. kvfree(map->pages);
  111. kvfree(map->grants);
  112. kvfree(map->map_ops);
  113. kvfree(map->unmap_ops);
  114. kvfree(map->kmap_ops);
  115. kvfree(map->kunmap_ops);
  116. kvfree(map->being_removed);
  117. kfree(map);
  118. }
  119. struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
  120. int dma_flags)
  121. {
  122. struct gntdev_grant_map *add;
  123. int i;
  124. add = kzalloc(sizeof(*add), GFP_KERNEL);
  125. if (NULL == add)
  126. return NULL;
  127. add->grants = kvmalloc_array(count, sizeof(add->grants[0]),
  128. GFP_KERNEL);
  129. add->map_ops = kvmalloc_array(count, sizeof(add->map_ops[0]),
  130. GFP_KERNEL);
  131. add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]),
  132. GFP_KERNEL);
  133. add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
  134. add->being_removed =
  135. kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL);
  136. if (NULL == add->grants ||
  137. NULL == add->map_ops ||
  138. NULL == add->unmap_ops ||
  139. NULL == add->pages ||
  140. NULL == add->being_removed)
  141. goto err;
  142. if (use_ptemod) {
  143. add->kmap_ops = kvmalloc_array(count, sizeof(add->kmap_ops[0]),
  144. GFP_KERNEL);
  145. add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]),
  146. GFP_KERNEL);
  147. if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
  148. goto err;
  149. }
  150. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  151. add->dma_flags = dma_flags;
  152. /*
  153. * Check if this mapping is requested to be backed
  154. * by a DMA buffer.
  155. */
  156. if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
  157. struct gnttab_dma_alloc_args args;
  158. add->frames = kvcalloc(count, sizeof(add->frames[0]),
  159. GFP_KERNEL);
  160. if (!add->frames)
  161. goto err;
  162. /* Remember the device, so we can free DMA memory. */
  163. add->dma_dev = priv->dma_dev;
  164. args.dev = priv->dma_dev;
  165. args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
  166. args.nr_pages = count;
  167. args.pages = add->pages;
  168. args.frames = add->frames;
  169. if (gnttab_dma_alloc_pages(&args))
  170. goto err;
  171. add->dma_vaddr = args.vaddr;
  172. add->dma_bus_addr = args.dev_bus_addr;
  173. } else
  174. #endif
  175. if (gnttab_alloc_pages(count, add->pages))
  176. goto err;
  177. for (i = 0; i < count; i++) {
  178. add->grants[i].domid = DOMID_INVALID;
  179. add->grants[i].ref = INVALID_GRANT_REF;
  180. add->map_ops[i].handle = INVALID_GRANT_HANDLE;
  181. add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
  182. if (use_ptemod) {
  183. add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
  184. add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
  185. }
  186. }
  187. add->index = 0;
  188. add->count = count;
  189. refcount_set(&add->users, 1);
  190. return add;
  191. err:
  192. gntdev_free_map(add);
  193. return NULL;
  194. }
  195. void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
  196. {
  197. struct gntdev_grant_map *map;
  198. list_for_each_entry(map, &priv->maps, next) {
  199. if (add->index + add->count < map->index) {
  200. list_add_tail(&add->next, &map->next);
  201. goto done;
  202. }
  203. add->index = map->index + map->count;
  204. }
  205. list_add_tail(&add->next, &priv->maps);
  206. done:
  207. gntdev_print_maps(priv, "[new]", add->index);
  208. }
  209. static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  210. int index, int count)
  211. {
  212. struct gntdev_grant_map *map;
  213. list_for_each_entry(map, &priv->maps, next) {
  214. if (map->index != index)
  215. continue;
  216. if (count && map->count != count)
  217. continue;
  218. return map;
  219. }
  220. return NULL;
  221. }
  222. void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
  223. {
  224. if (!map)
  225. return;
  226. if (!refcount_dec_and_test(&map->users))
  227. return;
  228. if (map->pages && !use_ptemod) {
  229. /*
  230. * Increment the reference count. This ensures that the
  231. * subsequent call to unmap_grant_pages() will not wind up
  232. * re-entering itself. It *can* wind up calling
  233. * gntdev_put_map() recursively, but such calls will be with a
  234. * reference count greater than 1, so they will return before
  235. * this code is reached. The recursion depth is thus limited to
  236. * 1. Do NOT use refcount_inc() here, as it will detect that
  237. * the reference count is zero and WARN().
  238. */
  239. refcount_set(&map->users, 1);
  240. /*
  241. * Unmap the grants. This may or may not be asynchronous, so it
  242. * is possible that the reference count is 1 on return, but it
  243. * could also be greater than 1.
  244. */
  245. unmap_grant_pages(map, 0, map->count);
  246. /* Check if the memory now needs to be freed */
  247. if (!refcount_dec_and_test(&map->users))
  248. return;
  249. /*
  250. * All pages have been returned to the hypervisor, so free the
  251. * map.
  252. */
  253. }
  254. if (use_ptemod && map->notifier_init)
  255. mmu_interval_notifier_remove(&map->notifier);
  256. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  257. notify_remote_via_evtchn(map->notify.event);
  258. evtchn_put(map->notify.event);
  259. }
  260. gntdev_free_map(map);
  261. }
  262. /* ------------------------------------------------------------------ */
  263. static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
  264. {
  265. struct gntdev_grant_map *map = data;
  266. unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT;
  267. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
  268. (1 << _GNTMAP_guest_avail0);
  269. u64 pte_maddr;
  270. BUG_ON(pgnr >= map->count);
  271. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  272. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  273. map->grants[pgnr].ref,
  274. map->grants[pgnr].domid);
  275. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  276. INVALID_GRANT_HANDLE);
  277. return 0;
  278. }
  279. int gntdev_map_grant_pages(struct gntdev_grant_map *map)
  280. {
  281. size_t alloced = 0;
  282. int i, err = 0;
  283. if (!use_ptemod) {
  284. /* Note: it could already be mapped */
  285. if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
  286. return 0;
  287. for (i = 0; i < map->count; i++) {
  288. unsigned long addr = (unsigned long)
  289. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  290. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  291. map->grants[i].ref,
  292. map->grants[i].domid);
  293. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  294. map->flags, INVALID_GRANT_HANDLE);
  295. }
  296. } else {
  297. /*
  298. * Setup the map_ops corresponding to the pte entries pointing
  299. * to the kernel linear addresses of the struct pages.
  300. * These ptes are completely different from the user ptes dealt
  301. * with find_grant_ptes.
  302. * Note that GNTMAP_device_map isn't needed here: The
  303. * dev_bus_addr output field gets consumed only from ->map_ops,
  304. * and by not requesting it when mapping we also avoid needing
  305. * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
  306. * reference to the page in the hypervisor).
  307. */
  308. unsigned int flags = (map->flags & ~GNTMAP_device_map) |
  309. GNTMAP_host_map;
  310. for (i = 0; i < map->count; i++) {
  311. unsigned long address = (unsigned long)
  312. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  313. BUG_ON(PageHighMem(map->pages[i]));
  314. gnttab_set_map_op(&map->kmap_ops[i], address, flags,
  315. map->grants[i].ref,
  316. map->grants[i].domid);
  317. gnttab_set_unmap_op(&map->kunmap_ops[i], address,
  318. flags, INVALID_GRANT_HANDLE);
  319. }
  320. }
  321. pr_debug("map %d+%d\n", map->index, map->count);
  322. err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
  323. map->count);
  324. for (i = 0; i < map->count; i++) {
  325. if (map->map_ops[i].status == GNTST_okay) {
  326. map->unmap_ops[i].handle = map->map_ops[i].handle;
  327. alloced++;
  328. } else if (!err)
  329. err = -EINVAL;
  330. if (map->flags & GNTMAP_device_map)
  331. map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
  332. if (use_ptemod) {
  333. if (map->kmap_ops[i].status == GNTST_okay) {
  334. alloced++;
  335. map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
  336. } else if (!err)
  337. err = -EINVAL;
  338. }
  339. }
  340. atomic_add(alloced, &map->live_grants);
  341. return err;
  342. }
  343. static void __unmap_grant_pages_done(int result,
  344. struct gntab_unmap_queue_data *data)
  345. {
  346. unsigned int i;
  347. struct gntdev_grant_map *map = data->data;
  348. unsigned int offset = data->unmap_ops - map->unmap_ops;
  349. int successful_unmaps = 0;
  350. int live_grants;
  351. for (i = 0; i < data->count; i++) {
  352. if (map->unmap_ops[offset + i].status == GNTST_okay &&
  353. map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
  354. successful_unmaps++;
  355. WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay &&
  356. map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
  357. pr_debug("unmap handle=%d st=%d\n",
  358. map->unmap_ops[offset+i].handle,
  359. map->unmap_ops[offset+i].status);
  360. map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
  361. if (use_ptemod) {
  362. if (map->kunmap_ops[offset + i].status == GNTST_okay &&
  363. map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
  364. successful_unmaps++;
  365. WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay &&
  366. map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
  367. pr_debug("kunmap handle=%u st=%d\n",
  368. map->kunmap_ops[offset+i].handle,
  369. map->kunmap_ops[offset+i].status);
  370. map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
  371. }
  372. }
  373. /*
  374. * Decrease the live-grant counter. This must happen after the loop to
  375. * prevent premature reuse of the grants by gnttab_mmap().
  376. */
  377. live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
  378. if (WARN_ON(live_grants < 0))
  379. pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
  380. __func__, live_grants, successful_unmaps);
  381. /* Release reference taken by __unmap_grant_pages */
  382. gntdev_put_map(NULL, map);
  383. }
  384. static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
  385. int pages)
  386. {
  387. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  388. int pgno = (map->notify.addr >> PAGE_SHIFT);
  389. if (pgno >= offset && pgno < offset + pages) {
  390. /* No need for kmap, pages are in lowmem */
  391. uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
  392. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  393. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  394. }
  395. }
  396. map->unmap_data.unmap_ops = map->unmap_ops + offset;
  397. map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
  398. map->unmap_data.pages = map->pages + offset;
  399. map->unmap_data.count = pages;
  400. map->unmap_data.done = __unmap_grant_pages_done;
  401. map->unmap_data.data = map;
  402. refcount_inc(&map->users); /* to keep map alive during async call below */
  403. gnttab_unmap_refs_async(&map->unmap_data);
  404. }
  405. static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
  406. int pages)
  407. {
  408. int range;
  409. if (atomic_read(&map->live_grants) == 0)
  410. return; /* Nothing to do */
  411. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  412. /* It is possible the requested range will have a "hole" where we
  413. * already unmapped some of the grants. Only unmap valid ranges.
  414. */
  415. while (pages) {
  416. while (pages && map->being_removed[offset]) {
  417. offset++;
  418. pages--;
  419. }
  420. range = 0;
  421. while (range < pages) {
  422. if (map->being_removed[offset + range])
  423. break;
  424. map->being_removed[offset + range] = true;
  425. range++;
  426. }
  427. if (range)
  428. __unmap_grant_pages(map, offset, range);
  429. offset += range;
  430. pages -= range;
  431. }
  432. }
  433. /* ------------------------------------------------------------------ */
  434. static void gntdev_vma_open(struct vm_area_struct *vma)
  435. {
  436. struct gntdev_grant_map *map = vma->vm_private_data;
  437. pr_debug("gntdev_vma_open %p\n", vma);
  438. refcount_inc(&map->users);
  439. }
  440. static void gntdev_vma_close(struct vm_area_struct *vma)
  441. {
  442. struct gntdev_grant_map *map = vma->vm_private_data;
  443. struct file *file = vma->vm_file;
  444. struct gntdev_priv *priv = file->private_data;
  445. pr_debug("gntdev_vma_close %p\n", vma);
  446. vma->vm_private_data = NULL;
  447. gntdev_put_map(priv, map);
  448. }
  449. static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
  450. unsigned long addr)
  451. {
  452. struct gntdev_grant_map *map = vma->vm_private_data;
  453. return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
  454. }
  455. static const struct vm_operations_struct gntdev_vmops = {
  456. .open = gntdev_vma_open,
  457. .close = gntdev_vma_close,
  458. .find_special_page = gntdev_vma_find_special_page,
  459. };
  460. /* ------------------------------------------------------------------ */
  461. static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
  462. const struct mmu_notifier_range *range,
  463. unsigned long cur_seq)
  464. {
  465. struct gntdev_grant_map *map =
  466. container_of(mn, struct gntdev_grant_map, notifier);
  467. unsigned long mstart, mend;
  468. unsigned long map_start, map_end;
  469. if (!mmu_notifier_range_blockable(range))
  470. return false;
  471. map_start = map->pages_vm_start;
  472. map_end = map->pages_vm_start + (map->count << PAGE_SHIFT);
  473. /*
  474. * If the VMA is split or otherwise changed the notifier is not
  475. * updated, but we don't want to process VA's outside the modified
  476. * VMA. FIXME: It would be much more understandable to just prevent
  477. * modifying the VMA in the first place.
  478. */
  479. if (map_start >= range->end || map_end <= range->start)
  480. return true;
  481. mstart = max(range->start, map_start);
  482. mend = min(range->end, map_end);
  483. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  484. map->index, map->count, map_start, map_end,
  485. range->start, range->end, mstart, mend);
  486. unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT,
  487. (mend - mstart) >> PAGE_SHIFT);
  488. return true;
  489. }
  490. static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
  491. .invalidate = gntdev_invalidate,
  492. };
  493. /* ------------------------------------------------------------------ */
  494. static int gntdev_open(struct inode *inode, struct file *flip)
  495. {
  496. struct gntdev_priv *priv;
  497. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  498. if (!priv)
  499. return -ENOMEM;
  500. INIT_LIST_HEAD(&priv->maps);
  501. mutex_init(&priv->lock);
  502. mutex_init(&priv->batch_lock);
  503. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  504. priv->dmabuf_priv = gntdev_dmabuf_init(flip);
  505. if (IS_ERR(priv->dmabuf_priv)) {
  506. int ret = PTR_ERR(priv->dmabuf_priv);
  507. kfree(priv);
  508. return ret;
  509. }
  510. #endif
  511. flip->private_data = priv;
  512. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  513. priv->dma_dev = gntdev_miscdev.this_device;
  514. dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
  515. #endif
  516. pr_debug("priv %p\n", priv);
  517. return 0;
  518. }
  519. static int gntdev_release(struct inode *inode, struct file *flip)
  520. {
  521. struct gntdev_priv *priv = flip->private_data;
  522. struct gntdev_grant_map *map;
  523. struct gntdev_copy_batch *batch;
  524. pr_debug("priv %p\n", priv);
  525. mutex_lock(&priv->lock);
  526. while (!list_empty(&priv->maps)) {
  527. map = list_entry(priv->maps.next,
  528. struct gntdev_grant_map, next);
  529. list_del(&map->next);
  530. gntdev_put_map(NULL /* already removed */, map);
  531. }
  532. mutex_unlock(&priv->lock);
  533. mutex_lock(&priv->batch_lock);
  534. while (priv->batch) {
  535. batch = priv->batch;
  536. priv->batch = batch->next;
  537. kfree(batch);
  538. }
  539. mutex_unlock(&priv->batch_lock);
  540. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  541. gntdev_dmabuf_fini(priv->dmabuf_priv);
  542. #endif
  543. kfree(priv);
  544. return 0;
  545. }
  546. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  547. struct ioctl_gntdev_map_grant_ref __user *u)
  548. {
  549. struct ioctl_gntdev_map_grant_ref op;
  550. struct gntdev_grant_map *map;
  551. int err;
  552. if (copy_from_user(&op, u, sizeof(op)) != 0)
  553. return -EFAULT;
  554. pr_debug("priv %p, add %d\n", priv, op.count);
  555. if (unlikely(gntdev_test_page_count(op.count)))
  556. return -EINVAL;
  557. err = -ENOMEM;
  558. map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
  559. if (!map)
  560. return err;
  561. if (copy_from_user(map->grants, &u->refs,
  562. sizeof(map->grants[0]) * op.count) != 0) {
  563. gntdev_put_map(NULL, map);
  564. return -EFAULT;
  565. }
  566. mutex_lock(&priv->lock);
  567. gntdev_add_map(priv, map);
  568. op.index = map->index << PAGE_SHIFT;
  569. mutex_unlock(&priv->lock);
  570. if (copy_to_user(u, &op, sizeof(op)) != 0)
  571. return -EFAULT;
  572. return 0;
  573. }
  574. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  575. struct ioctl_gntdev_unmap_grant_ref __user *u)
  576. {
  577. struct ioctl_gntdev_unmap_grant_ref op;
  578. struct gntdev_grant_map *map;
  579. int err = -ENOENT;
  580. if (copy_from_user(&op, u, sizeof(op)) != 0)
  581. return -EFAULT;
  582. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  583. mutex_lock(&priv->lock);
  584. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  585. if (map) {
  586. list_del(&map->next);
  587. err = 0;
  588. }
  589. mutex_unlock(&priv->lock);
  590. if (map)
  591. gntdev_put_map(priv, map);
  592. return err;
  593. }
  594. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  595. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  596. {
  597. struct ioctl_gntdev_get_offset_for_vaddr op;
  598. struct vm_area_struct *vma;
  599. struct gntdev_grant_map *map;
  600. int rv = -EINVAL;
  601. if (copy_from_user(&op, u, sizeof(op)) != 0)
  602. return -EFAULT;
  603. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  604. mmap_read_lock(current->mm);
  605. vma = find_vma(current->mm, op.vaddr);
  606. if (!vma || vma->vm_ops != &gntdev_vmops)
  607. goto out_unlock;
  608. map = vma->vm_private_data;
  609. if (!map)
  610. goto out_unlock;
  611. op.offset = map->index << PAGE_SHIFT;
  612. op.count = map->count;
  613. rv = 0;
  614. out_unlock:
  615. mmap_read_unlock(current->mm);
  616. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  617. return -EFAULT;
  618. return rv;
  619. }
  620. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  621. {
  622. struct ioctl_gntdev_unmap_notify op;
  623. struct gntdev_grant_map *map;
  624. int rc;
  625. int out_flags;
  626. evtchn_port_t out_event;
  627. if (copy_from_user(&op, u, sizeof(op)))
  628. return -EFAULT;
  629. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  630. return -EINVAL;
  631. /* We need to grab a reference to the event channel we are going to use
  632. * to send the notify before releasing the reference we may already have
  633. * (if someone has called this ioctl twice). This is required so that
  634. * it is possible to change the clear_byte part of the notification
  635. * without disturbing the event channel part, which may now be the last
  636. * reference to that event channel.
  637. */
  638. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  639. if (evtchn_get(op.event_channel_port))
  640. return -EINVAL;
  641. }
  642. out_flags = op.action;
  643. out_event = op.event_channel_port;
  644. mutex_lock(&priv->lock);
  645. list_for_each_entry(map, &priv->maps, next) {
  646. uint64_t begin = map->index << PAGE_SHIFT;
  647. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  648. if (op.index >= begin && op.index < end)
  649. goto found;
  650. }
  651. rc = -ENOENT;
  652. goto unlock_out;
  653. found:
  654. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  655. (map->flags & GNTMAP_readonly)) {
  656. rc = -EINVAL;
  657. goto unlock_out;
  658. }
  659. out_flags = map->notify.flags;
  660. out_event = map->notify.event;
  661. map->notify.flags = op.action;
  662. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  663. map->notify.event = op.event_channel_port;
  664. rc = 0;
  665. unlock_out:
  666. mutex_unlock(&priv->lock);
  667. /* Drop the reference to the event channel we did not save in the map */
  668. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  669. evtchn_put(out_event);
  670. return rc;
  671. }
  672. static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
  673. unsigned long *gfn)
  674. {
  675. unsigned long addr = (unsigned long)virt;
  676. struct page *page;
  677. unsigned long xen_pfn;
  678. int ret;
  679. ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
  680. if (ret < 0)
  681. return ret;
  682. batch->pages[batch->nr_pages++] = page;
  683. xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
  684. *gfn = pfn_to_gfn(xen_pfn);
  685. return 0;
  686. }
  687. static void gntdev_put_pages(struct gntdev_copy_batch *batch)
  688. {
  689. unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
  690. batch->nr_pages = 0;
  691. batch->writeable = false;
  692. }
  693. static int gntdev_copy(struct gntdev_copy_batch *batch)
  694. {
  695. unsigned int i;
  696. gnttab_batch_copy(batch->ops, batch->nr_ops);
  697. gntdev_put_pages(batch);
  698. /*
  699. * For each completed op, update the status if the op failed
  700. * and all previous ops for the segment were successful.
  701. */
  702. for (i = 0; i < batch->nr_ops; i++) {
  703. s16 status = batch->ops[i].status;
  704. s16 old_status;
  705. if (status == GNTST_okay)
  706. continue;
  707. if (__get_user(old_status, batch->status[i]))
  708. return -EFAULT;
  709. if (old_status != GNTST_okay)
  710. continue;
  711. if (__put_user(status, batch->status[i]))
  712. return -EFAULT;
  713. }
  714. batch->nr_ops = 0;
  715. return 0;
  716. }
  717. static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
  718. struct gntdev_grant_copy_segment *seg,
  719. s16 __user *status)
  720. {
  721. uint16_t copied = 0;
  722. /*
  723. * Disallow local -> local copies since there is only space in
  724. * batch->pages for one page per-op and this would be a very
  725. * expensive memcpy().
  726. */
  727. if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
  728. return -EINVAL;
  729. /* Can't cross page if source/dest is a grant ref. */
  730. if (seg->flags & GNTCOPY_source_gref) {
  731. if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
  732. return -EINVAL;
  733. }
  734. if (seg->flags & GNTCOPY_dest_gref) {
  735. if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
  736. return -EINVAL;
  737. }
  738. if (put_user(GNTST_okay, status))
  739. return -EFAULT;
  740. while (copied < seg->len) {
  741. struct gnttab_copy *op;
  742. void __user *virt;
  743. size_t len, off;
  744. unsigned long gfn;
  745. int ret;
  746. if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
  747. ret = gntdev_copy(batch);
  748. if (ret < 0)
  749. return ret;
  750. }
  751. len = seg->len - copied;
  752. op = &batch->ops[batch->nr_ops];
  753. op->flags = 0;
  754. if (seg->flags & GNTCOPY_source_gref) {
  755. op->source.u.ref = seg->source.foreign.ref;
  756. op->source.domid = seg->source.foreign.domid;
  757. op->source.offset = seg->source.foreign.offset + copied;
  758. op->flags |= GNTCOPY_source_gref;
  759. } else {
  760. virt = seg->source.virt + copied;
  761. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  762. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  763. batch->writeable = false;
  764. ret = gntdev_get_page(batch, virt, &gfn);
  765. if (ret < 0)
  766. return ret;
  767. op->source.u.gmfn = gfn;
  768. op->source.domid = DOMID_SELF;
  769. op->source.offset = off;
  770. }
  771. if (seg->flags & GNTCOPY_dest_gref) {
  772. op->dest.u.ref = seg->dest.foreign.ref;
  773. op->dest.domid = seg->dest.foreign.domid;
  774. op->dest.offset = seg->dest.foreign.offset + copied;
  775. op->flags |= GNTCOPY_dest_gref;
  776. } else {
  777. virt = seg->dest.virt + copied;
  778. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  779. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  780. batch->writeable = true;
  781. ret = gntdev_get_page(batch, virt, &gfn);
  782. if (ret < 0)
  783. return ret;
  784. op->dest.u.gmfn = gfn;
  785. op->dest.domid = DOMID_SELF;
  786. op->dest.offset = off;
  787. }
  788. op->len = len;
  789. copied += len;
  790. batch->status[batch->nr_ops] = status;
  791. batch->nr_ops++;
  792. }
  793. return 0;
  794. }
  795. static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
  796. {
  797. struct ioctl_gntdev_grant_copy copy;
  798. struct gntdev_copy_batch *batch;
  799. unsigned int i;
  800. int ret = 0;
  801. if (copy_from_user(&copy, u, sizeof(copy)))
  802. return -EFAULT;
  803. mutex_lock(&priv->batch_lock);
  804. if (!priv->batch) {
  805. batch = kmalloc(sizeof(*batch), GFP_KERNEL);
  806. } else {
  807. batch = priv->batch;
  808. priv->batch = batch->next;
  809. }
  810. mutex_unlock(&priv->batch_lock);
  811. if (!batch)
  812. return -ENOMEM;
  813. batch->nr_ops = 0;
  814. batch->nr_pages = 0;
  815. for (i = 0; i < copy.count; i++) {
  816. struct gntdev_grant_copy_segment seg;
  817. if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
  818. ret = -EFAULT;
  819. gntdev_put_pages(batch);
  820. goto out;
  821. }
  822. ret = gntdev_grant_copy_seg(batch, &seg, &copy.segments[i].status);
  823. if (ret < 0) {
  824. gntdev_put_pages(batch);
  825. goto out;
  826. }
  827. cond_resched();
  828. }
  829. if (batch->nr_ops)
  830. ret = gntdev_copy(batch);
  831. out:
  832. mutex_lock(&priv->batch_lock);
  833. batch->next = priv->batch;
  834. priv->batch = batch;
  835. mutex_unlock(&priv->batch_lock);
  836. return ret;
  837. }
  838. static long gntdev_ioctl(struct file *flip,
  839. unsigned int cmd, unsigned long arg)
  840. {
  841. struct gntdev_priv *priv = flip->private_data;
  842. void __user *ptr = (void __user *)arg;
  843. switch (cmd) {
  844. case IOCTL_GNTDEV_MAP_GRANT_REF:
  845. return gntdev_ioctl_map_grant_ref(priv, ptr);
  846. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  847. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  848. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  849. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  850. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  851. return gntdev_ioctl_notify(priv, ptr);
  852. case IOCTL_GNTDEV_GRANT_COPY:
  853. return gntdev_ioctl_grant_copy(priv, ptr);
  854. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  855. case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
  856. return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
  857. case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
  858. return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
  859. case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
  860. return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
  861. case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
  862. return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
  863. #endif
  864. default:
  865. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  866. return -ENOIOCTLCMD;
  867. }
  868. return 0;
  869. }
  870. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  871. {
  872. struct gntdev_priv *priv = flip->private_data;
  873. int index = vma->vm_pgoff;
  874. int count = vma_pages(vma);
  875. struct gntdev_grant_map *map;
  876. int err = -EINVAL;
  877. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  878. return -EINVAL;
  879. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  880. index, count, vma->vm_start, vma->vm_pgoff);
  881. mutex_lock(&priv->lock);
  882. map = gntdev_find_map_index(priv, index, count);
  883. if (!map)
  884. goto unlock_out;
  885. if (!atomic_add_unless(&map->in_use, 1, 1))
  886. goto unlock_out;
  887. refcount_inc(&map->users);
  888. vma->vm_ops = &gntdev_vmops;
  889. vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP);
  890. if (use_ptemod)
  891. vm_flags_set(vma, VM_DONTCOPY);
  892. vma->vm_private_data = map;
  893. if (map->flags) {
  894. if ((vma->vm_flags & VM_WRITE) &&
  895. (map->flags & GNTMAP_readonly))
  896. goto out_unlock_put;
  897. } else {
  898. map->flags = GNTMAP_host_map;
  899. if (!(vma->vm_flags & VM_WRITE))
  900. map->flags |= GNTMAP_readonly;
  901. }
  902. map->pages_vm_start = vma->vm_start;
  903. if (use_ptemod) {
  904. err = mmu_interval_notifier_insert_locked(
  905. &map->notifier, vma->vm_mm, vma->vm_start,
  906. vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
  907. if (err)
  908. goto out_unlock_put;
  909. map->notifier_init = true;
  910. }
  911. mutex_unlock(&priv->lock);
  912. if (use_ptemod) {
  913. /*
  914. * gntdev takes the address of the PTE in find_grant_ptes() and
  915. * passes it to the hypervisor in gntdev_map_grant_pages(). The
  916. * purpose of the notifier is to prevent the hypervisor pointer
  917. * to the PTE from going stale.
  918. *
  919. * Since this vma's mappings can't be touched without the
  920. * mmap_lock, and we are holding it now, there is no need for
  921. * the notifier_range locking pattern.
  922. */
  923. mmu_interval_read_begin(&map->notifier);
  924. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  925. vma->vm_end - vma->vm_start,
  926. find_grant_ptes, map);
  927. if (err) {
  928. pr_warn("find_grant_ptes() failure.\n");
  929. goto out_put_map;
  930. }
  931. }
  932. err = gntdev_map_grant_pages(map);
  933. if (err)
  934. goto out_put_map;
  935. if (!use_ptemod) {
  936. err = vm_map_pages_zero(vma, map->pages, map->count);
  937. if (err)
  938. goto out_put_map;
  939. }
  940. return 0;
  941. unlock_out:
  942. mutex_unlock(&priv->lock);
  943. return err;
  944. out_unlock_put:
  945. mutex_unlock(&priv->lock);
  946. out_put_map:
  947. if (use_ptemod)
  948. unmap_grant_pages(map, 0, map->count);
  949. gntdev_put_map(priv, map);
  950. return err;
  951. }
  952. static const struct file_operations gntdev_fops = {
  953. .owner = THIS_MODULE,
  954. .open = gntdev_open,
  955. .release = gntdev_release,
  956. .mmap = gntdev_mmap,
  957. .unlocked_ioctl = gntdev_ioctl
  958. };
  959. static struct miscdevice gntdev_miscdev = {
  960. .minor = MISC_DYNAMIC_MINOR,
  961. .name = "xen/gntdev",
  962. .fops = &gntdev_fops,
  963. };
  964. /* ------------------------------------------------------------------ */
  965. static int __init gntdev_init(void)
  966. {
  967. int err;
  968. if (!xen_domain())
  969. return -ENODEV;
  970. use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
  971. err = misc_register(&gntdev_miscdev);
  972. if (err != 0) {
  973. pr_err("Could not register gntdev device\n");
  974. return err;
  975. }
  976. return 0;
  977. }
  978. static void __exit gntdev_exit(void)
  979. {
  980. misc_deregister(&gntdev_miscdev);
  981. }
  982. module_init(gntdev_init);
  983. module_exit(gntdev_exit);
  984. /* ------------------------------------------------------------------ */