gntdev.c 30 KB

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