gntdev.c 30 KB

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