vhost.c 35 KB

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