gntdev.c 23 KB

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