From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 600D4C63697 for ; Thu, 19 Nov 2020 15:44:22 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9241F2224A for ; Thu, 19 Nov 2020 15:44:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="L2uBtTfU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9241F2224A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34514 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfm6e-0004hL-AF for qemu-devel@archiver.kernel.org; Thu, 19 Nov 2020 10:44:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41610) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfm2U-00082P-OH for qemu-devel@nongnu.org; Thu, 19 Nov 2020 10:40:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59117) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kfm2R-0001FG-Q4 for qemu-devel@nongnu.org; Thu, 19 Nov 2020 10:40:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605800399; 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=x8HeKtwyuQTxlGUjo4v0/FEMaUvRnaPC1BcdpQzACOM=; b=L2uBtTfUyy4mM/myXB5uwwTGVqqwWPkszUBbNrfxvGQINqwIvwvRuo75FOpfiMOCZqHCrE vusJlrfbMO7b9Qo3e/rLRzI1lvW65SL3i60CdgmmKRvisXGEmIgae6ksDp58MmywGTlNCg 3lNhjbboEhauS7InB/N6LgsEn6SMW+Q= 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-235-v8EEDwiqMjm6msK--_NK3g-1; Thu, 19 Nov 2020 10:39:56 -0500 X-MC-Unique: v8EEDwiqMjm6msK--_NK3g-1 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 856B69CC02; Thu, 19 Nov 2020 15:39:55 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-158.ams2.redhat.com [10.36.114.158]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19AD65C1A1; Thu, 19 Nov 2020 15:39:39 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 1/9] memory: Introduce RamDiscardMgr for RAM memory regions Date: Thu, 19 Nov 2020 16:39:10 +0100 Message-Id: <20201119153918.120976-2-david@redhat.com> In-Reply-To: <20201119153918.120976-1-david@redhat.com> References: <20201119153918.120976-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: 8bit Content-Type: text/plain; charset="US-ASCII" Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 03:44:58 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, RCVD_IN_MSPIKE_H3=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 , David Hildenbrand , "Michael S. Tsirkin" , "Dr . David Alan Gilbert" , Peter Xu , Luiz Capitulino , Auger Eric , Alex Williamson , teawater , Igor Mammedov , Paolo Bonzini , Marek Kedzierski Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 RamDiscardMgr 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. 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 | 225 ++++++++++++++++++++++++++++++++++++++++++ softmmu/memory.c | 22 +++++ 2 files changed, 247 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 0f3e6bcd5e..468cbb53a4 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -42,6 +42,12 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass; DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass, IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION) +#define TYPE_RAM_DISCARD_MGR "qemu:ram-discard-mgr" +typedef struct RamDiscardMgrClass RamDiscardMgrClass; +typedef struct RamDiscardMgr RamDiscardMgr; +DECLARE_OBJ_CHECKERS(RamDiscardMgr, RamDiscardMgrClass, RAM_DISCARD_MGR, + TYPE_RAM_DISCARD_MGR); + #ifdef CONFIG_FUZZ void fuzz_dma_read_cb(size_t addr, size_t len, @@ -116,6 +122,66 @@ struct IOMMUNotifier { }; typedef struct IOMMUNotifier IOMMUNotifier; +struct RamDiscardListener; +typedef int (*NotifyRamPopulate)(struct RamDiscardListener *rdl, + const MemoryRegion *mr, ram_addr_t offset, + ram_addr_t size); +typedef void (*NotifyRamDiscard)(struct RamDiscardListener *rdl, + const MemoryRegion *mr, ram_addr_t offset, + ram_addr_t size); +typedef void (*NotifyRamDiscardAll)(struct RamDiscardListener *rdl, + const MemoryRegion *mr); + +typedef struct RamDiscardListener { + /* + * @notify_populate: + * + * Notification that previously discarded memory is about to get populated. + * Listeners are able to object. If any listener objects, already + * successfully notified listeners are notified about a discard again. + * + * @rdl: the #RamDiscardListener getting notified + * @mr: the relevant #MemoryRegion + * @offset: offset into the #MemoryRegion, aligned to minimum granularity of + * the #RamDiscardMgr + * @size: the size, aligned to minimum granularity of the #RamDiscardMgr + * + * Returns 0 on success. If the notification is rejected by the listener, + * an error is returned. + */ + NotifyRamPopulate notify_populate; + + /* + * @notify_discard: + * + * Notification that previously populated memory was discarded successfully + * and listeners should drop all references to such memory and prevent + * new population (e.g., unmap). + * + * @rdl: the #RamDiscardListener getting notified + * @mr: the relevant #MemoryRegion + * @offset: offset into the #MemoryRegion, aligned to minimum granularity of + * the #RamDiscardMgr + * @size: the size, aligned to minimum granularity of the #RamDiscardMgr + */ + NotifyRamDiscard notify_discard; + + /* + * @notify_discard_all: + * + * Notification that all previously populated memory was discarded + * successfully. + * + * Note: this callback is optional. If not set, individual notify_populate() + * notifications are triggered. + * + * @rdl: the #RamDiscardListener getting notified + * @mr: the relevant #MemoryRegion + */ + NotifyRamDiscardAll notify_discard_all; + QLIST_ENTRY(RamDiscardListener) next; +} RamDiscardListener; + /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */ #define RAM_PREALLOC (1 << 0) @@ -151,6 +217,16 @@ static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, n->iommu_idx = iommu_idx; } +static inline void ram_discard_listener_init(RamDiscardListener *rdl, + NotifyRamPopulate populate_fn, + NotifyRamDiscard discard_fn, + NotifyRamDiscardAll discard_all_fn) +{ + rdl->notify_populate = populate_fn; + rdl->notify_discard = discard_fn; + rdl->notify_discard_all = discard_all_fn; +} + /* * Memory region callbacks */ @@ -425,6 +501,120 @@ struct IOMMUMemoryRegionClass { Error **errp); }; +/* + * RamDiscardMgrClass: + * + * A #RamDiscardMgr coordinates which parts of specific RAM #MemoryRegion + * 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 #RamDiscardMgr can only be set for a RAM #MemoryRegion while the + * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegion is + * mapped. + * + * The #RamDiscardMgr 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 #RamDiscardMgr 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 to not + * access unplugged (discarded) memory - especially via DMA. virtio-mem will + * properly coordinate with listeners before memory is plugged (populated), + * and after memory is unplugged (discarded). + * + * Listeners are called in multiples of the minimum granularity and changes are + * aligned to the minimum granularity within the #MemoryRegion. Listeners have + * to prepare for memory becomming discarded in a different granularity than it + * was populated and the other way around. + */ +struct RamDiscardMgrClass { + /* 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 #RamDiscardMgr. + * + * @rdm: the #RamDiscardMgr + * @mr: the #MemoryRegion + * + * Returns the minimum granularity. + */ + uint64_t (*get_min_granularity)(const RamDiscardMgr *rdm, + const MemoryRegion *mr); + + /** + * @is_populated: + * + * Check whether the given range within the #MemoryRegion is completely + * populated (i.e., no parts are currently discarded). There are no + * alignment requirements for the range. + * + * @rdm: the #RamDiscardMgr + * @mr: the #MemoryRegion + * @offset: offset into the #MemoryRegion + * @size: size in the #MemoryRegion + * + * Returns the minimum granularity. + */ + bool (*is_populated)(const RamDiscardMgr *rdm, const MemoryRegion *mr, + ram_addr_t start, ram_addr_t offset); + + /** + * @register_listener: + * + * Register a #RamDiscardListener for a #MemoryRegion via the + * #RamDiscardMgr. + * + * @rdm: the #RamDiscardMgr + * @mr: the #MemoryRegion + * @rdl: the #RamDiscardListener + */ + void (*register_listener)(RamDiscardMgr *rdm, const MemoryRegion *mr, + RamDiscardListener *rdl); + + /** + * @unregister_listener: + * + * Unregister a previously registered #RamDiscardListener for a + * #MemoryRegion via the #RamDiscardMgr. + * + * @rdm: the #RamDiscardMgr + * @mr: the #MemoryRegion + * @rdl: the #RamDiscardListener + */ + void (*unregister_listener)(RamDiscardMgr *rdm, const MemoryRegion *mr, + RamDiscardListener *rdl); + + /** + * @replay_populated: + * + * Notify the #RamDiscardListener about all populated parts within the + * #MemoryRegion via the #RamDiscardMgr + * + * In case any notification fails, no further notifications are triggered. + * The listener is not required to be registered. + * + * @rdm: the #RamDiscardMgr + * @mr: the #MemoryRegion + * @rdl: the #RamDiscardListener + * + * Returns 0 on success, or a negative error if any notification failed. + */ + int (*replay_populated)(const RamDiscardMgr *rdm, const MemoryRegion *mr, + RamDiscardListener *rdl); +}; + typedef struct CoalescedMemoryRange CoalescedMemoryRange; typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; @@ -471,6 +661,7 @@ struct MemoryRegion { const char *name; unsigned ioeventfd_nb; MemoryRegionIoeventfd *ioeventfds; + RamDiscardMgr *rdm; /* Only for RAM */ }; struct IOMMUMemoryRegion { @@ -1965,6 +2156,40 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr); */ bool memory_region_is_mapped(MemoryRegion *mr); +/** + * memory_region_get_ram_discard_mgr: get the #RamDiscardMgr for a + * #MemoryRegion + * + * The #RamDiscardMgr cannot change while a memory region is mapped. + * + * @mr: the #MemoryRegion + */ +RamDiscardMgr *memory_region_get_ram_discard_mgr(MemoryRegion *mr); + +/** + * memory_region_has_ram_discard_mgr: check whether a #MemoryRegion has a + * #RamDiscardMgr assigned + * + * @mr: the #MemoryRegion + */ +static inline bool memory_region_has_ram_discard_mgr(MemoryRegion *mr) +{ + return !!memory_region_get_ram_discard_mgr(mr); +} + +/** + * memory_region_set_ram_discard_mgr: set the #RamDiscardMgr for a + * #MemoryRegion + * + * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion + * that does not cover RAM, or a #MemoryRegion that already has a + * #RamDiscardMgr assigned. + * + * @mr: the #MemoryRegion + * @urn: #RamDiscardMgr to set + */ +void memory_region_set_ram_discard_mgr(MemoryRegion *mr, RamDiscardMgr *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 aa393f1bb0..fbdc50fa8b 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2013,6 +2013,21 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr) return imrc->num_indexes(iommu_mr); } +RamDiscardMgr *memory_region_get_ram_discard_mgr(MemoryRegion *mr) +{ + if (!memory_region_is_mapped(mr) || !memory_region_is_ram(mr)) { + return false; + } + return mr->rdm; +} + +void memory_region_set_ram_discard_mgr(MemoryRegion *mr, RamDiscardMgr *rdm) +{ + g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr)); + g_assert(!rdm || !mr->rdm); + mr->rdm = rdm; +} + void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) { uint8_t mask = 1 << client; @@ -3294,10 +3309,17 @@ static const TypeInfo iommu_memory_region_info = { .abstract = true, }; +static const TypeInfo ram_discard_mgr_info = { + .parent = TYPE_INTERFACE, + .name = TYPE_RAM_DISCARD_MGR, + .class_size = sizeof(RamDiscardMgrClass), +}; + static void memory_register_types(void) { type_register_static(&memory_region_info); type_register_static(&iommu_memory_region_info); + type_register_static(&ram_discard_mgr_info); } type_init(memory_register_types) -- 2.26.2