gntdev.c 15 KB

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