vhost.c 43 KB

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