gntdev.c 15 KB

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