gntdev.c 29 KB

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