gntdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 <linux/highmem.h>
  34. #include <xen/xen.h>
  35. #include <xen/grant_table.h>
  36. #include <xen/balloon.h>
  37. #include <xen/gntdev.h>
  38. #include <xen/events.h>
  39. #include <asm/xen/hypervisor.h>
  40. #include <asm/xen/hypercall.h>
  41. #include <asm/xen/page.h>
  42. MODULE_LICENSE("GPL");
  43. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  44. "Gerd Hoffmann <kraxel@redhat.com>");
  45. MODULE_DESCRIPTION("User-space granted page access driver");
  46. static int limit = 1024*1024;
  47. module_param(limit, int, 0644);
  48. MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
  49. "the gntdev device");
  50. static atomic_t pages_mapped = ATOMIC_INIT(0);
  51. static int use_ptemod;
  52. struct gntdev_priv {
  53. struct list_head maps;
  54. /* lock protects maps from concurrent changes */
  55. spinlock_t lock;
  56. struct mm_struct *mm;
  57. struct mmu_notifier mn;
  58. };
  59. struct unmap_notify {
  60. int flags;
  61. /* Address relative to the start of the grant_map */
  62. int addr;
  63. int event;
  64. };
  65. struct grant_map {
  66. struct list_head next;
  67. struct vm_area_struct *vma;
  68. int index;
  69. int count;
  70. int flags;
  71. atomic_t users;
  72. struct unmap_notify notify;
  73. struct ioctl_gntdev_grant_ref *grants;
  74. struct gnttab_map_grant_ref *map_ops;
  75. struct gnttab_unmap_grant_ref *unmap_ops;
  76. struct gnttab_map_grant_ref *kmap_ops;
  77. struct page **pages;
  78. };
  79. static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
  80. /* ------------------------------------------------------------------ */
  81. static void gntdev_print_maps(struct gntdev_priv *priv,
  82. char *text, int text_index)
  83. {
  84. #ifdef DEBUG
  85. struct grant_map *map;
  86. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  87. list_for_each_entry(map, &priv->maps, next)
  88. pr_debug(" index %2d, count %2d %s\n",
  89. map->index, map->count,
  90. map->index == text_index && text ? text : "");
  91. #endif
  92. }
  93. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  94. {
  95. struct grant_map *add;
  96. int i;
  97. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  98. if (NULL == add)
  99. return NULL;
  100. add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
  101. add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
  102. add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
  103. add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
  104. add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
  105. if (NULL == add->grants ||
  106. NULL == add->map_ops ||
  107. NULL == add->unmap_ops ||
  108. NULL == add->kmap_ops ||
  109. NULL == add->pages)
  110. goto err;
  111. if (alloc_xenballooned_pages(count, add->pages, false /* lowmem */))
  112. goto err;
  113. for (i = 0; i < count; i++) {
  114. add->map_ops[i].handle = -1;
  115. add->unmap_ops[i].handle = -1;
  116. add->kmap_ops[i].handle = -1;
  117. }
  118. add->index = 0;
  119. add->count = count;
  120. atomic_set(&add->users, 1);
  121. return add;
  122. err:
  123. kfree(add->pages);
  124. kfree(add->grants);
  125. kfree(add->map_ops);
  126. kfree(add->unmap_ops);
  127. kfree(add->kmap_ops);
  128. kfree(add);
  129. return NULL;
  130. }
  131. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  132. {
  133. struct grant_map *map;
  134. list_for_each_entry(map, &priv->maps, next) {
  135. if (add->index + add->count < map->index) {
  136. list_add_tail(&add->next, &map->next);
  137. goto done;
  138. }
  139. add->index = map->index + map->count;
  140. }
  141. list_add_tail(&add->next, &priv->maps);
  142. done:
  143. gntdev_print_maps(priv, "[new]", add->index);
  144. }
  145. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  146. int index, int count)
  147. {
  148. struct grant_map *map;
  149. list_for_each_entry(map, &priv->maps, next) {
  150. if (map->index != index)
  151. continue;
  152. if (count && map->count != count)
  153. continue;
  154. return map;
  155. }
  156. return NULL;
  157. }
  158. static void gntdev_put_map(struct grant_map *map)
  159. {
  160. if (!map)
  161. return;
  162. if (!atomic_dec_and_test(&map->users))
  163. return;
  164. atomic_sub(map->count, &pages_mapped);
  165. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  166. notify_remote_via_evtchn(map->notify.event);
  167. evtchn_put(map->notify.event);
  168. }
  169. if (map->pages) {
  170. if (!use_ptemod)
  171. unmap_grant_pages(map, 0, map->count);
  172. free_xenballooned_pages(map->count, map->pages);
  173. }
  174. kfree(map->pages);
  175. kfree(map->grants);
  176. kfree(map->map_ops);
  177. kfree(map->unmap_ops);
  178. kfree(map);
  179. }
  180. /* ------------------------------------------------------------------ */
  181. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  182. unsigned long addr, void *data)
  183. {
  184. struct grant_map *map = data;
  185. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  186. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  187. u64 pte_maddr;
  188. BUG_ON(pgnr >= map->count);
  189. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  190. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  191. map->grants[pgnr].ref,
  192. map->grants[pgnr].domid);
  193. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  194. -1 /* handle */);
  195. return 0;
  196. }
  197. static int map_grant_pages(struct grant_map *map)
  198. {
  199. int i, err = 0;
  200. if (!use_ptemod) {
  201. /* Note: it could already be mapped */
  202. if (map->map_ops[0].handle != -1)
  203. return 0;
  204. for (i = 0; i < map->count; i++) {
  205. unsigned long addr = (unsigned long)
  206. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  207. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  208. map->grants[i].ref,
  209. map->grants[i].domid);
  210. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  211. map->flags, -1 /* handle */);
  212. }
  213. } else {
  214. /*
  215. * Setup the map_ops corresponding to the pte entries pointing
  216. * to the kernel linear addresses of the struct pages.
  217. * These ptes are completely different from the user ptes dealt
  218. * with find_grant_ptes.
  219. */
  220. for (i = 0; i < map->count; i++) {
  221. unsigned level;
  222. unsigned long address = (unsigned long)
  223. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  224. pte_t *ptep;
  225. u64 pte_maddr = 0;
  226. BUG_ON(PageHighMem(map->pages[i]));
  227. ptep = lookup_address(address, &level);
  228. pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
  229. gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
  230. map->flags |
  231. GNTMAP_host_map |
  232. GNTMAP_contains_pte,
  233. map->grants[i].ref,
  234. map->grants[i].domid);
  235. }
  236. }
  237. pr_debug("map %d+%d\n", map->index, map->count);
  238. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  239. map->pages, map->count);
  240. if (err)
  241. return err;
  242. for (i = 0; i < map->count; i++) {
  243. if (map->map_ops[i].status)
  244. err = -EINVAL;
  245. else {
  246. BUG_ON(map->map_ops[i].handle == -1);
  247. map->unmap_ops[i].handle = map->map_ops[i].handle;
  248. pr_debug("map handle=%d\n", map->map_ops[i].handle);
  249. }
  250. }
  251. return err;
  252. }
  253. static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
  254. {
  255. int i, err = 0;
  256. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  257. int pgno = (map->notify.addr >> PAGE_SHIFT);
  258. if (pgno >= offset && pgno < offset + pages && use_ptemod) {
  259. void __user *tmp = (void __user *)
  260. map->vma->vm_start + map->notify.addr;
  261. err = copy_to_user(tmp, &err, 1);
  262. if (err)
  263. return -EFAULT;
  264. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  265. } else if (pgno >= offset && pgno < offset + pages) {
  266. uint8_t *tmp = kmap(map->pages[pgno]);
  267. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  268. kunmap(map->pages[pgno]);
  269. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  270. }
  271. }
  272. err = gnttab_unmap_refs(map->unmap_ops + offset,
  273. use_ptemod ? map->kmap_ops + offset : NULL, map->pages + offset,
  274. pages);
  275. if (err)
  276. return err;
  277. for (i = 0; i < pages; i++) {
  278. if (map->unmap_ops[offset+i].status)
  279. err = -EINVAL;
  280. pr_debug("unmap handle=%d st=%d\n",
  281. map->unmap_ops[offset+i].handle,
  282. map->unmap_ops[offset+i].status);
  283. map->unmap_ops[offset+i].handle = -1;
  284. }
  285. return err;
  286. }
  287. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  288. {
  289. int range, err = 0;
  290. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  291. /* It is possible the requested range will have a "hole" where we
  292. * already unmapped some of the grants. Only unmap valid ranges.
  293. */
  294. while (pages && !err) {
  295. while (pages && map->unmap_ops[offset].handle == -1) {
  296. offset++;
  297. pages--;
  298. }
  299. range = 0;
  300. while (range < pages) {
  301. if (map->unmap_ops[offset+range].handle == -1) {
  302. range--;
  303. break;
  304. }
  305. range++;
  306. }
  307. err = __unmap_grant_pages(map, offset, range);
  308. offset += range;
  309. pages -= range;
  310. }
  311. return err;
  312. }
  313. /* ------------------------------------------------------------------ */
  314. static void gntdev_vma_open(struct vm_area_struct *vma)
  315. {
  316. struct grant_map *map = vma->vm_private_data;
  317. pr_debug("gntdev_vma_open %p\n", vma);
  318. atomic_inc(&map->users);
  319. }
  320. static void gntdev_vma_close(struct vm_area_struct *vma)
  321. {
  322. struct grant_map *map = vma->vm_private_data;
  323. pr_debug("gntdev_vma_close %p\n", vma);
  324. map->vma = NULL;
  325. vma->vm_private_data = NULL;
  326. gntdev_put_map(map);
  327. }
  328. static struct vm_operations_struct gntdev_vmops = {
  329. .open = gntdev_vma_open,
  330. .close = gntdev_vma_close,
  331. };
  332. /* ------------------------------------------------------------------ */
  333. static void mn_invl_range_start(struct mmu_notifier *mn,
  334. struct mm_struct *mm,
  335. unsigned long start, unsigned long end)
  336. {
  337. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  338. struct grant_map *map;
  339. unsigned long mstart, mend;
  340. int err;
  341. spin_lock(&priv->lock);
  342. list_for_each_entry(map, &priv->maps, next) {
  343. if (!map->vma)
  344. continue;
  345. if (map->vma->vm_start >= end)
  346. continue;
  347. if (map->vma->vm_end <= start)
  348. continue;
  349. mstart = max(start, map->vma->vm_start);
  350. mend = min(end, map->vma->vm_end);
  351. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  352. map->index, map->count,
  353. map->vma->vm_start, map->vma->vm_end,
  354. start, end, mstart, mend);
  355. err = unmap_grant_pages(map,
  356. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  357. (mend - mstart) >> PAGE_SHIFT);
  358. WARN_ON(err);
  359. }
  360. spin_unlock(&priv->lock);
  361. }
  362. static void mn_invl_page(struct mmu_notifier *mn,
  363. struct mm_struct *mm,
  364. unsigned long address)
  365. {
  366. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  367. }
  368. static void mn_release(struct mmu_notifier *mn,
  369. struct mm_struct *mm)
  370. {
  371. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  372. struct grant_map *map;
  373. int err;
  374. spin_lock(&priv->lock);
  375. list_for_each_entry(map, &priv->maps, next) {
  376. if (!map->vma)
  377. continue;
  378. pr_debug("map %d+%d (%lx %lx)\n",
  379. map->index, map->count,
  380. map->vma->vm_start, map->vma->vm_end);
  381. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  382. WARN_ON(err);
  383. }
  384. spin_unlock(&priv->lock);
  385. }
  386. static struct mmu_notifier_ops gntdev_mmu_ops = {
  387. .release = mn_release,
  388. .invalidate_page = mn_invl_page,
  389. .invalidate_range_start = mn_invl_range_start,
  390. };
  391. /* ------------------------------------------------------------------ */
  392. static int gntdev_open(struct inode *inode, struct file *flip)
  393. {
  394. struct gntdev_priv *priv;
  395. int ret = 0;
  396. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  397. if (!priv)
  398. return -ENOMEM;
  399. INIT_LIST_HEAD(&priv->maps);
  400. spin_lock_init(&priv->lock);
  401. if (use_ptemod) {
  402. priv->mm = get_task_mm(current);
  403. if (!priv->mm) {
  404. kfree(priv);
  405. return -ENOMEM;
  406. }
  407. priv->mn.ops = &gntdev_mmu_ops;
  408. ret = mmu_notifier_register(&priv->mn, priv->mm);
  409. mmput(priv->mm);
  410. }
  411. if (ret) {
  412. kfree(priv);
  413. return ret;
  414. }
  415. flip->private_data = priv;
  416. pr_debug("priv %p\n", priv);
  417. return 0;
  418. }
  419. static int gntdev_release(struct inode *inode, struct file *flip)
  420. {
  421. struct gntdev_priv *priv = flip->private_data;
  422. struct grant_map *map;
  423. pr_debug("priv %p\n", priv);
  424. while (!list_empty(&priv->maps)) {
  425. map = list_entry(priv->maps.next, struct grant_map, next);
  426. list_del(&map->next);
  427. gntdev_put_map(map);
  428. }
  429. if (use_ptemod)
  430. mmu_notifier_unregister(&priv->mn, priv->mm);
  431. kfree(priv);
  432. return 0;
  433. }
  434. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  435. struct ioctl_gntdev_map_grant_ref __user *u)
  436. {
  437. struct ioctl_gntdev_map_grant_ref op;
  438. struct grant_map *map;
  439. int err;
  440. if (copy_from_user(&op, u, sizeof(op)) != 0)
  441. return -EFAULT;
  442. pr_debug("priv %p, add %d\n", priv, op.count);
  443. if (unlikely(op.count <= 0))
  444. return -EINVAL;
  445. err = -ENOMEM;
  446. map = gntdev_alloc_map(priv, op.count);
  447. if (!map)
  448. return err;
  449. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  450. pr_debug("can't map: over limit\n");
  451. gntdev_put_map(map);
  452. return err;
  453. }
  454. if (copy_from_user(map->grants, &u->refs,
  455. sizeof(map->grants[0]) * op.count) != 0) {
  456. gntdev_put_map(map);
  457. return err;
  458. }
  459. spin_lock(&priv->lock);
  460. gntdev_add_map(priv, map);
  461. op.index = map->index << PAGE_SHIFT;
  462. spin_unlock(&priv->lock);
  463. if (copy_to_user(u, &op, sizeof(op)) != 0)
  464. return -EFAULT;
  465. return 0;
  466. }
  467. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  468. struct ioctl_gntdev_unmap_grant_ref __user *u)
  469. {
  470. struct ioctl_gntdev_unmap_grant_ref op;
  471. struct grant_map *map;
  472. int err = -ENOENT;
  473. if (copy_from_user(&op, u, sizeof(op)) != 0)
  474. return -EFAULT;
  475. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  476. spin_lock(&priv->lock);
  477. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  478. if (map) {
  479. list_del(&map->next);
  480. err = 0;
  481. }
  482. spin_unlock(&priv->lock);
  483. if (map)
  484. gntdev_put_map(map);
  485. return err;
  486. }
  487. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  488. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  489. {
  490. struct ioctl_gntdev_get_offset_for_vaddr op;
  491. struct vm_area_struct *vma;
  492. struct grant_map *map;
  493. if (copy_from_user(&op, u, sizeof(op)) != 0)
  494. return -EFAULT;
  495. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  496. vma = find_vma(current->mm, op.vaddr);
  497. if (!vma || vma->vm_ops != &gntdev_vmops)
  498. return -EINVAL;
  499. map = vma->vm_private_data;
  500. if (!map)
  501. return -EINVAL;
  502. op.offset = map->index << PAGE_SHIFT;
  503. op.count = map->count;
  504. if (copy_to_user(u, &op, sizeof(op)) != 0)
  505. return -EFAULT;
  506. return 0;
  507. }
  508. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  509. {
  510. struct ioctl_gntdev_unmap_notify op;
  511. struct grant_map *map;
  512. int rc;
  513. int out_flags;
  514. unsigned int out_event;
  515. if (copy_from_user(&op, u, sizeof(op)))
  516. return -EFAULT;
  517. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  518. return -EINVAL;
  519. /* We need to grab a reference to the event channel we are going to use
  520. * to send the notify before releasing the reference we may already have
  521. * (if someone has called this ioctl twice). This is required so that
  522. * it is possible to change the clear_byte part of the notification
  523. * without disturbing the event channel part, which may now be the last
  524. * reference to that event channel.
  525. */
  526. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  527. if (evtchn_get(op.event_channel_port))
  528. return -EINVAL;
  529. }
  530. out_flags = op.action;
  531. out_event = op.event_channel_port;
  532. spin_lock(&priv->lock);
  533. list_for_each_entry(map, &priv->maps, next) {
  534. uint64_t begin = map->index << PAGE_SHIFT;
  535. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  536. if (op.index >= begin && op.index < end)
  537. goto found;
  538. }
  539. rc = -ENOENT;
  540. goto unlock_out;
  541. found:
  542. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  543. (map->flags & GNTMAP_readonly)) {
  544. rc = -EINVAL;
  545. goto unlock_out;
  546. }
  547. out_flags = map->notify.flags;
  548. out_event = map->notify.event;
  549. map->notify.flags = op.action;
  550. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  551. map->notify.event = op.event_channel_port;
  552. rc = 0;
  553. unlock_out:
  554. spin_unlock(&priv->lock);
  555. /* Drop the reference to the event channel we did not save in the map */
  556. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  557. evtchn_put(out_event);
  558. return rc;
  559. }
  560. static long gntdev_ioctl(struct file *flip,
  561. unsigned int cmd, unsigned long arg)
  562. {
  563. struct gntdev_priv *priv = flip->private_data;
  564. void __user *ptr = (void __user *)arg;
  565. switch (cmd) {
  566. case IOCTL_GNTDEV_MAP_GRANT_REF:
  567. return gntdev_ioctl_map_grant_ref(priv, ptr);
  568. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  569. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  570. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  571. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  572. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  573. return gntdev_ioctl_notify(priv, ptr);
  574. default:
  575. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  576. return -ENOIOCTLCMD;
  577. }
  578. return 0;
  579. }
  580. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  581. {
  582. struct gntdev_priv *priv = flip->private_data;
  583. int index = vma->vm_pgoff;
  584. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  585. struct grant_map *map;
  586. int i, err = -EINVAL;
  587. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  588. return -EINVAL;
  589. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  590. index, count, vma->vm_start, vma->vm_pgoff);
  591. spin_lock(&priv->lock);
  592. map = gntdev_find_map_index(priv, index, count);
  593. if (!map)
  594. goto unlock_out;
  595. if (use_ptemod && map->vma)
  596. goto unlock_out;
  597. if (use_ptemod && priv->mm != vma->vm_mm) {
  598. printk(KERN_WARNING "Huh? Other mm?\n");
  599. goto unlock_out;
  600. }
  601. atomic_inc(&map->users);
  602. vma->vm_ops = &gntdev_vmops;
  603. vma->vm_flags |= VM_RESERVED|VM_DONTEXPAND;
  604. if (use_ptemod)
  605. vma->vm_flags |= VM_DONTCOPY;
  606. vma->vm_private_data = map;
  607. if (use_ptemod)
  608. map->vma = vma;
  609. if (map->flags) {
  610. if ((vma->vm_flags & VM_WRITE) &&
  611. (map->flags & GNTMAP_readonly))
  612. goto out_unlock_put;
  613. } else {
  614. map->flags = GNTMAP_host_map;
  615. if (!(vma->vm_flags & VM_WRITE))
  616. map->flags |= GNTMAP_readonly;
  617. }
  618. spin_unlock(&priv->lock);
  619. if (use_ptemod) {
  620. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  621. vma->vm_end - vma->vm_start,
  622. find_grant_ptes, map);
  623. if (err) {
  624. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  625. goto out_put_map;
  626. }
  627. }
  628. err = map_grant_pages(map);
  629. if (err)
  630. goto out_put_map;
  631. if (!use_ptemod) {
  632. for (i = 0; i < count; i++) {
  633. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  634. map->pages[i]);
  635. if (err)
  636. goto out_put_map;
  637. }
  638. }
  639. return 0;
  640. unlock_out:
  641. spin_unlock(&priv->lock);
  642. return err;
  643. out_unlock_put:
  644. spin_unlock(&priv->lock);
  645. out_put_map:
  646. if (use_ptemod)
  647. map->vma = NULL;
  648. gntdev_put_map(map);
  649. return err;
  650. }
  651. static const struct file_operations gntdev_fops = {
  652. .owner = THIS_MODULE,
  653. .open = gntdev_open,
  654. .release = gntdev_release,
  655. .mmap = gntdev_mmap,
  656. .unlocked_ioctl = gntdev_ioctl
  657. };
  658. static struct miscdevice gntdev_miscdev = {
  659. .minor = MISC_DYNAMIC_MINOR,
  660. .name = "xen/gntdev",
  661. .fops = &gntdev_fops,
  662. };
  663. /* ------------------------------------------------------------------ */
  664. static int __init gntdev_init(void)
  665. {
  666. int err;
  667. if (!xen_domain())
  668. return -ENODEV;
  669. use_ptemod = xen_pv_domain();
  670. err = misc_register(&gntdev_miscdev);
  671. if (err != 0) {
  672. printk(KERN_ERR "Could not register gntdev device\n");
  673. return err;
  674. }
  675. return 0;
  676. }
  677. static void __exit gntdev_exit(void)
  678. {
  679. misc_deregister(&gntdev_miscdev);
  680. }
  681. module_init(gntdev_init);
  682. module_exit(gntdev_exit);
  683. /* ------------------------------------------------------------------ */