vhost.c 35 KB

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