vhost.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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/virtual/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/mmu_context.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/mutex.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/kthread.h>
  26. #include <linux/cgroup.h>
  27. #include "vhost.h"
  28. enum {
  29. VHOST_MEMORY_MAX_NREGIONS = 64,
  30. VHOST_MEMORY_F_LOG = 0x1,
  31. };
  32. #define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
  33. #define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
  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 = container_of(wait, struct vhost_poll, wait);
  46. if (!((unsigned long)key & poll->mask))
  47. return 0;
  48. vhost_poll_queue(poll);
  49. return 0;
  50. }
  51. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
  52. {
  53. INIT_LIST_HEAD(&work->node);
  54. work->fn = fn;
  55. init_waitqueue_head(&work->done);
  56. work->flushing = 0;
  57. work->queue_seq = work->done_seq = 0;
  58. }
  59. /* Init poll structure */
  60. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  61. unsigned long mask, struct vhost_dev *dev)
  62. {
  63. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  64. init_poll_funcptr(&poll->table, vhost_poll_func);
  65. poll->mask = mask;
  66. poll->dev = dev;
  67. poll->wqh = NULL;
  68. vhost_work_init(&poll->work, fn);
  69. }
  70. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  71. * keep a reference to a file until after vhost_poll_stop is called. */
  72. int vhost_poll_start(struct vhost_poll *poll, struct file *file)
  73. {
  74. unsigned long mask;
  75. int ret = 0;
  76. if (poll->wqh)
  77. return 0;
  78. mask = file->f_op->poll(file, &poll->table);
  79. if (mask)
  80. vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
  81. if (mask & POLLERR) {
  82. if (poll->wqh)
  83. remove_wait_queue(poll->wqh, &poll->wait);
  84. ret = -EINVAL;
  85. }
  86. return ret;
  87. }
  88. /* Stop polling a file. After this function returns, it becomes safe to drop the
  89. * file reference. You must also flush afterwards. */
  90. void vhost_poll_stop(struct vhost_poll *poll)
  91. {
  92. if (poll->wqh) {
  93. remove_wait_queue(poll->wqh, &poll->wait);
  94. poll->wqh = NULL;
  95. }
  96. }
  97. static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
  98. unsigned seq)
  99. {
  100. int left;
  101. spin_lock_irq(&dev->work_lock);
  102. left = seq - work->done_seq;
  103. spin_unlock_irq(&dev->work_lock);
  104. return left <= 0;
  105. }
  106. static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
  107. {
  108. unsigned seq;
  109. int flushing;
  110. spin_lock_irq(&dev->work_lock);
  111. seq = work->queue_seq;
  112. work->flushing++;
  113. spin_unlock_irq(&dev->work_lock);
  114. wait_event(work->done, vhost_work_seq_done(dev, work, seq));
  115. spin_lock_irq(&dev->work_lock);
  116. flushing = --work->flushing;
  117. spin_unlock_irq(&dev->work_lock);
  118. BUG_ON(flushing < 0);
  119. }
  120. /* Flush any work that has been scheduled. When calling this, don't hold any
  121. * locks that are also used by the callback. */
  122. void vhost_poll_flush(struct vhost_poll *poll)
  123. {
  124. vhost_work_flush(poll->dev, &poll->work);
  125. }
  126. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&dev->work_lock, flags);
  130. if (list_empty(&work->node)) {
  131. list_add_tail(&work->node, &dev->work_list);
  132. work->queue_seq++;
  133. wake_up_process(dev->worker);
  134. }
  135. spin_unlock_irqrestore(&dev->work_lock, flags);
  136. }
  137. void vhost_poll_queue(struct vhost_poll *poll)
  138. {
  139. vhost_work_queue(poll->dev, &poll->work);
  140. }
  141. static void vhost_vq_reset(struct vhost_dev *dev,
  142. struct vhost_virtqueue *vq)
  143. {
  144. vq->num = 1;
  145. vq->desc = NULL;
  146. vq->avail = NULL;
  147. vq->used = NULL;
  148. vq->last_avail_idx = 0;
  149. vq->avail_idx = 0;
  150. vq->last_used_idx = 0;
  151. vq->signalled_used = 0;
  152. vq->signalled_used_valid = false;
  153. vq->used_flags = 0;
  154. vq->log_used = false;
  155. vq->log_addr = -1ull;
  156. vq->vhost_hlen = 0;
  157. vq->sock_hlen = 0;
  158. vq->private_data = NULL;
  159. vq->log_base = NULL;
  160. vq->error_ctx = NULL;
  161. vq->error = NULL;
  162. vq->kick = NULL;
  163. vq->call_ctx = NULL;
  164. vq->call = NULL;
  165. vq->log_ctx = NULL;
  166. }
  167. static int vhost_worker(void *data)
  168. {
  169. struct vhost_dev *dev = data;
  170. struct vhost_work *work = NULL;
  171. unsigned uninitialized_var(seq);
  172. mm_segment_t oldfs = get_fs();
  173. set_fs(USER_DS);
  174. use_mm(dev->mm);
  175. for (;;) {
  176. /* mb paired w/ kthread_stop */
  177. set_current_state(TASK_INTERRUPTIBLE);
  178. spin_lock_irq(&dev->work_lock);
  179. if (work) {
  180. work->done_seq = seq;
  181. if (work->flushing)
  182. wake_up_all(&work->done);
  183. }
  184. if (kthread_should_stop()) {
  185. spin_unlock_irq(&dev->work_lock);
  186. __set_current_state(TASK_RUNNING);
  187. break;
  188. }
  189. if (!list_empty(&dev->work_list)) {
  190. work = list_first_entry(&dev->work_list,
  191. struct vhost_work, node);
  192. list_del_init(&work->node);
  193. seq = work->queue_seq;
  194. } else
  195. work = NULL;
  196. spin_unlock_irq(&dev->work_lock);
  197. if (work) {
  198. __set_current_state(TASK_RUNNING);
  199. work->fn(work);
  200. if (need_resched())
  201. schedule();
  202. } else
  203. schedule();
  204. }
  205. unuse_mm(dev->mm);
  206. set_fs(oldfs);
  207. return 0;
  208. }
  209. static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  210. {
  211. kfree(vq->indirect);
  212. vq->indirect = NULL;
  213. kfree(vq->log);
  214. vq->log = NULL;
  215. kfree(vq->heads);
  216. vq->heads = NULL;
  217. }
  218. /* Helper to allocate iovec buffers for all vqs. */
  219. static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  220. {
  221. int i;
  222. for (i = 0; i < dev->nvqs; ++i) {
  223. dev->vqs[i]->indirect = kmalloc(sizeof *dev->vqs[i]->indirect *
  224. UIO_MAXIOV, GFP_KERNEL);
  225. dev->vqs[i]->log = kmalloc(sizeof *dev->vqs[i]->log * UIO_MAXIOV,
  226. GFP_KERNEL);
  227. dev->vqs[i]->heads = kmalloc(sizeof *dev->vqs[i]->heads *
  228. UIO_MAXIOV, GFP_KERNEL);
  229. if (!dev->vqs[i]->indirect || !dev->vqs[i]->log ||
  230. !dev->vqs[i]->heads)
  231. goto err_nomem;
  232. }
  233. return 0;
  234. err_nomem:
  235. for (; i >= 0; --i)
  236. vhost_vq_free_iovecs(dev->vqs[i]);
  237. return -ENOMEM;
  238. }
  239. static void vhost_dev_free_iovecs(struct vhost_dev *dev)
  240. {
  241. int i;
  242. for (i = 0; i < dev->nvqs; ++i)
  243. vhost_vq_free_iovecs(dev->vqs[i]);
  244. }
  245. long vhost_dev_init(struct vhost_dev *dev,
  246. struct vhost_virtqueue **vqs, int nvqs)
  247. {
  248. int i;
  249. dev->vqs = vqs;
  250. dev->nvqs = nvqs;
  251. mutex_init(&dev->mutex);
  252. dev->log_ctx = NULL;
  253. dev->log_file = NULL;
  254. dev->memory = NULL;
  255. dev->mm = NULL;
  256. spin_lock_init(&dev->work_lock);
  257. INIT_LIST_HEAD(&dev->work_list);
  258. dev->worker = NULL;
  259. for (i = 0; i < dev->nvqs; ++i) {
  260. dev->vqs[i]->log = NULL;
  261. dev->vqs[i]->indirect = NULL;
  262. dev->vqs[i]->heads = NULL;
  263. dev->vqs[i]->dev = dev;
  264. mutex_init(&dev->vqs[i]->mutex);
  265. vhost_vq_reset(dev, dev->vqs[i]);
  266. if (dev->vqs[i]->handle_kick)
  267. vhost_poll_init(&dev->vqs[i]->poll,
  268. dev->vqs[i]->handle_kick, POLLIN, dev);
  269. }
  270. return 0;
  271. }
  272. /* Caller should have device mutex */
  273. long vhost_dev_check_owner(struct vhost_dev *dev)
  274. {
  275. /* Are you the owner? If not, I don't think you mean to do that */
  276. return dev->mm == current->mm ? 0 : -EPERM;
  277. }
  278. struct vhost_attach_cgroups_struct {
  279. struct vhost_work work;
  280. struct task_struct *owner;
  281. int ret;
  282. };
  283. static void vhost_attach_cgroups_work(struct vhost_work *work)
  284. {
  285. struct vhost_attach_cgroups_struct *s;
  286. s = container_of(work, struct vhost_attach_cgroups_struct, work);
  287. s->ret = cgroup_attach_task_all(s->owner, current);
  288. }
  289. static int vhost_attach_cgroups(struct vhost_dev *dev)
  290. {
  291. struct vhost_attach_cgroups_struct attach;
  292. attach.owner = current;
  293. vhost_work_init(&attach.work, vhost_attach_cgroups_work);
  294. vhost_work_queue(dev, &attach.work);
  295. vhost_work_flush(dev, &attach.work);
  296. return attach.ret;
  297. }
  298. /* Caller should have device mutex */
  299. static long vhost_dev_set_owner(struct vhost_dev *dev)
  300. {
  301. struct task_struct *worker;
  302. int err;
  303. /* Is there an owner already? */
  304. if (dev->mm) {
  305. err = -EBUSY;
  306. goto err_mm;
  307. }
  308. /* No owner, become one */
  309. dev->mm = get_task_mm(current);
  310. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  311. if (IS_ERR(worker)) {
  312. err = PTR_ERR(worker);
  313. goto err_worker;
  314. }
  315. dev->worker = worker;
  316. wake_up_process(worker); /* avoid contributing to loadavg */
  317. err = vhost_attach_cgroups(dev);
  318. if (err)
  319. goto err_cgroup;
  320. err = vhost_dev_alloc_iovecs(dev);
  321. if (err)
  322. goto err_cgroup;
  323. return 0;
  324. err_cgroup:
  325. kthread_stop(worker);
  326. dev->worker = NULL;
  327. err_worker:
  328. if (dev->mm)
  329. mmput(dev->mm);
  330. dev->mm = NULL;
  331. err_mm:
  332. return err;
  333. }
  334. /* Caller should have device mutex */
  335. long vhost_dev_reset_owner(struct vhost_dev *dev)
  336. {
  337. struct vhost_memory *memory;
  338. /* Restore memory to default empty mapping. */
  339. memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
  340. if (!memory)
  341. return -ENOMEM;
  342. vhost_dev_cleanup(dev, true);
  343. memory->nregions = 0;
  344. RCU_INIT_POINTER(dev->memory, memory);
  345. return 0;
  346. }
  347. void vhost_dev_stop(struct vhost_dev *dev)
  348. {
  349. int i;
  350. for (i = 0; i < dev->nvqs; ++i) {
  351. if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
  352. vhost_poll_stop(&dev->vqs[i]->poll);
  353. vhost_poll_flush(&dev->vqs[i]->poll);
  354. }
  355. }
  356. }
  357. /* Caller should have device mutex if and only if locked is set */
  358. void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
  359. {
  360. int i;
  361. for (i = 0; i < dev->nvqs; ++i) {
  362. if (dev->vqs[i]->error_ctx)
  363. eventfd_ctx_put(dev->vqs[i]->error_ctx);
  364. if (dev->vqs[i]->error)
  365. fput(dev->vqs[i]->error);
  366. if (dev->vqs[i]->kick)
  367. fput(dev->vqs[i]->kick);
  368. if (dev->vqs[i]->call_ctx)
  369. eventfd_ctx_put(dev->vqs[i]->call_ctx);
  370. if (dev->vqs[i]->call)
  371. fput(dev->vqs[i]->call);
  372. vhost_vq_reset(dev, dev->vqs[i]);
  373. }
  374. vhost_dev_free_iovecs(dev);
  375. if (dev->log_ctx)
  376. eventfd_ctx_put(dev->log_ctx);
  377. dev->log_ctx = NULL;
  378. if (dev->log_file)
  379. fput(dev->log_file);
  380. dev->log_file = NULL;
  381. /* No one will access memory at this point */
  382. kfree(rcu_dereference_protected(dev->memory,
  383. locked ==
  384. lockdep_is_held(&dev->mutex)));
  385. RCU_INIT_POINTER(dev->memory, NULL);
  386. WARN_ON(!list_empty(&dev->work_list));
  387. if (dev->worker) {
  388. kthread_stop(dev->worker);
  389. dev->worker = NULL;
  390. }
  391. if (dev->mm)
  392. mmput(dev->mm);
  393. dev->mm = NULL;
  394. }
  395. static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  396. {
  397. u64 a = addr / VHOST_PAGE_SIZE / 8;
  398. /* Make sure 64 bit math will not overflow. */
  399. if (a > ULONG_MAX - (unsigned long)log_base ||
  400. a + (unsigned long)log_base > ULONG_MAX)
  401. return 0;
  402. return access_ok(VERIFY_WRITE, log_base + a,
  403. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  404. }
  405. /* Caller should have vq mutex and device mutex. */
  406. static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
  407. int log_all)
  408. {
  409. int i;
  410. if (!mem)
  411. return 0;
  412. for (i = 0; i < mem->nregions; ++i) {
  413. struct vhost_memory_region *m = mem->regions + i;
  414. unsigned long a = m->userspace_addr;
  415. if (m->memory_size > ULONG_MAX)
  416. return 0;
  417. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  418. m->memory_size))
  419. return 0;
  420. else if (log_all && !log_access_ok(log_base,
  421. m->guest_phys_addr,
  422. m->memory_size))
  423. return 0;
  424. }
  425. return 1;
  426. }
  427. /* Can we switch to this memory table? */
  428. /* Caller should have device mutex but not vq mutex */
  429. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  430. int log_all)
  431. {
  432. int i;
  433. for (i = 0; i < d->nvqs; ++i) {
  434. int ok;
  435. mutex_lock(&d->vqs[i]->mutex);
  436. /* If ring is inactive, will check when it's enabled. */
  437. if (d->vqs[i]->private_data)
  438. ok = vq_memory_access_ok(d->vqs[i]->log_base, mem,
  439. log_all);
  440. else
  441. ok = 1;
  442. mutex_unlock(&d->vqs[i]->mutex);
  443. if (!ok)
  444. return 0;
  445. }
  446. return 1;
  447. }
  448. static int vq_access_ok(struct vhost_dev *d, unsigned int num,
  449. struct vring_desc __user *desc,
  450. struct vring_avail __user *avail,
  451. struct vring_used __user *used)
  452. {
  453. size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  454. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  455. access_ok(VERIFY_READ, avail,
  456. sizeof *avail + num * sizeof *avail->ring + s) &&
  457. access_ok(VERIFY_WRITE, used,
  458. sizeof *used + num * sizeof *used->ring + s);
  459. }
  460. /* Can we log writes? */
  461. /* Caller should have device mutex but not vq mutex */
  462. int vhost_log_access_ok(struct vhost_dev *dev)
  463. {
  464. struct vhost_memory *mp;
  465. mp = rcu_dereference_protected(dev->memory,
  466. lockdep_is_held(&dev->mutex));
  467. return memory_access_ok(dev, mp, 1);
  468. }
  469. /* Verify access for write logging. */
  470. /* Caller should have vq mutex and device mutex */
  471. static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
  472. void __user *log_base)
  473. {
  474. struct vhost_memory *mp;
  475. size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  476. mp = rcu_dereference_protected(vq->dev->memory,
  477. lockdep_is_held(&vq->mutex));
  478. return vq_memory_access_ok(log_base, mp,
  479. vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
  480. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  481. sizeof *vq->used +
  482. vq->num * sizeof *vq->used->ring + s));
  483. }
  484. /* Can we start vq? */
  485. /* Caller should have vq mutex and device mutex */
  486. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  487. {
  488. return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
  489. vq_log_access_ok(vq->dev, vq, vq->log_base);
  490. }
  491. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  492. {
  493. struct vhost_memory mem, *newmem, *oldmem;
  494. unsigned long size = offsetof(struct vhost_memory, regions);
  495. if (copy_from_user(&mem, m, size))
  496. return -EFAULT;
  497. if (mem.padding)
  498. return -EOPNOTSUPP;
  499. if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
  500. return -E2BIG;
  501. newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
  502. if (!newmem)
  503. return -ENOMEM;
  504. memcpy(newmem, &mem, size);
  505. if (copy_from_user(newmem->regions, m->regions,
  506. mem.nregions * sizeof *m->regions)) {
  507. kfree(newmem);
  508. return -EFAULT;
  509. }
  510. if (!memory_access_ok(d, newmem,
  511. vhost_has_feature(d, VHOST_F_LOG_ALL))) {
  512. kfree(newmem);
  513. return -EFAULT;
  514. }
  515. oldmem = rcu_dereference_protected(d->memory,
  516. lockdep_is_held(&d->mutex));
  517. rcu_assign_pointer(d->memory, newmem);
  518. synchronize_rcu();
  519. kfree(oldmem);
  520. return 0;
  521. }
  522. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
  523. {
  524. struct file *eventfp, *filep = NULL;
  525. bool pollstart = false, pollstop = false;
  526. struct eventfd_ctx *ctx = NULL;
  527. u32 __user *idxp = argp;
  528. struct vhost_virtqueue *vq;
  529. struct vhost_vring_state s;
  530. struct vhost_vring_file f;
  531. struct vhost_vring_addr a;
  532. u32 idx;
  533. long r;
  534. r = get_user(idx, idxp);
  535. if (r < 0)
  536. return r;
  537. if (idx >= d->nvqs)
  538. return -ENOBUFS;
  539. vq = d->vqs[idx];
  540. mutex_lock(&vq->mutex);
  541. switch (ioctl) {
  542. case VHOST_SET_VRING_NUM:
  543. /* Resizing ring with an active backend?
  544. * You don't want to do that. */
  545. if (vq->private_data) {
  546. r = -EBUSY;
  547. break;
  548. }
  549. if (copy_from_user(&s, argp, sizeof s)) {
  550. r = -EFAULT;
  551. break;
  552. }
  553. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  554. r = -EINVAL;
  555. break;
  556. }
  557. vq->num = s.num;
  558. break;
  559. case VHOST_SET_VRING_BASE:
  560. /* Moving base with an active backend?
  561. * You don't want to do that. */
  562. if (vq->private_data) {
  563. r = -EBUSY;
  564. break;
  565. }
  566. if (copy_from_user(&s, argp, sizeof s)) {
  567. r = -EFAULT;
  568. break;
  569. }
  570. if (s.num > 0xffff) {
  571. r = -EINVAL;
  572. break;
  573. }
  574. vq->last_avail_idx = s.num;
  575. /* Forget the cached index value. */
  576. vq->avail_idx = vq->last_avail_idx;
  577. break;
  578. case VHOST_GET_VRING_BASE:
  579. s.index = idx;
  580. s.num = vq->last_avail_idx;
  581. if (copy_to_user(argp, &s, sizeof s))
  582. r = -EFAULT;
  583. break;
  584. case VHOST_SET_VRING_ADDR:
  585. if (copy_from_user(&a, argp, sizeof a)) {
  586. r = -EFAULT;
  587. break;
  588. }
  589. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  590. r = -EOPNOTSUPP;
  591. break;
  592. }
  593. /* For 32bit, verify that the top 32bits of the user
  594. data are set to zero. */
  595. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  596. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  597. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  598. r = -EFAULT;
  599. break;
  600. }
  601. if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
  602. (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
  603. (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
  604. r = -EINVAL;
  605. break;
  606. }
  607. /* We only verify access here if backend is configured.
  608. * If it is not, we don't as size might not have been setup.
  609. * We will verify when backend is configured. */
  610. if (vq->private_data) {
  611. if (!vq_access_ok(d, vq->num,
  612. (void __user *)(unsigned long)a.desc_user_addr,
  613. (void __user *)(unsigned long)a.avail_user_addr,
  614. (void __user *)(unsigned long)a.used_user_addr)) {
  615. r = -EINVAL;
  616. break;
  617. }
  618. /* Also validate log access for used ring if enabled. */
  619. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  620. !log_access_ok(vq->log_base, a.log_guest_addr,
  621. sizeof *vq->used +
  622. vq->num * sizeof *vq->used->ring)) {
  623. r = -EINVAL;
  624. break;
  625. }
  626. }
  627. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  628. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  629. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  630. vq->log_addr = a.log_guest_addr;
  631. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  632. break;
  633. case VHOST_SET_VRING_KICK:
  634. if (copy_from_user(&f, argp, sizeof f)) {
  635. r = -EFAULT;
  636. break;
  637. }
  638. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  639. if (IS_ERR(eventfp)) {
  640. r = PTR_ERR(eventfp);
  641. break;
  642. }
  643. if (eventfp != vq->kick) {
  644. pollstop = (filep = vq->kick) != NULL;
  645. pollstart = (vq->kick = eventfp) != NULL;
  646. } else
  647. filep = eventfp;
  648. break;
  649. case VHOST_SET_VRING_CALL:
  650. if (copy_from_user(&f, argp, sizeof f)) {
  651. r = -EFAULT;
  652. break;
  653. }
  654. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  655. if (IS_ERR(eventfp)) {
  656. r = PTR_ERR(eventfp);
  657. break;
  658. }
  659. if (eventfp != vq->call) {
  660. filep = vq->call;
  661. ctx = vq->call_ctx;
  662. vq->call = eventfp;
  663. vq->call_ctx = eventfp ?
  664. eventfd_ctx_fileget(eventfp) : NULL;
  665. } else
  666. filep = eventfp;
  667. break;
  668. case VHOST_SET_VRING_ERR:
  669. if (copy_from_user(&f, argp, sizeof f)) {
  670. r = -EFAULT;
  671. break;
  672. }
  673. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  674. if (IS_ERR(eventfp)) {
  675. r = PTR_ERR(eventfp);
  676. break;
  677. }
  678. if (eventfp != vq->error) {
  679. filep = vq->error;
  680. vq->error = eventfp;
  681. ctx = vq->error_ctx;
  682. vq->error_ctx = eventfp ?
  683. eventfd_ctx_fileget(eventfp) : NULL;
  684. } else
  685. filep = eventfp;
  686. break;
  687. default:
  688. r = -ENOIOCTLCMD;
  689. }
  690. if (pollstop && vq->handle_kick)
  691. vhost_poll_stop(&vq->poll);
  692. if (ctx)
  693. eventfd_ctx_put(ctx);
  694. if (filep)
  695. fput(filep);
  696. if (pollstart && vq->handle_kick)
  697. r = vhost_poll_start(&vq->poll, vq->kick);
  698. mutex_unlock(&vq->mutex);
  699. if (pollstop && vq->handle_kick)
  700. vhost_poll_flush(&vq->poll);
  701. return r;
  702. }
  703. /* Caller must have device mutex */
  704. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  705. {
  706. struct file *eventfp, *filep = NULL;
  707. struct eventfd_ctx *ctx = NULL;
  708. u64 p;
  709. long r;
  710. int i, fd;
  711. /* If you are not the owner, you can become one */
  712. if (ioctl == VHOST_SET_OWNER) {
  713. r = vhost_dev_set_owner(d);
  714. goto done;
  715. }
  716. /* You must be the owner to do anything else */
  717. r = vhost_dev_check_owner(d);
  718. if (r)
  719. goto done;
  720. switch (ioctl) {
  721. case VHOST_SET_MEM_TABLE:
  722. r = vhost_set_memory(d, argp);
  723. break;
  724. case VHOST_SET_LOG_BASE:
  725. if (copy_from_user(&p, argp, sizeof p)) {
  726. r = -EFAULT;
  727. break;
  728. }
  729. if ((u64)(unsigned long)p != p) {
  730. r = -EFAULT;
  731. break;
  732. }
  733. for (i = 0; i < d->nvqs; ++i) {
  734. struct vhost_virtqueue *vq;
  735. void __user *base = (void __user *)(unsigned long)p;
  736. vq = d->vqs[i];
  737. mutex_lock(&vq->mutex);
  738. /* If ring is inactive, will check when it's enabled. */
  739. if (vq->private_data && !vq_log_access_ok(d, vq, base))
  740. r = -EFAULT;
  741. else
  742. vq->log_base = base;
  743. mutex_unlock(&vq->mutex);
  744. }
  745. break;
  746. case VHOST_SET_LOG_FD:
  747. r = get_user(fd, (int __user *)argp);
  748. if (r < 0)
  749. break;
  750. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  751. if (IS_ERR(eventfp)) {
  752. r = PTR_ERR(eventfp);
  753. break;
  754. }
  755. if (eventfp != d->log_file) {
  756. filep = d->log_file;
  757. ctx = d->log_ctx;
  758. d->log_ctx = eventfp ?
  759. eventfd_ctx_fileget(eventfp) : NULL;
  760. } else
  761. filep = eventfp;
  762. for (i = 0; i < d->nvqs; ++i) {
  763. mutex_lock(&d->vqs[i]->mutex);
  764. d->vqs[i]->log_ctx = d->log_ctx;
  765. mutex_unlock(&d->vqs[i]->mutex);
  766. }
  767. if (ctx)
  768. eventfd_ctx_put(ctx);
  769. if (filep)
  770. fput(filep);
  771. break;
  772. default:
  773. r = -ENOIOCTLCMD;
  774. break;
  775. }
  776. done:
  777. return r;
  778. }
  779. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  780. __u64 addr, __u32 len)
  781. {
  782. struct vhost_memory_region *reg;
  783. int i;
  784. /* linear search is not brilliant, but we really have on the order of 6
  785. * regions in practice */
  786. for (i = 0; i < mem->nregions; ++i) {
  787. reg = mem->regions + i;
  788. if (reg->guest_phys_addr <= addr &&
  789. reg->guest_phys_addr + reg->memory_size - 1 >= addr)
  790. return reg;
  791. }
  792. return NULL;
  793. }
  794. /* TODO: This is really inefficient. We need something like get_user()
  795. * (instruction directly accesses the data, with an exception table entry
  796. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  797. */
  798. static int set_bit_to_user(int nr, void __user *addr)
  799. {
  800. unsigned long log = (unsigned long)addr;
  801. struct page *page;
  802. void *base;
  803. int bit = nr + (log % PAGE_SIZE) * 8;
  804. int r;
  805. r = get_user_pages_fast(log, 1, 1, &page);
  806. if (r < 0)
  807. return r;
  808. BUG_ON(r != 1);
  809. base = kmap_atomic(page);
  810. set_bit(bit, base);
  811. kunmap_atomic(base);
  812. set_page_dirty_lock(page);
  813. put_page(page);
  814. return 0;
  815. }
  816. static int log_write(void __user *log_base,
  817. u64 write_address, u64 write_length)
  818. {
  819. u64 write_page = write_address / VHOST_PAGE_SIZE;
  820. int r;
  821. if (!write_length)
  822. return 0;
  823. write_length += write_address % VHOST_PAGE_SIZE;
  824. for (;;) {
  825. u64 base = (u64)(unsigned long)log_base;
  826. u64 log = base + write_page / 8;
  827. int bit = write_page % 8;
  828. if ((u64)(unsigned long)log != log)
  829. return -EFAULT;
  830. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  831. if (r < 0)
  832. return r;
  833. if (write_length <= VHOST_PAGE_SIZE)
  834. break;
  835. write_length -= VHOST_PAGE_SIZE;
  836. write_page += 1;
  837. }
  838. return r;
  839. }
  840. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  841. unsigned int log_num, u64 len)
  842. {
  843. int i, r;
  844. /* Make sure data written is seen before log. */
  845. smp_wmb();
  846. for (i = 0; i < log_num; ++i) {
  847. u64 l = min(log[i].len, len);
  848. r = log_write(vq->log_base, log[i].addr, l);
  849. if (r < 0)
  850. return r;
  851. len -= l;
  852. if (!len) {
  853. if (vq->log_ctx)
  854. eventfd_signal(vq->log_ctx, 1);
  855. return 0;
  856. }
  857. }
  858. /* Length written exceeds what we have stored. This is a bug. */
  859. BUG();
  860. return 0;
  861. }
  862. static int vhost_update_used_flags(struct vhost_virtqueue *vq)
  863. {
  864. void __user *used;
  865. if (__put_user(vq->used_flags, &vq->used->flags) < 0)
  866. return -EFAULT;
  867. if (unlikely(vq->log_used)) {
  868. /* Make sure the flag is seen before log. */
  869. smp_wmb();
  870. /* Log used flag write. */
  871. used = &vq->used->flags;
  872. log_write(vq->log_base, vq->log_addr +
  873. (used - (void __user *)vq->used),
  874. sizeof vq->used->flags);
  875. if (vq->log_ctx)
  876. eventfd_signal(vq->log_ctx, 1);
  877. }
  878. return 0;
  879. }
  880. static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
  881. {
  882. if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
  883. return -EFAULT;
  884. if (unlikely(vq->log_used)) {
  885. void __user *used;
  886. /* Make sure the event is seen before log. */
  887. smp_wmb();
  888. /* Log avail event write */
  889. used = vhost_avail_event(vq);
  890. log_write(vq->log_base, vq->log_addr +
  891. (used - (void __user *)vq->used),
  892. sizeof *vhost_avail_event(vq));
  893. if (vq->log_ctx)
  894. eventfd_signal(vq->log_ctx, 1);
  895. }
  896. return 0;
  897. }
  898. int vhost_init_used(struct vhost_virtqueue *vq)
  899. {
  900. int r;
  901. if (!vq->private_data)
  902. return 0;
  903. r = vhost_update_used_flags(vq);
  904. if (r)
  905. return r;
  906. vq->signalled_used_valid = false;
  907. return get_user(vq->last_used_idx, &vq->used->idx);
  908. }
  909. static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
  910. struct iovec iov[], int iov_size)
  911. {
  912. const struct vhost_memory_region *reg;
  913. struct vhost_memory *mem;
  914. struct iovec *_iov;
  915. u64 s = 0;
  916. int ret = 0;
  917. rcu_read_lock();
  918. mem = rcu_dereference(dev->memory);
  919. while ((u64)len > s) {
  920. u64 size;
  921. if (unlikely(ret >= iov_size)) {
  922. ret = -ENOBUFS;
  923. break;
  924. }
  925. reg = find_region(mem, addr, len);
  926. if (unlikely(!reg)) {
  927. ret = -EFAULT;
  928. break;
  929. }
  930. _iov = iov + ret;
  931. size = reg->memory_size - addr + reg->guest_phys_addr;
  932. _iov->iov_len = min((u64)len - s, size);
  933. _iov->iov_base = (void __user *)(unsigned long)
  934. (reg->userspace_addr + addr - reg->guest_phys_addr);
  935. s += size;
  936. addr += size;
  937. ++ret;
  938. }
  939. rcu_read_unlock();
  940. return ret;
  941. }
  942. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  943. * function returns the next descriptor in the chain,
  944. * or -1U if we're at the end. */
  945. static unsigned next_desc(struct vring_desc *desc)
  946. {
  947. unsigned int next;
  948. /* If this descriptor says it doesn't chain, we're done. */
  949. if (!(desc->flags & VRING_DESC_F_NEXT))
  950. return -1U;
  951. /* Check they're not leading us off end of descriptors. */
  952. next = desc->next;
  953. /* Make sure compiler knows to grab that: we don't want it changing! */
  954. /* We will use the result as an index in an array, so most
  955. * architectures only need a compiler barrier here. */
  956. read_barrier_depends();
  957. return next;
  958. }
  959. static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  960. struct iovec iov[], unsigned int iov_size,
  961. unsigned int *out_num, unsigned int *in_num,
  962. struct vhost_log *log, unsigned int *log_num,
  963. struct vring_desc *indirect)
  964. {
  965. struct vring_desc desc;
  966. unsigned int i = 0, count, found = 0;
  967. int ret;
  968. /* Sanity check */
  969. if (unlikely(indirect->len % sizeof desc)) {
  970. vq_err(vq, "Invalid length in indirect descriptor: "
  971. "len 0x%llx not multiple of 0x%zx\n",
  972. (unsigned long long)indirect->len,
  973. sizeof desc);
  974. return -EINVAL;
  975. }
  976. ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
  977. UIO_MAXIOV);
  978. if (unlikely(ret < 0)) {
  979. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  980. return ret;
  981. }
  982. /* We will use the result as an address to read from, so most
  983. * architectures only need a compiler barrier here. */
  984. read_barrier_depends();
  985. count = indirect->len / sizeof desc;
  986. /* Buffers are chained via a 16 bit next field, so
  987. * we can have at most 2^16 of these. */
  988. if (unlikely(count > USHRT_MAX + 1)) {
  989. vq_err(vq, "Indirect buffer length too big: %d\n",
  990. indirect->len);
  991. return -E2BIG;
  992. }
  993. do {
  994. unsigned iov_count = *in_num + *out_num;
  995. if (unlikely(++found > count)) {
  996. vq_err(vq, "Loop detected: last one at %u "
  997. "indirect size %u\n",
  998. i, count);
  999. return -EINVAL;
  1000. }
  1001. if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
  1002. vq->indirect, sizeof desc))) {
  1003. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  1004. i, (size_t)indirect->addr + i * sizeof desc);
  1005. return -EINVAL;
  1006. }
  1007. if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
  1008. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  1009. i, (size_t)indirect->addr + i * sizeof desc);
  1010. return -EINVAL;
  1011. }
  1012. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  1013. iov_size - iov_count);
  1014. if (unlikely(ret < 0)) {
  1015. vq_err(vq, "Translation failure %d indirect idx %d\n",
  1016. ret, i);
  1017. return ret;
  1018. }
  1019. /* If this is an input descriptor, increment that count. */
  1020. if (desc.flags & VRING_DESC_F_WRITE) {
  1021. *in_num += ret;
  1022. if (unlikely(log)) {
  1023. log[*log_num].addr = desc.addr;
  1024. log[*log_num].len = desc.len;
  1025. ++*log_num;
  1026. }
  1027. } else {
  1028. /* If it's an output descriptor, they're all supposed
  1029. * to come before any input descriptors. */
  1030. if (unlikely(*in_num)) {
  1031. vq_err(vq, "Indirect descriptor "
  1032. "has out after in: idx %d\n", i);
  1033. return -EINVAL;
  1034. }
  1035. *out_num += ret;
  1036. }
  1037. } while ((i = next_desc(&desc)) != -1);
  1038. return 0;
  1039. }
  1040. /* This looks in the virtqueue and for the first available buffer, and converts
  1041. * it to an iovec for convenient access. Since descriptors consist of some
  1042. * number of output then some number of input descriptors, it's actually two
  1043. * iovecs, but we pack them into one and note how many of each there were.
  1044. *
  1045. * This function returns the descriptor number found, or vq->num (which is
  1046. * never a valid descriptor number) if none was found. A negative code is
  1047. * returned on error. */
  1048. int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  1049. struct iovec iov[], unsigned int iov_size,
  1050. unsigned int *out_num, unsigned int *in_num,
  1051. struct vhost_log *log, unsigned int *log_num)
  1052. {
  1053. struct vring_desc desc;
  1054. unsigned int i, head, found = 0;
  1055. u16 last_avail_idx;
  1056. int ret;
  1057. /* Check it isn't doing very strange things with descriptor numbers. */
  1058. last_avail_idx = vq->last_avail_idx;
  1059. if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
  1060. vq_err(vq, "Failed to access avail idx at %p\n",
  1061. &vq->avail->idx);
  1062. return -EFAULT;
  1063. }
  1064. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  1065. vq_err(vq, "Guest moved used index from %u to %u",
  1066. last_avail_idx, vq->avail_idx);
  1067. return -EFAULT;
  1068. }
  1069. /* If there's nothing new since last we looked, return invalid. */
  1070. if (vq->avail_idx == last_avail_idx)
  1071. return vq->num;
  1072. /* Only get avail ring entries after they have been exposed by guest. */
  1073. smp_rmb();
  1074. /* Grab the next descriptor number they're advertising, and increment
  1075. * the index we've seen. */
  1076. if (unlikely(__get_user(head,
  1077. &vq->avail->ring[last_avail_idx % vq->num]))) {
  1078. vq_err(vq, "Failed to read head: idx %d address %p\n",
  1079. last_avail_idx,
  1080. &vq->avail->ring[last_avail_idx % vq->num]);
  1081. return -EFAULT;
  1082. }
  1083. /* If their number is silly, that's an error. */
  1084. if (unlikely(head >= vq->num)) {
  1085. vq_err(vq, "Guest says index %u > %u is available",
  1086. head, vq->num);
  1087. return -EINVAL;
  1088. }
  1089. /* When we start there are none of either input nor output. */
  1090. *out_num = *in_num = 0;
  1091. if (unlikely(log))
  1092. *log_num = 0;
  1093. i = head;
  1094. do {
  1095. unsigned iov_count = *in_num + *out_num;
  1096. if (unlikely(i >= vq->num)) {
  1097. vq_err(vq, "Desc index is %u > %u, head = %u",
  1098. i, vq->num, head);
  1099. return -EINVAL;
  1100. }
  1101. if (unlikely(++found > vq->num)) {
  1102. vq_err(vq, "Loop detected: last one at %u "
  1103. "vq size %u head %u\n",
  1104. i, vq->num, head);
  1105. return -EINVAL;
  1106. }
  1107. ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
  1108. if (unlikely(ret)) {
  1109. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  1110. i, vq->desc + i);
  1111. return -EFAULT;
  1112. }
  1113. if (desc.flags & VRING_DESC_F_INDIRECT) {
  1114. ret = get_indirect(dev, vq, iov, iov_size,
  1115. out_num, in_num,
  1116. log, log_num, &desc);
  1117. if (unlikely(ret < 0)) {
  1118. vq_err(vq, "Failure detected "
  1119. "in indirect descriptor at idx %d\n", i);
  1120. return ret;
  1121. }
  1122. continue;
  1123. }
  1124. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  1125. iov_size - iov_count);
  1126. if (unlikely(ret < 0)) {
  1127. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  1128. ret, i);
  1129. return ret;
  1130. }
  1131. if (desc.flags & VRING_DESC_F_WRITE) {
  1132. /* If this is an input descriptor,
  1133. * increment that count. */
  1134. *in_num += ret;
  1135. if (unlikely(log)) {
  1136. log[*log_num].addr = desc.addr;
  1137. log[*log_num].len = desc.len;
  1138. ++*log_num;
  1139. }
  1140. } else {
  1141. /* If it's an output descriptor, they're all supposed
  1142. * to come before any input descriptors. */
  1143. if (unlikely(*in_num)) {
  1144. vq_err(vq, "Descriptor has out after in: "
  1145. "idx %d\n", i);
  1146. return -EINVAL;
  1147. }
  1148. *out_num += ret;
  1149. }
  1150. } while ((i = next_desc(&desc)) != -1);
  1151. /* On success, increment avail index. */
  1152. vq->last_avail_idx++;
  1153. /* Assume notifications from guest are disabled at this point,
  1154. * if they aren't we would need to update avail_event index. */
  1155. BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
  1156. return head;
  1157. }
  1158. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1159. void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
  1160. {
  1161. vq->last_avail_idx -= n;
  1162. }
  1163. /* After we've used one of their buffers, we tell them about it. We'll then
  1164. * want to notify the guest, using eventfd. */
  1165. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1166. {
  1167. struct vring_used_elem __user *used;
  1168. /* The virtqueue contains a ring of used buffers. Get a pointer to the
  1169. * next entry in that used ring. */
  1170. used = &vq->used->ring[vq->last_used_idx % vq->num];
  1171. if (__put_user(head, &used->id)) {
  1172. vq_err(vq, "Failed to write used id");
  1173. return -EFAULT;
  1174. }
  1175. if (__put_user(len, &used->len)) {
  1176. vq_err(vq, "Failed to write used len");
  1177. return -EFAULT;
  1178. }
  1179. /* Make sure buffer is written before we update index. */
  1180. smp_wmb();
  1181. if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
  1182. vq_err(vq, "Failed to increment used idx");
  1183. return -EFAULT;
  1184. }
  1185. if (unlikely(vq->log_used)) {
  1186. /* Make sure data is seen before log. */
  1187. smp_wmb();
  1188. /* Log used ring entry write. */
  1189. log_write(vq->log_base,
  1190. vq->log_addr +
  1191. ((void __user *)used - (void __user *)vq->used),
  1192. sizeof *used);
  1193. /* Log used index update. */
  1194. log_write(vq->log_base,
  1195. vq->log_addr + offsetof(struct vring_used, idx),
  1196. sizeof vq->used->idx);
  1197. if (vq->log_ctx)
  1198. eventfd_signal(vq->log_ctx, 1);
  1199. }
  1200. vq->last_used_idx++;
  1201. /* If the driver never bothers to signal in a very long while,
  1202. * used index might wrap around. If that happens, invalidate
  1203. * signalled_used index we stored. TODO: make sure driver
  1204. * signals at least once in 2^16 and remove this. */
  1205. if (unlikely(vq->last_used_idx == vq->signalled_used))
  1206. vq->signalled_used_valid = false;
  1207. return 0;
  1208. }
  1209. static int __vhost_add_used_n(struct vhost_virtqueue *vq,
  1210. struct vring_used_elem *heads,
  1211. unsigned count)
  1212. {
  1213. struct vring_used_elem __user *used;
  1214. u16 old, new;
  1215. int start;
  1216. start = vq->last_used_idx % vq->num;
  1217. used = vq->used->ring + start;
  1218. if (__copy_to_user(used, heads, count * sizeof *used)) {
  1219. vq_err(vq, "Failed to write used");
  1220. return -EFAULT;
  1221. }
  1222. if (unlikely(vq->log_used)) {
  1223. /* Make sure data is seen before log. */
  1224. smp_wmb();
  1225. /* Log used ring entry write. */
  1226. log_write(vq->log_base,
  1227. vq->log_addr +
  1228. ((void __user *)used - (void __user *)vq->used),
  1229. count * sizeof *used);
  1230. }
  1231. old = vq->last_used_idx;
  1232. new = (vq->last_used_idx += count);
  1233. /* If the driver never bothers to signal in a very long while,
  1234. * used index might wrap around. If that happens, invalidate
  1235. * signalled_used index we stored. TODO: make sure driver
  1236. * signals at least once in 2^16 and remove this. */
  1237. if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
  1238. vq->signalled_used_valid = false;
  1239. return 0;
  1240. }
  1241. /* After we've used one of their buffers, we tell them about it. We'll then
  1242. * want to notify the guest, using eventfd. */
  1243. int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
  1244. unsigned count)
  1245. {
  1246. int start, n, r;
  1247. start = vq->last_used_idx % vq->num;
  1248. n = vq->num - start;
  1249. if (n < count) {
  1250. r = __vhost_add_used_n(vq, heads, n);
  1251. if (r < 0)
  1252. return r;
  1253. heads += n;
  1254. count -= n;
  1255. }
  1256. r = __vhost_add_used_n(vq, heads, count);
  1257. /* Make sure buffer is written before we update index. */
  1258. smp_wmb();
  1259. if (put_user(vq->last_used_idx, &vq->used->idx)) {
  1260. vq_err(vq, "Failed to increment used idx");
  1261. return -EFAULT;
  1262. }
  1263. if (unlikely(vq->log_used)) {
  1264. /* Log used index update. */
  1265. log_write(vq->log_base,
  1266. vq->log_addr + offsetof(struct vring_used, idx),
  1267. sizeof vq->used->idx);
  1268. if (vq->log_ctx)
  1269. eventfd_signal(vq->log_ctx, 1);
  1270. }
  1271. return r;
  1272. }
  1273. static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1274. {
  1275. __u16 old, new, event;
  1276. bool v;
  1277. /* Flush out used index updates. This is paired
  1278. * with the barrier that the Guest executes when enabling
  1279. * interrupts. */
  1280. smp_mb();
  1281. if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1282. unlikely(vq->avail_idx == vq->last_avail_idx))
  1283. return true;
  1284. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1285. __u16 flags;
  1286. if (__get_user(flags, &vq->avail->flags)) {
  1287. vq_err(vq, "Failed to get flags");
  1288. return true;
  1289. }
  1290. return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
  1291. }
  1292. old = vq->signalled_used;
  1293. v = vq->signalled_used_valid;
  1294. new = vq->signalled_used = vq->last_used_idx;
  1295. vq->signalled_used_valid = true;
  1296. if (unlikely(!v))
  1297. return true;
  1298. if (get_user(event, vhost_used_event(vq))) {
  1299. vq_err(vq, "Failed to get used event idx");
  1300. return true;
  1301. }
  1302. return vring_need_event(event, new, old);
  1303. }
  1304. /* This actually signals the guest, using eventfd. */
  1305. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1306. {
  1307. /* Signal the Guest tell them we used something up. */
  1308. if (vq->call_ctx && vhost_notify(dev, vq))
  1309. eventfd_signal(vq->call_ctx, 1);
  1310. }
  1311. /* And here's the combo meal deal. Supersize me! */
  1312. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1313. struct vhost_virtqueue *vq,
  1314. unsigned int head, int len)
  1315. {
  1316. vhost_add_used(vq, head, len);
  1317. vhost_signal(dev, vq);
  1318. }
  1319. /* multi-buffer version of vhost_add_used_and_signal */
  1320. void vhost_add_used_and_signal_n(struct vhost_dev *dev,
  1321. struct vhost_virtqueue *vq,
  1322. struct vring_used_elem *heads, unsigned count)
  1323. {
  1324. vhost_add_used_n(vq, heads, count);
  1325. vhost_signal(dev, vq);
  1326. }
  1327. /* OK, now we need to know about added descriptors. */
  1328. bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1329. {
  1330. u16 avail_idx;
  1331. int r;
  1332. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  1333. return false;
  1334. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  1335. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1336. r = vhost_update_used_flags(vq);
  1337. if (r) {
  1338. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1339. &vq->used->flags, r);
  1340. return false;
  1341. }
  1342. } else {
  1343. r = vhost_update_avail_event(vq, vq->avail_idx);
  1344. if (r) {
  1345. vq_err(vq, "Failed to update avail event index at %p: %d\n",
  1346. vhost_avail_event(vq), r);
  1347. return false;
  1348. }
  1349. }
  1350. /* They could have slipped one in as we were doing that: make
  1351. * sure it's written, then check again. */
  1352. smp_mb();
  1353. r = __get_user(avail_idx, &vq->avail->idx);
  1354. if (r) {
  1355. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  1356. &vq->avail->idx, r);
  1357. return false;
  1358. }
  1359. return avail_idx != vq->avail_idx;
  1360. }
  1361. /* We don't need to be notified again. */
  1362. void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1363. {
  1364. int r;
  1365. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  1366. return;
  1367. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  1368. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1369. r = vhost_update_used_flags(vq);
  1370. if (r)
  1371. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1372. &vq->used->flags, r);
  1373. }
  1374. }