vhost.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /*
  2. * vhost support
  3. *
  4. * Copyright Red Hat, Inc. 2010
  5. *
  6. * Authors:
  7. * Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qapi/error.h"
  17. #include "hw/virtio/vhost.h"
  18. #include "qemu/atomic.h"
  19. #include "qemu/range.h"
  20. #include "qemu/error-report.h"
  21. #include "qemu/memfd.h"
  22. #include "qemu/log.h"
  23. #include "standard-headers/linux/vhost_types.h"
  24. #include "hw/virtio/virtio-bus.h"
  25. #include "hw/mem/memory-device.h"
  26. #include "migration/blocker.h"
  27. #include "migration/qemu-file-types.h"
  28. #include "sysemu/dma.h"
  29. #include "trace.h"
  30. /* enabled until disconnected backend stabilizes */
  31. #define _VHOST_DEBUG 1
  32. #ifdef _VHOST_DEBUG
  33. #define VHOST_OPS_DEBUG(retval, fmt, ...) \
  34. do { \
  35. error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
  36. strerror(-retval), -retval); \
  37. } while (0)
  38. #else
  39. #define VHOST_OPS_DEBUG(retval, fmt, ...) \
  40. do { } while (0)
  41. #endif
  42. static struct vhost_log *vhost_log[VHOST_BACKEND_TYPE_MAX];
  43. static struct vhost_log *vhost_log_shm[VHOST_BACKEND_TYPE_MAX];
  44. static QLIST_HEAD(, vhost_dev) vhost_log_devs[VHOST_BACKEND_TYPE_MAX];
  45. /* Memslots used by backends that support private memslots (without an fd). */
  46. static unsigned int used_memslots;
  47. /* Memslots used by backends that only support shared memslots (with an fd). */
  48. static unsigned int used_shared_memslots;
  49. static QLIST_HEAD(, vhost_dev) vhost_devices =
  50. QLIST_HEAD_INITIALIZER(vhost_devices);
  51. unsigned int vhost_get_max_memslots(void)
  52. {
  53. unsigned int max = UINT_MAX;
  54. struct vhost_dev *hdev;
  55. QLIST_FOREACH(hdev, &vhost_devices, entry) {
  56. max = MIN(max, hdev->vhost_ops->vhost_backend_memslots_limit(hdev));
  57. }
  58. return max;
  59. }
  60. unsigned int vhost_get_free_memslots(void)
  61. {
  62. unsigned int free = UINT_MAX;
  63. struct vhost_dev *hdev;
  64. QLIST_FOREACH(hdev, &vhost_devices, entry) {
  65. unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
  66. unsigned int cur_free;
  67. if (hdev->vhost_ops->vhost_backend_no_private_memslots &&
  68. hdev->vhost_ops->vhost_backend_no_private_memslots(hdev)) {
  69. cur_free = r - used_shared_memslots;
  70. } else {
  71. cur_free = r - used_memslots;
  72. }
  73. free = MIN(free, cur_free);
  74. }
  75. return free;
  76. }
  77. static void vhost_dev_sync_region(struct vhost_dev *dev,
  78. MemoryRegionSection *section,
  79. uint64_t mfirst, uint64_t mlast,
  80. uint64_t rfirst, uint64_t rlast)
  81. {
  82. vhost_log_chunk_t *dev_log = dev->log->log;
  83. uint64_t start = MAX(mfirst, rfirst);
  84. uint64_t end = MIN(mlast, rlast);
  85. vhost_log_chunk_t *from = dev_log + start / VHOST_LOG_CHUNK;
  86. vhost_log_chunk_t *to = dev_log + end / VHOST_LOG_CHUNK + 1;
  87. uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK);
  88. if (end < start) {
  89. return;
  90. }
  91. assert(end / VHOST_LOG_CHUNK < dev->log_size);
  92. assert(start / VHOST_LOG_CHUNK < dev->log_size);
  93. for (;from < to; ++from) {
  94. vhost_log_chunk_t log;
  95. /* We first check with non-atomic: much cheaper,
  96. * and we expect non-dirty to be the common case. */
  97. if (!*from) {
  98. addr += VHOST_LOG_CHUNK;
  99. continue;
  100. }
  101. /* Data must be read atomically. We don't really need barrier semantics
  102. * but it's easier to use atomic_* than roll our own. */
  103. log = qatomic_xchg(from, 0);
  104. while (log) {
  105. int bit = ctzl(log);
  106. hwaddr page_addr;
  107. hwaddr section_offset;
  108. hwaddr mr_offset;
  109. page_addr = addr + bit * VHOST_LOG_PAGE;
  110. section_offset = page_addr - section->offset_within_address_space;
  111. mr_offset = section_offset + section->offset_within_region;
  112. memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
  113. log &= ~(0x1ull << bit);
  114. }
  115. addr += VHOST_LOG_CHUNK;
  116. }
  117. }
  118. bool vhost_dev_has_iommu(struct vhost_dev *dev)
  119. {
  120. VirtIODevice *vdev = dev->vdev;
  121. /*
  122. * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
  123. * incremental memory mapping API via IOTLB API. For platform that
  124. * does not have IOMMU, there's no need to enable this feature
  125. * which may cause unnecessary IOTLB miss/update transactions.
  126. */
  127. if (vdev) {
  128. return virtio_bus_device_iommu_enabled(vdev) &&
  129. virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
  130. } else {
  131. return false;
  132. }
  133. }
  134. static inline bool vhost_dev_should_log(struct vhost_dev *dev)
  135. {
  136. assert(dev->vhost_ops);
  137. assert(dev->vhost_ops->backend_type > VHOST_BACKEND_TYPE_NONE);
  138. assert(dev->vhost_ops->backend_type < VHOST_BACKEND_TYPE_MAX);
  139. return dev == QLIST_FIRST(&vhost_log_devs[dev->vhost_ops->backend_type]);
  140. }
  141. static inline void vhost_dev_elect_mem_logger(struct vhost_dev *hdev, bool add)
  142. {
  143. VhostBackendType backend_type;
  144. assert(hdev->vhost_ops);
  145. backend_type = hdev->vhost_ops->backend_type;
  146. assert(backend_type > VHOST_BACKEND_TYPE_NONE);
  147. assert(backend_type < VHOST_BACKEND_TYPE_MAX);
  148. if (add && !QLIST_IS_INSERTED(hdev, logdev_entry)) {
  149. if (QLIST_EMPTY(&vhost_log_devs[backend_type])) {
  150. QLIST_INSERT_HEAD(&vhost_log_devs[backend_type],
  151. hdev, logdev_entry);
  152. } else {
  153. /*
  154. * The first vhost_device in the list is selected as the shared
  155. * logger to scan memory sections. Put new entry next to the head
  156. * to avoid inadvertent change to the underlying logger device.
  157. * This is done in order to get better cache locality and to avoid
  158. * performance churn on the hot path for log scanning. Even when
  159. * new devices come and go quickly, it wouldn't end up changing
  160. * the active leading logger device at all.
  161. */
  162. QLIST_INSERT_AFTER(QLIST_FIRST(&vhost_log_devs[backend_type]),
  163. hdev, logdev_entry);
  164. }
  165. } else if (!add && QLIST_IS_INSERTED(hdev, logdev_entry)) {
  166. QLIST_REMOVE(hdev, logdev_entry);
  167. }
  168. }
  169. static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
  170. MemoryRegionSection *section,
  171. hwaddr first,
  172. hwaddr last)
  173. {
  174. int i;
  175. hwaddr start_addr;
  176. hwaddr end_addr;
  177. if (!dev->log_enabled || !dev->started) {
  178. return 0;
  179. }
  180. start_addr = section->offset_within_address_space;
  181. end_addr = range_get_last(start_addr, int128_get64(section->size));
  182. start_addr = MAX(first, start_addr);
  183. end_addr = MIN(last, end_addr);
  184. if (vhost_dev_should_log(dev)) {
  185. for (i = 0; i < dev->mem->nregions; ++i) {
  186. struct vhost_memory_region *reg = dev->mem->regions + i;
  187. vhost_dev_sync_region(dev, section, start_addr, end_addr,
  188. reg->guest_phys_addr,
  189. range_get_last(reg->guest_phys_addr,
  190. reg->memory_size));
  191. }
  192. }
  193. for (i = 0; i < dev->nvqs; ++i) {
  194. struct vhost_virtqueue *vq = dev->vqs + i;
  195. if (!vq->used_phys && !vq->used_size) {
  196. continue;
  197. }
  198. if (vhost_dev_has_iommu(dev)) {
  199. IOMMUTLBEntry iotlb;
  200. hwaddr used_phys = vq->used_phys, used_size = vq->used_size;
  201. hwaddr phys, s, offset;
  202. while (used_size) {
  203. rcu_read_lock();
  204. iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
  205. used_phys,
  206. true,
  207. MEMTXATTRS_UNSPECIFIED);
  208. rcu_read_unlock();
  209. if (!iotlb.target_as) {
  210. qemu_log_mask(LOG_GUEST_ERROR, "translation "
  211. "failure for used_iova %"PRIx64"\n",
  212. used_phys);
  213. return -EINVAL;
  214. }
  215. offset = used_phys & iotlb.addr_mask;
  216. phys = iotlb.translated_addr + offset;
  217. /*
  218. * Distance from start of used ring until last byte of
  219. * IOMMU page.
  220. */
  221. s = iotlb.addr_mask - offset;
  222. /*
  223. * Size of used ring, or of the part of it until end
  224. * of IOMMU page. To avoid zero result, do the adding
  225. * outside of MIN().
  226. */
  227. s = MIN(s, used_size - 1) + 1;
  228. vhost_dev_sync_region(dev, section, start_addr, end_addr, phys,
  229. range_get_last(phys, s));
  230. used_size -= s;
  231. used_phys += s;
  232. }
  233. } else {
  234. vhost_dev_sync_region(dev, section, start_addr,
  235. end_addr, vq->used_phys,
  236. range_get_last(vq->used_phys, vq->used_size));
  237. }
  238. }
  239. return 0;
  240. }
  241. static void vhost_log_sync(MemoryListener *listener,
  242. MemoryRegionSection *section)
  243. {
  244. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  245. memory_listener);
  246. vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
  247. }
  248. static void vhost_log_sync_range(struct vhost_dev *dev,
  249. hwaddr first, hwaddr last)
  250. {
  251. int i;
  252. /* FIXME: this is N^2 in number of sections */
  253. for (i = 0; i < dev->n_mem_sections; ++i) {
  254. MemoryRegionSection *section = &dev->mem_sections[i];
  255. vhost_sync_dirty_bitmap(dev, section, first, last);
  256. }
  257. }
  258. static uint64_t vhost_get_log_size(struct vhost_dev *dev)
  259. {
  260. uint64_t log_size = 0;
  261. int i;
  262. for (i = 0; i < dev->mem->nregions; ++i) {
  263. struct vhost_memory_region *reg = dev->mem->regions + i;
  264. uint64_t last = range_get_last(reg->guest_phys_addr,
  265. reg->memory_size);
  266. log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
  267. }
  268. return log_size;
  269. }
  270. static int vhost_set_backend_type(struct vhost_dev *dev,
  271. VhostBackendType backend_type)
  272. {
  273. int r = 0;
  274. switch (backend_type) {
  275. #ifdef CONFIG_VHOST_KERNEL
  276. case VHOST_BACKEND_TYPE_KERNEL:
  277. dev->vhost_ops = &kernel_ops;
  278. break;
  279. #endif
  280. #ifdef CONFIG_VHOST_USER
  281. case VHOST_BACKEND_TYPE_USER:
  282. dev->vhost_ops = &user_ops;
  283. break;
  284. #endif
  285. #ifdef CONFIG_VHOST_VDPA
  286. case VHOST_BACKEND_TYPE_VDPA:
  287. dev->vhost_ops = &vdpa_ops;
  288. break;
  289. #endif
  290. default:
  291. error_report("Unknown vhost backend type");
  292. r = -1;
  293. }
  294. if (r == 0) {
  295. assert(dev->vhost_ops->backend_type == backend_type);
  296. }
  297. return r;
  298. }
  299. static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)
  300. {
  301. Error *err = NULL;
  302. struct vhost_log *log;
  303. uint64_t logsize = size * sizeof(*(log->log));
  304. int fd = -1;
  305. log = g_new0(struct vhost_log, 1);
  306. if (share) {
  307. log->log = qemu_memfd_alloc("vhost-log", logsize,
  308. F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
  309. &fd, &err);
  310. if (err) {
  311. error_report_err(err);
  312. g_free(log);
  313. return NULL;
  314. }
  315. memset(log->log, 0, logsize);
  316. } else {
  317. log->log = g_malloc0(logsize);
  318. }
  319. log->size = size;
  320. log->refcnt = 1;
  321. log->fd = fd;
  322. return log;
  323. }
  324. static struct vhost_log *vhost_log_get(VhostBackendType backend_type,
  325. uint64_t size, bool share)
  326. {
  327. struct vhost_log *log;
  328. assert(backend_type > VHOST_BACKEND_TYPE_NONE);
  329. assert(backend_type < VHOST_BACKEND_TYPE_MAX);
  330. log = share ? vhost_log_shm[backend_type] : vhost_log[backend_type];
  331. if (!log || log->size != size) {
  332. log = vhost_log_alloc(size, share);
  333. if (share) {
  334. vhost_log_shm[backend_type] = log;
  335. } else {
  336. vhost_log[backend_type] = log;
  337. }
  338. } else {
  339. ++log->refcnt;
  340. }
  341. return log;
  342. }
  343. static void vhost_log_put(struct vhost_dev *dev, bool sync)
  344. {
  345. struct vhost_log *log = dev->log;
  346. VhostBackendType backend_type;
  347. if (!log) {
  348. return;
  349. }
  350. assert(dev->vhost_ops);
  351. backend_type = dev->vhost_ops->backend_type;
  352. if (backend_type == VHOST_BACKEND_TYPE_NONE ||
  353. backend_type >= VHOST_BACKEND_TYPE_MAX) {
  354. return;
  355. }
  356. --log->refcnt;
  357. if (log->refcnt == 0) {
  358. /* Sync only the range covered by the old log */
  359. if (dev->log_size && sync) {
  360. vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
  361. }
  362. if (vhost_log[backend_type] == log) {
  363. g_free(log->log);
  364. vhost_log[backend_type] = NULL;
  365. } else if (vhost_log_shm[backend_type] == log) {
  366. qemu_memfd_free(log->log, log->size * sizeof(*(log->log)),
  367. log->fd);
  368. vhost_log_shm[backend_type] = NULL;
  369. }
  370. g_free(log);
  371. }
  372. vhost_dev_elect_mem_logger(dev, false);
  373. dev->log = NULL;
  374. dev->log_size = 0;
  375. }
  376. static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
  377. {
  378. return dev->vhost_ops->vhost_requires_shm_log &&
  379. dev->vhost_ops->vhost_requires_shm_log(dev);
  380. }
  381. static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
  382. {
  383. struct vhost_log *log = vhost_log_get(dev->vhost_ops->backend_type,
  384. size, vhost_dev_log_is_shared(dev));
  385. uint64_t log_base = (uintptr_t)log->log;
  386. int r;
  387. /* inform backend of log switching, this must be done before
  388. releasing the current log, to ensure no logging is lost */
  389. r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
  390. if (r < 0) {
  391. VHOST_OPS_DEBUG(r, "vhost_set_log_base failed");
  392. }
  393. vhost_log_put(dev, true);
  394. dev->log = log;
  395. dev->log_size = size;
  396. }
  397. static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
  398. hwaddr *plen, bool is_write)
  399. {
  400. if (!vhost_dev_has_iommu(dev)) {
  401. return cpu_physical_memory_map(addr, plen, is_write);
  402. } else {
  403. return (void *)(uintptr_t)addr;
  404. }
  405. }
  406. static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer,
  407. hwaddr len, int is_write,
  408. hwaddr access_len)
  409. {
  410. if (!vhost_dev_has_iommu(dev)) {
  411. cpu_physical_memory_unmap(buffer, len, is_write, access_len);
  412. }
  413. }
  414. static int vhost_verify_ring_part_mapping(void *ring_hva,
  415. uint64_t ring_gpa,
  416. uint64_t ring_size,
  417. void *reg_hva,
  418. uint64_t reg_gpa,
  419. uint64_t reg_size)
  420. {
  421. uint64_t hva_ring_offset;
  422. uint64_t ring_last = range_get_last(ring_gpa, ring_size);
  423. uint64_t reg_last = range_get_last(reg_gpa, reg_size);
  424. if (ring_last < reg_gpa || ring_gpa > reg_last) {
  425. return 0;
  426. }
  427. /* check that whole ring's is mapped */
  428. if (ring_last > reg_last) {
  429. return -ENOMEM;
  430. }
  431. /* check that ring's MemoryRegion wasn't replaced */
  432. hva_ring_offset = ring_gpa - reg_gpa;
  433. if (ring_hva != reg_hva + hva_ring_offset) {
  434. return -EBUSY;
  435. }
  436. return 0;
  437. }
  438. static int vhost_verify_ring_mappings(struct vhost_dev *dev,
  439. void *reg_hva,
  440. uint64_t reg_gpa,
  441. uint64_t reg_size)
  442. {
  443. int i, j;
  444. int r = 0;
  445. const char *part_name[] = {
  446. "descriptor table",
  447. "available ring",
  448. "used ring"
  449. };
  450. if (vhost_dev_has_iommu(dev)) {
  451. return 0;
  452. }
  453. for (i = 0; i < dev->nvqs; ++i) {
  454. struct vhost_virtqueue *vq = dev->vqs + i;
  455. if (vq->desc_phys == 0) {
  456. continue;
  457. }
  458. j = 0;
  459. r = vhost_verify_ring_part_mapping(
  460. vq->desc, vq->desc_phys, vq->desc_size,
  461. reg_hva, reg_gpa, reg_size);
  462. if (r) {
  463. break;
  464. }
  465. j++;
  466. r = vhost_verify_ring_part_mapping(
  467. vq->avail, vq->avail_phys, vq->avail_size,
  468. reg_hva, reg_gpa, reg_size);
  469. if (r) {
  470. break;
  471. }
  472. j++;
  473. r = vhost_verify_ring_part_mapping(
  474. vq->used, vq->used_phys, vq->used_size,
  475. reg_hva, reg_gpa, reg_size);
  476. if (r) {
  477. break;
  478. }
  479. }
  480. if (r == -ENOMEM) {
  481. error_report("Unable to map %s for ring %d", part_name[j], i);
  482. } else if (r == -EBUSY) {
  483. error_report("%s relocated for ring %d", part_name[j], i);
  484. }
  485. return r;
  486. }
  487. /*
  488. * vhost_section: identify sections needed for vhost access
  489. *
  490. * We only care about RAM sections here (where virtqueue and guest
  491. * internals accessed by virtio might live).
  492. */
  493. static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section)
  494. {
  495. MemoryRegion *mr = section->mr;
  496. if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) {
  497. uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr);
  498. uint8_t handled_dirty;
  499. /*
  500. * Kernel based vhost doesn't handle any block which is doing
  501. * dirty-tracking other than migration for which it has
  502. * specific logging support. However for TCG the kernel never
  503. * gets involved anyway so we can also ignore it's
  504. * self-modiying code detection flags. However a vhost-user
  505. * client could still confuse a TCG guest if it re-writes
  506. * executable memory that has already been translated.
  507. */
  508. handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) |
  509. (1 << DIRTY_MEMORY_CODE);
  510. if (dirty_mask & ~handled_dirty) {
  511. trace_vhost_reject_section(mr->name, 1);
  512. return false;
  513. }
  514. /*
  515. * Some backends (like vhost-user) can only handle memory regions
  516. * that have an fd (can be mapped into a different process). Filter
  517. * the ones without an fd out, if requested.
  518. *
  519. * TODO: we might have to limit to MAP_SHARED as well.
  520. */
  521. if (memory_region_get_fd(section->mr) < 0 &&
  522. dev->vhost_ops->vhost_backend_no_private_memslots &&
  523. dev->vhost_ops->vhost_backend_no_private_memslots(dev)) {
  524. trace_vhost_reject_section(mr->name, 2);
  525. return false;
  526. }
  527. trace_vhost_section(mr->name);
  528. return true;
  529. } else {
  530. trace_vhost_reject_section(mr->name, 3);
  531. return false;
  532. }
  533. }
  534. static void vhost_begin(MemoryListener *listener)
  535. {
  536. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  537. memory_listener);
  538. dev->tmp_sections = NULL;
  539. dev->n_tmp_sections = 0;
  540. }
  541. static void vhost_commit(MemoryListener *listener)
  542. {
  543. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  544. memory_listener);
  545. MemoryRegionSection *old_sections;
  546. int n_old_sections;
  547. uint64_t log_size;
  548. size_t regions_size;
  549. int r;
  550. int i;
  551. bool changed = false;
  552. /* Note we can be called before the device is started, but then
  553. * starting the device calls set_mem_table, so we need to have
  554. * built the data structures.
  555. */
  556. old_sections = dev->mem_sections;
  557. n_old_sections = dev->n_mem_sections;
  558. dev->mem_sections = dev->tmp_sections;
  559. dev->n_mem_sections = dev->n_tmp_sections;
  560. if (dev->n_mem_sections != n_old_sections) {
  561. changed = true;
  562. } else {
  563. /* Same size, lets check the contents */
  564. for (i = 0; i < n_old_sections; i++) {
  565. if (!MemoryRegionSection_eq(&old_sections[i],
  566. &dev->mem_sections[i])) {
  567. changed = true;
  568. break;
  569. }
  570. }
  571. }
  572. trace_vhost_commit(dev->started, changed);
  573. if (!changed) {
  574. goto out;
  575. }
  576. /* Rebuild the regions list from the new sections list */
  577. regions_size = offsetof(struct vhost_memory, regions) +
  578. dev->n_mem_sections * sizeof dev->mem->regions[0];
  579. dev->mem = g_realloc(dev->mem, regions_size);
  580. dev->mem->nregions = dev->n_mem_sections;
  581. if (dev->vhost_ops->vhost_backend_no_private_memslots &&
  582. dev->vhost_ops->vhost_backend_no_private_memslots(dev)) {
  583. used_shared_memslots = dev->mem->nregions;
  584. } else {
  585. used_memslots = dev->mem->nregions;
  586. }
  587. for (i = 0; i < dev->n_mem_sections; i++) {
  588. struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
  589. struct MemoryRegionSection *mrs = dev->mem_sections + i;
  590. cur_vmr->guest_phys_addr = mrs->offset_within_address_space;
  591. cur_vmr->memory_size = int128_get64(mrs->size);
  592. cur_vmr->userspace_addr =
  593. (uintptr_t)memory_region_get_ram_ptr(mrs->mr) +
  594. mrs->offset_within_region;
  595. cur_vmr->flags_padding = 0;
  596. }
  597. if (!dev->started) {
  598. goto out;
  599. }
  600. for (i = 0; i < dev->mem->nregions; i++) {
  601. if (vhost_verify_ring_mappings(dev,
  602. (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
  603. dev->mem->regions[i].guest_phys_addr,
  604. dev->mem->regions[i].memory_size)) {
  605. error_report("Verify ring failure on region %d", i);
  606. abort();
  607. }
  608. }
  609. if (!dev->log_enabled) {
  610. r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
  611. if (r < 0) {
  612. VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
  613. }
  614. goto out;
  615. }
  616. log_size = vhost_get_log_size(dev);
  617. /* We allocate an extra 4K bytes to log,
  618. * to reduce the * number of reallocations. */
  619. #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
  620. /* To log more, must increase log size before table update. */
  621. if (dev->log_size < log_size) {
  622. vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
  623. }
  624. r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
  625. if (r < 0) {
  626. VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
  627. }
  628. /* To log less, can only decrease log size after table update. */
  629. if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
  630. vhost_dev_log_resize(dev, log_size);
  631. }
  632. out:
  633. /* Deref the old list of sections, this must happen _after_ the
  634. * vhost_set_mem_table to ensure the client isn't still using the
  635. * section we're about to unref.
  636. */
  637. while (n_old_sections--) {
  638. memory_region_unref(old_sections[n_old_sections].mr);
  639. }
  640. g_free(old_sections);
  641. return;
  642. }
  643. /* Adds the section data to the tmp_section structure.
  644. * It relies on the listener calling us in memory address order
  645. * and for each region (via the _add and _nop methods) to
  646. * join neighbours.
  647. */
  648. static void vhost_region_add_section(struct vhost_dev *dev,
  649. MemoryRegionSection *section)
  650. {
  651. bool need_add = true;
  652. uint64_t mrs_size = int128_get64(section->size);
  653. uint64_t mrs_gpa = section->offset_within_address_space;
  654. uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
  655. section->offset_within_region;
  656. RAMBlock *mrs_rb = section->mr->ram_block;
  657. trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size,
  658. mrs_host);
  659. if (dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER) {
  660. /* Round the section to it's page size */
  661. /* First align the start down to a page boundary */
  662. size_t mrs_page = qemu_ram_pagesize(mrs_rb);
  663. uint64_t alignage = mrs_host & (mrs_page - 1);
  664. if (alignage) {
  665. mrs_host -= alignage;
  666. mrs_size += alignage;
  667. mrs_gpa -= alignage;
  668. }
  669. /* Now align the size up to a page boundary */
  670. alignage = mrs_size & (mrs_page - 1);
  671. if (alignage) {
  672. mrs_size += mrs_page - alignage;
  673. }
  674. trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa,
  675. mrs_size, mrs_host);
  676. }
  677. if (dev->n_tmp_sections && !section->unmergeable) {
  678. /* Since we already have at least one section, lets see if
  679. * this extends it; since we're scanning in order, we only
  680. * have to look at the last one, and the FlatView that calls
  681. * us shouldn't have overlaps.
  682. */
  683. MemoryRegionSection *prev_sec = dev->tmp_sections +
  684. (dev->n_tmp_sections - 1);
  685. uint64_t prev_gpa_start = prev_sec->offset_within_address_space;
  686. uint64_t prev_size = int128_get64(prev_sec->size);
  687. uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size);
  688. uint64_t prev_host_start =
  689. (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) +
  690. prev_sec->offset_within_region;
  691. uint64_t prev_host_end = range_get_last(prev_host_start, prev_size);
  692. if (mrs_gpa <= (prev_gpa_end + 1)) {
  693. /* OK, looks like overlapping/intersecting - it's possible that
  694. * the rounding to page sizes has made them overlap, but they should
  695. * match up in the same RAMBlock if they do.
  696. */
  697. if (mrs_gpa < prev_gpa_start) {
  698. error_report("%s:Section '%s' rounded to %"PRIx64
  699. " prior to previous '%s' %"PRIx64,
  700. __func__, section->mr->name, mrs_gpa,
  701. prev_sec->mr->name, prev_gpa_start);
  702. /* A way to cleanly fail here would be better */
  703. return;
  704. }
  705. /* Offset from the start of the previous GPA to this GPA */
  706. size_t offset = mrs_gpa - prev_gpa_start;
  707. if (prev_host_start + offset == mrs_host &&
  708. section->mr == prev_sec->mr && !prev_sec->unmergeable) {
  709. uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size);
  710. need_add = false;
  711. prev_sec->offset_within_address_space =
  712. MIN(prev_gpa_start, mrs_gpa);
  713. prev_sec->offset_within_region =
  714. MIN(prev_host_start, mrs_host) -
  715. (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr);
  716. prev_sec->size = int128_make64(max_end - MIN(prev_host_start,
  717. mrs_host));
  718. trace_vhost_region_add_section_merge(section->mr->name,
  719. int128_get64(prev_sec->size),
  720. prev_sec->offset_within_address_space,
  721. prev_sec->offset_within_region);
  722. } else {
  723. /* adjoining regions are fine, but overlapping ones with
  724. * different blocks/offsets shouldn't happen
  725. */
  726. if (mrs_gpa != prev_gpa_end + 1) {
  727. error_report("%s: Overlapping but not coherent sections "
  728. "at %"PRIx64,
  729. __func__, mrs_gpa);
  730. return;
  731. }
  732. }
  733. }
  734. }
  735. if (need_add) {
  736. ++dev->n_tmp_sections;
  737. dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections,
  738. dev->n_tmp_sections);
  739. dev->tmp_sections[dev->n_tmp_sections - 1] = *section;
  740. /* The flatview isn't stable and we don't use it, making it NULL
  741. * means we can memcmp the list.
  742. */
  743. dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL;
  744. memory_region_ref(section->mr);
  745. }
  746. }
  747. /* Used for both add and nop callbacks */
  748. static void vhost_region_addnop(MemoryListener *listener,
  749. MemoryRegionSection *section)
  750. {
  751. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  752. memory_listener);
  753. if (!vhost_section(dev, section)) {
  754. return;
  755. }
  756. vhost_region_add_section(dev, section);
  757. }
  758. static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
  759. {
  760. struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
  761. struct vhost_dev *hdev = iommu->hdev;
  762. hwaddr iova = iotlb->iova + iommu->iommu_offset;
  763. if (vhost_backend_invalidate_device_iotlb(hdev, iova,
  764. iotlb->addr_mask + 1)) {
  765. error_report("Fail to invalidate device iotlb");
  766. }
  767. }
  768. static void vhost_iommu_region_add(MemoryListener *listener,
  769. MemoryRegionSection *section)
  770. {
  771. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  772. iommu_listener);
  773. struct vhost_iommu *iommu;
  774. Int128 end;
  775. int iommu_idx;
  776. IOMMUMemoryRegion *iommu_mr;
  777. if (!memory_region_is_iommu(section->mr)) {
  778. return;
  779. }
  780. iommu_mr = IOMMU_MEMORY_REGION(section->mr);
  781. iommu = g_malloc0(sizeof(*iommu));
  782. end = int128_add(int128_make64(section->offset_within_region),
  783. section->size);
  784. end = int128_sub(end, int128_one());
  785. iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
  786. MEMTXATTRS_UNSPECIFIED);
  787. iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify,
  788. dev->vdev->device_iotlb_enabled ?
  789. IOMMU_NOTIFIER_DEVIOTLB_UNMAP :
  790. IOMMU_NOTIFIER_UNMAP,
  791. section->offset_within_region,
  792. int128_get64(end),
  793. iommu_idx);
  794. iommu->mr = section->mr;
  795. iommu->iommu_offset = section->offset_within_address_space -
  796. section->offset_within_region;
  797. iommu->hdev = dev;
  798. memory_region_register_iommu_notifier(section->mr, &iommu->n,
  799. &error_fatal);
  800. QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
  801. /* TODO: can replay help performance here? */
  802. }
  803. static void vhost_iommu_region_del(MemoryListener *listener,
  804. MemoryRegionSection *section)
  805. {
  806. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  807. iommu_listener);
  808. struct vhost_iommu *iommu;
  809. if (!memory_region_is_iommu(section->mr)) {
  810. return;
  811. }
  812. QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
  813. if (iommu->mr == section->mr &&
  814. iommu->n.start == section->offset_within_region) {
  815. memory_region_unregister_iommu_notifier(iommu->mr,
  816. &iommu->n);
  817. QLIST_REMOVE(iommu, iommu_next);
  818. g_free(iommu);
  819. break;
  820. }
  821. }
  822. }
  823. void vhost_toggle_device_iotlb(VirtIODevice *vdev)
  824. {
  825. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
  826. struct vhost_dev *dev;
  827. struct vhost_iommu *iommu;
  828. if (vdev->vhost_started) {
  829. dev = vdc->get_vhost(vdev);
  830. } else {
  831. return;
  832. }
  833. QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
  834. memory_region_unregister_iommu_notifier(iommu->mr, &iommu->n);
  835. iommu->n.notifier_flags = vdev->device_iotlb_enabled ?
  836. IOMMU_NOTIFIER_DEVIOTLB_UNMAP : IOMMU_NOTIFIER_UNMAP;
  837. memory_region_register_iommu_notifier(iommu->mr, &iommu->n,
  838. &error_fatal);
  839. }
  840. }
  841. static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
  842. struct vhost_virtqueue *vq,
  843. unsigned idx, bool enable_log)
  844. {
  845. struct vhost_vring_addr addr;
  846. int r;
  847. memset(&addr, 0, sizeof(struct vhost_vring_addr));
  848. if (dev->vhost_ops->vhost_vq_get_addr) {
  849. r = dev->vhost_ops->vhost_vq_get_addr(dev, &addr, vq);
  850. if (r < 0) {
  851. VHOST_OPS_DEBUG(r, "vhost_vq_get_addr failed");
  852. return r;
  853. }
  854. } else {
  855. addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc;
  856. addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail;
  857. addr.used_user_addr = (uint64_t)(unsigned long)vq->used;
  858. }
  859. addr.index = idx;
  860. addr.log_guest_addr = vq->used_phys;
  861. addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
  862. r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);
  863. if (r < 0) {
  864. VHOST_OPS_DEBUG(r, "vhost_set_vring_addr failed");
  865. }
  866. return r;
  867. }
  868. static int vhost_dev_set_features(struct vhost_dev *dev,
  869. bool enable_log)
  870. {
  871. uint64_t features = dev->acked_features;
  872. int r;
  873. if (enable_log) {
  874. features |= 0x1ULL << VHOST_F_LOG_ALL;
  875. }
  876. if (!vhost_dev_has_iommu(dev)) {
  877. features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
  878. }
  879. if (dev->vhost_ops->vhost_force_iommu) {
  880. if (dev->vhost_ops->vhost_force_iommu(dev) == true) {
  881. features |= 0x1ULL << VIRTIO_F_IOMMU_PLATFORM;
  882. }
  883. }
  884. r = dev->vhost_ops->vhost_set_features(dev, features);
  885. if (r < 0) {
  886. VHOST_OPS_DEBUG(r, "vhost_set_features failed");
  887. goto out;
  888. }
  889. if (dev->vhost_ops->vhost_set_backend_cap) {
  890. r = dev->vhost_ops->vhost_set_backend_cap(dev);
  891. if (r < 0) {
  892. VHOST_OPS_DEBUG(r, "vhost_set_backend_cap failed");
  893. goto out;
  894. }
  895. }
  896. out:
  897. return r;
  898. }
  899. static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
  900. {
  901. int r, i, idx;
  902. hwaddr addr;
  903. r = vhost_dev_set_features(dev, enable_log);
  904. if (r < 0) {
  905. goto err_features;
  906. }
  907. for (i = 0; i < dev->nvqs; ++i) {
  908. idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
  909. addr = virtio_queue_get_desc_addr(dev->vdev, idx);
  910. if (!addr) {
  911. /*
  912. * The queue might not be ready for start. If this
  913. * is the case there is no reason to continue the process.
  914. * The similar logic is used by the vhost_virtqueue_start()
  915. * routine.
  916. */
  917. continue;
  918. }
  919. r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
  920. enable_log);
  921. if (r < 0) {
  922. goto err_vq;
  923. }
  924. }
  925. /*
  926. * At log start we select our vhost_device logger that will scan the
  927. * memory sections and skip for the others. This is possible because
  928. * the log is shared amongst all vhost devices for a given type of
  929. * backend.
  930. */
  931. vhost_dev_elect_mem_logger(dev, enable_log);
  932. return 0;
  933. err_vq:
  934. for (; i >= 0; --i) {
  935. idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
  936. addr = virtio_queue_get_desc_addr(dev->vdev, idx);
  937. if (!addr) {
  938. continue;
  939. }
  940. vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
  941. dev->log_enabled);
  942. }
  943. vhost_dev_set_features(dev, dev->log_enabled);
  944. err_features:
  945. return r;
  946. }
  947. static int vhost_migration_log(MemoryListener *listener, bool enable)
  948. {
  949. struct vhost_dev *dev = container_of(listener, struct vhost_dev,
  950. memory_listener);
  951. int r;
  952. if (enable == dev->log_enabled) {
  953. return 0;
  954. }
  955. if (!dev->started) {
  956. dev->log_enabled = enable;
  957. return 0;
  958. }
  959. r = 0;
  960. if (!enable) {
  961. r = vhost_dev_set_log(dev, false);
  962. if (r < 0) {
  963. goto check_dev_state;
  964. }
  965. vhost_log_put(dev, false);
  966. } else {
  967. vhost_dev_log_resize(dev, vhost_get_log_size(dev));
  968. r = vhost_dev_set_log(dev, true);
  969. if (r < 0) {
  970. goto check_dev_state;
  971. }
  972. }
  973. check_dev_state:
  974. dev->log_enabled = enable;
  975. /*
  976. * vhost-user-* devices could change their state during log
  977. * initialization due to disconnect. So check dev state after
  978. * vhost communication.
  979. */
  980. if (!dev->started) {
  981. /*
  982. * Since device is in the stopped state, it is okay for
  983. * migration. Return success.
  984. */
  985. r = 0;
  986. }
  987. if (r) {
  988. /* An error occurred. */
  989. dev->log_enabled = false;
  990. }
  991. return r;
  992. }
  993. static bool vhost_log_global_start(MemoryListener *listener, Error **errp)
  994. {
  995. int r;
  996. r = vhost_migration_log(listener, true);
  997. if (r < 0) {
  998. abort();
  999. }
  1000. return true;
  1001. }
  1002. static void vhost_log_global_stop(MemoryListener *listener)
  1003. {
  1004. int r;
  1005. r = vhost_migration_log(listener, false);
  1006. if (r < 0) {
  1007. abort();
  1008. }
  1009. }
  1010. static void vhost_log_start(MemoryListener *listener,
  1011. MemoryRegionSection *section,
  1012. int old, int new)
  1013. {
  1014. /* FIXME: implement */
  1015. }
  1016. static void vhost_log_stop(MemoryListener *listener,
  1017. MemoryRegionSection *section,
  1018. int old, int new)
  1019. {
  1020. /* FIXME: implement */
  1021. }
  1022. /* The vhost driver natively knows how to handle the vrings of non
  1023. * cross-endian legacy devices and modern devices. Only legacy devices
  1024. * exposed to a bi-endian guest may require the vhost driver to use a
  1025. * specific endianness.
  1026. */
  1027. static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
  1028. {
  1029. if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1030. return false;
  1031. }
  1032. #if HOST_BIG_ENDIAN
  1033. return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
  1034. #else
  1035. return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
  1036. #endif
  1037. }
  1038. static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
  1039. bool is_big_endian,
  1040. int vhost_vq_index)
  1041. {
  1042. int r;
  1043. struct vhost_vring_state s = {
  1044. .index = vhost_vq_index,
  1045. .num = is_big_endian
  1046. };
  1047. r = dev->vhost_ops->vhost_set_vring_endian(dev, &s);
  1048. if (r < 0) {
  1049. VHOST_OPS_DEBUG(r, "vhost_set_vring_endian failed");
  1050. }
  1051. return r;
  1052. }
  1053. static int vhost_memory_region_lookup(struct vhost_dev *hdev,
  1054. uint64_t gpa, uint64_t *uaddr,
  1055. uint64_t *len)
  1056. {
  1057. int i;
  1058. for (i = 0; i < hdev->mem->nregions; i++) {
  1059. struct vhost_memory_region *reg = hdev->mem->regions + i;
  1060. if (gpa >= reg->guest_phys_addr &&
  1061. reg->guest_phys_addr + reg->memory_size > gpa) {
  1062. *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr;
  1063. *len = reg->guest_phys_addr + reg->memory_size - gpa;
  1064. return 0;
  1065. }
  1066. }
  1067. return -EFAULT;
  1068. }
  1069. int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
  1070. {
  1071. IOMMUTLBEntry iotlb;
  1072. uint64_t uaddr, len;
  1073. int ret = -EFAULT;
  1074. RCU_READ_LOCK_GUARD();
  1075. trace_vhost_iotlb_miss(dev, 1);
  1076. iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
  1077. iova, write,
  1078. MEMTXATTRS_UNSPECIFIED);
  1079. if (iotlb.target_as != NULL) {
  1080. ret = vhost_memory_region_lookup(dev, iotlb.translated_addr,
  1081. &uaddr, &len);
  1082. if (ret) {
  1083. trace_vhost_iotlb_miss(dev, 3);
  1084. error_report("Fail to lookup the translated address "
  1085. "%"PRIx64, iotlb.translated_addr);
  1086. goto out;
  1087. }
  1088. len = MIN(iotlb.addr_mask + 1, len);
  1089. iova = iova & ~iotlb.addr_mask;
  1090. ret = vhost_backend_update_device_iotlb(dev, iova, uaddr,
  1091. len, iotlb.perm);
  1092. if (ret) {
  1093. trace_vhost_iotlb_miss(dev, 4);
  1094. error_report("Fail to update device iotlb");
  1095. goto out;
  1096. }
  1097. }
  1098. trace_vhost_iotlb_miss(dev, 2);
  1099. out:
  1100. return ret;
  1101. }
  1102. int vhost_virtqueue_start(struct vhost_dev *dev,
  1103. struct VirtIODevice *vdev,
  1104. struct vhost_virtqueue *vq,
  1105. unsigned idx)
  1106. {
  1107. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  1108. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  1109. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  1110. hwaddr s, l, a;
  1111. int r;
  1112. int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
  1113. struct vhost_vring_file file = {
  1114. .index = vhost_vq_index
  1115. };
  1116. struct vhost_vring_state state = {
  1117. .index = vhost_vq_index
  1118. };
  1119. struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
  1120. a = virtio_queue_get_desc_addr(vdev, idx);
  1121. if (a == 0) {
  1122. /* Queue might not be ready for start */
  1123. return 0;
  1124. }
  1125. vq->num = state.num = virtio_queue_get_num(vdev, idx);
  1126. r = dev->vhost_ops->vhost_set_vring_num(dev, &state);
  1127. if (r) {
  1128. VHOST_OPS_DEBUG(r, "vhost_set_vring_num failed");
  1129. return r;
  1130. }
  1131. state.num = virtio_queue_get_last_avail_idx(vdev, idx);
  1132. r = dev->vhost_ops->vhost_set_vring_base(dev, &state);
  1133. if (r) {
  1134. VHOST_OPS_DEBUG(r, "vhost_set_vring_base failed");
  1135. return r;
  1136. }
  1137. if (vhost_needs_vring_endian(vdev)) {
  1138. r = vhost_virtqueue_set_vring_endian_legacy(dev,
  1139. virtio_is_big_endian(vdev),
  1140. vhost_vq_index);
  1141. if (r) {
  1142. return r;
  1143. }
  1144. }
  1145. vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
  1146. vq->desc_phys = a;
  1147. vq->desc = vhost_memory_map(dev, a, &l, false);
  1148. if (!vq->desc || l != s) {
  1149. r = -ENOMEM;
  1150. goto fail_alloc_desc;
  1151. }
  1152. vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
  1153. vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx);
  1154. vq->avail = vhost_memory_map(dev, a, &l, false);
  1155. if (!vq->avail || l != s) {
  1156. r = -ENOMEM;
  1157. goto fail_alloc_avail;
  1158. }
  1159. vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
  1160. vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
  1161. vq->used = vhost_memory_map(dev, a, &l, true);
  1162. if (!vq->used || l != s) {
  1163. r = -ENOMEM;
  1164. goto fail_alloc_used;
  1165. }
  1166. r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
  1167. if (r < 0) {
  1168. goto fail_alloc;
  1169. }
  1170. file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
  1171. r = dev->vhost_ops->vhost_set_vring_kick(dev, &file);
  1172. if (r) {
  1173. VHOST_OPS_DEBUG(r, "vhost_set_vring_kick failed");
  1174. goto fail_kick;
  1175. }
  1176. /* Clear and discard previous events if any. */
  1177. event_notifier_test_and_clear(&vq->masked_notifier);
  1178. /* Init vring in unmasked state, unless guest_notifier_mask
  1179. * will do it later.
  1180. */
  1181. if (!vdev->use_guest_notifier_mask) {
  1182. /* TODO: check and handle errors. */
  1183. vhost_virtqueue_mask(dev, vdev, idx, false);
  1184. }
  1185. if (k->query_guest_notifiers &&
  1186. k->query_guest_notifiers(qbus->parent) &&
  1187. virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
  1188. file.fd = -1;
  1189. r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
  1190. if (r) {
  1191. goto fail_vector;
  1192. }
  1193. }
  1194. return 0;
  1195. fail_vector:
  1196. fail_kick:
  1197. fail_alloc:
  1198. vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
  1199. 0, 0);
  1200. fail_alloc_used:
  1201. vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
  1202. 0, 0);
  1203. fail_alloc_avail:
  1204. vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
  1205. 0, 0);
  1206. fail_alloc_desc:
  1207. return r;
  1208. }
  1209. void vhost_virtqueue_stop(struct vhost_dev *dev,
  1210. struct VirtIODevice *vdev,
  1211. struct vhost_virtqueue *vq,
  1212. unsigned idx)
  1213. {
  1214. int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
  1215. struct vhost_vring_state state = {
  1216. .index = vhost_vq_index,
  1217. };
  1218. int r;
  1219. if (virtio_queue_get_desc_addr(vdev, idx) == 0) {
  1220. /* Don't stop the virtqueue which might have not been started */
  1221. return;
  1222. }
  1223. r = dev->vhost_ops->vhost_get_vring_base(dev, &state);
  1224. if (r < 0) {
  1225. VHOST_OPS_DEBUG(r, "vhost VQ %u ring restore failed: %d", idx, r);
  1226. /* Connection to the backend is broken, so let's sync internal
  1227. * last avail idx to the device used idx.
  1228. */
  1229. virtio_queue_restore_last_avail_idx(vdev, idx);
  1230. } else {
  1231. virtio_queue_set_last_avail_idx(vdev, idx, state.num);
  1232. }
  1233. virtio_queue_invalidate_signalled_used(vdev, idx);
  1234. virtio_queue_update_used_idx(vdev, idx);
  1235. /* In the cross-endian case, we need to reset the vring endianness to
  1236. * native as legacy devices expect so by default.
  1237. */
  1238. if (vhost_needs_vring_endian(vdev)) {
  1239. vhost_virtqueue_set_vring_endian_legacy(dev,
  1240. !virtio_is_big_endian(vdev),
  1241. vhost_vq_index);
  1242. }
  1243. vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
  1244. 1, virtio_queue_get_used_size(vdev, idx));
  1245. vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
  1246. 0, virtio_queue_get_avail_size(vdev, idx));
  1247. vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
  1248. 0, virtio_queue_get_desc_size(vdev, idx));
  1249. }
  1250. static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev,
  1251. int n, uint32_t timeout)
  1252. {
  1253. int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
  1254. struct vhost_vring_state state = {
  1255. .index = vhost_vq_index,
  1256. .num = timeout,
  1257. };
  1258. int r;
  1259. if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) {
  1260. return -EINVAL;
  1261. }
  1262. r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state);
  1263. if (r) {
  1264. VHOST_OPS_DEBUG(r, "vhost_set_vring_busyloop_timeout failed");
  1265. return r;
  1266. }
  1267. return 0;
  1268. }
  1269. static void vhost_virtqueue_error_notifier(EventNotifier *n)
  1270. {
  1271. struct vhost_virtqueue *vq = container_of(n, struct vhost_virtqueue,
  1272. error_notifier);
  1273. struct vhost_dev *dev = vq->dev;
  1274. int index = vq - dev->vqs;
  1275. if (event_notifier_test_and_clear(n) && dev->vdev) {
  1276. VHOST_OPS_DEBUG(-EINVAL, "vhost vring error in virtqueue %d",
  1277. dev->vq_index + index);
  1278. }
  1279. }
  1280. static int vhost_virtqueue_init(struct vhost_dev *dev,
  1281. struct vhost_virtqueue *vq, int n)
  1282. {
  1283. int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
  1284. struct vhost_vring_file file = {
  1285. .index = vhost_vq_index,
  1286. };
  1287. int r = event_notifier_init(&vq->masked_notifier, 0);
  1288. if (r < 0) {
  1289. return r;
  1290. }
  1291. file.fd = event_notifier_get_wfd(&vq->masked_notifier);
  1292. r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
  1293. if (r) {
  1294. VHOST_OPS_DEBUG(r, "vhost_set_vring_call failed");
  1295. goto fail_call;
  1296. }
  1297. vq->dev = dev;
  1298. if (dev->vhost_ops->vhost_set_vring_err) {
  1299. r = event_notifier_init(&vq->error_notifier, 0);
  1300. if (r < 0) {
  1301. goto fail_call;
  1302. }
  1303. file.fd = event_notifier_get_fd(&vq->error_notifier);
  1304. r = dev->vhost_ops->vhost_set_vring_err(dev, &file);
  1305. if (r) {
  1306. VHOST_OPS_DEBUG(r, "vhost_set_vring_err failed");
  1307. goto fail_err;
  1308. }
  1309. event_notifier_set_handler(&vq->error_notifier,
  1310. vhost_virtqueue_error_notifier);
  1311. }
  1312. return 0;
  1313. fail_err:
  1314. event_notifier_cleanup(&vq->error_notifier);
  1315. fail_call:
  1316. event_notifier_cleanup(&vq->masked_notifier);
  1317. return r;
  1318. }
  1319. static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
  1320. {
  1321. event_notifier_cleanup(&vq->masked_notifier);
  1322. if (vq->dev->vhost_ops->vhost_set_vring_err) {
  1323. event_notifier_set_handler(&vq->error_notifier, NULL);
  1324. event_notifier_cleanup(&vq->error_notifier);
  1325. }
  1326. }
  1327. int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  1328. VhostBackendType backend_type, uint32_t busyloop_timeout,
  1329. Error **errp)
  1330. {
  1331. unsigned int used, reserved, limit;
  1332. uint64_t features;
  1333. int i, r, n_initialized_vqs = 0;
  1334. hdev->vdev = NULL;
  1335. hdev->migration_blocker = NULL;
  1336. r = vhost_set_backend_type(hdev, backend_type);
  1337. assert(r >= 0);
  1338. r = hdev->vhost_ops->vhost_backend_init(hdev, opaque, errp);
  1339. if (r < 0) {
  1340. goto fail;
  1341. }
  1342. r = hdev->vhost_ops->vhost_set_owner(hdev);
  1343. if (r < 0) {
  1344. error_setg_errno(errp, -r, "vhost_set_owner failed");
  1345. goto fail;
  1346. }
  1347. r = hdev->vhost_ops->vhost_get_features(hdev, &features);
  1348. if (r < 0) {
  1349. error_setg_errno(errp, -r, "vhost_get_features failed");
  1350. goto fail;
  1351. }
  1352. limit = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
  1353. if (limit < MEMORY_DEVICES_SAFE_MAX_MEMSLOTS &&
  1354. memory_devices_memslot_auto_decision_active()) {
  1355. error_setg(errp, "some memory device (like virtio-mem)"
  1356. " decided how many memory slots to use based on the overall"
  1357. " number of memory slots; this vhost backend would further"
  1358. " restricts the overall number of memory slots");
  1359. error_append_hint(errp, "Try plugging this vhost backend before"
  1360. " plugging such memory devices.\n");
  1361. r = -EINVAL;
  1362. goto fail;
  1363. }
  1364. for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
  1365. r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
  1366. if (r < 0) {
  1367. error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i);
  1368. goto fail;
  1369. }
  1370. }
  1371. if (busyloop_timeout) {
  1372. for (i = 0; i < hdev->nvqs; ++i) {
  1373. r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i,
  1374. busyloop_timeout);
  1375. if (r < 0) {
  1376. error_setg_errno(errp, -r, "Failed to set busyloop timeout");
  1377. goto fail_busyloop;
  1378. }
  1379. }
  1380. }
  1381. hdev->features = features;
  1382. hdev->memory_listener = (MemoryListener) {
  1383. .name = "vhost",
  1384. .begin = vhost_begin,
  1385. .commit = vhost_commit,
  1386. .region_add = vhost_region_addnop,
  1387. .region_nop = vhost_region_addnop,
  1388. .log_start = vhost_log_start,
  1389. .log_stop = vhost_log_stop,
  1390. .log_sync = vhost_log_sync,
  1391. .log_global_start = vhost_log_global_start,
  1392. .log_global_stop = vhost_log_global_stop,
  1393. .priority = MEMORY_LISTENER_PRIORITY_DEV_BACKEND
  1394. };
  1395. hdev->iommu_listener = (MemoryListener) {
  1396. .name = "vhost-iommu",
  1397. .region_add = vhost_iommu_region_add,
  1398. .region_del = vhost_iommu_region_del,
  1399. };
  1400. if (hdev->migration_blocker == NULL) {
  1401. if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
  1402. error_setg(&hdev->migration_blocker,
  1403. "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
  1404. } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) {
  1405. error_setg(&hdev->migration_blocker,
  1406. "Migration disabled: failed to allocate shared memory");
  1407. }
  1408. }
  1409. if (hdev->migration_blocker != NULL) {
  1410. r = migrate_add_blocker_normal(&hdev->migration_blocker, errp);
  1411. if (r < 0) {
  1412. goto fail_busyloop;
  1413. }
  1414. }
  1415. hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
  1416. hdev->n_mem_sections = 0;
  1417. hdev->mem_sections = NULL;
  1418. hdev->log = NULL;
  1419. hdev->log_size = 0;
  1420. hdev->log_enabled = false;
  1421. hdev->started = false;
  1422. memory_listener_register(&hdev->memory_listener, &address_space_memory);
  1423. QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
  1424. /*
  1425. * The listener we registered properly updated the corresponding counter.
  1426. * So we can trust that these values are accurate.
  1427. */
  1428. if (hdev->vhost_ops->vhost_backend_no_private_memslots &&
  1429. hdev->vhost_ops->vhost_backend_no_private_memslots(hdev)) {
  1430. used = used_shared_memslots;
  1431. } else {
  1432. used = used_memslots;
  1433. }
  1434. /*
  1435. * We assume that all reserved memslots actually require a real memslot
  1436. * in our vhost backend. This might not be true, for example, if the
  1437. * memslot would be ROM. If ever relevant, we can optimize for that --
  1438. * but we'll need additional information about the reservations.
  1439. */
  1440. reserved = memory_devices_get_reserved_memslots();
  1441. if (used + reserved > limit) {
  1442. error_setg(errp, "vhost backend memory slots limit (%d) is less"
  1443. " than current number of used (%d) and reserved (%d)"
  1444. " memory slots for memory devices.", limit, used, reserved);
  1445. r = -EINVAL;
  1446. goto fail_busyloop;
  1447. }
  1448. return 0;
  1449. fail_busyloop:
  1450. if (busyloop_timeout) {
  1451. while (--i >= 0) {
  1452. vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0);
  1453. }
  1454. }
  1455. fail:
  1456. hdev->nvqs = n_initialized_vqs;
  1457. vhost_dev_cleanup(hdev);
  1458. return r;
  1459. }
  1460. void vhost_dev_cleanup(struct vhost_dev *hdev)
  1461. {
  1462. int i;
  1463. trace_vhost_dev_cleanup(hdev);
  1464. for (i = 0; i < hdev->nvqs; ++i) {
  1465. vhost_virtqueue_cleanup(hdev->vqs + i);
  1466. }
  1467. if (hdev->mem) {
  1468. /* those are only safe after successful init */
  1469. memory_listener_unregister(&hdev->memory_listener);
  1470. QLIST_REMOVE(hdev, entry);
  1471. }
  1472. migrate_del_blocker(&hdev->migration_blocker);
  1473. g_free(hdev->mem);
  1474. g_free(hdev->mem_sections);
  1475. if (hdev->vhost_ops) {
  1476. hdev->vhost_ops->vhost_backend_cleanup(hdev);
  1477. }
  1478. assert(!hdev->log);
  1479. memset(hdev, 0, sizeof(struct vhost_dev));
  1480. }
  1481. void vhost_dev_disable_notifiers_nvqs(struct vhost_dev *hdev,
  1482. VirtIODevice *vdev,
  1483. unsigned int nvqs)
  1484. {
  1485. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  1486. int i, r;
  1487. /*
  1488. * Batch all the host notifiers in a single transaction to avoid
  1489. * quadratic time complexity in address_space_update_ioeventfds().
  1490. */
  1491. memory_region_transaction_begin();
  1492. for (i = 0; i < nvqs; ++i) {
  1493. r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
  1494. false);
  1495. if (r < 0) {
  1496. error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
  1497. }
  1498. assert(r >= 0);
  1499. }
  1500. /*
  1501. * The transaction expects the ioeventfds to be open when it
  1502. * commits. Do it now, before the cleanup loop.
  1503. */
  1504. memory_region_transaction_commit();
  1505. for (i = 0; i < nvqs; ++i) {
  1506. virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
  1507. }
  1508. virtio_device_release_ioeventfd(vdev);
  1509. }
  1510. /* Stop processing guest IO notifications in qemu.
  1511. * Start processing them in vhost in kernel.
  1512. */
  1513. int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
  1514. {
  1515. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  1516. int i, r;
  1517. /* We will pass the notifiers to the kernel, make sure that QEMU
  1518. * doesn't interfere.
  1519. */
  1520. r = virtio_device_grab_ioeventfd(vdev);
  1521. if (r < 0) {
  1522. error_report("binding does not support host notifiers");
  1523. return r;
  1524. }
  1525. /*
  1526. * Batch all the host notifiers in a single transaction to avoid
  1527. * quadratic time complexity in address_space_update_ioeventfds().
  1528. */
  1529. memory_region_transaction_begin();
  1530. for (i = 0; i < hdev->nvqs; ++i) {
  1531. r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
  1532. true);
  1533. if (r < 0) {
  1534. error_report("vhost VQ %d notifier binding failed: %d", i, -r);
  1535. memory_region_transaction_commit();
  1536. vhost_dev_disable_notifiers_nvqs(hdev, vdev, i);
  1537. return r;
  1538. }
  1539. }
  1540. memory_region_transaction_commit();
  1541. return 0;
  1542. }
  1543. /* Stop processing guest IO notifications in vhost.
  1544. * Start processing them in qemu.
  1545. * This might actually run the qemu handlers right away,
  1546. * so virtio in qemu must be completely setup when this is called.
  1547. */
  1548. void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
  1549. {
  1550. vhost_dev_disable_notifiers_nvqs(hdev, vdev, hdev->nvqs);
  1551. }
  1552. /* Test and clear event pending status.
  1553. * Should be called after unmask to avoid losing events.
  1554. */
  1555. bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
  1556. {
  1557. struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
  1558. assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
  1559. return event_notifier_test_and_clear(&vq->masked_notifier);
  1560. }
  1561. /* Mask/unmask events from this vq. */
  1562. void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
  1563. bool mask)
  1564. {
  1565. struct VirtQueue *vvq = virtio_get_queue(vdev, n);
  1566. int r, index = n - hdev->vq_index;
  1567. struct vhost_vring_file file;
  1568. /* should only be called after backend is connected */
  1569. assert(hdev->vhost_ops);
  1570. if (mask) {
  1571. assert(vdev->use_guest_notifier_mask);
  1572. file.fd = event_notifier_get_wfd(&hdev->vqs[index].masked_notifier);
  1573. } else {
  1574. file.fd = event_notifier_get_wfd(virtio_queue_get_guest_notifier(vvq));
  1575. }
  1576. file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
  1577. r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
  1578. if (r < 0) {
  1579. error_report("vhost_set_vring_call failed %d", -r);
  1580. }
  1581. }
  1582. bool vhost_config_pending(struct vhost_dev *hdev)
  1583. {
  1584. assert(hdev->vhost_ops);
  1585. if ((hdev->started == false) ||
  1586. (hdev->vhost_ops->vhost_set_config_call == NULL)) {
  1587. return false;
  1588. }
  1589. EventNotifier *notifier =
  1590. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier;
  1591. return event_notifier_test_and_clear(notifier);
  1592. }
  1593. void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask)
  1594. {
  1595. int fd;
  1596. int r;
  1597. EventNotifier *notifier =
  1598. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier;
  1599. EventNotifier *config_notifier = &vdev->config_notifier;
  1600. assert(hdev->vhost_ops);
  1601. if ((hdev->started == false) ||
  1602. (hdev->vhost_ops->vhost_set_config_call == NULL)) {
  1603. return;
  1604. }
  1605. if (mask) {
  1606. assert(vdev->use_guest_notifier_mask);
  1607. fd = event_notifier_get_fd(notifier);
  1608. } else {
  1609. fd = event_notifier_get_fd(config_notifier);
  1610. }
  1611. r = hdev->vhost_ops->vhost_set_config_call(hdev, fd);
  1612. if (r < 0) {
  1613. error_report("vhost_set_config_call failed %d", -r);
  1614. }
  1615. }
  1616. static void vhost_stop_config_intr(struct vhost_dev *dev)
  1617. {
  1618. int fd = -1;
  1619. assert(dev->vhost_ops);
  1620. if (dev->vhost_ops->vhost_set_config_call) {
  1621. dev->vhost_ops->vhost_set_config_call(dev, fd);
  1622. }
  1623. }
  1624. static void vhost_start_config_intr(struct vhost_dev *dev)
  1625. {
  1626. int r;
  1627. assert(dev->vhost_ops);
  1628. int fd = event_notifier_get_fd(&dev->vdev->config_notifier);
  1629. if (dev->vhost_ops->vhost_set_config_call) {
  1630. r = dev->vhost_ops->vhost_set_config_call(dev, fd);
  1631. if (!r) {
  1632. event_notifier_set(&dev->vdev->config_notifier);
  1633. }
  1634. }
  1635. }
  1636. uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
  1637. uint64_t features)
  1638. {
  1639. const int *bit = feature_bits;
  1640. while (*bit != VHOST_INVALID_FEATURE_BIT) {
  1641. uint64_t bit_mask = (1ULL << *bit);
  1642. if (!(hdev->features & bit_mask)) {
  1643. features &= ~bit_mask;
  1644. }
  1645. bit++;
  1646. }
  1647. return features;
  1648. }
  1649. void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
  1650. uint64_t features)
  1651. {
  1652. const int *bit = feature_bits;
  1653. while (*bit != VHOST_INVALID_FEATURE_BIT) {
  1654. uint64_t bit_mask = (1ULL << *bit);
  1655. if (features & bit_mask) {
  1656. hdev->acked_features |= bit_mask;
  1657. }
  1658. bit++;
  1659. }
  1660. }
  1661. int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
  1662. uint32_t config_len, Error **errp)
  1663. {
  1664. assert(hdev->vhost_ops);
  1665. if (hdev->vhost_ops->vhost_get_config) {
  1666. return hdev->vhost_ops->vhost_get_config(hdev, config, config_len,
  1667. errp);
  1668. }
  1669. error_setg(errp, "vhost_get_config not implemented");
  1670. return -ENOSYS;
  1671. }
  1672. int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data,
  1673. uint32_t offset, uint32_t size, uint32_t flags)
  1674. {
  1675. assert(hdev->vhost_ops);
  1676. if (hdev->vhost_ops->vhost_set_config) {
  1677. return hdev->vhost_ops->vhost_set_config(hdev, data, offset,
  1678. size, flags);
  1679. }
  1680. return -ENOSYS;
  1681. }
  1682. void vhost_dev_set_config_notifier(struct vhost_dev *hdev,
  1683. const VhostDevConfigOps *ops)
  1684. {
  1685. hdev->config_ops = ops;
  1686. }
  1687. void vhost_dev_free_inflight(struct vhost_inflight *inflight)
  1688. {
  1689. if (inflight && inflight->addr) {
  1690. qemu_memfd_free(inflight->addr, inflight->size, inflight->fd);
  1691. inflight->addr = NULL;
  1692. inflight->fd = -1;
  1693. }
  1694. }
  1695. int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev)
  1696. {
  1697. int r;
  1698. if (hdev->vhost_ops->vhost_get_inflight_fd == NULL ||
  1699. hdev->vhost_ops->vhost_set_inflight_fd == NULL) {
  1700. return 0;
  1701. }
  1702. hdev->vdev = vdev;
  1703. r = vhost_dev_set_features(hdev, hdev->log_enabled);
  1704. if (r < 0) {
  1705. VHOST_OPS_DEBUG(r, "vhost_dev_prepare_inflight failed");
  1706. return r;
  1707. }
  1708. return 0;
  1709. }
  1710. int vhost_dev_set_inflight(struct vhost_dev *dev,
  1711. struct vhost_inflight *inflight)
  1712. {
  1713. int r;
  1714. if (dev->vhost_ops->vhost_set_inflight_fd && inflight->addr) {
  1715. r = dev->vhost_ops->vhost_set_inflight_fd(dev, inflight);
  1716. if (r) {
  1717. VHOST_OPS_DEBUG(r, "vhost_set_inflight_fd failed");
  1718. return r;
  1719. }
  1720. }
  1721. return 0;
  1722. }
  1723. int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
  1724. struct vhost_inflight *inflight)
  1725. {
  1726. int r;
  1727. if (dev->vhost_ops->vhost_get_inflight_fd) {
  1728. r = dev->vhost_ops->vhost_get_inflight_fd(dev, queue_size, inflight);
  1729. if (r) {
  1730. VHOST_OPS_DEBUG(r, "vhost_get_inflight_fd failed");
  1731. return r;
  1732. }
  1733. }
  1734. return 0;
  1735. }
  1736. static int vhost_dev_set_vring_enable(struct vhost_dev *hdev, int enable)
  1737. {
  1738. if (!hdev->vhost_ops->vhost_set_vring_enable) {
  1739. return 0;
  1740. }
  1741. /*
  1742. * For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not
  1743. * been negotiated, the rings start directly in the enabled state, and
  1744. * .vhost_set_vring_enable callback will fail since
  1745. * VHOST_USER_SET_VRING_ENABLE is not supported.
  1746. */
  1747. if (hdev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER &&
  1748. !virtio_has_feature(hdev->backend_features,
  1749. VHOST_USER_F_PROTOCOL_FEATURES)) {
  1750. return 0;
  1751. }
  1752. return hdev->vhost_ops->vhost_set_vring_enable(hdev, enable);
  1753. }
  1754. /*
  1755. * Host notifiers must be enabled at this point.
  1756. *
  1757. * If @vrings is true, this function will enable all vrings before starting the
  1758. * device. If it is false, the vring initialization is left to be done by the
  1759. * caller.
  1760. */
  1761. int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
  1762. {
  1763. int i, r;
  1764. /* should only be called after backend is connected */
  1765. assert(hdev->vhost_ops);
  1766. trace_vhost_dev_start(hdev, vdev->name, vrings);
  1767. vdev->vhost_started = true;
  1768. hdev->started = true;
  1769. hdev->vdev = vdev;
  1770. r = vhost_dev_set_features(hdev, hdev->log_enabled);
  1771. if (r < 0) {
  1772. goto fail_features;
  1773. }
  1774. if (vhost_dev_has_iommu(hdev)) {
  1775. memory_listener_register(&hdev->iommu_listener, vdev->dma_as);
  1776. }
  1777. r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem);
  1778. if (r < 0) {
  1779. VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
  1780. goto fail_mem;
  1781. }
  1782. for (i = 0; i < hdev->nvqs; ++i) {
  1783. r = vhost_virtqueue_start(hdev,
  1784. vdev,
  1785. hdev->vqs + i,
  1786. hdev->vq_index + i);
  1787. if (r < 0) {
  1788. goto fail_vq;
  1789. }
  1790. }
  1791. r = event_notifier_init(
  1792. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier, 0);
  1793. if (r < 0) {
  1794. VHOST_OPS_DEBUG(r, "event_notifier_init failed");
  1795. goto fail_vq;
  1796. }
  1797. event_notifier_test_and_clear(
  1798. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier);
  1799. if (!vdev->use_guest_notifier_mask) {
  1800. vhost_config_mask(hdev, vdev, true);
  1801. }
  1802. if (hdev->log_enabled) {
  1803. uint64_t log_base;
  1804. hdev->log_size = vhost_get_log_size(hdev);
  1805. hdev->log = vhost_log_get(hdev->vhost_ops->backend_type,
  1806. hdev->log_size,
  1807. vhost_dev_log_is_shared(hdev));
  1808. log_base = (uintptr_t)hdev->log->log;
  1809. r = hdev->vhost_ops->vhost_set_log_base(hdev,
  1810. hdev->log_size ? log_base : 0,
  1811. hdev->log);
  1812. if (r < 0) {
  1813. VHOST_OPS_DEBUG(r, "vhost_set_log_base failed");
  1814. goto fail_log;
  1815. }
  1816. vhost_dev_elect_mem_logger(hdev, true);
  1817. }
  1818. if (vrings) {
  1819. r = vhost_dev_set_vring_enable(hdev, true);
  1820. if (r) {
  1821. goto fail_log;
  1822. }
  1823. }
  1824. if (hdev->vhost_ops->vhost_dev_start) {
  1825. r = hdev->vhost_ops->vhost_dev_start(hdev, true);
  1826. if (r) {
  1827. goto fail_start;
  1828. }
  1829. }
  1830. if (vhost_dev_has_iommu(hdev) &&
  1831. hdev->vhost_ops->vhost_set_iotlb_callback) {
  1832. hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true);
  1833. /* Update used ring information for IOTLB to work correctly,
  1834. * vhost-kernel code requires for this.*/
  1835. for (i = 0; i < hdev->nvqs; ++i) {
  1836. struct vhost_virtqueue *vq = hdev->vqs + i;
  1837. vhost_device_iotlb_miss(hdev, vq->used_phys, true);
  1838. }
  1839. }
  1840. vhost_start_config_intr(hdev);
  1841. return 0;
  1842. fail_start:
  1843. if (vrings) {
  1844. vhost_dev_set_vring_enable(hdev, false);
  1845. }
  1846. fail_log:
  1847. vhost_log_put(hdev, false);
  1848. fail_vq:
  1849. while (--i >= 0) {
  1850. vhost_virtqueue_stop(hdev,
  1851. vdev,
  1852. hdev->vqs + i,
  1853. hdev->vq_index + i);
  1854. }
  1855. fail_mem:
  1856. if (vhost_dev_has_iommu(hdev)) {
  1857. memory_listener_unregister(&hdev->iommu_listener);
  1858. }
  1859. fail_features:
  1860. vdev->vhost_started = false;
  1861. hdev->started = false;
  1862. return r;
  1863. }
  1864. /* Host notifiers must be enabled at this point. */
  1865. void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
  1866. {
  1867. int i;
  1868. /* should only be called after backend is connected */
  1869. assert(hdev->vhost_ops);
  1870. event_notifier_test_and_clear(
  1871. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier);
  1872. event_notifier_test_and_clear(&vdev->config_notifier);
  1873. event_notifier_cleanup(
  1874. &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier);
  1875. trace_vhost_dev_stop(hdev, vdev->name, vrings);
  1876. if (hdev->vhost_ops->vhost_dev_start) {
  1877. hdev->vhost_ops->vhost_dev_start(hdev, false);
  1878. }
  1879. if (vrings) {
  1880. vhost_dev_set_vring_enable(hdev, false);
  1881. }
  1882. for (i = 0; i < hdev->nvqs; ++i) {
  1883. vhost_virtqueue_stop(hdev,
  1884. vdev,
  1885. hdev->vqs + i,
  1886. hdev->vq_index + i);
  1887. }
  1888. if (hdev->vhost_ops->vhost_reset_status) {
  1889. hdev->vhost_ops->vhost_reset_status(hdev);
  1890. }
  1891. if (vhost_dev_has_iommu(hdev)) {
  1892. if (hdev->vhost_ops->vhost_set_iotlb_callback) {
  1893. hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false);
  1894. }
  1895. memory_listener_unregister(&hdev->iommu_listener);
  1896. }
  1897. vhost_stop_config_intr(hdev);
  1898. vhost_log_put(hdev, true);
  1899. hdev->started = false;
  1900. vdev->vhost_started = false;
  1901. hdev->vdev = NULL;
  1902. }
  1903. int vhost_net_set_backend(struct vhost_dev *hdev,
  1904. struct vhost_vring_file *file)
  1905. {
  1906. if (hdev->vhost_ops->vhost_net_set_backend) {
  1907. return hdev->vhost_ops->vhost_net_set_backend(hdev, file);
  1908. }
  1909. return -ENOSYS;
  1910. }
  1911. int vhost_reset_device(struct vhost_dev *hdev)
  1912. {
  1913. if (hdev->vhost_ops->vhost_reset_device) {
  1914. return hdev->vhost_ops->vhost_reset_device(hdev);
  1915. }
  1916. return -ENOSYS;
  1917. }
  1918. bool vhost_supports_device_state(struct vhost_dev *dev)
  1919. {
  1920. if (dev->vhost_ops->vhost_supports_device_state) {
  1921. return dev->vhost_ops->vhost_supports_device_state(dev);
  1922. }
  1923. return false;
  1924. }
  1925. int vhost_set_device_state_fd(struct vhost_dev *dev,
  1926. VhostDeviceStateDirection direction,
  1927. VhostDeviceStatePhase phase,
  1928. int fd,
  1929. int *reply_fd,
  1930. Error **errp)
  1931. {
  1932. if (dev->vhost_ops->vhost_set_device_state_fd) {
  1933. return dev->vhost_ops->vhost_set_device_state_fd(dev, direction, phase,
  1934. fd, reply_fd, errp);
  1935. }
  1936. error_setg(errp,
  1937. "vhost transport does not support migration state transfer");
  1938. return -ENOSYS;
  1939. }
  1940. int vhost_check_device_state(struct vhost_dev *dev, Error **errp)
  1941. {
  1942. if (dev->vhost_ops->vhost_check_device_state) {
  1943. return dev->vhost_ops->vhost_check_device_state(dev, errp);
  1944. }
  1945. error_setg(errp,
  1946. "vhost transport does not support migration state transfer");
  1947. return -ENOSYS;
  1948. }
  1949. int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp)
  1950. {
  1951. ERRP_GUARD();
  1952. /* Maximum chunk size in which to transfer the state */
  1953. const size_t chunk_size = 1 * 1024 * 1024;
  1954. g_autofree void *transfer_buf = NULL;
  1955. g_autoptr(GError) g_err = NULL;
  1956. int pipe_fds[2], read_fd = -1, write_fd = -1, reply_fd = -1;
  1957. int ret;
  1958. /* [0] for reading (our end), [1] for writing (back-end's end) */
  1959. if (!g_unix_open_pipe(pipe_fds, FD_CLOEXEC, &g_err)) {
  1960. error_setg(errp, "Failed to set up state transfer pipe: %s",
  1961. g_err->message);
  1962. ret = -EINVAL;
  1963. goto fail;
  1964. }
  1965. read_fd = pipe_fds[0];
  1966. write_fd = pipe_fds[1];
  1967. /*
  1968. * VHOST_TRANSFER_STATE_PHASE_STOPPED means the device must be stopped.
  1969. * Ideally, it is suspended, but SUSPEND/RESUME currently do not exist for
  1970. * vhost-user, so just check that it is stopped at all.
  1971. */
  1972. assert(!dev->started);
  1973. /* Transfer ownership of write_fd to the back-end */
  1974. ret = vhost_set_device_state_fd(dev,
  1975. VHOST_TRANSFER_STATE_DIRECTION_SAVE,
  1976. VHOST_TRANSFER_STATE_PHASE_STOPPED,
  1977. write_fd,
  1978. &reply_fd,
  1979. errp);
  1980. if (ret < 0) {
  1981. error_prepend(errp, "Failed to initiate state transfer: ");
  1982. goto fail;
  1983. }
  1984. /* If the back-end wishes to use a different pipe, switch over */
  1985. if (reply_fd >= 0) {
  1986. close(read_fd);
  1987. read_fd = reply_fd;
  1988. }
  1989. transfer_buf = g_malloc(chunk_size);
  1990. while (true) {
  1991. ssize_t read_ret;
  1992. read_ret = RETRY_ON_EINTR(read(read_fd, transfer_buf, chunk_size));
  1993. if (read_ret < 0) {
  1994. ret = -errno;
  1995. error_setg_errno(errp, -ret, "Failed to receive state");
  1996. goto fail;
  1997. }
  1998. assert(read_ret <= chunk_size);
  1999. qemu_put_be32(f, read_ret);
  2000. if (read_ret == 0) {
  2001. /* EOF */
  2002. break;
  2003. }
  2004. qemu_put_buffer(f, transfer_buf, read_ret);
  2005. }
  2006. /*
  2007. * Back-end will not really care, but be clean and close our end of the pipe
  2008. * before inquiring the back-end about whether transfer was successful
  2009. */
  2010. close(read_fd);
  2011. read_fd = -1;
  2012. /* Also, verify that the device is still stopped */
  2013. assert(!dev->started);
  2014. ret = vhost_check_device_state(dev, errp);
  2015. if (ret < 0) {
  2016. goto fail;
  2017. }
  2018. ret = 0;
  2019. fail:
  2020. if (read_fd >= 0) {
  2021. close(read_fd);
  2022. }
  2023. return ret;
  2024. }
  2025. int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp)
  2026. {
  2027. ERRP_GUARD();
  2028. size_t transfer_buf_size = 0;
  2029. g_autofree void *transfer_buf = NULL;
  2030. g_autoptr(GError) g_err = NULL;
  2031. int pipe_fds[2], read_fd = -1, write_fd = -1, reply_fd = -1;
  2032. int ret;
  2033. /* [0] for reading (back-end's end), [1] for writing (our end) */
  2034. if (!g_unix_open_pipe(pipe_fds, FD_CLOEXEC, &g_err)) {
  2035. error_setg(errp, "Failed to set up state transfer pipe: %s",
  2036. g_err->message);
  2037. ret = -EINVAL;
  2038. goto fail;
  2039. }
  2040. read_fd = pipe_fds[0];
  2041. write_fd = pipe_fds[1];
  2042. /*
  2043. * VHOST_TRANSFER_STATE_PHASE_STOPPED means the device must be stopped.
  2044. * Ideally, it is suspended, but SUSPEND/RESUME currently do not exist for
  2045. * vhost-user, so just check that it is stopped at all.
  2046. */
  2047. assert(!dev->started);
  2048. /* Transfer ownership of read_fd to the back-end */
  2049. ret = vhost_set_device_state_fd(dev,
  2050. VHOST_TRANSFER_STATE_DIRECTION_LOAD,
  2051. VHOST_TRANSFER_STATE_PHASE_STOPPED,
  2052. read_fd,
  2053. &reply_fd,
  2054. errp);
  2055. if (ret < 0) {
  2056. error_prepend(errp, "Failed to initiate state transfer: ");
  2057. goto fail;
  2058. }
  2059. /* If the back-end wishes to use a different pipe, switch over */
  2060. if (reply_fd >= 0) {
  2061. close(write_fd);
  2062. write_fd = reply_fd;
  2063. }
  2064. while (true) {
  2065. size_t this_chunk_size = qemu_get_be32(f);
  2066. ssize_t write_ret;
  2067. const uint8_t *transfer_pointer;
  2068. if (this_chunk_size == 0) {
  2069. /* End of state */
  2070. break;
  2071. }
  2072. if (transfer_buf_size < this_chunk_size) {
  2073. transfer_buf = g_realloc(transfer_buf, this_chunk_size);
  2074. transfer_buf_size = this_chunk_size;
  2075. }
  2076. if (qemu_get_buffer(f, transfer_buf, this_chunk_size) <
  2077. this_chunk_size)
  2078. {
  2079. error_setg(errp, "Failed to read state");
  2080. ret = -EINVAL;
  2081. goto fail;
  2082. }
  2083. transfer_pointer = transfer_buf;
  2084. while (this_chunk_size > 0) {
  2085. write_ret = RETRY_ON_EINTR(
  2086. write(write_fd, transfer_pointer, this_chunk_size)
  2087. );
  2088. if (write_ret < 0) {
  2089. ret = -errno;
  2090. error_setg_errno(errp, -ret, "Failed to send state");
  2091. goto fail;
  2092. } else if (write_ret == 0) {
  2093. error_setg(errp, "Failed to send state: Connection is closed");
  2094. ret = -ECONNRESET;
  2095. goto fail;
  2096. }
  2097. assert(write_ret <= this_chunk_size);
  2098. this_chunk_size -= write_ret;
  2099. transfer_pointer += write_ret;
  2100. }
  2101. }
  2102. /*
  2103. * Close our end, thus ending transfer, before inquiring the back-end about
  2104. * whether transfer was successful
  2105. */
  2106. close(write_fd);
  2107. write_fd = -1;
  2108. /* Also, verify that the device is still stopped */
  2109. assert(!dev->started);
  2110. ret = vhost_check_device_state(dev, errp);
  2111. if (ret < 0) {
  2112. goto fail;
  2113. }
  2114. ret = 0;
  2115. fail:
  2116. if (write_fd >= 0) {
  2117. close(write_fd);
  2118. }
  2119. return ret;
  2120. }