vhost.c 35 KB

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