From patchwork Wed Oct 8 05:28:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 121467 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A87043857023 for ; Wed, 8 Oct 2025 05:29:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A87043857023 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 58F873858C83 for ; Wed, 8 Oct 2025 05:29:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58F873858C83 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 58F873858C83 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1759901346; cv=none; b=i9CjhJw5peC/9cUYPJeQVZRt0AuxKtFwy3qBG3UPcmgvVeOGmOrBOge37eq0VjO8GmHtJ1cTE1j4LUukXEIY296QyGicsri4mtCxA4q0TK/wQDOcwgXqP2QPjEOkS73JYElgXMa+l9/4Q0AuwSGgAslZEyBJgETWGCmHdVFlgEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1759901346; c=relaxed/simple; bh=Pz3xhu3aHNNxJYJKF77DcJW16bPeq/pEYtAiFok3c8o=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=oyIcDTliFY4ZR6TD0h4dpjMPKCDxN++GrsJ79MeP9t/wIsuLSjC2OrehVBVdmDg/hQqFubeEiQ/t2ND5Aq8za4dv2dUqVUUh3gQAjjih1X5R0HCjPORuI2ciF+CwWntghy5umjrffN2jY0Qf7TRX0GPqG4+H+sV0RNZrRaXEzC8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 58F873858C83 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D3A9322EE; Tue, 7 Oct 2025 22:28:57 -0700 (PDT) Received: from localhost.localdomain (unknown [10.163.66.161]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 94E533F59E; Tue, 7 Oct 2025 22:29:03 -0700 (PDT) From: Dev Jain To: libc-alpha@sourceware.org Cc: Wilco.Dijkstra@arm.com, adhemerval.zanella@linaro.org, Dev Jain Subject: [PATCH v2] realloc: Do not call madvise if oldsize >= THP size Date: Wed, 8 Oct 2025 10:58:57 +0530 Message-Id: <20251008052857.25604-1-dev.jain@arm.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Linux handles virtual memory in Virtual Memory Areas (VMAs). The madvise(MADV_HUGEPAGE) call works on a VMA granularity, which sets the VM_HUGEPAGE flag on the VMA. If this VMA or a portion of it is mremapped to a different location, Linux will create a new VMA, which will have the same flags as the old one. This implies that the VM_HUGEPAGE flag will be retained. Therefore, if we can guarantee that the old VMA was marked with VM_HUGEPAGE, then there is no need to call madvise_thp() in mremap_chunk(). The old chunk comes from an mmap allocation, which has already been enlightened for THP. This implies that, if THP is on, and the size of the old chunk is greater than or equal to thp_pagesize, the representative VMA of this chunk has the VM_HUGEPAGE flag set. Hence in this case we can avoid invoking the madvise() syscall. Reviewed-by: Adhemerval Zanella --- v1->v2: - Add two spaces after period in the comment malloc/malloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index f46cfd5eac..3a7fb9477d 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3065,7 +3065,10 @@ mremap_chunk (mchunkptr p, size_t new_size) if (cp == MAP_FAILED) return NULL; - madvise_thp (cp, new_size); + /* mremap preserves the region's flags - this means that if the old chunk + was marked with MADV_HUGEPAGE, the new chunk will retain that. */ + if (total_size < mp_.thp_pagesize) + madvise_thp (cp, new_size); p = (mchunkptr) (cp + offset);