gntdev.c 18 KB

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