vhost.c 58 KB

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