vhost.c 39 KB

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