gntdev.c 28 KB

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