vhost.c 28 KB

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