gntdev.c 27 KB

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