gntdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 = arbitrary_virt_to_machine(pte).maddr;
  181. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
  182. GNTMAP_contains_pte | map->flags,
  183. map->grants[pgnr].ref,
  184. map->grants[pgnr].domid);
  185. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
  186. GNTMAP_contains_pte | map->flags,
  187. 0 /* handle */);
  188. return 0;
  189. }
  190. static int map_grant_pages(struct grant_map *map)
  191. {
  192. int i, err = 0;
  193. pr_debug("map %d+%d\n", map->index, map->count);
  194. err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
  195. map->map_ops, map->count);
  196. if (err)
  197. return err;
  198. for (i = 0; i < map->count; i++) {
  199. if (map->map_ops[i].status)
  200. err = -EINVAL;
  201. map->unmap_ops[i].handle = map->map_ops[i].handle;
  202. }
  203. return err;
  204. }
  205. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  206. {
  207. int i, err = 0;
  208. pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  209. err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
  210. map->unmap_ops + offset, pages);
  211. if (err)
  212. return err;
  213. for (i = 0; i < pages; i++) {
  214. if (map->unmap_ops[offset+i].status)
  215. err = -EINVAL;
  216. map->unmap_ops[offset+i].handle = 0;
  217. }
  218. return err;
  219. }
  220. /* ------------------------------------------------------------------ */
  221. static void gntdev_vma_close(struct vm_area_struct *vma)
  222. {
  223. struct grant_map *map = vma->vm_private_data;
  224. pr_debug("close %p\n", vma);
  225. map->is_mapped = 0;
  226. map->vma = NULL;
  227. vma->vm_private_data = NULL;
  228. }
  229. static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  230. {
  231. pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
  232. vmf->virtual_address, vmf->pgoff);
  233. vmf->flags = VM_FAULT_ERROR;
  234. return 0;
  235. }
  236. static struct vm_operations_struct gntdev_vmops = {
  237. .close = gntdev_vma_close,
  238. .fault = gntdev_vma_fault,
  239. };
  240. /* ------------------------------------------------------------------ */
  241. static void mn_invl_range_start(struct mmu_notifier *mn,
  242. struct mm_struct *mm,
  243. unsigned long start, unsigned long end)
  244. {
  245. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  246. struct grant_map *map;
  247. unsigned long mstart, mend;
  248. int err;
  249. spin_lock(&priv->lock);
  250. list_for_each_entry(map, &priv->maps, next) {
  251. if (!map->vma)
  252. continue;
  253. if (!map->is_mapped)
  254. continue;
  255. if (map->vma->vm_start >= end)
  256. continue;
  257. if (map->vma->vm_end <= start)
  258. continue;
  259. mstart = max(start, map->vma->vm_start);
  260. mend = min(end, map->vma->vm_end);
  261. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  262. map->index, map->count,
  263. map->vma->vm_start, map->vma->vm_end,
  264. start, end, mstart, mend);
  265. err = unmap_grant_pages(map,
  266. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  267. (mend - mstart) >> PAGE_SHIFT);
  268. WARN_ON(err);
  269. }
  270. spin_unlock(&priv->lock);
  271. }
  272. static void mn_invl_page(struct mmu_notifier *mn,
  273. struct mm_struct *mm,
  274. unsigned long address)
  275. {
  276. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  277. }
  278. static void mn_release(struct mmu_notifier *mn,
  279. struct mm_struct *mm)
  280. {
  281. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  282. struct grant_map *map;
  283. int err;
  284. spin_lock(&priv->lock);
  285. list_for_each_entry(map, &priv->maps, next) {
  286. if (!map->vma)
  287. continue;
  288. pr_debug("map %d+%d (%lx %lx)\n",
  289. map->index, map->count,
  290. map->vma->vm_start, map->vma->vm_end);
  291. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  292. WARN_ON(err);
  293. }
  294. spin_unlock(&priv->lock);
  295. }
  296. struct mmu_notifier_ops gntdev_mmu_ops = {
  297. .release = mn_release,
  298. .invalidate_page = mn_invl_page,
  299. .invalidate_range_start = mn_invl_range_start,
  300. };
  301. /* ------------------------------------------------------------------ */
  302. static int gntdev_open(struct inode *inode, struct file *flip)
  303. {
  304. struct gntdev_priv *priv;
  305. int ret = 0;
  306. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  307. if (!priv)
  308. return -ENOMEM;
  309. INIT_LIST_HEAD(&priv->maps);
  310. spin_lock_init(&priv->lock);
  311. priv->limit = limit;
  312. priv->mm = get_task_mm(current);
  313. if (!priv->mm) {
  314. kfree(priv);
  315. return -ENOMEM;
  316. }
  317. priv->mn.ops = &gntdev_mmu_ops;
  318. ret = mmu_notifier_register(&priv->mn, priv->mm);
  319. mmput(priv->mm);
  320. if (ret) {
  321. kfree(priv);
  322. return ret;
  323. }
  324. flip->private_data = priv;
  325. pr_debug("priv %p\n", priv);
  326. return 0;
  327. }
  328. static int gntdev_release(struct inode *inode, struct file *flip)
  329. {
  330. struct gntdev_priv *priv = flip->private_data;
  331. struct grant_map *map;
  332. int err;
  333. pr_debug("priv %p\n", priv);
  334. spin_lock(&priv->lock);
  335. while (!list_empty(&priv->maps)) {
  336. map = list_entry(priv->maps.next, struct grant_map, next);
  337. err = gntdev_del_map(map);
  338. if (WARN_ON(err))
  339. gntdev_free_map(map);
  340. }
  341. spin_unlock(&priv->lock);
  342. mmu_notifier_unregister(&priv->mn, priv->mm);
  343. kfree(priv);
  344. return 0;
  345. }
  346. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  347. struct ioctl_gntdev_map_grant_ref __user *u)
  348. {
  349. struct ioctl_gntdev_map_grant_ref op;
  350. struct grant_map *map;
  351. int err;
  352. if (copy_from_user(&op, u, sizeof(op)) != 0)
  353. return -EFAULT;
  354. pr_debug("priv %p, add %d\n", priv, op.count);
  355. if (unlikely(op.count <= 0))
  356. return -EINVAL;
  357. if (unlikely(op.count > priv->limit))
  358. return -EINVAL;
  359. err = -ENOMEM;
  360. map = gntdev_alloc_map(priv, op.count);
  361. if (!map)
  362. return err;
  363. if (copy_from_user(map->grants, &u->refs,
  364. sizeof(map->grants[0]) * op.count) != 0) {
  365. gntdev_free_map(map);
  366. return err;
  367. }
  368. spin_lock(&priv->lock);
  369. gntdev_add_map(priv, map);
  370. op.index = map->index << PAGE_SHIFT;
  371. spin_unlock(&priv->lock);
  372. if (copy_to_user(u, &op, sizeof(op)) != 0) {
  373. spin_lock(&priv->lock);
  374. gntdev_del_map(map);
  375. spin_unlock(&priv->lock);
  376. gntdev_free_map(map);
  377. return err;
  378. }
  379. return 0;
  380. }
  381. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  382. struct ioctl_gntdev_unmap_grant_ref __user *u)
  383. {
  384. struct ioctl_gntdev_unmap_grant_ref op;
  385. struct grant_map *map;
  386. int err = -ENOENT;
  387. if (copy_from_user(&op, u, sizeof(op)) != 0)
  388. return -EFAULT;
  389. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  390. spin_lock(&priv->lock);
  391. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  392. if (map)
  393. err = gntdev_del_map(map);
  394. spin_unlock(&priv->lock);
  395. if (!err)
  396. gntdev_free_map(map);
  397. return err;
  398. }
  399. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  400. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  401. {
  402. struct ioctl_gntdev_get_offset_for_vaddr op;
  403. struct grant_map *map;
  404. if (copy_from_user(&op, u, sizeof(op)) != 0)
  405. return -EFAULT;
  406. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  407. spin_lock(&priv->lock);
  408. map = gntdev_find_map_vaddr(priv, op.vaddr);
  409. if (map == NULL ||
  410. map->vma->vm_start != op.vaddr) {
  411. spin_unlock(&priv->lock);
  412. return -EINVAL;
  413. }
  414. op.offset = map->index << PAGE_SHIFT;
  415. op.count = map->count;
  416. spin_unlock(&priv->lock);
  417. if (copy_to_user(u, &op, sizeof(op)) != 0)
  418. return -EFAULT;
  419. return 0;
  420. }
  421. static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv,
  422. struct ioctl_gntdev_set_max_grants __user *u)
  423. {
  424. struct ioctl_gntdev_set_max_grants op;
  425. if (copy_from_user(&op, u, sizeof(op)) != 0)
  426. return -EFAULT;
  427. pr_debug("priv %p, limit %d\n", priv, op.count);
  428. if (op.count > limit)
  429. return -E2BIG;
  430. spin_lock(&priv->lock);
  431. priv->limit = op.count;
  432. spin_unlock(&priv->lock);
  433. return 0;
  434. }
  435. static long gntdev_ioctl(struct file *flip,
  436. unsigned int cmd, unsigned long arg)
  437. {
  438. struct gntdev_priv *priv = flip->private_data;
  439. void __user *ptr = (void __user *)arg;
  440. switch (cmd) {
  441. case IOCTL_GNTDEV_MAP_GRANT_REF:
  442. return gntdev_ioctl_map_grant_ref(priv, ptr);
  443. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  444. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  445. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  446. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  447. case IOCTL_GNTDEV_SET_MAX_GRANTS:
  448. return gntdev_ioctl_set_max_grants(priv, ptr);
  449. default:
  450. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  451. return -ENOIOCTLCMD;
  452. }
  453. return 0;
  454. }
  455. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  456. {
  457. struct gntdev_priv *priv = flip->private_data;
  458. int index = vma->vm_pgoff;
  459. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  460. struct grant_map *map;
  461. int err = -EINVAL;
  462. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  463. return -EINVAL;
  464. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  465. index, count, vma->vm_start, vma->vm_pgoff);
  466. spin_lock(&priv->lock);
  467. map = gntdev_find_map_index(priv, index, count);
  468. if (!map)
  469. goto unlock_out;
  470. if (map->vma)
  471. goto unlock_out;
  472. if (priv->mm != vma->vm_mm) {
  473. printk(KERN_WARNING "Huh? Other mm?\n");
  474. goto unlock_out;
  475. }
  476. vma->vm_ops = &gntdev_vmops;
  477. vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
  478. vma->vm_private_data = map;
  479. map->vma = vma;
  480. map->flags = GNTMAP_host_map | GNTMAP_application_map;
  481. if (!(vma->vm_flags & VM_WRITE))
  482. map->flags |= GNTMAP_readonly;
  483. spin_unlock(&priv->lock);
  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. return err;
  490. }
  491. err = map_grant_pages(map);
  492. if (err) {
  493. printk(KERN_WARNING "map_grant_pages() failure.\n");
  494. return err;
  495. }
  496. map->is_mapped = 1;
  497. return 0;
  498. unlock_out:
  499. spin_unlock(&priv->lock);
  500. return err;
  501. }
  502. static const struct file_operations gntdev_fops = {
  503. .owner = THIS_MODULE,
  504. .open = gntdev_open,
  505. .release = gntdev_release,
  506. .mmap = gntdev_mmap,
  507. .unlocked_ioctl = gntdev_ioctl
  508. };
  509. static struct miscdevice gntdev_miscdev = {
  510. .minor = MISC_DYNAMIC_MINOR,
  511. .name = "xen/gntdev",
  512. .fops = &gntdev_fops,
  513. };
  514. /* ------------------------------------------------------------------ */
  515. static int __init gntdev_init(void)
  516. {
  517. int err;
  518. if (!xen_domain())
  519. return -ENODEV;
  520. err = misc_register(&gntdev_miscdev);
  521. if (err != 0) {
  522. printk(KERN_ERR "Could not register gntdev device\n");
  523. return err;
  524. }
  525. return 0;
  526. }
  527. static void __exit gntdev_exit(void)
  528. {
  529. misc_deregister(&gntdev_miscdev);
  530. }
  531. module_init(gntdev_init);
  532. module_exit(gntdev_exit);
  533. /* ------------------------------------------------------------------ */