vhost.c 59 KB

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