vhost.c 57 KB

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