gntdev.c 30 KB

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