gntdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/fs.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/mmu_notifier.h>
  28. #include <linux/types.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/sched.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/slab.h>
  33. #include <xen/xen.h>
  34. #include <xen/grant_table.h>
  35. #include <xen/gntdev.h>
  36. #include <asm/xen/hypervisor.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/page.h>
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  41. "Gerd Hoffmann <kraxel@redhat.com>");
  42. MODULE_DESCRIPTION("User-space granted page access driver");
  43. static int limit = 1024;
  44. module_param(limit, int, 0644);
  45. MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped at "
  46. "once by a gntdev instance");
  47. struct gntdev_priv {
  48. struct list_head maps;
  49. uint32_t used;
  50. uint32_t limit;
  51. /* lock protects maps from concurrent changes */
  52. spinlock_t lock;
  53. struct mm_struct *mm;
  54. struct mmu_notifier mn;
  55. };
  56. struct grant_map {
  57. struct list_head next;
  58. struct gntdev_priv *priv;
  59. struct vm_area_struct *vma;
  60. int index;
  61. int count;
  62. int flags;
  63. int is_mapped;
  64. struct ioctl_gntdev_grant_ref *grants;
  65. struct gnttab_map_grant_ref *map_ops;
  66. struct gnttab_unmap_grant_ref *unmap_ops;
  67. };
  68. /* ------------------------------------------------------------------ */
  69. static void gntdev_print_maps(struct gntdev_priv *priv,
  70. char *text, int text_index)
  71. {
  72. #ifdef DEBUG
  73. struct grant_map *map;
  74. pr_debug("maps list (priv %p, usage %d/%d)\n",
  75. priv, priv->used, priv->limit);
  76. list_for_each_entry(map, &priv->maps, next)
  77. pr_debug(" index %2d, count %2d %s\n",
  78. map->index, map->count,
  79. map->index == text_index && text ? text : "");
  80. #endif
  81. }
  82. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  83. {
  84. struct grant_map *add;
  85. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  86. if (NULL == add)
  87. return NULL;
  88. add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
  89. add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
  90. add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
  91. if (NULL == add->grants ||
  92. NULL == add->map_ops ||
  93. NULL == add->unmap_ops)
  94. goto err;
  95. add->index = 0;
  96. add->count = count;
  97. add->priv = priv;
  98. if (add->count + priv->used > priv->limit)
  99. goto err;
  100. return add;
  101. err:
  102. kfree(add->grants);
  103. kfree(add->map_ops);
  104. kfree(add->unmap_ops);
  105. kfree(add);
  106. return NULL;
  107. }
  108. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  109. {
  110. struct grant_map *map;
  111. list_for_each_entry(map, &priv->maps, next) {
  112. if (add->index + add->count < map->index) {
  113. list_add_tail(&add->next, &map->next);
  114. goto done;
  115. }
  116. add->index = map->index + map->count;
  117. }
  118. list_add_tail(&add->next, &priv->maps);
  119. done:
  120. priv->used += add->count;
  121. gntdev_print_maps(priv, "[new]", add->index);
  122. }
  123. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  124. int index, int count)
  125. {
  126. struct grant_map *map;
  127. list_for_each_entry(map, &priv->maps, next) {
  128. if (map->index != index)
  129. continue;
  130. if (map->count != count)
  131. continue;
  132. return map;
  133. }
  134. return NULL;
  135. }
  136. static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv,
  137. unsigned long vaddr)
  138. {
  139. struct grant_map *map;
  140. list_for_each_entry(map, &priv->maps, next) {
  141. if (!map->vma)
  142. continue;
  143. if (vaddr < map->vma->vm_start)
  144. continue;
  145. if (vaddr >= map->vma->vm_end)
  146. continue;
  147. return map;
  148. }
  149. return NULL;
  150. }
  151. static int gntdev_del_map(struct grant_map *map)
  152. {
  153. int i;
  154. if (map->vma)
  155. return -EBUSY;
  156. for (i = 0; i < map->count; i++)
  157. if (map->unmap_ops[i].handle)
  158. return -EBUSY;
  159. map->priv->used -= map->count;
  160. list_del(&map->next);
  161. return 0;
  162. }
  163. static void gntdev_free_map(struct grant_map *map)
  164. {
  165. if (!map)
  166. return;
  167. kfree(map->grants);
  168. kfree(map->map_ops);
  169. kfree(map->unmap_ops);
  170. kfree(map);
  171. }
  172. /* ------------------------------------------------------------------ */
  173. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  174. unsigned long addr, void *data)
  175. {
  176. struct grant_map *map = data;
  177. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  178. u64 pte_maddr;
  179. BUG_ON(pgnr >= map->count);
  180. pte_maddr = (u64)pfn_to_mfn(page_to_pfn(token)) << PAGE_SHIFT;
  181. pte_maddr += (unsigned long)pte & ~PAGE_MASK;
  182. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
  183. GNTMAP_contains_pte | map->flags,
  184. map->grants[pgnr].ref,
  185. map->grants[pgnr].domid);
  186. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
  187. GNTMAP_contains_pte | map->flags,
  188. 0 /* handle */);
  189. return 0;
  190. }
  191. static int map_grant_pages(struct grant_map *map)
  192. {
  193. int i, err = 0;
  194. pr_debug("map %d+%d\n", map->index, map->count);
  195. err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
  196. map->map_ops, map->count);
  197. if (err)
  198. return err;
  199. for (i = 0; i < map->count; i++) {
  200. if (map->map_ops[i].status)
  201. err = -EINVAL;
  202. map->unmap_ops[i].handle = map->map_ops[i].handle;
  203. }
  204. return err;
  205. }
  206. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  207. {
  208. int i, err = 0;
  209. pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  210. err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
  211. map->unmap_ops + offset, pages);
  212. if (err)
  213. return err;
  214. for (i = 0; i < pages; i++) {
  215. if (map->unmap_ops[offset+i].status)
  216. err = -EINVAL;
  217. map->unmap_ops[offset+i].handle = 0;
  218. }
  219. return err;
  220. }
  221. /* ------------------------------------------------------------------ */
  222. static void gntdev_vma_close(struct vm_area_struct *vma)
  223. {
  224. struct grant_map *map = vma->vm_private_data;
  225. pr_debug("close %p\n", vma);
  226. map->is_mapped = 0;
  227. map->vma = NULL;
  228. vma->vm_private_data = NULL;
  229. }
  230. static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  231. {
  232. pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
  233. vmf->virtual_address, vmf->pgoff);
  234. vmf->flags = VM_FAULT_ERROR;
  235. return 0;
  236. }
  237. static struct vm_operations_struct gntdev_vmops = {
  238. .close = gntdev_vma_close,
  239. .fault = gntdev_vma_fault,
  240. };
  241. /* ------------------------------------------------------------------ */
  242. static void mn_invl_range_start(struct mmu_notifier *mn,
  243. struct mm_struct *mm,
  244. unsigned long start, unsigned long end)
  245. {
  246. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  247. struct grant_map *map;
  248. unsigned long mstart, mend;
  249. int err;
  250. spin_lock(&priv->lock);
  251. list_for_each_entry(map, &priv->maps, next) {
  252. if (!map->vma)
  253. continue;
  254. if (!map->is_mapped)
  255. continue;
  256. if (map->vma->vm_start >= end)
  257. continue;
  258. if (map->vma->vm_end <= start)
  259. continue;
  260. mstart = max(start, map->vma->vm_start);
  261. mend = min(end, map->vma->vm_end);
  262. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  263. map->index, map->count,
  264. map->vma->vm_start, map->vma->vm_end,
  265. start, end, mstart, mend);
  266. err = unmap_grant_pages(map,
  267. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  268. (mend - mstart) >> PAGE_SHIFT);
  269. WARN_ON(err);
  270. }
  271. spin_unlock(&priv->lock);
  272. }
  273. static void mn_invl_page(struct mmu_notifier *mn,
  274. struct mm_struct *mm,
  275. unsigned long address)
  276. {
  277. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  278. }
  279. static void mn_release(struct mmu_notifier *mn,
  280. struct mm_struct *mm)
  281. {
  282. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  283. struct grant_map *map;
  284. int err;
  285. spin_lock(&priv->lock);
  286. list_for_each_entry(map, &priv->maps, next) {
  287. if (!map->vma)
  288. continue;
  289. pr_debug("map %d+%d (%lx %lx)\n",
  290. map->index, map->count,
  291. map->vma->vm_start, map->vma->vm_end);
  292. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  293. WARN_ON(err);
  294. }
  295. spin_unlock(&priv->lock);
  296. }
  297. struct mmu_notifier_ops gntdev_mmu_ops = {
  298. .release = mn_release,
  299. .invalidate_page = mn_invl_page,
  300. .invalidate_range_start = mn_invl_range_start,
  301. };
  302. /* ------------------------------------------------------------------ */
  303. static int gntdev_open(struct inode *inode, struct file *flip)
  304. {
  305. struct gntdev_priv *priv;
  306. int ret = 0;
  307. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  308. if (!priv)
  309. return -ENOMEM;
  310. INIT_LIST_HEAD(&priv->maps);
  311. spin_lock_init(&priv->lock);
  312. priv->limit = limit;
  313. priv->mm = get_task_mm(current);
  314. if (!priv->mm) {
  315. kfree(priv);
  316. return -ENOMEM;
  317. }
  318. priv->mn.ops = &gntdev_mmu_ops;
  319. ret = mmu_notifier_register(&priv->mn, priv->mm);
  320. mmput(priv->mm);
  321. if (ret) {
  322. kfree(priv);
  323. return ret;
  324. }
  325. flip->private_data = priv;
  326. pr_debug("priv %p\n", priv);
  327. return 0;
  328. }
  329. static int gntdev_release(struct inode *inode, struct file *flip)
  330. {
  331. struct gntdev_priv *priv = flip->private_data;
  332. struct grant_map *map;
  333. int err;
  334. pr_debug("priv %p\n", priv);
  335. spin_lock(&priv->lock);
  336. while (!list_empty(&priv->maps)) {
  337. map = list_entry(priv->maps.next, struct grant_map, next);
  338. err = gntdev_del_map(map);
  339. if (WARN_ON(err))
  340. gntdev_free_map(map);
  341. }
  342. spin_unlock(&priv->lock);
  343. mmu_notifier_unregister(&priv->mn, priv->mm);
  344. kfree(priv);
  345. return 0;
  346. }
  347. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  348. struct ioctl_gntdev_map_grant_ref __user *u)
  349. {
  350. struct ioctl_gntdev_map_grant_ref op;
  351. struct grant_map *map;
  352. int err;
  353. if (copy_from_user(&op, u, sizeof(op)) != 0)
  354. return -EFAULT;
  355. pr_debug("priv %p, add %d\n", priv, op.count);
  356. if (unlikely(op.count <= 0))
  357. return -EINVAL;
  358. if (unlikely(op.count > priv->limit))
  359. return -EINVAL;
  360. err = -ENOMEM;
  361. map = gntdev_alloc_map(priv, op.count);
  362. if (!map)
  363. return err;
  364. if (copy_from_user(map->grants, &u->refs,
  365. sizeof(map->grants[0]) * op.count) != 0) {
  366. gntdev_free_map(map);
  367. return err;
  368. }
  369. spin_lock(&priv->lock);
  370. gntdev_add_map(priv, map);
  371. op.index = map->index << PAGE_SHIFT;
  372. spin_unlock(&priv->lock);
  373. if (copy_to_user(u, &op, sizeof(op)) != 0) {
  374. spin_lock(&priv->lock);
  375. gntdev_del_map(map);
  376. spin_unlock(&priv->lock);
  377. gntdev_free_map(map);
  378. return err;
  379. }
  380. return 0;
  381. }
  382. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  383. struct ioctl_gntdev_unmap_grant_ref __user *u)
  384. {
  385. struct ioctl_gntdev_unmap_grant_ref op;
  386. struct grant_map *map;
  387. int err = -ENOENT;
  388. if (copy_from_user(&op, u, sizeof(op)) != 0)
  389. return -EFAULT;
  390. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  391. spin_lock(&priv->lock);
  392. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  393. if (map)
  394. err = gntdev_del_map(map);
  395. spin_unlock(&priv->lock);
  396. if (!err)
  397. gntdev_free_map(map);
  398. return err;
  399. }
  400. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  401. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  402. {
  403. struct ioctl_gntdev_get_offset_for_vaddr op;
  404. struct grant_map *map;
  405. if (copy_from_user(&op, u, sizeof(op)) != 0)
  406. return -EFAULT;
  407. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  408. spin_lock(&priv->lock);
  409. map = gntdev_find_map_vaddr(priv, op.vaddr);
  410. if (map == NULL ||
  411. map->vma->vm_start != op.vaddr) {
  412. spin_unlock(&priv->lock);
  413. return -EINVAL;
  414. }
  415. op.offset = map->index << PAGE_SHIFT;
  416. op.count = map->count;
  417. spin_unlock(&priv->lock);
  418. if (copy_to_user(u, &op, sizeof(op)) != 0)
  419. return -EFAULT;
  420. return 0;
  421. }
  422. static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv,
  423. struct ioctl_gntdev_set_max_grants __user *u)
  424. {
  425. struct ioctl_gntdev_set_max_grants op;
  426. if (copy_from_user(&op, u, sizeof(op)) != 0)
  427. return -EFAULT;
  428. pr_debug("priv %p, limit %d\n", priv, op.count);
  429. if (op.count > limit)
  430. return -E2BIG;
  431. spin_lock(&priv->lock);
  432. priv->limit = op.count;
  433. spin_unlock(&priv->lock);
  434. return 0;
  435. }
  436. static long gntdev_ioctl(struct file *flip,
  437. unsigned int cmd, unsigned long arg)
  438. {
  439. struct gntdev_priv *priv = flip->private_data;
  440. void __user *ptr = (void __user *)arg;
  441. switch (cmd) {
  442. case IOCTL_GNTDEV_MAP_GRANT_REF:
  443. return gntdev_ioctl_map_grant_ref(priv, ptr);
  444. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  445. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  446. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  447. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  448. case IOCTL_GNTDEV_SET_MAX_GRANTS:
  449. return gntdev_ioctl_set_max_grants(priv, ptr);
  450. default:
  451. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  452. return -ENOIOCTLCMD;
  453. }
  454. return 0;
  455. }
  456. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  457. {
  458. struct gntdev_priv *priv = flip->private_data;
  459. int index = vma->vm_pgoff;
  460. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  461. struct grant_map *map;
  462. int err = -EINVAL;
  463. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  464. return -EINVAL;
  465. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  466. index, count, vma->vm_start, vma->vm_pgoff);
  467. spin_lock(&priv->lock);
  468. map = gntdev_find_map_index(priv, index, count);
  469. if (!map)
  470. goto unlock_out;
  471. if (map->vma)
  472. goto unlock_out;
  473. if (priv->mm != vma->vm_mm) {
  474. printk(KERN_WARNING "Huh? Other mm?\n");
  475. goto unlock_out;
  476. }
  477. vma->vm_ops = &gntdev_vmops;
  478. vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
  479. vma->vm_private_data = map;
  480. map->vma = vma;
  481. map->flags = GNTMAP_host_map | GNTMAP_application_map;
  482. if (!(vma->vm_flags & VM_WRITE))
  483. map->flags |= GNTMAP_readonly;
  484. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  485. vma->vm_end - vma->vm_start,
  486. find_grant_ptes, map);
  487. if (err) {
  488. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  489. goto unlock_out;
  490. }
  491. err = map_grant_pages(map);
  492. if (err) {
  493. printk(KERN_WARNING "map_grant_pages() failure.\n");
  494. goto unlock_out;
  495. }
  496. map->is_mapped = 1;
  497. unlock_out:
  498. spin_unlock(&priv->lock);
  499. return err;
  500. }
  501. static const struct file_operations gntdev_fops = {
  502. .owner = THIS_MODULE,
  503. .open = gntdev_open,
  504. .release = gntdev_release,
  505. .mmap = gntdev_mmap,
  506. .unlocked_ioctl = gntdev_ioctl
  507. };
  508. static struct miscdevice gntdev_miscdev = {
  509. .minor = MISC_DYNAMIC_MINOR,
  510. .name = "xen/gntdev",
  511. .fops = &gntdev_fops,
  512. };
  513. /* ------------------------------------------------------------------ */
  514. static int __init gntdev_init(void)
  515. {
  516. int err;
  517. if (!xen_domain())
  518. return -ENODEV;
  519. err = misc_register(&gntdev_miscdev);
  520. if (err != 0) {
  521. printk(KERN_ERR "Could not register gntdev device\n");
  522. return err;
  523. }
  524. return 0;
  525. }
  526. static void __exit gntdev_exit(void)
  527. {
  528. misc_deregister(&gntdev_miscdev);
  529. }
  530. module_init(gntdev_init);
  531. module_exit(gntdev_exit);
  532. /* ------------------------------------------------------------------ */