vhost.c 63 KB

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