vhost.c 42 KB

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