vhost.c 33 KB

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