vhost.c 33 KB

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