vhost.c 63 KB

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