gntdev.c 18 KB

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