gntdev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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 = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
  101. add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
  102. add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
  103. add->kmap_ops = kzalloc(sizeof(add->kmap_ops[0]) * count, GFP_KERNEL);
  104. add->pages = kzalloc(sizeof(add->pages[0]) * count, 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. if (map->pages) {
  168. if (!use_ptemod)
  169. unmap_grant_pages(map, 0, map->count);
  170. free_xenballooned_pages(map->count, map->pages);
  171. }
  172. kfree(map->pages);
  173. kfree(map->grants);
  174. kfree(map->map_ops);
  175. kfree(map->unmap_ops);
  176. kfree(map);
  177. }
  178. /* ------------------------------------------------------------------ */
  179. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  180. unsigned long addr, void *data)
  181. {
  182. struct grant_map *map = data;
  183. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  184. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  185. u64 pte_maddr;
  186. BUG_ON(pgnr >= map->count);
  187. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  188. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  189. map->grants[pgnr].ref,
  190. map->grants[pgnr].domid);
  191. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  192. -1 /* handle */);
  193. return 0;
  194. }
  195. static int map_grant_pages(struct grant_map *map)
  196. {
  197. int i, err = 0;
  198. if (!use_ptemod) {
  199. /* Note: it could already be mapped */
  200. if (map->map_ops[0].handle != -1)
  201. return 0;
  202. for (i = 0; i < map->count; i++) {
  203. unsigned long addr = (unsigned long)
  204. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  205. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  206. map->grants[i].ref,
  207. map->grants[i].domid);
  208. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  209. map->flags, -1 /* handle */);
  210. }
  211. } else {
  212. /*
  213. * Setup the map_ops corresponding to the pte entries pointing
  214. * to the kernel linear addresses of the struct pages.
  215. * These ptes are completely different from the user ptes dealt
  216. * with find_grant_ptes.
  217. */
  218. for (i = 0; i < map->count; i++) {
  219. unsigned level;
  220. unsigned long address = (unsigned long)
  221. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  222. pte_t *ptep;
  223. u64 pte_maddr = 0;
  224. BUG_ON(PageHighMem(map->pages[i]));
  225. ptep = lookup_address(address, &level);
  226. pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
  227. gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
  228. map->flags |
  229. GNTMAP_host_map |
  230. GNTMAP_contains_pte,
  231. map->grants[i].ref,
  232. map->grants[i].domid);
  233. }
  234. }
  235. pr_debug("map %d+%d\n", map->index, map->count);
  236. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  237. map->pages, map->count);
  238. if (err)
  239. return err;
  240. for (i = 0; i < map->count; i++) {
  241. if (map->map_ops[i].status)
  242. err = -EINVAL;
  243. else {
  244. BUG_ON(map->map_ops[i].handle == -1);
  245. map->unmap_ops[i].handle = map->map_ops[i].handle;
  246. pr_debug("map handle=%d\n", map->map_ops[i].handle);
  247. }
  248. }
  249. return err;
  250. }
  251. static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
  252. {
  253. int i, err = 0;
  254. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  255. int pgno = (map->notify.addr >> PAGE_SHIFT);
  256. if (pgno >= offset && pgno < offset + pages && use_ptemod) {
  257. void __user *tmp = (void __user *)
  258. map->vma->vm_start + map->notify.addr;
  259. err = copy_to_user(tmp, &err, 1);
  260. if (err)
  261. return -EFAULT;
  262. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  263. } else if (pgno >= offset && pgno < offset + pages) {
  264. uint8_t *tmp = kmap(map->pages[pgno]);
  265. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  266. kunmap(map->pages[pgno]);
  267. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  268. }
  269. }
  270. err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages);
  271. if (err)
  272. return err;
  273. for (i = 0; i < pages; i++) {
  274. if (map->unmap_ops[offset+i].status)
  275. err = -EINVAL;
  276. pr_debug("unmap handle=%d st=%d\n",
  277. map->unmap_ops[offset+i].handle,
  278. map->unmap_ops[offset+i].status);
  279. map->unmap_ops[offset+i].handle = -1;
  280. }
  281. return err;
  282. }
  283. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  284. {
  285. int range, err = 0;
  286. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  287. /* It is possible the requested range will have a "hole" where we
  288. * already unmapped some of the grants. Only unmap valid ranges.
  289. */
  290. while (pages && !err) {
  291. while (pages && map->unmap_ops[offset].handle == -1) {
  292. offset++;
  293. pages--;
  294. }
  295. range = 0;
  296. while (range < pages) {
  297. if (map->unmap_ops[offset+range].handle == -1) {
  298. range--;
  299. break;
  300. }
  301. range++;
  302. }
  303. err = __unmap_grant_pages(map, offset, range);
  304. offset += range;
  305. pages -= range;
  306. }
  307. return err;
  308. }
  309. /* ------------------------------------------------------------------ */
  310. static void gntdev_vma_open(struct vm_area_struct *vma)
  311. {
  312. struct grant_map *map = vma->vm_private_data;
  313. pr_debug("gntdev_vma_open %p\n", vma);
  314. atomic_inc(&map->users);
  315. }
  316. static void gntdev_vma_close(struct vm_area_struct *vma)
  317. {
  318. struct grant_map *map = vma->vm_private_data;
  319. pr_debug("gntdev_vma_close %p\n", vma);
  320. map->vma = NULL;
  321. vma->vm_private_data = NULL;
  322. gntdev_put_map(map);
  323. }
  324. static struct vm_operations_struct gntdev_vmops = {
  325. .open = gntdev_vma_open,
  326. .close = gntdev_vma_close,
  327. };
  328. /* ------------------------------------------------------------------ */
  329. static void mn_invl_range_start(struct mmu_notifier *mn,
  330. struct mm_struct *mm,
  331. unsigned long start, unsigned long end)
  332. {
  333. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  334. struct grant_map *map;
  335. unsigned long mstart, mend;
  336. int err;
  337. spin_lock(&priv->lock);
  338. list_for_each_entry(map, &priv->maps, next) {
  339. if (!map->vma)
  340. continue;
  341. if (map->vma->vm_start >= end)
  342. continue;
  343. if (map->vma->vm_end <= start)
  344. continue;
  345. mstart = max(start, map->vma->vm_start);
  346. mend = min(end, map->vma->vm_end);
  347. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  348. map->index, map->count,
  349. map->vma->vm_start, map->vma->vm_end,
  350. start, end, mstart, mend);
  351. err = unmap_grant_pages(map,
  352. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  353. (mend - mstart) >> PAGE_SHIFT);
  354. WARN_ON(err);
  355. }
  356. spin_unlock(&priv->lock);
  357. }
  358. static void mn_invl_page(struct mmu_notifier *mn,
  359. struct mm_struct *mm,
  360. unsigned long address)
  361. {
  362. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  363. }
  364. static void mn_release(struct mmu_notifier *mn,
  365. struct mm_struct *mm)
  366. {
  367. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  368. struct grant_map *map;
  369. int err;
  370. spin_lock(&priv->lock);
  371. list_for_each_entry(map, &priv->maps, next) {
  372. if (!map->vma)
  373. continue;
  374. pr_debug("map %d+%d (%lx %lx)\n",
  375. map->index, map->count,
  376. map->vma->vm_start, map->vma->vm_end);
  377. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  378. WARN_ON(err);
  379. }
  380. spin_unlock(&priv->lock);
  381. }
  382. struct mmu_notifier_ops gntdev_mmu_ops = {
  383. .release = mn_release,
  384. .invalidate_page = mn_invl_page,
  385. .invalidate_range_start = mn_invl_range_start,
  386. };
  387. /* ------------------------------------------------------------------ */
  388. static int gntdev_open(struct inode *inode, struct file *flip)
  389. {
  390. struct gntdev_priv *priv;
  391. int ret = 0;
  392. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  393. if (!priv)
  394. return -ENOMEM;
  395. INIT_LIST_HEAD(&priv->maps);
  396. spin_lock_init(&priv->lock);
  397. if (use_ptemod) {
  398. priv->mm = get_task_mm(current);
  399. if (!priv->mm) {
  400. kfree(priv);
  401. return -ENOMEM;
  402. }
  403. priv->mn.ops = &gntdev_mmu_ops;
  404. ret = mmu_notifier_register(&priv->mn, priv->mm);
  405. mmput(priv->mm);
  406. }
  407. if (ret) {
  408. kfree(priv);
  409. return ret;
  410. }
  411. flip->private_data = priv;
  412. pr_debug("priv %p\n", priv);
  413. return 0;
  414. }
  415. static int gntdev_release(struct inode *inode, struct file *flip)
  416. {
  417. struct gntdev_priv *priv = flip->private_data;
  418. struct grant_map *map;
  419. pr_debug("priv %p\n", priv);
  420. while (!list_empty(&priv->maps)) {
  421. map = list_entry(priv->maps.next, struct grant_map, next);
  422. list_del(&map->next);
  423. gntdev_put_map(map);
  424. }
  425. if (use_ptemod)
  426. mmu_notifier_unregister(&priv->mn, priv->mm);
  427. kfree(priv);
  428. return 0;
  429. }
  430. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  431. struct ioctl_gntdev_map_grant_ref __user *u)
  432. {
  433. struct ioctl_gntdev_map_grant_ref op;
  434. struct grant_map *map;
  435. int err;
  436. if (copy_from_user(&op, u, sizeof(op)) != 0)
  437. return -EFAULT;
  438. pr_debug("priv %p, add %d\n", priv, op.count);
  439. if (unlikely(op.count <= 0))
  440. return -EINVAL;
  441. err = -ENOMEM;
  442. map = gntdev_alloc_map(priv, op.count);
  443. if (!map)
  444. return err;
  445. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  446. pr_debug("can't map: over limit\n");
  447. gntdev_put_map(map);
  448. return err;
  449. }
  450. if (copy_from_user(map->grants, &u->refs,
  451. sizeof(map->grants[0]) * op.count) != 0) {
  452. gntdev_put_map(map);
  453. return err;
  454. }
  455. spin_lock(&priv->lock);
  456. gntdev_add_map(priv, map);
  457. op.index = map->index << PAGE_SHIFT;
  458. spin_unlock(&priv->lock);
  459. if (copy_to_user(u, &op, sizeof(op)) != 0)
  460. return -EFAULT;
  461. return 0;
  462. }
  463. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  464. struct ioctl_gntdev_unmap_grant_ref __user *u)
  465. {
  466. struct ioctl_gntdev_unmap_grant_ref op;
  467. struct grant_map *map;
  468. int err = -ENOENT;
  469. if (copy_from_user(&op, u, sizeof(op)) != 0)
  470. return -EFAULT;
  471. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  472. spin_lock(&priv->lock);
  473. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  474. if (map) {
  475. list_del(&map->next);
  476. err = 0;
  477. }
  478. spin_unlock(&priv->lock);
  479. if (map)
  480. gntdev_put_map(map);
  481. return err;
  482. }
  483. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  484. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  485. {
  486. struct ioctl_gntdev_get_offset_for_vaddr op;
  487. struct vm_area_struct *vma;
  488. struct grant_map *map;
  489. if (copy_from_user(&op, u, sizeof(op)) != 0)
  490. return -EFAULT;
  491. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  492. vma = find_vma(current->mm, op.vaddr);
  493. if (!vma || vma->vm_ops != &gntdev_vmops)
  494. return -EINVAL;
  495. map = vma->vm_private_data;
  496. if (!map)
  497. return -EINVAL;
  498. op.offset = map->index << PAGE_SHIFT;
  499. op.count = map->count;
  500. if (copy_to_user(u, &op, sizeof(op)) != 0)
  501. return -EFAULT;
  502. return 0;
  503. }
  504. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  505. {
  506. struct ioctl_gntdev_unmap_notify op;
  507. struct grant_map *map;
  508. int rc;
  509. if (copy_from_user(&op, u, sizeof(op)))
  510. return -EFAULT;
  511. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  512. return -EINVAL;
  513. spin_lock(&priv->lock);
  514. list_for_each_entry(map, &priv->maps, next) {
  515. uint64_t begin = map->index << PAGE_SHIFT;
  516. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  517. if (op.index >= begin && op.index < end)
  518. goto found;
  519. }
  520. rc = -ENOENT;
  521. goto unlock_out;
  522. found:
  523. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  524. (map->flags & GNTMAP_readonly)) {
  525. rc = -EINVAL;
  526. goto unlock_out;
  527. }
  528. map->notify.flags = op.action;
  529. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  530. map->notify.event = op.event_channel_port;
  531. rc = 0;
  532. unlock_out:
  533. spin_unlock(&priv->lock);
  534. return rc;
  535. }
  536. static long gntdev_ioctl(struct file *flip,
  537. unsigned int cmd, unsigned long arg)
  538. {
  539. struct gntdev_priv *priv = flip->private_data;
  540. void __user *ptr = (void __user *)arg;
  541. switch (cmd) {
  542. case IOCTL_GNTDEV_MAP_GRANT_REF:
  543. return gntdev_ioctl_map_grant_ref(priv, ptr);
  544. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  545. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  546. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  547. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  548. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  549. return gntdev_ioctl_notify(priv, ptr);
  550. default:
  551. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  552. return -ENOIOCTLCMD;
  553. }
  554. return 0;
  555. }
  556. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  557. {
  558. struct gntdev_priv *priv = flip->private_data;
  559. int index = vma->vm_pgoff;
  560. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  561. struct grant_map *map;
  562. int i, err = -EINVAL;
  563. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  564. return -EINVAL;
  565. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  566. index, count, vma->vm_start, vma->vm_pgoff);
  567. spin_lock(&priv->lock);
  568. map = gntdev_find_map_index(priv, index, count);
  569. if (!map)
  570. goto unlock_out;
  571. if (use_ptemod && map->vma)
  572. goto unlock_out;
  573. if (use_ptemod && priv->mm != vma->vm_mm) {
  574. printk(KERN_WARNING "Huh? Other mm?\n");
  575. goto unlock_out;
  576. }
  577. atomic_inc(&map->users);
  578. vma->vm_ops = &gntdev_vmops;
  579. vma->vm_flags |= VM_RESERVED|VM_DONTEXPAND;
  580. if (use_ptemod)
  581. vma->vm_flags |= VM_DONTCOPY|VM_PFNMAP;
  582. vma->vm_private_data = map;
  583. if (use_ptemod)
  584. map->vma = vma;
  585. if (map->flags) {
  586. if ((vma->vm_flags & VM_WRITE) &&
  587. (map->flags & GNTMAP_readonly))
  588. goto out_unlock_put;
  589. } else {
  590. map->flags = GNTMAP_host_map;
  591. if (!(vma->vm_flags & VM_WRITE))
  592. map->flags |= GNTMAP_readonly;
  593. }
  594. spin_unlock(&priv->lock);
  595. if (use_ptemod) {
  596. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  597. vma->vm_end - vma->vm_start,
  598. find_grant_ptes, map);
  599. if (err) {
  600. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  601. goto out_put_map;
  602. }
  603. }
  604. err = map_grant_pages(map);
  605. if (err)
  606. goto out_put_map;
  607. if (!use_ptemod) {
  608. for (i = 0; i < count; i++) {
  609. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  610. map->pages[i]);
  611. if (err)
  612. goto out_put_map;
  613. }
  614. }
  615. return 0;
  616. unlock_out:
  617. spin_unlock(&priv->lock);
  618. return err;
  619. out_unlock_put:
  620. spin_unlock(&priv->lock);
  621. out_put_map:
  622. if (use_ptemod)
  623. map->vma = NULL;
  624. gntdev_put_map(map);
  625. return err;
  626. }
  627. static const struct file_operations gntdev_fops = {
  628. .owner = THIS_MODULE,
  629. .open = gntdev_open,
  630. .release = gntdev_release,
  631. .mmap = gntdev_mmap,
  632. .unlocked_ioctl = gntdev_ioctl
  633. };
  634. static struct miscdevice gntdev_miscdev = {
  635. .minor = MISC_DYNAMIC_MINOR,
  636. .name = "xen/gntdev",
  637. .fops = &gntdev_fops,
  638. };
  639. /* ------------------------------------------------------------------ */
  640. static int __init gntdev_init(void)
  641. {
  642. int err;
  643. if (!xen_domain())
  644. return -ENODEV;
  645. use_ptemod = xen_pv_domain();
  646. err = misc_register(&gntdev_miscdev);
  647. if (err != 0) {
  648. printk(KERN_ERR "Could not register gntdev device\n");
  649. return err;
  650. }
  651. return 0;
  652. }
  653. static void __exit gntdev_exit(void)
  654. {
  655. misc_deregister(&gntdev_miscdev);
  656. }
  657. module_init(gntdev_init);
  658. module_exit(gntdev_exit);
  659. /* ------------------------------------------------------------------ */