vhost.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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 "vhost.h"
  30. static ushort max_mem_regions = 64;
  31. module_param(max_mem_regions, ushort, 0444);
  32. MODULE_PARM_DESC(max_mem_regions,
  33. "Maximum number of memory regions in memory map. (default: 64)");
  34. enum {
  35. VHOST_MEMORY_F_LOG = 0x1,
  36. };
  37. #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
  38. #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
  39. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  40. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  41. {
  42. vq->user_be = !virtio_legacy_is_little_endian();
  43. }
  44. static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
  45. {
  46. vq->user_be = true;
  47. }
  48. static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
  49. {
  50. vq->user_be = false;
  51. }
  52. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  53. {
  54. struct vhost_vring_state s;
  55. if (vq->private_data)
  56. return -EBUSY;
  57. if (copy_from_user(&s, argp, sizeof(s)))
  58. return -EFAULT;
  59. if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
  60. s.num != VHOST_VRING_BIG_ENDIAN)
  61. return -EINVAL;
  62. if (s.num == VHOST_VRING_BIG_ENDIAN)
  63. vhost_enable_cross_endian_big(vq);
  64. else
  65. vhost_enable_cross_endian_little(vq);
  66. return 0;
  67. }
  68. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  69. int __user *argp)
  70. {
  71. struct vhost_vring_state s = {
  72. .index = idx,
  73. .num = vq->user_be
  74. };
  75. if (copy_to_user(argp, &s, sizeof(s)))
  76. return -EFAULT;
  77. return 0;
  78. }
  79. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  80. {
  81. /* Note for legacy virtio: user_be is initialized at reset time
  82. * according to the host endianness. If userspace does not set an
  83. * explicit endianness, the default behavior is native endian, as
  84. * expected by legacy virtio.
  85. */
  86. vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
  87. }
  88. #else
  89. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  90. {
  91. }
  92. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  93. {
  94. return -ENOIOCTLCMD;
  95. }
  96. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  97. int __user *argp)
  98. {
  99. return -ENOIOCTLCMD;
  100. }
  101. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  102. {
  103. if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
  104. vq->is_le = true;
  105. }
  106. #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
  107. static void vhost_reset_is_le(struct vhost_virtqueue *vq)
  108. {
  109. vq->is_le = virtio_legacy_is_little_endian();
  110. }
  111. static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
  112. poll_table *pt)
  113. {
  114. struct vhost_poll *poll;
  115. poll = container_of(pt, struct vhost_poll, table);
  116. poll->wqh = wqh;
  117. add_wait_queue(wqh, &poll->wait);
  118. }
  119. static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
  120. void *key)
  121. {
  122. struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
  123. if (!((unsigned long)key & poll->mask))
  124. return 0;
  125. vhost_poll_queue(poll);
  126. return 0;
  127. }
  128. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
  129. {
  130. INIT_LIST_HEAD(&work->node);
  131. work->fn = fn;
  132. init_waitqueue_head(&work->done);
  133. work->flushing = 0;
  134. work->queue_seq = work->done_seq = 0;
  135. }
  136. EXPORT_SYMBOL_GPL(vhost_work_init);
  137. /* Init poll structure */
  138. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  139. unsigned long mask, struct vhost_dev *dev)
  140. {
  141. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  142. init_poll_funcptr(&poll->table, vhost_poll_func);
  143. poll->mask = mask;
  144. poll->dev = dev;
  145. poll->wqh = NULL;
  146. vhost_work_init(&poll->work, fn);
  147. }
  148. EXPORT_SYMBOL_GPL(vhost_poll_init);
  149. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  150. * keep a reference to a file until after vhost_poll_stop is called. */
  151. int vhost_poll_start(struct vhost_poll *poll, struct file *file)
  152. {
  153. unsigned long mask;
  154. int ret = 0;
  155. if (poll->wqh)
  156. return 0;
  157. mask = file->f_op->poll(file, &poll->table);
  158. if (mask)
  159. vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
  160. if (mask & POLLERR) {
  161. if (poll->wqh)
  162. remove_wait_queue(poll->wqh, &poll->wait);
  163. ret = -EINVAL;
  164. }
  165. return ret;
  166. }
  167. EXPORT_SYMBOL_GPL(vhost_poll_start);
  168. /* Stop polling a file. After this function returns, it becomes safe to drop the
  169. * file reference. You must also flush afterwards. */
  170. void vhost_poll_stop(struct vhost_poll *poll)
  171. {
  172. if (poll->wqh) {
  173. remove_wait_queue(poll->wqh, &poll->wait);
  174. poll->wqh = NULL;
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(vhost_poll_stop);
  178. static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
  179. unsigned seq)
  180. {
  181. int left;
  182. spin_lock_irq(&dev->work_lock);
  183. left = seq - work->done_seq;
  184. spin_unlock_irq(&dev->work_lock);
  185. return left <= 0;
  186. }
  187. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
  188. {
  189. unsigned seq;
  190. int flushing;
  191. spin_lock_irq(&dev->work_lock);
  192. seq = work->queue_seq;
  193. work->flushing++;
  194. spin_unlock_irq(&dev->work_lock);
  195. wait_event(work->done, vhost_work_seq_done(dev, work, seq));
  196. spin_lock_irq(&dev->work_lock);
  197. flushing = --work->flushing;
  198. spin_unlock_irq(&dev->work_lock);
  199. BUG_ON(flushing < 0);
  200. }
  201. EXPORT_SYMBOL_GPL(vhost_work_flush);
  202. /* Flush any work that has been scheduled. When calling this, don't hold any
  203. * locks that are also used by the callback. */
  204. void vhost_poll_flush(struct vhost_poll *poll)
  205. {
  206. vhost_work_flush(poll->dev, &poll->work);
  207. }
  208. EXPORT_SYMBOL_GPL(vhost_poll_flush);
  209. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
  210. {
  211. unsigned long flags;
  212. spin_lock_irqsave(&dev->work_lock, flags);
  213. if (list_empty(&work->node)) {
  214. list_add_tail(&work->node, &dev->work_list);
  215. work->queue_seq++;
  216. spin_unlock_irqrestore(&dev->work_lock, flags);
  217. wake_up_process(dev->worker);
  218. } else {
  219. spin_unlock_irqrestore(&dev->work_lock, flags);
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(vhost_work_queue);
  223. /* A lockless hint for busy polling code to exit the loop */
  224. bool vhost_has_work(struct vhost_dev *dev)
  225. {
  226. return !list_empty(&dev->work_list);
  227. }
  228. EXPORT_SYMBOL_GPL(vhost_has_work);
  229. void vhost_poll_queue(struct vhost_poll *poll)
  230. {
  231. vhost_work_queue(poll->dev, &poll->work);
  232. }
  233. EXPORT_SYMBOL_GPL(vhost_poll_queue);
  234. static void vhost_vq_reset(struct vhost_dev *dev,
  235. struct vhost_virtqueue *vq)
  236. {
  237. vq->num = 1;
  238. vq->desc = NULL;
  239. vq->avail = NULL;
  240. vq->used = NULL;
  241. vq->last_avail_idx = 0;
  242. vq->avail_idx = 0;
  243. vq->last_used_idx = 0;
  244. vq->signalled_used = 0;
  245. vq->signalled_used_valid = false;
  246. vq->used_flags = 0;
  247. vq->log_used = false;
  248. vq->log_addr = -1ull;
  249. vq->private_data = NULL;
  250. vq->acked_features = 0;
  251. vq->log_base = NULL;
  252. vq->error_ctx = NULL;
  253. vq->error = NULL;
  254. vq->kick = NULL;
  255. vq->call_ctx = NULL;
  256. vq->call = NULL;
  257. vq->log_ctx = NULL;
  258. vq->memory = NULL;
  259. vhost_reset_is_le(vq);
  260. vhost_disable_cross_endian(vq);
  261. vq->busyloop_timeout = 0;
  262. }
  263. static int vhost_worker(void *data)
  264. {
  265. struct vhost_dev *dev = data;
  266. struct vhost_work *work = NULL;
  267. unsigned uninitialized_var(seq);
  268. mm_segment_t oldfs = get_fs();
  269. set_fs(USER_DS);
  270. use_mm(dev->mm);
  271. for (;;) {
  272. /* mb paired w/ kthread_stop */
  273. set_current_state(TASK_INTERRUPTIBLE);
  274. spin_lock_irq(&dev->work_lock);
  275. if (work) {
  276. work->done_seq = seq;
  277. if (work->flushing)
  278. wake_up_all(&work->done);
  279. }
  280. if (kthread_should_stop()) {
  281. spin_unlock_irq(&dev->work_lock);
  282. __set_current_state(TASK_RUNNING);
  283. break;
  284. }
  285. if (!list_empty(&dev->work_list)) {
  286. work = list_first_entry(&dev->work_list,
  287. struct vhost_work, node);
  288. list_del_init(&work->node);
  289. seq = work->queue_seq;
  290. } else
  291. work = NULL;
  292. spin_unlock_irq(&dev->work_lock);
  293. if (work) {
  294. __set_current_state(TASK_RUNNING);
  295. work->fn(work);
  296. if (need_resched())
  297. schedule();
  298. } else
  299. schedule();
  300. }
  301. unuse_mm(dev->mm);
  302. set_fs(oldfs);
  303. return 0;
  304. }
  305. static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  306. {
  307. kfree(vq->indirect);
  308. vq->indirect = NULL;
  309. kfree(vq->log);
  310. vq->log = NULL;
  311. kfree(vq->heads);
  312. vq->heads = NULL;
  313. }
  314. /* Helper to allocate iovec buffers for all vqs. */
  315. static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  316. {
  317. struct vhost_virtqueue *vq;
  318. int i;
  319. for (i = 0; i < dev->nvqs; ++i) {
  320. vq = dev->vqs[i];
  321. vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
  322. GFP_KERNEL);
  323. vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
  324. vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
  325. if (!vq->indirect || !vq->log || !vq->heads)
  326. goto err_nomem;
  327. }
  328. return 0;
  329. err_nomem:
  330. for (; i >= 0; --i)
  331. vhost_vq_free_iovecs(dev->vqs[i]);
  332. return -ENOMEM;
  333. }
  334. static void vhost_dev_free_iovecs(struct vhost_dev *dev)
  335. {
  336. int i;
  337. for (i = 0; i < dev->nvqs; ++i)
  338. vhost_vq_free_iovecs(dev->vqs[i]);
  339. }
  340. void vhost_dev_init(struct vhost_dev *dev,
  341. struct vhost_virtqueue **vqs, int nvqs)
  342. {
  343. struct vhost_virtqueue *vq;
  344. int i;
  345. dev->vqs = vqs;
  346. dev->nvqs = nvqs;
  347. mutex_init(&dev->mutex);
  348. dev->log_ctx = NULL;
  349. dev->log_file = NULL;
  350. dev->memory = NULL;
  351. dev->mm = NULL;
  352. spin_lock_init(&dev->work_lock);
  353. INIT_LIST_HEAD(&dev->work_list);
  354. dev->worker = NULL;
  355. for (i = 0; i < dev->nvqs; ++i) {
  356. vq = dev->vqs[i];
  357. vq->log = NULL;
  358. vq->indirect = NULL;
  359. vq->heads = NULL;
  360. vq->dev = dev;
  361. mutex_init(&vq->mutex);
  362. vhost_vq_reset(dev, vq);
  363. if (vq->handle_kick)
  364. vhost_poll_init(&vq->poll, vq->handle_kick,
  365. POLLIN, dev);
  366. }
  367. }
  368. EXPORT_SYMBOL_GPL(vhost_dev_init);
  369. /* Caller should have device mutex */
  370. long vhost_dev_check_owner(struct vhost_dev *dev)
  371. {
  372. /* Are you the owner? If not, I don't think you mean to do that */
  373. return dev->mm == current->mm ? 0 : -EPERM;
  374. }
  375. EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
  376. struct vhost_attach_cgroups_struct {
  377. struct vhost_work work;
  378. struct task_struct *owner;
  379. int ret;
  380. };
  381. static void vhost_attach_cgroups_work(struct vhost_work *work)
  382. {
  383. struct vhost_attach_cgroups_struct *s;
  384. s = container_of(work, struct vhost_attach_cgroups_struct, work);
  385. s->ret = cgroup_attach_task_all(s->owner, current);
  386. }
  387. static int vhost_attach_cgroups(struct vhost_dev *dev)
  388. {
  389. struct vhost_attach_cgroups_struct attach;
  390. attach.owner = current;
  391. vhost_work_init(&attach.work, vhost_attach_cgroups_work);
  392. vhost_work_queue(dev, &attach.work);
  393. vhost_work_flush(dev, &attach.work);
  394. return attach.ret;
  395. }
  396. /* Caller should have device mutex */
  397. bool vhost_dev_has_owner(struct vhost_dev *dev)
  398. {
  399. return dev->mm;
  400. }
  401. EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
  402. /* Caller should have device mutex */
  403. long vhost_dev_set_owner(struct vhost_dev *dev)
  404. {
  405. struct task_struct *worker;
  406. int err;
  407. /* Is there an owner already? */
  408. if (vhost_dev_has_owner(dev)) {
  409. err = -EBUSY;
  410. goto err_mm;
  411. }
  412. /* No owner, become one */
  413. dev->mm = get_task_mm(current);
  414. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  415. if (IS_ERR(worker)) {
  416. err = PTR_ERR(worker);
  417. goto err_worker;
  418. }
  419. dev->worker = worker;
  420. wake_up_process(worker); /* avoid contributing to loadavg */
  421. err = vhost_attach_cgroups(dev);
  422. if (err)
  423. goto err_cgroup;
  424. err = vhost_dev_alloc_iovecs(dev);
  425. if (err)
  426. goto err_cgroup;
  427. return 0;
  428. err_cgroup:
  429. kthread_stop(worker);
  430. dev->worker = NULL;
  431. err_worker:
  432. if (dev->mm)
  433. mmput(dev->mm);
  434. dev->mm = NULL;
  435. err_mm:
  436. return err;
  437. }
  438. EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
  439. struct vhost_memory *vhost_dev_reset_owner_prepare(void)
  440. {
  441. return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
  442. }
  443. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
  444. /* Caller should have device mutex */
  445. void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
  446. {
  447. int i;
  448. vhost_dev_cleanup(dev, true);
  449. /* Restore memory to default empty mapping. */
  450. memory->nregions = 0;
  451. dev->memory = memory;
  452. /* We don't need VQ locks below since vhost_dev_cleanup makes sure
  453. * VQs aren't running.
  454. */
  455. for (i = 0; i < dev->nvqs; ++i)
  456. dev->vqs[i]->memory = memory;
  457. }
  458. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
  459. void vhost_dev_stop(struct vhost_dev *dev)
  460. {
  461. int i;
  462. for (i = 0; i < dev->nvqs; ++i) {
  463. if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
  464. vhost_poll_stop(&dev->vqs[i]->poll);
  465. vhost_poll_flush(&dev->vqs[i]->poll);
  466. }
  467. }
  468. }
  469. EXPORT_SYMBOL_GPL(vhost_dev_stop);
  470. /* Caller should have device mutex if and only if locked is set */
  471. void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
  472. {
  473. int i;
  474. for (i = 0; i < dev->nvqs; ++i) {
  475. if (dev->vqs[i]->error_ctx)
  476. eventfd_ctx_put(dev->vqs[i]->error_ctx);
  477. if (dev->vqs[i]->error)
  478. fput(dev->vqs[i]->error);
  479. if (dev->vqs[i]->kick)
  480. fput(dev->vqs[i]->kick);
  481. if (dev->vqs[i]->call_ctx)
  482. eventfd_ctx_put(dev->vqs[i]->call_ctx);
  483. if (dev->vqs[i]->call)
  484. fput(dev->vqs[i]->call);
  485. vhost_vq_reset(dev, dev->vqs[i]);
  486. }
  487. vhost_dev_free_iovecs(dev);
  488. if (dev->log_ctx)
  489. eventfd_ctx_put(dev->log_ctx);
  490. dev->log_ctx = NULL;
  491. if (dev->log_file)
  492. fput(dev->log_file);
  493. dev->log_file = NULL;
  494. /* No one will access memory at this point */
  495. kvfree(dev->memory);
  496. dev->memory = NULL;
  497. WARN_ON(!list_empty(&dev->work_list));
  498. if (dev->worker) {
  499. kthread_stop(dev->worker);
  500. dev->worker = NULL;
  501. }
  502. if (dev->mm)
  503. mmput(dev->mm);
  504. dev->mm = NULL;
  505. }
  506. EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
  507. static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  508. {
  509. u64 a = addr / VHOST_PAGE_SIZE / 8;
  510. /* Make sure 64 bit math will not overflow. */
  511. if (a > ULONG_MAX - (unsigned long)log_base ||
  512. a + (unsigned long)log_base > ULONG_MAX)
  513. return 0;
  514. return access_ok(VERIFY_WRITE, log_base + a,
  515. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  516. }
  517. /* Caller should have vq mutex and device mutex. */
  518. static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
  519. int log_all)
  520. {
  521. int i;
  522. if (!mem)
  523. return 0;
  524. for (i = 0; i < mem->nregions; ++i) {
  525. struct vhost_memory_region *m = mem->regions + i;
  526. unsigned long a = m->userspace_addr;
  527. if (m->memory_size > ULONG_MAX)
  528. return 0;
  529. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  530. m->memory_size))
  531. return 0;
  532. else if (log_all && !log_access_ok(log_base,
  533. m->guest_phys_addr,
  534. m->memory_size))
  535. return 0;
  536. }
  537. return 1;
  538. }
  539. /* Can we switch to this memory table? */
  540. /* Caller should have device mutex but not vq mutex */
  541. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  542. int log_all)
  543. {
  544. int i;
  545. for (i = 0; i < d->nvqs; ++i) {
  546. int ok;
  547. bool log;
  548. mutex_lock(&d->vqs[i]->mutex);
  549. log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
  550. /* If ring is inactive, will check when it's enabled. */
  551. if (d->vqs[i]->private_data)
  552. ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
  553. else
  554. ok = 1;
  555. mutex_unlock(&d->vqs[i]->mutex);
  556. if (!ok)
  557. return 0;
  558. }
  559. return 1;
  560. }
  561. static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
  562. struct vring_desc __user *desc,
  563. struct vring_avail __user *avail,
  564. struct vring_used __user *used)
  565. {
  566. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  567. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  568. access_ok(VERIFY_READ, avail,
  569. sizeof *avail + num * sizeof *avail->ring + s) &&
  570. access_ok(VERIFY_WRITE, used,
  571. sizeof *used + num * sizeof *used->ring + s);
  572. }
  573. /* Can we log writes? */
  574. /* Caller should have device mutex but not vq mutex */
  575. int vhost_log_access_ok(struct vhost_dev *dev)
  576. {
  577. return memory_access_ok(dev, dev->memory, 1);
  578. }
  579. EXPORT_SYMBOL_GPL(vhost_log_access_ok);
  580. /* Verify access for write logging. */
  581. /* Caller should have vq mutex and device mutex */
  582. static int vq_log_access_ok(struct vhost_virtqueue *vq,
  583. void __user *log_base)
  584. {
  585. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  586. return vq_memory_access_ok(log_base, vq->memory,
  587. vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
  588. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  589. sizeof *vq->used +
  590. vq->num * sizeof *vq->used->ring + s));
  591. }
  592. /* Can we start vq? */
  593. /* Caller should have vq mutex and device mutex */
  594. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  595. {
  596. return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
  597. vq_log_access_ok(vq, vq->log_base);
  598. }
  599. EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
  600. static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
  601. {
  602. const struct vhost_memory_region *r1 = p1, *r2 = p2;
  603. if (r1->guest_phys_addr < r2->guest_phys_addr)
  604. return 1;
  605. if (r1->guest_phys_addr > r2->guest_phys_addr)
  606. return -1;
  607. return 0;
  608. }
  609. static void *vhost_kvzalloc(unsigned long size)
  610. {
  611. void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  612. if (!n)
  613. n = vzalloc(size);
  614. return n;
  615. }
  616. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  617. {
  618. struct vhost_memory mem, *newmem, *oldmem;
  619. unsigned long size = offsetof(struct vhost_memory, regions);
  620. int i;
  621. if (copy_from_user(&mem, m, size))
  622. return -EFAULT;
  623. if (mem.padding)
  624. return -EOPNOTSUPP;
  625. if (mem.nregions > max_mem_regions)
  626. return -E2BIG;
  627. newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
  628. if (!newmem)
  629. return -ENOMEM;
  630. memcpy(newmem, &mem, size);
  631. if (copy_from_user(newmem->regions, m->regions,
  632. mem.nregions * sizeof *m->regions)) {
  633. kvfree(newmem);
  634. return -EFAULT;
  635. }
  636. sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
  637. vhost_memory_reg_sort_cmp, NULL);
  638. if (!memory_access_ok(d, newmem, 0)) {
  639. kvfree(newmem);
  640. return -EFAULT;
  641. }
  642. oldmem = d->memory;
  643. d->memory = newmem;
  644. /* All memory accesses are done under some VQ mutex. */
  645. for (i = 0; i < d->nvqs; ++i) {
  646. mutex_lock(&d->vqs[i]->mutex);
  647. d->vqs[i]->memory = newmem;
  648. mutex_unlock(&d->vqs[i]->mutex);
  649. }
  650. kvfree(oldmem);
  651. return 0;
  652. }
  653. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
  654. {
  655. struct file *eventfp, *filep = NULL;
  656. bool pollstart = false, pollstop = false;
  657. struct eventfd_ctx *ctx = NULL;
  658. u32 __user *idxp = argp;
  659. struct vhost_virtqueue *vq;
  660. struct vhost_vring_state s;
  661. struct vhost_vring_file f;
  662. struct vhost_vring_addr a;
  663. u32 idx;
  664. long r;
  665. r = get_user(idx, idxp);
  666. if (r < 0)
  667. return r;
  668. if (idx >= d->nvqs)
  669. return -ENOBUFS;
  670. vq = d->vqs[idx];
  671. mutex_lock(&vq->mutex);
  672. switch (ioctl) {
  673. case VHOST_SET_VRING_NUM:
  674. /* Resizing ring with an active backend?
  675. * You don't want to do that. */
  676. if (vq->private_data) {
  677. r = -EBUSY;
  678. break;
  679. }
  680. if (copy_from_user(&s, argp, sizeof s)) {
  681. r = -EFAULT;
  682. break;
  683. }
  684. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  685. r = -EINVAL;
  686. break;
  687. }
  688. vq->num = s.num;
  689. break;
  690. case VHOST_SET_VRING_BASE:
  691. /* Moving base with an active backend?
  692. * You don't want to do that. */
  693. if (vq->private_data) {
  694. r = -EBUSY;
  695. break;
  696. }
  697. if (copy_from_user(&s, argp, sizeof s)) {
  698. r = -EFAULT;
  699. break;
  700. }
  701. if (s.num > 0xffff) {
  702. r = -EINVAL;
  703. break;
  704. }
  705. vq->last_avail_idx = s.num;
  706. /* Forget the cached index value. */
  707. vq->avail_idx = vq->last_avail_idx;
  708. break;
  709. case VHOST_GET_VRING_BASE:
  710. s.index = idx;
  711. s.num = vq->last_avail_idx;
  712. if (copy_to_user(argp, &s, sizeof s))
  713. r = -EFAULT;
  714. break;
  715. case VHOST_SET_VRING_ADDR:
  716. if (copy_from_user(&a, argp, sizeof a)) {
  717. r = -EFAULT;
  718. break;
  719. }
  720. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  721. r = -EOPNOTSUPP;
  722. break;
  723. }
  724. /* For 32bit, verify that the top 32bits of the user
  725. data are set to zero. */
  726. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  727. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  728. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  729. r = -EFAULT;
  730. break;
  731. }
  732. /* Make sure it's safe to cast pointers to vring types. */
  733. BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
  734. BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
  735. if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
  736. (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
  737. (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
  738. r = -EINVAL;
  739. break;
  740. }
  741. /* We only verify access here if backend is configured.
  742. * If it is not, we don't as size might not have been setup.
  743. * We will verify when backend is configured. */
  744. if (vq->private_data) {
  745. if (!vq_access_ok(vq, vq->num,
  746. (void __user *)(unsigned long)a.desc_user_addr,
  747. (void __user *)(unsigned long)a.avail_user_addr,
  748. (void __user *)(unsigned long)a.used_user_addr)) {
  749. r = -EINVAL;
  750. break;
  751. }
  752. /* Also validate log access for used ring if enabled. */
  753. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  754. !log_access_ok(vq->log_base, a.log_guest_addr,
  755. sizeof *vq->used +
  756. vq->num * sizeof *vq->used->ring)) {
  757. r = -EINVAL;
  758. break;
  759. }
  760. }
  761. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  762. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  763. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  764. vq->log_addr = a.log_guest_addr;
  765. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  766. break;
  767. case VHOST_SET_VRING_KICK:
  768. if (copy_from_user(&f, argp, sizeof f)) {
  769. r = -EFAULT;
  770. break;
  771. }
  772. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  773. if (IS_ERR(eventfp)) {
  774. r = PTR_ERR(eventfp);
  775. break;
  776. }
  777. if (eventfp != vq->kick) {
  778. pollstop = (filep = vq->kick) != NULL;
  779. pollstart = (vq->kick = eventfp) != NULL;
  780. } else
  781. filep = eventfp;
  782. break;
  783. case VHOST_SET_VRING_CALL:
  784. if (copy_from_user(&f, argp, sizeof f)) {
  785. r = -EFAULT;
  786. break;
  787. }
  788. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  789. if (IS_ERR(eventfp)) {
  790. r = PTR_ERR(eventfp);
  791. break;
  792. }
  793. if (eventfp != vq->call) {
  794. filep = vq->call;
  795. ctx = vq->call_ctx;
  796. vq->call = eventfp;
  797. vq->call_ctx = eventfp ?
  798. eventfd_ctx_fileget(eventfp) : NULL;
  799. } else
  800. filep = eventfp;
  801. break;
  802. case VHOST_SET_VRING_ERR:
  803. if (copy_from_user(&f, argp, sizeof f)) {
  804. r = -EFAULT;
  805. break;
  806. }
  807. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  808. if (IS_ERR(eventfp)) {
  809. r = PTR_ERR(eventfp);
  810. break;
  811. }
  812. if (eventfp != vq->error) {
  813. filep = vq->error;
  814. vq->error = eventfp;
  815. ctx = vq->error_ctx;
  816. vq->error_ctx = eventfp ?
  817. eventfd_ctx_fileget(eventfp) : NULL;
  818. } else
  819. filep = eventfp;
  820. break;
  821. case VHOST_SET_VRING_ENDIAN:
  822. r = vhost_set_vring_endian(vq, argp);
  823. break;
  824. case VHOST_GET_VRING_ENDIAN:
  825. r = vhost_get_vring_endian(vq, idx, argp);
  826. break;
  827. case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
  828. if (copy_from_user(&s, argp, sizeof(s))) {
  829. r = -EFAULT;
  830. break;
  831. }
  832. vq->busyloop_timeout = s.num;
  833. break;
  834. case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
  835. s.index = idx;
  836. s.num = vq->busyloop_timeout;
  837. if (copy_to_user(argp, &s, sizeof(s)))
  838. r = -EFAULT;
  839. break;
  840. default:
  841. r = -ENOIOCTLCMD;
  842. }
  843. if (pollstop && vq->handle_kick)
  844. vhost_poll_stop(&vq->poll);
  845. if (ctx)
  846. eventfd_ctx_put(ctx);
  847. if (filep)
  848. fput(filep);
  849. if (pollstart && vq->handle_kick)
  850. r = vhost_poll_start(&vq->poll, vq->kick);
  851. mutex_unlock(&vq->mutex);
  852. if (pollstop && vq->handle_kick)
  853. vhost_poll_flush(&vq->poll);
  854. return r;
  855. }
  856. EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
  857. /* Caller must have device mutex */
  858. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  859. {
  860. struct file *eventfp, *filep = NULL;
  861. struct eventfd_ctx *ctx = NULL;
  862. u64 p;
  863. long r;
  864. int i, fd;
  865. /* If you are not the owner, you can become one */
  866. if (ioctl == VHOST_SET_OWNER) {
  867. r = vhost_dev_set_owner(d);
  868. goto done;
  869. }
  870. /* You must be the owner to do anything else */
  871. r = vhost_dev_check_owner(d);
  872. if (r)
  873. goto done;
  874. switch (ioctl) {
  875. case VHOST_SET_MEM_TABLE:
  876. r = vhost_set_memory(d, argp);
  877. break;
  878. case VHOST_SET_LOG_BASE:
  879. if (copy_from_user(&p, argp, sizeof p)) {
  880. r = -EFAULT;
  881. break;
  882. }
  883. if ((u64)(unsigned long)p != p) {
  884. r = -EFAULT;
  885. break;
  886. }
  887. for (i = 0; i < d->nvqs; ++i) {
  888. struct vhost_virtqueue *vq;
  889. void __user *base = (void __user *)(unsigned long)p;
  890. vq = d->vqs[i];
  891. mutex_lock(&vq->mutex);
  892. /* If ring is inactive, will check when it's enabled. */
  893. if (vq->private_data && !vq_log_access_ok(vq, base))
  894. r = -EFAULT;
  895. else
  896. vq->log_base = base;
  897. mutex_unlock(&vq->mutex);
  898. }
  899. break;
  900. case VHOST_SET_LOG_FD:
  901. r = get_user(fd, (int __user *)argp);
  902. if (r < 0)
  903. break;
  904. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  905. if (IS_ERR(eventfp)) {
  906. r = PTR_ERR(eventfp);
  907. break;
  908. }
  909. if (eventfp != d->log_file) {
  910. filep = d->log_file;
  911. d->log_file = eventfp;
  912. ctx = d->log_ctx;
  913. d->log_ctx = eventfp ?
  914. eventfd_ctx_fileget(eventfp) : NULL;
  915. } else
  916. filep = eventfp;
  917. for (i = 0; i < d->nvqs; ++i) {
  918. mutex_lock(&d->vqs[i]->mutex);
  919. d->vqs[i]->log_ctx = d->log_ctx;
  920. mutex_unlock(&d->vqs[i]->mutex);
  921. }
  922. if (ctx)
  923. eventfd_ctx_put(ctx);
  924. if (filep)
  925. fput(filep);
  926. break;
  927. default:
  928. r = -ENOIOCTLCMD;
  929. break;
  930. }
  931. done:
  932. return r;
  933. }
  934. EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
  935. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  936. __u64 addr, __u32 len)
  937. {
  938. const struct vhost_memory_region *reg;
  939. int start = 0, end = mem->nregions;
  940. while (start < end) {
  941. int slot = start + (end - start) / 2;
  942. reg = mem->regions + slot;
  943. if (addr >= reg->guest_phys_addr)
  944. end = slot;
  945. else
  946. start = slot + 1;
  947. }
  948. reg = mem->regions + start;
  949. if (addr >= reg->guest_phys_addr &&
  950. reg->guest_phys_addr + reg->memory_size > addr)
  951. return reg;
  952. return NULL;
  953. }
  954. /* TODO: This is really inefficient. We need something like get_user()
  955. * (instruction directly accesses the data, with an exception table entry
  956. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  957. */
  958. static int set_bit_to_user(int nr, void __user *addr)
  959. {
  960. unsigned long log = (unsigned long)addr;
  961. struct page *page;
  962. void *base;
  963. int bit = nr + (log % PAGE_SIZE) * 8;
  964. int r;
  965. r = get_user_pages_fast(log, 1, 1, &page);
  966. if (r < 0)
  967. return r;
  968. BUG_ON(r != 1);
  969. base = kmap_atomic(page);
  970. set_bit(bit, base);
  971. kunmap_atomic(base);
  972. set_page_dirty_lock(page);
  973. put_page(page);
  974. return 0;
  975. }
  976. static int log_write(void __user *log_base,
  977. u64 write_address, u64 write_length)
  978. {
  979. u64 write_page = write_address / VHOST_PAGE_SIZE;
  980. int r;
  981. if (!write_length)
  982. return 0;
  983. write_length += write_address % VHOST_PAGE_SIZE;
  984. for (;;) {
  985. u64 base = (u64)(unsigned long)log_base;
  986. u64 log = base + write_page / 8;
  987. int bit = write_page % 8;
  988. if ((u64)(unsigned long)log != log)
  989. return -EFAULT;
  990. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  991. if (r < 0)
  992. return r;
  993. if (write_length <= VHOST_PAGE_SIZE)
  994. break;
  995. write_length -= VHOST_PAGE_SIZE;
  996. write_page += 1;
  997. }
  998. return r;
  999. }
  1000. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  1001. unsigned int log_num, u64 len)
  1002. {
  1003. int i, r;
  1004. /* Make sure data written is seen before log. */
  1005. smp_wmb();
  1006. for (i = 0; i < log_num; ++i) {
  1007. u64 l = min(log[i].len, len);
  1008. r = log_write(vq->log_base, log[i].addr, l);
  1009. if (r < 0)
  1010. return r;
  1011. len -= l;
  1012. if (!len) {
  1013. if (vq->log_ctx)
  1014. eventfd_signal(vq->log_ctx, 1);
  1015. return 0;
  1016. }
  1017. }
  1018. /* Length written exceeds what we have stored. This is a bug. */
  1019. BUG();
  1020. return 0;
  1021. }
  1022. EXPORT_SYMBOL_GPL(vhost_log_write);
  1023. static int vhost_update_used_flags(struct vhost_virtqueue *vq)
  1024. {
  1025. void __user *used;
  1026. if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
  1027. return -EFAULT;
  1028. if (unlikely(vq->log_used)) {
  1029. /* Make sure the flag is seen before log. */
  1030. smp_wmb();
  1031. /* Log used flag write. */
  1032. used = &vq->used->flags;
  1033. log_write(vq->log_base, vq->log_addr +
  1034. (used - (void __user *)vq->used),
  1035. sizeof vq->used->flags);
  1036. if (vq->log_ctx)
  1037. eventfd_signal(vq->log_ctx, 1);
  1038. }
  1039. return 0;
  1040. }
  1041. static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
  1042. {
  1043. if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
  1044. return -EFAULT;
  1045. if (unlikely(vq->log_used)) {
  1046. void __user *used;
  1047. /* Make sure the event is seen before log. */
  1048. smp_wmb();
  1049. /* Log avail event write */
  1050. used = vhost_avail_event(vq);
  1051. log_write(vq->log_base, vq->log_addr +
  1052. (used - (void __user *)vq->used),
  1053. sizeof *vhost_avail_event(vq));
  1054. if (vq->log_ctx)
  1055. eventfd_signal(vq->log_ctx, 1);
  1056. }
  1057. return 0;
  1058. }
  1059. int vhost_vq_init_access(struct vhost_virtqueue *vq)
  1060. {
  1061. __virtio16 last_used_idx;
  1062. int r;
  1063. bool is_le = vq->is_le;
  1064. if (!vq->private_data) {
  1065. vhost_reset_is_le(vq);
  1066. return 0;
  1067. }
  1068. vhost_init_is_le(vq);
  1069. r = vhost_update_used_flags(vq);
  1070. if (r)
  1071. goto err;
  1072. vq->signalled_used_valid = false;
  1073. if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
  1074. r = -EFAULT;
  1075. goto err;
  1076. }
  1077. r = __get_user(last_used_idx, &vq->used->idx);
  1078. if (r)
  1079. goto err;
  1080. vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
  1081. return 0;
  1082. err:
  1083. vq->is_le = is_le;
  1084. return r;
  1085. }
  1086. EXPORT_SYMBOL_GPL(vhost_vq_init_access);
  1087. static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
  1088. struct iovec iov[], int iov_size)
  1089. {
  1090. const struct vhost_memory_region *reg;
  1091. struct vhost_memory *mem;
  1092. struct iovec *_iov;
  1093. u64 s = 0;
  1094. int ret = 0;
  1095. mem = vq->memory;
  1096. while ((u64)len > s) {
  1097. u64 size;
  1098. if (unlikely(ret >= iov_size)) {
  1099. ret = -ENOBUFS;
  1100. break;
  1101. }
  1102. reg = find_region(mem, addr, len);
  1103. if (unlikely(!reg)) {
  1104. ret = -EFAULT;
  1105. break;
  1106. }
  1107. _iov = iov + ret;
  1108. size = reg->memory_size - addr + reg->guest_phys_addr;
  1109. _iov->iov_len = min((u64)len - s, size);
  1110. _iov->iov_base = (void __user *)(unsigned long)
  1111. (reg->userspace_addr + addr - reg->guest_phys_addr);
  1112. s += size;
  1113. addr += size;
  1114. ++ret;
  1115. }
  1116. return ret;
  1117. }
  1118. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  1119. * function returns the next descriptor in the chain,
  1120. * or -1U if we're at the end. */
  1121. static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
  1122. {
  1123. unsigned int next;
  1124. /* If this descriptor says it doesn't chain, we're done. */
  1125. if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
  1126. return -1U;
  1127. /* Check they're not leading us off end of descriptors. */
  1128. next = vhost16_to_cpu(vq, desc->next);
  1129. /* Make sure compiler knows to grab that: we don't want it changing! */
  1130. /* We will use the result as an index in an array, so most
  1131. * architectures only need a compiler barrier here. */
  1132. read_barrier_depends();
  1133. return next;
  1134. }
  1135. static int get_indirect(struct vhost_virtqueue *vq,
  1136. struct iovec iov[], unsigned int iov_size,
  1137. unsigned int *out_num, unsigned int *in_num,
  1138. struct vhost_log *log, unsigned int *log_num,
  1139. struct vring_desc *indirect)
  1140. {
  1141. struct vring_desc desc;
  1142. unsigned int i = 0, count, found = 0;
  1143. u32 len = vhost32_to_cpu(vq, indirect->len);
  1144. struct iov_iter from;
  1145. int ret;
  1146. /* Sanity check */
  1147. if (unlikely(len % sizeof desc)) {
  1148. vq_err(vq, "Invalid length in indirect descriptor: "
  1149. "len 0x%llx not multiple of 0x%zx\n",
  1150. (unsigned long long)len,
  1151. sizeof desc);
  1152. return -EINVAL;
  1153. }
  1154. ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
  1155. UIO_MAXIOV);
  1156. if (unlikely(ret < 0)) {
  1157. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  1158. return ret;
  1159. }
  1160. iov_iter_init(&from, READ, vq->indirect, ret, len);
  1161. /* We will use the result as an address to read from, so most
  1162. * architectures only need a compiler barrier here. */
  1163. read_barrier_depends();
  1164. count = len / sizeof desc;
  1165. /* Buffers are chained via a 16 bit next field, so
  1166. * we can have at most 2^16 of these. */
  1167. if (unlikely(count > USHRT_MAX + 1)) {
  1168. vq_err(vq, "Indirect buffer length too big: %d\n",
  1169. indirect->len);
  1170. return -E2BIG;
  1171. }
  1172. do {
  1173. unsigned iov_count = *in_num + *out_num;
  1174. if (unlikely(++found > count)) {
  1175. vq_err(vq, "Loop detected: last one at %u "
  1176. "indirect size %u\n",
  1177. i, count);
  1178. return -EINVAL;
  1179. }
  1180. if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
  1181. sizeof(desc))) {
  1182. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  1183. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1184. return -EINVAL;
  1185. }
  1186. if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
  1187. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  1188. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1189. return -EINVAL;
  1190. }
  1191. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1192. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1193. iov_size - iov_count);
  1194. if (unlikely(ret < 0)) {
  1195. vq_err(vq, "Translation failure %d indirect idx %d\n",
  1196. ret, i);
  1197. return ret;
  1198. }
  1199. /* If this is an input descriptor, increment that count. */
  1200. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
  1201. *in_num += ret;
  1202. if (unlikely(log)) {
  1203. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1204. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1205. ++*log_num;
  1206. }
  1207. } else {
  1208. /* If it's an output descriptor, they're all supposed
  1209. * to come before any input descriptors. */
  1210. if (unlikely(*in_num)) {
  1211. vq_err(vq, "Indirect descriptor "
  1212. "has out after in: idx %d\n", i);
  1213. return -EINVAL;
  1214. }
  1215. *out_num += ret;
  1216. }
  1217. } while ((i = next_desc(vq, &desc)) != -1);
  1218. return 0;
  1219. }
  1220. /* This looks in the virtqueue and for the first available buffer, and converts
  1221. * it to an iovec for convenient access. Since descriptors consist of some
  1222. * number of output then some number of input descriptors, it's actually two
  1223. * iovecs, but we pack them into one and note how many of each there were.
  1224. *
  1225. * This function returns the descriptor number found, or vq->num (which is
  1226. * never a valid descriptor number) if none was found. A negative code is
  1227. * returned on error. */
  1228. int vhost_get_vq_desc(struct vhost_virtqueue *vq,
  1229. struct iovec iov[], unsigned int iov_size,
  1230. unsigned int *out_num, unsigned int *in_num,
  1231. struct vhost_log *log, unsigned int *log_num)
  1232. {
  1233. struct vring_desc desc;
  1234. unsigned int i, head, found = 0;
  1235. u16 last_avail_idx;
  1236. __virtio16 avail_idx;
  1237. __virtio16 ring_head;
  1238. int ret;
  1239. /* Check it isn't doing very strange things with descriptor numbers. */
  1240. last_avail_idx = vq->last_avail_idx;
  1241. if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
  1242. vq_err(vq, "Failed to access avail idx at %p\n",
  1243. &vq->avail->idx);
  1244. return -EFAULT;
  1245. }
  1246. vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
  1247. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  1248. vq_err(vq, "Guest moved used index from %u to %u",
  1249. last_avail_idx, vq->avail_idx);
  1250. return -EFAULT;
  1251. }
  1252. /* If there's nothing new since last we looked, return invalid. */
  1253. if (vq->avail_idx == last_avail_idx)
  1254. return vq->num;
  1255. /* Only get avail ring entries after they have been exposed by guest. */
  1256. smp_rmb();
  1257. /* Grab the next descriptor number they're advertising, and increment
  1258. * the index we've seen. */
  1259. if (unlikely(__get_user(ring_head,
  1260. &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
  1261. vq_err(vq, "Failed to read head: idx %d address %p\n",
  1262. last_avail_idx,
  1263. &vq->avail->ring[last_avail_idx % vq->num]);
  1264. return -EFAULT;
  1265. }
  1266. head = vhost16_to_cpu(vq, ring_head);
  1267. /* If their number is silly, that's an error. */
  1268. if (unlikely(head >= vq->num)) {
  1269. vq_err(vq, "Guest says index %u > %u is available",
  1270. head, vq->num);
  1271. return -EINVAL;
  1272. }
  1273. /* When we start there are none of either input nor output. */
  1274. *out_num = *in_num = 0;
  1275. if (unlikely(log))
  1276. *log_num = 0;
  1277. i = head;
  1278. do {
  1279. unsigned iov_count = *in_num + *out_num;
  1280. if (unlikely(i >= vq->num)) {
  1281. vq_err(vq, "Desc index is %u > %u, head = %u",
  1282. i, vq->num, head);
  1283. return -EINVAL;
  1284. }
  1285. if (unlikely(++found > vq->num)) {
  1286. vq_err(vq, "Loop detected: last one at %u "
  1287. "vq size %u head %u\n",
  1288. i, vq->num, head);
  1289. return -EINVAL;
  1290. }
  1291. ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
  1292. if (unlikely(ret)) {
  1293. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  1294. i, vq->desc + i);
  1295. return -EFAULT;
  1296. }
  1297. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
  1298. ret = get_indirect(vq, iov, iov_size,
  1299. out_num, in_num,
  1300. log, log_num, &desc);
  1301. if (unlikely(ret < 0)) {
  1302. vq_err(vq, "Failure detected "
  1303. "in indirect descriptor at idx %d\n", i);
  1304. return ret;
  1305. }
  1306. continue;
  1307. }
  1308. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1309. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1310. iov_size - iov_count);
  1311. if (unlikely(ret < 0)) {
  1312. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  1313. ret, i);
  1314. return ret;
  1315. }
  1316. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
  1317. /* If this is an input descriptor,
  1318. * increment that count. */
  1319. *in_num += ret;
  1320. if (unlikely(log)) {
  1321. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1322. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1323. ++*log_num;
  1324. }
  1325. } else {
  1326. /* If it's an output descriptor, they're all supposed
  1327. * to come before any input descriptors. */
  1328. if (unlikely(*in_num)) {
  1329. vq_err(vq, "Descriptor has out after in: "
  1330. "idx %d\n", i);
  1331. return -EINVAL;
  1332. }
  1333. *out_num += ret;
  1334. }
  1335. } while ((i = next_desc(vq, &desc)) != -1);
  1336. /* On success, increment avail index. */
  1337. vq->last_avail_idx++;
  1338. /* Assume notifications from guest are disabled at this point,
  1339. * if they aren't we would need to update avail_event index. */
  1340. BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
  1341. return head;
  1342. }
  1343. EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
  1344. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1345. void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
  1346. {
  1347. vq->last_avail_idx -= n;
  1348. }
  1349. EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
  1350. /* After we've used one of their buffers, we tell them about it. We'll then
  1351. * want to notify the guest, using eventfd. */
  1352. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1353. {
  1354. struct vring_used_elem heads = {
  1355. cpu_to_vhost32(vq, head),
  1356. cpu_to_vhost32(vq, len)
  1357. };
  1358. return vhost_add_used_n(vq, &heads, 1);
  1359. }
  1360. EXPORT_SYMBOL_GPL(vhost_add_used);
  1361. static int __vhost_add_used_n(struct vhost_virtqueue *vq,
  1362. struct vring_used_elem *heads,
  1363. unsigned count)
  1364. {
  1365. struct vring_used_elem __user *used;
  1366. u16 old, new;
  1367. int start;
  1368. start = vq->last_used_idx & (vq->num - 1);
  1369. used = vq->used->ring + start;
  1370. if (count == 1) {
  1371. if (__put_user(heads[0].id, &used->id)) {
  1372. vq_err(vq, "Failed to write used id");
  1373. return -EFAULT;
  1374. }
  1375. if (__put_user(heads[0].len, &used->len)) {
  1376. vq_err(vq, "Failed to write used len");
  1377. return -EFAULT;
  1378. }
  1379. } else if (__copy_to_user(used, heads, count * sizeof *used)) {
  1380. vq_err(vq, "Failed to write used");
  1381. return -EFAULT;
  1382. }
  1383. if (unlikely(vq->log_used)) {
  1384. /* Make sure data is seen before log. */
  1385. smp_wmb();
  1386. /* Log used ring entry write. */
  1387. log_write(vq->log_base,
  1388. vq->log_addr +
  1389. ((void __user *)used - (void __user *)vq->used),
  1390. count * sizeof *used);
  1391. }
  1392. old = vq->last_used_idx;
  1393. new = (vq->last_used_idx += count);
  1394. /* If the driver never bothers to signal in a very long while,
  1395. * used index might wrap around. If that happens, invalidate
  1396. * signalled_used index we stored. TODO: make sure driver
  1397. * signals at least once in 2^16 and remove this. */
  1398. if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
  1399. vq->signalled_used_valid = false;
  1400. return 0;
  1401. }
  1402. /* After we've used one of their buffers, we tell them about it. We'll then
  1403. * want to notify the guest, using eventfd. */
  1404. int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
  1405. unsigned count)
  1406. {
  1407. int start, n, r;
  1408. start = vq->last_used_idx & (vq->num - 1);
  1409. n = vq->num - start;
  1410. if (n < count) {
  1411. r = __vhost_add_used_n(vq, heads, n);
  1412. if (r < 0)
  1413. return r;
  1414. heads += n;
  1415. count -= n;
  1416. }
  1417. r = __vhost_add_used_n(vq, heads, count);
  1418. /* Make sure buffer is written before we update index. */
  1419. smp_wmb();
  1420. if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
  1421. vq_err(vq, "Failed to increment used idx");
  1422. return -EFAULT;
  1423. }
  1424. if (unlikely(vq->log_used)) {
  1425. /* Log used index update. */
  1426. log_write(vq->log_base,
  1427. vq->log_addr + offsetof(struct vring_used, idx),
  1428. sizeof vq->used->idx);
  1429. if (vq->log_ctx)
  1430. eventfd_signal(vq->log_ctx, 1);
  1431. }
  1432. return r;
  1433. }
  1434. EXPORT_SYMBOL_GPL(vhost_add_used_n);
  1435. static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1436. {
  1437. __u16 old, new;
  1438. __virtio16 event;
  1439. bool v;
  1440. /* Flush out used index updates. This is paired
  1441. * with the barrier that the Guest executes when enabling
  1442. * interrupts. */
  1443. smp_mb();
  1444. if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1445. unlikely(vq->avail_idx == vq->last_avail_idx))
  1446. return true;
  1447. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  1448. __virtio16 flags;
  1449. if (__get_user(flags, &vq->avail->flags)) {
  1450. vq_err(vq, "Failed to get flags");
  1451. return true;
  1452. }
  1453. return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
  1454. }
  1455. old = vq->signalled_used;
  1456. v = vq->signalled_used_valid;
  1457. new = vq->signalled_used = vq->last_used_idx;
  1458. vq->signalled_used_valid = true;
  1459. if (unlikely(!v))
  1460. return true;
  1461. if (__get_user(event, vhost_used_event(vq))) {
  1462. vq_err(vq, "Failed to get used event idx");
  1463. return true;
  1464. }
  1465. return vring_need_event(vhost16_to_cpu(vq, event), new, old);
  1466. }
  1467. /* This actually signals the guest, using eventfd. */
  1468. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1469. {
  1470. /* Signal the Guest tell them we used something up. */
  1471. if (vq->call_ctx && vhost_notify(dev, vq))
  1472. eventfd_signal(vq->call_ctx, 1);
  1473. }
  1474. EXPORT_SYMBOL_GPL(vhost_signal);
  1475. /* And here's the combo meal deal. Supersize me! */
  1476. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1477. struct vhost_virtqueue *vq,
  1478. unsigned int head, int len)
  1479. {
  1480. vhost_add_used(vq, head, len);
  1481. vhost_signal(dev, vq);
  1482. }
  1483. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
  1484. /* multi-buffer version of vhost_add_used_and_signal */
  1485. void vhost_add_used_and_signal_n(struct vhost_dev *dev,
  1486. struct vhost_virtqueue *vq,
  1487. struct vring_used_elem *heads, unsigned count)
  1488. {
  1489. vhost_add_used_n(vq, heads, count);
  1490. vhost_signal(dev, vq);
  1491. }
  1492. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
  1493. /* return true if we're sure that avaiable ring is empty */
  1494. bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1495. {
  1496. __virtio16 avail_idx;
  1497. int r;
  1498. r = __get_user(avail_idx, &vq->avail->idx);
  1499. if (r)
  1500. return false;
  1501. return vhost16_to_cpu(vq, avail_idx) == vq->avail_idx;
  1502. }
  1503. EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
  1504. /* OK, now we need to know about added descriptors. */
  1505. bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1506. {
  1507. __virtio16 avail_idx;
  1508. int r;
  1509. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  1510. return false;
  1511. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  1512. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  1513. r = vhost_update_used_flags(vq);
  1514. if (r) {
  1515. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1516. &vq->used->flags, r);
  1517. return false;
  1518. }
  1519. } else {
  1520. r = vhost_update_avail_event(vq, vq->avail_idx);
  1521. if (r) {
  1522. vq_err(vq, "Failed to update avail event index at %p: %d\n",
  1523. vhost_avail_event(vq), r);
  1524. return false;
  1525. }
  1526. }
  1527. /* They could have slipped one in as we were doing that: make
  1528. * sure it's written, then check again. */
  1529. smp_mb();
  1530. r = __get_user(avail_idx, &vq->avail->idx);
  1531. if (r) {
  1532. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  1533. &vq->avail->idx, r);
  1534. return false;
  1535. }
  1536. return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
  1537. }
  1538. EXPORT_SYMBOL_GPL(vhost_enable_notify);
  1539. /* We don't need to be notified again. */
  1540. void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1541. {
  1542. int r;
  1543. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  1544. return;
  1545. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  1546. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  1547. r = vhost_update_used_flags(vq);
  1548. if (r)
  1549. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1550. &vq->used->flags, r);
  1551. }
  1552. }
  1553. EXPORT_SYMBOL_GPL(vhost_disable_notify);
  1554. static int __init vhost_init(void)
  1555. {
  1556. return 0;
  1557. }
  1558. static void __exit vhost_exit(void)
  1559. {
  1560. }
  1561. module_init(vhost_init);
  1562. module_exit(vhost_exit);
  1563. MODULE_VERSION("0.0.1");
  1564. MODULE_LICENSE("GPL v2");
  1565. MODULE_AUTHOR("Michael S. Tsirkin");
  1566. MODULE_DESCRIPTION("Host kernel accelerator for virtio");