gntdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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) {
  270. /* No need for kmap, pages are in lowmem */
  271. uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
  272. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  273. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  274. }
  275. }
  276. err = gnttab_unmap_refs(map->unmap_ops + offset,
  277. use_ptemod ? map->kmap_ops + offset : NULL, map->pages + offset,
  278. pages);
  279. if (err)
  280. return err;
  281. for (i = 0; i < pages; i++) {
  282. if (map->unmap_ops[offset+i].status)
  283. err = -EINVAL;
  284. pr_debug("unmap handle=%d st=%d\n",
  285. map->unmap_ops[offset+i].handle,
  286. map->unmap_ops[offset+i].status);
  287. map->unmap_ops[offset+i].handle = -1;
  288. }
  289. return err;
  290. }
  291. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  292. {
  293. int range, err = 0;
  294. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  295. /* It is possible the requested range will have a "hole" where we
  296. * already unmapped some of the grants. Only unmap valid ranges.
  297. */
  298. while (pages && !err) {
  299. while (pages && map->unmap_ops[offset].handle == -1) {
  300. offset++;
  301. pages--;
  302. }
  303. range = 0;
  304. while (range < pages) {
  305. if (map->unmap_ops[offset+range].handle == -1) {
  306. range--;
  307. break;
  308. }
  309. range++;
  310. }
  311. err = __unmap_grant_pages(map, offset, range);
  312. offset += range;
  313. pages -= range;
  314. }
  315. return err;
  316. }
  317. /* ------------------------------------------------------------------ */
  318. static void gntdev_vma_open(struct vm_area_struct *vma)
  319. {
  320. struct grant_map *map = vma->vm_private_data;
  321. pr_debug("gntdev_vma_open %p\n", vma);
  322. atomic_inc(&map->users);
  323. }
  324. static void gntdev_vma_close(struct vm_area_struct *vma)
  325. {
  326. struct grant_map *map = vma->vm_private_data;
  327. struct file *file = vma->vm_file;
  328. struct gntdev_priv *priv = file->private_data;
  329. pr_debug("gntdev_vma_close %p\n", vma);
  330. if (use_ptemod) {
  331. /* It is possible that an mmu notifier could be running
  332. * concurrently, so take priv->lock to ensure that the vma won't
  333. * vanishing during the unmap_grant_pages call, since we will
  334. * spin here until that completes. Such a concurrent call will
  335. * not do any unmapping, since that has been done prior to
  336. * closing the vma, but it may still iterate the unmap_ops list.
  337. */
  338. spin_lock(&priv->lock);
  339. map->vma = NULL;
  340. spin_unlock(&priv->lock);
  341. }
  342. vma->vm_private_data = NULL;
  343. gntdev_put_map(priv, map);
  344. }
  345. static struct vm_operations_struct gntdev_vmops = {
  346. .open = gntdev_vma_open,
  347. .close = gntdev_vma_close,
  348. };
  349. /* ------------------------------------------------------------------ */
  350. static void unmap_if_in_range(struct grant_map *map,
  351. unsigned long start, unsigned long end)
  352. {
  353. unsigned long mstart, mend;
  354. int err;
  355. if (!map->vma)
  356. return;
  357. if (map->vma->vm_start >= end)
  358. return;
  359. if (map->vma->vm_end <= start)
  360. return;
  361. mstart = max(start, map->vma->vm_start);
  362. mend = min(end, map->vma->vm_end);
  363. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  364. map->index, map->count,
  365. map->vma->vm_start, map->vma->vm_end,
  366. start, end, mstart, mend);
  367. err = unmap_grant_pages(map,
  368. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  369. (mend - mstart) >> PAGE_SHIFT);
  370. WARN_ON(err);
  371. }
  372. static void mn_invl_range_start(struct mmu_notifier *mn,
  373. struct mm_struct *mm,
  374. unsigned long start, unsigned long end)
  375. {
  376. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  377. struct grant_map *map;
  378. spin_lock(&priv->lock);
  379. list_for_each_entry(map, &priv->maps, next) {
  380. unmap_if_in_range(map, start, end);
  381. }
  382. list_for_each_entry(map, &priv->freeable_maps, next) {
  383. unmap_if_in_range(map, start, end);
  384. }
  385. spin_unlock(&priv->lock);
  386. }
  387. static void mn_invl_page(struct mmu_notifier *mn,
  388. struct mm_struct *mm,
  389. unsigned long address)
  390. {
  391. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  392. }
  393. static void mn_release(struct mmu_notifier *mn,
  394. struct mm_struct *mm)
  395. {
  396. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  397. struct grant_map *map;
  398. int err;
  399. spin_lock(&priv->lock);
  400. list_for_each_entry(map, &priv->maps, next) {
  401. if (!map->vma)
  402. continue;
  403. pr_debug("map %d+%d (%lx %lx)\n",
  404. map->index, map->count,
  405. map->vma->vm_start, map->vma->vm_end);
  406. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  407. WARN_ON(err);
  408. }
  409. list_for_each_entry(map, &priv->freeable_maps, next) {
  410. if (!map->vma)
  411. continue;
  412. pr_debug("map %d+%d (%lx %lx)\n",
  413. map->index, map->count,
  414. map->vma->vm_start, map->vma->vm_end);
  415. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  416. WARN_ON(err);
  417. }
  418. spin_unlock(&priv->lock);
  419. }
  420. static struct mmu_notifier_ops gntdev_mmu_ops = {
  421. .release = mn_release,
  422. .invalidate_page = mn_invl_page,
  423. .invalidate_range_start = mn_invl_range_start,
  424. };
  425. /* ------------------------------------------------------------------ */
  426. static int gntdev_open(struct inode *inode, struct file *flip)
  427. {
  428. struct gntdev_priv *priv;
  429. int ret = 0;
  430. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  431. if (!priv)
  432. return -ENOMEM;
  433. INIT_LIST_HEAD(&priv->maps);
  434. INIT_LIST_HEAD(&priv->freeable_maps);
  435. spin_lock_init(&priv->lock);
  436. if (use_ptemod) {
  437. priv->mm = get_task_mm(current);
  438. if (!priv->mm) {
  439. kfree(priv);
  440. return -ENOMEM;
  441. }
  442. priv->mn.ops = &gntdev_mmu_ops;
  443. ret = mmu_notifier_register(&priv->mn, priv->mm);
  444. mmput(priv->mm);
  445. }
  446. if (ret) {
  447. kfree(priv);
  448. return ret;
  449. }
  450. flip->private_data = priv;
  451. pr_debug("priv %p\n", priv);
  452. return 0;
  453. }
  454. static int gntdev_release(struct inode *inode, struct file *flip)
  455. {
  456. struct gntdev_priv *priv = flip->private_data;
  457. struct grant_map *map;
  458. pr_debug("priv %p\n", priv);
  459. while (!list_empty(&priv->maps)) {
  460. map = list_entry(priv->maps.next, struct grant_map, next);
  461. list_del(&map->next);
  462. gntdev_put_map(NULL /* already removed */, map);
  463. }
  464. WARN_ON(!list_empty(&priv->freeable_maps));
  465. if (use_ptemod)
  466. mmu_notifier_unregister(&priv->mn, priv->mm);
  467. kfree(priv);
  468. return 0;
  469. }
  470. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  471. struct ioctl_gntdev_map_grant_ref __user *u)
  472. {
  473. struct ioctl_gntdev_map_grant_ref op;
  474. struct grant_map *map;
  475. int err;
  476. if (copy_from_user(&op, u, sizeof(op)) != 0)
  477. return -EFAULT;
  478. pr_debug("priv %p, add %d\n", priv, op.count);
  479. if (unlikely(op.count <= 0))
  480. return -EINVAL;
  481. err = -ENOMEM;
  482. map = gntdev_alloc_map(priv, op.count);
  483. if (!map)
  484. return err;
  485. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  486. pr_debug("can't map: over limit\n");
  487. gntdev_put_map(NULL, map);
  488. return err;
  489. }
  490. if (copy_from_user(map->grants, &u->refs,
  491. sizeof(map->grants[0]) * op.count) != 0) {
  492. gntdev_put_map(NULL, map);
  493. return -EFAULT;
  494. }
  495. spin_lock(&priv->lock);
  496. gntdev_add_map(priv, map);
  497. op.index = map->index << PAGE_SHIFT;
  498. spin_unlock(&priv->lock);
  499. if (copy_to_user(u, &op, sizeof(op)) != 0)
  500. return -EFAULT;
  501. return 0;
  502. }
  503. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  504. struct ioctl_gntdev_unmap_grant_ref __user *u)
  505. {
  506. struct ioctl_gntdev_unmap_grant_ref op;
  507. struct grant_map *map;
  508. int err = -ENOENT;
  509. if (copy_from_user(&op, u, sizeof(op)) != 0)
  510. return -EFAULT;
  511. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  512. spin_lock(&priv->lock);
  513. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  514. if (map) {
  515. list_del(&map->next);
  516. if (populate_freeable_maps)
  517. list_add_tail(&map->next, &priv->freeable_maps);
  518. err = 0;
  519. }
  520. spin_unlock(&priv->lock);
  521. if (map)
  522. gntdev_put_map(priv, map);
  523. return err;
  524. }
  525. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  526. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  527. {
  528. struct ioctl_gntdev_get_offset_for_vaddr op;
  529. struct vm_area_struct *vma;
  530. struct grant_map *map;
  531. int rv = -EINVAL;
  532. if (copy_from_user(&op, u, sizeof(op)) != 0)
  533. return -EFAULT;
  534. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  535. down_read(&current->mm->mmap_sem);
  536. vma = find_vma(current->mm, op.vaddr);
  537. if (!vma || vma->vm_ops != &gntdev_vmops)
  538. goto out_unlock;
  539. map = vma->vm_private_data;
  540. if (!map)
  541. goto out_unlock;
  542. op.offset = map->index << PAGE_SHIFT;
  543. op.count = map->count;
  544. rv = 0;
  545. out_unlock:
  546. up_read(&current->mm->mmap_sem);
  547. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  548. return -EFAULT;
  549. return rv;
  550. }
  551. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  552. {
  553. struct ioctl_gntdev_unmap_notify op;
  554. struct grant_map *map;
  555. int rc;
  556. int out_flags;
  557. unsigned int out_event;
  558. if (copy_from_user(&op, u, sizeof(op)))
  559. return -EFAULT;
  560. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  561. return -EINVAL;
  562. /* We need to grab a reference to the event channel we are going to use
  563. * to send the notify before releasing the reference we may already have
  564. * (if someone has called this ioctl twice). This is required so that
  565. * it is possible to change the clear_byte part of the notification
  566. * without disturbing the event channel part, which may now be the last
  567. * reference to that event channel.
  568. */
  569. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  570. if (evtchn_get(op.event_channel_port))
  571. return -EINVAL;
  572. }
  573. out_flags = op.action;
  574. out_event = op.event_channel_port;
  575. spin_lock(&priv->lock);
  576. list_for_each_entry(map, &priv->maps, next) {
  577. uint64_t begin = map->index << PAGE_SHIFT;
  578. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  579. if (op.index >= begin && op.index < end)
  580. goto found;
  581. }
  582. rc = -ENOENT;
  583. goto unlock_out;
  584. found:
  585. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  586. (map->flags & GNTMAP_readonly)) {
  587. rc = -EINVAL;
  588. goto unlock_out;
  589. }
  590. out_flags = map->notify.flags;
  591. out_event = map->notify.event;
  592. map->notify.flags = op.action;
  593. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  594. map->notify.event = op.event_channel_port;
  595. rc = 0;
  596. unlock_out:
  597. spin_unlock(&priv->lock);
  598. /* Drop the reference to the event channel we did not save in the map */
  599. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  600. evtchn_put(out_event);
  601. return rc;
  602. }
  603. static long gntdev_ioctl(struct file *flip,
  604. unsigned int cmd, unsigned long arg)
  605. {
  606. struct gntdev_priv *priv = flip->private_data;
  607. void __user *ptr = (void __user *)arg;
  608. switch (cmd) {
  609. case IOCTL_GNTDEV_MAP_GRANT_REF:
  610. return gntdev_ioctl_map_grant_ref(priv, ptr);
  611. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  612. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  613. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  614. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  615. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  616. return gntdev_ioctl_notify(priv, ptr);
  617. default:
  618. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  619. return -ENOIOCTLCMD;
  620. }
  621. return 0;
  622. }
  623. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  624. {
  625. struct gntdev_priv *priv = flip->private_data;
  626. int index = vma->vm_pgoff;
  627. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  628. struct grant_map *map;
  629. int i, err = -EINVAL;
  630. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  631. return -EINVAL;
  632. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  633. index, count, vma->vm_start, vma->vm_pgoff);
  634. spin_lock(&priv->lock);
  635. map = gntdev_find_map_index(priv, index, count);
  636. if (!map)
  637. goto unlock_out;
  638. if (use_ptemod && map->vma)
  639. goto unlock_out;
  640. if (use_ptemod && priv->mm != vma->vm_mm) {
  641. printk(KERN_WARNING "Huh? Other mm?\n");
  642. goto unlock_out;
  643. }
  644. atomic_inc(&map->users);
  645. vma->vm_ops = &gntdev_vmops;
  646. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  647. if (use_ptemod)
  648. vma->vm_flags |= VM_DONTCOPY;
  649. vma->vm_private_data = map;
  650. if (use_ptemod)
  651. map->vma = vma;
  652. if (map->flags) {
  653. if ((vma->vm_flags & VM_WRITE) &&
  654. (map->flags & GNTMAP_readonly))
  655. goto out_unlock_put;
  656. } else {
  657. map->flags = GNTMAP_host_map;
  658. if (!(vma->vm_flags & VM_WRITE))
  659. map->flags |= GNTMAP_readonly;
  660. }
  661. spin_unlock(&priv->lock);
  662. if (use_ptemod) {
  663. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  664. vma->vm_end - vma->vm_start,
  665. find_grant_ptes, map);
  666. if (err) {
  667. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  668. goto out_put_map;
  669. }
  670. }
  671. err = map_grant_pages(map);
  672. if (err)
  673. goto out_put_map;
  674. if (!use_ptemod) {
  675. for (i = 0; i < count; i++) {
  676. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  677. map->pages[i]);
  678. if (err)
  679. goto out_put_map;
  680. }
  681. }
  682. return 0;
  683. unlock_out:
  684. spin_unlock(&priv->lock);
  685. return err;
  686. out_unlock_put:
  687. spin_unlock(&priv->lock);
  688. out_put_map:
  689. if (use_ptemod)
  690. map->vma = NULL;
  691. gntdev_put_map(priv, map);
  692. return err;
  693. }
  694. static const struct file_operations gntdev_fops = {
  695. .owner = THIS_MODULE,
  696. .open = gntdev_open,
  697. .release = gntdev_release,
  698. .mmap = gntdev_mmap,
  699. .unlocked_ioctl = gntdev_ioctl
  700. };
  701. static struct miscdevice gntdev_miscdev = {
  702. .minor = MISC_DYNAMIC_MINOR,
  703. .name = "xen/gntdev",
  704. .fops = &gntdev_fops,
  705. };
  706. /* ------------------------------------------------------------------ */
  707. static int __init gntdev_init(void)
  708. {
  709. int err;
  710. if (!xen_domain())
  711. return -ENODEV;
  712. use_ptemod = xen_pv_domain();
  713. err = misc_register(&gntdev_miscdev);
  714. if (err != 0) {
  715. printk(KERN_ERR "Could not register gntdev device\n");
  716. return err;
  717. }
  718. return 0;
  719. }
  720. static void __exit gntdev_exit(void)
  721. {
  722. misc_deregister(&gntdev_miscdev);
  723. }
  724. module_init(gntdev_init);
  725. module_exit(gntdev_exit);
  726. /* ------------------------------------------------------------------ */