vhost.c 40 KB

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