gntdev.c 18 KB

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