From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C18AE3BFAFA for ; Mon, 8 Jun 2026 08:39:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780907998; cv=none; b=XsfL2ZfsLcBAO+wagVj6ZJ0fCxrNkYCKIMuA4WIif7Y8iRHVM7mxCEZPrtSTPJDd3izLekw6rzLv6garApWcGJa0rL5x2nlM2iBEBQ3PSMv8irJWB6h63frLBrMgplsyqJ4HlNWhKkTxh5A2bQSWAq7JcBdn2obRpCIB3Cv++tY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780907998; c=relaxed/simple; bh=uD9B0Z2r8dApvmwrr4a0YLqj2hTMaiDgCysJxcZ5WsU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=KUl6tLIYY/IaZsvWd4zGyHlVcNkOiGKG7d6pAgGzlo9eMdy6kKty4+dGo434v0dkwpMUHsBivHYuiycrvSF0OP2n6ygpOWg8R/z41/mX0omPLEq85gsMe5aWWy2a3GopkyPNkAALzZRdsC/QEqNIXt3obdtcZnzWfIGZC+lN2qQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=djOaZnLL; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=qE0sxdoT; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="djOaZnLL"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="qE0sxdoT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780907992; 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: in-reply-to:in-reply-to:references:references; bh=ScAoSfQrcD4R0Yhb9nAUBLjdu8qZL5GRGpRfEb8F90Q=; b=djOaZnLL68tiyDAA1RNRRLjf7i6w9D0+hyMhUbXNybuejkcs8cciJYlviaGWH3tnp2vxRn PmJ/bUGptBD9ZicZ3KwgOMLSQrQcjjalYs5EsgtDEYO/Arr9yjdgckpgYjuQMK8/YGwAwD tifQXUyF9w0v1taJAmkDO0kwnI6KZgo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-410-9U33sh7WNliG3xWSe2iFCg-1; Mon, 08 Jun 2026 04:39:51 -0400 X-MC-Unique: 9U33sh7WNliG3xWSe2iFCg-1 X-Mimecast-MFC-AGG-ID: 9U33sh7WNliG3xWSe2iFCg_1780907990 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-490b7959b9fso41826745e9.2 for ; Mon, 08 Jun 2026 01:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780907990; x=1781512790; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ScAoSfQrcD4R0Yhb9nAUBLjdu8qZL5GRGpRfEb8F90Q=; b=qE0sxdoT0LlQcezUjcJpe+ChEv1ZJ6WoapcZmuE+WqwwcOMjo59KdyWpyFObwF/Lfi Dim2mBGNOo0zC/shxQ07v0QeKDyBLts/ZlGTrBHhsIfEdWtCNd/sOsLNLTz+IZ3747aM Bjsau0nt/HWCPDJDgNVSXwQwY9fkeMENTY+7+4Q56AzQleEguJKUm1GneO3lfynX1oIX JWyatfKQGwp4hGE46fFiRPo2enog5VbAp5LQOOzxihUGa8dd8MnLk+N3plwGWkp84MAR U9bxqI1+2HLMQiqh1zHA4XyBqzw7GFCi5KWmODsbVq0f8tW2wsz1AOomtV4rYAFgxMAu /xLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780907990; x=1781512790; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ScAoSfQrcD4R0Yhb9nAUBLjdu8qZL5GRGpRfEb8F90Q=; b=Yp8ffYY+peKLxNA120H4RcWzh7pEbKqpemRDiO381HsMtsf1FIluzzg2vPQ2/0ml3B honfBabA9b8CMzBszZTsWuypN5pbUFjCZ22EfetxcOM63JI9gc2cBahVY4Sw78/IBP1K pyFmCsQ1L5YO3NT7HNnQ/vkZdFmNOs/nWiwIEZEApNd8u2s98GPMrVqpbyWoercjsnil VYxAnXn7AEVkF0m1XJB91bLcmbuCXGX06L87/GGE7DZ/Z0b1Cvw9kXIaet3HzeJ3VbuO D0w2XpzMYohipMPaS0FhWODPmYMxdBVXRfNi5Hlg+tXsVPTzBnyZNcYUuTSKxPvrNX8W wDaw== X-Gm-Message-State: AOJu0YxsPOXmFtvUswi4kyPQM4ay7qzLmPcOPAF4hsQ4SxnMHdI4hUNT 6jQ0e2pjc/SCSL6hA1BBPINewF8PR3FgpSciz2YEkedMgll4PWYAUDvZ3NdJbUJTbL/Lvpudlnx XOYJeI7Mv2BZw1fcCDMtBxszP2Wux8sSuMjVvUnlwRpCV1kuunuVE26SVoHMXrFw5ampIMgh3f2 yWV6dY1dERtFii2m28D7OjgS2/6vkjvvs+bcgebd8i2xk= X-Gm-Gg: Acq92OGAhgkjZxIX1YXEnQ87sIfzqi8/WBfKxR5GxVoHJMTSVqrMTcC0FccP8DFR1Pm 8+SpJdsFGkjH6erUXA27Zv6W4q5mNjcDoggdmBAqhiS46tZEIg916cGRLk10jLO7AvJAhYoCUa6 LL74Y0TQg+YdzkEvTJ3qffbx83omV31Q2WhjW2s4eTmH/kxF87EyhiT4T69JqjPFrl9JWszbiTd sU0/ify30nXl9AaluBHm6iC2BIF5QrQOCael/slE0Y9SnQZnIsR4mZkEQo1LB1dXeStcjzRWU+6 91Ju4FsKQv4AItUAs7cbkZPGaSgCq8ZdK/mZ3GyreYjUJ2SJuFnEKqo2QaLUwSL/8dC9DoA0kol CFYScgBFYW9oQf/6sxA6s1NM= X-Received: by 2002:a05:600c:3e15:b0:490:adb6:793d with SMTP id 5b1f17b1804b1-490c25f67d9mr239705435e9.26.1780907989635; Mon, 08 Jun 2026 01:39:49 -0700 (PDT) X-Received: by 2002:a05:600c:3e15:b0:490:adb6:793d with SMTP id 5b1f17b1804b1-490c25f67d9mr239704145e9.26.1780907988974; Mon, 08 Jun 2026 01:39:48 -0700 (PDT) Received: from redhat.com ([31.152.37.159]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490bc3d66c8sm356783495e9.10.2026.06.08.01.39.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jun 2026 01:39:48 -0700 (PDT) Date: Mon, 8 Jun 2026 04:39:37 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: "David Hildenbrand (Arm)" , Jason Wang , Xuan Zhuo , Eugenio =?utf-8?B?UMOpcmV6?= , Muchun Song , Oscar Salvador , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Hugh Dickins , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo , Axel Rasmussen , Yuanchu Xie , Wei Xu , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , virtualization@lists.linux.dev, linux-mm@kvack.org, Andrea Arcangeli Subject: [PATCH v10 29/37] mm: memfd: skip zeroing for zeroed hugetlb pool pages Message-ID: <0d6c2d31f48ff454223ad4f1d37ef7b73263bf5d.1780906288.git.mst@redhat.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent Add bool *zeroed output to alloc_hugetlb_folio_reserve() so callers can check whether the pool page is known-zero. memfd's memfd_alloc_folio() uses this to skip the explicit folio_zero_user() when the page is already zero. This avoids redundant zeroing for memfd hugetlb pages that were pre-allocated into the pool and never mapped to userspace. Note: HPG_zeroed is currently only set for surplus pages allocated with __GFP_ZERO (via alloc_surplus_hugetlb_folio), not for pool pages from alloc_pool_huge_folio. So the zeroed output from alloc_hugetlb_folio_reserve is typically false for pool-only reservations. It becomes true when surplus pages fill the reservation. The addr_hint 0 passed to folio_zero_user is acceptable for memfd: these pages are not mapped yet and will get proper dcache handling at mmap time via the page fault path. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- include/linux/cma.h | 3 ++- include/linux/hugetlb.h | 6 ++++-- mm/cma.c | 6 ++++-- mm/hugetlb.c | 11 +++++++++-- mm/hugetlb_cma.c | 4 ++-- mm/memfd.c | 14 ++++++++------ 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/include/linux/cma.h b/include/linux/cma.h index 8555d38a97b1..dee88909cf5d 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -53,7 +53,8 @@ extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, unsigned int align, bool no_warn); -struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order); +struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, + gfp_t caller_gfp); bool cma_release_frozen(struct cma *cma, const struct page *pages, unsigned long count); diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 06d033a57a61..7eb529eabe99 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -708,7 +708,8 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback); struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask); + nodemask_t *nmask, gfp_t gfp_mask, + bool *zeroed); int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping, pgoff_t idx); @@ -1128,7 +1129,8 @@ static inline void wait_for_freed_hugetlb_folios(void) static inline struct folio * alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask) + nodemask_t *nmask, gfp_t gfp_mask, + bool *zeroed) { return NULL; } diff --git a/mm/cma.c b/mm/cma.c index c7ca567f4c5c..27971f6264ab 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -924,9 +924,11 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, return __cma_alloc_frozen(cma, count, align, gfp); } -struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order) +struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, + gfp_t caller_gfp) { - gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN; + gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN | + (caller_gfp & __GFP_ZERO); return __cma_alloc_frozen(cma, 1 << order, order, gfp); } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ed00db703911..a087e915783f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2196,7 +2196,7 @@ struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h, } struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask) + nodemask_t *nmask, gfp_t gfp_mask, bool *zeroed) { struct folio *folio; @@ -2212,6 +2212,12 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, h->resv_huge_pages--; spin_unlock_irq(&hugetlb_lock); + + if (zeroed && folio) { + *zeroed = folio_test_hugetlb_zeroed(folio); + folio_clear_hugetlb_zeroed(folio); + } + return folio; } @@ -2296,7 +2302,8 @@ static int gather_surplus_pages(struct hstate *h, long delta) * It is okay to use NUMA_NO_NODE because we use numa_mem_id() * down the road to pick the current node if that is the case. */ - folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h), + folio = alloc_surplus_hugetlb_folio(h, + htlb_alloc_mask(h), NUMA_NO_NODE, &alloc_nodemask, USER_ADDR_NONE); if (!folio) { diff --git a/mm/hugetlb_cma.c b/mm/hugetlb_cma.c index 7693ccefd0c6..c9266b25be3d 100644 --- a/mm/hugetlb_cma.c +++ b/mm/hugetlb_cma.c @@ -35,14 +35,14 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask, return NULL; if (hugetlb_cma[nid]) - page = cma_alloc_frozen_compound(hugetlb_cma[nid], order); + page = cma_alloc_frozen_compound(hugetlb_cma[nid], order, gfp_mask); if (!page && !(gfp_mask & __GFP_THISNODE)) { for_each_node_mask(node, *nodemask) { if (node == nid || !hugetlb_cma[node]) continue; - page = cma_alloc_frozen_compound(hugetlb_cma[node], order); + page = cma_alloc_frozen_compound(hugetlb_cma[node], order, gfp_mask); if (page) break; } diff --git a/mm/memfd.c b/mm/memfd.c index abe13b291ddc..a99617a62e33 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -69,6 +69,7 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) #ifdef CONFIG_HUGETLB_PAGE struct folio *folio; gfp_t gfp_mask; + bool zeroed; if (is_file_hugepages(memfd)) { /* @@ -93,17 +94,18 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) folio = alloc_hugetlb_folio_reserve(h, numa_node_id(), NULL, - gfp_mask); + gfp_mask, + &zeroed); if (folio) { u32 hash; /* - * Zero the folio to prevent information leaks to userspace. - * Use folio_zero_user() which is optimized for huge/gigantic - * pages. Pass 0 as addr_hint since this is not a faulting path - * and we don't have a user virtual address yet. + * Zero the folio to prevent information leaks to + * userspace. Skip if the pool page is known-zero + * (HPG_zeroed set during pool pre-allocation). */ - folio_zero_user(folio, 0); + if (!zeroed) + folio_zero_user(folio, 0); /* * Mark the folio uptodate before adding to page cache, -- MST