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 89A69421F14 for ; Fri, 29 May 2026 15:24:17 +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=1780068259; cv=none; b=LBrdbMSpoCDiYSLZDKoUvhHBan6TL9sSvbPrrwg5hd/gfRGUpyteHHbSvLlFn2LzP0bqSo3VdWvuG5AvqsoCLyit9/3c2eZq6DfsF0RgcxRFAPCPMHE5XjwNg6tiGkd9CP5uufqprgn8WieR/QughN03VEFMAYov6/ByC3vM10U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780068259; c=relaxed/simple; bh=Axczvl8C15WZZkzyVXb9w4AL4G4ah5FFkNusKG6v0bk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gYtZhbxqEpruqbq2jb7ak0o0FkFW2D0bV1sXLjI6kmStlEd8JUV2DwTh1gHeMlAOsL0uVdIx2bQVvC2DADCXTvXnmqHZStF1tvQ//5K6pXMJ42EnqHES6Q3f8XtjzyOedElp80PMZREymwF8Vct07G0kLGYWQ793vrnc5g1sbAU= 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=hmjHS/xz; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=CxejAr9l; 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="hmjHS/xz"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="CxejAr9l" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780068256; 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=Ha0MKvEjPpqKYjU+TNR9cOwfxNwOYM3BFVBNkyNiIfo=; b=hmjHS/xz6CWhW6iIu6yN2Rj+aC6ePhv1TnPgBFi2JTpyqu0lb0JeLiP82KBRifb3qI/72m lAiHZuNk42XbfB67HCHlmW8iS0KLE40f8i6U84rvNO3QeIjGUZp8r3YHr/vWaZj5HvFpol iaoRJUHx/4Gy76tmL1+SgeC8/mTYrS8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-218-f2l9qu0MPE2GLYFK3rPcHA-1; Fri, 29 May 2026 11:24:15 -0400 X-MC-Unique: f2l9qu0MPE2GLYFK3rPcHA-1 X-Mimecast-MFC-AGG-ID: f2l9qu0MPE2GLYFK3rPcHA_1780068254 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4903dcb32f8so69896415e9.0 for ; Fri, 29 May 2026 08:24:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780068254; x=1780673054; 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=Ha0MKvEjPpqKYjU+TNR9cOwfxNwOYM3BFVBNkyNiIfo=; b=CxejAr9lWDksrvKmsTrsjjJq3XQZMgX8fCSdk7Dl53tHCAFEYkfqOS3soRQ4YcVwbY YkZZxunQI5ccwDhbqZasF9UJME20r7QdlZooK15fz5n+xA5uvPlr4S0cdQg6y64jp/s+ nyoCVK9xJV0YM3V28GYV0HP9DuBC7rN6FpslzhV7zaOPAwbC0sz63tgOU5/v0+9WHxrc E/ccCJEgZvdoCUHU1jgNOYyyadBGl5RCqqI0C0l5kurNIG7MSxIlNZwqlHrhd43zzoDq ai0NNBPkpVElasPJ93tFeae9/qCmT/61YlvPqO1i/BFE7ONY7nqSJV2r7ZgRD7mrYryN 8uJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780068254; x=1780673054; 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=Ha0MKvEjPpqKYjU+TNR9cOwfxNwOYM3BFVBNkyNiIfo=; b=Ln9kXDlnaQ23LQ72JLyTKZjAaYf6a44WSF7IPxMuWYDBR/3g5aKLtKSbVjYyHzZbc0 hGvqN7SVy6O98QPk9vckG7LPXfp3D//bbsdMnyOAZ36hpy22SNviEV8aLAcBh5U6EB1y pjMSFq+bBiPg27wwmM9GA3MEAUwz4znjZXNxjzolcNPTUi3atGyFHypGGYe8+WDt2ICh JxxeY0fhveOQ52y+Y2v1AHPImlhlZ3KXdb/59wZ5ueqCG3WoJ5I6Vzsrg0tYVTTzFiRC DK0+VoevOYtrybhVSIyB8KO3ZD6IDyeV3g/aVdpND4/REUuYezIAv/aE2T437s3p9Spc Rtgg== X-Gm-Message-State: AOJu0YwijtzWkfz/5JSmwa9ZwtkIld6DR5sxioznJdq9GucpLJPZdvWL H46HJ0Ranhz7iJi23U4Uf2xOrU0YAGqita8O73QxYtHkNbQId4fq7Z/Q1kM11CqIMfR2etYoAif A+TMchcabzuXAiF65ncWL1/EswqnzNUfj/aR77N4lQOKzfUmf8NZAIOJtsVxXu2rNRdk10CaK+8 sL0ZMwmAC/opUEO7DX14GQszfyue93ll6L0+9zGa6dycw= X-Gm-Gg: Acq92OG+MkfTMWXuF/vouaO3bf2cVSm9NzRZAoT0W+2qlf+xInTXawr8Ssag0D6txNA Rts4gTN847eT2H/3lg7oaGBevp038fKHz6+KOGVG3IC4Dq3Tfgl6gNJw8j9GxfYfDn0OI/Esc5O s35CraR+GyH3+wK3+HcQvfXou+GPTv96NRXg9Y0qt45PeUlp82nd0s2rVPnS182QQfaUoo49Dae qMxHryVI0CR0bDIH020h6NzQN5pwqOqvWU8A7ysDhlGASHcRLVkxIQmyYI3/8SI3He2Z8QeEjxp V5huE5N37ksiyQvga7hKGPakXwyA8qEQ7XBT/EjDLi9nNmG+TeGT66vIlnsi9F/qE4O3IxjrRXj qehrrehLrZMxX4R7KX73yuDTaRXKtgYv9+Yc8//RsZivN3yKXVZsq3Q== X-Received: by 2002:a05:600d:640f:10b0:489:2005:b36e with SMTP id 5b1f17b1804b1-490a2941d4emr1179115e9.19.1780068253562; Fri, 29 May 2026 08:24:13 -0700 (PDT) X-Received: by 2002:a05:600d:640f:10b0:489:2005:b36e with SMTP id 5b1f17b1804b1-490a2941d4emr1178125e9.19.1780068252950; Fri, 29 May 2026 08:24:12 -0700 (PDT) Received: from redhat.com (IGLD-80-230-25-45.inter.net.il. [80.230.25.45]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4909c103a89sm16422195e9.20.2026.05.29.08.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 08:24:12 -0700 (PDT) Date: Fri, 29 May 2026 11:24:07 -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 v9 21/37] mm: memfd: skip zeroing for zeroed hugetlb pool pages Message-ID: <43b19542a07b568279d1a6e734d6729d347b78d5.1780067977.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.27.0.106.g8ac3dc51b1 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. 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 49e5557d6cc0..3d23267014e6 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -714,7 +714,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); @@ -1134,7 +1135,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 4ccf4eb91d92..649972101ace 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2208,7 +2208,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; @@ -2224,6 +2224,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; } @@ -2308,7 +2314,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 fb425f4e315f..5518f7d2d91f 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