vhost.c 40 KB

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