gntdev.c 18 KB

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