gntdev.c 27 KB

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