vhost.c 28 KB

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