vhost.c 57 KB

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