vhost.c 35 KB

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