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. range--;
  329. break;
  330. }
  331. range++;
  332. }
  333. err = __unmap_grant_pages(map, offset, range);
  334. offset += range;
  335. pages -= range;
  336. }
  337. return err;
  338. }
  339. /* ------------------------------------------------------------------ */
  340. static void gntdev_vma_open(struct vm_area_struct *vma)
  341. {
  342. struct grant_map *map = vma->vm_private_data;
  343. pr_debug("gntdev_vma_open %p\n", vma);
  344. refcount_inc(&map->users);
  345. }
  346. static void gntdev_vma_close(struct vm_area_struct *vma)
  347. {
  348. struct grant_map *map = vma->vm_private_data;
  349. struct file *file = vma->vm_file;
  350. struct gntdev_priv *priv = file->private_data;
  351. pr_debug("gntdev_vma_close %p\n", vma);
  352. if (use_ptemod) {
  353. /* It is possible that an mmu notifier could be running
  354. * concurrently, so take priv->lock to ensure that the vma won't
  355. * vanishing during the unmap_grant_pages call, since we will
  356. * spin here until that completes. Such a concurrent call will
  357. * not do any unmapping, since that has been done prior to
  358. * closing the vma, but it may still iterate the unmap_ops list.
  359. */
  360. mutex_lock(&priv->lock);
  361. map->vma = NULL;
  362. mutex_unlock(&priv->lock);
  363. }
  364. vma->vm_private_data = NULL;
  365. gntdev_put_map(priv, map);
  366. }
  367. static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
  368. unsigned long addr)
  369. {
  370. struct grant_map *map = vma->vm_private_data;
  371. return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
  372. }
  373. static const struct vm_operations_struct gntdev_vmops = {
  374. .open = gntdev_vma_open,
  375. .close = gntdev_vma_close,
  376. .find_special_page = gntdev_vma_find_special_page,
  377. };
  378. /* ------------------------------------------------------------------ */
  379. static void unmap_if_in_range(struct grant_map *map,
  380. unsigned long start, unsigned long end)
  381. {
  382. unsigned long mstart, mend;
  383. int err;
  384. if (!map->vma)
  385. return;
  386. if (map->vma->vm_start >= end)
  387. return;
  388. if (map->vma->vm_end <= start)
  389. return;
  390. mstart = max(start, map->vma->vm_start);
  391. mend = min(end, map->vma->vm_end);
  392. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  393. map->index, map->count,
  394. map->vma->vm_start, map->vma->vm_end,
  395. start, end, mstart, mend);
  396. err = unmap_grant_pages(map,
  397. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  398. (mend - mstart) >> PAGE_SHIFT);
  399. WARN_ON(err);
  400. }
  401. static void mn_invl_range_start(struct mmu_notifier *mn,
  402. struct mm_struct *mm,
  403. unsigned long start, unsigned long end)
  404. {
  405. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  406. struct grant_map *map;
  407. mutex_lock(&priv->lock);
  408. list_for_each_entry(map, &priv->maps, next) {
  409. unmap_if_in_range(map, start, end);
  410. }
  411. list_for_each_entry(map, &priv->freeable_maps, next) {
  412. unmap_if_in_range(map, start, end);
  413. }
  414. mutex_unlock(&priv->lock);
  415. }
  416. static void mn_release(struct mmu_notifier *mn,
  417. struct mm_struct *mm)
  418. {
  419. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  420. struct grant_map *map;
  421. int err;
  422. mutex_lock(&priv->lock);
  423. list_for_each_entry(map, &priv->maps, next) {
  424. if (!map->vma)
  425. continue;
  426. pr_debug("map %d+%d (%lx %lx)\n",
  427. map->index, map->count,
  428. map->vma->vm_start, map->vma->vm_end);
  429. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  430. WARN_ON(err);
  431. }
  432. list_for_each_entry(map, &priv->freeable_maps, next) {
  433. if (!map->vma)
  434. continue;
  435. pr_debug("map %d+%d (%lx %lx)\n",
  436. map->index, map->count,
  437. map->vma->vm_start, map->vma->vm_end);
  438. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  439. WARN_ON(err);
  440. }
  441. mutex_unlock(&priv->lock);
  442. }
  443. static const struct mmu_notifier_ops gntdev_mmu_ops = {
  444. .release = mn_release,
  445. .invalidate_range_start = mn_invl_range_start,
  446. };
  447. /* ------------------------------------------------------------------ */
  448. static int gntdev_open(struct inode *inode, struct file *flip)
  449. {
  450. struct gntdev_priv *priv;
  451. int ret = 0;
  452. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  453. if (!priv)
  454. return -ENOMEM;
  455. INIT_LIST_HEAD(&priv->maps);
  456. INIT_LIST_HEAD(&priv->freeable_maps);
  457. mutex_init(&priv->lock);
  458. if (use_ptemod) {
  459. priv->mm = get_task_mm(current);
  460. if (!priv->mm) {
  461. kfree(priv);
  462. return -ENOMEM;
  463. }
  464. priv->mn.ops = &gntdev_mmu_ops;
  465. ret = mmu_notifier_register(&priv->mn, priv->mm);
  466. mmput(priv->mm);
  467. }
  468. if (ret) {
  469. kfree(priv);
  470. return ret;
  471. }
  472. flip->private_data = priv;
  473. pr_debug("priv %p\n", priv);
  474. return 0;
  475. }
  476. static int gntdev_release(struct inode *inode, struct file *flip)
  477. {
  478. struct gntdev_priv *priv = flip->private_data;
  479. struct grant_map *map;
  480. pr_debug("priv %p\n", priv);
  481. mutex_lock(&priv->lock);
  482. while (!list_empty(&priv->maps)) {
  483. map = list_entry(priv->maps.next, struct grant_map, next);
  484. list_del(&map->next);
  485. gntdev_put_map(NULL /* already removed */, map);
  486. }
  487. WARN_ON(!list_empty(&priv->freeable_maps));
  488. mutex_unlock(&priv->lock);
  489. if (use_ptemod)
  490. mmu_notifier_unregister(&priv->mn, priv->mm);
  491. kfree(priv);
  492. return 0;
  493. }
  494. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  495. struct ioctl_gntdev_map_grant_ref __user *u)
  496. {
  497. struct ioctl_gntdev_map_grant_ref op;
  498. struct grant_map *map;
  499. int err;
  500. if (copy_from_user(&op, u, sizeof(op)) != 0)
  501. return -EFAULT;
  502. pr_debug("priv %p, add %d\n", priv, op.count);
  503. if (unlikely(op.count <= 0))
  504. return -EINVAL;
  505. err = -ENOMEM;
  506. map = gntdev_alloc_map(priv, op.count);
  507. if (!map)
  508. return err;
  509. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  510. pr_debug("can't map: over limit\n");
  511. gntdev_put_map(NULL, map);
  512. return err;
  513. }
  514. if (copy_from_user(map->grants, &u->refs,
  515. sizeof(map->grants[0]) * op.count) != 0) {
  516. gntdev_put_map(NULL, map);
  517. return -EFAULT;
  518. }
  519. mutex_lock(&priv->lock);
  520. gntdev_add_map(priv, map);
  521. op.index = map->index << PAGE_SHIFT;
  522. mutex_unlock(&priv->lock);
  523. if (copy_to_user(u, &op, sizeof(op)) != 0)
  524. return -EFAULT;
  525. return 0;
  526. }
  527. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  528. struct ioctl_gntdev_unmap_grant_ref __user *u)
  529. {
  530. struct ioctl_gntdev_unmap_grant_ref op;
  531. struct grant_map *map;
  532. int err = -ENOENT;
  533. if (copy_from_user(&op, u, sizeof(op)) != 0)
  534. return -EFAULT;
  535. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  536. mutex_lock(&priv->lock);
  537. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  538. if (map) {
  539. list_del(&map->next);
  540. if (populate_freeable_maps)
  541. list_add_tail(&map->next, &priv->freeable_maps);
  542. err = 0;
  543. }
  544. mutex_unlock(&priv->lock);
  545. if (map)
  546. gntdev_put_map(priv, map);
  547. return err;
  548. }
  549. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  550. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  551. {
  552. struct ioctl_gntdev_get_offset_for_vaddr op;
  553. struct vm_area_struct *vma;
  554. struct grant_map *map;
  555. int rv = -EINVAL;
  556. if (copy_from_user(&op, u, sizeof(op)) != 0)
  557. return -EFAULT;
  558. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  559. down_read(&current->mm->mmap_sem);
  560. vma = find_vma(current->mm, op.vaddr);
  561. if (!vma || vma->vm_ops != &gntdev_vmops)
  562. goto out_unlock;
  563. map = vma->vm_private_data;
  564. if (!map)
  565. goto out_unlock;
  566. op.offset = map->index << PAGE_SHIFT;
  567. op.count = map->count;
  568. rv = 0;
  569. out_unlock:
  570. up_read(&current->mm->mmap_sem);
  571. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  572. return -EFAULT;
  573. return rv;
  574. }
  575. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  576. {
  577. struct ioctl_gntdev_unmap_notify op;
  578. struct grant_map *map;
  579. int rc;
  580. int out_flags;
  581. unsigned int out_event;
  582. if (copy_from_user(&op, u, sizeof(op)))
  583. return -EFAULT;
  584. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  585. return -EINVAL;
  586. /* We need to grab a reference to the event channel we are going to use
  587. * to send the notify before releasing the reference we may already have
  588. * (if someone has called this ioctl twice). This is required so that
  589. * it is possible to change the clear_byte part of the notification
  590. * without disturbing the event channel part, which may now be the last
  591. * reference to that event channel.
  592. */
  593. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  594. if (evtchn_get(op.event_channel_port))
  595. return -EINVAL;
  596. }
  597. out_flags = op.action;
  598. out_event = op.event_channel_port;
  599. mutex_lock(&priv->lock);
  600. list_for_each_entry(map, &priv->maps, next) {
  601. uint64_t begin = map->index << PAGE_SHIFT;
  602. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  603. if (op.index >= begin && op.index < end)
  604. goto found;
  605. }
  606. rc = -ENOENT;
  607. goto unlock_out;
  608. found:
  609. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  610. (map->flags & GNTMAP_readonly)) {
  611. rc = -EINVAL;
  612. goto unlock_out;
  613. }
  614. out_flags = map->notify.flags;
  615. out_event = map->notify.event;
  616. map->notify.flags = op.action;
  617. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  618. map->notify.event = op.event_channel_port;
  619. rc = 0;
  620. unlock_out:
  621. mutex_unlock(&priv->lock);
  622. /* Drop the reference to the event channel we did not save in the map */
  623. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  624. evtchn_put(out_event);
  625. return rc;
  626. }
  627. #define GNTDEV_COPY_BATCH 16
  628. struct gntdev_copy_batch {
  629. struct gnttab_copy ops[GNTDEV_COPY_BATCH];
  630. struct page *pages[GNTDEV_COPY_BATCH];
  631. s16 __user *status[GNTDEV_COPY_BATCH];
  632. unsigned int nr_ops;
  633. unsigned int nr_pages;
  634. };
  635. static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
  636. bool writeable, unsigned long *gfn)
  637. {
  638. unsigned long addr = (unsigned long)virt;
  639. struct page *page;
  640. unsigned long xen_pfn;
  641. int ret;
  642. ret = get_user_pages_fast(addr, 1, writeable, &page);
  643. if (ret < 0)
  644. return ret;
  645. batch->pages[batch->nr_pages++] = page;
  646. xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
  647. *gfn = pfn_to_gfn(xen_pfn);
  648. return 0;
  649. }
  650. static void gntdev_put_pages(struct gntdev_copy_batch *batch)
  651. {
  652. unsigned int i;
  653. for (i = 0; i < batch->nr_pages; i++)
  654. put_page(batch->pages[i]);
  655. batch->nr_pages = 0;
  656. }
  657. static int gntdev_copy(struct gntdev_copy_batch *batch)
  658. {
  659. unsigned int i;
  660. gnttab_batch_copy(batch->ops, batch->nr_ops);
  661. gntdev_put_pages(batch);
  662. /*
  663. * For each completed op, update the status if the op failed
  664. * and all previous ops for the segment were successful.
  665. */
  666. for (i = 0; i < batch->nr_ops; i++) {
  667. s16 status = batch->ops[i].status;
  668. s16 old_status;
  669. if (status == GNTST_okay)
  670. continue;
  671. if (__get_user(old_status, batch->status[i]))
  672. return -EFAULT;
  673. if (old_status != GNTST_okay)
  674. continue;
  675. if (__put_user(status, batch->status[i]))
  676. return -EFAULT;
  677. }
  678. batch->nr_ops = 0;
  679. return 0;
  680. }
  681. static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
  682. struct gntdev_grant_copy_segment *seg,
  683. s16 __user *status)
  684. {
  685. uint16_t copied = 0;
  686. /*
  687. * Disallow local -> local copies since there is only space in
  688. * batch->pages for one page per-op and this would be a very
  689. * expensive memcpy().
  690. */
  691. if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
  692. return -EINVAL;
  693. /* Can't cross page if source/dest is a grant ref. */
  694. if (seg->flags & GNTCOPY_source_gref) {
  695. if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
  696. return -EINVAL;
  697. }
  698. if (seg->flags & GNTCOPY_dest_gref) {
  699. if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
  700. return -EINVAL;
  701. }
  702. if (put_user(GNTST_okay, status))
  703. return -EFAULT;
  704. while (copied < seg->len) {
  705. struct gnttab_copy *op;
  706. void __user *virt;
  707. size_t len, off;
  708. unsigned long gfn;
  709. int ret;
  710. if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
  711. ret = gntdev_copy(batch);
  712. if (ret < 0)
  713. return ret;
  714. }
  715. len = seg->len - copied;
  716. op = &batch->ops[batch->nr_ops];
  717. op->flags = 0;
  718. if (seg->flags & GNTCOPY_source_gref) {
  719. op->source.u.ref = seg->source.foreign.ref;
  720. op->source.domid = seg->source.foreign.domid;
  721. op->source.offset = seg->source.foreign.offset + copied;
  722. op->flags |= GNTCOPY_source_gref;
  723. } else {
  724. virt = seg->source.virt + copied;
  725. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  726. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  727. ret = gntdev_get_page(batch, virt, false, &gfn);
  728. if (ret < 0)
  729. return ret;
  730. op->source.u.gmfn = gfn;
  731. op->source.domid = DOMID_SELF;
  732. op->source.offset = off;
  733. }
  734. if (seg->flags & GNTCOPY_dest_gref) {
  735. op->dest.u.ref = seg->dest.foreign.ref;
  736. op->dest.domid = seg->dest.foreign.domid;
  737. op->dest.offset = seg->dest.foreign.offset + copied;
  738. op->flags |= GNTCOPY_dest_gref;
  739. } else {
  740. virt = seg->dest.virt + copied;
  741. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  742. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  743. ret = gntdev_get_page(batch, virt, true, &gfn);
  744. if (ret < 0)
  745. return ret;
  746. op->dest.u.gmfn = gfn;
  747. op->dest.domid = DOMID_SELF;
  748. op->dest.offset = off;
  749. }
  750. op->len = len;
  751. copied += len;
  752. batch->status[batch->nr_ops] = status;
  753. batch->nr_ops++;
  754. }
  755. return 0;
  756. }
  757. static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
  758. {
  759. struct ioctl_gntdev_grant_copy copy;
  760. struct gntdev_copy_batch batch;
  761. unsigned int i;
  762. int ret = 0;
  763. if (copy_from_user(&copy, u, sizeof(copy)))
  764. return -EFAULT;
  765. batch.nr_ops = 0;
  766. batch.nr_pages = 0;
  767. for (i = 0; i < copy.count; i++) {
  768. struct gntdev_grant_copy_segment seg;
  769. if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
  770. ret = -EFAULT;
  771. goto out;
  772. }
  773. ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
  774. if (ret < 0)
  775. goto out;
  776. cond_resched();
  777. }
  778. if (batch.nr_ops)
  779. ret = gntdev_copy(&batch);
  780. return ret;
  781. out:
  782. gntdev_put_pages(&batch);
  783. return ret;
  784. }
  785. static long gntdev_ioctl(struct file *flip,
  786. unsigned int cmd, unsigned long arg)
  787. {
  788. struct gntdev_priv *priv = flip->private_data;
  789. void __user *ptr = (void __user *)arg;
  790. switch (cmd) {
  791. case IOCTL_GNTDEV_MAP_GRANT_REF:
  792. return gntdev_ioctl_map_grant_ref(priv, ptr);
  793. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  794. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  795. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  796. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  797. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  798. return gntdev_ioctl_notify(priv, ptr);
  799. case IOCTL_GNTDEV_GRANT_COPY:
  800. return gntdev_ioctl_grant_copy(priv, ptr);
  801. default:
  802. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  803. return -ENOIOCTLCMD;
  804. }
  805. return 0;
  806. }
  807. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  808. {
  809. struct gntdev_priv *priv = flip->private_data;
  810. int index = vma->vm_pgoff;
  811. int count = vma_pages(vma);
  812. struct grant_map *map;
  813. int i, err = -EINVAL;
  814. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  815. return -EINVAL;
  816. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  817. index, count, vma->vm_start, vma->vm_pgoff);
  818. mutex_lock(&priv->lock);
  819. map = gntdev_find_map_index(priv, index, count);
  820. if (!map)
  821. goto unlock_out;
  822. if (use_ptemod && map->vma)
  823. goto unlock_out;
  824. if (use_ptemod && priv->mm != vma->vm_mm) {
  825. pr_warn("Huh? Other mm?\n");
  826. goto unlock_out;
  827. }
  828. refcount_inc(&map->users);
  829. vma->vm_ops = &gntdev_vmops;
  830. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
  831. if (use_ptemod)
  832. vma->vm_flags |= VM_DONTCOPY;
  833. vma->vm_private_data = map;
  834. if (use_ptemod)
  835. map->vma = vma;
  836. if (map->flags) {
  837. if ((vma->vm_flags & VM_WRITE) &&
  838. (map->flags & GNTMAP_readonly))
  839. goto out_unlock_put;
  840. } else {
  841. map->flags = GNTMAP_host_map;
  842. if (!(vma->vm_flags & VM_WRITE))
  843. map->flags |= GNTMAP_readonly;
  844. }
  845. mutex_unlock(&priv->lock);
  846. if (use_ptemod) {
  847. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  848. vma->vm_end - vma->vm_start,
  849. find_grant_ptes, map);
  850. if (err) {
  851. pr_warn("find_grant_ptes() failure.\n");
  852. goto out_put_map;
  853. }
  854. }
  855. err = map_grant_pages(map);
  856. if (err)
  857. goto out_put_map;
  858. if (!use_ptemod) {
  859. for (i = 0; i < count; i++) {
  860. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  861. map->pages[i]);
  862. if (err)
  863. goto out_put_map;
  864. }
  865. } else {
  866. #ifdef CONFIG_X86
  867. /*
  868. * If the PTEs were not made special by the grant map
  869. * hypercall, do so here.
  870. *
  871. * This is racy since the mapping is already visible
  872. * to userspace but userspace should be well-behaved
  873. * enough to not touch it until the mmap() call
  874. * returns.
  875. */
  876. if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
  877. apply_to_page_range(vma->vm_mm, vma->vm_start,
  878. vma->vm_end - vma->vm_start,
  879. set_grant_ptes_as_special, NULL);
  880. }
  881. #endif
  882. map->pages_vm_start = vma->vm_start;
  883. }
  884. return 0;
  885. unlock_out:
  886. mutex_unlock(&priv->lock);
  887. return err;
  888. out_unlock_put:
  889. mutex_unlock(&priv->lock);
  890. out_put_map:
  891. if (use_ptemod)
  892. map->vma = NULL;
  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. /* ------------------------------------------------------------------ */