gntdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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, map->pages + offset,
  273. pages, true);
  274. if (err)
  275. return err;
  276. for (i = 0; i < pages; i++) {
  277. if (map->unmap_ops[offset+i].status)
  278. err = -EINVAL;
  279. pr_debug("unmap handle=%d st=%d\n",
  280. map->unmap_ops[offset+i].handle,
  281. map->unmap_ops[offset+i].status);
  282. map->unmap_ops[offset+i].handle = -1;
  283. }
  284. return err;
  285. }
  286. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  287. {
  288. int range, err = 0;
  289. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  290. /* It is possible the requested range will have a "hole" where we
  291. * already unmapped some of the grants. Only unmap valid ranges.
  292. */
  293. while (pages && !err) {
  294. while (pages && map->unmap_ops[offset].handle == -1) {
  295. offset++;
  296. pages--;
  297. }
  298. range = 0;
  299. while (range < pages) {
  300. if (map->unmap_ops[offset+range].handle == -1) {
  301. range--;
  302. break;
  303. }
  304. range++;
  305. }
  306. err = __unmap_grant_pages(map, offset, range);
  307. offset += range;
  308. pages -= range;
  309. }
  310. return err;
  311. }
  312. /* ------------------------------------------------------------------ */
  313. static void gntdev_vma_open(struct vm_area_struct *vma)
  314. {
  315. struct grant_map *map = vma->vm_private_data;
  316. pr_debug("gntdev_vma_open %p\n", vma);
  317. atomic_inc(&map->users);
  318. }
  319. static void gntdev_vma_close(struct vm_area_struct *vma)
  320. {
  321. struct grant_map *map = vma->vm_private_data;
  322. pr_debug("gntdev_vma_close %p\n", vma);
  323. map->vma = NULL;
  324. vma->vm_private_data = NULL;
  325. gntdev_put_map(map);
  326. }
  327. static struct vm_operations_struct gntdev_vmops = {
  328. .open = gntdev_vma_open,
  329. .close = gntdev_vma_close,
  330. };
  331. /* ------------------------------------------------------------------ */
  332. static void mn_invl_range_start(struct mmu_notifier *mn,
  333. struct mm_struct *mm,
  334. unsigned long start, unsigned long end)
  335. {
  336. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  337. struct grant_map *map;
  338. unsigned long mstart, mend;
  339. int err;
  340. spin_lock(&priv->lock);
  341. list_for_each_entry(map, &priv->maps, next) {
  342. if (!map->vma)
  343. continue;
  344. if (map->vma->vm_start >= end)
  345. continue;
  346. if (map->vma->vm_end <= start)
  347. continue;
  348. mstart = max(start, map->vma->vm_start);
  349. mend = min(end, map->vma->vm_end);
  350. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  351. map->index, map->count,
  352. map->vma->vm_start, map->vma->vm_end,
  353. start, end, mstart, mend);
  354. err = unmap_grant_pages(map,
  355. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  356. (mend - mstart) >> PAGE_SHIFT);
  357. WARN_ON(err);
  358. }
  359. spin_unlock(&priv->lock);
  360. }
  361. static void mn_invl_page(struct mmu_notifier *mn,
  362. struct mm_struct *mm,
  363. unsigned long address)
  364. {
  365. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  366. }
  367. static void mn_release(struct mmu_notifier *mn,
  368. struct mm_struct *mm)
  369. {
  370. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  371. struct grant_map *map;
  372. int err;
  373. spin_lock(&priv->lock);
  374. list_for_each_entry(map, &priv->maps, next) {
  375. if (!map->vma)
  376. continue;
  377. pr_debug("map %d+%d (%lx %lx)\n",
  378. map->index, map->count,
  379. map->vma->vm_start, map->vma->vm_end);
  380. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  381. WARN_ON(err);
  382. }
  383. spin_unlock(&priv->lock);
  384. }
  385. struct mmu_notifier_ops gntdev_mmu_ops = {
  386. .release = mn_release,
  387. .invalidate_page = mn_invl_page,
  388. .invalidate_range_start = mn_invl_range_start,
  389. };
  390. /* ------------------------------------------------------------------ */
  391. static int gntdev_open(struct inode *inode, struct file *flip)
  392. {
  393. struct gntdev_priv *priv;
  394. int ret = 0;
  395. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  396. if (!priv)
  397. return -ENOMEM;
  398. INIT_LIST_HEAD(&priv->maps);
  399. spin_lock_init(&priv->lock);
  400. if (use_ptemod) {
  401. priv->mm = get_task_mm(current);
  402. if (!priv->mm) {
  403. kfree(priv);
  404. return -ENOMEM;
  405. }
  406. priv->mn.ops = &gntdev_mmu_ops;
  407. ret = mmu_notifier_register(&priv->mn, priv->mm);
  408. mmput(priv->mm);
  409. }
  410. if (ret) {
  411. kfree(priv);
  412. return ret;
  413. }
  414. flip->private_data = priv;
  415. pr_debug("priv %p\n", priv);
  416. return 0;
  417. }
  418. static int gntdev_release(struct inode *inode, struct file *flip)
  419. {
  420. struct gntdev_priv *priv = flip->private_data;
  421. struct grant_map *map;
  422. pr_debug("priv %p\n", priv);
  423. while (!list_empty(&priv->maps)) {
  424. map = list_entry(priv->maps.next, struct grant_map, next);
  425. list_del(&map->next);
  426. gntdev_put_map(map);
  427. }
  428. if (use_ptemod)
  429. mmu_notifier_unregister(&priv->mn, priv->mm);
  430. kfree(priv);
  431. return 0;
  432. }
  433. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  434. struct ioctl_gntdev_map_grant_ref __user *u)
  435. {
  436. struct ioctl_gntdev_map_grant_ref op;
  437. struct grant_map *map;
  438. int err;
  439. if (copy_from_user(&op, u, sizeof(op)) != 0)
  440. return -EFAULT;
  441. pr_debug("priv %p, add %d\n", priv, op.count);
  442. if (unlikely(op.count <= 0))
  443. return -EINVAL;
  444. err = -ENOMEM;
  445. map = gntdev_alloc_map(priv, op.count);
  446. if (!map)
  447. return err;
  448. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  449. pr_debug("can't map: over limit\n");
  450. gntdev_put_map(map);
  451. return err;
  452. }
  453. if (copy_from_user(map->grants, &u->refs,
  454. sizeof(map->grants[0]) * op.count) != 0) {
  455. gntdev_put_map(map);
  456. return err;
  457. }
  458. spin_lock(&priv->lock);
  459. gntdev_add_map(priv, map);
  460. op.index = map->index << PAGE_SHIFT;
  461. spin_unlock(&priv->lock);
  462. if (copy_to_user(u, &op, sizeof(op)) != 0)
  463. return -EFAULT;
  464. return 0;
  465. }
  466. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  467. struct ioctl_gntdev_unmap_grant_ref __user *u)
  468. {
  469. struct ioctl_gntdev_unmap_grant_ref op;
  470. struct grant_map *map;
  471. int err = -ENOENT;
  472. if (copy_from_user(&op, u, sizeof(op)) != 0)
  473. return -EFAULT;
  474. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  475. spin_lock(&priv->lock);
  476. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  477. if (map) {
  478. list_del(&map->next);
  479. err = 0;
  480. }
  481. spin_unlock(&priv->lock);
  482. if (map)
  483. gntdev_put_map(map);
  484. return err;
  485. }
  486. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  487. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  488. {
  489. struct ioctl_gntdev_get_offset_for_vaddr op;
  490. struct vm_area_struct *vma;
  491. struct grant_map *map;
  492. if (copy_from_user(&op, u, sizeof(op)) != 0)
  493. return -EFAULT;
  494. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  495. vma = find_vma(current->mm, op.vaddr);
  496. if (!vma || vma->vm_ops != &gntdev_vmops)
  497. return -EINVAL;
  498. map = vma->vm_private_data;
  499. if (!map)
  500. return -EINVAL;
  501. op.offset = map->index << PAGE_SHIFT;
  502. op.count = map->count;
  503. if (copy_to_user(u, &op, sizeof(op)) != 0)
  504. return -EFAULT;
  505. return 0;
  506. }
  507. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  508. {
  509. struct ioctl_gntdev_unmap_notify op;
  510. struct grant_map *map;
  511. int rc;
  512. int out_flags;
  513. unsigned int out_event;
  514. if (copy_from_user(&op, u, sizeof(op)))
  515. return -EFAULT;
  516. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  517. return -EINVAL;
  518. /* We need to grab a reference to the event channel we are going to use
  519. * to send the notify before releasing the reference we may already have
  520. * (if someone has called this ioctl twice). This is required so that
  521. * it is possible to change the clear_byte part of the notification
  522. * without disturbing the event channel part, which may now be the last
  523. * reference to that event channel.
  524. */
  525. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  526. if (evtchn_get(op.event_channel_port))
  527. return -EINVAL;
  528. }
  529. out_flags = op.action;
  530. out_event = op.event_channel_port;
  531. spin_lock(&priv->lock);
  532. list_for_each_entry(map, &priv->maps, next) {
  533. uint64_t begin = map->index << PAGE_SHIFT;
  534. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  535. if (op.index >= begin && op.index < end)
  536. goto found;
  537. }
  538. rc = -ENOENT;
  539. goto unlock_out;
  540. found:
  541. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  542. (map->flags & GNTMAP_readonly)) {
  543. rc = -EINVAL;
  544. goto unlock_out;
  545. }
  546. out_flags = map->notify.flags;
  547. out_event = map->notify.event;
  548. map->notify.flags = op.action;
  549. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  550. map->notify.event = op.event_channel_port;
  551. rc = 0;
  552. unlock_out:
  553. spin_unlock(&priv->lock);
  554. /* Drop the reference to the event channel we did not save in the map */
  555. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  556. evtchn_put(out_event);
  557. return rc;
  558. }
  559. static long gntdev_ioctl(struct file *flip,
  560. unsigned int cmd, unsigned long arg)
  561. {
  562. struct gntdev_priv *priv = flip->private_data;
  563. void __user *ptr = (void __user *)arg;
  564. switch (cmd) {
  565. case IOCTL_GNTDEV_MAP_GRANT_REF:
  566. return gntdev_ioctl_map_grant_ref(priv, ptr);
  567. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  568. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  569. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  570. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  571. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  572. return gntdev_ioctl_notify(priv, ptr);
  573. default:
  574. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  575. return -ENOIOCTLCMD;
  576. }
  577. return 0;
  578. }
  579. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  580. {
  581. struct gntdev_priv *priv = flip->private_data;
  582. int index = vma->vm_pgoff;
  583. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  584. struct grant_map *map;
  585. int i, err = -EINVAL;
  586. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  587. return -EINVAL;
  588. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  589. index, count, vma->vm_start, vma->vm_pgoff);
  590. spin_lock(&priv->lock);
  591. map = gntdev_find_map_index(priv, index, count);
  592. if (!map)
  593. goto unlock_out;
  594. if (use_ptemod && map->vma)
  595. goto unlock_out;
  596. if (use_ptemod && priv->mm != vma->vm_mm) {
  597. printk(KERN_WARNING "Huh? Other mm?\n");
  598. goto unlock_out;
  599. }
  600. atomic_inc(&map->users);
  601. vma->vm_ops = &gntdev_vmops;
  602. vma->vm_flags |= VM_RESERVED|VM_DONTEXPAND;
  603. if (use_ptemod)
  604. vma->vm_flags |= VM_DONTCOPY|VM_PFNMAP;
  605. vma->vm_private_data = map;
  606. if (use_ptemod)
  607. map->vma = vma;
  608. if (map->flags) {
  609. if ((vma->vm_flags & VM_WRITE) &&
  610. (map->flags & GNTMAP_readonly))
  611. goto out_unlock_put;
  612. } else {
  613. map->flags = GNTMAP_host_map;
  614. if (!(vma->vm_flags & VM_WRITE))
  615. map->flags |= GNTMAP_readonly;
  616. }
  617. spin_unlock(&priv->lock);
  618. if (use_ptemod) {
  619. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  620. vma->vm_end - vma->vm_start,
  621. find_grant_ptes, map);
  622. if (err) {
  623. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  624. goto out_put_map;
  625. }
  626. }
  627. err = map_grant_pages(map);
  628. if (err)
  629. goto out_put_map;
  630. if (!use_ptemod) {
  631. for (i = 0; i < count; i++) {
  632. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  633. map->pages[i]);
  634. if (err)
  635. goto out_put_map;
  636. }
  637. }
  638. return 0;
  639. unlock_out:
  640. spin_unlock(&priv->lock);
  641. return err;
  642. out_unlock_put:
  643. spin_unlock(&priv->lock);
  644. out_put_map:
  645. if (use_ptemod)
  646. map->vma = NULL;
  647. gntdev_put_map(map);
  648. return err;
  649. }
  650. static const struct file_operations gntdev_fops = {
  651. .owner = THIS_MODULE,
  652. .open = gntdev_open,
  653. .release = gntdev_release,
  654. .mmap = gntdev_mmap,
  655. .unlocked_ioctl = gntdev_ioctl
  656. };
  657. static struct miscdevice gntdev_miscdev = {
  658. .minor = MISC_DYNAMIC_MINOR,
  659. .name = "xen/gntdev",
  660. .fops = &gntdev_fops,
  661. };
  662. /* ------------------------------------------------------------------ */
  663. static int __init gntdev_init(void)
  664. {
  665. int err;
  666. if (!xen_domain())
  667. return -ENODEV;
  668. use_ptemod = xen_pv_domain();
  669. err = misc_register(&gntdev_miscdev);
  670. if (err != 0) {
  671. printk(KERN_ERR "Could not register gntdev device\n");
  672. return err;
  673. }
  674. return 0;
  675. }
  676. static void __exit gntdev_exit(void)
  677. {
  678. misc_deregister(&gntdev_miscdev);
  679. }
  680. module_init(gntdev_init);
  681. module_exit(gntdev_exit);
  682. /* ------------------------------------------------------------------ */