vhost.c 57 KB

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