vhost.c 34 KB

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