vhost.c 58 KB

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