vhost.c 38 KB

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