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.129.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 2ABC23C0606 for ; Wed, 20 May 2026 22:22:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779315746; cv=none; b=cRFmCXGREU90AGBjiLO26FbueMHWnczQWE7pEeAgsZCKPZu1a68d6AOCJXPtbSqCs/xQQwXKsb7MVI1lG2hKwKrbKZMU5zWz1NBspgbkViZ2lEavNsqJPvDorqnMjy9GHSx1mVnhU+jf/ZnB7sY50JLOPl7PRuvB8ZLpbPetcf8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779315746; c=relaxed/simple; bh=Axczvl8C15WZZkzyVXb9w4AL4G4ah5FFkNusKG6v0bk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JkViN9+ZnTQ4UHhL2q5xWtlj1zIQzYeAhH5KP/H/4MsJMULD5ayva14fDiIbjCjUOwuLNsWkNkUNNL8abePFAN280wjnQ9xewQtK3Fvi9nvMpvNhs/rhamNmRGxXiGWRMx4ddlSjd9RO1Bf6s5nFmKGHRIypps3gMOE0WtG0sfk= 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=M8Rzke9D; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=PA8A9GfH; arc=none smtp.client-ip=170.10.129.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="M8Rzke9D"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="PA8A9GfH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779315744; 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=M8Rzke9Dcg0AYjal93ef4JM+IULMFy9nSs6XpFb4RwnTIENJHGkVqS26rIFVHGYW6A0okG dA5MA2URC/G3oxXfXa4q3NBp9F4ZfSKTNiClL69qmK+TtCxGdndb/V6aH1sk7kVd25QIIe BgJBjVKQZkbGb1k5XoiLe3BFRcQfeJw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-111-yZh-gSlTPI2bNENjHFe6Mw-1; Wed, 20 May 2026 18:22:23 -0400 X-MC-Unique: yZh-gSlTPI2bNENjHFe6Mw-1 X-Mimecast-MFC-AGG-ID: yZh-gSlTPI2bNENjHFe6Mw_1779315742 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-44d83e45febso4336397f8f.0 for ; Wed, 20 May 2026 15:22:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1779315742; x=1779920542; 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=PA8A9GfHcWGmoIwQ2ECXHbI+/0UNrQic4DAWpby5cTP+sYs681LGKgUNU72rdDy1fH dflhc0a15L/PnriGSE8WQa3TeHR+Sx3owr3GiB7SUQNLF3eHmSymbg2C9+1vq8um2PsS wob1wMbhnKBP5izJcw1sUcYqdAWkoDBAWf+XdM4aGLIfDXuS03fY3TKtzfKH9+9PYfHt w4WNcC6I77+QihfLlDUsbu+UKcWdaPWe6xtYNdVFZA5gVTHAH864aEAUT7FZ5411qmbb gKCWmK/skDW9Q4Bo1jsuuuMdVsUFLSyZ1xoHJUSgyAUXRJhEJd3kNu8qev/aDXJubnZV vsaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779315742; x=1779920542; 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=gDSp1LYUAc+VDA16YO6pngUGmbPZGYFZ4MnWa6ob5cYh9LWPADaKY8/qDOjFu9FYzP mtImZPAH5hsckuges+vFvN7syD95FJxyctFRjCjGN97+733ASOAqn0CLW5IJOVF/Vbm4 VVq962fpF0VIOL2BRoCp2V0gOLmRe64wx1tw3XbOzw2JniwSioam1nBRrWo6tj2Zlx+C 43qlQ9VPBVI0vdqakVxoarRWnrVUKA8vZVcAeFd6MHSPeo1tLsiFWip/7t013+JymOgk Xtj9nwrYh2+13coWsJI/g7LQgnk0mvQYVAQCYHBl6qiWNcnkboglJHZtZvQxFCu/nnzC j+oQ== X-Gm-Message-State: AOJu0Yy/Mo971mfw6PcHATQeqg5dMUtzsfJQ4zkL3MOZav5/a8sa+kjA PjKQ74PhrCcGFKdfuVQiZo5tf7KCl/8MzZblwpc+AmGDpUb3636iCEYmc9WbYg/LYIbOd4hcath rB+/KqywIn0AgoXUlXR3SRClgglMrCFD7HJxxtiYJaAAtSWrvQ4hu6GmiPbewITEh8wSye4kcmH w59GFLLs9IDmd1PkydjzL9k9/q+i4vS0CLmEszIPxJKVg= X-Gm-Gg: Acq92OF0Ofp1fYO/0GFPNwjxXOonpFDZklBCzz96lQ4QfxJTuHFH05NXhc+cMP1NFQf ShNqJ1ItVRmiKsSH9DBy/pY/Gcki5iMNc1JC7n8l1MyFCJ7bbcarV6kv+ZrfY6K7wVtMVo3aNoX coEx2ouUIAyExY6SpKMyKQ27Eb0sFllH/IfJNX+J/5Y4QfrnRYdqaqikGBazKf3Zm+KpUnwgqoq 6TmZ3sKRsrJh3cFDrZLViVuVruuBt0S6Z+wORyF+kCHLkuXTkrmoaF14BJgR+0CFMHQhhXrr9bf B2NShXnSC3rPvA84KH5YOkvfIamDDWGsqFWxy3Ld4izEMI3AYAyd7FRunvVt0eRE7bw9lLf0vj/ t3531ZBuiZ79o3SQ+bqc+4SfRHiXoI2TH+L13kiFZG2g= X-Received: by 2002:a05:600c:8286:b0:489:32b:ac0b with SMTP id 5b1f17b1804b1-4903606a0bdmr2450975e9.6.1779315741749; Wed, 20 May 2026 15:22:21 -0700 (PDT) X-Received: by 2002:a05:600c:8286:b0:489:32b:ac0b with SMTP id 5b1f17b1804b1-4903606a0bdmr2450295e9.6.1779315741251; Wed, 20 May 2026 15:22:21 -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-49033d3f99csm31986145e9.6.2026.05.20.15.22.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 15:22:20 -0700 (PDT) Date: Wed, 20 May 2026 18:22:16 -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 v8 21/37] mm: memfd: skip zeroing for zeroed hugetlb pool pages Message-ID: <05befd0e641749ea70c1289f5a6ea7edaed266d7.1779315441.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