vhost.c 40 KB

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