gntdev.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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/sched/mm.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/slab.h>
  35. #include <linux/highmem.h>
  36. #include <linux/refcount.h>
  37. #include <xen/xen.h>
  38. #include <xen/grant_table.h>
  39. #include <xen/balloon.h>
  40. #include <xen/gntdev.h>
  41. #include <xen/events.h>
  42. #include <xen/page.h>
  43. #include <asm/xen/hypervisor.h>
  44. #include <asm/xen/hypercall.h>
  45. MODULE_LICENSE("GPL");
  46. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  47. "Gerd Hoffmann <kraxel@redhat.com>");
  48. MODULE_DESCRIPTION("User-space granted page access driver");
  49. static int limit = 1024*1024;
  50. module_param(limit, int, 0644);
  51. MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
  52. "the gntdev device");
  53. static atomic_t pages_mapped = ATOMIC_INIT(0);
  54. static int use_ptemod;
  55. #define populate_freeable_maps use_ptemod
  56. struct gntdev_priv {
  57. /* maps with visible offsets in the file descriptor */
  58. struct list_head maps;
  59. /* maps that are not visible; will be freed on munmap.
  60. * Only populated if populate_freeable_maps == 1 */
  61. struct list_head freeable_maps;
  62. /* lock protects maps and freeable_maps */
  63. struct mutex lock;
  64. struct mm_struct *mm;
  65. struct mmu_notifier mn;
  66. };
  67. struct unmap_notify {
  68. int flags;
  69. /* Address relative to the start of the grant_map */
  70. int addr;
  71. int event;
  72. };
  73. struct grant_map {
  74. struct list_head next;
  75. struct vm_area_struct *vma;
  76. int index;
  77. int count;
  78. int flags;
  79. refcount_t users;
  80. struct unmap_notify notify;
  81. struct ioctl_gntdev_grant_ref *grants;
  82. struct gnttab_map_grant_ref *map_ops;
  83. struct gnttab_unmap_grant_ref *unmap_ops;
  84. struct gnttab_map_grant_ref *kmap_ops;
  85. struct gnttab_unmap_grant_ref *kunmap_ops;
  86. struct page **pages;
  87. unsigned long pages_vm_start;
  88. };
  89. static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
  90. /* ------------------------------------------------------------------ */
  91. static void gntdev_print_maps(struct gntdev_priv *priv,
  92. char *text, int text_index)
  93. {
  94. #ifdef DEBUG
  95. struct grant_map *map;
  96. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  97. list_for_each_entry(map, &priv->maps, next)
  98. pr_debug(" index %2d, count %2d %s\n",
  99. map->index, map->count,
  100. map->index == text_index && text ? text : "");
  101. #endif
  102. }
  103. static void gntdev_free_map(struct grant_map *map)
  104. {
  105. if (map == NULL)
  106. return;
  107. if (map->pages)
  108. gnttab_free_pages(map->count, map->pages);
  109. kfree(map->pages);
  110. kfree(map->grants);
  111. kfree(map->map_ops);
  112. kfree(map->unmap_ops);
  113. kfree(map->kmap_ops);
  114. kfree(map->kunmap_ops);
  115. kfree(map);
  116. }
  117. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  118. {
  119. struct grant_map *add;
  120. int i;
  121. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  122. if (NULL == add)
  123. return NULL;
  124. add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
  125. add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
  126. add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
  127. add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
  128. add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
  129. add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
  130. if (NULL == add->grants ||
  131. NULL == add->map_ops ||
  132. NULL == add->unmap_ops ||
  133. NULL == add->kmap_ops ||
  134. NULL == add->kunmap_ops ||
  135. NULL == add->pages)
  136. goto err;
  137. if (gnttab_alloc_pages(count, add->pages))
  138. goto err;
  139. for (i = 0; i < count; i++) {
  140. add->map_ops[i].handle = -1;
  141. add->unmap_ops[i].handle = -1;
  142. add->kmap_ops[i].handle = -1;
  143. add->kunmap_ops[i].handle = -1;
  144. }
  145. add->index = 0;
  146. add->count = count;
  147. refcount_set(&add->users, 1);
  148. return add;
  149. err:
  150. gntdev_free_map(add);
  151. return NULL;
  152. }
  153. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  154. {
  155. struct grant_map *map;
  156. list_for_each_entry(map, &priv->maps, next) {
  157. if (add->index + add->count < map->index) {
  158. list_add_tail(&add->next, &map->next);
  159. goto done;
  160. }
  161. add->index = map->index + map->count;
  162. }
  163. list_add_tail(&add->next, &priv->maps);
  164. done:
  165. gntdev_print_maps(priv, "[new]", add->index);
  166. }
  167. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  168. int index, int count)
  169. {
  170. struct grant_map *map;
  171. list_for_each_entry(map, &priv->maps, next) {
  172. if (map->index != index)
  173. continue;
  174. if (count && map->count != count)
  175. continue;
  176. return map;
  177. }
  178. return NULL;
  179. }
  180. static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
  181. {
  182. if (!map)
  183. return;
  184. if (!refcount_dec_and_test(&map->users))
  185. return;
  186. atomic_sub(map->count, &pages_mapped);
  187. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  188. notify_remote_via_evtchn(map->notify.event);
  189. evtchn_put(map->notify.event);
  190. }
  191. if (populate_freeable_maps && priv) {
  192. mutex_lock(&priv->lock);
  193. list_del(&map->next);
  194. mutex_unlock(&priv->lock);
  195. }
  196. if (map->pages && !use_ptemod)
  197. unmap_grant_pages(map, 0, map->count);
  198. gntdev_free_map(map);
  199. }
  200. /* ------------------------------------------------------------------ */
  201. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  202. unsigned long addr, void *data)
  203. {
  204. struct grant_map *map = data;
  205. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  206. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  207. u64 pte_maddr;
  208. BUG_ON(pgnr >= map->count);
  209. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  210. /*
  211. * Set the PTE as special to force get_user_pages_fast() fall
  212. * back to the slow path. If this is not supported as part of
  213. * the grant map, it will be done afterwards.
  214. */
  215. if (xen_feature(XENFEAT_gnttab_map_avail_bits))
  216. flags |= (1 << _GNTMAP_guest_avail0);
  217. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  218. map->grants[pgnr].ref,
  219. map->grants[pgnr].domid);
  220. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  221. -1 /* handle */);
  222. return 0;
  223. }
  224. #ifdef CONFIG_X86
  225. static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
  226. unsigned long addr, void *data)
  227. {
  228. set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
  229. return 0;
  230. }
  231. #endif
  232. static int map_grant_pages(struct grant_map *map)
  233. {
  234. int i, err = 0;
  235. if (!use_ptemod) {
  236. /* Note: it could already be mapped */
  237. if (map->map_ops[0].handle != -1)
  238. return 0;
  239. for (i = 0; i < map->count; i++) {
  240. unsigned long addr = (unsigned long)
  241. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  242. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  243. map->grants[i].ref,
  244. map->grants[i].domid);
  245. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  246. map->flags, -1 /* handle */);
  247. }
  248. } else {
  249. /*
  250. * Setup the map_ops corresponding to the pte entries pointing
  251. * to the kernel linear addresses of the struct pages.
  252. * These ptes are completely different from the user ptes dealt
  253. * with find_grant_ptes.
  254. */
  255. for (i = 0; i < map->count; i++) {
  256. unsigned long address = (unsigned long)
  257. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  258. BUG_ON(PageHighMem(map->pages[i]));
  259. gnttab_set_map_op(&map->kmap_ops[i], address,
  260. map->flags | GNTMAP_host_map,
  261. map->grants[i].ref,
  262. map->grants[i].domid);
  263. gnttab_set_unmap_op(&map->kunmap_ops[i], address,
  264. map->flags | GNTMAP_host_map, -1);
  265. }
  266. }
  267. pr_debug("map %d+%d\n", map->index, map->count);
  268. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  269. map->pages, map->count);
  270. if (err)
  271. return err;
  272. for (i = 0; i < map->count; i++) {
  273. if (map->map_ops[i].status) {
  274. err = -EINVAL;
  275. continue;
  276. }
  277. map->unmap_ops[i].handle = map->map_ops[i].handle;
  278. if (use_ptemod)
  279. map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
  280. }
  281. return err;
  282. }
  283. static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
  284. {
  285. int i, err = 0;
  286. struct gntab_unmap_queue_data unmap_data;
  287. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  288. int pgno = (map->notify.addr >> PAGE_SHIFT);
  289. if (pgno >= offset && pgno < offset + pages) {
  290. /* No need for kmap, pages are in lowmem */
  291. uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
  292. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  293. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  294. }
  295. }
  296. unmap_data.unmap_ops = map->unmap_ops + offset;
  297. unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
  298. unmap_data.pages = map->pages + offset;
  299. unmap_data.count = pages;
  300. err = gnttab_unmap_refs_sync(&unmap_data);
  301. if (err)
  302. return err;
  303. for (i = 0; i < pages; i++) {
  304. if (map->unmap_ops[offset+i].status)
  305. err = -EINVAL;
  306. pr_debug("unmap handle=%d st=%d\n",
  307. map->unmap_ops[offset+i].handle,
  308. map->unmap_ops[offset+i].status);
  309. map->unmap_ops[offset+i].handle = -1;
  310. }
  311. return err;
  312. }
  313. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  314. {
  315. int range, err = 0;
  316. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  317. /* It is possible the requested range will have a "hole" where we
  318. * already unmapped some of the grants. Only unmap valid ranges.
  319. */
  320. while (pages && !err) {
  321. while (pages && map->unmap_ops[offset].handle == -1) {
  322. offset++;
  323. pages--;
  324. }
  325. range = 0;
  326. while (range < pages) {
  327. if (map->unmap_ops[offset+range].handle == -1) {
  328. range--;
  329. break;
  330. }
  331. range++;
  332. }
  333. err = __unmap_grant_pages(map, offset, range);
  334. offset += range;
  335. pages -= range;
  336. }
  337. return err;
  338. }
  339. /* ------------------------------------------------------------------ */
  340. static void gntdev_vma_open(struct vm_area_struct *vma)
  341. {
  342. struct grant_map *map = vma->vm_private_data;
  343. pr_debug("gntdev_vma_open %p\n", vma);
  344. refcount_inc(&map->users);
  345. }
  346. static void gntdev_vma_close(struct vm_area_struct *vma)
  347. {
  348. struct grant_map *map = vma->vm_private_data;
  349. struct file *file = vma->vm_file;
  350. struct gntdev_priv *priv = file->private_data;
  351. pr_debug("gntdev_vma_close %p\n", vma);
  352. if (use_ptemod) {
  353. /* It is possible that an mmu notifier could be running
  354. * concurrently, so take priv->lock to ensure that the vma won't
  355. * vanishing during the unmap_grant_pages call, since we will
  356. * spin here until that completes. Such a concurrent call will
  357. * not do any unmapping, since that has been done prior to
  358. * closing the vma, but it may still iterate the unmap_ops list.
  359. */
  360. mutex_lock(&priv->lock);
  361. map->vma = NULL;
  362. mutex_unlock(&priv->lock);
  363. }
  364. vma->vm_private_data = NULL;
  365. gntdev_put_map(priv, map);
  366. }
  367. static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
  368. unsigned long addr)
  369. {
  370. struct grant_map *map = vma->vm_private_data;
  371. return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
  372. }
  373. static const struct vm_operations_struct gntdev_vmops = {
  374. .open = gntdev_vma_open,
  375. .close = gntdev_vma_close,
  376. .find_special_page = gntdev_vma_find_special_page,
  377. };
  378. /* ------------------------------------------------------------------ */
  379. static void unmap_if_in_range(struct grant_map *map,
  380. unsigned long start, unsigned long end)
  381. {
  382. unsigned long mstart, mend;
  383. int err;
  384. if (!map->vma)
  385. return;
  386. if (map->vma->vm_start >= end)
  387. return;
  388. if (map->vma->vm_end <= start)
  389. return;
  390. mstart = max(start, map->vma->vm_start);
  391. mend = min(end, map->vma->vm_end);
  392. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  393. map->index, map->count,
  394. map->vma->vm_start, map->vma->vm_end,
  395. start, end, mstart, mend);
  396. err = unmap_grant_pages(map,
  397. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  398. (mend - mstart) >> PAGE_SHIFT);
  399. WARN_ON(err);
  400. }
  401. static void mn_invl_range_start(struct mmu_notifier *mn,
  402. struct mm_struct *mm,
  403. unsigned long start, unsigned long end)
  404. {
  405. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  406. struct grant_map *map;
  407. mutex_lock(&priv->lock);
  408. list_for_each_entry(map, &priv->maps, next) {
  409. unmap_if_in_range(map, start, end);
  410. }
  411. list_for_each_entry(map, &priv->freeable_maps, next) {
  412. unmap_if_in_range(map, start, end);
  413. }
  414. mutex_unlock(&priv->lock);
  415. }
  416. static void mn_invl_page(struct mmu_notifier *mn,
  417. struct mm_struct *mm,
  418. unsigned long address)
  419. {
  420. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  421. }
  422. static void mn_release(struct mmu_notifier *mn,
  423. struct mm_struct *mm)
  424. {
  425. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  426. struct grant_map *map;
  427. int err;
  428. mutex_lock(&priv->lock);
  429. list_for_each_entry(map, &priv->maps, next) {
  430. if (!map->vma)
  431. continue;
  432. pr_debug("map %d+%d (%lx %lx)\n",
  433. map->index, map->count,
  434. map->vma->vm_start, map->vma->vm_end);
  435. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  436. WARN_ON(err);
  437. }
  438. list_for_each_entry(map, &priv->freeable_maps, next) {
  439. if (!map->vma)
  440. continue;
  441. pr_debug("map %d+%d (%lx %lx)\n",
  442. map->index, map->count,
  443. map->vma->vm_start, map->vma->vm_end);
  444. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  445. WARN_ON(err);
  446. }
  447. mutex_unlock(&priv->lock);
  448. }
  449. static const struct mmu_notifier_ops gntdev_mmu_ops = {
  450. .release = mn_release,
  451. .invalidate_page = mn_invl_page,
  452. .invalidate_range_start = mn_invl_range_start,
  453. };
  454. /* ------------------------------------------------------------------ */
  455. static int gntdev_open(struct inode *inode, struct file *flip)
  456. {
  457. struct gntdev_priv *priv;
  458. int ret = 0;
  459. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  460. if (!priv)
  461. return -ENOMEM;
  462. INIT_LIST_HEAD(&priv->maps);
  463. INIT_LIST_HEAD(&priv->freeable_maps);
  464. mutex_init(&priv->lock);
  465. if (use_ptemod) {
  466. priv->mm = get_task_mm(current);
  467. if (!priv->mm) {
  468. kfree(priv);
  469. return -ENOMEM;
  470. }
  471. priv->mn.ops = &gntdev_mmu_ops;
  472. ret = mmu_notifier_register(&priv->mn, priv->mm);
  473. mmput(priv->mm);
  474. }
  475. if (ret) {
  476. kfree(priv);
  477. return ret;
  478. }
  479. flip->private_data = priv;
  480. pr_debug("priv %p\n", priv);
  481. return 0;
  482. }
  483. static int gntdev_release(struct inode *inode, struct file *flip)
  484. {
  485. struct gntdev_priv *priv = flip->private_data;
  486. struct grant_map *map;
  487. pr_debug("priv %p\n", priv);
  488. mutex_lock(&priv->lock);
  489. while (!list_empty(&priv->maps)) {
  490. map = list_entry(priv->maps.next, struct grant_map, next);
  491. list_del(&map->next);
  492. gntdev_put_map(NULL /* already removed */, map);
  493. }
  494. WARN_ON(!list_empty(&priv->freeable_maps));
  495. mutex_unlock(&priv->lock);
  496. if (use_ptemod)
  497. mmu_notifier_unregister(&priv->mn, priv->mm);
  498. kfree(priv);
  499. return 0;
  500. }
  501. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  502. struct ioctl_gntdev_map_grant_ref __user *u)
  503. {
  504. struct ioctl_gntdev_map_grant_ref op;
  505. struct grant_map *map;
  506. int err;
  507. if (copy_from_user(&op, u, sizeof(op)) != 0)
  508. return -EFAULT;
  509. pr_debug("priv %p, add %d\n", priv, op.count);
  510. if (unlikely(op.count <= 0))
  511. return -EINVAL;
  512. err = -ENOMEM;
  513. map = gntdev_alloc_map(priv, op.count);
  514. if (!map)
  515. return err;
  516. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  517. pr_debug("can't map: over limit\n");
  518. gntdev_put_map(NULL, map);
  519. return err;
  520. }
  521. if (copy_from_user(map->grants, &u->refs,
  522. sizeof(map->grants[0]) * op.count) != 0) {
  523. gntdev_put_map(NULL, map);
  524. return -EFAULT;
  525. }
  526. mutex_lock(&priv->lock);
  527. gntdev_add_map(priv, map);
  528. op.index = map->index << PAGE_SHIFT;
  529. mutex_unlock(&priv->lock);
  530. if (copy_to_user(u, &op, sizeof(op)) != 0)
  531. return -EFAULT;
  532. return 0;
  533. }
  534. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  535. struct ioctl_gntdev_unmap_grant_ref __user *u)
  536. {
  537. struct ioctl_gntdev_unmap_grant_ref op;
  538. struct grant_map *map;
  539. int err = -ENOENT;
  540. if (copy_from_user(&op, u, sizeof(op)) != 0)
  541. return -EFAULT;
  542. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  543. mutex_lock(&priv->lock);
  544. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  545. if (map) {
  546. list_del(&map->next);
  547. if (populate_freeable_maps)
  548. list_add_tail(&map->next, &priv->freeable_maps);
  549. err = 0;
  550. }
  551. mutex_unlock(&priv->lock);
  552. if (map)
  553. gntdev_put_map(priv, map);
  554. return err;
  555. }
  556. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  557. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  558. {
  559. struct ioctl_gntdev_get_offset_for_vaddr op;
  560. struct vm_area_struct *vma;
  561. struct grant_map *map;
  562. int rv = -EINVAL;
  563. if (copy_from_user(&op, u, sizeof(op)) != 0)
  564. return -EFAULT;
  565. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  566. down_read(&current->mm->mmap_sem);
  567. vma = find_vma(current->mm, op.vaddr);
  568. if (!vma || vma->vm_ops != &gntdev_vmops)
  569. goto out_unlock;
  570. map = vma->vm_private_data;
  571. if (!map)
  572. goto out_unlock;
  573. op.offset = map->index << PAGE_SHIFT;
  574. op.count = map->count;
  575. rv = 0;
  576. out_unlock:
  577. up_read(&current->mm->mmap_sem);
  578. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  579. return -EFAULT;
  580. return rv;
  581. }
  582. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  583. {
  584. struct ioctl_gntdev_unmap_notify op;
  585. struct grant_map *map;
  586. int rc;
  587. int out_flags;
  588. unsigned int out_event;
  589. if (copy_from_user(&op, u, sizeof(op)))
  590. return -EFAULT;
  591. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  592. return -EINVAL;
  593. /* We need to grab a reference to the event channel we are going to use
  594. * to send the notify before releasing the reference we may already have
  595. * (if someone has called this ioctl twice). This is required so that
  596. * it is possible to change the clear_byte part of the notification
  597. * without disturbing the event channel part, which may now be the last
  598. * reference to that event channel.
  599. */
  600. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  601. if (evtchn_get(op.event_channel_port))
  602. return -EINVAL;
  603. }
  604. out_flags = op.action;
  605. out_event = op.event_channel_port;
  606. mutex_lock(&priv->lock);
  607. list_for_each_entry(map, &priv->maps, next) {
  608. uint64_t begin = map->index << PAGE_SHIFT;
  609. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  610. if (op.index >= begin && op.index < end)
  611. goto found;
  612. }
  613. rc = -ENOENT;
  614. goto unlock_out;
  615. found:
  616. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  617. (map->flags & GNTMAP_readonly)) {
  618. rc = -EINVAL;
  619. goto unlock_out;
  620. }
  621. out_flags = map->notify.flags;
  622. out_event = map->notify.event;
  623. map->notify.flags = op.action;
  624. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  625. map->notify.event = op.event_channel_port;
  626. rc = 0;
  627. unlock_out:
  628. mutex_unlock(&priv->lock);
  629. /* Drop the reference to the event channel we did not save in the map */
  630. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  631. evtchn_put(out_event);
  632. return rc;
  633. }
  634. #define GNTDEV_COPY_BATCH 16
  635. struct gntdev_copy_batch {
  636. struct gnttab_copy ops[GNTDEV_COPY_BATCH];
  637. struct page *pages[GNTDEV_COPY_BATCH];
  638. s16 __user *status[GNTDEV_COPY_BATCH];
  639. unsigned int nr_ops;
  640. unsigned int nr_pages;
  641. };
  642. static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
  643. bool writeable, unsigned long *gfn)
  644. {
  645. unsigned long addr = (unsigned long)virt;
  646. struct page *page;
  647. unsigned long xen_pfn;
  648. int ret;
  649. ret = get_user_pages_fast(addr, 1, writeable, &page);
  650. if (ret < 0)
  651. return ret;
  652. batch->pages[batch->nr_pages++] = page;
  653. xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
  654. *gfn = pfn_to_gfn(xen_pfn);
  655. return 0;
  656. }
  657. static void gntdev_put_pages(struct gntdev_copy_batch *batch)
  658. {
  659. unsigned int i;
  660. for (i = 0; i < batch->nr_pages; i++)
  661. put_page(batch->pages[i]);
  662. batch->nr_pages = 0;
  663. }
  664. static int gntdev_copy(struct gntdev_copy_batch *batch)
  665. {
  666. unsigned int i;
  667. gnttab_batch_copy(batch->ops, batch->nr_ops);
  668. gntdev_put_pages(batch);
  669. /*
  670. * For each completed op, update the status if the op failed
  671. * and all previous ops for the segment were successful.
  672. */
  673. for (i = 0; i < batch->nr_ops; i++) {
  674. s16 status = batch->ops[i].status;
  675. s16 old_status;
  676. if (status == GNTST_okay)
  677. continue;
  678. if (__get_user(old_status, batch->status[i]))
  679. return -EFAULT;
  680. if (old_status != GNTST_okay)
  681. continue;
  682. if (__put_user(status, batch->status[i]))
  683. return -EFAULT;
  684. }
  685. batch->nr_ops = 0;
  686. return 0;
  687. }
  688. static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
  689. struct gntdev_grant_copy_segment *seg,
  690. s16 __user *status)
  691. {
  692. uint16_t copied = 0;
  693. /*
  694. * Disallow local -> local copies since there is only space in
  695. * batch->pages for one page per-op and this would be a very
  696. * expensive memcpy().
  697. */
  698. if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
  699. return -EINVAL;
  700. /* Can't cross page if source/dest is a grant ref. */
  701. if (seg->flags & GNTCOPY_source_gref) {
  702. if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
  703. return -EINVAL;
  704. }
  705. if (seg->flags & GNTCOPY_dest_gref) {
  706. if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
  707. return -EINVAL;
  708. }
  709. if (put_user(GNTST_okay, status))
  710. return -EFAULT;
  711. while (copied < seg->len) {
  712. struct gnttab_copy *op;
  713. void __user *virt;
  714. size_t len, off;
  715. unsigned long gfn;
  716. int ret;
  717. if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
  718. ret = gntdev_copy(batch);
  719. if (ret < 0)
  720. return ret;
  721. }
  722. len = seg->len - copied;
  723. op = &batch->ops[batch->nr_ops];
  724. op->flags = 0;
  725. if (seg->flags & GNTCOPY_source_gref) {
  726. op->source.u.ref = seg->source.foreign.ref;
  727. op->source.domid = seg->source.foreign.domid;
  728. op->source.offset = seg->source.foreign.offset + copied;
  729. op->flags |= GNTCOPY_source_gref;
  730. } else {
  731. virt = seg->source.virt + copied;
  732. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  733. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  734. ret = gntdev_get_page(batch, virt, false, &gfn);
  735. if (ret < 0)
  736. return ret;
  737. op->source.u.gmfn = gfn;
  738. op->source.domid = DOMID_SELF;
  739. op->source.offset = off;
  740. }
  741. if (seg->flags & GNTCOPY_dest_gref) {
  742. op->dest.u.ref = seg->dest.foreign.ref;
  743. op->dest.domid = seg->dest.foreign.domid;
  744. op->dest.offset = seg->dest.foreign.offset + copied;
  745. op->flags |= GNTCOPY_dest_gref;
  746. } else {
  747. virt = seg->dest.virt + copied;
  748. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  749. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  750. ret = gntdev_get_page(batch, virt, true, &gfn);
  751. if (ret < 0)
  752. return ret;
  753. op->dest.u.gmfn = gfn;
  754. op->dest.domid = DOMID_SELF;
  755. op->dest.offset = off;
  756. }
  757. op->len = len;
  758. copied += len;
  759. batch->status[batch->nr_ops] = status;
  760. batch->nr_ops++;
  761. }
  762. return 0;
  763. }
  764. static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
  765. {
  766. struct ioctl_gntdev_grant_copy copy;
  767. struct gntdev_copy_batch batch;
  768. unsigned int i;
  769. int ret = 0;
  770. if (copy_from_user(&copy, u, sizeof(copy)))
  771. return -EFAULT;
  772. batch.nr_ops = 0;
  773. batch.nr_pages = 0;
  774. for (i = 0; i < copy.count; i++) {
  775. struct gntdev_grant_copy_segment seg;
  776. if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
  777. ret = -EFAULT;
  778. goto out;
  779. }
  780. ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
  781. if (ret < 0)
  782. goto out;
  783. cond_resched();
  784. }
  785. if (batch.nr_ops)
  786. ret = gntdev_copy(&batch);
  787. return ret;
  788. out:
  789. gntdev_put_pages(&batch);
  790. return ret;
  791. }
  792. static long gntdev_ioctl(struct file *flip,
  793. unsigned int cmd, unsigned long arg)
  794. {
  795. struct gntdev_priv *priv = flip->private_data;
  796. void __user *ptr = (void __user *)arg;
  797. switch (cmd) {
  798. case IOCTL_GNTDEV_MAP_GRANT_REF:
  799. return gntdev_ioctl_map_grant_ref(priv, ptr);
  800. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  801. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  802. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  803. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  804. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  805. return gntdev_ioctl_notify(priv, ptr);
  806. case IOCTL_GNTDEV_GRANT_COPY:
  807. return gntdev_ioctl_grant_copy(priv, ptr);
  808. default:
  809. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  810. return -ENOIOCTLCMD;
  811. }
  812. return 0;
  813. }
  814. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  815. {
  816. struct gntdev_priv *priv = flip->private_data;
  817. int index = vma->vm_pgoff;
  818. int count = vma_pages(vma);
  819. struct grant_map *map;
  820. int i, err = -EINVAL;
  821. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  822. return -EINVAL;
  823. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  824. index, count, vma->vm_start, vma->vm_pgoff);
  825. mutex_lock(&priv->lock);
  826. map = gntdev_find_map_index(priv, index, count);
  827. if (!map)
  828. goto unlock_out;
  829. if (use_ptemod && map->vma)
  830. goto unlock_out;
  831. if (use_ptemod && priv->mm != vma->vm_mm) {
  832. pr_warn("Huh? Other mm?\n");
  833. goto unlock_out;
  834. }
  835. refcount_inc(&map->users);
  836. vma->vm_ops = &gntdev_vmops;
  837. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
  838. if (use_ptemod)
  839. vma->vm_flags |= VM_DONTCOPY;
  840. vma->vm_private_data = map;
  841. if (use_ptemod)
  842. map->vma = vma;
  843. if (map->flags) {
  844. if ((vma->vm_flags & VM_WRITE) &&
  845. (map->flags & GNTMAP_readonly))
  846. goto out_unlock_put;
  847. } else {
  848. map->flags = GNTMAP_host_map;
  849. if (!(vma->vm_flags & VM_WRITE))
  850. map->flags |= GNTMAP_readonly;
  851. }
  852. mutex_unlock(&priv->lock);
  853. if (use_ptemod) {
  854. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  855. vma->vm_end - vma->vm_start,
  856. find_grant_ptes, map);
  857. if (err) {
  858. pr_warn("find_grant_ptes() failure.\n");
  859. goto out_put_map;
  860. }
  861. }
  862. err = map_grant_pages(map);
  863. if (err)
  864. goto out_put_map;
  865. if (!use_ptemod) {
  866. for (i = 0; i < count; i++) {
  867. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  868. map->pages[i]);
  869. if (err)
  870. goto out_put_map;
  871. }
  872. } else {
  873. #ifdef CONFIG_X86
  874. /*
  875. * If the PTEs were not made special by the grant map
  876. * hypercall, do so here.
  877. *
  878. * This is racy since the mapping is already visible
  879. * to userspace but userspace should be well-behaved
  880. * enough to not touch it until the mmap() call
  881. * returns.
  882. */
  883. if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
  884. apply_to_page_range(vma->vm_mm, vma->vm_start,
  885. vma->vm_end - vma->vm_start,
  886. set_grant_ptes_as_special, NULL);
  887. }
  888. #endif
  889. map->pages_vm_start = vma->vm_start;
  890. }
  891. return 0;
  892. unlock_out:
  893. mutex_unlock(&priv->lock);
  894. return err;
  895. out_unlock_put:
  896. mutex_unlock(&priv->lock);
  897. out_put_map:
  898. if (use_ptemod)
  899. map->vma = NULL;
  900. gntdev_put_map(priv, map);
  901. return err;
  902. }
  903. static const struct file_operations gntdev_fops = {
  904. .owner = THIS_MODULE,
  905. .open = gntdev_open,
  906. .release = gntdev_release,
  907. .mmap = gntdev_mmap,
  908. .unlocked_ioctl = gntdev_ioctl
  909. };
  910. static struct miscdevice gntdev_miscdev = {
  911. .minor = MISC_DYNAMIC_MINOR,
  912. .name = "xen/gntdev",
  913. .fops = &gntdev_fops,
  914. };
  915. /* ------------------------------------------------------------------ */
  916. static int __init gntdev_init(void)
  917. {
  918. int err;
  919. if (!xen_domain())
  920. return -ENODEV;
  921. use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
  922. err = misc_register(&gntdev_miscdev);
  923. if (err != 0) {
  924. pr_err("Could not register gntdev device\n");
  925. return err;
  926. }
  927. return 0;
  928. }
  929. static void __exit gntdev_exit(void)
  930. {
  931. misc_deregister(&gntdev_miscdev);
  932. }
  933. module_init(gntdev_init);
  934. module_exit(gntdev_exit);
  935. /* ------------------------------------------------------------------ */