vhost.c 28 KB

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