vhost.c 38 KB

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