From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308168; cv=none; d=zohomail.com; s=zohoarc; b=Bp9mj+w8MInZfvhRyN1o8rRlye0k2WU4PhueYlhP7NtUvUQ0D61dKF40zdD/4vwDT4zcKyAivVGjZjsND9BERQqWcuuHT5Xm0xTUs8qGFgV4RI317YX7PLbe0hdJpmhEEG0mZaSRK0gxNYz645Aip0RMko4z4ZBzdnl3a0BseOo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308168; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4Vhrz0kfHTns1X4c1uf0ed01YYwIHZK57+R+PKXqD9o=; b=NhBsYVelQUlZk2RxWIKSl1UJZO8L5/r9HCvCwTfGO8AiuhutDP2TWLy0EPDGb2sGnRtXYUhNwZYq6Tuv2aaHRrBnEQ+1gdBBbjDYQdVmNIy3Ur5eSa/KdhO4H3CB2J3S3SxYqK0KlMSk3Nm4uWsPPDofJRhggb/nFHBTRIOFfrg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308168279866.3948410597842; Tue, 13 Apr 2021 03:02:48 -0700 (PDT) Received: from localhost ([::1]:39922 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFsc-0004aP-NW for importer@patchew.org; Tue, 13 Apr 2021 06:02:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40892) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFn1-00079p-Gx for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:56:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33348) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFmy-0004Bg-LY for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:56:59 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-529-zjycwCCWMBedUM1GiN3q0A-1; Tue, 13 Apr 2021 05:56:54 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 37F5D1084423; Tue, 13 Apr 2021 09:56:53 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9ABAD5C8A8; Tue, 13 Apr 2021 09:56:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307816; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4Vhrz0kfHTns1X4c1uf0ed01YYwIHZK57+R+PKXqD9o=; b=MJhfu3d/RGuZrW9unBV6uTa0e/ZRQGHMAARBkDv+ACYgXRw/TvT2uTmuvt4l0O13XhbQTR 8BlcqbazLeDlXnl7n/3B4zkPsRm/PZ26ZMqDiQRvVpbZ/g+mPdJeuakDATfwRB/zFT6+Y9 +O5XpkicjRBOjh8W6dEOuoB326bcblg= X-MC-Unique: zjycwCCWMBedUM1GiN3q0A-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 01/13] memory: Introduce RamDiscardManager for RAM memory regions Date: Tue, 13 Apr 2021 11:55:19 +0200 Message-Id: <20210413095531.25603-2-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We have some special RAM memory regions (managed by virtio-mem), whereby the guest agreed to only use selected memory ranges. "unused" parts are discarded so they won't consume memory - to logically unplug these memory ranges. Before the VM is allowed to use such logically unplugged memory again, coordination with the hypervisor is required. This results in "sparse" mmaps/RAMBlocks/memory regions, whereby only coordinated parts are valid to be used/accessed by the VM. In most cases, we don't care about that - e.g., in KVM, we simply have a single KVM memory slot. However, in case of vfio, registering the whole region with the kernel results in all pages getting pinned, and therefore an unexpected high memory consumption - discarding of RAM in that context is broken. Let's introduce a way to coordinate discarding/populating memory within a RAM memory region with such special consumers of RAM memory regions: they can register as listeners and get updates on memory getting discarded and populated. Using this machinery, vfio will be able to map only the currently populated parts, resulting in discarded parts not getting pinned and not consuming memory. A RamDiscardManager has to be set for a memory region before it is getting mapped, and cannot change while the memory region is mapped. Note: At some point, we might want to let RAMBlock users (esp. vfio used for nvme://) consume this interface as well. We'll need RAMBlock notifier calls when a RAMBlock is getting mapped/unmapped (via the corresponding memory region), so we can properly register a listener there as well. Reviewed-by: Pankaj Gupta Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- include/exec/memory.h | 286 ++++++++++++++++++++++++++++++++++++++---- softmmu/memory.c | 71 +++++++++++ 2 files changed, 335 insertions(+), 22 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 5728a681b2..38a3b41ac1 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -42,6 +42,12 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionC= lass; DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass, IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION) =20 +#define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager" +typedef struct RamDiscardManagerClass RamDiscardManagerClass; +typedef struct RamDiscardManager RamDiscardManager; +DECLARE_OBJ_CHECKERS(RamDiscardManager, RamDiscardManagerClass, + RAM_DISCARD_MANAGER, TYPE_RAM_DISCARD_MANAGER); + #ifdef CONFIG_FUZZ void fuzz_dma_read_cb(size_t addr, size_t len, @@ -65,6 +71,28 @@ struct ReservedRegion { unsigned type; }; =20 +/** + * struct MemoryRegionSection: describes a fragment of a #MemoryRegion + * + * @mr: the region, or %NULL if empty + * @fv: the flat view of the address space the region is mapped in + * @offset_within_region: the beginning of the section, relative to @mr's = start + * @size: the size of the section; will not exceed @mr's boundaries + * @offset_within_address_space: the address of the first byte of the sect= ion + * relative to the region's address space + * @readonly: writes to this section are ignored + * @nonvolatile: this section is non-volatile + */ +struct MemoryRegionSection { + Int128 size; + MemoryRegion *mr; + FlatView *fv; + hwaddr offset_within_region; + hwaddr offset_within_address_space; + bool readonly; + bool nonvolatile; +}; + typedef struct IOMMUTLBEntry IOMMUTLBEntry; =20 /* See address_space_translate: bit 0 is read, bit 1 is write. */ @@ -441,6 +469,206 @@ struct IOMMUMemoryRegionClass { Error **errp); }; =20 +typedef struct RamDiscardListener RamDiscardListener; +typedef int (*NotifyRamPopulate)(RamDiscardListener *rdl, + MemoryRegionSection *section); +typedef void (*NotifyRamDiscard)(RamDiscardListener *rdl, + MemoryRegionSection *section); + +struct RamDiscardListener { + /* + * @notify_populate: + * + * Notification that previously discarded memory is about to get popul= ated. + * Listeners are able to object. If any listener objects, already + * successfully notified listeners are notified about a discard again. + * + * @rdl: the #RamDiscardListener getting notified + * @section: the #MemoryRegionSection to get populated. The section + * is aligned within the memory region to the minimum granul= arity + * unless it would exceed the registered section. + * + * Returns 0 on success. If the notification is rejected by the listen= er, + * an error is returned. + */ + NotifyRamPopulate notify_populate; + + /* + * @notify_discard: + * + * Notification that previously populated memory was discarded success= fully + * and listeners should drop all references to such memory and prevent + * new population (e.g., unmap). + * + * @rdl: the #RamDiscardListener getting notified + * @section: the #MemoryRegionSection to get populated. The section + * is aligned within the memory region to the minimum granul= arity + * unless it would exceed the registered section. + */ + NotifyRamDiscard notify_discard; + + /* + * @double_discard_supported: + * + * The listener suppors getting @notify_discard notifications that span + * already discarded parts. + */ + bool double_discard_supported; + + MemoryRegionSection *section; + QLIST_ENTRY(RamDiscardListener) next; +}; + +static inline void ram_discard_listener_init(RamDiscardListener *rdl, + NotifyRamPopulate populate_fn, + NotifyRamDiscard discard_fn, + bool double_discard_supported) +{ + rdl->notify_populate =3D populate_fn; + rdl->notify_discard =3D discard_fn; + rdl->double_discard_supported =3D double_discard_supported; +} + +typedef int (*ReplayRamPopulate)(MemoryRegionSection *section, void *opaqu= e); + +/* + * RamDiscardManagerClass: + * + * A #RamDiscardManager coordinates which parts of specific RAM #MemoryReg= ion + * regions are currently populated to be used/accessed by the VM, notifying + * after parts were discarded (freeing up memory) and before parts will be + * populated (consuming memory), to be used/acessed by the VM. + * + * A #RamDiscardManager can only be set for a RAM #MemoryRegion while the + * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegio= n is + * mapped. + * + * The #RamDiscardManager is intended to be used by technologies that are + * incompatible with discarding of RAM (e.g., VFIO, which may pin all + * memory inside a #MemoryRegion), and require proper coordination to only + * map the currently populated parts, to hinder parts that are expected to + * remain discarded from silently getting populated and consuming memory. + * Technologies that support discarding of RAM don't have to bother and can + * simply map the whole #MemoryRegion. + * + * An example #RamDiscardManager is virtio-mem, which logically (un)plugs + * memory within an assigned RAM #MemoryRegion, coordinated with the VM. + * Logically unplugging memory consists of discarding RAM. The VM agreed t= o not + * access unplugged (discarded) memory - especially via DMA. virtio-mem wi= ll + * properly coordinate with listeners before memory is plugged (populated), + * and after memory is unplugged (discarded). + * + * Listeners are called in multiples of the minimum granularity (unless it + * would exceed the registered range) and changes are aligned to the minim= um + * granularity within the #MemoryRegion. Listeners have to prepare for mem= ory + * becomming discarded in a different granularity than it was populated an= d the + * other way around. + */ +struct RamDiscardManagerClass { + /* private */ + InterfaceClass parent_class; + + /* public */ + + /** + * @get_min_granularity: + * + * Get the minimum granularity in which listeners will get notified + * about changes within the #MemoryRegion via the #RamDiscardManager. + * + * @rdm: the #RamDiscardManager + * @mr: the #MemoryRegion + * + * Returns the minimum granularity. + */ + uint64_t (*get_min_granularity)(const RamDiscardManager *rdm, + const MemoryRegion *mr); + + /** + * @is_populated: + * + * Check whether the given #MemoryRegionSection is completely populated + * (i.e., no parts are currently discarded) via the #RamDiscardManager. + * There are no alignment requirements. + * + * @rdm: the #RamDiscardManager + * @section: the #MemoryRegionSection + * + * Returns whether the given range is completely populated. + */ + bool (*is_populated)(const RamDiscardManager *rdm, + const MemoryRegionSection *section); + + /** + * @replay_populated: + * + * Call the #ReplayRamPopulate callback for all populated parts within= the + * #MemoryRegionSection via the #RamDiscardManager. + * + * In case any call fails, no further calls are made. + * + * @rdm: the #RamDiscardManager + * @section: the #MemoryRegionSection + * @replay_fn: the #ReplayRamPopulate callback + * @opaque: pointer to forward to the callback + * + * Returns 0 on success, or a negative error if any notification faile= d. + */ + int (*replay_populated)(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamPopulate replay_fn, void *opaque); + + /** + * @register_listener: + * + * Register a #RamDiscardListener for the given #MemoryRegionSection a= nd + * immediately notify the #RamDiscardListener about all populated parts + * within the #MemoryRegionSection via the #RamDiscardManager. + * + * In case any notification fails, no further notifications are trigge= red + * and an error is logged. + * + * @rdm: the #RamDiscardManager + * @rdl: the #RamDiscardListener + * @section: the #MemoryRegionSection + */ + void (*register_listener)(RamDiscardManager *rdm, + RamDiscardListener *rdl, + MemoryRegionSection *section); + + /** + * @unregister_listener: + * + * Unregister a previously registered #RamDiscardListener via the + * #RamDiscardManager after notifying the #RamDiscardListener about all + * populated parts becoming unpopulated within the registered + * #MemoryRegionSection. + * + * @rdm: the #RamDiscardManager + * @rdl: the #RamDiscardListener + */ + void (*unregister_listener)(RamDiscardManager *rdm, + RamDiscardListener *rdl); +}; + +uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *= rdm, + const MemoryRegion *mr); + +bool ram_discard_manager_is_populated(const RamDiscardManager *rdm, + const MemoryRegionSection *section); + +int ram_discard_manager_replay_populated(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamPopulate replay_fn, + void *opaque); + +void ram_discard_manager_register_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl, + MemoryRegionSection *section); + +void ram_discard_manager_unregister_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl); + typedef struct CoalescedMemoryRange CoalescedMemoryRange; typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; =20 @@ -487,6 +715,7 @@ struct MemoryRegion { const char *name; unsigned ioeventfd_nb; MemoryRegionIoeventfd *ioeventfds; + RamDiscardManager *rdm; /* Only for RAM */ }; =20 struct IOMMUMemoryRegion { @@ -806,28 +1035,6 @@ typedef bool (*flatview_cb)(Int128 start, */ void flatview_for_each_range(FlatView *fv, flatview_cb cb, void *opaque); =20 -/** - * struct MemoryRegionSection: describes a fragment of a #MemoryRegion - * - * @mr: the region, or %NULL if empty - * @fv: the flat view of the address space the region is mapped in - * @offset_within_region: the beginning of the section, relative to @mr's = start - * @size: the size of the section; will not exceed @mr's boundaries - * @offset_within_address_space: the address of the first byte of the sect= ion - * relative to the region's address space - * @readonly: writes to this section are ignored - * @nonvolatile: this section is non-volatile - */ -struct MemoryRegionSection { - Int128 size; - MemoryRegion *mr; - FlatView *fv; - hwaddr offset_within_region; - hwaddr offset_within_address_space; - bool readonly; - bool nonvolatile; -}; - static inline bool MemoryRegionSection_eq(MemoryRegionSection *a, MemoryRegionSection *b) { @@ -2003,6 +2210,41 @@ bool memory_region_present(MemoryRegion *container, = hwaddr addr); */ bool memory_region_is_mapped(MemoryRegion *mr); =20 +/** + * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a + * #MemoryRegion + * + * The #RamDiscardManager cannot change while a memory region is mapped. + * + * @mr: the #MemoryRegion + */ +RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr); + +/** + * memory_region_has_ram_discard_manager: check whether a #MemoryRegion ha= s a + * #RamDiscardManager assigned + * + * @mr: the #MemoryRegion + */ +static inline bool memory_region_has_ram_discard_manager(MemoryRegion *mr) +{ + return !!memory_region_get_ram_discard_manager(mr); +} + +/** + * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a + * #MemoryRegion + * + * This function must not be called for a mapped #MemoryRegion, a #MemoryR= egion + * that does not cover RAM, or a #MemoryRegion that already has a + * #RamDiscardManager assigned. + * + * @mr: the #MemoryRegion + * @urn: #RamDiscardManager to set + */ +void memory_region_set_ram_discard_manager(MemoryRegion *mr, + RamDiscardManager *rdm); + /** * memory_region_find: translate an address/size relative to a * MemoryRegion into a #MemoryRegionSection. diff --git a/softmmu/memory.c b/softmmu/memory.c index d4493ef9e4..26ea87d77a 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2030,6 +2030,70 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegio= n *iommu_mr) return imrc->num_indexes(iommu_mr); } =20 +RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr) +{ + if (!memory_region_is_mapped(mr) || !memory_region_is_ram(mr)) { + return NULL; + } + return mr->rdm; +} + +void memory_region_set_ram_discard_manager(MemoryRegion *mr, + RamDiscardManager *rdm) +{ + g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr)); + g_assert(!rdm || !mr->rdm); + mr->rdm =3D rdm; +} + +uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *= rdm, + const MemoryRegion *mr) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); + + g_assert(rdmc->get_min_granularity); + return rdmc->get_min_granularity(rdm, mr); +} + +bool ram_discard_manager_is_populated(const RamDiscardManager *rdm, + const MemoryRegionSection *section) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); + + g_assert(rdmc->is_populated); + return rdmc->is_populated(rdm, section); +} + +int ram_discard_manager_replay_populated(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamPopulate replay_fn, + void *opaque) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); + + g_assert(rdmc->replay_populated); + return rdmc->replay_populated(rdm, section, replay_fn, opaque); +} + +void ram_discard_manager_register_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl, + MemoryRegionSection *section) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); + + g_assert(rdmc->register_listener); + rdmc->register_listener(rdm, rdl, section); +} + +void ram_discard_manager_unregister_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); + + g_assert(rdmc->unregister_listener); + rdmc->unregister_listener(rdm, rdl); +} + void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) { uint8_t mask =3D 1 << client; @@ -3310,10 +3374,17 @@ static const TypeInfo iommu_memory_region_info =3D { .abstract =3D true, }; =20 +static const TypeInfo ram_discard_manager_info =3D { + .parent =3D TYPE_INTERFACE, + .name =3D TYPE_RAM_DISCARD_MANAGER, + .class_size =3D sizeof(RamDiscardManagerClass), +}; + static void memory_register_types(void) { type_register_static(&memory_region_info); type_register_static(&iommu_memory_region_info); + type_register_static(&ram_discard_manager_info); } =20 type_init(memory_register_types) --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618307948; cv=none; d=zohomail.com; s=zohoarc; b=bOIJDWpSEUsU74CTGyLObMqaFZJ13a7ahYwAq4+c2xK/mJJXmoW3X4VIH7WduhQMTkCd/PoZ0ApPKU+Ci7H7x5GiRHLi7m1tfU+BD+S28C5jvMpBgBfiGHCDQntPD9ggEwiBPu1ceZa9KLyukoV2YE6xr/iB+VbARzgdQpVjgI4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618307948; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0MGpkdIKxvLwhVzcmqhAuYzCR38cQPs+gxvD9ayJdU4=; b=PK5aWEDaSa/ps/1tzBUcfTFRmEXcUvfVz96hhh/j7lN7g/ZU9CxBPn+EU7GrhQ1NiIpwoPoIvUYvpsRhD5Sfg22kiTxzzEO922TWMTDN7/jAoudRLFsuUPN49tIDZw8jHCKPlK+zsKpZG6yWc6X9oMZaBG5RXhRuvAjUZGvqWX4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161830794813820.948515622929563; Tue, 13 Apr 2021 02:59:08 -0700 (PDT) Received: from localhost ([::1]:56650 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFp5-0008G2-3P for importer@patchew.org; Tue, 13 Apr 2021 05:59:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40982) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFnO-0007Hn-Ub for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:57:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:23597) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFnN-0004Lt-9M for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:57:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-119-KmIY18kKOHiB4DcE6IgLFw-1; Tue, 13 Apr 2021 05:57:14 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6507791276; Tue, 13 Apr 2021 09:57:13 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97D555C5FD; Tue, 13 Apr 2021 09:56:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307838; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0MGpkdIKxvLwhVzcmqhAuYzCR38cQPs+gxvD9ayJdU4=; b=NVLARJKtf0FYlBzsuCMSIfQMy0BsuBHyU0I9/jJF2O/NBh+2FfknQ6TcCuoM+9CCW8fQTE EaO1T8MwdWC+X4jzVYP8p1qJljkAlk9tIFc+WJjSFfFMXVbfGJdQ7gtx60PiCm/5ML4Nfc gw7BGRyP4Ft+wjwPVpiGWW1MywqsMf0= X-MC-Unique: KmIY18kKOHiB4DcE6IgLFw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 02/13] memory: Helpers to copy/free a MemoryRegionSection Date: Tue, 13 Apr 2021 11:55:20 +0200 Message-Id: <20210413095531.25603-3-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S. Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" In case one wants to create a permanent copy of a MemoryRegionSections, one needs access to flatview_ref()/flatview_unref(). Instead of exposing these, let's just add helpers to copy/free a MemoryRegionSection and properly adjust references. Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- include/exec/memory.h | 20 ++++++++++++++++++++ softmmu/memory.c | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 38a3b41ac1..e806d0140e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1047,6 +1047,26 @@ static inline bool MemoryRegionSection_eq(MemoryRegi= onSection *a, a->nonvolatile =3D=3D b->nonvolatile; } =20 +/** + * memory_region_section_new_copy: Copy a memory region section + * + * Allocate memory for a new copy, copy the memory region section, and + * properly take a reference on all relevant members. + * + * @s: the #MemoryRegionSection to copy + */ +MemoryRegionSection *memory_region_section_new_copy(MemoryRegionSection *s= ); + +/** + * memory_region_section_new_copy: Free a copied memory region section + * + * Free a copy of a memory section created via memory_region_section_new_c= opy(). + * properly dropping references on all relevant members. + * + * @s: the #MemoryRegionSection to copy + */ +void memory_region_section_free_copy(MemoryRegionSection *s); + /** * memory_region_init: Initialize a memory region * diff --git a/softmmu/memory.c b/softmmu/memory.c index 26ea87d77a..776c7cac38 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2694,6 +2694,33 @@ MemoryRegionSection memory_region_find(MemoryRegion = *mr, return ret; } =20 +MemoryRegionSection *memory_region_section_new_copy(MemoryRegionSection *s) +{ + MemoryRegionSection *tmp =3D g_new(MemoryRegionSection, 1); + + *tmp =3D *s; + if (tmp->mr) { + memory_region_ref(tmp->mr); + } + if (tmp->fv) { + bool ret =3D flatview_ref(tmp->fv); + + g_assert(ret); + } + return tmp; +} + +void memory_region_section_free_copy(MemoryRegionSection *s) +{ + if (s->fv) { + flatview_unref(s->fv); + } + if (s->mr) { + memory_region_unref(s->mr); + } + g_free(s); +} + bool memory_region_present(MemoryRegion *container, hwaddr addr) { MemoryRegion *mr; --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308217; cv=none; d=zohomail.com; s=zohoarc; b=ZC3047VD0Tg5crj/Iw8NxAMnRKDKurvPFoX9SZrYYUgTVR1+w/maYxUPNRT7v8oap01cypCsX9P4e0rWbYuhqTXbrk0uSTn88Y2J4ag7azC3WBFuFMpz58A7Ao0ntuaGLHFaFehziBoxB+xbwHeHdVnM3hyc7NtijXNRCIy0wVw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308217; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ApNb0tXtuhjuAMjd5kbyJzjLUnCPeD+SZQYkXRqV6Rs=; b=RrKMKCjK8mEwCsFT1Sn2xYD726xrGFMCRQtEXuJGJV0WjvkK/WFhEQYzUVeSJhN8wGEXrZ9OKvyx47MDArqUUC48DgWyv83DFoDzKiztkUfCjjqUafJwVxbEXES/69Qnxl2ahXjnQDtA976eaTUcBk5oUUZXHi+47BThBL0Zi2g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308217228135.3238822843938; Tue, 13 Apr 2021 03:03:37 -0700 (PDT) Received: from localhost ([::1]:36964 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFrw-0003NX-D5 for importer@patchew.org; Tue, 13 Apr 2021 06:02:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41044) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFne-0007ef-Ah for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:57:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40339) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFnc-0004VT-In for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:57:38 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-48-k-pTt4KDNNer5VNM75j16A-1; Tue, 13 Apr 2021 05:57:34 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1CECD91271; Tue, 13 Apr 2021 09:57:33 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1B675C5FD; Tue, 13 Apr 2021 09:57:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307855; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ApNb0tXtuhjuAMjd5kbyJzjLUnCPeD+SZQYkXRqV6Rs=; b=De4UVJa5AbsR5SJv7HB8w7FTfEqXWg94caQRg5o+8CbI+VJMPicV+eCqaiJSQv8JsvyHvG hUCa6cK2Ty3qUvs8yTzPF0yTqocRpYs+V9ZbBUKCy9o6npg3gMhnY+J7idkcO+h+vNjMJg zFtxI6vh1/yaGxSNWkMTObFdwR28fqw= X-MC-Unique: k-pTt4KDNNer5VNM75j16A-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 03/13] virtio-mem: Factor out traversing unplugged ranges Date: Tue, 13 Apr 2021 11:55:21 +0200 Message-Id: <20210413095531.25603-4-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's factor out the core logic, no need to replicate. Reviewed-by: Pankaj Gupta Acked-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 86 ++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 655824ff81..471e464171 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -145,6 +145,33 @@ static bool virtio_mem_is_busy(void) return migration_in_incoming_postcopy() || !migration_is_idle(); } =20 +typedef int (*virtio_mem_range_cb)(const VirtIOMEM *vmem, void *arg, + uint64_t offset, uint64_t size); + +static int virtio_mem_for_each_unplugged_range(const VirtIOMEM *vmem, void= *arg, + virtio_mem_range_cb cb) +{ + unsigned long first_zero_bit, last_zero_bit; + uint64_t offset, size; + int ret =3D 0; + + first_zero_bit =3D find_first_zero_bit(vmem->bitmap, vmem->bitmap_size= ); + while (first_zero_bit < vmem->bitmap_size) { + offset =3D first_zero_bit * vmem->block_size; + last_zero_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, + first_zero_bit + 1) - 1; + size =3D (last_zero_bit - first_zero_bit + 1) * vmem->block_size; + + ret =3D cb(vmem, arg, offset, size); + if (ret) { + break; + } + first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, + last_zero_bit + 2); + } + return ret; +} + static bool virtio_mem_test_bitmap(VirtIOMEM *vmem, uint64_t start_gpa, uint64_t size, bool plugged) { @@ -594,33 +621,27 @@ static void virtio_mem_device_unrealize(DeviceState *= dev) ram_block_discard_require(false); } =20 -static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) +static int virtio_mem_discard_range_cb(const VirtIOMEM *vmem, void *arg, + uint64_t offset, uint64_t size) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; - unsigned long first_zero_bit, last_zero_bit; - uint64_t offset, length; int ret; =20 - /* Find consecutive unplugged blocks and discard the consecutive range= . */ - first_zero_bit =3D find_first_zero_bit(vmem->bitmap, vmem->bitmap_size= ); - while (first_zero_bit < vmem->bitmap_size) { - offset =3D first_zero_bit * vmem->block_size; - last_zero_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, - first_zero_bit + 1) - 1; - length =3D (last_zero_bit - first_zero_bit + 1) * vmem->block_size; - - ret =3D ram_block_discard_range(rb, offset, length); - if (ret) { - error_report("Unexpected error discarding RAM: %s", - strerror(-ret)); - return -EINVAL; - } - first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, - last_zero_bit + 2); + ret =3D ram_block_discard_range(rb, offset, size); + if (ret) { + error_report("Unexpected error discarding RAM: %s", strerror(-ret)= ); + return -EINVAL; } return 0; } =20 +static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) +{ + /* Make sure all memory is really discarded after migration. */ + return virtio_mem_for_each_unplugged_range(vmem, NULL, + virtio_mem_discard_range_cb= ); +} + static int virtio_mem_post_load(void *opaque, int version_id) { if (migration_in_incoming_postcopy()) { @@ -872,28 +893,19 @@ static void virtio_mem_set_block_size(Object *obj, Vi= sitor *v, const char *name, vmem->block_size =3D value; } =20 -static void virtio_mem_precopy_exclude_unplugged(VirtIOMEM *vmem) +static int virtio_mem_precopy_exclude_range_cb(const VirtIOMEM *vmem, void= *arg, + uint64_t offset, uint64_t s= ize) { void * const host =3D qemu_ram_get_host_addr(vmem->memdev->mr.ram_bloc= k); - unsigned long first_zero_bit, last_zero_bit; - uint64_t offset, length; =20 - /* - * Find consecutive unplugged blocks and exclude them from migration. - * - * Note: Blocks cannot get (un)plugged during precopy, no locking need= ed. - */ - first_zero_bit =3D find_first_zero_bit(vmem->bitmap, vmem->bitmap_size= ); - while (first_zero_bit < vmem->bitmap_size) { - offset =3D first_zero_bit * vmem->block_size; - last_zero_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, - first_zero_bit + 1) - 1; - length =3D (last_zero_bit - first_zero_bit + 1) * vmem->block_size; + qemu_guest_free_page_hint(host + offset, size); + return 0; +} =20 - qemu_guest_free_page_hint(host + offset, length); - first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, - last_zero_bit + 2); - } +static void virtio_mem_precopy_exclude_unplugged(VirtIOMEM *vmem) +{ + virtio_mem_for_each_unplugged_range(vmem, NULL, + virtio_mem_precopy_exclude_range_c= b); } =20 static int virtio_mem_precopy_notify(NotifierWithReturn *n, void *data) --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308016; cv=none; d=zohomail.com; s=zohoarc; b=g+pIY0BcUjaMvtQtDth6xWlTuQN61PUoqZ3Kl/4esGdSc4AHIVDVlxiwWLlc1az+gBBrUYdedHlRPtjCuOXSSTL/S6AU9S5K4o8DAkfSgWaRf4C2x9aTh6krFJx95iwxIaTSGxPpJ/YWhEX43tX6+WvFXByjGu3w5kdXIca8LDo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308016; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Qz6sYR65UIbSPKS0kC6ukDT8GPEPlW2PQGbTZ1SUruM=; b=SzyvGeNcWBuegGz3m675IM7Y/JENLvbVrMVOzEXMLsdqoXS3h/aWh1zvG8KNhWhO7YUFh/DTBnLBbKNdClmGUKDdx+rlLvDJ2xulPchJhuMid//XZR84tMmQT8fK45A5jMV+LdDlGeBwAIjLWHGqXDwxvTm0GxQ48DR943UOuCY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308016145942.432905051309; Tue, 13 Apr 2021 03:00:16 -0700 (PDT) Received: from localhost ([::1]:59756 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFq6-00015s-0r for importer@patchew.org; Tue, 13 Apr 2021 06:00:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41150) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFo1-0007wo-H0 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56592) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFnz-0004hL-Oz for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:01 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-439-_UPqCb1jO42ymyvFqdI0ZA-1; Tue, 13 Apr 2021 05:57:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 355D310053E7; Tue, 13 Apr 2021 09:57:54 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6EB9C5C3E4; Tue, 13 Apr 2021 09:57:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qz6sYR65UIbSPKS0kC6ukDT8GPEPlW2PQGbTZ1SUruM=; b=ZSzSPfPhfrb6xUQOyvy46kW+5wI5Tu2vqzKlHhvlyJA8ZgFYqaxnickXHhNAYgzkw/ycE1 OiQkTXjHmbH8s8J8+ACtWg4mYGP4+HpqssYoZuwQ4CuY7q1E9kMKCRbDpv61PBvVmQewDS mMvGk0SVWaowRdgQ1ApOUTA+85caQQg= X-MC-Unique: _UPqCb1jO42ymyvFqdI0ZA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 04/13] virtio-mem: Don't report errors when ram_block_discard_range() fails Date: Tue, 13 Apr 2021 11:55:22 +0200 Message-Id: <20210413095531.25603-5-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S. Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Any errors are unexpected and ram_block_discard_range() already properly prints errors. Let's stop manually reporting errors. Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 471e464171..bbe42ad83b 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -246,17 +246,14 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem= , uint64_t start_gpa, uint64_t size, bool plug) { const uint64_t offset =3D start_gpa - vmem->addr; - int ret; + RAMBlock *rb =3D vmem->memdev->mr.ram_block; =20 if (virtio_mem_is_busy()) { return -EBUSY; } =20 if (!plug) { - ret =3D ram_block_discard_range(vmem->memdev->mr.ram_block, offset= , size); - if (ret) { - error_report("Unexpected error discarding RAM: %s", - strerror(-ret)); + if (ram_block_discard_range(rb, offset, size)) { return -EBUSY; } } @@ -345,15 +342,12 @@ static void virtio_mem_resize_usable_region(VirtIOMEM= *vmem, static int virtio_mem_unplug_all(VirtIOMEM *vmem) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; - int ret; =20 if (virtio_mem_is_busy()) { return -EBUSY; } =20 - ret =3D ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb)); - if (ret) { - error_report("Unexpected error discarding RAM: %s", strerror(-ret)= ); + if (ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb))) { return -EBUSY; } bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); @@ -625,14 +619,8 @@ static int virtio_mem_discard_range_cb(const VirtIOMEM= *vmem, void *arg, uint64_t offset, uint64_t size) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; - int ret; =20 - ret =3D ram_block_discard_range(rb, offset, size); - if (ret) { - error_report("Unexpected error discarding RAM: %s", strerror(-ret)= ); - return -EINVAL; - } - return 0; + return ram_block_discard_range(rb, offset, size) ? -EINVAL : 0; } =20 static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308325; cv=none; d=zohomail.com; s=zohoarc; b=jxO2oJrGl1aLwRzYlJNPQcEjkFu7toNHSP3W0LS5XPTzzXA3PxdlbRIZemd0QFnovYWSw8Jm5cbPLBEq8V+f9NMfid0bBgoBtGjxuZ3jAUFyxvlSSMgGPMwOQsCP5I1fxoC9SGJNL0qF73TIpBAQOYTbiY2DLqgtOCKqeEYcGMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308325; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cR2rSK+ujh+Sv2mmrZbqLMcR535n2RRR8xM4067PLPU=; b=Oaciafw8nphaIBZKOaDAlvbtIrlUioGWj5Nq6OUTaKLfEb5MXnk9fpdcTJhOn90eqx5OohoXgPy8MQJigEy4HcqF48fOOfNOQ2sKjl6PgLtCcSqbmVkawyO72MUctOojphpK+T0qIE3zPa8fZEhXj6pvzZM46zVXN1YOQN6C0SY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308325137514.6526274201681; Tue, 13 Apr 2021 03:05:25 -0700 (PDT) Received: from localhost ([::1]:50034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFv8-0000B6-AW for importer@patchew.org; Tue, 13 Apr 2021 06:05:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41250) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoM-00082b-S9 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26017) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoJ-0004rM-DA for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-248-5Pxyy85oM0q_VxAHKpWuQA-1; Tue, 13 Apr 2021 05:58:15 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E14EC8030C9; Tue, 13 Apr 2021 09:58:13 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DF5C5C8AB; Tue, 13 Apr 2021 09:57:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307898; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cR2rSK+ujh+Sv2mmrZbqLMcR535n2RRR8xM4067PLPU=; b=JHuKZtoLqJRg2DM7Wj/b1GCh/xTtCZyPZc7jXjpR/m8A4AkMablL/01F2AXhcWWPwWacE6 J3h0Ggo+3nRboVnLLAQg6SdyDvq5CBuhH88gdssF3NMg0i955xlRPkWbqC5BHiQpLPnqxI CHpQHfr1AwtirhUeJzV8oNzhhcAnvKI= X-MC-Unique: 5Pxyy85oM0q_VxAHKpWuQA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 05/13] virtio-mem: Implement RamDiscardManager interface Date: Tue, 13 Apr 2021 11:55:23 +0200 Message-Id: <20210413095531.25603-6-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's properly notify when (un)plugging blocks, after discarding memory and before allowing the guest to consume memory. Handle errors from notifiers gracefully (e.g., no remaining VFIO mappings) when plugging, rolling back the change and telling the guest that the VM is busy. One special case to take care of is replaying all notifications after restoring the vmstate. The device starts out with all memory discarded, so after loading the vmstate, we have to notify about all plugged blocks. Acked-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 288 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-mem.h | 3 + 2 files changed, 288 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index bbe42ad83b..e209b56057 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -172,7 +172,146 @@ static int virtio_mem_for_each_unplugged_range(const = VirtIOMEM *vmem, void *arg, return ret; } =20 -static bool virtio_mem_test_bitmap(VirtIOMEM *vmem, uint64_t start_gpa, +/* + * Adjust the memory section to cover the intersection with the given rang= e. + * + * Returns false if the intersection is empty, otherwise returns true. + */ +static bool virito_mem_intersect_memory_section(MemoryRegionSection *s, + uint64_t offset, uint64_t = size) +{ + uint64_t start =3D MAX(s->offset_within_region, offset); + uint64_t end =3D MIN(s->offset_within_region + int128_get64(s->size), + offset + size); + + if (end <=3D start) { + return false; + } + + s->offset_within_address_space +=3D start - s->offset_within_region; + s->offset_within_region =3D start; + s->size =3D int128_make64(end - start); + return true; +} + +typedef int (*virtio_mem_section_cb)(MemoryRegionSection *s, void *arg); + +static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem, + MemoryRegionSection *s, + void *arg, + virtio_mem_section_cb cb) +{ + unsigned long first_bit, last_bit; + uint64_t offset, size; + int ret =3D 0; + + first_bit =3D s->offset_within_region / vmem->bitmap_size; + first_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, first_bit= ); + while (first_bit < vmem->bitmap_size) { + MemoryRegionSection tmp =3D *s; + + offset =3D first_bit * vmem->block_size; + last_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_size, + first_bit + 1) - 1; + size =3D (last_bit - first_bit + 1) * vmem->block_size; + + if (!virito_mem_intersect_memory_section(&tmp, offset, size)) { + break; + } + ret =3D cb(&tmp, arg); + if (ret) { + break; + } + first_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, + last_bit + 2); + } + return ret; +} + +static int virtio_mem_notify_populate_cb(MemoryRegionSection *s, void *arg) +{ + RamDiscardListener *rdl =3D arg; + + return rdl->notify_populate(rdl, s); +} + +static int virtio_mem_notify_discard_cb(MemoryRegionSection *s, void *arg) +{ + RamDiscardListener *rdl =3D arg; + + rdl->notify_discard(rdl, s); + return 0; +} + +static void virtio_mem_notify_unplug(VirtIOMEM *vmem, uint64_t offset, + uint64_t size) +{ + RamDiscardListener *rdl; + + QLIST_FOREACH(rdl, &vmem->rdl_list, next) { + MemoryRegionSection tmp =3D *rdl->section; + + if (!virito_mem_intersect_memory_section(&tmp, offset, size)) { + continue; + } + rdl->notify_discard(rdl, &tmp); + } +} + +static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint64_t offset, + uint64_t size) +{ + RamDiscardListener *rdl, *rdl2; + int ret =3D 0; + + QLIST_FOREACH(rdl, &vmem->rdl_list, next) { + MemoryRegionSection tmp =3D *rdl->section; + + if (!virito_mem_intersect_memory_section(&tmp, offset, size)) { + continue; + } + ret =3D rdl->notify_populate(rdl, &tmp); + if (ret) { + break; + } + } + + if (ret) { + /* Notify all already-notified listeners. */ + QLIST_FOREACH(rdl2, &vmem->rdl_list, next) { + MemoryRegionSection tmp =3D *rdl->section; + + if (rdl2 =3D=3D rdl) { + break; + } + if (!virito_mem_intersect_memory_section(&tmp, offset, size)) { + continue; + } + rdl2->notify_discard(rdl2, &tmp); + } + } + return ret; +} + +static void virtio_mem_notify_unplug_all(VirtIOMEM *vmem) +{ + RamDiscardListener *rdl; + + if (!vmem->size) { + return; + } + + QLIST_FOREACH(rdl, &vmem->rdl_list, next) { + if (rdl->double_discard_supported) { + rdl->notify_discard(rdl, rdl->section); + } else { + virtio_mem_for_each_plugged_section(vmem, rdl->section, rdl, + virtio_mem_notify_discard_= cb); + } + } +} + +static bool virtio_mem_test_bitmap(const VirtIOMEM *vmem, uint64_t start_g= pa, uint64_t size, bool plugged) { const unsigned long first_bit =3D (start_gpa - vmem->addr) / vmem->blo= ck_size; @@ -225,7 +364,8 @@ static void virtio_mem_send_response_simple(VirtIOMEM *= vmem, virtio_mem_send_response(vmem, elem, &resp); } =20 -static bool virtio_mem_valid_range(VirtIOMEM *vmem, uint64_t gpa, uint64_t= size) +static bool virtio_mem_valid_range(const VirtIOMEM *vmem, uint64_t gpa, + uint64_t size) { if (!QEMU_IS_ALIGNED(gpa, vmem->block_size)) { return false; @@ -256,6 +396,11 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem,= uint64_t start_gpa, if (ram_block_discard_range(rb, offset, size)) { return -EBUSY; } + virtio_mem_notify_unplug(vmem, offset, size); + } else if (virtio_mem_notify_plug(vmem, offset, size)) { + /* Could be a mapping attempt resulted in memory getting populated= . */ + ram_block_discard_range(vmem->memdev->mr.ram_block, offset, size); + return -EBUSY; } virtio_mem_set_bitmap(vmem, start_gpa, size, plug); return 0; @@ -350,6 +495,8 @@ static int virtio_mem_unplug_all(VirtIOMEM *vmem) if (ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb))) { return -EBUSY; } + virtio_mem_notify_unplug_all(vmem); + bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); if (vmem->size) { vmem->size =3D 0; @@ -598,6 +745,13 @@ static void virtio_mem_device_realize(DeviceState *dev= , Error **errp) vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem)); qemu_register_reset(virtio_mem_system_reset, vmem); precopy_add_notifier(&vmem->precopy_notifier); + + /* + * Set ourselves as RamDiscardManager before the plug handler maps the + * memory region and exposes it via an address space. + */ + memory_region_set_ram_discard_manager(&vmem->memdev->mr, + RAM_DISCARD_MANAGER(vmem)); } =20 static void virtio_mem_device_unrealize(DeviceState *dev) @@ -605,6 +759,11 @@ static void virtio_mem_device_unrealize(DeviceState *d= ev) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOMEM *vmem =3D VIRTIO_MEM(dev); =20 + /* + * The unplug handler unmapped the memory region, it cannot be + * found via an address space anymore. Unset ourselves. + */ + memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL); precopy_remove_notifier(&vmem->precopy_notifier); qemu_unregister_reset(virtio_mem_system_reset, vmem); vmstate_unregister_ram(&vmem->memdev->mr, DEVICE(vmem)); @@ -632,11 +791,27 @@ static int virtio_mem_restore_unplugged(VirtIOMEM *vm= em) =20 static int virtio_mem_post_load(void *opaque, int version_id) { + VirtIOMEM *vmem =3D VIRTIO_MEM(opaque); + RamDiscardListener *rdl; + int ret; + + /* + * We started out with all memory discarded and our memory region is m= apped + * into an address space. Replay, now that we updated the bitmap. + */ + QLIST_FOREACH(rdl, &vmem->rdl_list, next) { + ret =3D virtio_mem_for_each_plugged_section(vmem, rdl->section, rd= l, + virtio_mem_notify_populat= e_cb); + if (ret) { + return ret; + } + } + if (migration_in_incoming_postcopy()) { return 0; } =20 - return virtio_mem_restore_unplugged(VIRTIO_MEM(opaque)); + return virtio_mem_restore_unplugged(vmem); } =20 typedef struct VirtIOMEMMigSanityChecks { @@ -921,6 +1096,7 @@ static void virtio_mem_instance_init(Object *obj) =20 notifier_list_init(&vmem->size_change_notifiers); vmem->precopy_notifier.notify =3D virtio_mem_precopy_notify; + QLIST_INIT(&vmem->rdl_list); =20 object_property_add(obj, VIRTIO_MEM_SIZE_PROP, "size", virtio_mem_get_= size, NULL, NULL, NULL); @@ -940,11 +1116,107 @@ static Property virtio_mem_properties[] =3D { DEFINE_PROP_END_OF_LIST(), }; =20 +static uint64_t virtio_mem_rdm_get_min_granularity(const RamDiscardManager= *rdm, + const MemoryRegion *mr) +{ + const VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); + + g_assert(mr =3D=3D &vmem->memdev->mr); + return vmem->block_size; +} + +static bool virtio_mem_rdm_is_populated(const RamDiscardManager *rdm, + const MemoryRegionSection *s) +{ + const VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); + uint64_t start_gpa =3D vmem->addr + s->offset_within_region; + uint64_t end_gpa =3D start_gpa + int128_get64(s->size); + + g_assert(s->mr =3D=3D &vmem->memdev->mr); + + start_gpa =3D QEMU_ALIGN_DOWN(start_gpa, vmem->block_size); + end_gpa =3D QEMU_ALIGN_UP(end_gpa, vmem->block_size); + + if (!virtio_mem_valid_range(vmem, start_gpa, end_gpa - start_gpa)) { + return false; + } + + return virtio_mem_test_bitmap(vmem, start_gpa, end_gpa - start_gpa, tr= ue); +} + +struct VirtIOMEMReplayData { + void *fn; + void *opaque; +}; + +static int virtio_mem_rdm_replay_populated_cb(MemoryRegionSection *s, void= *arg) +{ + struct VirtIOMEMReplayData *data =3D arg; + + return ((ReplayRamPopulate)data->fn)(s, data->opaque); +} + +static int virtio_mem_rdm_replay_populated(const RamDiscardManager *rdm, + MemoryRegionSection *s, + ReplayRamPopulate replay_fn, + void *opaque) +{ + const VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); + struct VirtIOMEMReplayData data =3D { + .fn =3D replay_fn, + .opaque =3D opaque, + }; + + g_assert(s->mr =3D=3D &vmem->memdev->mr); + return virtio_mem_for_each_plugged_section(vmem, s, &data, + virtio_mem_rdm_replay_populate= d_cb); +} + +static void virtio_mem_rdm_register_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl, + MemoryRegionSection *s) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); + int ret; + + g_assert(s->mr =3D=3D &vmem->memdev->mr); + rdl->section =3D memory_region_section_new_copy(s); + + QLIST_INSERT_HEAD(&vmem->rdl_list, rdl, next); + ret =3D virtio_mem_for_each_plugged_section(vmem, rdl->section, rdl, + virtio_mem_notify_populate_c= b); + if (ret) { + error_report("%s: Replaying plugged ranges failed: %s", __func__, + strerror(-ret)); + } +} + +static void virtio_mem_rdm_unregister_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); + + g_assert(rdl->section->mr =3D=3D &vmem->memdev->mr); + if (vmem->size) { + if (rdl->double_discard_supported) { + rdl->notify_discard(rdl, rdl->section); + } else { + virtio_mem_for_each_plugged_section(vmem, rdl->section, rdl, + virtio_mem_notify_discard_= cb); + } + } + + memory_region_section_free_copy(rdl->section); + rdl->section =3D NULL; + QLIST_REMOVE(rdl, next); +} + static void virtio_mem_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); VirtIOMEMClass *vmc =3D VIRTIO_MEM_CLASS(klass); + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_CLASS(klass); =20 device_class_set_props(dc, virtio_mem_properties); dc->vmsd =3D &vmstate_virtio_mem; @@ -960,6 +1232,12 @@ static void virtio_mem_class_init(ObjectClass *klass,= void *data) vmc->get_memory_region =3D virtio_mem_get_memory_region; vmc->add_size_change_notifier =3D virtio_mem_add_size_change_notifier; vmc->remove_size_change_notifier =3D virtio_mem_remove_size_change_not= ifier; + + rdmc->get_min_granularity =3D virtio_mem_rdm_get_min_granularity; + rdmc->is_populated =3D virtio_mem_rdm_is_populated; + rdmc->replay_populated =3D virtio_mem_rdm_replay_populated; + rdmc->register_listener =3D virtio_mem_rdm_register_listener; + rdmc->unregister_listener =3D virtio_mem_rdm_unregister_listener; } =20 static const TypeInfo virtio_mem_info =3D { @@ -969,6 +1247,10 @@ static const TypeInfo virtio_mem_info =3D { .instance_init =3D virtio_mem_instance_init, .class_init =3D virtio_mem_class_init, .class_size =3D sizeof(VirtIOMEMClass), + .interfaces =3D (InterfaceInfo[]) { + { TYPE_RAM_DISCARD_MANAGER }, + { } + }, }; =20 static void virtio_register_types(void) diff --git a/include/hw/virtio/virtio-mem.h b/include/hw/virtio/virtio-mem.h index 4eeb82d5dd..9a6e348fa2 100644 --- a/include/hw/virtio/virtio-mem.h +++ b/include/hw/virtio/virtio-mem.h @@ -67,6 +67,9 @@ struct VirtIOMEM { =20 /* don't migrate unplugged memory */ NotifierWithReturn precopy_notifier; + + /* listeners to notify on plug/unplug activity. */ + QLIST_HEAD(, RamDiscardListener) rdl_list; }; =20 struct VirtIOMEMClass { --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308027; cv=none; d=zohomail.com; s=zohoarc; b=h2z4207U9bMZbr8hQSTrFpxERe8o7vpV3YDJkoMEajLMBkxTMkOGIQ2Y4k7PZ0QxRxpFB+t/YulBARDgwIChVwxyqCevgJo41+i5V4B0mICFfVuyOQ5yiKKyFsJC9Vc5w19tvtTL5B9T2W9ylyHTdU4IJoXHAU3CULtw3d0VL4k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308027; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yOStPQQt0mqHHSgBcrb78nNDmXoRuFFG9gm7xqarcq0=; b=S2jRrgNYM4PhoTnL4YlT/IAtFT652QWnmQl2p+aGdsxKWiKkvCkejeUmQ1laiFAG9Lz6cqocqKehjhRhJC4iRlx8Kctq8fX6KhOtEvX6b3N3J2HMbr8HK9jwFD0YbqBVmYVQ1C1rkPsnV3OWhBs6B8oZ67LmsumEv0cmxV+w+Gs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308027543952.9508693606442; Tue, 13 Apr 2021 03:00:27 -0700 (PDT) Received: from localhost ([::1]:60768 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFqJ-0001Uq-Tg for importer@patchew.org; Tue, 13 Apr 2021 06:00:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41294) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoT-0008Dj-Uh for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53140) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoR-0004wo-JC for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:29 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-17-xiFq6xoVMxS7SiYcPga_tw-1; Tue, 13 Apr 2021 05:58:25 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0FE6A1922960; Tue, 13 Apr 2021 09:58:24 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4AF9A5E1A4; Tue, 13 Apr 2021 09:58:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yOStPQQt0mqHHSgBcrb78nNDmXoRuFFG9gm7xqarcq0=; b=gzZDhQaIQHBD/gjFEiPfz8Uzy6v0KzFcvZYKuijE2w/66NtFXKEXJ6vjytLggOhtOitRXt LzblUPJhAqMlJ4BUdKvWt7xogTMoByEdGJaXJf20P2FMkexv4jzYFCbLqbLHsfhrz5TH3U lxlyaA23Gs2G4H2PQvnjL09q7Zx/T+Y= X-MC-Unique: xiFq6xoVMxS7SiYcPga_tw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 06/13] vfio: Support for RamDiscardManager in the !vIOMMU case Date: Tue, 13 Apr 2021 11:55:24 +0200 Message-Id: <20210413095531.25603-7-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Implement support for RamDiscardManager, to prepare for virtio-mem support. Instead of mapping the whole memory section, we only map "populated" parts and update the mapping when notified about discarding/population of memory via the RamDiscardListener. Similarly, when syncing the dirty bitmaps, sync only the actually mapped (populated) parts by replaying via the notifier. Using virtio-mem with vfio is still blocked via ram_block_discard_disable()/ram_block_discard_require() after this patch. Reviewed-by: Alex Williamson Acked-by: Alex Williamson Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/vfio/common.c | 164 ++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-common.h | 11 +++ 2 files changed, 175 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index ae5654fcdb..5af7755227 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -649,6 +649,110 @@ out: rcu_read_unlock(); } =20 +static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl, + MemoryRegionSection *section) +{ + VFIORamDiscardListener *vrdl =3D container_of(rdl, VFIORamDiscardListe= ner, + listener); + const hwaddr size =3D int128_get64(section->size); + const hwaddr iova =3D section->offset_within_address_space; + int ret; + + /* Unmap with a single call. */ + ret =3D vfio_dma_unmap(vrdl->container, iova, size , NULL); + if (ret) { + error_report("%s: vfio_dma_unmap() failed: %s", __func__, + strerror(-ret)); + } +} + +static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl, + MemoryRegionSection *section) +{ + VFIORamDiscardListener *vrdl =3D container_of(rdl, VFIORamDiscardListe= ner, + listener); + const hwaddr end =3D section->offset_within_region + + int128_get64(section->size); + hwaddr start, next, iova; + void *vaddr; + int ret; + + /* + * Map in (aligned within memory region) minimum granularity, so we can + * unmap in minimum granularity later. + */ + for (start =3D section->offset_within_region; start < end; start =3D n= ext) { + next =3D ROUND_UP(start + 1, vrdl->granularity); + next =3D MIN(next, end); + + iova =3D start - section->offset_within_region + + section->offset_within_address_space; + vaddr =3D memory_region_get_ram_ptr(section->mr) + start; + + ret =3D vfio_dma_map(vrdl->container, iova, next - start, + vaddr, section->readonly); + if (ret) { + /* Rollback */ + vfio_ram_discard_notify_discard(rdl, section); + return ret; + } + } + return 0; +} + +static void vfio_register_ram_discard_listener(VFIOContainer *container, + MemoryRegionSection *sectio= n) +{ + RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(secti= on->mr); + VFIORamDiscardListener *vrdl; + + /* Ignore some corner cases not relevant in practice. */ + g_assert(QEMU_IS_ALIGNED(section->offset_within_region, TARGET_PAGE_SI= ZE)); + g_assert(QEMU_IS_ALIGNED(section->offset_within_address_space, + TARGET_PAGE_SIZE)); + g_assert(QEMU_IS_ALIGNED(int128_get64(section->size), TARGET_PAGE_SIZE= )); + + vrdl =3D g_new0(VFIORamDiscardListener, 1); + vrdl->container =3D container; + vrdl->mr =3D section->mr; + vrdl->offset_within_address_space =3D section->offset_within_address_s= pace; + vrdl->size =3D int128_get64(section->size); + vrdl->granularity =3D ram_discard_manager_get_min_granularity(rdm, + section->m= r); + + g_assert(vrdl->granularity && is_power_of_2(vrdl->granularity)); + g_assert(vrdl->granularity >=3D 1 << ctz64(container->pgsizes)); + + ram_discard_listener_init(&vrdl->listener, + vfio_ram_discard_notify_populate, + vfio_ram_discard_notify_discard, true); + ram_discard_manager_register_listener(rdm, &vrdl->listener, section); + QLIST_INSERT_HEAD(&container->vrdl_list, vrdl, next); +} + +static void vfio_unregister_ram_discard_listener(VFIOContainer *container, + MemoryRegionSection *sect= ion) +{ + RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(secti= on->mr); + VFIORamDiscardListener *vrdl =3D NULL; + + QLIST_FOREACH(vrdl, &container->vrdl_list, next) { + if (vrdl->mr =3D=3D section->mr && + vrdl->offset_within_address_space =3D=3D + section->offset_within_address_space) { + break; + } + } + + if (!vrdl) { + hw_error("vfio: Trying to unregister missing RAM discard listener"= ); + } + + ram_discard_manager_unregister_listener(rdm, &vrdl->listener); + QLIST_REMOVE(vrdl, next); + g_free(vrdl); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -810,6 +914,16 @@ static void vfio_listener_region_add(MemoryListener *l= istener, =20 /* Here we assume that memory_region_is_ram(section->mr)=3D=3Dtrue */ =20 + /* + * For RAM memory regions with a RamDiscardManager, we only want to ma= p the + * actually populated parts - and update the mapping whenever we're no= tified + * about changes. + */ + if (memory_region_has_ram_discard_manager(section->mr)) { + vfio_register_ram_discard_listener(container, section); + return; + } + vaddr =3D memory_region_get_ram_ptr(section->mr) + section->offset_within_region + (iova - section->offset_within_address_space); @@ -947,6 +1061,10 @@ static void vfio_listener_region_del(MemoryListener *= listener, =20 pgmask =3D (1ULL << ctz64(hostwin->iova_pgsizes)) - 1; try_unmap =3D !((iova & pgmask) || (int128_get64(llsize) & pgmask)= ); + } else if (memory_region_has_ram_discard_manager(section->mr)) { + vfio_unregister_ram_discard_listener(container, section); + /* Unregistering will trigger an unmap. */ + try_unmap =3D false; } =20 if (try_unmap) { @@ -1108,6 +1226,49 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifie= r *n, IOMMUTLBEntry *iotlb) rcu_read_unlock(); } =20 +static int vfio_ram_discard_get_dirty_bitmap(MemoryRegionSection *section, + void *opaque) +{ + const hwaddr size =3D int128_get64(section->size); + const hwaddr iova =3D section->offset_within_address_space; + const ram_addr_t ram_addr =3D memory_region_get_ram_addr(section->mr) + + section->offset_within_region; + VFIORamDiscardListener *vrdl =3D opaque; + + /* + * Sync the whole mapped region (spanning multiple individual mappings) + * in one go. + */ + return vfio_get_dirty_bitmap(vrdl->container, iova, size, ram_addr); +} + +static int vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainer *cont= ainer, + MemoryRegionSection *se= ction) +{ + RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(secti= on->mr); + VFIORamDiscardListener *vrdl =3D NULL; + + QLIST_FOREACH(vrdl, &container->vrdl_list, next) { + if (vrdl->mr =3D=3D section->mr && + vrdl->offset_within_address_space =3D=3D + section->offset_within_address_space) { + break; + } + } + + if (!vrdl) { + hw_error("vfio: Trying to sync missing RAM discard listener"); + } + + /* + * We only want/can synchronize the bitmap for actually mapped parts - + * which correspond to populated parts. Replay all populated parts. + */ + return ram_discard_manager_replay_populated(rdm, section, + vfio_ram_discard_get_dirty_b= itmap, + &vrdl); +} + static int vfio_sync_dirty_bitmap(VFIOContainer *container, MemoryRegionSection *section) { @@ -1139,6 +1300,8 @@ static int vfio_sync_dirty_bitmap(VFIOContainer *cont= ainer, } } return 0; + } else if (memory_region_has_ram_discard_manager(section->mr)) { + return vfio_sync_ram_discard_listener_dirty_bitmap(container, sect= ion); } =20 ram_addr =3D memory_region_get_ram_addr(section->mr) + @@ -1770,6 +1933,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, container->dirty_pages_supported =3D false; QLIST_INIT(&container->giommu_list); QLIST_INIT(&container->hostwin_list); + QLIST_INIT(&container->vrdl_list); =20 ret =3D vfio_init_container(container, group->fd, errp); if (ret) { diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 6141162d7a..681432213d 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -91,6 +91,7 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; + QLIST_HEAD(, VFIORamDiscardListener) vrdl_list; QLIST_ENTRY(VFIOContainer) next; } VFIOContainer; =20 @@ -102,6 +103,16 @@ typedef struct VFIOGuestIOMMU { QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; } VFIOGuestIOMMU; =20 +typedef struct VFIORamDiscardListener { + VFIOContainer *container; + MemoryRegion *mr; + hwaddr offset_within_address_space; + hwaddr size; + uint64_t granularity; + RamDiscardListener listener; + QLIST_ENTRY(VFIORamDiscardListener) next; +} VFIORamDiscardListener; + typedef struct VFIOHostDMAWindow { hwaddr min_iova; hwaddr max_iova; --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308256; cv=none; d=zohomail.com; s=zohoarc; b=QfnePDGHJyLqsWzzID6L7KT+/Rdo83hYLJQzoHPq+AbfPo6xk/QL+BolSgOvMQHgSyURGgHhGXhfOU4TSmVTK7TaP4qbE8SCcU5vX1J8UGByhNRfmSz7SEPExgX9+T8AJ0T8u9y7O23/EIM0n6A7G4pvbnmNj3KrRsEiCjP4pug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308256; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=xhSOp+M45oRyu9zB/EFfHM8w5lOudfNZ6i0iN92mlKw=; b=Ki7qcM+p/d/n7ekGrn6D6JtkkzCQ39pXt6Ywd58KxK8av1DEobPQ7/CnmMSUVZJ0Fw7p4ec/t5Yr0xTTlHE2UL7J7zi8sYzhsY1dMr4VU5RmY/DqwMC5kDf/f1u1urnm/RBHaTvEdFOAZbj0kJGxta68CTEjow7/Yt6OZOt1kFQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161830825614381.21151785989707; Tue, 13 Apr 2021 03:04:16 -0700 (PDT) Received: from localhost ([::1]:45192 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFu3-0006gN-Bb for importer@patchew.org; Tue, 13 Apr 2021 06:04:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41312) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFog-00009o-PB for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:48372) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoe-00053t-VB for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:42 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-135-oniQCl5JOCi1d4NmQFbhwg-1; Tue, 13 Apr 2021 05:58:38 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5A60B10054F6; Tue, 13 Apr 2021 09:58:37 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C5EE5C3E4; Tue, 13 Apr 2021 09:58:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307920; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xhSOp+M45oRyu9zB/EFfHM8w5lOudfNZ6i0iN92mlKw=; b=KL9tPNSOj6jT+kmm7grKc91RXs9t8kNZlMibkMHtEbyk5cz6AGEp6il5WWji9JcqqyXu6T bmOi5l6d4cPvAP+AibN2eU+SCi6XEdnLz6v3Ng02PtVnKIMBp01YwOBkWQHcgt96HAdhXX 5uc0PdCUkac4rsMmT39/WF3XJmL8Ebw= X-MC-Unique: oniQCl5JOCi1d4NmQFbhwg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 07/13] vfio: Query and store the maximum number of possible DMA mappings Date: Tue, 13 Apr 2021 11:55:25 +0200 Message-Id: <20210413095531.25603-8-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's query the maximum number of possible DMA mappings by querying the available mappings when creating the container (before any mappings are created). We'll use this informaton soon to perform some sanity checks and warn the user. Reviewed-by: Alex Williamson Acked-by: Alex Williamson Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/vfio/common.c | 4 ++++ include/hw/vfio/vfio-common.h | 1 + 2 files changed, 5 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 5af7755227..79628d60ae 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1931,6 +1931,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, container->fd =3D fd; container->error =3D NULL; container->dirty_pages_supported =3D false; + container->dma_max_mappings =3D 0; QLIST_INIT(&container->giommu_list); QLIST_INIT(&container->hostwin_list); QLIST_INIT(&container->vrdl_list); @@ -1962,7 +1963,10 @@ static int vfio_connect_container(VFIOGroup *group, = AddressSpace *as, vfio_host_win_add(container, 0, (hwaddr)-1, info->iova_pgsizes); container->pgsizes =3D info->iova_pgsizes; =20 + /* The default in the kernel ("dma_entry_limit") is 65535. */ + container->dma_max_mappings =3D 65535; if (!ret) { + vfio_get_info_dma_avail(info, &container->dma_max_mappings); vfio_get_iommu_info_migration(container, info); } g_free(info); diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 681432213d..8af11b0a76 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -88,6 +88,7 @@ typedef struct VFIOContainer { uint64_t dirty_pgsizes; uint64_t max_dirty_bitmap_size; unsigned long pgsizes; + unsigned int dma_max_mappings; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308199; cv=none; d=zohomail.com; s=zohoarc; b=MX3vI0WhySc4ZV3s1srkLPw6E4umQ74kSCTSBZVTCvVDM3e29zf5AjXyP2ViRQOdD3pU4uPKgDR26QlceBVl5sbfJsio5rPu7pkl0xvujZijo2A8eLZLEchVPzdkZ7RXrV5EfQtHn1zoGMPULiP/zAywHLtfMrxd/2o0N30/+mA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308199; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=OF2Avqhi1NxubbIevtnHklbZmpcAhaxAAg2V6MJw3Es=; b=WkeEOUZBxXhoP6j2z2DueITO85YdgL3YYseyTMRoiulVRNsSWHW/ynKd/Ff+8YNmxNjiMSJaipW+F3DHi/gk+jOWcVmmPDhVuwPobiR71xrHnu1u56IJhcUO8hkdUC8Tqo1g9lO8sWNBT/RxMNNrPXbdlweoTx0m/jp74BwbGIY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308199244279.60418870103433; Tue, 13 Apr 2021 03:03:19 -0700 (PDT) Received: from localhost ([::1]:41088 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFt7-00052D-N7 for importer@patchew.org; Tue, 13 Apr 2021 06:03:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41326) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFor-0000Ff-0a for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:52639) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFoo-0005A0-Pl for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:58:52 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-218-aF9ya0ZnM2KbHo44ke_vnA-1; Tue, 13 Apr 2021 05:58:48 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9607C9127B; Tue, 13 Apr 2021 09:58:47 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id B95BF5E1A4; Tue, 13 Apr 2021 09:58:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307930; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OF2Avqhi1NxubbIevtnHklbZmpcAhaxAAg2V6MJw3Es=; b=SY98rnZLzl4gExUkFyEFowWjGN2BcKbqOhZAZU4fCFUwqwiROg3ml2GdcX1zAbVDMIhygf hRAFf8b2AB11DuE8ud90YC7DNbKambCblyPmHvlopzHkFujsDWg/FM48/OPKHqC6DEnKMQ uM6anOxb2r5Qpyq7TpW8xbzFvoZi1wM= X-MC-Unique: aF9ya0ZnM2KbHo44ke_vnA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 08/13] vfio: Sanity check maximum number of DMA mappings with RamDiscardManager Date: Tue, 13 Apr 2021 11:55:26 +0200 Message-Id: <20210413095531.25603-9-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Although RamDiscardManager can handle running into the maximum number of DMA mappings by propagating errors when creating a DMA mapping, we want to sanity check and warn the user early that there is a theoretical setup issue and that virtio-mem might not be able to provide as much memory towards a VM as desired. As suggested by Alex, let's use the number of KVM memory slots to guess how many other mappings we might see over time. Acked-by: Alex Williamson Reviewed-by: Alex Williamson Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/vfio/common.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 79628d60ae..f8a2fe8441 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -728,6 +728,49 @@ static void vfio_register_ram_discard_listener(VFIOCon= tainer *container, vfio_ram_discard_notify_discard, true); ram_discard_manager_register_listener(rdm, &vrdl->listener, section); QLIST_INSERT_HEAD(&container->vrdl_list, vrdl, next); + + /* + * Sanity-check if we have a theoretically problematic setup where we = could + * exceed the maximum number of possible DMA mappings over time. We as= sume + * that each mapped section in the same address space as a RamDiscardM= anager + * section consumes exactly one DMA mapping, with the exception of + * RamDiscardManager sections; i.e., we don't expect to have gIOMMU se= ctions + * in the same address space as RamDiscardManager sections. + * + * We assume that each section in the address space consumes one memsl= ot. + * We take the number of KVM memory slots as a best guess for the maxi= mum + * number of sections in the address space we could have over time, + * also consuming DMA mappings. + */ + if (container->dma_max_mappings) { + unsigned int vrdl_count =3D 0, vrdl_mappings =3D 0, max_memslots = =3D 512; + +#ifdef CONFIG_KVM + if (kvm_enabled()) { + max_memslots =3D kvm_get_max_memslots(); + } +#endif + + QLIST_FOREACH(vrdl, &container->vrdl_list, next) { + hwaddr start, end; + + start =3D QEMU_ALIGN_DOWN(vrdl->offset_within_address_space, + vrdl->granularity); + end =3D ROUND_UP(vrdl->offset_within_address_space + vrdl->siz= e, + vrdl->granularity); + vrdl_mappings +=3D (end - start) / vrdl->granularity; + vrdl_count++; + } + + if (vrdl_mappings + max_memslots - vrdl_count > + container->dma_max_mappings) { + warn_report("%s: possibly running out of DMA mappings. E.g., t= ry" + " increasing the 'block-size' of virtio-mem devies= ." + " Maximum possible DMA mappings: %d, Maximum possi= ble" + " memslots: %d", __func__, container->dma_max_mapp= ings, + max_memslots); + } + } } =20 static void vfio_unregister_ram_discard_listener(VFIOContainer *container, --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308419; cv=none; d=zohomail.com; s=zohoarc; b=j/njc+umJguAts4kyiSNy7gvBiEjDgYweo0d4cvAH8gX4j1LpV3pFAR4sVerBce3CAvLl63xsomXqt1ekaJno7FSXvrBldH3xYBr4vdlub3XZJxwG3q/B3VjnIAJ3aWhrd/oSLukX9b5pV+1BEP+lLFl8axyVhITIQG0sk4nLWs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308419; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=m+6THe7kuEatUC1VEYqCfUQQ/dGBe0D5MnCNr7ARK7g=; b=Rs72EiLIpCezcNMO2gaYz9DnOh9PYzGobHYZF6DgjIcwgP172x9ili0MIdJlgksjNVLDg10zLcpZF8rCwDt32YaMk787QTioeUecCqYDNw4UdoibcarEi+mFPTlDtkP6PT1ndBi3E8fNoACNrp34YVVUo10LRtOqGbieHQZRFRQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308419067790.7959674798514; Tue, 13 Apr 2021 03:06:59 -0700 (PDT) Received: from localhost ([::1]:55368 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFwg-0002Oq-AY for importer@patchew.org; Tue, 13 Apr 2021 06:06:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41372) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFp5-0000cH-DT for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:31611) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFp2-0005Ij-Ux for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:07 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-47-rdLxEiPDOauKF9_ANUkHiQ-1; Tue, 13 Apr 2021 05:59:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 521B610053ED; Tue, 13 Apr 2021 09:59:01 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECD575FC14; Tue, 13 Apr 2021 09:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307944; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m+6THe7kuEatUC1VEYqCfUQQ/dGBe0D5MnCNr7ARK7g=; b=eGexl5MwDLamZOVfoMEXs15qiB6Lq3BIHVByiZ3x6WX0nkIMZSHg3mf/W5q0MWPTMXiZz5 axYVjhkHDZQbb860Wp40KI0JKsWutvcZa5/mQlWDUPimp9JZtK2+gi3wPdSvcjc/bunyFO K+T4cqPHvDboaCIOvLKWo9kfHytOz6M= X-MC-Unique: rdLxEiPDOauKF9_ANUkHiQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 09/13] vfio: Support for RamDiscardManager in the vIOMMU case Date: Tue, 13 Apr 2021 11:55:27 +0200 Message-Id: <20210413095531.25603-10-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" vIOMMU support works already with RamDiscardManager as long as guests only map populated memory. Both, populated and discarded memory is mapped into &address_space_memory, where vfio_get_xlat_addr() will find that memory, to create the vfio mapping. Sane guests will never map discarded memory (e.g., unplugged memory blocks in virtio-mem) into an IOMMU - or keep it mapped into an IOMMU while memory is getting discarded. However, there are two cases where a malicious guests could trigger pinning of more memory than intended. One case is easy to handle: the guest trying to map discarded memory into an IOMMU. The other case is harder to handle: the guest keeping memory mapped in the IOMMU while it is getting discarded. We would have to walk over all mappings when discarding memory and identify if any mapping would be a violation. Let's keep it simple for now and print a warning, indicating that setting RLIMIT_MEMLOCK can mitigate such attacks. We have to take care of incoming migration: at the point the IOMMUs get restored and start creating mappings in vfio, RamDiscardManager implementations might not be back up and running yet: let's add runstate priorities to enforce the order when restoring. Acked-by: Alex Williamson Reviewed-by: Alex Williamson Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/vfio/common.c | 39 +++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-mem.c | 1 + include/migration/vmstate.h | 1 + 3 files changed, 41 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index f8a2fe8441..8a9bbf2791 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -36,6 +36,7 @@ #include "qemu/range.h" #include "sysemu/kvm.h" #include "sysemu/reset.h" +#include "sysemu/runstate.h" #include "trace.h" #include "qapi/error.h" #include "migration/migration.h" @@ -569,6 +570,44 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, v= oid **vaddr, error_report("iommu map to non memory area %"HWADDR_PRIx"", xlat); return false; + } else if (memory_region_has_ram_discard_manager(mr)) { + RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(m= r); + MemoryRegionSection tmp =3D { + .mr =3D mr, + .offset_within_region =3D xlat, + .size =3D int128_make64(len), + }; + + /* + * Malicious VMs can map memory into the IOMMU, which is expected + * to remain discarded. vfio will pin all pages, populating memory. + * Disallow that. vmstate priorities make sure any RamDiscardManag= er + * were already restored before IOMMUs are restored. + */ + if (!ram_discard_manager_is_populated(rdm, &tmp)) { + error_report("iommu map to discarded memory (e.g., unplugged v= ia" + " virtio-mem): %"HWADDR_PRIx"", + iotlb->translated_addr); + return false; + } + + /* + * Malicious VMs might trigger discarding of IOMMU-mapped memory. = The + * pages will remain pinned inside vfio until unmapped, resulting = in a + * higher memory consumption than expected. If memory would get + * populated again later, there would be an inconsistency between = pages + * pinned by vfio and pages seen by QEMU. This is the case until + * unmapped from the IOMMU (e.g., during device reset). + * + * With malicious guests, we really only care about pinning more m= emory + * than expected. RLIMIT_MEMLOCK set for the user/process can neve= r be + * exceeded and can be used to mitigate this problem. + */ + warn_report_once("Using vfio with vIOMMUs and coordinated discardi= ng of" + " RAM (e.g., virtio-mem) works, however, maliciou= s" + " guests can trigger pinning of more memory than" + " intended via an IOMMU. It's possible to mitigat= e " + " by setting/adjusting RLIMIT_MEMLOCK."); } =20 /* diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index e209b56057..cbd07fc9f1 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -886,6 +886,7 @@ static const VMStateDescription vmstate_virtio_mem_devi= ce =3D { .name =3D "virtio-mem-device", .minimum_version_id =3D 1, .version_id =3D 1, + .priority =3D MIG_PRI_VIRTIO_MEM, .post_load =3D virtio_mem_post_load, .fields =3D (VMStateField[]) { VMSTATE_WITH_TMP(VirtIOMEM, VirtIOMEMMigSanityChecks, diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 075ee80096..3bf58ff043 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -153,6 +153,7 @@ typedef enum { MIG_PRI_DEFAULT =3D 0, MIG_PRI_IOMMU, /* Must happen before PCI devices */ MIG_PRI_PCI_BUS, /* Must happen before IOMMU */ + MIG_PRI_VIRTIO_MEM, /* Must happen before IOMMU */ MIG_PRI_GICV3_ITS, /* Must happen before PCI devices */ MIG_PRI_GICV3, /* Must happen before the ITS */ MIG_PRI_MAX, --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308172; cv=none; d=zohomail.com; s=zohoarc; b=RVmv1rsp/ywJE0IgJSGW524uluH3HNUSTt78A1QGq/g8ITkbF79LH9D3/vipMPXQjTBKmmJZ5fbl60Th7flBwXKaTc/FFX1fQxJUxrz0ARHbQCBTIzsxO6HN4J/ok4rHoEQMNbo3KdRGIm7qZWfNWCjDpuB5WnMf7lsU/ZXxPX8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308172; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oT3YapBW4mzPMORsz7SDVTLDP4PZpFbsq/Hdc4/fMP8=; b=bFeKrGhQ80iq1OGo/JX7mlM28F1EUHw8wpY+osD29khdD3+SY3XFMlaCkhomyQChEvt2CT6OrdDwR6kGjYEroPO3cnaNWTvJab2ujBlyfvsvq6OZXllL6MZJ2Hi1m829qtAlB3bezcKtI6D0ObTTFXAlDim0pI98flMl+g5ivH0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308172386981.1711517445472; Tue, 13 Apr 2021 03:02:52 -0700 (PDT) Received: from localhost ([::1]:40084 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFsg-0004eK-O5 for importer@patchew.org; Tue, 13 Apr 2021 06:02:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41484) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpL-0000tf-CX for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:53399) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpI-0005S6-17 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:23 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-98-CxHDTBjYOIixXjyJFn--Jg-1; Tue, 13 Apr 2021 05:59:15 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D1664107ACCA; Tue, 13 Apr 2021 09:59:14 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id A923B5E1A4; Tue, 13 Apr 2021 09:59:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307957; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oT3YapBW4mzPMORsz7SDVTLDP4PZpFbsq/Hdc4/fMP8=; b=AFawz1Z/71nYt6OuBZOn6qgihxm2nzdZ6OJ5SXkOFA0agY9daGj8Ug8N5vQoXkM/OzWyb/ XiEp5bb5yNTufUQUujRdK7K4A2DkA9mqr1YuUj9oV9pMHGnp33M1N/eROYPgV96DldBY11 C2Q48mlY7RJeTJKC7GG87vMgT01vfW8= X-MC-Unique: CxHDTBjYOIixXjyJFn--Jg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 10/13] softmmu/physmem: Don't use atomic operations in ram_block_discard_(disable|require) Date: Tue, 13 Apr 2021 11:55:28 +0200 Message-Id: <20210413095531.25603-11-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We have users in migration context that don't hold the BQL (when finishing migration). To prepare for further changes, use a dedicated mutex instead of atomic operations. Keep using qatomic_read ("READ_ONCE") for the functions that only extract the current state (e.g., used by virtio-balloon), locking isn't necessary. While at it, split up the counter into two variables to make it easier to understand. Suggested-by: Peter Xu Reviewed-by: Peter Xu Reviewed-by: Pankaj Gupta Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- softmmu/physmem.c | 70 ++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 85034d9c11..aaa2b2eb92 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3647,56 +3647,64 @@ void mtree_print_dispatch(AddressSpaceDispatch *d, = MemoryRegion *root) } } =20 -/* - * If positive, discarding RAM is disabled. If negative, discarding RAM is - * required to work and cannot be disabled. - */ -static int ram_block_discard_disabled; +static unsigned int ram_block_discard_required_cnt; +static unsigned int ram_block_discard_disabled_cnt; +static QemuMutex ram_block_discard_disable_mutex; + +static void ram_block_discard_disable_mutex_lock(void) +{ + static gsize initialized; + + if (g_once_init_enter(&initialized)) { + qemu_mutex_init(&ram_block_discard_disable_mutex); + g_once_init_leave(&initialized, 1); + } + qemu_mutex_lock(&ram_block_discard_disable_mutex); +} + +static void ram_block_discard_disable_mutex_unlock(void) +{ + qemu_mutex_unlock(&ram_block_discard_disable_mutex); +} =20 int ram_block_discard_disable(bool state) { - int old; + int ret =3D 0; =20 + ram_block_discard_disable_mutex_lock(); if (!state) { - qatomic_dec(&ram_block_discard_disabled); - return 0; + ram_block_discard_disabled_cnt--; + } else if (!ram_block_discard_required_cnt) { + ram_block_discard_disabled_cnt++; + } else { + ret =3D -EBUSY; } - - do { - old =3D qatomic_read(&ram_block_discard_disabled); - if (old < 0) { - return -EBUSY; - } - } while (qatomic_cmpxchg(&ram_block_discard_disabled, - old, old + 1) !=3D old); - return 0; + ram_block_discard_disable_mutex_unlock(); + return ret; } =20 int ram_block_discard_require(bool state) { - int old; + int ret =3D 0; =20 + ram_block_discard_disable_mutex_lock(); if (!state) { - qatomic_inc(&ram_block_discard_disabled); - return 0; + ram_block_discard_required_cnt--; + } else if (!ram_block_discard_disabled_cnt) { + ram_block_discard_required_cnt++; + } else { + ret =3D -EBUSY; } - - do { - old =3D qatomic_read(&ram_block_discard_disabled); - if (old > 0) { - return -EBUSY; - } - } while (qatomic_cmpxchg(&ram_block_discard_disabled, - old, old - 1) !=3D old); - return 0; + ram_block_discard_disable_mutex_unlock(); + return ret; } =20 bool ram_block_discard_is_disabled(void) { - return qatomic_read(&ram_block_discard_disabled) > 0; + return qatomic_read(&ram_block_discard_disabled_cnt); } =20 bool ram_block_discard_is_required(void) { - return qatomic_read(&ram_block_discard_disabled) < 0; + return qatomic_read(&ram_block_discard_required_cnt); } --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308353; cv=none; d=zohomail.com; s=zohoarc; b=FmmBM6l+i2FZifYqef83OnSmgcrIXqfhN9IPv5LMzCrHjq/EYn0f6pqr5feebE8O9k4moO9TS6LBwdWhDo1zVgha5LwmBayrPUKSQQiL1eM0gTf08hkRWI6nJGU+0yruFpAD+ztrFLusK7T/pJ/9wCFQdQ+Fee9SfDKuRYvUL+0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308353; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cHy/j8hqiVNIkl3GaviDwK2CUCJTLHno49nd7iM3rfI=; b=Dz0zoALqDRU3vVjbHqLb4HARAOX2/QnLsTOPQHBAM2P2gEQJJH6hvbkRkhU+R8fThTg3EHTJdesy1JlbMhXKKxTGUf1d1dQrzffH/MFqN14049+yg0zIzRUiVU7LQx3boCcRgWbn6fY1xQ6VqYLNXuKK29dA08Ql3l5H7yqlSeA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16183083533980.06840913602320597; Tue, 13 Apr 2021 03:05:53 -0700 (PDT) Received: from localhost ([::1]:51518 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFvc-0000mD-9e for importer@patchew.org; Tue, 13 Apr 2021 06:05:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41524) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpW-00015n-Md for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:57602) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpS-0005aP-LW for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:34 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-187-T5PLEN33O_-fTA1Rj9nqeA-1; Tue, 13 Apr 2021 05:59:26 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3024E9126D; Tue, 13 Apr 2021 09:59:25 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 531385C239; Tue, 13 Apr 2021 09:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307970; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cHy/j8hqiVNIkl3GaviDwK2CUCJTLHno49nd7iM3rfI=; b=CLvznuLM/+wQuwIA4EIF/xpNnYfZ/rA8GvhF2TYexvDRrQrj1rFwIOWnS+ZisReHgl23v+ 84BSWgpCBTmH9TibO4nN5Xwo5BvWugG81XGl/sYZHp1AGl5G3EUYx5ocaUEdvFql2daw1+ xW0dmthoZF/r0zl1H6J/CiKNaqoEo6E= X-MC-Unique: T5PLEN33O_-fTA1Rj9nqeA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 11/13] softmmu/physmem: Extend ram_block_discard_(require|disable) by two discard types Date: Tue, 13 Apr 2021 11:55:29 +0200 Message-Id: <20210413095531.25603-12-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We want to separate the two cases whereby we discard ram - uncoordinated: e.g., virito-balloon - coordinated: e.g., virtio-mem coordinated via the RamDiscardManager Reviewed-by: Pankaj Gupta Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- include/exec/memory.h | 18 +++++++++++++-- softmmu/physmem.c | 54 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index e806d0140e..bba7b6643e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2873,6 +2873,12 @@ static inline MemOp devend_memop(enum device_endian = end) */ int ram_block_discard_disable(bool state); =20 +/* + * See ram_block_discard_disable(): only disable uncoordinated discards, + * keeping coordinated discards (via the RamDiscardManager) enabled. + */ +int ram_block_uncoordinated_discard_disable(bool state); + /* * Inhibit technologies that disable discarding of pages in RAM blocks. * @@ -2882,12 +2888,20 @@ int ram_block_discard_disable(bool state); int ram_block_discard_require(bool state); =20 /* - * Test if discarding of memory in ram blocks is disabled. + * See ram_block_discard_require(): only inhibit technologies that disable + * uncoordinated discarding of pages in RAM blocks, allowing co-existance = with + * technologies that only inhibit uncoordinated discards (via the + * RamDiscardManager). + */ +int ram_block_coordinated_discard_require(bool state); + +/* + * Test if any discarding of memory in ram blocks is disabled. */ bool ram_block_discard_is_disabled(void); =20 /* - * Test if discarding of memory in ram blocks is required to work reliably. + * Test if any discarding of memory in ram blocks is required to work reli= ably. */ bool ram_block_discard_is_required(void); =20 diff --git a/softmmu/physmem.c b/softmmu/physmem.c index aaa2b2eb92..ead7b5c429 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3647,8 +3647,14 @@ void mtree_print_dispatch(AddressSpaceDispatch *d, M= emoryRegion *root) } } =20 +/* Require any discards to work. */ static unsigned int ram_block_discard_required_cnt; +/* Require only coordinated discards to work. */ +static unsigned int ram_block_coordinated_discard_required_cnt; +/* Disable any discards. */ static unsigned int ram_block_discard_disabled_cnt; +/* Disable only uncoordinated discards. */ +static unsigned int ram_block_uncoordinated_discard_disabled_cnt; static QemuMutex ram_block_discard_disable_mutex; =20 static void ram_block_discard_disable_mutex_lock(void) @@ -3674,10 +3680,27 @@ int ram_block_discard_disable(bool state) ram_block_discard_disable_mutex_lock(); if (!state) { ram_block_discard_disabled_cnt--; - } else if (!ram_block_discard_required_cnt) { - ram_block_discard_disabled_cnt++; + } else if (ram_block_discard_required_cnt || + ram_block_coordinated_discard_required_cnt) { + ret =3D -EBUSY; } else { + ram_block_discard_disabled_cnt++; + } + ram_block_discard_disable_mutex_unlock(); + return ret; +} + +int ram_block_uncoordinated_discard_disable(bool state) +{ + int ret =3D 0; + + ram_block_discard_disable_mutex_lock(); + if (!state) { + ram_block_uncoordinated_discard_disabled_cnt--; + } else if (ram_block_discard_required_cnt) { ret =3D -EBUSY; + } else { + ram_block_uncoordinated_discard_disabled_cnt++; } ram_block_discard_disable_mutex_unlock(); return ret; @@ -3690,10 +3713,27 @@ int ram_block_discard_require(bool state) ram_block_discard_disable_mutex_lock(); if (!state) { ram_block_discard_required_cnt--; - } else if (!ram_block_discard_disabled_cnt) { - ram_block_discard_required_cnt++; + } else if (ram_block_discard_disabled_cnt || + ram_block_uncoordinated_discard_disabled_cnt) { + ret =3D -EBUSY; } else { + ram_block_discard_required_cnt++; + } + ram_block_discard_disable_mutex_unlock(); + return ret; +} + +int ram_block_coordinated_discard_require(bool state) +{ + int ret =3D 0; + + ram_block_discard_disable_mutex_lock(); + if (!state) { + ram_block_coordinated_discard_required_cnt--; + } else if (ram_block_discard_disabled_cnt) { ret =3D -EBUSY; + } else { + ram_block_coordinated_discard_required_cnt++; } ram_block_discard_disable_mutex_unlock(); return ret; @@ -3701,10 +3741,12 @@ int ram_block_discard_require(bool state) =20 bool ram_block_discard_is_disabled(void) { - return qatomic_read(&ram_block_discard_disabled_cnt); + return qatomic_read(&ram_block_discard_disabled_cnt) || + qatomic_read(&ram_block_uncoordinated_discard_disabled_cnt); } =20 bool ram_block_discard_is_required(void) { - return qatomic_read(&ram_block_discard_required_cnt); + return qatomic_read(&ram_block_discard_required_cnt) || + qatomic_read(&ram_block_coordinated_discard_required_cnt); } --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308437; cv=none; d=zohomail.com; s=zohoarc; b=Dr9NmrYs5NJYv5e9l7PJ2rDZDqWr1PLcobeVqu0W2DXRpYnOLlN6Pz3kVjkYonV3bCjs9YApWqAiua6DnhefrfmJniQpc8Xh2w8zH5lvXhhh7CDQeMC/wrGVxq+xoq7htrFC7oaPF5JAIvxLNzr7A6ikIRTZbquHRyBFrzt/Dk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308437; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Sx7EiblSzr3Xp4ApmxhXjuiazRUedMf7Pwb3aktxY4I=; b=hY+Y4kMzlcGX39Yx16oJE6fpVn1r+MGEiXI2O/wy5AbJKm2HoEql3D+j2QI5la9gOZxy6Sj1oXfArncWgwuh6sOt2Yv41s4AwjokL1P52J+OCnOW8zPGgbV//ijVCxZd41i1Phhp0J0ZNuQlY08mqRlz6WkIhN6ygIiHUpwEu/U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308437109906.0342625184547; Tue, 13 Apr 2021 03:07:17 -0700 (PDT) Received: from localhost ([::1]:56418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFwy-0002p2-D6 for importer@patchew.org; Tue, 13 Apr 2021 06:07:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41554) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpn-0001HL-B8 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:27590) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpl-0005jm-2M for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:51 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-392-rmeLwTIWNya2ucanoYMQPQ-1; Tue, 13 Apr 2021 05:59:45 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6F163107ACC7; Tue, 13 Apr 2021 09:59:44 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FEDD5E1A4; Tue, 13 Apr 2021 09:59:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Sx7EiblSzr3Xp4ApmxhXjuiazRUedMf7Pwb3aktxY4I=; b=eJt6rdFVkrOXwR/koLG4pxcPmOeWbeK9hbx2HyLgm8ybh2zJpDfZzypBReMgE/I+QF4Y5Q edTCRTktWLOVrvFrYaKji7dKCZaEETUdxh26MUvh+o/FlYOtEIYkH8PoD8++oe1I0DfBko Rbj6zRPpiqnFIQRLr0U8k8nJOHCGt3g= X-MC-Unique: rmeLwTIWNya2ucanoYMQPQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 12/13] virtio-mem: Require only coordinated discards Date: Tue, 13 Apr 2021 11:55:30 +0200 Message-Id: <20210413095531.25603-13-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We implement the RamDiscardManager interface and only require coordinated discarding of RAM to work. Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Pankaj Gupta Acked-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index cbd07fc9f1..9e79b5b50c 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -719,7 +719,7 @@ static void virtio_mem_device_realize(DeviceState *dev,= Error **errp) return; } =20 - if (ram_block_discard_require(true)) { + if (ram_block_coordinated_discard_require(true)) { error_setg(errp, "Discarding RAM is disabled"); return; } @@ -727,7 +727,7 @@ static void virtio_mem_device_realize(DeviceState *dev,= Error **errp) ret =3D ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb)); if (ret) { error_setg_errno(errp, -ret, "Unexpected error discarding RAM"); - ram_block_discard_require(false); + ram_block_coordinated_discard_require(false); return; } =20 @@ -771,7 +771,7 @@ static void virtio_mem_device_unrealize(DeviceState *de= v) virtio_del_queue(vdev, 0); virtio_cleanup(vdev); g_free(vmem->bitmap); - ram_block_discard_require(false); + ram_block_coordinated_discard_require(false); } =20 static int virtio_mem_discard_range_cb(const VirtIOMEM *vmem, void *arg, --=20 2.30.2 From nobody Wed May 15 15:50:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618308299; cv=none; d=zohomail.com; s=zohoarc; b=hXzzAasxyiug2HJZSazoi5T3787jX1lL4sR7V5inBtDui0ElofO/yEbaaXOEAPcOy/qk+MKwUzdOhd7c6I+VMN7lNcXw0vw5ldyo7ksCnz/GeFpxCbY45akzCIRu+jpuxIslCSYw5R3w/93Zpm7haOGM/aSvwmaTDdQP5eyZoHM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618308299; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sAU9nur57kM07WbZdwilL/Tv3tl65rOUzUqxp2e5eKo=; b=KmfLF+eiAy22mi+dNYRrOXgEqx5Z35WE66l9oj4Vb7z2BSucMi6LRi/ufSzNia3ixHjlznVWGTcTwqnFYEd3Jp1ViHh8kYw6QO1ZVpIqv92+cD4X2/OVOLDu4ZwDWnlBHSN7YVLoBrELCFW8wIsdtvOlUE7jGXUjzQMbgH9+qfw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618308299839145.35833471966225; Tue, 13 Apr 2021 03:04:59 -0700 (PDT) Received: from localhost ([::1]:48296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWFul-0007v6-3h for importer@patchew.org; Tue, 13 Apr 2021 06:04:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpv-0001Ub-V4 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26961) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWFpt-0005pY-M7 for qemu-devel@nongnu.org; Tue, 13 Apr 2021 05:59:59 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-103-leFGqq3iOv25_VWEgmB3JA-1; Tue, 13 Apr 2021 05:59:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5D4441922968; Tue, 13 Apr 2021 09:59:54 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-69.ams2.redhat.com [10.36.115.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id CBBD45E1A4; Tue, 13 Apr 2021 09:59:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618307997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sAU9nur57kM07WbZdwilL/Tv3tl65rOUzUqxp2e5eKo=; b=YeSP4b2WAaLkj1qK/H9XNFvxXglEHTUmaZ040xcQvuXJxT/2BuDoLhZnYpDD90zl7wnAP9 orCgceZzLldNJtpBdU2L1ymLdp+9xybJ7Tmlg1uOvrGmCVGHUQYM+5GYYxfN1bL0L5pAG4 gm1UovStQSCV6U0T3RythkqY1tRL620= X-MC-Unique: leFGqq3iOv25_VWEgmB3JA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RESEND v7 13/13] vfio: Disable only uncoordinated discards for VFIO_TYPE1 iommus Date: Tue, 13 Apr 2021 11:55:31 +0200 Message-Id: <20210413095531.25603-14-david@redhat.com> In-Reply-To: <20210413095531.25603-1-david@redhat.com> References: <20210413095531.25603-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Wei Yang , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We support coordinated discarding of RAM using the RamDiscardManager for the VFIO_TYPE1 iommus. Let's unlock support for coordinated discards, keeping uncoordinated discards (e.g., via virtio-balloon) disabled if possible. This unlocks virtio-mem + vfio on x86-64. Note that vfio used via "nvme://" by the block layer has to be implemented/unlocked separately. For now, virtio-mem only supports x86-64; we don't restrict RamDiscardManager to x86-64, though: arm64 and s390x are supposed to work as well, and we'll test once unlocking virtio-mem support. The spapr IOMMUs will need special care, to be tackled later, e.g.., once supporting virtio-mem. Note: The block size of a virtio-mem device has to be set to sane sizes, depending on the maximum hotplug size - to not run out of vfio mappings. The default virtio-mem block size is usually in the range of a couple of MBs. The maximum number of mapping is 64k, shared with other users. Assume you want to hotplug 256GB using virtio-mem - the block size would have to be set to at least 8 MiB (resulting in 32768 separate mappings). Acked-by: Alex Williamson Reviewed-by: Alex Williamson Acked-by: Michael S. Tsirkin Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/vfio/common.c | 65 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 8a9bbf2791..3f0d111360 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -135,6 +135,29 @@ static const char *index_to_str(VFIODevice *vbasedev, = int index) } } =20 +static int vfio_ram_block_discard_disable(VFIOContainer *container, bool s= tate) +{ + switch (container->iommu_type) { + case VFIO_TYPE1v2_IOMMU: + case VFIO_TYPE1_IOMMU: + /* + * We support coordinated discarding of RAM via the RamDiscardMana= ger. + */ + return ram_block_uncoordinated_discard_disable(state); + default: + /* + * VFIO_SPAPR_TCE_IOMMU most probably works just fine with + * RamDiscardManager, however, it is completely untested. + * + * VFIO_SPAPR_TCE_v2_IOMMU with "DMA memory preregistering" does + * completely the opposite of managing mapping/pinning dynamically= as + * required by RamDiscardManager. We would have to special-case se= ctions + * with a RamDiscardManager. + */ + return ram_block_discard_disable(state); + } +} + int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, int action, int fd, Error **errp) { @@ -1977,15 +2000,25 @@ static int vfio_connect_container(VFIOGroup *group,= AddressSpace *as, * new memory, it will not yet set ram_block_discard_set_required() and * therefore, neither stops us here or deals with the sudden memory * consumption of inflated memory. + * + * We do support discarding of memory coordinated via the RamDiscardMa= nager + * with some IOMMU types. vfio_ram_block_discard_disable() handles the + * details once we know which type of IOMMU we are using. */ - ret =3D ram_block_discard_disable(true); - if (ret) { - error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken"= ); - return ret; - } =20 QLIST_FOREACH(container, &space->containers, next) { if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) { + ret =3D vfio_ram_block_discard_disable(container, true); + if (ret) { + error_setg_errno(errp, -ret, + "Cannot set discarding of RAM broken"); + if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, + &container->fd)) { + error_report("vfio: error disconnecting group %d from" + " container", group->groupid); + } + return ret; + } group->container =3D container; QLIST_INSERT_HEAD(&container->group_list, group, container_nex= t); vfio_kvm_device_add_group(group); @@ -2023,6 +2056,12 @@ static int vfio_connect_container(VFIOGroup *group, = AddressSpace *as, goto free_container_exit; } =20 + ret =3D vfio_ram_block_discard_disable(container, true); + if (ret) { + error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken"= ); + goto free_container_exit; + } + switch (container->iommu_type) { case VFIO_TYPE1v2_IOMMU: case VFIO_TYPE1_IOMMU: @@ -2070,7 +2109,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, if (ret) { error_setg_errno(errp, errno, "failed to enable container"= ); ret =3D -errno; - goto free_container_exit; + goto enable_discards_exit; } } else { container->prereg_listener =3D vfio_prereg_listener; @@ -2082,7 +2121,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, ret =3D -1; error_propagate_prepend(errp, container->error, "RAM memory listener initialization failed: "); - goto free_container_exit; + goto enable_discards_exit; } } =20 @@ -2095,7 +2134,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, if (v2) { memory_listener_unregister(&container->prereg_listener); } - goto free_container_exit; + goto enable_discards_exit; } =20 if (v2) { @@ -2110,7 +2149,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, if (ret) { error_setg_errno(errp, -ret, "failed to remove existing window"); - goto free_container_exit; + goto enable_discards_exit; } } else { /* The default table uses 4K pages */ @@ -2151,6 +2190,9 @@ listener_release_exit: vfio_kvm_device_del_group(group); vfio_listener_release(container); =20 +enable_discards_exit: + vfio_ram_block_discard_disable(container, false); + free_container_exit: g_free(container); =20 @@ -2158,7 +2200,6 @@ close_fd_exit: close(fd); =20 put_space_exit: - ram_block_discard_disable(false); vfio_put_address_space(space); =20 return ret; @@ -2280,7 +2321,7 @@ void vfio_put_group(VFIOGroup *group) } =20 if (!group->ram_block_discard_allowed) { - ram_block_discard_disable(false); + vfio_ram_block_discard_disable(group->container, false); } vfio_kvm_device_del_group(group); vfio_disconnect_container(group); @@ -2334,7 +2375,7 @@ int vfio_get_device(VFIOGroup *group, const char *nam= e, =20 if (!group->ram_block_discard_allowed) { group->ram_block_discard_allowed =3D true; - ram_block_discard_disable(false); + vfio_ram_block_discard_disable(group->container, false); } } =20 --=20 2.30.2