vhost.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. for (i = 0; i < mem->nregions; ++i) {
  211. struct vhost_memory_region *m = mem->regions + i;
  212. unsigned long a = m->userspace_addr;
  213. if (m->memory_size > ULONG_MAX)
  214. return 0;
  215. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  216. m->memory_size))
  217. return 0;
  218. else if (log_all && !log_access_ok(log_base,
  219. m->guest_phys_addr,
  220. m->memory_size))
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. /* Can we switch to this memory table? */
  226. /* Caller should have device mutex but not vq mutex */
  227. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  228. int log_all)
  229. {
  230. int i;
  231. for (i = 0; i < d->nvqs; ++i) {
  232. int ok;
  233. mutex_lock(&d->vqs[i].mutex);
  234. /* If ring is inactive, will check when it's enabled. */
  235. if (d->vqs[i].private_data)
  236. ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
  237. log_all);
  238. else
  239. ok = 1;
  240. mutex_unlock(&d->vqs[i].mutex);
  241. if (!ok)
  242. return 0;
  243. }
  244. return 1;
  245. }
  246. static int vq_access_ok(unsigned int num,
  247. struct vring_desc __user *desc,
  248. struct vring_avail __user *avail,
  249. struct vring_used __user *used)
  250. {
  251. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  252. access_ok(VERIFY_READ, avail,
  253. sizeof *avail + num * sizeof *avail->ring) &&
  254. access_ok(VERIFY_WRITE, used,
  255. sizeof *used + num * sizeof *used->ring);
  256. }
  257. /* Can we log writes? */
  258. /* Caller should have device mutex but not vq mutex */
  259. int vhost_log_access_ok(struct vhost_dev *dev)
  260. {
  261. return memory_access_ok(dev, dev->memory, 1);
  262. }
  263. /* Verify access for write logging. */
  264. /* Caller should have vq mutex and device mutex */
  265. static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
  266. {
  267. return vq_memory_access_ok(log_base, vq->dev->memory,
  268. vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
  269. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  270. sizeof *vq->used +
  271. vq->num * sizeof *vq->used->ring));
  272. }
  273. /* Can we start vq? */
  274. /* Caller should have vq mutex and device mutex */
  275. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  276. {
  277. return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
  278. vq_log_access_ok(vq, vq->log_base);
  279. }
  280. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  281. {
  282. struct vhost_memory mem, *newmem, *oldmem;
  283. unsigned long size = offsetof(struct vhost_memory, regions);
  284. long r;
  285. r = copy_from_user(&mem, m, size);
  286. if (r)
  287. return r;
  288. if (mem.padding)
  289. return -EOPNOTSUPP;
  290. if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
  291. return -E2BIG;
  292. newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
  293. if (!newmem)
  294. return -ENOMEM;
  295. memcpy(newmem, &mem, size);
  296. r = copy_from_user(newmem->regions, m->regions,
  297. mem.nregions * sizeof *m->regions);
  298. if (r) {
  299. kfree(newmem);
  300. return r;
  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. r = copy_from_user(&s, argp, sizeof s);
  346. if (r < 0)
  347. break;
  348. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  349. r = -EINVAL;
  350. break;
  351. }
  352. vq->num = s.num;
  353. break;
  354. case VHOST_SET_VRING_BASE:
  355. /* Moving base with an active backend?
  356. * You don't want to do that. */
  357. if (vq->private_data) {
  358. r = -EBUSY;
  359. break;
  360. }
  361. r = copy_from_user(&s, argp, sizeof s);
  362. if (r < 0)
  363. break;
  364. if (s.num > 0xffff) {
  365. r = -EINVAL;
  366. break;
  367. }
  368. vq->last_avail_idx = s.num;
  369. /* Forget the cached index value. */
  370. vq->avail_idx = vq->last_avail_idx;
  371. break;
  372. case VHOST_GET_VRING_BASE:
  373. s.index = idx;
  374. s.num = vq->last_avail_idx;
  375. r = copy_to_user(argp, &s, sizeof s);
  376. break;
  377. case VHOST_SET_VRING_ADDR:
  378. r = copy_from_user(&a, argp, sizeof a);
  379. if (r < 0)
  380. break;
  381. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  382. r = -EOPNOTSUPP;
  383. break;
  384. }
  385. /* For 32bit, verify that the top 32bits of the user
  386. data are set to zero. */
  387. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  388. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  389. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  390. r = -EFAULT;
  391. break;
  392. }
  393. if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
  394. (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
  395. (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
  396. r = -EINVAL;
  397. break;
  398. }
  399. /* We only verify access here if backend is configured.
  400. * If it is not, we don't as size might not have been setup.
  401. * We will verify when backend is configured. */
  402. if (vq->private_data) {
  403. if (!vq_access_ok(vq->num,
  404. (void __user *)(unsigned long)a.desc_user_addr,
  405. (void __user *)(unsigned long)a.avail_user_addr,
  406. (void __user *)(unsigned long)a.used_user_addr)) {
  407. r = -EINVAL;
  408. break;
  409. }
  410. /* Also validate log access for used ring if enabled. */
  411. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  412. !log_access_ok(vq->log_base, a.log_guest_addr,
  413. sizeof *vq->used +
  414. vq->num * sizeof *vq->used->ring)) {
  415. r = -EINVAL;
  416. break;
  417. }
  418. }
  419. r = init_used(vq, (struct vring_used __user *)(unsigned long)
  420. a.used_user_addr);
  421. if (r)
  422. break;
  423. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  424. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  425. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  426. vq->log_addr = a.log_guest_addr;
  427. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  428. break;
  429. case VHOST_SET_VRING_KICK:
  430. r = copy_from_user(&f, argp, sizeof f);
  431. if (r < 0)
  432. break;
  433. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  434. if (IS_ERR(eventfp))
  435. return PTR_ERR(eventfp);
  436. if (eventfp != vq->kick) {
  437. pollstop = filep = vq->kick;
  438. pollstart = vq->kick = eventfp;
  439. } else
  440. filep = eventfp;
  441. break;
  442. case VHOST_SET_VRING_CALL:
  443. r = copy_from_user(&f, argp, sizeof f);
  444. if (r < 0)
  445. break;
  446. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  447. if (IS_ERR(eventfp))
  448. return PTR_ERR(eventfp);
  449. if (eventfp != vq->call) {
  450. filep = vq->call;
  451. ctx = vq->call_ctx;
  452. vq->call = eventfp;
  453. vq->call_ctx = eventfp ?
  454. eventfd_ctx_fileget(eventfp) : NULL;
  455. } else
  456. filep = eventfp;
  457. break;
  458. case VHOST_SET_VRING_ERR:
  459. r = copy_from_user(&f, argp, sizeof f);
  460. if (r < 0)
  461. break;
  462. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  463. if (IS_ERR(eventfp))
  464. return PTR_ERR(eventfp);
  465. if (eventfp != vq->error) {
  466. filep = vq->error;
  467. vq->error = eventfp;
  468. ctx = vq->error_ctx;
  469. vq->error_ctx = eventfp ?
  470. eventfd_ctx_fileget(eventfp) : NULL;
  471. } else
  472. filep = eventfp;
  473. break;
  474. default:
  475. r = -ENOIOCTLCMD;
  476. }
  477. if (pollstop && vq->handle_kick)
  478. vhost_poll_stop(&vq->poll);
  479. if (ctx)
  480. eventfd_ctx_put(ctx);
  481. if (filep)
  482. fput(filep);
  483. if (pollstart && vq->handle_kick)
  484. vhost_poll_start(&vq->poll, vq->kick);
  485. mutex_unlock(&vq->mutex);
  486. if (pollstop && vq->handle_kick)
  487. vhost_poll_flush(&vq->poll);
  488. return r;
  489. }
  490. /* Caller must have device mutex */
  491. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
  492. {
  493. void __user *argp = (void __user *)arg;
  494. struct file *eventfp, *filep = NULL;
  495. struct eventfd_ctx *ctx = NULL;
  496. u64 p;
  497. long r;
  498. int i, fd;
  499. /* If you are not the owner, you can become one */
  500. if (ioctl == VHOST_SET_OWNER) {
  501. r = vhost_dev_set_owner(d);
  502. goto done;
  503. }
  504. /* You must be the owner to do anything else */
  505. r = vhost_dev_check_owner(d);
  506. if (r)
  507. goto done;
  508. switch (ioctl) {
  509. case VHOST_SET_MEM_TABLE:
  510. r = vhost_set_memory(d, argp);
  511. break;
  512. case VHOST_SET_LOG_BASE:
  513. r = copy_from_user(&p, argp, sizeof p);
  514. if (r < 0)
  515. break;
  516. if ((u64)(unsigned long)p != p) {
  517. r = -EFAULT;
  518. break;
  519. }
  520. for (i = 0; i < d->nvqs; ++i) {
  521. struct vhost_virtqueue *vq;
  522. void __user *base = (void __user *)(unsigned long)p;
  523. vq = d->vqs + i;
  524. mutex_lock(&vq->mutex);
  525. /* If ring is inactive, will check when it's enabled. */
  526. if (vq->private_data && !vq_log_access_ok(vq, base))
  527. r = -EFAULT;
  528. else
  529. vq->log_base = base;
  530. mutex_unlock(&vq->mutex);
  531. }
  532. break;
  533. case VHOST_SET_LOG_FD:
  534. r = get_user(fd, (int __user *)argp);
  535. if (r < 0)
  536. break;
  537. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  538. if (IS_ERR(eventfp)) {
  539. r = PTR_ERR(eventfp);
  540. break;
  541. }
  542. if (eventfp != d->log_file) {
  543. filep = d->log_file;
  544. ctx = d->log_ctx;
  545. d->log_ctx = eventfp ?
  546. eventfd_ctx_fileget(eventfp) : NULL;
  547. } else
  548. filep = eventfp;
  549. for (i = 0; i < d->nvqs; ++i) {
  550. mutex_lock(&d->vqs[i].mutex);
  551. d->vqs[i].log_ctx = d->log_ctx;
  552. mutex_unlock(&d->vqs[i].mutex);
  553. }
  554. if (ctx)
  555. eventfd_ctx_put(ctx);
  556. if (filep)
  557. fput(filep);
  558. break;
  559. default:
  560. r = vhost_set_vring(d, ioctl, argp);
  561. break;
  562. }
  563. done:
  564. return r;
  565. }
  566. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  567. __u64 addr, __u32 len)
  568. {
  569. struct vhost_memory_region *reg;
  570. int i;
  571. /* linear search is not brilliant, but we really have on the order of 6
  572. * regions in practice */
  573. for (i = 0; i < mem->nregions; ++i) {
  574. reg = mem->regions + i;
  575. if (reg->guest_phys_addr <= addr &&
  576. reg->guest_phys_addr + reg->memory_size - 1 >= addr)
  577. return reg;
  578. }
  579. return NULL;
  580. }
  581. /* TODO: This is really inefficient. We need something like get_user()
  582. * (instruction directly accesses the data, with an exception table entry
  583. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  584. */
  585. static int set_bit_to_user(int nr, void __user *addr)
  586. {
  587. unsigned long log = (unsigned long)addr;
  588. struct page *page;
  589. void *base;
  590. int bit = nr + (log % PAGE_SIZE) * 8;
  591. int r;
  592. r = get_user_pages_fast(log, 1, 1, &page);
  593. if (r < 0)
  594. return r;
  595. BUG_ON(r != 1);
  596. base = kmap_atomic(page, KM_USER0);
  597. set_bit(bit, base);
  598. kunmap_atomic(base, KM_USER0);
  599. set_page_dirty_lock(page);
  600. put_page(page);
  601. return 0;
  602. }
  603. static int log_write(void __user *log_base,
  604. u64 write_address, u64 write_length)
  605. {
  606. int r;
  607. if (!write_length)
  608. return 0;
  609. write_address /= VHOST_PAGE_SIZE;
  610. for (;;) {
  611. u64 base = (u64)(unsigned long)log_base;
  612. u64 log = base + write_address / 8;
  613. int bit = write_address % 8;
  614. if ((u64)(unsigned long)log != log)
  615. return -EFAULT;
  616. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  617. if (r < 0)
  618. return r;
  619. if (write_length <= VHOST_PAGE_SIZE)
  620. break;
  621. write_length -= VHOST_PAGE_SIZE;
  622. write_address += VHOST_PAGE_SIZE;
  623. }
  624. return r;
  625. }
  626. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  627. unsigned int log_num, u64 len)
  628. {
  629. int i, r;
  630. /* Make sure data written is seen before log. */
  631. smp_wmb();
  632. for (i = 0; i < log_num; ++i) {
  633. u64 l = min(log[i].len, len);
  634. r = log_write(vq->log_base, log[i].addr, l);
  635. if (r < 0)
  636. return r;
  637. len -= l;
  638. if (!len)
  639. return 0;
  640. }
  641. if (vq->log_ctx)
  642. eventfd_signal(vq->log_ctx, 1);
  643. /* Length written exceeds what we have stored. This is a bug. */
  644. BUG();
  645. return 0;
  646. }
  647. int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
  648. struct iovec iov[], int iov_size)
  649. {
  650. const struct vhost_memory_region *reg;
  651. struct vhost_memory *mem;
  652. struct iovec *_iov;
  653. u64 s = 0;
  654. int ret = 0;
  655. rcu_read_lock();
  656. mem = rcu_dereference(dev->memory);
  657. while ((u64)len > s) {
  658. u64 size;
  659. if (ret >= iov_size) {
  660. ret = -ENOBUFS;
  661. break;
  662. }
  663. reg = find_region(mem, addr, len);
  664. if (!reg) {
  665. ret = -EFAULT;
  666. break;
  667. }
  668. _iov = iov + ret;
  669. size = reg->memory_size - addr + reg->guest_phys_addr;
  670. _iov->iov_len = min((u64)len, size);
  671. _iov->iov_base = (void *)(unsigned long)
  672. (reg->userspace_addr + addr - reg->guest_phys_addr);
  673. s += size;
  674. addr += size;
  675. ++ret;
  676. }
  677. rcu_read_unlock();
  678. return ret;
  679. }
  680. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  681. * function returns the next descriptor in the chain,
  682. * or -1U if we're at the end. */
  683. static unsigned next_desc(struct vring_desc *desc)
  684. {
  685. unsigned int next;
  686. /* If this descriptor says it doesn't chain, we're done. */
  687. if (!(desc->flags & VRING_DESC_F_NEXT))
  688. return -1U;
  689. /* Check they're not leading us off end of descriptors. */
  690. next = desc->next;
  691. /* Make sure compiler knows to grab that: we don't want it changing! */
  692. /* We will use the result as an index in an array, so most
  693. * architectures only need a compiler barrier here. */
  694. read_barrier_depends();
  695. return next;
  696. }
  697. static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  698. struct iovec iov[], unsigned int iov_size,
  699. unsigned int *out_num, unsigned int *in_num,
  700. struct vhost_log *log, unsigned int *log_num,
  701. struct vring_desc *indirect)
  702. {
  703. struct vring_desc desc;
  704. unsigned int i = 0, count, found = 0;
  705. int ret;
  706. /* Sanity check */
  707. if (indirect->len % sizeof desc) {
  708. vq_err(vq, "Invalid length in indirect descriptor: "
  709. "len 0x%llx not multiple of 0x%zx\n",
  710. (unsigned long long)indirect->len,
  711. sizeof desc);
  712. return -EINVAL;
  713. }
  714. ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
  715. ARRAY_SIZE(vq->indirect));
  716. if (ret < 0) {
  717. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  718. return ret;
  719. }
  720. /* We will use the result as an address to read from, so most
  721. * architectures only need a compiler barrier here. */
  722. read_barrier_depends();
  723. count = indirect->len / sizeof desc;
  724. /* Buffers are chained via a 16 bit next field, so
  725. * we can have at most 2^16 of these. */
  726. if (count > USHORT_MAX + 1) {
  727. vq_err(vq, "Indirect buffer length too big: %d\n",
  728. indirect->len);
  729. return -E2BIG;
  730. }
  731. do {
  732. unsigned iov_count = *in_num + *out_num;
  733. if (++found > count) {
  734. vq_err(vq, "Loop detected: last one at %u "
  735. "indirect size %u\n",
  736. i, count);
  737. return -EINVAL;
  738. }
  739. if (memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
  740. sizeof desc)) {
  741. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  742. i, (size_t)indirect->addr + i * sizeof desc);
  743. return -EINVAL;
  744. }
  745. if (desc.flags & VRING_DESC_F_INDIRECT) {
  746. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  747. i, (size_t)indirect->addr + i * sizeof desc);
  748. return -EINVAL;
  749. }
  750. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  751. iov_size - iov_count);
  752. if (ret < 0) {
  753. vq_err(vq, "Translation failure %d indirect idx %d\n",
  754. ret, i);
  755. return ret;
  756. }
  757. /* If this is an input descriptor, increment that count. */
  758. if (desc.flags & VRING_DESC_F_WRITE) {
  759. *in_num += ret;
  760. if (unlikely(log)) {
  761. log[*log_num].addr = desc.addr;
  762. log[*log_num].len = desc.len;
  763. ++*log_num;
  764. }
  765. } else {
  766. /* If it's an output descriptor, they're all supposed
  767. * to come before any input descriptors. */
  768. if (*in_num) {
  769. vq_err(vq, "Indirect descriptor "
  770. "has out after in: idx %d\n", i);
  771. return -EINVAL;
  772. }
  773. *out_num += ret;
  774. }
  775. } while ((i = next_desc(&desc)) != -1);
  776. return 0;
  777. }
  778. /* This looks in the virtqueue and for the first available buffer, and converts
  779. * it to an iovec for convenient access. Since descriptors consist of some
  780. * number of output then some number of input descriptors, it's actually two
  781. * iovecs, but we pack them into one and note how many of each there were.
  782. *
  783. * This function returns the descriptor number found, or vq->num (which
  784. * is never a valid descriptor number) if none was found. */
  785. unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  786. struct iovec iov[], unsigned int iov_size,
  787. unsigned int *out_num, unsigned int *in_num,
  788. struct vhost_log *log, unsigned int *log_num)
  789. {
  790. struct vring_desc desc;
  791. unsigned int i, head, found = 0;
  792. u16 last_avail_idx;
  793. int ret;
  794. /* Check it isn't doing very strange things with descriptor numbers. */
  795. last_avail_idx = vq->last_avail_idx;
  796. if (get_user(vq->avail_idx, &vq->avail->idx)) {
  797. vq_err(vq, "Failed to access avail idx at %p\n",
  798. &vq->avail->idx);
  799. return vq->num;
  800. }
  801. if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
  802. vq_err(vq, "Guest moved used index from %u to %u",
  803. last_avail_idx, vq->avail_idx);
  804. return vq->num;
  805. }
  806. /* If there's nothing new since last we looked, return invalid. */
  807. if (vq->avail_idx == last_avail_idx)
  808. return vq->num;
  809. /* Only get avail ring entries after they have been exposed by guest. */
  810. smp_rmb();
  811. /* Grab the next descriptor number they're advertising, and increment
  812. * the index we've seen. */
  813. if (get_user(head, &vq->avail->ring[last_avail_idx % vq->num])) {
  814. vq_err(vq, "Failed to read head: idx %d address %p\n",
  815. last_avail_idx,
  816. &vq->avail->ring[last_avail_idx % vq->num]);
  817. return vq->num;
  818. }
  819. /* If their number is silly, that's an error. */
  820. if (head >= vq->num) {
  821. vq_err(vq, "Guest says index %u > %u is available",
  822. head, vq->num);
  823. return vq->num;
  824. }
  825. /* When we start there are none of either input nor output. */
  826. *out_num = *in_num = 0;
  827. if (unlikely(log))
  828. *log_num = 0;
  829. i = head;
  830. do {
  831. unsigned iov_count = *in_num + *out_num;
  832. if (i >= vq->num) {
  833. vq_err(vq, "Desc index is %u > %u, head = %u",
  834. i, vq->num, head);
  835. return vq->num;
  836. }
  837. if (++found > vq->num) {
  838. vq_err(vq, "Loop detected: last one at %u "
  839. "vq size %u head %u\n",
  840. i, vq->num, head);
  841. return vq->num;
  842. }
  843. ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
  844. if (ret) {
  845. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  846. i, vq->desc + i);
  847. return vq->num;
  848. }
  849. if (desc.flags & VRING_DESC_F_INDIRECT) {
  850. ret = get_indirect(dev, vq, iov, iov_size,
  851. out_num, in_num,
  852. log, log_num, &desc);
  853. if (ret < 0) {
  854. vq_err(vq, "Failure detected "
  855. "in indirect descriptor at idx %d\n", i);
  856. return vq->num;
  857. }
  858. continue;
  859. }
  860. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  861. iov_size - iov_count);
  862. if (ret < 0) {
  863. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  864. ret, i);
  865. return vq->num;
  866. }
  867. if (desc.flags & VRING_DESC_F_WRITE) {
  868. /* If this is an input descriptor,
  869. * increment that count. */
  870. *in_num += ret;
  871. if (unlikely(log)) {
  872. log[*log_num].addr = desc.addr;
  873. log[*log_num].len = desc.len;
  874. ++*log_num;
  875. }
  876. } else {
  877. /* If it's an output descriptor, they're all supposed
  878. * to come before any input descriptors. */
  879. if (*in_num) {
  880. vq_err(vq, "Descriptor has out after in: "
  881. "idx %d\n", i);
  882. return vq->num;
  883. }
  884. *out_num += ret;
  885. }
  886. } while ((i = next_desc(&desc)) != -1);
  887. /* On success, increment avail index. */
  888. vq->last_avail_idx++;
  889. return head;
  890. }
  891. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  892. void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
  893. {
  894. vq->last_avail_idx--;
  895. }
  896. /* After we've used one of their buffers, we tell them about it. We'll then
  897. * want to notify the guest, using eventfd. */
  898. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  899. {
  900. struct vring_used_elem *used;
  901. /* The virtqueue contains a ring of used buffers. Get a pointer to the
  902. * next entry in that used ring. */
  903. used = &vq->used->ring[vq->last_used_idx % vq->num];
  904. if (put_user(head, &used->id)) {
  905. vq_err(vq, "Failed to write used id");
  906. return -EFAULT;
  907. }
  908. if (put_user(len, &used->len)) {
  909. vq_err(vq, "Failed to write used len");
  910. return -EFAULT;
  911. }
  912. /* Make sure buffer is written before we update index. */
  913. smp_wmb();
  914. if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
  915. vq_err(vq, "Failed to increment used idx");
  916. return -EFAULT;
  917. }
  918. if (unlikely(vq->log_used)) {
  919. /* Make sure data is seen before log. */
  920. smp_wmb();
  921. /* Log used ring entry write. */
  922. log_write(vq->log_base,
  923. vq->log_addr + ((void *)used - (void *)vq->used),
  924. sizeof *used);
  925. /* Log used index update. */
  926. log_write(vq->log_base,
  927. vq->log_addr + offsetof(struct vring_used, idx),
  928. sizeof vq->used->idx);
  929. if (vq->log_ctx)
  930. eventfd_signal(vq->log_ctx, 1);
  931. }
  932. vq->last_used_idx++;
  933. return 0;
  934. }
  935. /* This actually signals the guest, using eventfd. */
  936. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  937. {
  938. __u16 flags = 0;
  939. if (get_user(flags, &vq->avail->flags)) {
  940. vq_err(vq, "Failed to get flags");
  941. return;
  942. }
  943. /* If they don't want an interrupt, don't signal, unless empty. */
  944. if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
  945. (vq->avail_idx != vq->last_avail_idx ||
  946. !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
  947. return;
  948. /* Signal the Guest tell them we used something up. */
  949. if (vq->call_ctx)
  950. eventfd_signal(vq->call_ctx, 1);
  951. }
  952. /* And here's the combo meal deal. Supersize me! */
  953. void vhost_add_used_and_signal(struct vhost_dev *dev,
  954. struct vhost_virtqueue *vq,
  955. unsigned int head, int len)
  956. {
  957. vhost_add_used(vq, head, len);
  958. vhost_signal(dev, vq);
  959. }
  960. /* OK, now we need to know about added descriptors. */
  961. bool vhost_enable_notify(struct vhost_virtqueue *vq)
  962. {
  963. u16 avail_idx;
  964. int r;
  965. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  966. return false;
  967. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  968. r = put_user(vq->used_flags, &vq->used->flags);
  969. if (r) {
  970. vq_err(vq, "Failed to enable notification at %p: %d\n",
  971. &vq->used->flags, r);
  972. return false;
  973. }
  974. /* They could have slipped one in as we were doing that: make
  975. * sure it's written, then check again. */
  976. smp_mb();
  977. r = get_user(avail_idx, &vq->avail->idx);
  978. if (r) {
  979. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  980. &vq->avail->idx, r);
  981. return false;
  982. }
  983. return avail_idx != vq->last_avail_idx;
  984. }
  985. /* We don't need to be notified again. */
  986. void vhost_disable_notify(struct vhost_virtqueue *vq)
  987. {
  988. int r;
  989. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  990. return;
  991. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  992. r = put_user(vq->used_flags, &vq->used->flags);
  993. if (r)
  994. vq_err(vq, "Failed to enable notification at %p: %d\n",
  995. &vq->used->flags, r);
  996. }
  997. int vhost_init(void)
  998. {
  999. vhost_workqueue = create_singlethread_workqueue("vhost");
  1000. if (!vhost_workqueue)
  1001. return -ENOMEM;
  1002. return 0;
  1003. }
  1004. void vhost_cleanup(void)
  1005. {
  1006. destroy_workqueue(vhost_workqueue);
  1007. }