vhost.c 59 KB

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