vhost.c 40 KB

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