vhost.c 33 KB

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