gntdev.c 18 KB

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