vhost.c 33 KB

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