vhost.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. /* Copyright (C) 2009 Red Hat, Inc.
  2. * Copyright (C) 2006 Rusty Russell IBM Corporation
  3. *
  4. * Author: Michael S. Tsirkin <mst@redhat.com>
  5. *
  6. * Inspiration, some code, and most witty comments come from
  7. * Documentation/virtual/lguest/lguest.c, by Rusty Russell
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2.
  10. *
  11. * Generic code for virtio server in host kernel.
  12. */
  13. #include <linux/eventfd.h>
  14. #include <linux/vhost.h>
  15. #include <linux/uio.h>
  16. #include <linux/mm.h>
  17. #include <linux/mmu_context.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/mutex.h>
  20. #include <linux/poll.h>
  21. #include <linux/file.h>
  22. #include <linux/highmem.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/kthread.h>
  26. #include <linux/cgroup.h>
  27. #include <linux/module.h>
  28. #include <linux/sort.h>
  29. #include <linux/sched/mm.h>
  30. #include <linux/sched/signal.h>
  31. #include <linux/interval_tree_generic.h>
  32. #include "vhost.h"
  33. static ushort max_mem_regions = 64;
  34. module_param(max_mem_regions, ushort, 0444);
  35. MODULE_PARM_DESC(max_mem_regions,
  36. "Maximum number of memory regions in memory map. (default: 64)");
  37. static int max_iotlb_entries = 2048;
  38. module_param(max_iotlb_entries, int, 0444);
  39. MODULE_PARM_DESC(max_iotlb_entries,
  40. "Maximum number of iotlb entries. (default: 2048)");
  41. enum {
  42. VHOST_MEMORY_F_LOG = 0x1,
  43. };
  44. #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
  45. #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
  46. INTERVAL_TREE_DEFINE(struct vhost_umem_node,
  47. rb, __u64, __subtree_last,
  48. START, LAST, static inline, vhost_umem_interval_tree);
  49. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  50. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  51. {
  52. vq->user_be = !virtio_legacy_is_little_endian();
  53. }
  54. static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
  55. {
  56. vq->user_be = true;
  57. }
  58. static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
  59. {
  60. vq->user_be = false;
  61. }
  62. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  63. {
  64. struct vhost_vring_state s;
  65. if (vq->private_data)
  66. return -EBUSY;
  67. if (copy_from_user(&s, argp, sizeof(s)))
  68. return -EFAULT;
  69. if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
  70. s.num != VHOST_VRING_BIG_ENDIAN)
  71. return -EINVAL;
  72. if (s.num == VHOST_VRING_BIG_ENDIAN)
  73. vhost_enable_cross_endian_big(vq);
  74. else
  75. vhost_enable_cross_endian_little(vq);
  76. return 0;
  77. }
  78. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  79. int __user *argp)
  80. {
  81. struct vhost_vring_state s = {
  82. .index = idx,
  83. .num = vq->user_be
  84. };
  85. if (copy_to_user(argp, &s, sizeof(s)))
  86. return -EFAULT;
  87. return 0;
  88. }
  89. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  90. {
  91. /* Note for legacy virtio: user_be is initialized at reset time
  92. * according to the host endianness. If userspace does not set an
  93. * explicit endianness, the default behavior is native endian, as
  94. * expected by legacy virtio.
  95. */
  96. vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
  97. }
  98. #else
  99. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  100. {
  101. }
  102. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  103. {
  104. return -ENOIOCTLCMD;
  105. }
  106. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  107. int __user *argp)
  108. {
  109. return -ENOIOCTLCMD;
  110. }
  111. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  112. {
  113. vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
  114. || virtio_legacy_is_little_endian();
  115. }
  116. #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
  117. static void vhost_reset_is_le(struct vhost_virtqueue *vq)
  118. {
  119. vhost_init_is_le(vq);
  120. }
  121. struct vhost_flush_struct {
  122. struct vhost_work work;
  123. struct completion wait_event;
  124. };
  125. static void vhost_flush_work(struct vhost_work *work)
  126. {
  127. struct vhost_flush_struct *s;
  128. s = container_of(work, struct vhost_flush_struct, work);
  129. complete(&s->wait_event);
  130. }
  131. static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
  132. poll_table *pt)
  133. {
  134. struct vhost_poll *poll;
  135. poll = container_of(pt, struct vhost_poll, table);
  136. poll->wqh = wqh;
  137. add_wait_queue(wqh, &poll->wait);
  138. }
  139. static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
  140. void *key)
  141. {
  142. struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
  143. if (!(key_to_poll(key) & poll->mask))
  144. return 0;
  145. vhost_poll_queue(poll);
  146. return 0;
  147. }
  148. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
  149. {
  150. clear_bit(VHOST_WORK_QUEUED, &work->flags);
  151. work->fn = fn;
  152. }
  153. EXPORT_SYMBOL_GPL(vhost_work_init);
  154. /* Init poll structure */
  155. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  156. __poll_t mask, struct vhost_dev *dev)
  157. {
  158. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  159. init_poll_funcptr(&poll->table, vhost_poll_func);
  160. poll->mask = mask;
  161. poll->dev = dev;
  162. poll->wqh = NULL;
  163. vhost_work_init(&poll->work, fn);
  164. }
  165. EXPORT_SYMBOL_GPL(vhost_poll_init);
  166. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  167. * keep a reference to a file until after vhost_poll_stop is called. */
  168. int vhost_poll_start(struct vhost_poll *poll, struct file *file)
  169. {
  170. __poll_t mask;
  171. int ret = 0;
  172. if (poll->wqh)
  173. return 0;
  174. mask = vfs_poll(file, &poll->table);
  175. if (mask)
  176. vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
  177. if (mask & EPOLLERR) {
  178. vhost_poll_stop(poll);
  179. ret = -EINVAL;
  180. }
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(vhost_poll_start);
  184. /* Stop polling a file. After this function returns, it becomes safe to drop the
  185. * file reference. You must also flush afterwards. */
  186. void vhost_poll_stop(struct vhost_poll *poll)
  187. {
  188. if (poll->wqh) {
  189. remove_wait_queue(poll->wqh, &poll->wait);
  190. poll->wqh = NULL;
  191. }
  192. }
  193. EXPORT_SYMBOL_GPL(vhost_poll_stop);
  194. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
  195. {
  196. struct vhost_flush_struct flush;
  197. if (dev->worker) {
  198. init_completion(&flush.wait_event);
  199. vhost_work_init(&flush.work, vhost_flush_work);
  200. vhost_work_queue(dev, &flush.work);
  201. wait_for_completion(&flush.wait_event);
  202. }
  203. }
  204. EXPORT_SYMBOL_GPL(vhost_work_flush);
  205. /* Flush any work that has been scheduled. When calling this, don't hold any
  206. * locks that are also used by the callback. */
  207. void vhost_poll_flush(struct vhost_poll *poll)
  208. {
  209. vhost_work_flush(poll->dev, &poll->work);
  210. }
  211. EXPORT_SYMBOL_GPL(vhost_poll_flush);
  212. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
  213. {
  214. if (!dev->worker)
  215. return;
  216. if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
  217. /* We can only add the work to the list after we're
  218. * sure it was not in the list.
  219. * test_and_set_bit() implies a memory barrier.
  220. */
  221. llist_add(&work->node, &dev->work_list);
  222. wake_up_process(dev->worker);
  223. }
  224. }
  225. EXPORT_SYMBOL_GPL(vhost_work_queue);
  226. /* A lockless hint for busy polling code to exit the loop */
  227. bool vhost_has_work(struct vhost_dev *dev)
  228. {
  229. return !llist_empty(&dev->work_list);
  230. }
  231. EXPORT_SYMBOL_GPL(vhost_has_work);
  232. void vhost_poll_queue(struct vhost_poll *poll)
  233. {
  234. vhost_work_queue(poll->dev, &poll->work);
  235. }
  236. EXPORT_SYMBOL_GPL(vhost_poll_queue);
  237. static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
  238. {
  239. int j;
  240. for (j = 0; j < VHOST_NUM_ADDRS; j++)
  241. vq->meta_iotlb[j] = NULL;
  242. }
  243. static void vhost_vq_meta_reset(struct vhost_dev *d)
  244. {
  245. int i;
  246. for (i = 0; i < d->nvqs; ++i)
  247. __vhost_vq_meta_reset(d->vqs[i]);
  248. }
  249. static void vhost_vq_reset(struct vhost_dev *dev,
  250. struct vhost_virtqueue *vq)
  251. {
  252. vq->num = 1;
  253. vq->desc = NULL;
  254. vq->avail = NULL;
  255. vq->used = NULL;
  256. vq->last_avail_idx = 0;
  257. vq->avail_idx = 0;
  258. vq->last_used_idx = 0;
  259. vq->signalled_used = 0;
  260. vq->signalled_used_valid = false;
  261. vq->used_flags = 0;
  262. vq->log_used = false;
  263. vq->log_addr = -1ull;
  264. vq->private_data = NULL;
  265. vq->acked_features = 0;
  266. vq->acked_backend_features = 0;
  267. vq->log_base = NULL;
  268. vq->error_ctx = NULL;
  269. vq->kick = NULL;
  270. vq->call_ctx = NULL;
  271. vq->log_ctx = NULL;
  272. vhost_reset_is_le(vq);
  273. vhost_disable_cross_endian(vq);
  274. vq->busyloop_timeout = 0;
  275. vq->umem = NULL;
  276. vq->iotlb = NULL;
  277. __vhost_vq_meta_reset(vq);
  278. }
  279. static int vhost_worker(void *data)
  280. {
  281. struct vhost_dev *dev = data;
  282. struct vhost_work *work, *work_next;
  283. struct llist_node *node;
  284. mm_segment_t oldfs = get_fs();
  285. set_fs(USER_DS);
  286. use_mm(dev->mm);
  287. for (;;) {
  288. /* mb paired w/ kthread_stop */
  289. set_current_state(TASK_INTERRUPTIBLE);
  290. if (kthread_should_stop()) {
  291. __set_current_state(TASK_RUNNING);
  292. break;
  293. }
  294. node = llist_del_all(&dev->work_list);
  295. if (!node)
  296. schedule();
  297. node = llist_reverse_order(node);
  298. /* make sure flag is seen after deletion */
  299. smp_wmb();
  300. llist_for_each_entry_safe(work, work_next, node, node) {
  301. clear_bit(VHOST_WORK_QUEUED, &work->flags);
  302. __set_current_state(TASK_RUNNING);
  303. work->fn(work);
  304. if (need_resched())
  305. schedule();
  306. }
  307. }
  308. unuse_mm(dev->mm);
  309. set_fs(oldfs);
  310. return 0;
  311. }
  312. static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  313. {
  314. kfree(vq->indirect);
  315. vq->indirect = NULL;
  316. kfree(vq->log);
  317. vq->log = NULL;
  318. kfree(vq->heads);
  319. vq->heads = NULL;
  320. }
  321. /* Helper to allocate iovec buffers for all vqs. */
  322. static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  323. {
  324. struct vhost_virtqueue *vq;
  325. int i;
  326. for (i = 0; i < dev->nvqs; ++i) {
  327. vq = dev->vqs[i];
  328. vq->indirect = kmalloc_array(UIO_MAXIOV,
  329. sizeof(*vq->indirect),
  330. GFP_KERNEL);
  331. vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
  332. GFP_KERNEL);
  333. vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
  334. GFP_KERNEL);
  335. if (!vq->indirect || !vq->log || !vq->heads)
  336. goto err_nomem;
  337. }
  338. return 0;
  339. err_nomem:
  340. for (; i >= 0; --i)
  341. vhost_vq_free_iovecs(dev->vqs[i]);
  342. return -ENOMEM;
  343. }
  344. static void vhost_dev_free_iovecs(struct vhost_dev *dev)
  345. {
  346. int i;
  347. for (i = 0; i < dev->nvqs; ++i)
  348. vhost_vq_free_iovecs(dev->vqs[i]);
  349. }
  350. void vhost_dev_init(struct vhost_dev *dev,
  351. struct vhost_virtqueue **vqs, int nvqs)
  352. {
  353. struct vhost_virtqueue *vq;
  354. int i;
  355. dev->vqs = vqs;
  356. dev->nvqs = nvqs;
  357. mutex_init(&dev->mutex);
  358. dev->log_ctx = NULL;
  359. dev->umem = NULL;
  360. dev->iotlb = NULL;
  361. dev->mm = NULL;
  362. dev->worker = NULL;
  363. init_llist_head(&dev->work_list);
  364. init_waitqueue_head(&dev->wait);
  365. INIT_LIST_HEAD(&dev->read_list);
  366. INIT_LIST_HEAD(&dev->pending_list);
  367. spin_lock_init(&dev->iotlb_lock);
  368. for (i = 0; i < dev->nvqs; ++i) {
  369. vq = dev->vqs[i];
  370. vq->log = NULL;
  371. vq->indirect = NULL;
  372. vq->heads = NULL;
  373. vq->dev = dev;
  374. mutex_init(&vq->mutex);
  375. vhost_vq_reset(dev, vq);
  376. if (vq->handle_kick)
  377. vhost_poll_init(&vq->poll, vq->handle_kick,
  378. EPOLLIN, dev);
  379. }
  380. }
  381. EXPORT_SYMBOL_GPL(vhost_dev_init);
  382. /* Caller should have device mutex */
  383. long vhost_dev_check_owner(struct vhost_dev *dev)
  384. {
  385. /* Are you the owner? If not, I don't think you mean to do that */
  386. return dev->mm == current->mm ? 0 : -EPERM;
  387. }
  388. EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
  389. struct vhost_attach_cgroups_struct {
  390. struct vhost_work work;
  391. struct task_struct *owner;
  392. int ret;
  393. };
  394. static void vhost_attach_cgroups_work(struct vhost_work *work)
  395. {
  396. struct vhost_attach_cgroups_struct *s;
  397. s = container_of(work, struct vhost_attach_cgroups_struct, work);
  398. s->ret = cgroup_attach_task_all(s->owner, current);
  399. }
  400. static int vhost_attach_cgroups(struct vhost_dev *dev)
  401. {
  402. struct vhost_attach_cgroups_struct attach;
  403. attach.owner = current;
  404. vhost_work_init(&attach.work, vhost_attach_cgroups_work);
  405. vhost_work_queue(dev, &attach.work);
  406. vhost_work_flush(dev, &attach.work);
  407. return attach.ret;
  408. }
  409. /* Caller should have device mutex */
  410. bool vhost_dev_has_owner(struct vhost_dev *dev)
  411. {
  412. return dev->mm;
  413. }
  414. EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
  415. /* Caller should have device mutex */
  416. long vhost_dev_set_owner(struct vhost_dev *dev)
  417. {
  418. struct task_struct *worker;
  419. int err;
  420. /* Is there an owner already? */
  421. if (vhost_dev_has_owner(dev)) {
  422. err = -EBUSY;
  423. goto err_mm;
  424. }
  425. /* No owner, become one */
  426. dev->mm = get_task_mm(current);
  427. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  428. if (IS_ERR(worker)) {
  429. err = PTR_ERR(worker);
  430. goto err_worker;
  431. }
  432. dev->worker = worker;
  433. wake_up_process(worker); /* avoid contributing to loadavg */
  434. err = vhost_attach_cgroups(dev);
  435. if (err)
  436. goto err_cgroup;
  437. err = vhost_dev_alloc_iovecs(dev);
  438. if (err)
  439. goto err_cgroup;
  440. return 0;
  441. err_cgroup:
  442. kthread_stop(worker);
  443. dev->worker = NULL;
  444. err_worker:
  445. if (dev->mm)
  446. mmput(dev->mm);
  447. dev->mm = NULL;
  448. err_mm:
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
  452. struct vhost_umem *vhost_dev_reset_owner_prepare(void)
  453. {
  454. return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
  455. }
  456. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
  457. /* Caller should have device mutex */
  458. void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
  459. {
  460. int i;
  461. vhost_dev_cleanup(dev);
  462. /* Restore memory to default empty mapping. */
  463. INIT_LIST_HEAD(&umem->umem_list);
  464. dev->umem = umem;
  465. /* We don't need VQ locks below since vhost_dev_cleanup makes sure
  466. * VQs aren't running.
  467. */
  468. for (i = 0; i < dev->nvqs; ++i)
  469. dev->vqs[i]->umem = umem;
  470. }
  471. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
  472. void vhost_dev_stop(struct vhost_dev *dev)
  473. {
  474. int i;
  475. for (i = 0; i < dev->nvqs; ++i) {
  476. if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
  477. vhost_poll_stop(&dev->vqs[i]->poll);
  478. vhost_poll_flush(&dev->vqs[i]->poll);
  479. }
  480. }
  481. }
  482. EXPORT_SYMBOL_GPL(vhost_dev_stop);
  483. static void vhost_umem_free(struct vhost_umem *umem,
  484. struct vhost_umem_node *node)
  485. {
  486. vhost_umem_interval_tree_remove(node, &umem->umem_tree);
  487. list_del(&node->link);
  488. kfree(node);
  489. umem->numem--;
  490. }
  491. static void vhost_umem_clean(struct vhost_umem *umem)
  492. {
  493. struct vhost_umem_node *node, *tmp;
  494. if (!umem)
  495. return;
  496. list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
  497. vhost_umem_free(umem, node);
  498. kvfree(umem);
  499. }
  500. static void vhost_clear_msg(struct vhost_dev *dev)
  501. {
  502. struct vhost_msg_node *node, *n;
  503. spin_lock(&dev->iotlb_lock);
  504. list_for_each_entry_safe(node, n, &dev->read_list, node) {
  505. list_del(&node->node);
  506. kfree(node);
  507. }
  508. list_for_each_entry_safe(node, n, &dev->pending_list, node) {
  509. list_del(&node->node);
  510. kfree(node);
  511. }
  512. spin_unlock(&dev->iotlb_lock);
  513. }
  514. void vhost_dev_cleanup(struct vhost_dev *dev)
  515. {
  516. int i;
  517. for (i = 0; i < dev->nvqs; ++i) {
  518. if (dev->vqs[i]->error_ctx)
  519. eventfd_ctx_put(dev->vqs[i]->error_ctx);
  520. if (dev->vqs[i]->kick)
  521. fput(dev->vqs[i]->kick);
  522. if (dev->vqs[i]->call_ctx)
  523. eventfd_ctx_put(dev->vqs[i]->call_ctx);
  524. vhost_vq_reset(dev, dev->vqs[i]);
  525. }
  526. vhost_dev_free_iovecs(dev);
  527. if (dev->log_ctx)
  528. eventfd_ctx_put(dev->log_ctx);
  529. dev->log_ctx = NULL;
  530. /* No one will access memory at this point */
  531. vhost_umem_clean(dev->umem);
  532. dev->umem = NULL;
  533. vhost_umem_clean(dev->iotlb);
  534. dev->iotlb = NULL;
  535. vhost_clear_msg(dev);
  536. wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
  537. WARN_ON(!llist_empty(&dev->work_list));
  538. if (dev->worker) {
  539. kthread_stop(dev->worker);
  540. dev->worker = NULL;
  541. }
  542. if (dev->mm)
  543. mmput(dev->mm);
  544. dev->mm = NULL;
  545. }
  546. EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
  547. static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  548. {
  549. u64 a = addr / VHOST_PAGE_SIZE / 8;
  550. /* Make sure 64 bit math will not overflow. */
  551. if (a > ULONG_MAX - (unsigned long)log_base ||
  552. a + (unsigned long)log_base > ULONG_MAX)
  553. return false;
  554. return access_ok(VERIFY_WRITE, log_base + a,
  555. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  556. }
  557. static bool vhost_overflow(u64 uaddr, u64 size)
  558. {
  559. /* Make sure 64 bit math will not overflow. */
  560. return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
  561. }
  562. /* Caller should have vq mutex and device mutex. */
  563. static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
  564. int log_all)
  565. {
  566. struct vhost_umem_node *node;
  567. if (!umem)
  568. return false;
  569. list_for_each_entry(node, &umem->umem_list, link) {
  570. unsigned long a = node->userspace_addr;
  571. if (vhost_overflow(node->userspace_addr, node->size))
  572. return false;
  573. if (!access_ok(VERIFY_WRITE, (void __user *)a,
  574. node->size))
  575. return false;
  576. else if (log_all && !log_access_ok(log_base,
  577. node->start,
  578. node->size))
  579. return false;
  580. }
  581. return true;
  582. }
  583. static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
  584. u64 addr, unsigned int size,
  585. int type)
  586. {
  587. const struct vhost_umem_node *node = vq->meta_iotlb[type];
  588. if (!node)
  589. return NULL;
  590. return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
  591. }
  592. /* Can we switch to this memory table? */
  593. /* Caller should have device mutex but not vq mutex */
  594. static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
  595. int log_all)
  596. {
  597. int i;
  598. for (i = 0; i < d->nvqs; ++i) {
  599. bool ok;
  600. bool log;
  601. mutex_lock(&d->vqs[i]->mutex);
  602. log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
  603. /* If ring is inactive, will check when it's enabled. */
  604. if (d->vqs[i]->private_data)
  605. ok = vq_memory_access_ok(d->vqs[i]->log_base,
  606. umem, log);
  607. else
  608. ok = true;
  609. mutex_unlock(&d->vqs[i]->mutex);
  610. if (!ok)
  611. return false;
  612. }
  613. return true;
  614. }
  615. static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
  616. struct iovec iov[], int iov_size, int access);
  617. static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
  618. const void *from, unsigned size)
  619. {
  620. int ret;
  621. if (!vq->iotlb)
  622. return __copy_to_user(to, from, size);
  623. else {
  624. /* This function should be called after iotlb
  625. * prefetch, which means we're sure that all vq
  626. * could be access through iotlb. So -EAGAIN should
  627. * not happen in this case.
  628. */
  629. struct iov_iter t;
  630. void __user *uaddr = vhost_vq_meta_fetch(vq,
  631. (u64)(uintptr_t)to, size,
  632. VHOST_ADDR_USED);
  633. if (uaddr)
  634. return __copy_to_user(uaddr, from, size);
  635. ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
  636. ARRAY_SIZE(vq->iotlb_iov),
  637. VHOST_ACCESS_WO);
  638. if (ret < 0)
  639. goto out;
  640. iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
  641. ret = copy_to_iter(from, size, &t);
  642. if (ret == size)
  643. ret = 0;
  644. }
  645. out:
  646. return ret;
  647. }
  648. static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
  649. void __user *from, unsigned size)
  650. {
  651. int ret;
  652. if (!vq->iotlb)
  653. return __copy_from_user(to, from, size);
  654. else {
  655. /* This function should be called after iotlb
  656. * prefetch, which means we're sure that vq
  657. * could be access through iotlb. So -EAGAIN should
  658. * not happen in this case.
  659. */
  660. void __user *uaddr = vhost_vq_meta_fetch(vq,
  661. (u64)(uintptr_t)from, size,
  662. VHOST_ADDR_DESC);
  663. struct iov_iter f;
  664. if (uaddr)
  665. return __copy_from_user(to, uaddr, size);
  666. ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
  667. ARRAY_SIZE(vq->iotlb_iov),
  668. VHOST_ACCESS_RO);
  669. if (ret < 0) {
  670. vq_err(vq, "IOTLB translation failure: uaddr "
  671. "%p size 0x%llx\n", from,
  672. (unsigned long long) size);
  673. goto out;
  674. }
  675. iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
  676. ret = copy_from_iter(to, size, &f);
  677. if (ret == size)
  678. ret = 0;
  679. }
  680. out:
  681. return ret;
  682. }
  683. static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
  684. void __user *addr, unsigned int size,
  685. int type)
  686. {
  687. int ret;
  688. ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
  689. ARRAY_SIZE(vq->iotlb_iov),
  690. VHOST_ACCESS_RO);
  691. if (ret < 0) {
  692. vq_err(vq, "IOTLB translation failure: uaddr "
  693. "%p size 0x%llx\n", addr,
  694. (unsigned long long) size);
  695. return NULL;
  696. }
  697. if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
  698. vq_err(vq, "Non atomic userspace memory access: uaddr "
  699. "%p size 0x%llx\n", addr,
  700. (unsigned long long) size);
  701. return NULL;
  702. }
  703. return vq->iotlb_iov[0].iov_base;
  704. }
  705. /* This function should be called after iotlb
  706. * prefetch, which means we're sure that vq
  707. * could be access through iotlb. So -EAGAIN should
  708. * not happen in this case.
  709. */
  710. static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
  711. void *addr, unsigned int size,
  712. int type)
  713. {
  714. void __user *uaddr = vhost_vq_meta_fetch(vq,
  715. (u64)(uintptr_t)addr, size, type);
  716. if (uaddr)
  717. return uaddr;
  718. return __vhost_get_user_slow(vq, addr, size, type);
  719. }
  720. #define vhost_put_user(vq, x, ptr) \
  721. ({ \
  722. int ret = -EFAULT; \
  723. if (!vq->iotlb) { \
  724. ret = __put_user(x, ptr); \
  725. } else { \
  726. __typeof__(ptr) to = \
  727. (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
  728. sizeof(*ptr), VHOST_ADDR_USED); \
  729. if (to != NULL) \
  730. ret = __put_user(x, to); \
  731. else \
  732. ret = -EFAULT; \
  733. } \
  734. ret; \
  735. })
  736. #define vhost_get_user(vq, x, ptr, type) \
  737. ({ \
  738. int ret; \
  739. if (!vq->iotlb) { \
  740. ret = __get_user(x, ptr); \
  741. } else { \
  742. __typeof__(ptr) from = \
  743. (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
  744. sizeof(*ptr), \
  745. type); \
  746. if (from != NULL) \
  747. ret = __get_user(x, from); \
  748. else \
  749. ret = -EFAULT; \
  750. } \
  751. ret; \
  752. })
  753. #define vhost_get_avail(vq, x, ptr) \
  754. vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
  755. #define vhost_get_used(vq, x, ptr) \
  756. vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
  757. static void vhost_dev_lock_vqs(struct vhost_dev *d)
  758. {
  759. int i = 0;
  760. for (i = 0; i < d->nvqs; ++i)
  761. mutex_lock_nested(&d->vqs[i]->mutex, i);
  762. }
  763. static void vhost_dev_unlock_vqs(struct vhost_dev *d)
  764. {
  765. int i = 0;
  766. for (i = 0; i < d->nvqs; ++i)
  767. mutex_unlock(&d->vqs[i]->mutex);
  768. }
  769. static int vhost_new_umem_range(struct vhost_umem *umem,
  770. u64 start, u64 size, u64 end,
  771. u64 userspace_addr, int perm)
  772. {
  773. struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
  774. if (!node)
  775. return -ENOMEM;
  776. if (umem->numem == max_iotlb_entries) {
  777. tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
  778. vhost_umem_free(umem, tmp);
  779. }
  780. node->start = start;
  781. node->size = size;
  782. node->last = end;
  783. node->userspace_addr = userspace_addr;
  784. node->perm = perm;
  785. INIT_LIST_HEAD(&node->link);
  786. list_add_tail(&node->link, &umem->umem_list);
  787. vhost_umem_interval_tree_insert(node, &umem->umem_tree);
  788. umem->numem++;
  789. return 0;
  790. }
  791. static void vhost_del_umem_range(struct vhost_umem *umem,
  792. u64 start, u64 end)
  793. {
  794. struct vhost_umem_node *node;
  795. while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  796. start, end)))
  797. vhost_umem_free(umem, node);
  798. }
  799. static void vhost_iotlb_notify_vq(struct vhost_dev *d,
  800. struct vhost_iotlb_msg *msg)
  801. {
  802. struct vhost_msg_node *node, *n;
  803. spin_lock(&d->iotlb_lock);
  804. list_for_each_entry_safe(node, n, &d->pending_list, node) {
  805. struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
  806. if (msg->iova <= vq_msg->iova &&
  807. msg->iova + msg->size - 1 >= vq_msg->iova &&
  808. vq_msg->type == VHOST_IOTLB_MISS) {
  809. vhost_poll_queue(&node->vq->poll);
  810. list_del(&node->node);
  811. kfree(node);
  812. }
  813. }
  814. spin_unlock(&d->iotlb_lock);
  815. }
  816. static bool umem_access_ok(u64 uaddr, u64 size, int access)
  817. {
  818. unsigned long a = uaddr;
  819. /* Make sure 64 bit math will not overflow. */
  820. if (vhost_overflow(uaddr, size))
  821. return false;
  822. if ((access & VHOST_ACCESS_RO) &&
  823. !access_ok(VERIFY_READ, (void __user *)a, size))
  824. return false;
  825. if ((access & VHOST_ACCESS_WO) &&
  826. !access_ok(VERIFY_WRITE, (void __user *)a, size))
  827. return false;
  828. return true;
  829. }
  830. static int vhost_process_iotlb_msg(struct vhost_dev *dev,
  831. struct vhost_iotlb_msg *msg)
  832. {
  833. int ret = 0;
  834. mutex_lock(&dev->mutex);
  835. vhost_dev_lock_vqs(dev);
  836. switch (msg->type) {
  837. case VHOST_IOTLB_UPDATE:
  838. if (!dev->iotlb) {
  839. ret = -EFAULT;
  840. break;
  841. }
  842. if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
  843. ret = -EFAULT;
  844. break;
  845. }
  846. vhost_vq_meta_reset(dev);
  847. if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
  848. msg->iova + msg->size - 1,
  849. msg->uaddr, msg->perm)) {
  850. ret = -ENOMEM;
  851. break;
  852. }
  853. vhost_iotlb_notify_vq(dev, msg);
  854. break;
  855. case VHOST_IOTLB_INVALIDATE:
  856. if (!dev->iotlb) {
  857. ret = -EFAULT;
  858. break;
  859. }
  860. vhost_vq_meta_reset(dev);
  861. vhost_del_umem_range(dev->iotlb, msg->iova,
  862. msg->iova + msg->size - 1);
  863. break;
  864. default:
  865. ret = -EINVAL;
  866. break;
  867. }
  868. vhost_dev_unlock_vqs(dev);
  869. mutex_unlock(&dev->mutex);
  870. return ret;
  871. }
  872. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  873. struct iov_iter *from)
  874. {
  875. struct vhost_iotlb_msg msg;
  876. size_t offset;
  877. int type, ret;
  878. ret = copy_from_iter(&type, sizeof(type), from);
  879. if (ret != sizeof(type))
  880. goto done;
  881. switch (type) {
  882. case VHOST_IOTLB_MSG:
  883. /* There maybe a hole after type for V1 message type,
  884. * so skip it here.
  885. */
  886. offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
  887. break;
  888. case VHOST_IOTLB_MSG_V2:
  889. offset = sizeof(__u32);
  890. break;
  891. default:
  892. ret = -EINVAL;
  893. goto done;
  894. }
  895. iov_iter_advance(from, offset);
  896. ret = copy_from_iter(&msg, sizeof(msg), from);
  897. if (ret != sizeof(msg))
  898. goto done;
  899. if (vhost_process_iotlb_msg(dev, &msg)) {
  900. ret = -EFAULT;
  901. goto done;
  902. }
  903. ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
  904. sizeof(struct vhost_msg_v2);
  905. done:
  906. return ret;
  907. }
  908. EXPORT_SYMBOL(vhost_chr_write_iter);
  909. __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  910. poll_table *wait)
  911. {
  912. __poll_t mask = 0;
  913. poll_wait(file, &dev->wait, wait);
  914. if (!list_empty(&dev->read_list))
  915. mask |= EPOLLIN | EPOLLRDNORM;
  916. return mask;
  917. }
  918. EXPORT_SYMBOL(vhost_chr_poll);
  919. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  920. int noblock)
  921. {
  922. DEFINE_WAIT(wait);
  923. struct vhost_msg_node *node;
  924. ssize_t ret = 0;
  925. unsigned size = sizeof(struct vhost_msg);
  926. if (iov_iter_count(to) < size)
  927. return 0;
  928. while (1) {
  929. if (!noblock)
  930. prepare_to_wait(&dev->wait, &wait,
  931. TASK_INTERRUPTIBLE);
  932. node = vhost_dequeue_msg(dev, &dev->read_list);
  933. if (node)
  934. break;
  935. if (noblock) {
  936. ret = -EAGAIN;
  937. break;
  938. }
  939. if (signal_pending(current)) {
  940. ret = -ERESTARTSYS;
  941. break;
  942. }
  943. if (!dev->iotlb) {
  944. ret = -EBADFD;
  945. break;
  946. }
  947. schedule();
  948. }
  949. if (!noblock)
  950. finish_wait(&dev->wait, &wait);
  951. if (node) {
  952. struct vhost_iotlb_msg *msg;
  953. void *start = &node->msg;
  954. switch (node->msg.type) {
  955. case VHOST_IOTLB_MSG:
  956. size = sizeof(node->msg);
  957. msg = &node->msg.iotlb;
  958. break;
  959. case VHOST_IOTLB_MSG_V2:
  960. size = sizeof(node->msg_v2);
  961. msg = &node->msg_v2.iotlb;
  962. break;
  963. default:
  964. BUG();
  965. break;
  966. }
  967. ret = copy_to_iter(start, size, to);
  968. if (ret != size || msg->type != VHOST_IOTLB_MISS) {
  969. kfree(node);
  970. return ret;
  971. }
  972. vhost_enqueue_msg(dev, &dev->pending_list, node);
  973. }
  974. return ret;
  975. }
  976. EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
  977. static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
  978. {
  979. struct vhost_dev *dev = vq->dev;
  980. struct vhost_msg_node *node;
  981. struct vhost_iotlb_msg *msg;
  982. bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
  983. node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
  984. if (!node)
  985. return -ENOMEM;
  986. if (v2) {
  987. node->msg_v2.type = VHOST_IOTLB_MSG_V2;
  988. msg = &node->msg_v2.iotlb;
  989. } else {
  990. msg = &node->msg.iotlb;
  991. }
  992. msg->type = VHOST_IOTLB_MISS;
  993. msg->iova = iova;
  994. msg->perm = access;
  995. vhost_enqueue_msg(dev, &dev->read_list, node);
  996. return 0;
  997. }
  998. static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
  999. struct vring_desc __user *desc,
  1000. struct vring_avail __user *avail,
  1001. struct vring_used __user *used)
  1002. {
  1003. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  1004. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  1005. access_ok(VERIFY_READ, avail,
  1006. sizeof *avail + num * sizeof *avail->ring + s) &&
  1007. access_ok(VERIFY_WRITE, used,
  1008. sizeof *used + num * sizeof *used->ring + s);
  1009. }
  1010. static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
  1011. const struct vhost_umem_node *node,
  1012. int type)
  1013. {
  1014. int access = (type == VHOST_ADDR_USED) ?
  1015. VHOST_ACCESS_WO : VHOST_ACCESS_RO;
  1016. if (likely(node->perm & access))
  1017. vq->meta_iotlb[type] = node;
  1018. }
  1019. static bool iotlb_access_ok(struct vhost_virtqueue *vq,
  1020. int access, u64 addr, u64 len, int type)
  1021. {
  1022. const struct vhost_umem_node *node;
  1023. struct vhost_umem *umem = vq->iotlb;
  1024. u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
  1025. if (vhost_vq_meta_fetch(vq, addr, len, type))
  1026. return true;
  1027. while (len > s) {
  1028. node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  1029. addr,
  1030. last);
  1031. if (node == NULL || node->start > addr) {
  1032. vhost_iotlb_miss(vq, addr, access);
  1033. return false;
  1034. } else if (!(node->perm & access)) {
  1035. /* Report the possible access violation by
  1036. * request another translation from userspace.
  1037. */
  1038. return false;
  1039. }
  1040. size = node->size - addr + node->start;
  1041. if (orig_addr == addr && size >= len)
  1042. vhost_vq_meta_update(vq, node, type);
  1043. s += size;
  1044. addr += size;
  1045. }
  1046. return true;
  1047. }
  1048. int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
  1049. {
  1050. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  1051. unsigned int num = vq->num;
  1052. if (!vq->iotlb)
  1053. return 1;
  1054. return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
  1055. num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
  1056. iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
  1057. sizeof *vq->avail +
  1058. num * sizeof(*vq->avail->ring) + s,
  1059. VHOST_ADDR_AVAIL) &&
  1060. iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
  1061. sizeof *vq->used +
  1062. num * sizeof(*vq->used->ring) + s,
  1063. VHOST_ADDR_USED);
  1064. }
  1065. EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
  1066. /* Can we log writes? */
  1067. /* Caller should have device mutex but not vq mutex */
  1068. bool vhost_log_access_ok(struct vhost_dev *dev)
  1069. {
  1070. return memory_access_ok(dev, dev->umem, 1);
  1071. }
  1072. EXPORT_SYMBOL_GPL(vhost_log_access_ok);
  1073. /* Verify access for write logging. */
  1074. /* Caller should have vq mutex and device mutex */
  1075. static bool vq_log_access_ok(struct vhost_virtqueue *vq,
  1076. void __user *log_base)
  1077. {
  1078. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  1079. return vq_memory_access_ok(log_base, vq->umem,
  1080. vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
  1081. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  1082. sizeof *vq->used +
  1083. vq->num * sizeof *vq->used->ring + s));
  1084. }
  1085. /* Can we start vq? */
  1086. /* Caller should have vq mutex and device mutex */
  1087. bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
  1088. {
  1089. if (!vq_log_access_ok(vq, vq->log_base))
  1090. return false;
  1091. /* Access validation occurs at prefetch time with IOTLB */
  1092. if (vq->iotlb)
  1093. return true;
  1094. return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
  1095. }
  1096. EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
  1097. static struct vhost_umem *vhost_umem_alloc(void)
  1098. {
  1099. struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
  1100. if (!umem)
  1101. return NULL;
  1102. umem->umem_tree = RB_ROOT_CACHED;
  1103. umem->numem = 0;
  1104. INIT_LIST_HEAD(&umem->umem_list);
  1105. return umem;
  1106. }
  1107. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  1108. {
  1109. struct vhost_memory mem, *newmem;
  1110. struct vhost_memory_region *region;
  1111. struct vhost_umem *newumem, *oldumem;
  1112. unsigned long size = offsetof(struct vhost_memory, regions);
  1113. int i;
  1114. if (copy_from_user(&mem, m, size))
  1115. return -EFAULT;
  1116. if (mem.padding)
  1117. return -EOPNOTSUPP;
  1118. if (mem.nregions > max_mem_regions)
  1119. return -E2BIG;
  1120. newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
  1121. GFP_KERNEL);
  1122. if (!newmem)
  1123. return -ENOMEM;
  1124. memcpy(newmem, &mem, size);
  1125. if (copy_from_user(newmem->regions, m->regions,
  1126. mem.nregions * sizeof *m->regions)) {
  1127. kvfree(newmem);
  1128. return -EFAULT;
  1129. }
  1130. newumem = vhost_umem_alloc();
  1131. if (!newumem) {
  1132. kvfree(newmem);
  1133. return -ENOMEM;
  1134. }
  1135. for (region = newmem->regions;
  1136. region < newmem->regions + mem.nregions;
  1137. region++) {
  1138. if (vhost_new_umem_range(newumem,
  1139. region->guest_phys_addr,
  1140. region->memory_size,
  1141. region->guest_phys_addr +
  1142. region->memory_size - 1,
  1143. region->userspace_addr,
  1144. VHOST_ACCESS_RW))
  1145. goto err;
  1146. }
  1147. if (!memory_access_ok(d, newumem, 0))
  1148. goto err;
  1149. oldumem = d->umem;
  1150. d->umem = newumem;
  1151. /* All memory accesses are done under some VQ mutex. */
  1152. for (i = 0; i < d->nvqs; ++i) {
  1153. mutex_lock(&d->vqs[i]->mutex);
  1154. d->vqs[i]->umem = newumem;
  1155. mutex_unlock(&d->vqs[i]->mutex);
  1156. }
  1157. kvfree(newmem);
  1158. vhost_umem_clean(oldumem);
  1159. return 0;
  1160. err:
  1161. vhost_umem_clean(newumem);
  1162. kvfree(newmem);
  1163. return -EFAULT;
  1164. }
  1165. long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  1166. {
  1167. struct file *eventfp, *filep = NULL;
  1168. bool pollstart = false, pollstop = false;
  1169. struct eventfd_ctx *ctx = NULL;
  1170. u32 __user *idxp = argp;
  1171. struct vhost_virtqueue *vq;
  1172. struct vhost_vring_state s;
  1173. struct vhost_vring_file f;
  1174. struct vhost_vring_addr a;
  1175. u32 idx;
  1176. long r;
  1177. r = get_user(idx, idxp);
  1178. if (r < 0)
  1179. return r;
  1180. if (idx >= d->nvqs)
  1181. return -ENOBUFS;
  1182. vq = d->vqs[idx];
  1183. mutex_lock(&vq->mutex);
  1184. switch (ioctl) {
  1185. case VHOST_SET_VRING_NUM:
  1186. /* Resizing ring with an active backend?
  1187. * You don't want to do that. */
  1188. if (vq->private_data) {
  1189. r = -EBUSY;
  1190. break;
  1191. }
  1192. if (copy_from_user(&s, argp, sizeof s)) {
  1193. r = -EFAULT;
  1194. break;
  1195. }
  1196. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  1197. r = -EINVAL;
  1198. break;
  1199. }
  1200. vq->num = s.num;
  1201. break;
  1202. case VHOST_SET_VRING_BASE:
  1203. /* Moving base with an active backend?
  1204. * You don't want to do that. */
  1205. if (vq->private_data) {
  1206. r = -EBUSY;
  1207. break;
  1208. }
  1209. if (copy_from_user(&s, argp, sizeof s)) {
  1210. r = -EFAULT;
  1211. break;
  1212. }
  1213. if (s.num > 0xffff) {
  1214. r = -EINVAL;
  1215. break;
  1216. }
  1217. vq->last_avail_idx = s.num;
  1218. /* Forget the cached index value. */
  1219. vq->avail_idx = vq->last_avail_idx;
  1220. break;
  1221. case VHOST_GET_VRING_BASE:
  1222. s.index = idx;
  1223. s.num = vq->last_avail_idx;
  1224. if (copy_to_user(argp, &s, sizeof s))
  1225. r = -EFAULT;
  1226. break;
  1227. case VHOST_SET_VRING_ADDR:
  1228. if (copy_from_user(&a, argp, sizeof a)) {
  1229. r = -EFAULT;
  1230. break;
  1231. }
  1232. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  1233. r = -EOPNOTSUPP;
  1234. break;
  1235. }
  1236. /* For 32bit, verify that the top 32bits of the user
  1237. data are set to zero. */
  1238. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  1239. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  1240. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  1241. r = -EFAULT;
  1242. break;
  1243. }
  1244. /* Make sure it's safe to cast pointers to vring types. */
  1245. BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
  1246. BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
  1247. if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
  1248. (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
  1249. (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
  1250. r = -EINVAL;
  1251. break;
  1252. }
  1253. /* We only verify access here if backend is configured.
  1254. * If it is not, we don't as size might not have been setup.
  1255. * We will verify when backend is configured. */
  1256. if (vq->private_data) {
  1257. if (!vq_access_ok(vq, vq->num,
  1258. (void __user *)(unsigned long)a.desc_user_addr,
  1259. (void __user *)(unsigned long)a.avail_user_addr,
  1260. (void __user *)(unsigned long)a.used_user_addr)) {
  1261. r = -EINVAL;
  1262. break;
  1263. }
  1264. /* Also validate log access for used ring if enabled. */
  1265. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  1266. !log_access_ok(vq->log_base, a.log_guest_addr,
  1267. sizeof *vq->used +
  1268. vq->num * sizeof *vq->used->ring)) {
  1269. r = -EINVAL;
  1270. break;
  1271. }
  1272. }
  1273. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  1274. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  1275. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  1276. vq->log_addr = a.log_guest_addr;
  1277. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  1278. break;
  1279. case VHOST_SET_VRING_KICK:
  1280. if (copy_from_user(&f, argp, sizeof f)) {
  1281. r = -EFAULT;
  1282. break;
  1283. }
  1284. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  1285. if (IS_ERR(eventfp)) {
  1286. r = PTR_ERR(eventfp);
  1287. break;
  1288. }
  1289. if (eventfp != vq->kick) {
  1290. pollstop = (filep = vq->kick) != NULL;
  1291. pollstart = (vq->kick = eventfp) != NULL;
  1292. } else
  1293. filep = eventfp;
  1294. break;
  1295. case VHOST_SET_VRING_CALL:
  1296. if (copy_from_user(&f, argp, sizeof f)) {
  1297. r = -EFAULT;
  1298. break;
  1299. }
  1300. ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
  1301. if (IS_ERR(ctx)) {
  1302. r = PTR_ERR(ctx);
  1303. break;
  1304. }
  1305. swap(ctx, vq->call_ctx);
  1306. break;
  1307. case VHOST_SET_VRING_ERR:
  1308. if (copy_from_user(&f, argp, sizeof f)) {
  1309. r = -EFAULT;
  1310. break;
  1311. }
  1312. ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
  1313. if (IS_ERR(ctx)) {
  1314. r = PTR_ERR(ctx);
  1315. break;
  1316. }
  1317. swap(ctx, vq->error_ctx);
  1318. break;
  1319. case VHOST_SET_VRING_ENDIAN:
  1320. r = vhost_set_vring_endian(vq, argp);
  1321. break;
  1322. case VHOST_GET_VRING_ENDIAN:
  1323. r = vhost_get_vring_endian(vq, idx, argp);
  1324. break;
  1325. case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
  1326. if (copy_from_user(&s, argp, sizeof(s))) {
  1327. r = -EFAULT;
  1328. break;
  1329. }
  1330. vq->busyloop_timeout = s.num;
  1331. break;
  1332. case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
  1333. s.index = idx;
  1334. s.num = vq->busyloop_timeout;
  1335. if (copy_to_user(argp, &s, sizeof(s)))
  1336. r = -EFAULT;
  1337. break;
  1338. default:
  1339. r = -ENOIOCTLCMD;
  1340. }
  1341. if (pollstop && vq->handle_kick)
  1342. vhost_poll_stop(&vq->poll);
  1343. if (!IS_ERR_OR_NULL(ctx))
  1344. eventfd_ctx_put(ctx);
  1345. if (filep)
  1346. fput(filep);
  1347. if (pollstart && vq->handle_kick)
  1348. r = vhost_poll_start(&vq->poll, vq->kick);
  1349. mutex_unlock(&vq->mutex);
  1350. if (pollstop && vq->handle_kick)
  1351. vhost_poll_flush(&vq->poll);
  1352. return r;
  1353. }
  1354. EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
  1355. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
  1356. {
  1357. struct vhost_umem *niotlb, *oiotlb;
  1358. int i;
  1359. niotlb = vhost_umem_alloc();
  1360. if (!niotlb)
  1361. return -ENOMEM;
  1362. oiotlb = d->iotlb;
  1363. d->iotlb = niotlb;
  1364. for (i = 0; i < d->nvqs; ++i) {
  1365. struct vhost_virtqueue *vq = d->vqs[i];
  1366. mutex_lock(&vq->mutex);
  1367. vq->iotlb = niotlb;
  1368. __vhost_vq_meta_reset(vq);
  1369. mutex_unlock(&vq->mutex);
  1370. }
  1371. vhost_umem_clean(oiotlb);
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
  1375. /* Caller must have device mutex */
  1376. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  1377. {
  1378. struct eventfd_ctx *ctx;
  1379. u64 p;
  1380. long r;
  1381. int i, fd;
  1382. /* If you are not the owner, you can become one */
  1383. if (ioctl == VHOST_SET_OWNER) {
  1384. r = vhost_dev_set_owner(d);
  1385. goto done;
  1386. }
  1387. /* You must be the owner to do anything else */
  1388. r = vhost_dev_check_owner(d);
  1389. if (r)
  1390. goto done;
  1391. switch (ioctl) {
  1392. case VHOST_SET_MEM_TABLE:
  1393. r = vhost_set_memory(d, argp);
  1394. break;
  1395. case VHOST_SET_LOG_BASE:
  1396. if (copy_from_user(&p, argp, sizeof p)) {
  1397. r = -EFAULT;
  1398. break;
  1399. }
  1400. if ((u64)(unsigned long)p != p) {
  1401. r = -EFAULT;
  1402. break;
  1403. }
  1404. for (i = 0; i < d->nvqs; ++i) {
  1405. struct vhost_virtqueue *vq;
  1406. void __user *base = (void __user *)(unsigned long)p;
  1407. vq = d->vqs[i];
  1408. mutex_lock(&vq->mutex);
  1409. /* If ring is inactive, will check when it's enabled. */
  1410. if (vq->private_data && !vq_log_access_ok(vq, base))
  1411. r = -EFAULT;
  1412. else
  1413. vq->log_base = base;
  1414. mutex_unlock(&vq->mutex);
  1415. }
  1416. break;
  1417. case VHOST_SET_LOG_FD:
  1418. r = get_user(fd, (int __user *)argp);
  1419. if (r < 0)
  1420. break;
  1421. ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
  1422. if (IS_ERR(ctx)) {
  1423. r = PTR_ERR(ctx);
  1424. break;
  1425. }
  1426. swap(ctx, d->log_ctx);
  1427. for (i = 0; i < d->nvqs; ++i) {
  1428. mutex_lock(&d->vqs[i]->mutex);
  1429. d->vqs[i]->log_ctx = d->log_ctx;
  1430. mutex_unlock(&d->vqs[i]->mutex);
  1431. }
  1432. if (ctx)
  1433. eventfd_ctx_put(ctx);
  1434. break;
  1435. default:
  1436. r = -ENOIOCTLCMD;
  1437. break;
  1438. }
  1439. done:
  1440. return r;
  1441. }
  1442. EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
  1443. /* TODO: This is really inefficient. We need something like get_user()
  1444. * (instruction directly accesses the data, with an exception table entry
  1445. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  1446. */
  1447. static int set_bit_to_user(int nr, void __user *addr)
  1448. {
  1449. unsigned long log = (unsigned long)addr;
  1450. struct page *page;
  1451. void *base;
  1452. int bit = nr + (log % PAGE_SIZE) * 8;
  1453. int r;
  1454. r = get_user_pages_fast(log, 1, 1, &page);
  1455. if (r < 0)
  1456. return r;
  1457. BUG_ON(r != 1);
  1458. base = kmap_atomic(page);
  1459. set_bit(bit, base);
  1460. kunmap_atomic(base);
  1461. set_page_dirty_lock(page);
  1462. put_page(page);
  1463. return 0;
  1464. }
  1465. static int log_write(void __user *log_base,
  1466. u64 write_address, u64 write_length)
  1467. {
  1468. u64 write_page = write_address / VHOST_PAGE_SIZE;
  1469. int r;
  1470. if (!write_length)
  1471. return 0;
  1472. write_length += write_address % VHOST_PAGE_SIZE;
  1473. for (;;) {
  1474. u64 base = (u64)(unsigned long)log_base;
  1475. u64 log = base + write_page / 8;
  1476. int bit = write_page % 8;
  1477. if ((u64)(unsigned long)log != log)
  1478. return -EFAULT;
  1479. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  1480. if (r < 0)
  1481. return r;
  1482. if (write_length <= VHOST_PAGE_SIZE)
  1483. break;
  1484. write_length -= VHOST_PAGE_SIZE;
  1485. write_page += 1;
  1486. }
  1487. return r;
  1488. }
  1489. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  1490. unsigned int log_num, u64 len)
  1491. {
  1492. int i, r;
  1493. /* Make sure data written is seen before log. */
  1494. smp_wmb();
  1495. for (i = 0; i < log_num; ++i) {
  1496. u64 l = min(log[i].len, len);
  1497. r = log_write(vq->log_base, log[i].addr, l);
  1498. if (r < 0)
  1499. return r;
  1500. len -= l;
  1501. if (!len) {
  1502. if (vq->log_ctx)
  1503. eventfd_signal(vq->log_ctx, 1);
  1504. return 0;
  1505. }
  1506. }
  1507. /* Length written exceeds what we have stored. This is a bug. */
  1508. BUG();
  1509. return 0;
  1510. }
  1511. EXPORT_SYMBOL_GPL(vhost_log_write);
  1512. static int vhost_update_used_flags(struct vhost_virtqueue *vq)
  1513. {
  1514. void __user *used;
  1515. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
  1516. &vq->used->flags) < 0)
  1517. return -EFAULT;
  1518. if (unlikely(vq->log_used)) {
  1519. /* Make sure the flag is seen before log. */
  1520. smp_wmb();
  1521. /* Log used flag write. */
  1522. used = &vq->used->flags;
  1523. log_write(vq->log_base, vq->log_addr +
  1524. (used - (void __user *)vq->used),
  1525. sizeof vq->used->flags);
  1526. if (vq->log_ctx)
  1527. eventfd_signal(vq->log_ctx, 1);
  1528. }
  1529. return 0;
  1530. }
  1531. static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
  1532. {
  1533. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
  1534. vhost_avail_event(vq)))
  1535. return -EFAULT;
  1536. if (unlikely(vq->log_used)) {
  1537. void __user *used;
  1538. /* Make sure the event is seen before log. */
  1539. smp_wmb();
  1540. /* Log avail event write */
  1541. used = vhost_avail_event(vq);
  1542. log_write(vq->log_base, vq->log_addr +
  1543. (used - (void __user *)vq->used),
  1544. sizeof *vhost_avail_event(vq));
  1545. if (vq->log_ctx)
  1546. eventfd_signal(vq->log_ctx, 1);
  1547. }
  1548. return 0;
  1549. }
  1550. int vhost_vq_init_access(struct vhost_virtqueue *vq)
  1551. {
  1552. __virtio16 last_used_idx;
  1553. int r;
  1554. bool is_le = vq->is_le;
  1555. if (!vq->private_data)
  1556. return 0;
  1557. vhost_init_is_le(vq);
  1558. r = vhost_update_used_flags(vq);
  1559. if (r)
  1560. goto err;
  1561. vq->signalled_used_valid = false;
  1562. if (!vq->iotlb &&
  1563. !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
  1564. r = -EFAULT;
  1565. goto err;
  1566. }
  1567. r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
  1568. if (r) {
  1569. vq_err(vq, "Can't access used idx at %p\n",
  1570. &vq->used->idx);
  1571. goto err;
  1572. }
  1573. vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
  1574. return 0;
  1575. err:
  1576. vq->is_le = is_le;
  1577. return r;
  1578. }
  1579. EXPORT_SYMBOL_GPL(vhost_vq_init_access);
  1580. static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
  1581. struct iovec iov[], int iov_size, int access)
  1582. {
  1583. const struct vhost_umem_node *node;
  1584. struct vhost_dev *dev = vq->dev;
  1585. struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
  1586. struct iovec *_iov;
  1587. u64 s = 0;
  1588. int ret = 0;
  1589. while ((u64)len > s) {
  1590. u64 size;
  1591. if (unlikely(ret >= iov_size)) {
  1592. ret = -ENOBUFS;
  1593. break;
  1594. }
  1595. node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  1596. addr, addr + len - 1);
  1597. if (node == NULL || node->start > addr) {
  1598. if (umem != dev->iotlb) {
  1599. ret = -EFAULT;
  1600. break;
  1601. }
  1602. ret = -EAGAIN;
  1603. break;
  1604. } else if (!(node->perm & access)) {
  1605. ret = -EPERM;
  1606. break;
  1607. }
  1608. _iov = iov + ret;
  1609. size = node->size - addr + node->start;
  1610. _iov->iov_len = min((u64)len - s, size);
  1611. _iov->iov_base = (void __user *)(unsigned long)
  1612. (node->userspace_addr + addr - node->start);
  1613. s += size;
  1614. addr += size;
  1615. ++ret;
  1616. }
  1617. if (ret == -EAGAIN)
  1618. vhost_iotlb_miss(vq, addr, access);
  1619. return ret;
  1620. }
  1621. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  1622. * function returns the next descriptor in the chain,
  1623. * or -1U if we're at the end. */
  1624. static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
  1625. {
  1626. unsigned int next;
  1627. /* If this descriptor says it doesn't chain, we're done. */
  1628. if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
  1629. return -1U;
  1630. /* Check they're not leading us off end of descriptors. */
  1631. next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
  1632. return next;
  1633. }
  1634. static int get_indirect(struct vhost_virtqueue *vq,
  1635. struct iovec iov[], unsigned int iov_size,
  1636. unsigned int *out_num, unsigned int *in_num,
  1637. struct vhost_log *log, unsigned int *log_num,
  1638. struct vring_desc *indirect)
  1639. {
  1640. struct vring_desc desc;
  1641. unsigned int i = 0, count, found = 0;
  1642. u32 len = vhost32_to_cpu(vq, indirect->len);
  1643. struct iov_iter from;
  1644. int ret, access;
  1645. /* Sanity check */
  1646. if (unlikely(len % sizeof desc)) {
  1647. vq_err(vq, "Invalid length in indirect descriptor: "
  1648. "len 0x%llx not multiple of 0x%zx\n",
  1649. (unsigned long long)len,
  1650. sizeof desc);
  1651. return -EINVAL;
  1652. }
  1653. ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
  1654. UIO_MAXIOV, VHOST_ACCESS_RO);
  1655. if (unlikely(ret < 0)) {
  1656. if (ret != -EAGAIN)
  1657. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  1658. return ret;
  1659. }
  1660. iov_iter_init(&from, READ, vq->indirect, ret, len);
  1661. /* We will use the result as an address to read from, so most
  1662. * architectures only need a compiler barrier here. */
  1663. read_barrier_depends();
  1664. count = len / sizeof desc;
  1665. /* Buffers are chained via a 16 bit next field, so
  1666. * we can have at most 2^16 of these. */
  1667. if (unlikely(count > USHRT_MAX + 1)) {
  1668. vq_err(vq, "Indirect buffer length too big: %d\n",
  1669. indirect->len);
  1670. return -E2BIG;
  1671. }
  1672. do {
  1673. unsigned iov_count = *in_num + *out_num;
  1674. if (unlikely(++found > count)) {
  1675. vq_err(vq, "Loop detected: last one at %u "
  1676. "indirect size %u\n",
  1677. i, count);
  1678. return -EINVAL;
  1679. }
  1680. if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
  1681. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  1682. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1683. return -EINVAL;
  1684. }
  1685. if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
  1686. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  1687. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1688. return -EINVAL;
  1689. }
  1690. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
  1691. access = VHOST_ACCESS_WO;
  1692. else
  1693. access = VHOST_ACCESS_RO;
  1694. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1695. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1696. iov_size - iov_count, access);
  1697. if (unlikely(ret < 0)) {
  1698. if (ret != -EAGAIN)
  1699. vq_err(vq, "Translation failure %d indirect idx %d\n",
  1700. ret, i);
  1701. return ret;
  1702. }
  1703. /* If this is an input descriptor, increment that count. */
  1704. if (access == VHOST_ACCESS_WO) {
  1705. *in_num += ret;
  1706. if (unlikely(log)) {
  1707. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1708. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1709. ++*log_num;
  1710. }
  1711. } else {
  1712. /* If it's an output descriptor, they're all supposed
  1713. * to come before any input descriptors. */
  1714. if (unlikely(*in_num)) {
  1715. vq_err(vq, "Indirect descriptor "
  1716. "has out after in: idx %d\n", i);
  1717. return -EINVAL;
  1718. }
  1719. *out_num += ret;
  1720. }
  1721. } while ((i = next_desc(vq, &desc)) != -1);
  1722. return 0;
  1723. }
  1724. /* This looks in the virtqueue and for the first available buffer, and converts
  1725. * it to an iovec for convenient access. Since descriptors consist of some
  1726. * number of output then some number of input descriptors, it's actually two
  1727. * iovecs, but we pack them into one and note how many of each there were.
  1728. *
  1729. * This function returns the descriptor number found, or vq->num (which is
  1730. * never a valid descriptor number) if none was found. A negative code is
  1731. * returned on error. */
  1732. int vhost_get_vq_desc(struct vhost_virtqueue *vq,
  1733. struct iovec iov[], unsigned int iov_size,
  1734. unsigned int *out_num, unsigned int *in_num,
  1735. struct vhost_log *log, unsigned int *log_num)
  1736. {
  1737. struct vring_desc desc;
  1738. unsigned int i, head, found = 0;
  1739. u16 last_avail_idx;
  1740. __virtio16 avail_idx;
  1741. __virtio16 ring_head;
  1742. int ret, access;
  1743. /* Check it isn't doing very strange things with descriptor numbers. */
  1744. last_avail_idx = vq->last_avail_idx;
  1745. if (vq->avail_idx == vq->last_avail_idx) {
  1746. if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
  1747. vq_err(vq, "Failed to access avail idx at %p\n",
  1748. &vq->avail->idx);
  1749. return -EFAULT;
  1750. }
  1751. vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
  1752. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  1753. vq_err(vq, "Guest moved used index from %u to %u",
  1754. last_avail_idx, vq->avail_idx);
  1755. return -EFAULT;
  1756. }
  1757. /* If there's nothing new since last we looked, return
  1758. * invalid.
  1759. */
  1760. if (vq->avail_idx == last_avail_idx)
  1761. return vq->num;
  1762. /* Only get avail ring entries after they have been
  1763. * exposed by guest.
  1764. */
  1765. smp_rmb();
  1766. }
  1767. /* Grab the next descriptor number they're advertising, and increment
  1768. * the index we've seen. */
  1769. if (unlikely(vhost_get_avail(vq, ring_head,
  1770. &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
  1771. vq_err(vq, "Failed to read head: idx %d address %p\n",
  1772. last_avail_idx,
  1773. &vq->avail->ring[last_avail_idx % vq->num]);
  1774. return -EFAULT;
  1775. }
  1776. head = vhost16_to_cpu(vq, ring_head);
  1777. /* If their number is silly, that's an error. */
  1778. if (unlikely(head >= vq->num)) {
  1779. vq_err(vq, "Guest says index %u > %u is available",
  1780. head, vq->num);
  1781. return -EINVAL;
  1782. }
  1783. /* When we start there are none of either input nor output. */
  1784. *out_num = *in_num = 0;
  1785. if (unlikely(log))
  1786. *log_num = 0;
  1787. i = head;
  1788. do {
  1789. unsigned iov_count = *in_num + *out_num;
  1790. if (unlikely(i >= vq->num)) {
  1791. vq_err(vq, "Desc index is %u > %u, head = %u",
  1792. i, vq->num, head);
  1793. return -EINVAL;
  1794. }
  1795. if (unlikely(++found > vq->num)) {
  1796. vq_err(vq, "Loop detected: last one at %u "
  1797. "vq size %u head %u\n",
  1798. i, vq->num, head);
  1799. return -EINVAL;
  1800. }
  1801. ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
  1802. sizeof desc);
  1803. if (unlikely(ret)) {
  1804. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  1805. i, vq->desc + i);
  1806. return -EFAULT;
  1807. }
  1808. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
  1809. ret = get_indirect(vq, iov, iov_size,
  1810. out_num, in_num,
  1811. log, log_num, &desc);
  1812. if (unlikely(ret < 0)) {
  1813. if (ret != -EAGAIN)
  1814. vq_err(vq, "Failure detected "
  1815. "in indirect descriptor at idx %d\n", i);
  1816. return ret;
  1817. }
  1818. continue;
  1819. }
  1820. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
  1821. access = VHOST_ACCESS_WO;
  1822. else
  1823. access = VHOST_ACCESS_RO;
  1824. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1825. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1826. iov_size - iov_count, access);
  1827. if (unlikely(ret < 0)) {
  1828. if (ret != -EAGAIN)
  1829. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  1830. ret, i);
  1831. return ret;
  1832. }
  1833. if (access == VHOST_ACCESS_WO) {
  1834. /* If this is an input descriptor,
  1835. * increment that count. */
  1836. *in_num += ret;
  1837. if (unlikely(log)) {
  1838. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1839. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1840. ++*log_num;
  1841. }
  1842. } else {
  1843. /* If it's an output descriptor, they're all supposed
  1844. * to come before any input descriptors. */
  1845. if (unlikely(*in_num)) {
  1846. vq_err(vq, "Descriptor has out after in: "
  1847. "idx %d\n", i);
  1848. return -EINVAL;
  1849. }
  1850. *out_num += ret;
  1851. }
  1852. } while ((i = next_desc(vq, &desc)) != -1);
  1853. /* On success, increment avail index. */
  1854. vq->last_avail_idx++;
  1855. /* Assume notifications from guest are disabled at this point,
  1856. * if they aren't we would need to update avail_event index. */
  1857. BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
  1858. return head;
  1859. }
  1860. EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
  1861. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1862. void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
  1863. {
  1864. vq->last_avail_idx -= n;
  1865. }
  1866. EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
  1867. /* After we've used one of their buffers, we tell them about it. We'll then
  1868. * want to notify the guest, using eventfd. */
  1869. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1870. {
  1871. struct vring_used_elem heads = {
  1872. cpu_to_vhost32(vq, head),
  1873. cpu_to_vhost32(vq, len)
  1874. };
  1875. return vhost_add_used_n(vq, &heads, 1);
  1876. }
  1877. EXPORT_SYMBOL_GPL(vhost_add_used);
  1878. static int __vhost_add_used_n(struct vhost_virtqueue *vq,
  1879. struct vring_used_elem *heads,
  1880. unsigned count)
  1881. {
  1882. struct vring_used_elem __user *used;
  1883. u16 old, new;
  1884. int start;
  1885. start = vq->last_used_idx & (vq->num - 1);
  1886. used = vq->used->ring + start;
  1887. if (count == 1) {
  1888. if (vhost_put_user(vq, heads[0].id, &used->id)) {
  1889. vq_err(vq, "Failed to write used id");
  1890. return -EFAULT;
  1891. }
  1892. if (vhost_put_user(vq, heads[0].len, &used->len)) {
  1893. vq_err(vq, "Failed to write used len");
  1894. return -EFAULT;
  1895. }
  1896. } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
  1897. vq_err(vq, "Failed to write used");
  1898. return -EFAULT;
  1899. }
  1900. if (unlikely(vq->log_used)) {
  1901. /* Make sure data is seen before log. */
  1902. smp_wmb();
  1903. /* Log used ring entry write. */
  1904. log_write(vq->log_base,
  1905. vq->log_addr +
  1906. ((void __user *)used - (void __user *)vq->used),
  1907. count * sizeof *used);
  1908. }
  1909. old = vq->last_used_idx;
  1910. new = (vq->last_used_idx += count);
  1911. /* If the driver never bothers to signal in a very long while,
  1912. * used index might wrap around. If that happens, invalidate
  1913. * signalled_used index we stored. TODO: make sure driver
  1914. * signals at least once in 2^16 and remove this. */
  1915. if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
  1916. vq->signalled_used_valid = false;
  1917. return 0;
  1918. }
  1919. /* After we've used one of their buffers, we tell them about it. We'll then
  1920. * want to notify the guest, using eventfd. */
  1921. int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
  1922. unsigned count)
  1923. {
  1924. int start, n, r;
  1925. start = vq->last_used_idx & (vq->num - 1);
  1926. n = vq->num - start;
  1927. if (n < count) {
  1928. r = __vhost_add_used_n(vq, heads, n);
  1929. if (r < 0)
  1930. return r;
  1931. heads += n;
  1932. count -= n;
  1933. }
  1934. r = __vhost_add_used_n(vq, heads, count);
  1935. /* Make sure buffer is written before we update index. */
  1936. smp_wmb();
  1937. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
  1938. &vq->used->idx)) {
  1939. vq_err(vq, "Failed to increment used idx");
  1940. return -EFAULT;
  1941. }
  1942. if (unlikely(vq->log_used)) {
  1943. /* Log used index update. */
  1944. log_write(vq->log_base,
  1945. vq->log_addr + offsetof(struct vring_used, idx),
  1946. sizeof vq->used->idx);
  1947. if (vq->log_ctx)
  1948. eventfd_signal(vq->log_ctx, 1);
  1949. }
  1950. return r;
  1951. }
  1952. EXPORT_SYMBOL_GPL(vhost_add_used_n);
  1953. static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1954. {
  1955. __u16 old, new;
  1956. __virtio16 event;
  1957. bool v;
  1958. /* Flush out used index updates. This is paired
  1959. * with the barrier that the Guest executes when enabling
  1960. * interrupts. */
  1961. smp_mb();
  1962. if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1963. unlikely(vq->avail_idx == vq->last_avail_idx))
  1964. return true;
  1965. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  1966. __virtio16 flags;
  1967. if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
  1968. vq_err(vq, "Failed to get flags");
  1969. return true;
  1970. }
  1971. return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
  1972. }
  1973. old = vq->signalled_used;
  1974. v = vq->signalled_used_valid;
  1975. new = vq->signalled_used = vq->last_used_idx;
  1976. vq->signalled_used_valid = true;
  1977. if (unlikely(!v))
  1978. return true;
  1979. if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
  1980. vq_err(vq, "Failed to get used event idx");
  1981. return true;
  1982. }
  1983. return vring_need_event(vhost16_to_cpu(vq, event), new, old);
  1984. }
  1985. /* This actually signals the guest, using eventfd. */
  1986. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1987. {
  1988. /* Signal the Guest tell them we used something up. */
  1989. if (vq->call_ctx && vhost_notify(dev, vq))
  1990. eventfd_signal(vq->call_ctx, 1);
  1991. }
  1992. EXPORT_SYMBOL_GPL(vhost_signal);
  1993. /* And here's the combo meal deal. Supersize me! */
  1994. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1995. struct vhost_virtqueue *vq,
  1996. unsigned int head, int len)
  1997. {
  1998. vhost_add_used(vq, head, len);
  1999. vhost_signal(dev, vq);
  2000. }
  2001. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
  2002. /* multi-buffer version of vhost_add_used_and_signal */
  2003. void vhost_add_used_and_signal_n(struct vhost_dev *dev,
  2004. struct vhost_virtqueue *vq,
  2005. struct vring_used_elem *heads, unsigned count)
  2006. {
  2007. vhost_add_used_n(vq, heads, count);
  2008. vhost_signal(dev, vq);
  2009. }
  2010. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
  2011. /* return true if we're sure that avaiable ring is empty */
  2012. bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2013. {
  2014. __virtio16 avail_idx;
  2015. int r;
  2016. if (vq->avail_idx != vq->last_avail_idx)
  2017. return false;
  2018. r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
  2019. if (unlikely(r))
  2020. return false;
  2021. vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
  2022. return vq->avail_idx == vq->last_avail_idx;
  2023. }
  2024. EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
  2025. /* OK, now we need to know about added descriptors. */
  2026. bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2027. {
  2028. __virtio16 avail_idx;
  2029. int r;
  2030. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  2031. return false;
  2032. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  2033. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  2034. r = vhost_update_used_flags(vq);
  2035. if (r) {
  2036. vq_err(vq, "Failed to enable notification at %p: %d\n",
  2037. &vq->used->flags, r);
  2038. return false;
  2039. }
  2040. } else {
  2041. r = vhost_update_avail_event(vq, vq->avail_idx);
  2042. if (r) {
  2043. vq_err(vq, "Failed to update avail event index at %p: %d\n",
  2044. vhost_avail_event(vq), r);
  2045. return false;
  2046. }
  2047. }
  2048. /* They could have slipped one in as we were doing that: make
  2049. * sure it's written, then check again. */
  2050. smp_mb();
  2051. r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
  2052. if (r) {
  2053. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  2054. &vq->avail->idx, r);
  2055. return false;
  2056. }
  2057. return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
  2058. }
  2059. EXPORT_SYMBOL_GPL(vhost_enable_notify);
  2060. /* We don't need to be notified again. */
  2061. void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2062. {
  2063. int r;
  2064. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  2065. return;
  2066. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  2067. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  2068. r = vhost_update_used_flags(vq);
  2069. if (r)
  2070. vq_err(vq, "Failed to enable notification at %p: %d\n",
  2071. &vq->used->flags, r);
  2072. }
  2073. }
  2074. EXPORT_SYMBOL_GPL(vhost_disable_notify);
  2075. /* Create a new message. */
  2076. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
  2077. {
  2078. struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
  2079. if (!node)
  2080. return NULL;
  2081. /* Make sure all padding within the structure is initialized. */
  2082. memset(&node->msg, 0, sizeof node->msg);
  2083. node->vq = vq;
  2084. node->msg.type = type;
  2085. return node;
  2086. }
  2087. EXPORT_SYMBOL_GPL(vhost_new_msg);
  2088. void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
  2089. struct vhost_msg_node *node)
  2090. {
  2091. spin_lock(&dev->iotlb_lock);
  2092. list_add_tail(&node->node, head);
  2093. spin_unlock(&dev->iotlb_lock);
  2094. wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
  2095. }
  2096. EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
  2097. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  2098. struct list_head *head)
  2099. {
  2100. struct vhost_msg_node *node = NULL;
  2101. spin_lock(&dev->iotlb_lock);
  2102. if (!list_empty(head)) {
  2103. node = list_first_entry(head, struct vhost_msg_node,
  2104. node);
  2105. list_del(&node->node);
  2106. }
  2107. spin_unlock(&dev->iotlb_lock);
  2108. return node;
  2109. }
  2110. EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
  2111. static int __init vhost_init(void)
  2112. {
  2113. return 0;
  2114. }
  2115. static void __exit vhost_exit(void)
  2116. {
  2117. }
  2118. module_init(vhost_init);
  2119. module_exit(vhost_exit);
  2120. MODULE_VERSION("0.0.1");
  2121. MODULE_LICENSE("GPL v2");
  2122. MODULE_AUTHOR("Michael S. Tsirkin");
  2123. MODULE_DESCRIPTION("Host kernel accelerator for virtio");