vhost.c 61 KB

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