vhost.c 57 KB

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