vhost.c 59 KB

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