gntdev.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. * Note that GNTMAP_device_map isn't needed here: The
  274. * dev_bus_addr output field gets consumed only from ->map_ops,
  275. * and by not requesting it when mapping we also avoid needing
  276. * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
  277. * reference to the page in the hypervisor).
  278. */
  279. unsigned int flags = (map->flags & ~GNTMAP_device_map) |
  280. GNTMAP_host_map;
  281. for (i = 0; i < map->count; i++) {
  282. unsigned long address = (unsigned long)
  283. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  284. BUG_ON(PageHighMem(map->pages[i]));
  285. gnttab_set_map_op(&map->kmap_ops[i], address, flags,
  286. map->grants[i].ref,
  287. map->grants[i].domid);
  288. gnttab_set_unmap_op(&map->kunmap_ops[i], address,
  289. flags, -1);
  290. }
  291. }
  292. pr_debug("map %d+%d\n", map->index, map->count);
  293. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  294. map->pages, map->count);
  295. for (i = 0; i < map->count; i++) {
  296. if (map->map_ops[i].status == GNTST_okay)
  297. map->unmap_ops[i].handle = map->map_ops[i].handle;
  298. else if (!err)
  299. err = -EINVAL;
  300. if (map->flags & GNTMAP_device_map)
  301. map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
  302. if (use_ptemod) {
  303. if (map->kmap_ops[i].status == GNTST_okay)
  304. map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
  305. else if (!err)
  306. err = -EINVAL;
  307. }
  308. }
  309. return err;
  310. }
  311. static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
  312. int pages)
  313. {
  314. int i, err = 0;
  315. struct gntab_unmap_queue_data unmap_data;
  316. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  317. int pgno = (map->notify.addr >> PAGE_SHIFT);
  318. if (pgno >= offset && pgno < offset + pages) {
  319. /* No need for kmap, pages are in lowmem */
  320. uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
  321. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  322. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  323. }
  324. }
  325. unmap_data.unmap_ops = map->unmap_ops + offset;
  326. unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
  327. unmap_data.pages = map->pages + offset;
  328. unmap_data.count = pages;
  329. err = gnttab_unmap_refs_sync(&unmap_data);
  330. if (err)
  331. return err;
  332. for (i = 0; i < pages; i++) {
  333. if (map->unmap_ops[offset+i].status)
  334. err = -EINVAL;
  335. pr_debug("unmap handle=%d st=%d\n",
  336. map->unmap_ops[offset+i].handle,
  337. map->unmap_ops[offset+i].status);
  338. map->unmap_ops[offset+i].handle = -1;
  339. }
  340. return err;
  341. }
  342. static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
  343. int pages)
  344. {
  345. int range, err = 0;
  346. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  347. /* It is possible the requested range will have a "hole" where we
  348. * already unmapped some of the grants. Only unmap valid ranges.
  349. */
  350. while (pages && !err) {
  351. while (pages && map->unmap_ops[offset].handle == -1) {
  352. offset++;
  353. pages--;
  354. }
  355. range = 0;
  356. while (range < pages) {
  357. if (map->unmap_ops[offset+range].handle == -1)
  358. break;
  359. range++;
  360. }
  361. err = __unmap_grant_pages(map, offset, range);
  362. offset += range;
  363. pages -= range;
  364. }
  365. return err;
  366. }
  367. /* ------------------------------------------------------------------ */
  368. static void gntdev_vma_open(struct vm_area_struct *vma)
  369. {
  370. struct gntdev_grant_map *map = vma->vm_private_data;
  371. pr_debug("gntdev_vma_open %p\n", vma);
  372. refcount_inc(&map->users);
  373. }
  374. static void gntdev_vma_close(struct vm_area_struct *vma)
  375. {
  376. struct gntdev_grant_map *map = vma->vm_private_data;
  377. struct file *file = vma->vm_file;
  378. struct gntdev_priv *priv = file->private_data;
  379. pr_debug("gntdev_vma_close %p\n", vma);
  380. if (use_ptemod) {
  381. /* It is possible that an mmu notifier could be running
  382. * concurrently, so take priv->lock to ensure that the vma won't
  383. * vanishing during the unmap_grant_pages call, since we will
  384. * spin here until that completes. Such a concurrent call will
  385. * not do any unmapping, since that has been done prior to
  386. * closing the vma, but it may still iterate the unmap_ops list.
  387. */
  388. mutex_lock(&priv->lock);
  389. map->vma = NULL;
  390. mutex_unlock(&priv->lock);
  391. }
  392. vma->vm_private_data = NULL;
  393. gntdev_put_map(priv, map);
  394. }
  395. static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
  396. unsigned long addr)
  397. {
  398. struct gntdev_grant_map *map = vma->vm_private_data;
  399. return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
  400. }
  401. static const struct vm_operations_struct gntdev_vmops = {
  402. .open = gntdev_vma_open,
  403. .close = gntdev_vma_close,
  404. .find_special_page = gntdev_vma_find_special_page,
  405. };
  406. /* ------------------------------------------------------------------ */
  407. static bool in_range(struct gntdev_grant_map *map,
  408. unsigned long start, unsigned long end)
  409. {
  410. if (!map->vma)
  411. return false;
  412. if (map->vma->vm_start >= end)
  413. return false;
  414. if (map->vma->vm_end <= start)
  415. return false;
  416. return true;
  417. }
  418. static int unmap_if_in_range(struct gntdev_grant_map *map,
  419. unsigned long start, unsigned long end,
  420. bool blockable)
  421. {
  422. unsigned long mstart, mend;
  423. int err;
  424. if (!in_range(map, start, end))
  425. return 0;
  426. if (!blockable)
  427. return -EAGAIN;
  428. mstart = max(start, map->vma->vm_start);
  429. mend = min(end, map->vma->vm_end);
  430. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  431. map->index, map->count,
  432. map->vma->vm_start, map->vma->vm_end,
  433. start, end, mstart, mend);
  434. err = unmap_grant_pages(map,
  435. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  436. (mend - mstart) >> PAGE_SHIFT);
  437. WARN_ON(err);
  438. return 0;
  439. }
  440. static int mn_invl_range_start(struct mmu_notifier *mn,
  441. struct mm_struct *mm,
  442. unsigned long start, unsigned long end,
  443. bool blockable)
  444. {
  445. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  446. struct gntdev_grant_map *map;
  447. int ret = 0;
  448. if (blockable)
  449. mutex_lock(&priv->lock);
  450. else if (!mutex_trylock(&priv->lock))
  451. return -EAGAIN;
  452. list_for_each_entry(map, &priv->maps, next) {
  453. ret = unmap_if_in_range(map, start, end, blockable);
  454. if (ret)
  455. goto out_unlock;
  456. }
  457. list_for_each_entry(map, &priv->freeable_maps, next) {
  458. ret = unmap_if_in_range(map, start, end, blockable);
  459. if (ret)
  460. goto out_unlock;
  461. }
  462. out_unlock:
  463. mutex_unlock(&priv->lock);
  464. return ret;
  465. }
  466. static void mn_release(struct mmu_notifier *mn,
  467. struct mm_struct *mm)
  468. {
  469. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  470. struct gntdev_grant_map *map;
  471. int err;
  472. mutex_lock(&priv->lock);
  473. list_for_each_entry(map, &priv->maps, next) {
  474. if (!map->vma)
  475. continue;
  476. pr_debug("map %d+%d (%lx %lx)\n",
  477. map->index, map->count,
  478. map->vma->vm_start, map->vma->vm_end);
  479. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  480. WARN_ON(err);
  481. }
  482. list_for_each_entry(map, &priv->freeable_maps, next) {
  483. if (!map->vma)
  484. continue;
  485. pr_debug("map %d+%d (%lx %lx)\n",
  486. map->index, map->count,
  487. map->vma->vm_start, map->vma->vm_end);
  488. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  489. WARN_ON(err);
  490. }
  491. mutex_unlock(&priv->lock);
  492. }
  493. static const struct mmu_notifier_ops gntdev_mmu_ops = {
  494. .release = mn_release,
  495. .invalidate_range_start = mn_invl_range_start,
  496. };
  497. /* ------------------------------------------------------------------ */
  498. static int gntdev_open(struct inode *inode, struct file *flip)
  499. {
  500. struct gntdev_priv *priv;
  501. int ret = 0;
  502. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  503. if (!priv)
  504. return -ENOMEM;
  505. INIT_LIST_HEAD(&priv->maps);
  506. INIT_LIST_HEAD(&priv->freeable_maps);
  507. mutex_init(&priv->lock);
  508. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  509. priv->dmabuf_priv = gntdev_dmabuf_init(flip);
  510. if (IS_ERR(priv->dmabuf_priv)) {
  511. ret = PTR_ERR(priv->dmabuf_priv);
  512. kfree(priv);
  513. return ret;
  514. }
  515. #endif
  516. if (use_ptemod) {
  517. priv->mm = get_task_mm(current);
  518. if (!priv->mm) {
  519. kfree(priv);
  520. return -ENOMEM;
  521. }
  522. priv->mn.ops = &gntdev_mmu_ops;
  523. ret = mmu_notifier_register(&priv->mn, priv->mm);
  524. mmput(priv->mm);
  525. }
  526. if (ret) {
  527. kfree(priv);
  528. return ret;
  529. }
  530. flip->private_data = priv;
  531. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  532. priv->dma_dev = gntdev_miscdev.this_device;
  533. /*
  534. * The device is not spawn from a device tree, so arch_setup_dma_ops
  535. * is not called, thus leaving the device with dummy DMA ops.
  536. * Fix this by calling of_dma_configure() with a NULL node to set
  537. * default DMA ops.
  538. */
  539. of_dma_configure(priv->dma_dev, NULL, true);
  540. #endif
  541. pr_debug("priv %p\n", priv);
  542. return 0;
  543. }
  544. static int gntdev_release(struct inode *inode, struct file *flip)
  545. {
  546. struct gntdev_priv *priv = flip->private_data;
  547. struct gntdev_grant_map *map;
  548. pr_debug("priv %p\n", priv);
  549. mutex_lock(&priv->lock);
  550. while (!list_empty(&priv->maps)) {
  551. map = list_entry(priv->maps.next,
  552. struct gntdev_grant_map, next);
  553. list_del(&map->next);
  554. gntdev_put_map(NULL /* already removed */, map);
  555. }
  556. WARN_ON(!list_empty(&priv->freeable_maps));
  557. mutex_unlock(&priv->lock);
  558. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  559. gntdev_dmabuf_fini(priv->dmabuf_priv);
  560. #endif
  561. if (use_ptemod)
  562. mmu_notifier_unregister(&priv->mn, priv->mm);
  563. kfree(priv);
  564. return 0;
  565. }
  566. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  567. struct ioctl_gntdev_map_grant_ref __user *u)
  568. {
  569. struct ioctl_gntdev_map_grant_ref op;
  570. struct gntdev_grant_map *map;
  571. int err;
  572. if (copy_from_user(&op, u, sizeof(op)) != 0)
  573. return -EFAULT;
  574. pr_debug("priv %p, add %d\n", priv, op.count);
  575. if (unlikely(op.count <= 0))
  576. return -EINVAL;
  577. err = -ENOMEM;
  578. map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
  579. if (!map)
  580. return err;
  581. if (unlikely(gntdev_account_mapped_pages(op.count))) {
  582. pr_debug("can't map: over limit\n");
  583. gntdev_put_map(NULL, map);
  584. return err;
  585. }
  586. if (copy_from_user(map->grants, &u->refs,
  587. sizeof(map->grants[0]) * op.count) != 0) {
  588. gntdev_put_map(NULL, map);
  589. return -EFAULT;
  590. }
  591. mutex_lock(&priv->lock);
  592. gntdev_add_map(priv, map);
  593. op.index = map->index << PAGE_SHIFT;
  594. mutex_unlock(&priv->lock);
  595. if (copy_to_user(u, &op, sizeof(op)) != 0)
  596. return -EFAULT;
  597. return 0;
  598. }
  599. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  600. struct ioctl_gntdev_unmap_grant_ref __user *u)
  601. {
  602. struct ioctl_gntdev_unmap_grant_ref op;
  603. struct gntdev_grant_map *map;
  604. int err = -ENOENT;
  605. if (copy_from_user(&op, u, sizeof(op)) != 0)
  606. return -EFAULT;
  607. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  608. mutex_lock(&priv->lock);
  609. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  610. if (map) {
  611. list_del(&map->next);
  612. if (populate_freeable_maps)
  613. list_add_tail(&map->next, &priv->freeable_maps);
  614. err = 0;
  615. }
  616. mutex_unlock(&priv->lock);
  617. if (map)
  618. gntdev_put_map(priv, map);
  619. return err;
  620. }
  621. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  622. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  623. {
  624. struct ioctl_gntdev_get_offset_for_vaddr op;
  625. struct vm_area_struct *vma;
  626. struct gntdev_grant_map *map;
  627. int rv = -EINVAL;
  628. if (copy_from_user(&op, u, sizeof(op)) != 0)
  629. return -EFAULT;
  630. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  631. down_read(&current->mm->mmap_sem);
  632. vma = find_vma(current->mm, op.vaddr);
  633. if (!vma || vma->vm_ops != &gntdev_vmops)
  634. goto out_unlock;
  635. map = vma->vm_private_data;
  636. if (!map)
  637. goto out_unlock;
  638. op.offset = map->index << PAGE_SHIFT;
  639. op.count = map->count;
  640. rv = 0;
  641. out_unlock:
  642. up_read(&current->mm->mmap_sem);
  643. if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
  644. return -EFAULT;
  645. return rv;
  646. }
  647. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  648. {
  649. struct ioctl_gntdev_unmap_notify op;
  650. struct gntdev_grant_map *map;
  651. int rc;
  652. int out_flags;
  653. unsigned int out_event;
  654. if (copy_from_user(&op, u, sizeof(op)))
  655. return -EFAULT;
  656. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  657. return -EINVAL;
  658. /* We need to grab a reference to the event channel we are going to use
  659. * to send the notify before releasing the reference we may already have
  660. * (if someone has called this ioctl twice). This is required so that
  661. * it is possible to change the clear_byte part of the notification
  662. * without disturbing the event channel part, which may now be the last
  663. * reference to that event channel.
  664. */
  665. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  666. if (evtchn_get(op.event_channel_port))
  667. return -EINVAL;
  668. }
  669. out_flags = op.action;
  670. out_event = op.event_channel_port;
  671. mutex_lock(&priv->lock);
  672. list_for_each_entry(map, &priv->maps, next) {
  673. uint64_t begin = map->index << PAGE_SHIFT;
  674. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  675. if (op.index >= begin && op.index < end)
  676. goto found;
  677. }
  678. rc = -ENOENT;
  679. goto unlock_out;
  680. found:
  681. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  682. (map->flags & GNTMAP_readonly)) {
  683. rc = -EINVAL;
  684. goto unlock_out;
  685. }
  686. out_flags = map->notify.flags;
  687. out_event = map->notify.event;
  688. map->notify.flags = op.action;
  689. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  690. map->notify.event = op.event_channel_port;
  691. rc = 0;
  692. unlock_out:
  693. mutex_unlock(&priv->lock);
  694. /* Drop the reference to the event channel we did not save in the map */
  695. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  696. evtchn_put(out_event);
  697. return rc;
  698. }
  699. #define GNTDEV_COPY_BATCH 16
  700. struct gntdev_copy_batch {
  701. struct gnttab_copy ops[GNTDEV_COPY_BATCH];
  702. struct page *pages[GNTDEV_COPY_BATCH];
  703. s16 __user *status[GNTDEV_COPY_BATCH];
  704. unsigned int nr_ops;
  705. unsigned int nr_pages;
  706. bool writeable;
  707. };
  708. static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
  709. unsigned long *gfn)
  710. {
  711. unsigned long addr = (unsigned long)virt;
  712. struct page *page;
  713. unsigned long xen_pfn;
  714. int ret;
  715. ret = get_user_pages_fast(addr, 1, batch->writeable, &page);
  716. if (ret < 0)
  717. return ret;
  718. batch->pages[batch->nr_pages++] = page;
  719. xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
  720. *gfn = pfn_to_gfn(xen_pfn);
  721. return 0;
  722. }
  723. static void gntdev_put_pages(struct gntdev_copy_batch *batch)
  724. {
  725. unsigned int i;
  726. for (i = 0; i < batch->nr_pages; i++) {
  727. if (batch->writeable && !PageDirty(batch->pages[i]))
  728. set_page_dirty_lock(batch->pages[i]);
  729. put_page(batch->pages[i]);
  730. }
  731. batch->nr_pages = 0;
  732. batch->writeable = false;
  733. }
  734. static int gntdev_copy(struct gntdev_copy_batch *batch)
  735. {
  736. unsigned int i;
  737. gnttab_batch_copy(batch->ops, batch->nr_ops);
  738. gntdev_put_pages(batch);
  739. /*
  740. * For each completed op, update the status if the op failed
  741. * and all previous ops for the segment were successful.
  742. */
  743. for (i = 0; i < batch->nr_ops; i++) {
  744. s16 status = batch->ops[i].status;
  745. s16 old_status;
  746. if (status == GNTST_okay)
  747. continue;
  748. if (__get_user(old_status, batch->status[i]))
  749. return -EFAULT;
  750. if (old_status != GNTST_okay)
  751. continue;
  752. if (__put_user(status, batch->status[i]))
  753. return -EFAULT;
  754. }
  755. batch->nr_ops = 0;
  756. return 0;
  757. }
  758. static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
  759. struct gntdev_grant_copy_segment *seg,
  760. s16 __user *status)
  761. {
  762. uint16_t copied = 0;
  763. /*
  764. * Disallow local -> local copies since there is only space in
  765. * batch->pages for one page per-op and this would be a very
  766. * expensive memcpy().
  767. */
  768. if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
  769. return -EINVAL;
  770. /* Can't cross page if source/dest is a grant ref. */
  771. if (seg->flags & GNTCOPY_source_gref) {
  772. if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
  773. return -EINVAL;
  774. }
  775. if (seg->flags & GNTCOPY_dest_gref) {
  776. if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
  777. return -EINVAL;
  778. }
  779. if (put_user(GNTST_okay, status))
  780. return -EFAULT;
  781. while (copied < seg->len) {
  782. struct gnttab_copy *op;
  783. void __user *virt;
  784. size_t len, off;
  785. unsigned long gfn;
  786. int ret;
  787. if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
  788. ret = gntdev_copy(batch);
  789. if (ret < 0)
  790. return ret;
  791. }
  792. len = seg->len - copied;
  793. op = &batch->ops[batch->nr_ops];
  794. op->flags = 0;
  795. if (seg->flags & GNTCOPY_source_gref) {
  796. op->source.u.ref = seg->source.foreign.ref;
  797. op->source.domid = seg->source.foreign.domid;
  798. op->source.offset = seg->source.foreign.offset + copied;
  799. op->flags |= GNTCOPY_source_gref;
  800. } else {
  801. virt = seg->source.virt + copied;
  802. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  803. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  804. batch->writeable = false;
  805. ret = gntdev_get_page(batch, virt, &gfn);
  806. if (ret < 0)
  807. return ret;
  808. op->source.u.gmfn = gfn;
  809. op->source.domid = DOMID_SELF;
  810. op->source.offset = off;
  811. }
  812. if (seg->flags & GNTCOPY_dest_gref) {
  813. op->dest.u.ref = seg->dest.foreign.ref;
  814. op->dest.domid = seg->dest.foreign.domid;
  815. op->dest.offset = seg->dest.foreign.offset + copied;
  816. op->flags |= GNTCOPY_dest_gref;
  817. } else {
  818. virt = seg->dest.virt + copied;
  819. off = (unsigned long)virt & ~XEN_PAGE_MASK;
  820. len = min(len, (size_t)XEN_PAGE_SIZE - off);
  821. batch->writeable = true;
  822. ret = gntdev_get_page(batch, virt, &gfn);
  823. if (ret < 0)
  824. return ret;
  825. op->dest.u.gmfn = gfn;
  826. op->dest.domid = DOMID_SELF;
  827. op->dest.offset = off;
  828. }
  829. op->len = len;
  830. copied += len;
  831. batch->status[batch->nr_ops] = status;
  832. batch->nr_ops++;
  833. }
  834. return 0;
  835. }
  836. static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
  837. {
  838. struct ioctl_gntdev_grant_copy copy;
  839. struct gntdev_copy_batch batch;
  840. unsigned int i;
  841. int ret = 0;
  842. if (copy_from_user(&copy, u, sizeof(copy)))
  843. return -EFAULT;
  844. batch.nr_ops = 0;
  845. batch.nr_pages = 0;
  846. for (i = 0; i < copy.count; i++) {
  847. struct gntdev_grant_copy_segment seg;
  848. if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
  849. ret = -EFAULT;
  850. goto out;
  851. }
  852. ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
  853. if (ret < 0)
  854. goto out;
  855. cond_resched();
  856. }
  857. if (batch.nr_ops)
  858. ret = gntdev_copy(&batch);
  859. return ret;
  860. out:
  861. gntdev_put_pages(&batch);
  862. return ret;
  863. }
  864. static long gntdev_ioctl(struct file *flip,
  865. unsigned int cmd, unsigned long arg)
  866. {
  867. struct gntdev_priv *priv = flip->private_data;
  868. void __user *ptr = (void __user *)arg;
  869. switch (cmd) {
  870. case IOCTL_GNTDEV_MAP_GRANT_REF:
  871. return gntdev_ioctl_map_grant_ref(priv, ptr);
  872. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  873. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  874. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  875. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  876. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  877. return gntdev_ioctl_notify(priv, ptr);
  878. case IOCTL_GNTDEV_GRANT_COPY:
  879. return gntdev_ioctl_grant_copy(priv, ptr);
  880. #ifdef CONFIG_XEN_GNTDEV_DMABUF
  881. case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
  882. return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
  883. case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
  884. return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
  885. case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
  886. return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
  887. case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
  888. return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
  889. #endif
  890. default:
  891. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  892. return -ENOIOCTLCMD;
  893. }
  894. return 0;
  895. }
  896. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  897. {
  898. struct gntdev_priv *priv = flip->private_data;
  899. int index = vma->vm_pgoff;
  900. int count = vma_pages(vma);
  901. struct gntdev_grant_map *map;
  902. int i, err = -EINVAL;
  903. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  904. return -EINVAL;
  905. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  906. index, count, vma->vm_start, vma->vm_pgoff);
  907. mutex_lock(&priv->lock);
  908. map = gntdev_find_map_index(priv, index, count);
  909. if (!map)
  910. goto unlock_out;
  911. if (use_ptemod && map->vma)
  912. goto unlock_out;
  913. if (use_ptemod && priv->mm != vma->vm_mm) {
  914. pr_warn("Huh? Other mm?\n");
  915. goto unlock_out;
  916. }
  917. refcount_inc(&map->users);
  918. vma->vm_ops = &gntdev_vmops;
  919. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
  920. if (use_ptemod)
  921. vma->vm_flags |= VM_DONTCOPY;
  922. vma->vm_private_data = map;
  923. if (use_ptemod)
  924. map->vma = vma;
  925. if (map->flags) {
  926. if ((vma->vm_flags & VM_WRITE) &&
  927. (map->flags & GNTMAP_readonly))
  928. goto out_unlock_put;
  929. } else {
  930. map->flags = GNTMAP_host_map;
  931. if (!(vma->vm_flags & VM_WRITE))
  932. map->flags |= GNTMAP_readonly;
  933. }
  934. mutex_unlock(&priv->lock);
  935. if (use_ptemod) {
  936. map->pages_vm_start = vma->vm_start;
  937. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  938. vma->vm_end - vma->vm_start,
  939. find_grant_ptes, map);
  940. if (err) {
  941. pr_warn("find_grant_ptes() failure.\n");
  942. goto out_put_map;
  943. }
  944. }
  945. err = gntdev_map_grant_pages(map);
  946. if (err)
  947. goto out_put_map;
  948. if (!use_ptemod) {
  949. for (i = 0; i < count; i++) {
  950. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  951. map->pages[i]);
  952. if (err)
  953. goto out_put_map;
  954. }
  955. } else {
  956. #ifdef CONFIG_X86
  957. /*
  958. * If the PTEs were not made special by the grant map
  959. * hypercall, do so here.
  960. *
  961. * This is racy since the mapping is already visible
  962. * to userspace but userspace should be well-behaved
  963. * enough to not touch it until the mmap() call
  964. * returns.
  965. */
  966. if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
  967. apply_to_page_range(vma->vm_mm, vma->vm_start,
  968. vma->vm_end - vma->vm_start,
  969. set_grant_ptes_as_special, NULL);
  970. }
  971. #endif
  972. }
  973. return 0;
  974. unlock_out:
  975. mutex_unlock(&priv->lock);
  976. return err;
  977. out_unlock_put:
  978. mutex_unlock(&priv->lock);
  979. out_put_map:
  980. if (use_ptemod) {
  981. map->vma = NULL;
  982. unmap_grant_pages(map, 0, map->count);
  983. }
  984. gntdev_put_map(priv, map);
  985. return err;
  986. }
  987. static const struct file_operations gntdev_fops = {
  988. .owner = THIS_MODULE,
  989. .open = gntdev_open,
  990. .release = gntdev_release,
  991. .mmap = gntdev_mmap,
  992. .unlocked_ioctl = gntdev_ioctl
  993. };
  994. static struct miscdevice gntdev_miscdev = {
  995. .minor = MISC_DYNAMIC_MINOR,
  996. .name = "xen/gntdev",
  997. .fops = &gntdev_fops,
  998. };
  999. /* ------------------------------------------------------------------ */
  1000. static int __init gntdev_init(void)
  1001. {
  1002. int err;
  1003. if (!xen_domain())
  1004. return -ENODEV;
  1005. use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
  1006. err = misc_register(&gntdev_miscdev);
  1007. if (err != 0) {
  1008. pr_err("Could not register gntdev device\n");
  1009. return err;
  1010. }
  1011. return 0;
  1012. }
  1013. static void __exit gntdev_exit(void)
  1014. {
  1015. misc_deregister(&gntdev_miscdev);
  1016. }
  1017. module_init(gntdev_init);
  1018. module_exit(gntdev_exit);
  1019. /* ------------------------------------------------------------------ */