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=-16.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,USER_AGENT_GIT 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 BB7E2C48BE8 for ; Tue, 15 Jun 2021 14:14:35 +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 4BFF961450 for ; Tue, 15 Jun 2021 14:14:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4BFF961450 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]:56990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lt9pq-0001so-6I for qemu-devel@archiver.kernel.org; Tue, 15 Jun 2021 10:14:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59594) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lt9Hu-0006D8-Bd for qemu-devel@nongnu.org; Tue, 15 Jun 2021 09:39:31 -0400 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]:44911) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lt9Hq-0002qs-6o for qemu-devel@nongnu.org; Tue, 15 Jun 2021 09:39:29 -0400 Received: by mail-ej1-x630.google.com with SMTP id gt18so4153980ejc.11 for ; Tue, 15 Jun 2021 06:39:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=buGOH7XUq92PFXNAgM40ZqSCuPs0ldahTRIbvBcpUuw=; b=QjXMGpeisGSO1V7un+piWXZtVPKroXzEZkbsOuowTY5L89uqEx4W+p5Sp2vEffKmQP 3CY8beU74oSwxPDdu00X16j5HbGl3s0i18sQ6lvOhZaZMN1PERyPKQpDobN7X+RkMBao yzMPDdU26Apkae1I2mZ4AuIJSZAdaWae9zAXCcqePCzXTqb8YzfK3sNJRydOFt3xT2xJ jqTGWrgUcRx/i74puw/UOEShzy9w4UumakfSP/ODva6/yloN3SqsL+1EA2c1SNUVH6OH mf7HFb76qNnsyhRJqqtqdaIj59/2wGx2HreYcYO7Ir42AcU6XeBl2QyIvNf1JUAYBjka nsnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=buGOH7XUq92PFXNAgM40ZqSCuPs0ldahTRIbvBcpUuw=; b=Cma5bjpwWQ+ZinqL3qSTlmnQ8LTYETkyIcn/winA/VbwRGTn/HQH2LtAGQveokGizT ybmUzMcJ4X4BWs6Kyks1W07kxA6pJ6isECxHPlANaPE93huQ5nzbb46RBtP+zXLEKEw8 sqIKuLqwX0e0kluTzChX8dAmG0rE71goBVJ3+R9J1afIOzJbSY9gZJ/3M/Lrq+xOYu4S YeapP6BKWKM3y7a/pyTQGTSrFxaZTJqQctvrnOZy1XkqvG7kjWM16W/Dw2wmhpdPcBGC Ny9AylLSbZhUQ94uETUThqi8VBZm3s86xIQWfaTxTbJIVXS9nECmGeGc6n4d1IiBdKUz BQUQ== X-Gm-Message-State: AOAM530rvApG0dqdInwrTOEt58zUhjIpv8wwBT0Qczu7E0yFMhboFg90 hoHKjOd0nUuNJJUaLHqegds3kXxGdXM= X-Google-Smtp-Source: ABdhPJyqpb9LFs+PIuvOpMkb0cLQrP2qpkP3tKHhvfC7lBCG8YZcmi+193paboxX+ccvi8MYE8VNUA== X-Received: by 2002:a17:907:7b9e:: with SMTP id ne30mr21174283ejc.389.1623764360896; Tue, 15 Jun 2021 06:39:20 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id r17sm11843769edt.33.2021.06.15.06.39.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Jun 2021 06:39:20 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 25/33] memory: Introduce RAM_NORESERVE and wire it up in qemu_ram_mmap() Date: Tue, 15 Jun 2021 15:38:47 +0200 Message-Id: <20210615133855.775687-26-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210615133855.775687-1-pbonzini@redhat.com> References: <20210615133855.775687-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::630; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x630.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Peter Xu , David Hildenbrand Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: David Hildenbrand Let's introduce RAM_NORESERVE, allowing mmap'ing with MAP_NORESERVE. The new flag has the following semantics: " RAM is mmap-ed with MAP_NORESERVE. When set, reserving swap space (or huge pages if applicable) is skipped: will bail out if not supported. When not set, the OS will do the reservation, if supported for the memory type. " Allow passing it into: - memory_region_init_ram_nomigrate() - memory_region_init_resizeable_ram() - memory_region_init_ram_from_file() ... and teach qemu_ram_mmap() and qemu_anon_ram_alloc() about the flag. Bail out if the flag is not supported, which is the case right now for both, POSIX and win32. We will add Linux support next and allow specifying RAM_NORESERVE via memory backends. The target use case is virtio-mem, which dynamically exposes memory inside a large, sparse memory area to the VM. Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Peter Xu Acked-by: Eduardo Habkost for memory backend and machine core Signed-off-by: David Hildenbrand Message-Id: <20210510114328.21835-9-david@redhat.com> Signed-off-by: Paolo Bonzini --- include/exec/cpu-common.h | 1 + include/exec/memory.h | 15 ++++++++++++--- include/exec/ram_addr.h | 3 ++- include/qemu/osdep.h | 9 ++++++++- migration/ram.c | 3 +-- softmmu/physmem.c | 15 ++++++++++++--- util/mmap-alloc.c | 7 +++++++ util/oslib-posix.c | 6 ++++-- util/oslib-win32.c | 13 ++++++++++++- 9 files changed, 59 insertions(+), 13 deletions(-) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index ccabed4003..039d422bf4 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -59,6 +59,7 @@ ram_addr_t qemu_ram_get_offset(RAMBlock *rb); ram_addr_t qemu_ram_get_used_length(RAMBlock *rb); ram_addr_t qemu_ram_get_max_length(RAMBlock *rb); bool qemu_ram_is_shared(RAMBlock *rb); +bool qemu_ram_is_noreserve(RAMBlock *rb); bool qemu_ram_is_uf_zeroable(RAMBlock *rb); void qemu_ram_set_uf_zeroable(RAMBlock *rb); bool qemu_ram_is_migratable(RAMBlock *rb); diff --git a/include/exec/memory.h b/include/exec/memory.h index b1f8fa1df0..b116f7c64e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -155,6 +155,13 @@ typedef struct IOMMUTLBEvent { */ #define RAM_UF_WRITEPROTECT (1 << 6) +/* + * RAM is mmap-ed with MAP_NORESERVE. When set, reserving swap space (or huge + * pages if applicable) is skipped: will bail out if not supported. When not + * set, the OS will do the reservation, if supported for the memory type. + */ +#define RAM_NORESERVE (1 << 7) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, @@ -949,7 +956,7 @@ void memory_region_init_ram_nomigrate(MemoryRegion *mr, * @name: Region name, becomes part of RAMBlock name used in migration stream * must be unique within any device * @size: size of the region. - * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED. + * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE. * @errp: pointer to Error*, to store an error if it happens. * * Note that this function does not do anything to cause the data in the @@ -1005,7 +1012,8 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr, * @size: size of the region. * @align: alignment of the region base address; if 0, the default alignment * (getpagesize()) will be used. - * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM. + * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, + * RAM_NORESERVE, * @path: the path in which to allocate the RAM. * @readonly: true to open @path for reading, false for read/write. * @errp: pointer to Error*, to store an error if it happens. @@ -1031,7 +1039,8 @@ void memory_region_init_ram_from_file(MemoryRegion *mr, * @owner: the object that tracks the region's reference count * @name: the name of the region. * @size: size of the region. - * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM. + * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, + * RAM_NORESERVE. * @fd: the fd to mmap. * @offset: offset within the file referenced by fd * @errp: pointer to Error*, to store an error if it happens. diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 6d4513f8e2..551876bed0 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -104,7 +104,8 @@ long qemu_maxrampagesize(void); * Parameters: * @size: the size in bytes of the ram block * @mr: the memory region where the ram block is - * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM. + * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, + * RAM_NORESERVE. * @mem_path or @fd: specify the backing file or device * @readonly: true to open @path for reading, false for read/write. * @errp: pointer to Error*, to store an error if it happens diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 88106e88fc..38a3327db9 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -362,7 +362,8 @@ extern "C" { int qemu_daemon(int nochdir, int noclose); void *qemu_try_memalign(size_t alignment, size_t size); void *qemu_memalign(size_t alignment, size_t size); -void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared); +void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared, + bool noreserve); void qemu_vfree(void *ptr); void qemu_anon_ram_free(void *ptr, size_t size); @@ -383,6 +384,12 @@ void qemu_anon_ram_free(void *ptr, size_t size); */ #define QEMU_MAP_SYNC (1 << 2) +/* + * Use MAP_NORESERVE to skip reservation of swap space (or huge pages if + * applicable). Bail out if not supported/effective. + */ +#define QEMU_MAP_NORESERVE (1 << 3) + #define QEMU_MADV_INVALID -1 diff --git a/migration/ram.c b/migration/ram.c index 60ea913c54..723af67c2e 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -3343,8 +3343,7 @@ int colo_init_ram_cache(void) WITH_RCU_READ_LOCK_GUARD() { RAMBLOCK_FOREACH_NOT_IGNORED(block) { block->colo_cache = qemu_anon_ram_alloc(block->used_length, - NULL, - false); + NULL, false, false); if (!block->colo_cache) { error_report("%s: Can't alloc memory for COLO cache of block %s," "size 0x" RAM_ADDR_FMT, __func__, block->idstr, diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 24228c838c..a7725d8474 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1591,6 +1591,7 @@ static void *file_ram_alloc(RAMBlock *block, qemu_map_flags = readonly ? QEMU_MAP_READONLY : 0; qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0; qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0; + qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0; area = qemu_ram_mmap(fd, memory, block->mr->align, qemu_map_flags, offset); if (area == MAP_FAILED) { error_setg_errno(errp, errno, @@ -1716,6 +1717,11 @@ bool qemu_ram_is_shared(RAMBlock *rb) return rb->flags & RAM_SHARED; } +bool qemu_ram_is_noreserve(RAMBlock *rb) +{ + return rb->flags & RAM_NORESERVE; +} + /* Note: Only set at the start of postcopy */ bool qemu_ram_is_uf_zeroable(RAMBlock *rb) { @@ -1950,6 +1956,7 @@ static void dirty_memory_extend(ram_addr_t old_ram_size, static void ram_block_add(RAMBlock *new_block, Error **errp) { + const bool noreserve = qemu_ram_is_noreserve(new_block); const bool shared = qemu_ram_is_shared(new_block); RAMBlock *block; RAMBlock *last_block = NULL; @@ -1973,7 +1980,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp) } else { new_block->host = qemu_anon_ram_alloc(new_block->max_length, &new_block->mr->align, - shared); + shared, noreserve); if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'", @@ -2045,7 +2052,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, int64_t file_size, file_align; /* Just support these ram flags by now. */ - assert((ram_flags & ~(RAM_SHARED | RAM_PMEM)) == 0); + assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE)) == 0); if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); @@ -2137,6 +2144,8 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size, Error *local_err = NULL; assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC)) == 0); + assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC | + RAM_NORESERVE)) == 0); assert(!host ^ (ram_flags & RAM_PREALLOC)); size = HOST_PAGE_ALIGN(size); @@ -2170,7 +2179,7 @@ RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags, MemoryRegion *mr, Error **errp) { - assert((ram_flags & ~RAM_SHARED) == 0); + assert((ram_flags & ~(RAM_SHARED | RAM_NORESERVE)) == 0); return qemu_ram_alloc_internal(size, size, NULL, NULL, ram_flags, mr, errp); } diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 1ddc0e2a1e..d0cf4aaee5 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -20,6 +20,7 @@ #include "qemu/osdep.h" #include "qemu/mmap-alloc.h" #include "qemu/host-utils.h" +#include "qemu/error-report.h" #define HUGETLBFS_MAGIC 0x958458f6 @@ -121,6 +122,7 @@ static void *mmap_reserve(size_t size, int fd) static void *mmap_activate(void *ptr, size_t size, int fd, uint32_t qemu_map_flags, off_t map_offset) { + const bool noreserve = qemu_map_flags & QEMU_MAP_NORESERVE; const bool readonly = qemu_map_flags & QEMU_MAP_READONLY; const bool shared = qemu_map_flags & QEMU_MAP_SHARED; const bool sync = qemu_map_flags & QEMU_MAP_SYNC; @@ -129,6 +131,11 @@ static void *mmap_activate(void *ptr, size_t size, int fd, int flags = MAP_FIXED; void *activated_ptr; + if (noreserve) { + error_report("Skipping reservation of swap space is not supported"); + return MAP_FAILED; + } + flags |= fd == -1 ? MAP_ANONYMOUS : 0; flags |= shared ? MAP_SHARED : MAP_PRIVATE; if (shared && sync) { diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 0dd7784a88..e8bdb02e1d 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -227,9 +227,11 @@ void *qemu_memalign(size_t alignment, size_t size) } /* alloc shared memory pages */ -void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared) +void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared, + bool noreserve) { - const uint32_t qemu_map_flags = shared ? QEMU_MAP_SHARED : 0; + const uint32_t qemu_map_flags = (shared ? QEMU_MAP_SHARED : 0) | + (noreserve ? QEMU_MAP_NORESERVE : 0); size_t align = QEMU_VMALLOC_ALIGN; void *ptr = qemu_ram_mmap(-1, size, align, qemu_map_flags, 0); diff --git a/util/oslib-win32.c b/util/oslib-win32.c index ca99356fdf..ee3a3692d8 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -38,6 +38,7 @@ #include "trace.h" #include "qemu/sockets.h" #include "qemu/cutils.h" +#include "qemu/error-report.h" #include /* this must come after including "trace.h" */ @@ -76,10 +77,20 @@ static int get_allocation_granularity(void) return system_info.dwAllocationGranularity; } -void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared) +void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared, + bool noreserve) { void *ptr; + if (noreserve) { + /* + * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE + * area; we cannot easily mimic POSIX MAP_NORESERVE semantics. + */ + error_report("Skipping reservation of swap space is not supported."); + return NULL; + } + ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); trace_qemu_anon_ram_alloc(size, ptr); -- 2.31.1