gntdev.c 23 KB

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