vhost.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  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/vmalloc.h>
  25. #include <linux/kthread.h>
  26. #include <linux/cgroup.h>
  27. #include <linux/module.h>
  28. #include <linux/sort.h>
  29. #include <linux/sched/mm.h>
  30. #include <linux/sched/signal.h>
  31. #include <linux/interval_tree_generic.h>
  32. #include <linux/nospec.h>
  33. #include "vhost.h"
  34. static ushort max_mem_regions = 64;
  35. module_param(max_mem_regions, ushort, 0444);
  36. MODULE_PARM_DESC(max_mem_regions,
  37. "Maximum number of memory regions in memory map. (default: 64)");
  38. static int max_iotlb_entries = 2048;
  39. module_param(max_iotlb_entries, int, 0444);
  40. MODULE_PARM_DESC(max_iotlb_entries,
  41. "Maximum number of iotlb entries. (default: 2048)");
  42. enum {
  43. VHOST_MEMORY_F_LOG = 0x1,
  44. };
  45. #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
  46. #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
  47. INTERVAL_TREE_DEFINE(struct vhost_umem_node,
  48. rb, __u64, __subtree_last,
  49. START, LAST, static inline, vhost_umem_interval_tree);
  50. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  51. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  52. {
  53. vq->user_be = !virtio_legacy_is_little_endian();
  54. }
  55. static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
  56. {
  57. vq->user_be = true;
  58. }
  59. static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
  60. {
  61. vq->user_be = false;
  62. }
  63. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  64. {
  65. struct vhost_vring_state s;
  66. if (vq->private_data)
  67. return -EBUSY;
  68. if (copy_from_user(&s, argp, sizeof(s)))
  69. return -EFAULT;
  70. if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
  71. s.num != VHOST_VRING_BIG_ENDIAN)
  72. return -EINVAL;
  73. if (s.num == VHOST_VRING_BIG_ENDIAN)
  74. vhost_enable_cross_endian_big(vq);
  75. else
  76. vhost_enable_cross_endian_little(vq);
  77. return 0;
  78. }
  79. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  80. int __user *argp)
  81. {
  82. struct vhost_vring_state s = {
  83. .index = idx,
  84. .num = vq->user_be
  85. };
  86. if (copy_to_user(argp, &s, sizeof(s)))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  91. {
  92. /* Note for legacy virtio: user_be is initialized at reset time
  93. * according to the host endianness. If userspace does not set an
  94. * explicit endianness, the default behavior is native endian, as
  95. * expected by legacy virtio.
  96. */
  97. vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
  98. }
  99. #else
  100. static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
  101. {
  102. }
  103. static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
  104. {
  105. return -ENOIOCTLCMD;
  106. }
  107. static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
  108. int __user *argp)
  109. {
  110. return -ENOIOCTLCMD;
  111. }
  112. static void vhost_init_is_le(struct vhost_virtqueue *vq)
  113. {
  114. vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
  115. || virtio_legacy_is_little_endian();
  116. }
  117. #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
  118. static void vhost_reset_is_le(struct vhost_virtqueue *vq)
  119. {
  120. vhost_init_is_le(vq);
  121. }
  122. struct vhost_flush_struct {
  123. struct vhost_work work;
  124. struct completion wait_event;
  125. };
  126. static void vhost_flush_work(struct vhost_work *work)
  127. {
  128. struct vhost_flush_struct *s;
  129. s = container_of(work, struct vhost_flush_struct, work);
  130. complete(&s->wait_event);
  131. }
  132. static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
  133. poll_table *pt)
  134. {
  135. struct vhost_poll *poll;
  136. poll = container_of(pt, struct vhost_poll, table);
  137. poll->wqh = wqh;
  138. add_wait_queue(wqh, &poll->wait);
  139. }
  140. static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
  141. void *key)
  142. {
  143. struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
  144. if (!(key_to_poll(key) & poll->mask))
  145. return 0;
  146. vhost_poll_queue(poll);
  147. return 0;
  148. }
  149. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
  150. {
  151. clear_bit(VHOST_WORK_QUEUED, &work->flags);
  152. work->fn = fn;
  153. }
  154. EXPORT_SYMBOL_GPL(vhost_work_init);
  155. /* Init poll structure */
  156. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  157. __poll_t mask, struct vhost_dev *dev)
  158. {
  159. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  160. init_poll_funcptr(&poll->table, vhost_poll_func);
  161. poll->mask = mask;
  162. poll->dev = dev;
  163. poll->wqh = NULL;
  164. vhost_work_init(&poll->work, fn);
  165. }
  166. EXPORT_SYMBOL_GPL(vhost_poll_init);
  167. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  168. * keep a reference to a file until after vhost_poll_stop is called. */
  169. int vhost_poll_start(struct vhost_poll *poll, struct file *file)
  170. {
  171. __poll_t mask;
  172. int ret = 0;
  173. if (poll->wqh)
  174. return 0;
  175. mask = vfs_poll(file, &poll->table);
  176. if (mask)
  177. vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
  178. if (mask & EPOLLERR) {
  179. vhost_poll_stop(poll);
  180. ret = -EINVAL;
  181. }
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(vhost_poll_start);
  185. /* Stop polling a file. After this function returns, it becomes safe to drop the
  186. * file reference. You must also flush afterwards. */
  187. void vhost_poll_stop(struct vhost_poll *poll)
  188. {
  189. if (poll->wqh) {
  190. remove_wait_queue(poll->wqh, &poll->wait);
  191. poll->wqh = NULL;
  192. }
  193. }
  194. EXPORT_SYMBOL_GPL(vhost_poll_stop);
  195. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
  196. {
  197. struct vhost_flush_struct flush;
  198. if (dev->worker) {
  199. init_completion(&flush.wait_event);
  200. vhost_work_init(&flush.work, vhost_flush_work);
  201. vhost_work_queue(dev, &flush.work);
  202. wait_for_completion(&flush.wait_event);
  203. }
  204. }
  205. EXPORT_SYMBOL_GPL(vhost_work_flush);
  206. /* Flush any work that has been scheduled. When calling this, don't hold any
  207. * locks that are also used by the callback. */
  208. void vhost_poll_flush(struct vhost_poll *poll)
  209. {
  210. vhost_work_flush(poll->dev, &poll->work);
  211. }
  212. EXPORT_SYMBOL_GPL(vhost_poll_flush);
  213. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
  214. {
  215. if (!dev->worker)
  216. return;
  217. if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
  218. /* We can only add the work to the list after we're
  219. * sure it was not in the list.
  220. * test_and_set_bit() implies a memory barrier.
  221. */
  222. llist_add(&work->node, &dev->work_list);
  223. wake_up_process(dev->worker);
  224. }
  225. }
  226. EXPORT_SYMBOL_GPL(vhost_work_queue);
  227. /* A lockless hint for busy polling code to exit the loop */
  228. bool vhost_has_work(struct vhost_dev *dev)
  229. {
  230. return !llist_empty(&dev->work_list);
  231. }
  232. EXPORT_SYMBOL_GPL(vhost_has_work);
  233. void vhost_poll_queue(struct vhost_poll *poll)
  234. {
  235. vhost_work_queue(poll->dev, &poll->work);
  236. }
  237. EXPORT_SYMBOL_GPL(vhost_poll_queue);
  238. static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
  239. {
  240. int j;
  241. for (j = 0; j < VHOST_NUM_ADDRS; j++)
  242. vq->meta_iotlb[j] = NULL;
  243. }
  244. static void vhost_vq_meta_reset(struct vhost_dev *d)
  245. {
  246. int i;
  247. for (i = 0; i < d->nvqs; ++i) {
  248. mutex_lock(&d->vqs[i]->mutex);
  249. __vhost_vq_meta_reset(d->vqs[i]);
  250. mutex_unlock(&d->vqs[i]->mutex);
  251. }
  252. }
  253. static void vhost_vq_reset(struct vhost_dev *dev,
  254. struct vhost_virtqueue *vq)
  255. {
  256. vq->num = 1;
  257. vq->desc = NULL;
  258. vq->avail = NULL;
  259. vq->used = NULL;
  260. vq->last_avail_idx = 0;
  261. vq->avail_idx = 0;
  262. vq->last_used_idx = 0;
  263. vq->signalled_used = 0;
  264. vq->signalled_used_valid = false;
  265. vq->used_flags = 0;
  266. vq->log_used = false;
  267. vq->log_addr = -1ull;
  268. vq->private_data = NULL;
  269. vq->acked_features = 0;
  270. vq->acked_backend_features = 0;
  271. vq->log_base = NULL;
  272. vq->error_ctx = NULL;
  273. vq->kick = NULL;
  274. vq->call_ctx = NULL;
  275. vq->log_ctx = NULL;
  276. vhost_reset_is_le(vq);
  277. vhost_disable_cross_endian(vq);
  278. vq->busyloop_timeout = 0;
  279. vq->umem = NULL;
  280. vq->iotlb = NULL;
  281. __vhost_vq_meta_reset(vq);
  282. }
  283. static int vhost_worker(void *data)
  284. {
  285. struct vhost_dev *dev = data;
  286. struct vhost_work *work, *work_next;
  287. struct llist_node *node;
  288. mm_segment_t oldfs = get_fs();
  289. set_fs(USER_DS);
  290. use_mm(dev->mm);
  291. for (;;) {
  292. /* mb paired w/ kthread_stop */
  293. set_current_state(TASK_INTERRUPTIBLE);
  294. if (kthread_should_stop()) {
  295. __set_current_state(TASK_RUNNING);
  296. break;
  297. }
  298. node = llist_del_all(&dev->work_list);
  299. if (!node)
  300. schedule();
  301. node = llist_reverse_order(node);
  302. /* make sure flag is seen after deletion */
  303. smp_wmb();
  304. llist_for_each_entry_safe(work, work_next, node, node) {
  305. clear_bit(VHOST_WORK_QUEUED, &work->flags);
  306. __set_current_state(TASK_RUNNING);
  307. work->fn(work);
  308. if (need_resched())
  309. schedule();
  310. }
  311. }
  312. unuse_mm(dev->mm);
  313. set_fs(oldfs);
  314. return 0;
  315. }
  316. static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  317. {
  318. kfree(vq->indirect);
  319. vq->indirect = NULL;
  320. kfree(vq->log);
  321. vq->log = NULL;
  322. kfree(vq->heads);
  323. vq->heads = NULL;
  324. }
  325. /* Helper to allocate iovec buffers for all vqs. */
  326. static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  327. {
  328. struct vhost_virtqueue *vq;
  329. int i;
  330. for (i = 0; i < dev->nvqs; ++i) {
  331. vq = dev->vqs[i];
  332. vq->indirect = kmalloc_array(UIO_MAXIOV,
  333. sizeof(*vq->indirect),
  334. GFP_KERNEL);
  335. vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
  336. GFP_KERNEL);
  337. vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
  338. GFP_KERNEL);
  339. if (!vq->indirect || !vq->log || !vq->heads)
  340. goto err_nomem;
  341. }
  342. return 0;
  343. err_nomem:
  344. for (; i >= 0; --i)
  345. vhost_vq_free_iovecs(dev->vqs[i]);
  346. return -ENOMEM;
  347. }
  348. static void vhost_dev_free_iovecs(struct vhost_dev *dev)
  349. {
  350. int i;
  351. for (i = 0; i < dev->nvqs; ++i)
  352. vhost_vq_free_iovecs(dev->vqs[i]);
  353. }
  354. void vhost_dev_init(struct vhost_dev *dev,
  355. struct vhost_virtqueue **vqs, int nvqs)
  356. {
  357. struct vhost_virtqueue *vq;
  358. int i;
  359. dev->vqs = vqs;
  360. dev->nvqs = nvqs;
  361. mutex_init(&dev->mutex);
  362. dev->log_ctx = NULL;
  363. dev->umem = NULL;
  364. dev->iotlb = NULL;
  365. dev->mm = NULL;
  366. dev->worker = NULL;
  367. init_llist_head(&dev->work_list);
  368. init_waitqueue_head(&dev->wait);
  369. INIT_LIST_HEAD(&dev->read_list);
  370. INIT_LIST_HEAD(&dev->pending_list);
  371. spin_lock_init(&dev->iotlb_lock);
  372. for (i = 0; i < dev->nvqs; ++i) {
  373. vq = dev->vqs[i];
  374. vq->log = NULL;
  375. vq->indirect = NULL;
  376. vq->heads = NULL;
  377. vq->dev = dev;
  378. mutex_init(&vq->mutex);
  379. vhost_vq_reset(dev, vq);
  380. if (vq->handle_kick)
  381. vhost_poll_init(&vq->poll, vq->handle_kick,
  382. EPOLLIN, dev);
  383. }
  384. }
  385. EXPORT_SYMBOL_GPL(vhost_dev_init);
  386. /* Caller should have device mutex */
  387. long vhost_dev_check_owner(struct vhost_dev *dev)
  388. {
  389. /* Are you the owner? If not, I don't think you mean to do that */
  390. return dev->mm == current->mm ? 0 : -EPERM;
  391. }
  392. EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
  393. struct vhost_attach_cgroups_struct {
  394. struct vhost_work work;
  395. struct task_struct *owner;
  396. int ret;
  397. };
  398. static void vhost_attach_cgroups_work(struct vhost_work *work)
  399. {
  400. struct vhost_attach_cgroups_struct *s;
  401. s = container_of(work, struct vhost_attach_cgroups_struct, work);
  402. s->ret = cgroup_attach_task_all(s->owner, current);
  403. }
  404. static int vhost_attach_cgroups(struct vhost_dev *dev)
  405. {
  406. struct vhost_attach_cgroups_struct attach;
  407. attach.owner = current;
  408. vhost_work_init(&attach.work, vhost_attach_cgroups_work);
  409. vhost_work_queue(dev, &attach.work);
  410. vhost_work_flush(dev, &attach.work);
  411. return attach.ret;
  412. }
  413. /* Caller should have device mutex */
  414. bool vhost_dev_has_owner(struct vhost_dev *dev)
  415. {
  416. return dev->mm;
  417. }
  418. EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
  419. /* Caller should have device mutex */
  420. long vhost_dev_set_owner(struct vhost_dev *dev)
  421. {
  422. struct task_struct *worker;
  423. int err;
  424. /* Is there an owner already? */
  425. if (vhost_dev_has_owner(dev)) {
  426. err = -EBUSY;
  427. goto err_mm;
  428. }
  429. /* No owner, become one */
  430. dev->mm = get_task_mm(current);
  431. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  432. if (IS_ERR(worker)) {
  433. err = PTR_ERR(worker);
  434. goto err_worker;
  435. }
  436. dev->worker = worker;
  437. wake_up_process(worker); /* avoid contributing to loadavg */
  438. err = vhost_attach_cgroups(dev);
  439. if (err)
  440. goto err_cgroup;
  441. err = vhost_dev_alloc_iovecs(dev);
  442. if (err)
  443. goto err_cgroup;
  444. return 0;
  445. err_cgroup:
  446. kthread_stop(worker);
  447. dev->worker = NULL;
  448. err_worker:
  449. if (dev->mm)
  450. mmput(dev->mm);
  451. dev->mm = NULL;
  452. err_mm:
  453. return err;
  454. }
  455. EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
  456. struct vhost_umem *vhost_dev_reset_owner_prepare(void)
  457. {
  458. return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
  459. }
  460. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
  461. /* Caller should have device mutex */
  462. void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
  463. {
  464. int i;
  465. vhost_dev_cleanup(dev);
  466. /* Restore memory to default empty mapping. */
  467. INIT_LIST_HEAD(&umem->umem_list);
  468. dev->umem = umem;
  469. /* We don't need VQ locks below since vhost_dev_cleanup makes sure
  470. * VQs aren't running.
  471. */
  472. for (i = 0; i < dev->nvqs; ++i)
  473. dev->vqs[i]->umem = umem;
  474. }
  475. EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
  476. void vhost_dev_stop(struct vhost_dev *dev)
  477. {
  478. int i;
  479. for (i = 0; i < dev->nvqs; ++i) {
  480. if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
  481. vhost_poll_stop(&dev->vqs[i]->poll);
  482. vhost_poll_flush(&dev->vqs[i]->poll);
  483. }
  484. }
  485. }
  486. EXPORT_SYMBOL_GPL(vhost_dev_stop);
  487. static void vhost_umem_free(struct vhost_umem *umem,
  488. struct vhost_umem_node *node)
  489. {
  490. vhost_umem_interval_tree_remove(node, &umem->umem_tree);
  491. list_del(&node->link);
  492. kfree(node);
  493. umem->numem--;
  494. }
  495. static void vhost_umem_clean(struct vhost_umem *umem)
  496. {
  497. struct vhost_umem_node *node, *tmp;
  498. if (!umem)
  499. return;
  500. list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
  501. vhost_umem_free(umem, node);
  502. kvfree(umem);
  503. }
  504. static void vhost_clear_msg(struct vhost_dev *dev)
  505. {
  506. struct vhost_msg_node *node, *n;
  507. spin_lock(&dev->iotlb_lock);
  508. list_for_each_entry_safe(node, n, &dev->read_list, node) {
  509. list_del(&node->node);
  510. kfree(node);
  511. }
  512. list_for_each_entry_safe(node, n, &dev->pending_list, node) {
  513. list_del(&node->node);
  514. kfree(node);
  515. }
  516. spin_unlock(&dev->iotlb_lock);
  517. }
  518. void vhost_dev_cleanup(struct vhost_dev *dev)
  519. {
  520. int i;
  521. for (i = 0; i < dev->nvqs; ++i) {
  522. if (dev->vqs[i]->error_ctx)
  523. eventfd_ctx_put(dev->vqs[i]->error_ctx);
  524. if (dev->vqs[i]->kick)
  525. fput(dev->vqs[i]->kick);
  526. if (dev->vqs[i]->call_ctx)
  527. eventfd_ctx_put(dev->vqs[i]->call_ctx);
  528. vhost_vq_reset(dev, dev->vqs[i]);
  529. }
  530. vhost_dev_free_iovecs(dev);
  531. if (dev->log_ctx)
  532. eventfd_ctx_put(dev->log_ctx);
  533. dev->log_ctx = NULL;
  534. /* No one will access memory at this point */
  535. vhost_umem_clean(dev->umem);
  536. dev->umem = NULL;
  537. vhost_umem_clean(dev->iotlb);
  538. dev->iotlb = NULL;
  539. vhost_clear_msg(dev);
  540. wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
  541. WARN_ON(!llist_empty(&dev->work_list));
  542. if (dev->worker) {
  543. kthread_stop(dev->worker);
  544. dev->worker = NULL;
  545. }
  546. if (dev->mm)
  547. mmput(dev->mm);
  548. dev->mm = NULL;
  549. }
  550. EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
  551. static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  552. {
  553. u64 a = addr / VHOST_PAGE_SIZE / 8;
  554. /* Make sure 64 bit math will not overflow. */
  555. if (a > ULONG_MAX - (unsigned long)log_base ||
  556. a + (unsigned long)log_base > ULONG_MAX)
  557. return false;
  558. return access_ok(VERIFY_WRITE, log_base + a,
  559. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  560. }
  561. static bool vhost_overflow(u64 uaddr, u64 size)
  562. {
  563. /* Make sure 64 bit math will not overflow. */
  564. return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
  565. }
  566. /* Caller should have vq mutex and device mutex. */
  567. static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
  568. int log_all)
  569. {
  570. struct vhost_umem_node *node;
  571. if (!umem)
  572. return false;
  573. list_for_each_entry(node, &umem->umem_list, link) {
  574. unsigned long a = node->userspace_addr;
  575. if (vhost_overflow(node->userspace_addr, node->size))
  576. return false;
  577. if (!access_ok(VERIFY_WRITE, (void __user *)a,
  578. node->size))
  579. return false;
  580. else if (log_all && !log_access_ok(log_base,
  581. node->start,
  582. node->size))
  583. return false;
  584. }
  585. return true;
  586. }
  587. static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
  588. u64 addr, unsigned int size,
  589. int type)
  590. {
  591. const struct vhost_umem_node *node = vq->meta_iotlb[type];
  592. if (!node)
  593. return NULL;
  594. return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
  595. }
  596. /* Can we switch to this memory table? */
  597. /* Caller should have device mutex but not vq mutex */
  598. static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
  599. int log_all)
  600. {
  601. int i;
  602. for (i = 0; i < d->nvqs; ++i) {
  603. bool ok;
  604. bool log;
  605. mutex_lock(&d->vqs[i]->mutex);
  606. log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
  607. /* If ring is inactive, will check when it's enabled. */
  608. if (d->vqs[i]->private_data)
  609. ok = vq_memory_access_ok(d->vqs[i]->log_base,
  610. umem, log);
  611. else
  612. ok = true;
  613. mutex_unlock(&d->vqs[i]->mutex);
  614. if (!ok)
  615. return false;
  616. }
  617. return true;
  618. }
  619. static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
  620. struct iovec iov[], int iov_size, int access);
  621. static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
  622. const void *from, unsigned size)
  623. {
  624. int ret;
  625. if (!vq->iotlb)
  626. return __copy_to_user(to, from, size);
  627. else {
  628. /* This function should be called after iotlb
  629. * prefetch, which means we're sure that all vq
  630. * could be access through iotlb. So -EAGAIN should
  631. * not happen in this case.
  632. */
  633. struct iov_iter t;
  634. void __user *uaddr = vhost_vq_meta_fetch(vq,
  635. (u64)(uintptr_t)to, size,
  636. VHOST_ADDR_USED);
  637. if (uaddr)
  638. return __copy_to_user(uaddr, from, size);
  639. ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
  640. ARRAY_SIZE(vq->iotlb_iov),
  641. VHOST_ACCESS_WO);
  642. if (ret < 0)
  643. goto out;
  644. iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
  645. ret = copy_to_iter(from, size, &t);
  646. if (ret == size)
  647. ret = 0;
  648. }
  649. out:
  650. return ret;
  651. }
  652. static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
  653. void __user *from, unsigned size)
  654. {
  655. int ret;
  656. if (!vq->iotlb)
  657. return __copy_from_user(to, from, size);
  658. else {
  659. /* This function should be called after iotlb
  660. * prefetch, which means we're sure that vq
  661. * could be access through iotlb. So -EAGAIN should
  662. * not happen in this case.
  663. */
  664. void __user *uaddr = vhost_vq_meta_fetch(vq,
  665. (u64)(uintptr_t)from, size,
  666. VHOST_ADDR_DESC);
  667. struct iov_iter f;
  668. if (uaddr)
  669. return __copy_from_user(to, uaddr, size);
  670. ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
  671. ARRAY_SIZE(vq->iotlb_iov),
  672. VHOST_ACCESS_RO);
  673. if (ret < 0) {
  674. vq_err(vq, "IOTLB translation failure: uaddr "
  675. "%p size 0x%llx\n", from,
  676. (unsigned long long) size);
  677. goto out;
  678. }
  679. iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
  680. ret = copy_from_iter(to, size, &f);
  681. if (ret == size)
  682. ret = 0;
  683. }
  684. out:
  685. return ret;
  686. }
  687. static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
  688. void __user *addr, unsigned int size,
  689. int type)
  690. {
  691. int ret;
  692. ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
  693. ARRAY_SIZE(vq->iotlb_iov),
  694. VHOST_ACCESS_RO);
  695. if (ret < 0) {
  696. vq_err(vq, "IOTLB translation failure: uaddr "
  697. "%p size 0x%llx\n", addr,
  698. (unsigned long long) size);
  699. return NULL;
  700. }
  701. if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
  702. vq_err(vq, "Non atomic userspace memory access: uaddr "
  703. "%p size 0x%llx\n", addr,
  704. (unsigned long long) size);
  705. return NULL;
  706. }
  707. return vq->iotlb_iov[0].iov_base;
  708. }
  709. /* This function should be called after iotlb
  710. * prefetch, which means we're sure that vq
  711. * could be access through iotlb. So -EAGAIN should
  712. * not happen in this case.
  713. */
  714. static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
  715. void *addr, unsigned int size,
  716. int type)
  717. {
  718. void __user *uaddr = vhost_vq_meta_fetch(vq,
  719. (u64)(uintptr_t)addr, size, type);
  720. if (uaddr)
  721. return uaddr;
  722. return __vhost_get_user_slow(vq, addr, size, type);
  723. }
  724. #define vhost_put_user(vq, x, ptr) \
  725. ({ \
  726. int ret = -EFAULT; \
  727. if (!vq->iotlb) { \
  728. ret = __put_user(x, ptr); \
  729. } else { \
  730. __typeof__(ptr) to = \
  731. (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
  732. sizeof(*ptr), VHOST_ADDR_USED); \
  733. if (to != NULL) \
  734. ret = __put_user(x, to); \
  735. else \
  736. ret = -EFAULT; \
  737. } \
  738. ret; \
  739. })
  740. #define vhost_get_user(vq, x, ptr, type) \
  741. ({ \
  742. int ret; \
  743. if (!vq->iotlb) { \
  744. ret = __get_user(x, ptr); \
  745. } else { \
  746. __typeof__(ptr) from = \
  747. (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
  748. sizeof(*ptr), \
  749. type); \
  750. if (from != NULL) \
  751. ret = __get_user(x, from); \
  752. else \
  753. ret = -EFAULT; \
  754. } \
  755. ret; \
  756. })
  757. #define vhost_get_avail(vq, x, ptr) \
  758. vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
  759. #define vhost_get_used(vq, x, ptr) \
  760. vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
  761. static int vhost_new_umem_range(struct vhost_umem *umem,
  762. u64 start, u64 size, u64 end,
  763. u64 userspace_addr, int perm)
  764. {
  765. struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
  766. if (!node)
  767. return -ENOMEM;
  768. if (umem->numem == max_iotlb_entries) {
  769. tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
  770. vhost_umem_free(umem, tmp);
  771. }
  772. node->start = start;
  773. node->size = size;
  774. node->last = end;
  775. node->userspace_addr = userspace_addr;
  776. node->perm = perm;
  777. INIT_LIST_HEAD(&node->link);
  778. list_add_tail(&node->link, &umem->umem_list);
  779. vhost_umem_interval_tree_insert(node, &umem->umem_tree);
  780. umem->numem++;
  781. return 0;
  782. }
  783. static void vhost_del_umem_range(struct vhost_umem *umem,
  784. u64 start, u64 end)
  785. {
  786. struct vhost_umem_node *node;
  787. while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  788. start, end)))
  789. vhost_umem_free(umem, node);
  790. }
  791. static void vhost_iotlb_notify_vq(struct vhost_dev *d,
  792. struct vhost_iotlb_msg *msg)
  793. {
  794. struct vhost_msg_node *node, *n;
  795. spin_lock(&d->iotlb_lock);
  796. list_for_each_entry_safe(node, n, &d->pending_list, node) {
  797. struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
  798. if (msg->iova <= vq_msg->iova &&
  799. msg->iova + msg->size - 1 >= vq_msg->iova &&
  800. vq_msg->type == VHOST_IOTLB_MISS) {
  801. vhost_poll_queue(&node->vq->poll);
  802. list_del(&node->node);
  803. kfree(node);
  804. }
  805. }
  806. spin_unlock(&d->iotlb_lock);
  807. }
  808. static bool umem_access_ok(u64 uaddr, u64 size, int access)
  809. {
  810. unsigned long a = uaddr;
  811. /* Make sure 64 bit math will not overflow. */
  812. if (vhost_overflow(uaddr, size))
  813. return false;
  814. if ((access & VHOST_ACCESS_RO) &&
  815. !access_ok(VERIFY_READ, (void __user *)a, size))
  816. return false;
  817. if ((access & VHOST_ACCESS_WO) &&
  818. !access_ok(VERIFY_WRITE, (void __user *)a, size))
  819. return false;
  820. return true;
  821. }
  822. static int vhost_process_iotlb_msg(struct vhost_dev *dev,
  823. struct vhost_iotlb_msg *msg)
  824. {
  825. int ret = 0;
  826. mutex_lock(&dev->mutex);
  827. switch (msg->type) {
  828. case VHOST_IOTLB_UPDATE:
  829. if (!dev->iotlb) {
  830. ret = -EFAULT;
  831. break;
  832. }
  833. if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
  834. ret = -EFAULT;
  835. break;
  836. }
  837. vhost_vq_meta_reset(dev);
  838. if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
  839. msg->iova + msg->size - 1,
  840. msg->uaddr, msg->perm)) {
  841. ret = -ENOMEM;
  842. break;
  843. }
  844. vhost_iotlb_notify_vq(dev, msg);
  845. break;
  846. case VHOST_IOTLB_INVALIDATE:
  847. if (!dev->iotlb) {
  848. ret = -EFAULT;
  849. break;
  850. }
  851. vhost_vq_meta_reset(dev);
  852. vhost_del_umem_range(dev->iotlb, msg->iova,
  853. msg->iova + msg->size - 1);
  854. break;
  855. default:
  856. ret = -EINVAL;
  857. break;
  858. }
  859. mutex_unlock(&dev->mutex);
  860. return ret;
  861. }
  862. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  863. struct iov_iter *from)
  864. {
  865. struct vhost_iotlb_msg msg;
  866. size_t offset;
  867. int type, ret;
  868. ret = copy_from_iter(&type, sizeof(type), from);
  869. if (ret != sizeof(type))
  870. goto done;
  871. switch (type) {
  872. case VHOST_IOTLB_MSG:
  873. /* There maybe a hole after type for V1 message type,
  874. * so skip it here.
  875. */
  876. offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
  877. break;
  878. case VHOST_IOTLB_MSG_V2:
  879. offset = sizeof(__u32);
  880. break;
  881. default:
  882. ret = -EINVAL;
  883. goto done;
  884. }
  885. iov_iter_advance(from, offset);
  886. ret = copy_from_iter(&msg, sizeof(msg), from);
  887. if (ret != sizeof(msg))
  888. goto done;
  889. if (vhost_process_iotlb_msg(dev, &msg)) {
  890. ret = -EFAULT;
  891. goto done;
  892. }
  893. ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
  894. sizeof(struct vhost_msg_v2);
  895. done:
  896. return ret;
  897. }
  898. EXPORT_SYMBOL(vhost_chr_write_iter);
  899. __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  900. poll_table *wait)
  901. {
  902. __poll_t mask = 0;
  903. poll_wait(file, &dev->wait, wait);
  904. if (!list_empty(&dev->read_list))
  905. mask |= EPOLLIN | EPOLLRDNORM;
  906. return mask;
  907. }
  908. EXPORT_SYMBOL(vhost_chr_poll);
  909. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  910. int noblock)
  911. {
  912. DEFINE_WAIT(wait);
  913. struct vhost_msg_node *node;
  914. ssize_t ret = 0;
  915. unsigned size = sizeof(struct vhost_msg);
  916. if (iov_iter_count(to) < size)
  917. return 0;
  918. while (1) {
  919. if (!noblock)
  920. prepare_to_wait(&dev->wait, &wait,
  921. TASK_INTERRUPTIBLE);
  922. node = vhost_dequeue_msg(dev, &dev->read_list);
  923. if (node)
  924. break;
  925. if (noblock) {
  926. ret = -EAGAIN;
  927. break;
  928. }
  929. if (signal_pending(current)) {
  930. ret = -ERESTARTSYS;
  931. break;
  932. }
  933. if (!dev->iotlb) {
  934. ret = -EBADFD;
  935. break;
  936. }
  937. schedule();
  938. }
  939. if (!noblock)
  940. finish_wait(&dev->wait, &wait);
  941. if (node) {
  942. struct vhost_iotlb_msg *msg;
  943. void *start = &node->msg;
  944. switch (node->msg.type) {
  945. case VHOST_IOTLB_MSG:
  946. size = sizeof(node->msg);
  947. msg = &node->msg.iotlb;
  948. break;
  949. case VHOST_IOTLB_MSG_V2:
  950. size = sizeof(node->msg_v2);
  951. msg = &node->msg_v2.iotlb;
  952. break;
  953. default:
  954. BUG();
  955. break;
  956. }
  957. ret = copy_to_iter(start, size, to);
  958. if (ret != size || msg->type != VHOST_IOTLB_MISS) {
  959. kfree(node);
  960. return ret;
  961. }
  962. vhost_enqueue_msg(dev, &dev->pending_list, node);
  963. }
  964. return ret;
  965. }
  966. EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
  967. static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
  968. {
  969. struct vhost_dev *dev = vq->dev;
  970. struct vhost_msg_node *node;
  971. struct vhost_iotlb_msg *msg;
  972. bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
  973. node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
  974. if (!node)
  975. return -ENOMEM;
  976. if (v2) {
  977. node->msg_v2.type = VHOST_IOTLB_MSG_V2;
  978. msg = &node->msg_v2.iotlb;
  979. } else {
  980. msg = &node->msg.iotlb;
  981. }
  982. msg->type = VHOST_IOTLB_MISS;
  983. msg->iova = iova;
  984. msg->perm = access;
  985. vhost_enqueue_msg(dev, &dev->read_list, node);
  986. return 0;
  987. }
  988. static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
  989. struct vring_desc __user *desc,
  990. struct vring_avail __user *avail,
  991. struct vring_used __user *used)
  992. {
  993. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  994. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  995. access_ok(VERIFY_READ, avail,
  996. sizeof *avail + num * sizeof *avail->ring + s) &&
  997. access_ok(VERIFY_WRITE, used,
  998. sizeof *used + num * sizeof *used->ring + s);
  999. }
  1000. static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
  1001. const struct vhost_umem_node *node,
  1002. int type)
  1003. {
  1004. int access = (type == VHOST_ADDR_USED) ?
  1005. VHOST_ACCESS_WO : VHOST_ACCESS_RO;
  1006. if (likely(node->perm & access))
  1007. vq->meta_iotlb[type] = node;
  1008. }
  1009. static bool iotlb_access_ok(struct vhost_virtqueue *vq,
  1010. int access, u64 addr, u64 len, int type)
  1011. {
  1012. const struct vhost_umem_node *node;
  1013. struct vhost_umem *umem = vq->iotlb;
  1014. u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
  1015. if (vhost_vq_meta_fetch(vq, addr, len, type))
  1016. return true;
  1017. while (len > s) {
  1018. node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  1019. addr,
  1020. last);
  1021. if (node == NULL || node->start > addr) {
  1022. vhost_iotlb_miss(vq, addr, access);
  1023. return false;
  1024. } else if (!(node->perm & access)) {
  1025. /* Report the possible access violation by
  1026. * request another translation from userspace.
  1027. */
  1028. return false;
  1029. }
  1030. size = node->size - addr + node->start;
  1031. if (orig_addr == addr && size >= len)
  1032. vhost_vq_meta_update(vq, node, type);
  1033. s += size;
  1034. addr += size;
  1035. }
  1036. return true;
  1037. }
  1038. int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
  1039. {
  1040. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  1041. unsigned int num = vq->num;
  1042. if (!vq->iotlb)
  1043. return 1;
  1044. return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
  1045. num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
  1046. iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
  1047. sizeof *vq->avail +
  1048. num * sizeof(*vq->avail->ring) + s,
  1049. VHOST_ADDR_AVAIL) &&
  1050. iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
  1051. sizeof *vq->used +
  1052. num * sizeof(*vq->used->ring) + s,
  1053. VHOST_ADDR_USED);
  1054. }
  1055. EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
  1056. /* Can we log writes? */
  1057. /* Caller should have device mutex but not vq mutex */
  1058. bool vhost_log_access_ok(struct vhost_dev *dev)
  1059. {
  1060. return memory_access_ok(dev, dev->umem, 1);
  1061. }
  1062. EXPORT_SYMBOL_GPL(vhost_log_access_ok);
  1063. /* Verify access for write logging. */
  1064. /* Caller should have vq mutex and device mutex */
  1065. static bool vq_log_access_ok(struct vhost_virtqueue *vq,
  1066. void __user *log_base)
  1067. {
  1068. size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  1069. return vq_memory_access_ok(log_base, vq->umem,
  1070. vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
  1071. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  1072. sizeof *vq->used +
  1073. vq->num * sizeof *vq->used->ring + s));
  1074. }
  1075. /* Can we start vq? */
  1076. /* Caller should have vq mutex and device mutex */
  1077. bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
  1078. {
  1079. if (!vq_log_access_ok(vq, vq->log_base))
  1080. return false;
  1081. /* Access validation occurs at prefetch time with IOTLB */
  1082. if (vq->iotlb)
  1083. return true;
  1084. return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
  1085. }
  1086. EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
  1087. static struct vhost_umem *vhost_umem_alloc(void)
  1088. {
  1089. struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
  1090. if (!umem)
  1091. return NULL;
  1092. umem->umem_tree = RB_ROOT_CACHED;
  1093. umem->numem = 0;
  1094. INIT_LIST_HEAD(&umem->umem_list);
  1095. return umem;
  1096. }
  1097. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  1098. {
  1099. struct vhost_memory mem, *newmem;
  1100. struct vhost_memory_region *region;
  1101. struct vhost_umem *newumem, *oldumem;
  1102. unsigned long size = offsetof(struct vhost_memory, regions);
  1103. int i;
  1104. if (copy_from_user(&mem, m, size))
  1105. return -EFAULT;
  1106. if (mem.padding)
  1107. return -EOPNOTSUPP;
  1108. if (mem.nregions > max_mem_regions)
  1109. return -E2BIG;
  1110. newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
  1111. GFP_KERNEL);
  1112. if (!newmem)
  1113. return -ENOMEM;
  1114. memcpy(newmem, &mem, size);
  1115. if (copy_from_user(newmem->regions, m->regions,
  1116. mem.nregions * sizeof *m->regions)) {
  1117. kvfree(newmem);
  1118. return -EFAULT;
  1119. }
  1120. newumem = vhost_umem_alloc();
  1121. if (!newumem) {
  1122. kvfree(newmem);
  1123. return -ENOMEM;
  1124. }
  1125. for (region = newmem->regions;
  1126. region < newmem->regions + mem.nregions;
  1127. region++) {
  1128. if (vhost_new_umem_range(newumem,
  1129. region->guest_phys_addr,
  1130. region->memory_size,
  1131. region->guest_phys_addr +
  1132. region->memory_size - 1,
  1133. region->userspace_addr,
  1134. VHOST_ACCESS_RW))
  1135. goto err;
  1136. }
  1137. if (!memory_access_ok(d, newumem, 0))
  1138. goto err;
  1139. oldumem = d->umem;
  1140. d->umem = newumem;
  1141. /* All memory accesses are done under some VQ mutex. */
  1142. for (i = 0; i < d->nvqs; ++i) {
  1143. mutex_lock(&d->vqs[i]->mutex);
  1144. d->vqs[i]->umem = newumem;
  1145. mutex_unlock(&d->vqs[i]->mutex);
  1146. }
  1147. kvfree(newmem);
  1148. vhost_umem_clean(oldumem);
  1149. return 0;
  1150. err:
  1151. vhost_umem_clean(newumem);
  1152. kvfree(newmem);
  1153. return -EFAULT;
  1154. }
  1155. long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  1156. {
  1157. struct file *eventfp, *filep = NULL;
  1158. bool pollstart = false, pollstop = false;
  1159. struct eventfd_ctx *ctx = NULL;
  1160. u32 __user *idxp = argp;
  1161. struct vhost_virtqueue *vq;
  1162. struct vhost_vring_state s;
  1163. struct vhost_vring_file f;
  1164. struct vhost_vring_addr a;
  1165. u32 idx;
  1166. long r;
  1167. r = get_user(idx, idxp);
  1168. if (r < 0)
  1169. return r;
  1170. if (idx >= d->nvqs)
  1171. return -ENOBUFS;
  1172. idx = array_index_nospec(idx, d->nvqs);
  1173. vq = d->vqs[idx];
  1174. mutex_lock(&vq->mutex);
  1175. switch (ioctl) {
  1176. case VHOST_SET_VRING_NUM:
  1177. /* Resizing ring with an active backend?
  1178. * You don't want to do that. */
  1179. if (vq->private_data) {
  1180. r = -EBUSY;
  1181. break;
  1182. }
  1183. if (copy_from_user(&s, argp, sizeof s)) {
  1184. r = -EFAULT;
  1185. break;
  1186. }
  1187. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  1188. r = -EINVAL;
  1189. break;
  1190. }
  1191. vq->num = s.num;
  1192. break;
  1193. case VHOST_SET_VRING_BASE:
  1194. /* Moving base with an active backend?
  1195. * You don't want to do that. */
  1196. if (vq->private_data) {
  1197. r = -EBUSY;
  1198. break;
  1199. }
  1200. if (copy_from_user(&s, argp, sizeof s)) {
  1201. r = -EFAULT;
  1202. break;
  1203. }
  1204. if (s.num > 0xffff) {
  1205. r = -EINVAL;
  1206. break;
  1207. }
  1208. vq->last_avail_idx = s.num;
  1209. /* Forget the cached index value. */
  1210. vq->avail_idx = vq->last_avail_idx;
  1211. break;
  1212. case VHOST_GET_VRING_BASE:
  1213. s.index = idx;
  1214. s.num = vq->last_avail_idx;
  1215. if (copy_to_user(argp, &s, sizeof s))
  1216. r = -EFAULT;
  1217. break;
  1218. case VHOST_SET_VRING_ADDR:
  1219. if (copy_from_user(&a, argp, sizeof a)) {
  1220. r = -EFAULT;
  1221. break;
  1222. }
  1223. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  1224. r = -EOPNOTSUPP;
  1225. break;
  1226. }
  1227. /* For 32bit, verify that the top 32bits of the user
  1228. data are set to zero. */
  1229. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  1230. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  1231. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  1232. r = -EFAULT;
  1233. break;
  1234. }
  1235. /* Make sure it's safe to cast pointers to vring types. */
  1236. BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
  1237. BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
  1238. if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
  1239. (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
  1240. (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
  1241. r = -EINVAL;
  1242. break;
  1243. }
  1244. /* We only verify access here if backend is configured.
  1245. * If it is not, we don't as size might not have been setup.
  1246. * We will verify when backend is configured. */
  1247. if (vq->private_data) {
  1248. if (!vq_access_ok(vq, vq->num,
  1249. (void __user *)(unsigned long)a.desc_user_addr,
  1250. (void __user *)(unsigned long)a.avail_user_addr,
  1251. (void __user *)(unsigned long)a.used_user_addr)) {
  1252. r = -EINVAL;
  1253. break;
  1254. }
  1255. /* Also validate log access for used ring if enabled. */
  1256. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  1257. !log_access_ok(vq->log_base, a.log_guest_addr,
  1258. sizeof *vq->used +
  1259. vq->num * sizeof *vq->used->ring)) {
  1260. r = -EINVAL;
  1261. break;
  1262. }
  1263. }
  1264. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  1265. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  1266. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  1267. vq->log_addr = a.log_guest_addr;
  1268. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  1269. break;
  1270. case VHOST_SET_VRING_KICK:
  1271. if (copy_from_user(&f, argp, sizeof f)) {
  1272. r = -EFAULT;
  1273. break;
  1274. }
  1275. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  1276. if (IS_ERR(eventfp)) {
  1277. r = PTR_ERR(eventfp);
  1278. break;
  1279. }
  1280. if (eventfp != vq->kick) {
  1281. pollstop = (filep = vq->kick) != NULL;
  1282. pollstart = (vq->kick = eventfp) != NULL;
  1283. } else
  1284. filep = eventfp;
  1285. break;
  1286. case VHOST_SET_VRING_CALL:
  1287. if (copy_from_user(&f, argp, sizeof f)) {
  1288. r = -EFAULT;
  1289. break;
  1290. }
  1291. ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
  1292. if (IS_ERR(ctx)) {
  1293. r = PTR_ERR(ctx);
  1294. break;
  1295. }
  1296. swap(ctx, vq->call_ctx);
  1297. break;
  1298. case VHOST_SET_VRING_ERR:
  1299. if (copy_from_user(&f, argp, sizeof f)) {
  1300. r = -EFAULT;
  1301. break;
  1302. }
  1303. ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
  1304. if (IS_ERR(ctx)) {
  1305. r = PTR_ERR(ctx);
  1306. break;
  1307. }
  1308. swap(ctx, vq->error_ctx);
  1309. break;
  1310. case VHOST_SET_VRING_ENDIAN:
  1311. r = vhost_set_vring_endian(vq, argp);
  1312. break;
  1313. case VHOST_GET_VRING_ENDIAN:
  1314. r = vhost_get_vring_endian(vq, idx, argp);
  1315. break;
  1316. case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
  1317. if (copy_from_user(&s, argp, sizeof(s))) {
  1318. r = -EFAULT;
  1319. break;
  1320. }
  1321. vq->busyloop_timeout = s.num;
  1322. break;
  1323. case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
  1324. s.index = idx;
  1325. s.num = vq->busyloop_timeout;
  1326. if (copy_to_user(argp, &s, sizeof(s)))
  1327. r = -EFAULT;
  1328. break;
  1329. default:
  1330. r = -ENOIOCTLCMD;
  1331. }
  1332. if (pollstop && vq->handle_kick)
  1333. vhost_poll_stop(&vq->poll);
  1334. if (!IS_ERR_OR_NULL(ctx))
  1335. eventfd_ctx_put(ctx);
  1336. if (filep)
  1337. fput(filep);
  1338. if (pollstart && vq->handle_kick)
  1339. r = vhost_poll_start(&vq->poll, vq->kick);
  1340. mutex_unlock(&vq->mutex);
  1341. if (pollstop && vq->handle_kick)
  1342. vhost_poll_flush(&vq->poll);
  1343. return r;
  1344. }
  1345. EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
  1346. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
  1347. {
  1348. struct vhost_umem *niotlb, *oiotlb;
  1349. int i;
  1350. niotlb = vhost_umem_alloc();
  1351. if (!niotlb)
  1352. return -ENOMEM;
  1353. oiotlb = d->iotlb;
  1354. d->iotlb = niotlb;
  1355. for (i = 0; i < d->nvqs; ++i) {
  1356. struct vhost_virtqueue *vq = d->vqs[i];
  1357. mutex_lock(&vq->mutex);
  1358. vq->iotlb = niotlb;
  1359. __vhost_vq_meta_reset(vq);
  1360. mutex_unlock(&vq->mutex);
  1361. }
  1362. vhost_umem_clean(oiotlb);
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
  1366. /* Caller must have device mutex */
  1367. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  1368. {
  1369. struct eventfd_ctx *ctx;
  1370. u64 p;
  1371. long r;
  1372. int i, fd;
  1373. /* If you are not the owner, you can become one */
  1374. if (ioctl == VHOST_SET_OWNER) {
  1375. r = vhost_dev_set_owner(d);
  1376. goto done;
  1377. }
  1378. /* You must be the owner to do anything else */
  1379. r = vhost_dev_check_owner(d);
  1380. if (r)
  1381. goto done;
  1382. switch (ioctl) {
  1383. case VHOST_SET_MEM_TABLE:
  1384. r = vhost_set_memory(d, argp);
  1385. break;
  1386. case VHOST_SET_LOG_BASE:
  1387. if (copy_from_user(&p, argp, sizeof p)) {
  1388. r = -EFAULT;
  1389. break;
  1390. }
  1391. if ((u64)(unsigned long)p != p) {
  1392. r = -EFAULT;
  1393. break;
  1394. }
  1395. for (i = 0; i < d->nvqs; ++i) {
  1396. struct vhost_virtqueue *vq;
  1397. void __user *base = (void __user *)(unsigned long)p;
  1398. vq = d->vqs[i];
  1399. mutex_lock(&vq->mutex);
  1400. /* If ring is inactive, will check when it's enabled. */
  1401. if (vq->private_data && !vq_log_access_ok(vq, base))
  1402. r = -EFAULT;
  1403. else
  1404. vq->log_base = base;
  1405. mutex_unlock(&vq->mutex);
  1406. }
  1407. break;
  1408. case VHOST_SET_LOG_FD:
  1409. r = get_user(fd, (int __user *)argp);
  1410. if (r < 0)
  1411. break;
  1412. ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
  1413. if (IS_ERR(ctx)) {
  1414. r = PTR_ERR(ctx);
  1415. break;
  1416. }
  1417. swap(ctx, d->log_ctx);
  1418. for (i = 0; i < d->nvqs; ++i) {
  1419. mutex_lock(&d->vqs[i]->mutex);
  1420. d->vqs[i]->log_ctx = d->log_ctx;
  1421. mutex_unlock(&d->vqs[i]->mutex);
  1422. }
  1423. if (ctx)
  1424. eventfd_ctx_put(ctx);
  1425. break;
  1426. default:
  1427. r = -ENOIOCTLCMD;
  1428. break;
  1429. }
  1430. done:
  1431. return r;
  1432. }
  1433. EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
  1434. /* TODO: This is really inefficient. We need something like get_user()
  1435. * (instruction directly accesses the data, with an exception table entry
  1436. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  1437. */
  1438. static int set_bit_to_user(int nr, void __user *addr)
  1439. {
  1440. unsigned long log = (unsigned long)addr;
  1441. struct page *page;
  1442. void *base;
  1443. int bit = nr + (log % PAGE_SIZE) * 8;
  1444. int r;
  1445. r = get_user_pages_fast(log, 1, 1, &page);
  1446. if (r < 0)
  1447. return r;
  1448. BUG_ON(r != 1);
  1449. base = kmap_atomic(page);
  1450. set_bit(bit, base);
  1451. kunmap_atomic(base);
  1452. set_page_dirty_lock(page);
  1453. put_page(page);
  1454. return 0;
  1455. }
  1456. static int log_write(void __user *log_base,
  1457. u64 write_address, u64 write_length)
  1458. {
  1459. u64 write_page = write_address / VHOST_PAGE_SIZE;
  1460. int r;
  1461. if (!write_length)
  1462. return 0;
  1463. write_length += write_address % VHOST_PAGE_SIZE;
  1464. for (;;) {
  1465. u64 base = (u64)(unsigned long)log_base;
  1466. u64 log = base + write_page / 8;
  1467. int bit = write_page % 8;
  1468. if ((u64)(unsigned long)log != log)
  1469. return -EFAULT;
  1470. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  1471. if (r < 0)
  1472. return r;
  1473. if (write_length <= VHOST_PAGE_SIZE)
  1474. break;
  1475. write_length -= VHOST_PAGE_SIZE;
  1476. write_page += 1;
  1477. }
  1478. return r;
  1479. }
  1480. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  1481. unsigned int log_num, u64 len)
  1482. {
  1483. int i, r;
  1484. /* Make sure data written is seen before log. */
  1485. smp_wmb();
  1486. for (i = 0; i < log_num; ++i) {
  1487. u64 l = min(log[i].len, len);
  1488. r = log_write(vq->log_base, log[i].addr, l);
  1489. if (r < 0)
  1490. return r;
  1491. len -= l;
  1492. if (!len) {
  1493. if (vq->log_ctx)
  1494. eventfd_signal(vq->log_ctx, 1);
  1495. return 0;
  1496. }
  1497. }
  1498. /* Length written exceeds what we have stored. This is a bug. */
  1499. BUG();
  1500. return 0;
  1501. }
  1502. EXPORT_SYMBOL_GPL(vhost_log_write);
  1503. static int vhost_update_used_flags(struct vhost_virtqueue *vq)
  1504. {
  1505. void __user *used;
  1506. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
  1507. &vq->used->flags) < 0)
  1508. return -EFAULT;
  1509. if (unlikely(vq->log_used)) {
  1510. /* Make sure the flag is seen before log. */
  1511. smp_wmb();
  1512. /* Log used flag write. */
  1513. used = &vq->used->flags;
  1514. log_write(vq->log_base, vq->log_addr +
  1515. (used - (void __user *)vq->used),
  1516. sizeof vq->used->flags);
  1517. if (vq->log_ctx)
  1518. eventfd_signal(vq->log_ctx, 1);
  1519. }
  1520. return 0;
  1521. }
  1522. static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
  1523. {
  1524. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
  1525. vhost_avail_event(vq)))
  1526. return -EFAULT;
  1527. if (unlikely(vq->log_used)) {
  1528. void __user *used;
  1529. /* Make sure the event is seen before log. */
  1530. smp_wmb();
  1531. /* Log avail event write */
  1532. used = vhost_avail_event(vq);
  1533. log_write(vq->log_base, vq->log_addr +
  1534. (used - (void __user *)vq->used),
  1535. sizeof *vhost_avail_event(vq));
  1536. if (vq->log_ctx)
  1537. eventfd_signal(vq->log_ctx, 1);
  1538. }
  1539. return 0;
  1540. }
  1541. int vhost_vq_init_access(struct vhost_virtqueue *vq)
  1542. {
  1543. __virtio16 last_used_idx;
  1544. int r;
  1545. bool is_le = vq->is_le;
  1546. if (!vq->private_data)
  1547. return 0;
  1548. vhost_init_is_le(vq);
  1549. r = vhost_update_used_flags(vq);
  1550. if (r)
  1551. goto err;
  1552. vq->signalled_used_valid = false;
  1553. if (!vq->iotlb &&
  1554. !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
  1555. r = -EFAULT;
  1556. goto err;
  1557. }
  1558. r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
  1559. if (r) {
  1560. vq_err(vq, "Can't access used idx at %p\n",
  1561. &vq->used->idx);
  1562. goto err;
  1563. }
  1564. vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
  1565. return 0;
  1566. err:
  1567. vq->is_le = is_le;
  1568. return r;
  1569. }
  1570. EXPORT_SYMBOL_GPL(vhost_vq_init_access);
  1571. static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
  1572. struct iovec iov[], int iov_size, int access)
  1573. {
  1574. const struct vhost_umem_node *node;
  1575. struct vhost_dev *dev = vq->dev;
  1576. struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
  1577. struct iovec *_iov;
  1578. u64 s = 0;
  1579. int ret = 0;
  1580. while ((u64)len > s) {
  1581. u64 size;
  1582. if (unlikely(ret >= iov_size)) {
  1583. ret = -ENOBUFS;
  1584. break;
  1585. }
  1586. node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
  1587. addr, addr + len - 1);
  1588. if (node == NULL || node->start > addr) {
  1589. if (umem != dev->iotlb) {
  1590. ret = -EFAULT;
  1591. break;
  1592. }
  1593. ret = -EAGAIN;
  1594. break;
  1595. } else if (!(node->perm & access)) {
  1596. ret = -EPERM;
  1597. break;
  1598. }
  1599. _iov = iov + ret;
  1600. size = node->size - addr + node->start;
  1601. _iov->iov_len = min((u64)len - s, size);
  1602. _iov->iov_base = (void __user *)(unsigned long)
  1603. (node->userspace_addr + addr - node->start);
  1604. s += size;
  1605. addr += size;
  1606. ++ret;
  1607. }
  1608. if (ret == -EAGAIN)
  1609. vhost_iotlb_miss(vq, addr, access);
  1610. return ret;
  1611. }
  1612. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  1613. * function returns the next descriptor in the chain,
  1614. * or -1U if we're at the end. */
  1615. static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
  1616. {
  1617. unsigned int next;
  1618. /* If this descriptor says it doesn't chain, we're done. */
  1619. if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
  1620. return -1U;
  1621. /* Check they're not leading us off end of descriptors. */
  1622. next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
  1623. return next;
  1624. }
  1625. static int get_indirect(struct vhost_virtqueue *vq,
  1626. struct iovec iov[], unsigned int iov_size,
  1627. unsigned int *out_num, unsigned int *in_num,
  1628. struct vhost_log *log, unsigned int *log_num,
  1629. struct vring_desc *indirect)
  1630. {
  1631. struct vring_desc desc;
  1632. unsigned int i = 0, count, found = 0;
  1633. u32 len = vhost32_to_cpu(vq, indirect->len);
  1634. struct iov_iter from;
  1635. int ret, access;
  1636. /* Sanity check */
  1637. if (unlikely(len % sizeof desc)) {
  1638. vq_err(vq, "Invalid length in indirect descriptor: "
  1639. "len 0x%llx not multiple of 0x%zx\n",
  1640. (unsigned long long)len,
  1641. sizeof desc);
  1642. return -EINVAL;
  1643. }
  1644. ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
  1645. UIO_MAXIOV, VHOST_ACCESS_RO);
  1646. if (unlikely(ret < 0)) {
  1647. if (ret != -EAGAIN)
  1648. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  1649. return ret;
  1650. }
  1651. iov_iter_init(&from, READ, vq->indirect, ret, len);
  1652. /* We will use the result as an address to read from, so most
  1653. * architectures only need a compiler barrier here. */
  1654. read_barrier_depends();
  1655. count = len / sizeof desc;
  1656. /* Buffers are chained via a 16 bit next field, so
  1657. * we can have at most 2^16 of these. */
  1658. if (unlikely(count > USHRT_MAX + 1)) {
  1659. vq_err(vq, "Indirect buffer length too big: %d\n",
  1660. indirect->len);
  1661. return -E2BIG;
  1662. }
  1663. do {
  1664. unsigned iov_count = *in_num + *out_num;
  1665. if (unlikely(++found > count)) {
  1666. vq_err(vq, "Loop detected: last one at %u "
  1667. "indirect size %u\n",
  1668. i, count);
  1669. return -EINVAL;
  1670. }
  1671. if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
  1672. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  1673. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1674. return -EINVAL;
  1675. }
  1676. if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
  1677. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  1678. i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
  1679. return -EINVAL;
  1680. }
  1681. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
  1682. access = VHOST_ACCESS_WO;
  1683. else
  1684. access = VHOST_ACCESS_RO;
  1685. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1686. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1687. iov_size - iov_count, access);
  1688. if (unlikely(ret < 0)) {
  1689. if (ret != -EAGAIN)
  1690. vq_err(vq, "Translation failure %d indirect idx %d\n",
  1691. ret, i);
  1692. return ret;
  1693. }
  1694. /* If this is an input descriptor, increment that count. */
  1695. if (access == VHOST_ACCESS_WO) {
  1696. *in_num += ret;
  1697. if (unlikely(log)) {
  1698. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1699. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1700. ++*log_num;
  1701. }
  1702. } else {
  1703. /* If it's an output descriptor, they're all supposed
  1704. * to come before any input descriptors. */
  1705. if (unlikely(*in_num)) {
  1706. vq_err(vq, "Indirect descriptor "
  1707. "has out after in: idx %d\n", i);
  1708. return -EINVAL;
  1709. }
  1710. *out_num += ret;
  1711. }
  1712. } while ((i = next_desc(vq, &desc)) != -1);
  1713. return 0;
  1714. }
  1715. /* This looks in the virtqueue and for the first available buffer, and converts
  1716. * it to an iovec for convenient access. Since descriptors consist of some
  1717. * number of output then some number of input descriptors, it's actually two
  1718. * iovecs, but we pack them into one and note how many of each there were.
  1719. *
  1720. * This function returns the descriptor number found, or vq->num (which is
  1721. * never a valid descriptor number) if none was found. A negative code is
  1722. * returned on error. */
  1723. int vhost_get_vq_desc(struct vhost_virtqueue *vq,
  1724. struct iovec iov[], unsigned int iov_size,
  1725. unsigned int *out_num, unsigned int *in_num,
  1726. struct vhost_log *log, unsigned int *log_num)
  1727. {
  1728. struct vring_desc desc;
  1729. unsigned int i, head, found = 0;
  1730. u16 last_avail_idx;
  1731. __virtio16 avail_idx;
  1732. __virtio16 ring_head;
  1733. int ret, access;
  1734. /* Check it isn't doing very strange things with descriptor numbers. */
  1735. last_avail_idx = vq->last_avail_idx;
  1736. if (vq->avail_idx == vq->last_avail_idx) {
  1737. if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
  1738. vq_err(vq, "Failed to access avail idx at %p\n",
  1739. &vq->avail->idx);
  1740. return -EFAULT;
  1741. }
  1742. vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
  1743. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  1744. vq_err(vq, "Guest moved used index from %u to %u",
  1745. last_avail_idx, vq->avail_idx);
  1746. return -EFAULT;
  1747. }
  1748. /* If there's nothing new since last we looked, return
  1749. * invalid.
  1750. */
  1751. if (vq->avail_idx == last_avail_idx)
  1752. return vq->num;
  1753. /* Only get avail ring entries after they have been
  1754. * exposed by guest.
  1755. */
  1756. smp_rmb();
  1757. }
  1758. /* Grab the next descriptor number they're advertising, and increment
  1759. * the index we've seen. */
  1760. if (unlikely(vhost_get_avail(vq, ring_head,
  1761. &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
  1762. vq_err(vq, "Failed to read head: idx %d address %p\n",
  1763. last_avail_idx,
  1764. &vq->avail->ring[last_avail_idx % vq->num]);
  1765. return -EFAULT;
  1766. }
  1767. head = vhost16_to_cpu(vq, ring_head);
  1768. /* If their number is silly, that's an error. */
  1769. if (unlikely(head >= vq->num)) {
  1770. vq_err(vq, "Guest says index %u > %u is available",
  1771. head, vq->num);
  1772. return -EINVAL;
  1773. }
  1774. /* When we start there are none of either input nor output. */
  1775. *out_num = *in_num = 0;
  1776. if (unlikely(log))
  1777. *log_num = 0;
  1778. i = head;
  1779. do {
  1780. unsigned iov_count = *in_num + *out_num;
  1781. if (unlikely(i >= vq->num)) {
  1782. vq_err(vq, "Desc index is %u > %u, head = %u",
  1783. i, vq->num, head);
  1784. return -EINVAL;
  1785. }
  1786. if (unlikely(++found > vq->num)) {
  1787. vq_err(vq, "Loop detected: last one at %u "
  1788. "vq size %u head %u\n",
  1789. i, vq->num, head);
  1790. return -EINVAL;
  1791. }
  1792. ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
  1793. sizeof desc);
  1794. if (unlikely(ret)) {
  1795. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  1796. i, vq->desc + i);
  1797. return -EFAULT;
  1798. }
  1799. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
  1800. ret = get_indirect(vq, iov, iov_size,
  1801. out_num, in_num,
  1802. log, log_num, &desc);
  1803. if (unlikely(ret < 0)) {
  1804. if (ret != -EAGAIN)
  1805. vq_err(vq, "Failure detected "
  1806. "in indirect descriptor at idx %d\n", i);
  1807. return ret;
  1808. }
  1809. continue;
  1810. }
  1811. if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
  1812. access = VHOST_ACCESS_WO;
  1813. else
  1814. access = VHOST_ACCESS_RO;
  1815. ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
  1816. vhost32_to_cpu(vq, desc.len), iov + iov_count,
  1817. iov_size - iov_count, access);
  1818. if (unlikely(ret < 0)) {
  1819. if (ret != -EAGAIN)
  1820. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  1821. ret, i);
  1822. return ret;
  1823. }
  1824. if (access == VHOST_ACCESS_WO) {
  1825. /* If this is an input descriptor,
  1826. * increment that count. */
  1827. *in_num += ret;
  1828. if (unlikely(log)) {
  1829. log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
  1830. log[*log_num].len = vhost32_to_cpu(vq, desc.len);
  1831. ++*log_num;
  1832. }
  1833. } else {
  1834. /* If it's an output descriptor, they're all supposed
  1835. * to come before any input descriptors. */
  1836. if (unlikely(*in_num)) {
  1837. vq_err(vq, "Descriptor has out after in: "
  1838. "idx %d\n", i);
  1839. return -EINVAL;
  1840. }
  1841. *out_num += ret;
  1842. }
  1843. } while ((i = next_desc(vq, &desc)) != -1);
  1844. /* On success, increment avail index. */
  1845. vq->last_avail_idx++;
  1846. /* Assume notifications from guest are disabled at this point,
  1847. * if they aren't we would need to update avail_event index. */
  1848. BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
  1849. return head;
  1850. }
  1851. EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
  1852. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1853. void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
  1854. {
  1855. vq->last_avail_idx -= n;
  1856. }
  1857. EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
  1858. /* After we've used one of their buffers, we tell them about it. We'll then
  1859. * want to notify the guest, using eventfd. */
  1860. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1861. {
  1862. struct vring_used_elem heads = {
  1863. cpu_to_vhost32(vq, head),
  1864. cpu_to_vhost32(vq, len)
  1865. };
  1866. return vhost_add_used_n(vq, &heads, 1);
  1867. }
  1868. EXPORT_SYMBOL_GPL(vhost_add_used);
  1869. static int __vhost_add_used_n(struct vhost_virtqueue *vq,
  1870. struct vring_used_elem *heads,
  1871. unsigned count)
  1872. {
  1873. struct vring_used_elem __user *used;
  1874. u16 old, new;
  1875. int start;
  1876. start = vq->last_used_idx & (vq->num - 1);
  1877. used = vq->used->ring + start;
  1878. if (count == 1) {
  1879. if (vhost_put_user(vq, heads[0].id, &used->id)) {
  1880. vq_err(vq, "Failed to write used id");
  1881. return -EFAULT;
  1882. }
  1883. if (vhost_put_user(vq, heads[0].len, &used->len)) {
  1884. vq_err(vq, "Failed to write used len");
  1885. return -EFAULT;
  1886. }
  1887. } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
  1888. vq_err(vq, "Failed to write used");
  1889. return -EFAULT;
  1890. }
  1891. if (unlikely(vq->log_used)) {
  1892. /* Make sure data is seen before log. */
  1893. smp_wmb();
  1894. /* Log used ring entry write. */
  1895. log_write(vq->log_base,
  1896. vq->log_addr +
  1897. ((void __user *)used - (void __user *)vq->used),
  1898. count * sizeof *used);
  1899. }
  1900. old = vq->last_used_idx;
  1901. new = (vq->last_used_idx += count);
  1902. /* If the driver never bothers to signal in a very long while,
  1903. * used index might wrap around. If that happens, invalidate
  1904. * signalled_used index we stored. TODO: make sure driver
  1905. * signals at least once in 2^16 and remove this. */
  1906. if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
  1907. vq->signalled_used_valid = false;
  1908. return 0;
  1909. }
  1910. /* After we've used one of their buffers, we tell them about it. We'll then
  1911. * want to notify the guest, using eventfd. */
  1912. int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
  1913. unsigned count)
  1914. {
  1915. int start, n, r;
  1916. start = vq->last_used_idx & (vq->num - 1);
  1917. n = vq->num - start;
  1918. if (n < count) {
  1919. r = __vhost_add_used_n(vq, heads, n);
  1920. if (r < 0)
  1921. return r;
  1922. heads += n;
  1923. count -= n;
  1924. }
  1925. r = __vhost_add_used_n(vq, heads, count);
  1926. /* Make sure buffer is written before we update index. */
  1927. smp_wmb();
  1928. if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
  1929. &vq->used->idx)) {
  1930. vq_err(vq, "Failed to increment used idx");
  1931. return -EFAULT;
  1932. }
  1933. if (unlikely(vq->log_used)) {
  1934. /* Log used index update. */
  1935. log_write(vq->log_base,
  1936. vq->log_addr + offsetof(struct vring_used, idx),
  1937. sizeof vq->used->idx);
  1938. if (vq->log_ctx)
  1939. eventfd_signal(vq->log_ctx, 1);
  1940. }
  1941. return r;
  1942. }
  1943. EXPORT_SYMBOL_GPL(vhost_add_used_n);
  1944. static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1945. {
  1946. __u16 old, new;
  1947. __virtio16 event;
  1948. bool v;
  1949. /* Flush out used index updates. This is paired
  1950. * with the barrier that the Guest executes when enabling
  1951. * interrupts. */
  1952. smp_mb();
  1953. if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1954. unlikely(vq->avail_idx == vq->last_avail_idx))
  1955. return true;
  1956. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  1957. __virtio16 flags;
  1958. if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
  1959. vq_err(vq, "Failed to get flags");
  1960. return true;
  1961. }
  1962. return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
  1963. }
  1964. old = vq->signalled_used;
  1965. v = vq->signalled_used_valid;
  1966. new = vq->signalled_used = vq->last_used_idx;
  1967. vq->signalled_used_valid = true;
  1968. if (unlikely(!v))
  1969. return true;
  1970. if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
  1971. vq_err(vq, "Failed to get used event idx");
  1972. return true;
  1973. }
  1974. return vring_need_event(vhost16_to_cpu(vq, event), new, old);
  1975. }
  1976. /* This actually signals the guest, using eventfd. */
  1977. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1978. {
  1979. /* Signal the Guest tell them we used something up. */
  1980. if (vq->call_ctx && vhost_notify(dev, vq))
  1981. eventfd_signal(vq->call_ctx, 1);
  1982. }
  1983. EXPORT_SYMBOL_GPL(vhost_signal);
  1984. /* And here's the combo meal deal. Supersize me! */
  1985. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1986. struct vhost_virtqueue *vq,
  1987. unsigned int head, int len)
  1988. {
  1989. vhost_add_used(vq, head, len);
  1990. vhost_signal(dev, vq);
  1991. }
  1992. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
  1993. /* multi-buffer version of vhost_add_used_and_signal */
  1994. void vhost_add_used_and_signal_n(struct vhost_dev *dev,
  1995. struct vhost_virtqueue *vq,
  1996. struct vring_used_elem *heads, unsigned count)
  1997. {
  1998. vhost_add_used_n(vq, heads, count);
  1999. vhost_signal(dev, vq);
  2000. }
  2001. EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
  2002. /* return true if we're sure that avaiable ring is empty */
  2003. bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2004. {
  2005. __virtio16 avail_idx;
  2006. int r;
  2007. if (vq->avail_idx != vq->last_avail_idx)
  2008. return false;
  2009. r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
  2010. if (unlikely(r))
  2011. return false;
  2012. vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
  2013. return vq->avail_idx == vq->last_avail_idx;
  2014. }
  2015. EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
  2016. /* OK, now we need to know about added descriptors. */
  2017. bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2018. {
  2019. __virtio16 avail_idx;
  2020. int r;
  2021. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  2022. return false;
  2023. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  2024. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  2025. r = vhost_update_used_flags(vq);
  2026. if (r) {
  2027. vq_err(vq, "Failed to enable notification at %p: %d\n",
  2028. &vq->used->flags, r);
  2029. return false;
  2030. }
  2031. } else {
  2032. r = vhost_update_avail_event(vq, vq->avail_idx);
  2033. if (r) {
  2034. vq_err(vq, "Failed to update avail event index at %p: %d\n",
  2035. vhost_avail_event(vq), r);
  2036. return false;
  2037. }
  2038. }
  2039. /* They could have slipped one in as we were doing that: make
  2040. * sure it's written, then check again. */
  2041. smp_mb();
  2042. r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
  2043. if (r) {
  2044. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  2045. &vq->avail->idx, r);
  2046. return false;
  2047. }
  2048. return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
  2049. }
  2050. EXPORT_SYMBOL_GPL(vhost_enable_notify);
  2051. /* We don't need to be notified again. */
  2052. void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  2053. {
  2054. int r;
  2055. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  2056. return;
  2057. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  2058. if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
  2059. r = vhost_update_used_flags(vq);
  2060. if (r)
  2061. vq_err(vq, "Failed to enable notification at %p: %d\n",
  2062. &vq->used->flags, r);
  2063. }
  2064. }
  2065. EXPORT_SYMBOL_GPL(vhost_disable_notify);
  2066. /* Create a new message. */
  2067. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
  2068. {
  2069. struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
  2070. if (!node)
  2071. return NULL;
  2072. /* Make sure all padding within the structure is initialized. */
  2073. memset(&node->msg, 0, sizeof node->msg);
  2074. node->vq = vq;
  2075. node->msg.type = type;
  2076. return node;
  2077. }
  2078. EXPORT_SYMBOL_GPL(vhost_new_msg);
  2079. void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
  2080. struct vhost_msg_node *node)
  2081. {
  2082. spin_lock(&dev->iotlb_lock);
  2083. list_add_tail(&node->node, head);
  2084. spin_unlock(&dev->iotlb_lock);
  2085. wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
  2086. }
  2087. EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
  2088. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  2089. struct list_head *head)
  2090. {
  2091. struct vhost_msg_node *node = NULL;
  2092. spin_lock(&dev->iotlb_lock);
  2093. if (!list_empty(head)) {
  2094. node = list_first_entry(head, struct vhost_msg_node,
  2095. node);
  2096. list_del(&node->node);
  2097. }
  2098. spin_unlock(&dev->iotlb_lock);
  2099. return node;
  2100. }
  2101. EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
  2102. static int __init vhost_init(void)
  2103. {
  2104. return 0;
  2105. }
  2106. static void __exit vhost_exit(void)
  2107. {
  2108. }
  2109. module_init(vhost_init);
  2110. module_exit(vhost_exit);
  2111. MODULE_VERSION("0.0.1");
  2112. MODULE_LICENSE("GPL v2");
  2113. MODULE_AUTHOR("Michael S. Tsirkin");
  2114. MODULE_DESCRIPTION("Host kernel accelerator for virtio");