vhost.c 58 KB

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