From nobody Thu Nov 13 21:51:52 2025 Delivered-To: importer@patchew.org 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; 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=1582798907; cv=none; d=zohomail.com; s=zohoarc; b=NnG6rqylju3XNeCla/15q3XPKp0263XVSCOmPauz4DcekSZfeB8FQ1yE/oi3szXhxD+9rV59FdKgIV9dBg/2VJtNJb262Da4+E2wm5wJhdtHF5U9e/duSVcngCoHTdAx9vNTw6wD7FWOtWMXVxVtzAV5w1uF+tvNHo8L2KdTl7U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798907; 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=0M+moTdl0qV7+Qhg/JRf45CFzQnG475adDlEdMRPHAI=; b=jYB/Wo0x7L5Qq1CT9faNWztz4iqheLsxvBxirtH8IJdQ+7yfL+mTEIwpqH1dR+KkhofThJFA0Cfhnw6wHOpctHMUt+/CNb0Y+kJq8vUizMkdat7bYPlTaHLEeRCCE5s7xKLdDW462Jeecck5a8Bh5ZaBDid9ir8mO2iqUDCTaRY= 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 1582798907573107.9548376254653; Thu, 27 Feb 2020 02:21:47 -0800 (PST) Received: from localhost ([::1]:56658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GIc-0002Jo-BB for importer@patchew.org; Thu, 27 Feb 2020 05:21:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40010) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBF-0003VA-ML for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBE-0007wn-Gs for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:09 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:26460 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBE-0007p0-CS for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:08 -0500 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-404-atXLREa4MFGqPwq_dOlFQQ-1; Thu, 27 Feb 2020 05:14:02 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3F27D800D53; Thu, 27 Feb 2020 10:14:01 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82A6E6E3EE; Thu, 27 Feb 2020 10:13:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798446; 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=0M+moTdl0qV7+Qhg/JRf45CFzQnG475adDlEdMRPHAI=; b=YZnzfpOgTLLGcaJdyHBdepLDuoC9YVD6rayMsIh+FoU5H9KQNQeCIU1CmcI0iiPUTVxqC2 XIT8Y6oU80TLIU0Cbq9HObYHH1ecR8zXPFjmZf95T85IhJi1wKHQ4TNf2B9pfuNW0+RNGU nAPaqsuvA87imACWnt9bfUNvkfSIaGE= X-MC-Unique: atXLREa4MFGqPwq_dOlFQQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 08/15] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve() Date: Thu, 27 Feb 2020 11:11:58 +0100 Message-Id: <20200227101205.5616-9-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 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: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson 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 reserve a memory region without actually populating memory. Let's factor that out. Reviewed-by: Igor Kotrasinski Acked-by: Murilo Opsfelder Araujo Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index f0277f9fad..9e9534a07e 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } =20 +/* + * Reserve a new memory region of the requested size to be used for mapping + * from the given fd (if any). + */ +static void *mmap_reserve(size_t size, int fd) +{ + int flags =3D MAP_PRIVATE; + +#if defined(__powerpc64__) && defined(__linux__) + /* + * On ppc64 mappings in the same segment (aka slice) must share the sa= me + * page size. Since we will be re-allocating part of this segment + * from the supplied fd, we should make sure to use the same page size= , to + * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to + * avoid allocating backing store memory. + * We do this unless we are using the system page size, in which case + * anonymous memory is OK. + */ + if (fd =3D=3D -1 || qemu_fd_getpagesize(fd) =3D=3D qemu_real_host_page= _size) { + fd =3D -1; + flags |=3D MAP_ANONYMOUS; + } else { + flags |=3D MAP_NORESERVE; + } +#else + fd =3D -1; + flags |=3D MAP_ANONYMOUS; +#endif + + return mmap(0, size, PROT_NONE, flags, fd, 0); +} + static inline size_t mmap_guard_pagesize(int fd) { #if defined(__powerpc64__) && defined(__linux__) @@ -101,7 +133,6 @@ void *qemu_ram_mmap(int fd, const size_t guard_pagesize =3D mmap_guard_pagesize(fd); int flags; int map_sync_flags =3D 0; - int guardfd; size_t offset; size_t total; void *guardptr; @@ -113,30 +144,7 @@ void *qemu_ram_mmap(int fd, */ total =3D size + align; =20 -#if defined(__powerpc64__) && defined(__linux__) - /* On ppc64 mappings in the same segment (aka slice) must share the sa= me - * page size. Since we will be re-allocating part of this segment - * from the supplied fd, we should make sure to use the same page size= , to - * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to - * avoid allocating backing store memory. - * We do this unless we are using the system page size, in which case - * anonymous memory is OK. - */ - flags =3D MAP_PRIVATE; - if (fd =3D=3D -1 || guard_pagesize =3D=3D qemu_real_host_page_size) { - guardfd =3D -1; - flags |=3D MAP_ANONYMOUS; - } else { - guardfd =3D fd; - flags |=3D MAP_NORESERVE; - } -#else - guardfd =3D -1; - flags =3D MAP_PRIVATE | MAP_ANONYMOUS; -#endif - - guardptr =3D mmap(0, total, PROT_NONE, flags, guardfd, 0); - + guardptr =3D mmap_reserve(total, fd); if (guardptr =3D=3D MAP_FAILED) { return MAP_FAILED; } --=20 2.24.1