vhost.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /* Copyright (C) 2009 Red Hat, Inc.
  2. * Copyright (C) 2006 Rusty Russell IBM Corporation
  3. *
  4. * Author: Michael S. Tsirkin <mst@redhat.com>
  5. *
  6. * Inspiration, some code, and most witty comments come from
  7. * Documentation/lguest/lguest.c, by Rusty Russell
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2.
  10. *
  11. * Generic code for virtio server in host kernel.
  12. */
  13. #include <linux/eventfd.h>
  14. #include <linux/vhost.h>
  15. #include <linux/virtio_net.h>
  16. #include <linux/mm.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/mutex.h>
  19. #include <linux/workqueue.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/net.h>
  26. #include <linux/if_packet.h>
  27. #include <linux/if_arp.h>
  28. #include <net/sock.h>
  29. #include "vhost.h"
  30. enum {
  31. VHOST_MEMORY_MAX_NREGIONS = 64,
  32. VHOST_MEMORY_F_LOG = 0x1,
  33. };
  34. static struct workqueue_struct *vhost_workqueue;
  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;
  47. poll = container_of(wait, struct vhost_poll, wait);
  48. if (!((unsigned long)key & poll->mask))
  49. return 0;
  50. queue_work(vhost_workqueue, &poll->work);
  51. return 0;
  52. }
  53. /* Init poll structure */
  54. void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
  55. unsigned long mask)
  56. {
  57. INIT_WORK(&poll->work, func);
  58. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  59. init_poll_funcptr(&poll->table, vhost_poll_func);
  60. poll->mask = mask;
  61. }
  62. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  63. * keep a reference to a file until after vhost_poll_stop is called. */
  64. void vhost_poll_start(struct vhost_poll *poll, struct file *file)
  65. {
  66. unsigned long mask;
  67. mask = file->f_op->poll(file, &poll->table);
  68. if (mask)
  69. vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
  70. }
  71. /* Stop polling a file. After this function returns, it becomes safe to drop the
  72. * file reference. You must also flush afterwards. */
  73. void vhost_poll_stop(struct vhost_poll *poll)
  74. {
  75. remove_wait_queue(poll->wqh, &poll->wait);
  76. }
  77. /* Flush any work that has been scheduled. When calling this, don't hold any
  78. * locks that are also used by the callback. */
  79. void vhost_poll_flush(struct vhost_poll *poll)
  80. {
  81. flush_work(&poll->work);
  82. }
  83. void vhost_poll_queue(struct vhost_poll *poll)
  84. {
  85. queue_work(vhost_workqueue, &poll->work);
  86. }
  87. static void vhost_vq_reset(struct vhost_dev *dev,
  88. struct vhost_virtqueue *vq)
  89. {
  90. vq->num = 1;
  91. vq->desc = NULL;
  92. vq->avail = NULL;
  93. vq->used = NULL;
  94. vq->last_avail_idx = 0;
  95. vq->avail_idx = 0;
  96. vq->last_used_idx = 0;
  97. vq->used_flags = 0;
  98. vq->used_flags = 0;
  99. vq->log_used = false;
  100. vq->log_addr = -1ull;
  101. vq->hdr_size = 0;
  102. vq->private_data = NULL;
  103. vq->log_base = NULL;
  104. vq->error_ctx = NULL;
  105. vq->error = NULL;
  106. vq->kick = NULL;
  107. vq->call_ctx = NULL;
  108. vq->call = NULL;
  109. vq->log_ctx = NULL;
  110. }
  111. long vhost_dev_init(struct vhost_dev *dev,
  112. struct vhost_virtqueue *vqs, int nvqs)
  113. {
  114. int i;
  115. dev->vqs = vqs;
  116. dev->nvqs = nvqs;
  117. mutex_init(&dev->mutex);
  118. dev->log_ctx = NULL;
  119. dev->log_file = NULL;
  120. dev->memory = NULL;
  121. dev->mm = NULL;
  122. for (i = 0; i < dev->nvqs; ++i) {
  123. dev->vqs[i].dev = dev;
  124. mutex_init(&dev->vqs[i].mutex);
  125. vhost_vq_reset(dev, dev->vqs + i);
  126. if (dev->vqs[i].handle_kick)
  127. vhost_poll_init(&dev->vqs[i].poll,
  128. dev->vqs[i].handle_kick,
  129. POLLIN);
  130. }
  131. return 0;
  132. }
  133. /* Caller should have device mutex */
  134. long vhost_dev_check_owner(struct vhost_dev *dev)
  135. {
  136. /* Are you the owner? If not, I don't think you mean to do that */
  137. return dev->mm == current->mm ? 0 : -EPERM;
  138. }
  139. /* Caller should have device mutex */
  140. static long vhost_dev_set_owner(struct vhost_dev *dev)
  141. {
  142. /* Is there an owner already? */
  143. if (dev->mm)
  144. return -EBUSY;
  145. /* No owner, become one */
  146. dev->mm = get_task_mm(current);
  147. return 0;
  148. }
  149. /* Caller should have device mutex */
  150. long vhost_dev_reset_owner(struct vhost_dev *dev)
  151. {
  152. struct vhost_memory *memory;
  153. /* Restore memory to default empty mapping. */
  154. memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
  155. if (!memory)
  156. return -ENOMEM;
  157. vhost_dev_cleanup(dev);
  158. memory->nregions = 0;
  159. dev->memory = memory;
  160. return 0;
  161. }
  162. /* Caller should have device mutex */
  163. void vhost_dev_cleanup(struct vhost_dev *dev)
  164. {
  165. int i;
  166. for (i = 0; i < dev->nvqs; ++i) {
  167. if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
  168. vhost_poll_stop(&dev->vqs[i].poll);
  169. vhost_poll_flush(&dev->vqs[i].poll);
  170. }
  171. if (dev->vqs[i].error_ctx)
  172. eventfd_ctx_put(dev->vqs[i].error_ctx);
  173. if (dev->vqs[i].error)
  174. fput(dev->vqs[i].error);
  175. if (dev->vqs[i].kick)
  176. fput(dev->vqs[i].kick);
  177. if (dev->vqs[i].call_ctx)
  178. eventfd_ctx_put(dev->vqs[i].call_ctx);
  179. if (dev->vqs[i].call)
  180. fput(dev->vqs[i].call);
  181. vhost_vq_reset(dev, dev->vqs + i);
  182. }
  183. if (dev->log_ctx)
  184. eventfd_ctx_put(dev->log_ctx);
  185. dev->log_ctx = NULL;
  186. if (dev->log_file)
  187. fput(dev->log_file);
  188. dev->log_file = NULL;
  189. /* No one will access memory at this point */
  190. kfree(dev->memory);
  191. dev->memory = NULL;
  192. if (dev->mm)
  193. mmput(dev->mm);
  194. dev->mm = NULL;
  195. }
  196. static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  197. {
  198. u64 a = addr / VHOST_PAGE_SIZE / 8;
  199. /* Make sure 64 bit math will not overflow. */
  200. if (a > ULONG_MAX - (unsigned long)log_base ||
  201. a + (unsigned long)log_base > ULONG_MAX)
  202. return -EFAULT;
  203. return access_ok(VERIFY_WRITE, log_base + a,
  204. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  205. }
  206. /* Caller should have vq mutex and device mutex. */
  207. static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
  208. int log_all)
  209. {
  210. int i;
  211. if (!mem)
  212. return 0;
  213. for (i = 0; i < mem->nregions; ++i) {
  214. struct vhost_memory_region *m = mem->regions + i;
  215. unsigned long a = m->userspace_addr;
  216. if (m->memory_size > ULONG_MAX)
  217. return 0;
  218. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  219. m->memory_size))
  220. return 0;
  221. else if (log_all && !log_access_ok(log_base,
  222. m->guest_phys_addr,
  223. m->memory_size))
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. /* Can we switch to this memory table? */
  229. /* Caller should have device mutex but not vq mutex */
  230. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  231. int log_all)
  232. {
  233. int i;
  234. for (i = 0; i < d->nvqs; ++i) {
  235. int ok;
  236. mutex_lock(&d->vqs[i].mutex);
  237. /* If ring is inactive, will check when it's enabled. */
  238. if (d->vqs[i].private_data)
  239. ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
  240. log_all);
  241. else
  242. ok = 1;
  243. mutex_unlock(&d->vqs[i].mutex);
  244. if (!ok)
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. static int vq_access_ok(unsigned int num,
  250. struct vring_desc __user *desc,
  251. struct vring_avail __user *avail,
  252. struct vring_used __user *used)
  253. {
  254. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  255. access_ok(VERIFY_READ, avail,
  256. sizeof *avail + num * sizeof *avail->ring) &&
  257. access_ok(VERIFY_WRITE, used,
  258. sizeof *used + num * sizeof *used->ring);
  259. }
  260. /* Can we log writes? */
  261. /* Caller should have device mutex but not vq mutex */
  262. int vhost_log_access_ok(struct vhost_dev *dev)
  263. {
  264. return memory_access_ok(dev, dev->memory, 1);
  265. }
  266. /* Verify access for write logging. */
  267. /* Caller should have vq mutex and device mutex */
  268. static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
  269. {
  270. return vq_memory_access_ok(log_base, vq->dev->memory,
  271. vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
  272. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  273. sizeof *vq->used +
  274. vq->num * sizeof *vq->used->ring));
  275. }
  276. /* Can we start vq? */
  277. /* Caller should have vq mutex and device mutex */
  278. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  279. {
  280. return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
  281. vq_log_access_ok(vq, vq->log_base);
  282. }
  283. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  284. {
  285. struct vhost_memory mem, *newmem, *oldmem;
  286. unsigned long size = offsetof(struct vhost_memory, regions);
  287. if (copy_from_user(&mem, m, size))
  288. return -EFAULT;
  289. if (mem.padding)
  290. return -EOPNOTSUPP;
  291. if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
  292. return -E2BIG;
  293. newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
  294. if (!newmem)
  295. return -ENOMEM;
  296. memcpy(newmem, &mem, size);
  297. if (copy_from_user(newmem->regions, m->regions,
  298. mem.nregions * sizeof *m->regions)) {
  299. kfree(newmem);
  300. return -EFAULT;
  301. }
  302. if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL)))
  303. return -EFAULT;
  304. oldmem = d->memory;
  305. rcu_assign_pointer(d->memory, newmem);
  306. synchronize_rcu();
  307. kfree(oldmem);
  308. return 0;
  309. }
  310. static int init_used(struct vhost_virtqueue *vq,
  311. struct vring_used __user *used)
  312. {
  313. int r = put_user(vq->used_flags, &used->flags);
  314. if (r)
  315. return r;
  316. return get_user(vq->last_used_idx, &used->idx);
  317. }
  318. static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
  319. {
  320. struct file *eventfp, *filep = NULL,
  321. *pollstart = NULL, *pollstop = NULL;
  322. struct eventfd_ctx *ctx = NULL;
  323. u32 __user *idxp = argp;
  324. struct vhost_virtqueue *vq;
  325. struct vhost_vring_state s;
  326. struct vhost_vring_file f;
  327. struct vhost_vring_addr a;
  328. u32 idx;
  329. long r;
  330. r = get_user(idx, idxp);
  331. if (r < 0)
  332. return r;
  333. if (idx >= d->nvqs)
  334. return -ENOBUFS;
  335. vq = d->vqs + idx;
  336. mutex_lock(&vq->mutex);
  337. switch (ioctl) {
  338. case VHOST_SET_VRING_NUM:
  339. /* Resizing ring with an active backend?
  340. * You don't want to do that. */
  341. if (vq->private_data) {
  342. r = -EBUSY;
  343. break;
  344. }
  345. if (copy_from_user(&s, argp, sizeof s)) {
  346. r = -EFAULT;
  347. break;
  348. }
  349. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  350. r = -EINVAL;
  351. break;
  352. }
  353. vq->num = s.num;
  354. break;
  355. case VHOST_SET_VRING_BASE:
  356. /* Moving base with an active backend?
  357. * You don't want to do that. */
  358. if (vq->private_data) {
  359. r = -EBUSY;
  360. break;
  361. }
  362. if (copy_from_user(&s, argp, sizeof s)) {
  363. r = -EFAULT;
  364. break;
  365. }
  366. if (s.num > 0xffff) {
  367. r = -EINVAL;
  368. break;
  369. }
  370. vq->last_avail_idx = s.num;
  371. /* Forget the cached index value. */
  372. vq->avail_idx = vq->last_avail_idx;
  373. break;
  374. case VHOST_GET_VRING_BASE:
  375. s.index = idx;
  376. s.num = vq->last_avail_idx;
  377. if (copy_to_user(argp, &s, sizeof s))
  378. r = -EFAULT;
  379. break;
  380. case VHOST_SET_VRING_ADDR:
  381. if (copy_from_user(&a, argp, sizeof a)) {
  382. r = -EFAULT;
  383. break;
  384. }
  385. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  386. r = -EOPNOTSUPP;
  387. break;
  388. }
  389. /* For 32bit, verify that the top 32bits of the user
  390. data are set to zero. */
  391. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  392. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  393. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  394. r = -EFAULT;
  395. break;
  396. }
  397. if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
  398. (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
  399. (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
  400. r = -EINVAL;
  401. break;
  402. }
  403. /* We only verify access here if backend is configured.
  404. * If it is not, we don't as size might not have been setup.
  405. * We will verify when backend is configured. */
  406. if (vq->private_data) {
  407. if (!vq_access_ok(vq->num,
  408. (void __user *)(unsigned long)a.desc_user_addr,
  409. (void __user *)(unsigned long)a.avail_user_addr,
  410. (void __user *)(unsigned long)a.used_user_addr)) {
  411. r = -EINVAL;
  412. break;
  413. }
  414. /* Also validate log access for used ring if enabled. */
  415. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  416. !log_access_ok(vq->log_base, a.log_guest_addr,
  417. sizeof *vq->used +
  418. vq->num * sizeof *vq->used->ring)) {
  419. r = -EINVAL;
  420. break;
  421. }
  422. }
  423. r = init_used(vq, (struct vring_used __user *)(unsigned long)
  424. a.used_user_addr);
  425. if (r)
  426. break;
  427. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  428. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  429. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  430. vq->log_addr = a.log_guest_addr;
  431. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  432. break;
  433. case VHOST_SET_VRING_KICK:
  434. if (copy_from_user(&f, argp, sizeof f)) {
  435. r = -EFAULT;
  436. break;
  437. }
  438. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  439. if (IS_ERR(eventfp)) {
  440. r = PTR_ERR(eventfp);
  441. break;
  442. }
  443. if (eventfp != vq->kick) {
  444. pollstop = filep = vq->kick;
  445. pollstart = vq->kick = eventfp;
  446. } else
  447. filep = eventfp;
  448. break;
  449. case VHOST_SET_VRING_CALL:
  450. if (copy_from_user(&f, argp, sizeof f)) {
  451. r = -EFAULT;
  452. break;
  453. }
  454. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  455. if (IS_ERR(eventfp)) {
  456. r = PTR_ERR(eventfp);
  457. break;
  458. }
  459. if (eventfp != vq->call) {
  460. filep = vq->call;
  461. ctx = vq->call_ctx;
  462. vq->call = eventfp;
  463. vq->call_ctx = eventfp ?
  464. eventfd_ctx_fileget(eventfp) : NULL;
  465. } else
  466. filep = eventfp;
  467. break;
  468. case VHOST_SET_VRING_ERR:
  469. if (copy_from_user(&f, argp, sizeof f)) {
  470. r = -EFAULT;
  471. break;
  472. }
  473. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  474. if (IS_ERR(eventfp)) {
  475. r = PTR_ERR(eventfp);
  476. break;
  477. }
  478. if (eventfp != vq->error) {
  479. filep = vq->error;
  480. vq->error = eventfp;
  481. ctx = vq->error_ctx;
  482. vq->error_ctx = eventfp ?
  483. eventfd_ctx_fileget(eventfp) : NULL;
  484. } else
  485. filep = eventfp;
  486. break;
  487. default:
  488. r = -ENOIOCTLCMD;
  489. }
  490. if (pollstop && vq->handle_kick)
  491. vhost_poll_stop(&vq->poll);
  492. if (ctx)
  493. eventfd_ctx_put(ctx);
  494. if (filep)
  495. fput(filep);
  496. if (pollstart && vq->handle_kick)
  497. vhost_poll_start(&vq->poll, vq->kick);
  498. mutex_unlock(&vq->mutex);
  499. if (pollstop && vq->handle_kick)
  500. vhost_poll_flush(&vq->poll);
  501. return r;
  502. }
  503. /* Caller must have device mutex */
  504. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
  505. {
  506. void __user *argp = (void __user *)arg;
  507. struct file *eventfp, *filep = NULL;
  508. struct eventfd_ctx *ctx = NULL;
  509. u64 p;
  510. long r;
  511. int i, fd;
  512. /* If you are not the owner, you can become one */
  513. if (ioctl == VHOST_SET_OWNER) {
  514. r = vhost_dev_set_owner(d);
  515. goto done;
  516. }
  517. /* You must be the owner to do anything else */
  518. r = vhost_dev_check_owner(d);
  519. if (r)
  520. goto done;
  521. switch (ioctl) {
  522. case VHOST_SET_MEM_TABLE:
  523. r = vhost_set_memory(d, argp);
  524. break;
  525. case VHOST_SET_LOG_BASE:
  526. if (copy_from_user(&p, argp, sizeof p)) {
  527. r = -EFAULT;
  528. break;
  529. }
  530. if ((u64)(unsigned long)p != p) {
  531. r = -EFAULT;
  532. break;
  533. }
  534. for (i = 0; i < d->nvqs; ++i) {
  535. struct vhost_virtqueue *vq;
  536. void __user *base = (void __user *)(unsigned long)p;
  537. vq = d->vqs + i;
  538. mutex_lock(&vq->mutex);
  539. /* If ring is inactive, will check when it's enabled. */
  540. if (vq->private_data && !vq_log_access_ok(vq, base))
  541. r = -EFAULT;
  542. else
  543. vq->log_base = base;
  544. mutex_unlock(&vq->mutex);
  545. }
  546. break;
  547. case VHOST_SET_LOG_FD:
  548. r = get_user(fd, (int __user *)argp);
  549. if (r < 0)
  550. break;
  551. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  552. if (IS_ERR(eventfp)) {
  553. r = PTR_ERR(eventfp);
  554. break;
  555. }
  556. if (eventfp != d->log_file) {
  557. filep = d->log_file;
  558. ctx = d->log_ctx;
  559. d->log_ctx = eventfp ?
  560. eventfd_ctx_fileget(eventfp) : NULL;
  561. } else
  562. filep = eventfp;
  563. for (i = 0; i < d->nvqs; ++i) {
  564. mutex_lock(&d->vqs[i].mutex);
  565. d->vqs[i].log_ctx = d->log_ctx;
  566. mutex_unlock(&d->vqs[i].mutex);
  567. }
  568. if (ctx)
  569. eventfd_ctx_put(ctx);
  570. if (filep)
  571. fput(filep);
  572. break;
  573. default:
  574. r = vhost_set_vring(d, ioctl, argp);
  575. break;
  576. }
  577. done:
  578. return r;
  579. }
  580. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  581. __u64 addr, __u32 len)
  582. {
  583. struct vhost_memory_region *reg;
  584. int i;
  585. /* linear search is not brilliant, but we really have on the order of 6
  586. * regions in practice */
  587. for (i = 0; i < mem->nregions; ++i) {
  588. reg = mem->regions + i;
  589. if (reg->guest_phys_addr <= addr &&
  590. reg->guest_phys_addr + reg->memory_size - 1 >= addr)
  591. return reg;
  592. }
  593. return NULL;
  594. }
  595. /* TODO: This is really inefficient. We need something like get_user()
  596. * (instruction directly accesses the data, with an exception table entry
  597. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  598. */
  599. static int set_bit_to_user(int nr, void __user *addr)
  600. {
  601. unsigned long log = (unsigned long)addr;
  602. struct page *page;
  603. void *base;
  604. int bit = nr + (log % PAGE_SIZE) * 8;
  605. int r;
  606. r = get_user_pages_fast(log, 1, 1, &page);
  607. if (r < 0)
  608. return r;
  609. BUG_ON(r != 1);
  610. base = kmap_atomic(page, KM_USER0);
  611. set_bit(bit, base);
  612. kunmap_atomic(base, KM_USER0);
  613. set_page_dirty_lock(page);
  614. put_page(page);
  615. return 0;
  616. }
  617. static int log_write(void __user *log_base,
  618. u64 write_address, u64 write_length)
  619. {
  620. int r;
  621. if (!write_length)
  622. return 0;
  623. write_address /= VHOST_PAGE_SIZE;
  624. for (;;) {
  625. u64 base = (u64)(unsigned long)log_base;
  626. u64 log = base + write_address / 8;
  627. int bit = write_address % 8;
  628. if ((u64)(unsigned long)log != log)
  629. return -EFAULT;
  630. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  631. if (r < 0)
  632. return r;
  633. if (write_length <= VHOST_PAGE_SIZE)
  634. break;
  635. write_length -= VHOST_PAGE_SIZE;
  636. write_address += VHOST_PAGE_SIZE;
  637. }
  638. return r;
  639. }
  640. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  641. unsigned int log_num, u64 len)
  642. {
  643. int i, r;
  644. /* Make sure data written is seen before log. */
  645. smp_wmb();
  646. for (i = 0; i < log_num; ++i) {
  647. u64 l = min(log[i].len, len);
  648. r = log_write(vq->log_base, log[i].addr, l);
  649. if (r < 0)
  650. return r;
  651. len -= l;
  652. if (!len)
  653. return 0;
  654. }
  655. if (vq->log_ctx)
  656. eventfd_signal(vq->log_ctx, 1);
  657. /* Length written exceeds what we have stored. This is a bug. */
  658. BUG();
  659. return 0;
  660. }
  661. static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
  662. struct iovec iov[], int iov_size)
  663. {
  664. const struct vhost_memory_region *reg;
  665. struct vhost_memory *mem;
  666. struct iovec *_iov;
  667. u64 s = 0;
  668. int ret = 0;
  669. rcu_read_lock();
  670. mem = rcu_dereference(dev->memory);
  671. while ((u64)len > s) {
  672. u64 size;
  673. if (ret >= iov_size) {
  674. ret = -ENOBUFS;
  675. break;
  676. }
  677. reg = find_region(mem, addr, len);
  678. if (!reg) {
  679. ret = -EFAULT;
  680. break;
  681. }
  682. _iov = iov + ret;
  683. size = reg->memory_size - addr + reg->guest_phys_addr;
  684. _iov->iov_len = min((u64)len, size);
  685. _iov->iov_base = (void __user *)(unsigned long)
  686. (reg->userspace_addr + addr - reg->guest_phys_addr);
  687. s += size;
  688. addr += size;
  689. ++ret;
  690. }
  691. rcu_read_unlock();
  692. return ret;
  693. }
  694. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  695. * function returns the next descriptor in the chain,
  696. * or -1U if we're at the end. */
  697. static unsigned next_desc(struct vring_desc *desc)
  698. {
  699. unsigned int next;
  700. /* If this descriptor says it doesn't chain, we're done. */
  701. if (!(desc->flags & VRING_DESC_F_NEXT))
  702. return -1U;
  703. /* Check they're not leading us off end of descriptors. */
  704. next = desc->next;
  705. /* Make sure compiler knows to grab that: we don't want it changing! */
  706. /* We will use the result as an index in an array, so most
  707. * architectures only need a compiler barrier here. */
  708. read_barrier_depends();
  709. return next;
  710. }
  711. static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  712. struct iovec iov[], unsigned int iov_size,
  713. unsigned int *out_num, unsigned int *in_num,
  714. struct vhost_log *log, unsigned int *log_num,
  715. struct vring_desc *indirect)
  716. {
  717. struct vring_desc desc;
  718. unsigned int i = 0, count, found = 0;
  719. int ret;
  720. /* Sanity check */
  721. if (indirect->len % sizeof desc) {
  722. vq_err(vq, "Invalid length in indirect descriptor: "
  723. "len 0x%llx not multiple of 0x%zx\n",
  724. (unsigned long long)indirect->len,
  725. sizeof desc);
  726. return -EINVAL;
  727. }
  728. ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
  729. ARRAY_SIZE(vq->indirect));
  730. if (ret < 0) {
  731. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  732. return ret;
  733. }
  734. /* We will use the result as an address to read from, so most
  735. * architectures only need a compiler barrier here. */
  736. read_barrier_depends();
  737. count = indirect->len / sizeof desc;
  738. /* Buffers are chained via a 16 bit next field, so
  739. * we can have at most 2^16 of these. */
  740. if (count > USHORT_MAX + 1) {
  741. vq_err(vq, "Indirect buffer length too big: %d\n",
  742. indirect->len);
  743. return -E2BIG;
  744. }
  745. do {
  746. unsigned iov_count = *in_num + *out_num;
  747. if (++found > count) {
  748. vq_err(vq, "Loop detected: last one at %u "
  749. "indirect size %u\n",
  750. i, count);
  751. return -EINVAL;
  752. }
  753. if (memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
  754. sizeof desc)) {
  755. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  756. i, (size_t)indirect->addr + i * sizeof desc);
  757. return -EINVAL;
  758. }
  759. if (desc.flags & VRING_DESC_F_INDIRECT) {
  760. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  761. i, (size_t)indirect->addr + i * sizeof desc);
  762. return -EINVAL;
  763. }
  764. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  765. iov_size - iov_count);
  766. if (ret < 0) {
  767. vq_err(vq, "Translation failure %d indirect idx %d\n",
  768. ret, i);
  769. return ret;
  770. }
  771. /* If this is an input descriptor, increment that count. */
  772. if (desc.flags & VRING_DESC_F_WRITE) {
  773. *in_num += ret;
  774. if (unlikely(log)) {
  775. log[*log_num].addr = desc.addr;
  776. log[*log_num].len = desc.len;
  777. ++*log_num;
  778. }
  779. } else {
  780. /* If it's an output descriptor, they're all supposed
  781. * to come before any input descriptors. */
  782. if (*in_num) {
  783. vq_err(vq, "Indirect descriptor "
  784. "has out after in: idx %d\n", i);
  785. return -EINVAL;
  786. }
  787. *out_num += ret;
  788. }
  789. } while ((i = next_desc(&desc)) != -1);
  790. return 0;
  791. }
  792. /* This looks in the virtqueue and for the first available buffer, and converts
  793. * it to an iovec for convenient access. Since descriptors consist of some
  794. * number of output then some number of input descriptors, it's actually two
  795. * iovecs, but we pack them into one and note how many of each there were.
  796. *
  797. * This function returns the descriptor number found, or vq->num (which
  798. * is never a valid descriptor number) if none was found. */
  799. unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  800. struct iovec iov[], unsigned int iov_size,
  801. unsigned int *out_num, unsigned int *in_num,
  802. struct vhost_log *log, unsigned int *log_num)
  803. {
  804. struct vring_desc desc;
  805. unsigned int i, head, found = 0;
  806. u16 last_avail_idx;
  807. int ret;
  808. /* Check it isn't doing very strange things with descriptor numbers. */
  809. last_avail_idx = vq->last_avail_idx;
  810. if (get_user(vq->avail_idx, &vq->avail->idx)) {
  811. vq_err(vq, "Failed to access avail idx at %p\n",
  812. &vq->avail->idx);
  813. return vq->num;
  814. }
  815. if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
  816. vq_err(vq, "Guest moved used index from %u to %u",
  817. last_avail_idx, vq->avail_idx);
  818. return vq->num;
  819. }
  820. /* If there's nothing new since last we looked, return invalid. */
  821. if (vq->avail_idx == last_avail_idx)
  822. return vq->num;
  823. /* Only get avail ring entries after they have been exposed by guest. */
  824. smp_rmb();
  825. /* Grab the next descriptor number they're advertising, and increment
  826. * the index we've seen. */
  827. if (get_user(head, &vq->avail->ring[last_avail_idx % vq->num])) {
  828. vq_err(vq, "Failed to read head: idx %d address %p\n",
  829. last_avail_idx,
  830. &vq->avail->ring[last_avail_idx % vq->num]);
  831. return vq->num;
  832. }
  833. /* If their number is silly, that's an error. */
  834. if (head >= vq->num) {
  835. vq_err(vq, "Guest says index %u > %u is available",
  836. head, vq->num);
  837. return vq->num;
  838. }
  839. /* When we start there are none of either input nor output. */
  840. *out_num = *in_num = 0;
  841. if (unlikely(log))
  842. *log_num = 0;
  843. i = head;
  844. do {
  845. unsigned iov_count = *in_num + *out_num;
  846. if (i >= vq->num) {
  847. vq_err(vq, "Desc index is %u > %u, head = %u",
  848. i, vq->num, head);
  849. return vq->num;
  850. }
  851. if (++found > vq->num) {
  852. vq_err(vq, "Loop detected: last one at %u "
  853. "vq size %u head %u\n",
  854. i, vq->num, head);
  855. return vq->num;
  856. }
  857. ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
  858. if (ret) {
  859. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  860. i, vq->desc + i);
  861. return vq->num;
  862. }
  863. if (desc.flags & VRING_DESC_F_INDIRECT) {
  864. ret = get_indirect(dev, vq, iov, iov_size,
  865. out_num, in_num,
  866. log, log_num, &desc);
  867. if (ret < 0) {
  868. vq_err(vq, "Failure detected "
  869. "in indirect descriptor at idx %d\n", i);
  870. return vq->num;
  871. }
  872. continue;
  873. }
  874. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  875. iov_size - iov_count);
  876. if (ret < 0) {
  877. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  878. ret, i);
  879. return vq->num;
  880. }
  881. if (desc.flags & VRING_DESC_F_WRITE) {
  882. /* If this is an input descriptor,
  883. * increment that count. */
  884. *in_num += ret;
  885. if (unlikely(log)) {
  886. log[*log_num].addr = desc.addr;
  887. log[*log_num].len = desc.len;
  888. ++*log_num;
  889. }
  890. } else {
  891. /* If it's an output descriptor, they're all supposed
  892. * to come before any input descriptors. */
  893. if (*in_num) {
  894. vq_err(vq, "Descriptor has out after in: "
  895. "idx %d\n", i);
  896. return vq->num;
  897. }
  898. *out_num += ret;
  899. }
  900. } while ((i = next_desc(&desc)) != -1);
  901. /* On success, increment avail index. */
  902. vq->last_avail_idx++;
  903. return head;
  904. }
  905. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  906. void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
  907. {
  908. vq->last_avail_idx--;
  909. }
  910. /* After we've used one of their buffers, we tell them about it. We'll then
  911. * want to notify the guest, using eventfd. */
  912. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  913. {
  914. struct vring_used_elem __user *used;
  915. /* The virtqueue contains a ring of used buffers. Get a pointer to the
  916. * next entry in that used ring. */
  917. used = &vq->used->ring[vq->last_used_idx % vq->num];
  918. if (put_user(head, &used->id)) {
  919. vq_err(vq, "Failed to write used id");
  920. return -EFAULT;
  921. }
  922. if (put_user(len, &used->len)) {
  923. vq_err(vq, "Failed to write used len");
  924. return -EFAULT;
  925. }
  926. /* Make sure buffer is written before we update index. */
  927. smp_wmb();
  928. if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
  929. vq_err(vq, "Failed to increment used idx");
  930. return -EFAULT;
  931. }
  932. if (unlikely(vq->log_used)) {
  933. /* Make sure data is seen before log. */
  934. smp_wmb();
  935. /* Log used ring entry write. */
  936. log_write(vq->log_base,
  937. vq->log_addr +
  938. ((void __user *)used - (void __user *)vq->used),
  939. sizeof *used);
  940. /* Log used index update. */
  941. log_write(vq->log_base,
  942. vq->log_addr + offsetof(struct vring_used, idx),
  943. sizeof vq->used->idx);
  944. if (vq->log_ctx)
  945. eventfd_signal(vq->log_ctx, 1);
  946. }
  947. vq->last_used_idx++;
  948. return 0;
  949. }
  950. /* This actually signals the guest, using eventfd. */
  951. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  952. {
  953. __u16 flags;
  954. /* Flush out used index updates. This is paired
  955. * with the barrier that the Guest executes when enabling
  956. * interrupts. */
  957. smp_mb();
  958. if (get_user(flags, &vq->avail->flags)) {
  959. vq_err(vq, "Failed to get flags");
  960. return;
  961. }
  962. /* If they don't want an interrupt, don't signal, unless empty. */
  963. if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
  964. (vq->avail_idx != vq->last_avail_idx ||
  965. !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
  966. return;
  967. /* Signal the Guest tell them we used something up. */
  968. if (vq->call_ctx)
  969. eventfd_signal(vq->call_ctx, 1);
  970. }
  971. /* And here's the combo meal deal. Supersize me! */
  972. void vhost_add_used_and_signal(struct vhost_dev *dev,
  973. struct vhost_virtqueue *vq,
  974. unsigned int head, int len)
  975. {
  976. vhost_add_used(vq, head, len);
  977. vhost_signal(dev, vq);
  978. }
  979. /* OK, now we need to know about added descriptors. */
  980. bool vhost_enable_notify(struct vhost_virtqueue *vq)
  981. {
  982. u16 avail_idx;
  983. int r;
  984. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  985. return false;
  986. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  987. r = put_user(vq->used_flags, &vq->used->flags);
  988. if (r) {
  989. vq_err(vq, "Failed to enable notification at %p: %d\n",
  990. &vq->used->flags, r);
  991. return false;
  992. }
  993. /* They could have slipped one in as we were doing that: make
  994. * sure it's written, then check again. */
  995. smp_mb();
  996. r = get_user(avail_idx, &vq->avail->idx);
  997. if (r) {
  998. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  999. &vq->avail->idx, r);
  1000. return false;
  1001. }
  1002. return avail_idx != vq->last_avail_idx;
  1003. }
  1004. /* We don't need to be notified again. */
  1005. void vhost_disable_notify(struct vhost_virtqueue *vq)
  1006. {
  1007. int r;
  1008. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  1009. return;
  1010. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  1011. r = put_user(vq->used_flags, &vq->used->flags);
  1012. if (r)
  1013. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1014. &vq->used->flags, r);
  1015. }
  1016. int vhost_init(void)
  1017. {
  1018. vhost_workqueue = create_singlethread_workqueue("vhost");
  1019. if (!vhost_workqueue)
  1020. return -ENOMEM;
  1021. return 0;
  1022. }
  1023. void vhost_cleanup(void)
  1024. {
  1025. destroy_workqueue(vhost_workqueue);
  1026. }