gntdev.c 21 KB

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