gntdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. #define populate_freeable_maps use_ptemod
  53. struct gntdev_priv {
  54. /* maps with visible offsets in the file descriptor */
  55. struct list_head maps;
  56. /* maps that are not visible; will be freed on munmap.
  57. * Only populated if populate_freeable_maps == 1 */
  58. struct list_head freeable_maps;
  59. /* lock protects maps and freeable_maps */
  60. spinlock_t lock;
  61. struct mm_struct *mm;
  62. struct mmu_notifier mn;
  63. };
  64. struct unmap_notify {
  65. int flags;
  66. /* Address relative to the start of the grant_map */
  67. int addr;
  68. int event;
  69. };
  70. struct grant_map {
  71. struct list_head next;
  72. struct vm_area_struct *vma;
  73. int index;
  74. int count;
  75. int flags;
  76. atomic_t users;
  77. struct unmap_notify notify;
  78. struct ioctl_gntdev_grant_ref *grants;
  79. struct gnttab_map_grant_ref *map_ops;
  80. struct gnttab_unmap_grant_ref *unmap_ops;
  81. struct gnttab_map_grant_ref *kmap_ops;
  82. struct page **pages;
  83. };
  84. static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
  85. /* ------------------------------------------------------------------ */
  86. static void gntdev_print_maps(struct gntdev_priv *priv,
  87. char *text, int text_index)
  88. {
  89. #ifdef DEBUG
  90. struct grant_map *map;
  91. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  92. list_for_each_entry(map, &priv->maps, next)
  93. pr_debug(" index %2d, count %2d %s\n",
  94. map->index, map->count,
  95. map->index == text_index && text ? text : "");
  96. #endif
  97. }
  98. static void gntdev_free_map(struct grant_map *map)
  99. {
  100. if (map == NULL)
  101. return;
  102. if (map->pages)
  103. free_xenballooned_pages(map->count, map->pages);
  104. kfree(map->pages);
  105. kfree(map->grants);
  106. kfree(map->map_ops);
  107. kfree(map->unmap_ops);
  108. kfree(map->kmap_ops);
  109. kfree(map);
  110. }
  111. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  112. {
  113. struct grant_map *add;
  114. int i;
  115. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  116. if (NULL == add)
  117. return NULL;
  118. add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
  119. add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
  120. add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
  121. add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
  122. add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
  123. if (NULL == add->grants ||
  124. NULL == add->map_ops ||
  125. NULL == add->unmap_ops ||
  126. NULL == add->kmap_ops ||
  127. NULL == add->pages)
  128. goto err;
  129. if (alloc_xenballooned_pages(count, add->pages, false /* lowmem */))
  130. goto err;
  131. for (i = 0; i < count; i++) {
  132. add->map_ops[i].handle = -1;
  133. add->unmap_ops[i].handle = -1;
  134. add->kmap_ops[i].handle = -1;
  135. }
  136. add->index = 0;
  137. add->count = count;
  138. atomic_set(&add->users, 1);
  139. return add;
  140. err:
  141. gntdev_free_map(add);
  142. return NULL;
  143. }
  144. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  145. {
  146. struct grant_map *map;
  147. list_for_each_entry(map, &priv->maps, next) {
  148. if (add->index + add->count < map->index) {
  149. list_add_tail(&add->next, &map->next);
  150. goto done;
  151. }
  152. add->index = map->index + map->count;
  153. }
  154. list_add_tail(&add->next, &priv->maps);
  155. done:
  156. gntdev_print_maps(priv, "[new]", add->index);
  157. }
  158. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  159. int index, int count)
  160. {
  161. struct grant_map *map;
  162. list_for_each_entry(map, &priv->maps, next) {
  163. if (map->index != index)
  164. continue;
  165. if (count && map->count != count)
  166. continue;
  167. return map;
  168. }
  169. return NULL;
  170. }
  171. static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
  172. {
  173. if (!map)
  174. return;
  175. if (!atomic_dec_and_test(&map->users))
  176. return;
  177. atomic_sub(map->count, &pages_mapped);
  178. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  179. notify_remote_via_evtchn(map->notify.event);
  180. evtchn_put(map->notify.event);
  181. }
  182. if (populate_freeable_maps && priv) {
  183. spin_lock(&priv->lock);
  184. list_del(&map->next);
  185. spin_unlock(&priv->lock);
  186. }
  187. if (map->pages && !use_ptemod)
  188. unmap_grant_pages(map, 0, map->count);
  189. gntdev_free_map(map);
  190. }
  191. /* ------------------------------------------------------------------ */
  192. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  193. unsigned long addr, void *data)
  194. {
  195. struct grant_map *map = data;
  196. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  197. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  198. u64 pte_maddr;
  199. BUG_ON(pgnr >= map->count);
  200. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  201. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  202. map->grants[pgnr].ref,
  203. map->grants[pgnr].domid);
  204. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  205. -1 /* handle */);
  206. return 0;
  207. }
  208. static int map_grant_pages(struct grant_map *map)
  209. {
  210. int i, err = 0;
  211. if (!use_ptemod) {
  212. /* Note: it could already be mapped */
  213. if (map->map_ops[0].handle != -1)
  214. return 0;
  215. for (i = 0; i < map->count; i++) {
  216. unsigned long addr = (unsigned long)
  217. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  218. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  219. map->grants[i].ref,
  220. map->grants[i].domid);
  221. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  222. map->flags, -1 /* handle */);
  223. }
  224. } else {
  225. /*
  226. * Setup the map_ops corresponding to the pte entries pointing
  227. * to the kernel linear addresses of the struct pages.
  228. * These ptes are completely different from the user ptes dealt
  229. * with find_grant_ptes.
  230. */
  231. for (i = 0; i < map->count; i++) {
  232. unsigned level;
  233. unsigned long address = (unsigned long)
  234. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  235. pte_t *ptep;
  236. u64 pte_maddr = 0;
  237. BUG_ON(PageHighMem(map->pages[i]));
  238. ptep = lookup_address(address, &level);
  239. pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
  240. gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
  241. map->flags |
  242. GNTMAP_host_map |
  243. GNTMAP_contains_pte,
  244. map->grants[i].ref,
  245. map->grants[i].domid);
  246. }
  247. }
  248. pr_debug("map %d+%d\n", map->index, map->count);
  249. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  250. map->pages, map->count);
  251. if (err)
  252. return err;
  253. for (i = 0; i < map->count; i++) {
  254. if (map->map_ops[i].status)
  255. err = -EINVAL;
  256. else {
  257. BUG_ON(map->map_ops[i].handle == -1);
  258. map->unmap_ops[i].handle = map->map_ops[i].handle;
  259. pr_debug("map handle=%d\n", map->map_ops[i].handle);
  260. }
  261. }
  262. return err;
  263. }
  264. static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
  265. {
  266. int i, err = 0;
  267. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  268. int pgno = (map->notify.addr >> PAGE_SHIFT);
  269. if (pgno >= offset && pgno < offset + pages && use_ptemod) {
  270. void __user *tmp = (void __user *)
  271. map->vma->vm_start + map->notify.addr;
  272. err = copy_to_user(tmp, &err, 1);
  273. if (err)
  274. return -EFAULT;
  275. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  276. } else if (pgno >= offset && pgno < offset + pages) {
  277. uint8_t *tmp = kmap(map->pages[pgno]);
  278. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  279. kunmap(map->pages[pgno]);
  280. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  281. }
  282. }
  283. err = gnttab_unmap_refs(map->unmap_ops + offset,
  284. use_ptemod ? map->kmap_ops + offset : NULL, map->pages + offset,
  285. pages);
  286. if (err)
  287. return err;
  288. for (i = 0; i < pages; i++) {
  289. if (map->unmap_ops[offset+i].status)
  290. err = -EINVAL;
  291. pr_debug("unmap handle=%d st=%d\n",
  292. map->unmap_ops[offset+i].handle,
  293. map->unmap_ops[offset+i].status);
  294. map->unmap_ops[offset+i].handle = -1;
  295. }
  296. return err;
  297. }
  298. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  299. {
  300. int range, err = 0;
  301. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  302. /* It is possible the requested range will have a "hole" where we
  303. * already unmapped some of the grants. Only unmap valid ranges.
  304. */
  305. while (pages && !err) {
  306. while (pages && map->unmap_ops[offset].handle == -1) {
  307. offset++;
  308. pages--;
  309. }
  310. range = 0;
  311. while (range < pages) {
  312. if (map->unmap_ops[offset+range].handle == -1) {
  313. range--;
  314. break;
  315. }
  316. range++;
  317. }
  318. err = __unmap_grant_pages(map, offset, range);
  319. offset += range;
  320. pages -= range;
  321. }
  322. return err;
  323. }
  324. /* ------------------------------------------------------------------ */
  325. static void gntdev_vma_open(struct vm_area_struct *vma)
  326. {
  327. struct grant_map *map = vma->vm_private_data;
  328. pr_debug("gntdev_vma_open %p\n", vma);
  329. atomic_inc(&map->users);
  330. }
  331. static void gntdev_vma_close(struct vm_area_struct *vma)
  332. {
  333. struct grant_map *map = vma->vm_private_data;
  334. struct file *file = vma->vm_file;
  335. struct gntdev_priv *priv = file->private_data;
  336. pr_debug("gntdev_vma_close %p\n", vma);
  337. if (use_ptemod) {
  338. /* It is possible that an mmu notifier could be running
  339. * concurrently, so take priv->lock to ensure that the vma won't
  340. * vanishing during the unmap_grant_pages call, since we will
  341. * spin here until that completes. Such a concurrent call will
  342. * not do any unmapping, since that has been done prior to
  343. * closing the vma, but it may still iterate the unmap_ops list.
  344. */
  345. spin_lock(&priv->lock);
  346. map->vma = NULL;
  347. spin_unlock(&priv->lock);
  348. }
  349. vma->vm_private_data = NULL;
  350. gntdev_put_map(priv, map);
  351. }
  352. static struct vm_operations_struct gntdev_vmops = {
  353. .open = gntdev_vma_open,
  354. .close = gntdev_vma_close,
  355. };
  356. /* ------------------------------------------------------------------ */
  357. static void unmap_if_in_range(struct grant_map *map,
  358. unsigned long start, unsigned long end)
  359. {
  360. unsigned long mstart, mend;
  361. int err;
  362. if (!map->vma)
  363. return;
  364. if (map->vma->vm_start >= end)
  365. return;
  366. if (map->vma->vm_end <= start)
  367. return;
  368. mstart = max(start, map->vma->vm_start);
  369. mend = min(end, map->vma->vm_end);
  370. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  371. map->index, map->count,
  372. map->vma->vm_start, map->vma->vm_end,
  373. start, end, mstart, mend);
  374. err = unmap_grant_pages(map,
  375. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  376. (mend - mstart) >> PAGE_SHIFT);
  377. WARN_ON(err);
  378. }
  379. static void mn_invl_range_start(struct mmu_notifier *mn,
  380. struct mm_struct *mm,
  381. unsigned long start, unsigned long end)
  382. {
  383. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  384. struct grant_map *map;
  385. spin_lock(&priv->lock);
  386. list_for_each_entry(map, &priv->maps, next) {
  387. unmap_if_in_range(map, start, end);
  388. }
  389. list_for_each_entry(map, &priv->freeable_maps, next) {
  390. unmap_if_in_range(map, start, end);
  391. }
  392. spin_unlock(&priv->lock);
  393. }
  394. static void mn_invl_page(struct mmu_notifier *mn,
  395. struct mm_struct *mm,
  396. unsigned long address)
  397. {
  398. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  399. }
  400. static void mn_release(struct mmu_notifier *mn,
  401. struct mm_struct *mm)
  402. {
  403. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  404. struct grant_map *map;
  405. int err;
  406. spin_lock(&priv->lock);
  407. list_for_each_entry(map, &priv->maps, next) {
  408. if (!map->vma)
  409. continue;
  410. pr_debug("map %d+%d (%lx %lx)\n",
  411. map->index, map->count,
  412. map->vma->vm_start, map->vma->vm_end);
  413. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  414. WARN_ON(err);
  415. }
  416. list_for_each_entry(map, &priv->freeable_maps, next) {
  417. if (!map->vma)
  418. continue;
  419. pr_debug("map %d+%d (%lx %lx)\n",
  420. map->index, map->count,
  421. map->vma->vm_start, map->vma->vm_end);
  422. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  423. WARN_ON(err);
  424. }
  425. spin_unlock(&priv->lock);
  426. }
  427. static struct mmu_notifier_ops gntdev_mmu_ops = {
  428. .release = mn_release,
  429. .invalidate_page = mn_invl_page,
  430. .invalidate_range_start = mn_invl_range_start,
  431. };
  432. /* ------------------------------------------------------------------ */
  433. static int gntdev_open(struct inode *inode, struct file *flip)
  434. {
  435. struct gntdev_priv *priv;
  436. int ret = 0;
  437. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  438. if (!priv)
  439. return -ENOMEM;
  440. INIT_LIST_HEAD(&priv->maps);
  441. INIT_LIST_HEAD(&priv->freeable_maps);
  442. spin_lock_init(&priv->lock);
  443. if (use_ptemod) {
  444. priv->mm = get_task_mm(current);
  445. if (!priv->mm) {
  446. kfree(priv);
  447. return -ENOMEM;
  448. }
  449. priv->mn.ops = &gntdev_mmu_ops;
  450. ret = mmu_notifier_register(&priv->mn, priv->mm);
  451. mmput(priv->mm);
  452. }
  453. if (ret) {
  454. kfree(priv);
  455. return ret;
  456. }
  457. flip->private_data = priv;
  458. pr_debug("priv %p\n", priv);
  459. return 0;
  460. }
  461. static int gntdev_release(struct inode *inode, struct file *flip)
  462. {
  463. struct gntdev_priv *priv = flip->private_data;
  464. struct grant_map *map;
  465. pr_debug("priv %p\n", priv);
  466. while (!list_empty(&priv->maps)) {
  467. map = list_entry(priv->maps.next, struct grant_map, next);
  468. list_del(&map->next);
  469. gntdev_put_map(NULL /* already removed */, map);
  470. }
  471. WARN_ON(!list_empty(&priv->freeable_maps));
  472. if (use_ptemod)
  473. mmu_notifier_unregister(&priv->mn, priv->mm);
  474. kfree(priv);
  475. return 0;
  476. }
  477. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  478. struct ioctl_gntdev_map_grant_ref __user *u)
  479. {
  480. struct ioctl_gntdev_map_grant_ref op;
  481. struct grant_map *map;
  482. int err;
  483. if (copy_from_user(&op, u, sizeof(op)) != 0)
  484. return -EFAULT;
  485. pr_debug("priv %p, add %d\n", priv, op.count);
  486. if (unlikely(op.count <= 0))
  487. return -EINVAL;
  488. err = -ENOMEM;
  489. map = gntdev_alloc_map(priv, op.count);
  490. if (!map)
  491. return err;
  492. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  493. pr_debug("can't map: over limit\n");
  494. gntdev_put_map(NULL, map);
  495. return err;
  496. }
  497. if (copy_from_user(map->grants, &u->refs,
  498. sizeof(map->grants[0]) * op.count) != 0) {
  499. gntdev_put_map(NULL, map);
  500. return -EFAULT;
  501. }
  502. spin_lock(&priv->lock);
  503. gntdev_add_map(priv, map);
  504. op.index = map->index << PAGE_SHIFT;
  505. spin_unlock(&priv->lock);
  506. if (copy_to_user(u, &op, sizeof(op)) != 0)
  507. return -EFAULT;
  508. return 0;
  509. }
  510. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  511. struct ioctl_gntdev_unmap_grant_ref __user *u)
  512. {
  513. struct ioctl_gntdev_unmap_grant_ref op;
  514. struct grant_map *map;
  515. int err = -ENOENT;
  516. if (copy_from_user(&op, u, sizeof(op)) != 0)
  517. return -EFAULT;
  518. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  519. spin_lock(&priv->lock);
  520. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  521. if (map) {
  522. list_del(&map->next);
  523. if (populate_freeable_maps)
  524. list_add_tail(&map->next, &priv->freeable_maps);
  525. err = 0;
  526. }
  527. spin_unlock(&priv->lock);
  528. if (map)
  529. gntdev_put_map(priv, map);
  530. return err;
  531. }
  532. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  533. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  534. {
  535. struct ioctl_gntdev_get_offset_for_vaddr op;
  536. struct vm_area_struct *vma;
  537. struct grant_map *map;
  538. int rv = -EINVAL;
  539. if (copy_from_user(&op, u, sizeof(op)) != 0)
  540. return -EFAULT;
  541. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  542. down_read(&current->mm->mmap_sem);
  543. vma = find_vma(current->mm, op.vaddr);
  544. if (!vma || vma->vm_ops != &gntdev_vmops)
  545. goto out_unlock;
  546. map = vma->vm_private_data;
  547. if (!map)
  548. goto out_unlock;
  549. op.offset = map->index << PAGE_SHIFT;
  550. op.count = map->count;
  551. rv = 0;
  552. out_unlock:
  553. up_read(&current->mm->mmap_sem);
  554. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  555. return -EFAULT;
  556. return rv;
  557. }
  558. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  559. {
  560. struct ioctl_gntdev_unmap_notify op;
  561. struct grant_map *map;
  562. int rc;
  563. int out_flags;
  564. unsigned int out_event;
  565. if (copy_from_user(&op, u, sizeof(op)))
  566. return -EFAULT;
  567. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  568. return -EINVAL;
  569. /* We need to grab a reference to the event channel we are going to use
  570. * to send the notify before releasing the reference we may already have
  571. * (if someone has called this ioctl twice). This is required so that
  572. * it is possible to change the clear_byte part of the notification
  573. * without disturbing the event channel part, which may now be the last
  574. * reference to that event channel.
  575. */
  576. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  577. if (evtchn_get(op.event_channel_port))
  578. return -EINVAL;
  579. }
  580. out_flags = op.action;
  581. out_event = op.event_channel_port;
  582. spin_lock(&priv->lock);
  583. list_for_each_entry(map, &priv->maps, next) {
  584. uint64_t begin = map->index << PAGE_SHIFT;
  585. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  586. if (op.index >= begin && op.index < end)
  587. goto found;
  588. }
  589. rc = -ENOENT;
  590. goto unlock_out;
  591. found:
  592. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  593. (map->flags & GNTMAP_readonly)) {
  594. rc = -EINVAL;
  595. goto unlock_out;
  596. }
  597. out_flags = map->notify.flags;
  598. out_event = map->notify.event;
  599. map->notify.flags = op.action;
  600. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  601. map->notify.event = op.event_channel_port;
  602. rc = 0;
  603. unlock_out:
  604. spin_unlock(&priv->lock);
  605. /* Drop the reference to the event channel we did not save in the map */
  606. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  607. evtchn_put(out_event);
  608. return rc;
  609. }
  610. static long gntdev_ioctl(struct file *flip,
  611. unsigned int cmd, unsigned long arg)
  612. {
  613. struct gntdev_priv *priv = flip->private_data;
  614. void __user *ptr = (void __user *)arg;
  615. switch (cmd) {
  616. case IOCTL_GNTDEV_MAP_GRANT_REF:
  617. return gntdev_ioctl_map_grant_ref(priv, ptr);
  618. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  619. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  620. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  621. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  622. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  623. return gntdev_ioctl_notify(priv, ptr);
  624. default:
  625. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  626. return -ENOIOCTLCMD;
  627. }
  628. return 0;
  629. }
  630. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  631. {
  632. struct gntdev_priv *priv = flip->private_data;
  633. int index = vma->vm_pgoff;
  634. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  635. struct grant_map *map;
  636. int i, err = -EINVAL;
  637. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  638. return -EINVAL;
  639. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  640. index, count, vma->vm_start, vma->vm_pgoff);
  641. spin_lock(&priv->lock);
  642. map = gntdev_find_map_index(priv, index, count);
  643. if (!map)
  644. goto unlock_out;
  645. if (use_ptemod && map->vma)
  646. goto unlock_out;
  647. if (use_ptemod && priv->mm != vma->vm_mm) {
  648. printk(KERN_WARNING "Huh? Other mm?\n");
  649. goto unlock_out;
  650. }
  651. atomic_inc(&map->users);
  652. vma->vm_ops = &gntdev_vmops;
  653. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  654. if (use_ptemod)
  655. vma->vm_flags |= VM_DONTCOPY;
  656. vma->vm_private_data = map;
  657. if (use_ptemod)
  658. map->vma = vma;
  659. if (map->flags) {
  660. if ((vma->vm_flags & VM_WRITE) &&
  661. (map->flags & GNTMAP_readonly))
  662. goto out_unlock_put;
  663. } else {
  664. map->flags = GNTMAP_host_map;
  665. if (!(vma->vm_flags & VM_WRITE))
  666. map->flags |= GNTMAP_readonly;
  667. }
  668. spin_unlock(&priv->lock);
  669. if (use_ptemod) {
  670. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  671. vma->vm_end - vma->vm_start,
  672. find_grant_ptes, map);
  673. if (err) {
  674. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  675. goto out_put_map;
  676. }
  677. }
  678. err = map_grant_pages(map);
  679. if (err)
  680. goto out_put_map;
  681. if (!use_ptemod) {
  682. for (i = 0; i < count; i++) {
  683. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  684. map->pages[i]);
  685. if (err)
  686. goto out_put_map;
  687. }
  688. }
  689. return 0;
  690. unlock_out:
  691. spin_unlock(&priv->lock);
  692. return err;
  693. out_unlock_put:
  694. spin_unlock(&priv->lock);
  695. out_put_map:
  696. if (use_ptemod)
  697. map->vma = NULL;
  698. gntdev_put_map(priv, map);
  699. return err;
  700. }
  701. static const struct file_operations gntdev_fops = {
  702. .owner = THIS_MODULE,
  703. .open = gntdev_open,
  704. .release = gntdev_release,
  705. .mmap = gntdev_mmap,
  706. .unlocked_ioctl = gntdev_ioctl
  707. };
  708. static struct miscdevice gntdev_miscdev = {
  709. .minor = MISC_DYNAMIC_MINOR,
  710. .name = "xen/gntdev",
  711. .fops = &gntdev_fops,
  712. };
  713. /* ------------------------------------------------------------------ */
  714. static int __init gntdev_init(void)
  715. {
  716. int err;
  717. if (!xen_domain())
  718. return -ENODEV;
  719. use_ptemod = xen_pv_domain();
  720. err = misc_register(&gntdev_miscdev);
  721. if (err != 0) {
  722. printk(KERN_ERR "Could not register gntdev device\n");
  723. return err;
  724. }
  725. return 0;
  726. }
  727. static void __exit gntdev_exit(void)
  728. {
  729. misc_deregister(&gntdev_miscdev);
  730. }
  731. module_init(gntdev_init);
  732. module_exit(gntdev_exit);
  733. /* ------------------------------------------------------------------ */