vhost.c 41 KB

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