vhost.c 34 KB

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