gntdev.c 30 KB

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