vhost.c 39 KB

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