vhost.c 35 KB

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