vhost.c 35 KB

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