vhost.c 39 KB

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