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 467BA3D3CE4 for ; Mon, 11 May 2026 08:54:44 +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=1778489685; cv=none; b=VmOC/lGbPvCdBWOQyK4lkU4cazPTy5WRlOMGt7kmhMZgPYGig9oDS7Pv81+FW1QCLXVUZoL9qSC6g8yHUvTLoxx4fXYG9xWR5l6jinsSMR0wE1YVxTwxaz4X16EnhcBAss6UjDnZqaRmy/LcV6CI7sgWVV0hIVoBPZCildiFo3k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778489685; c=relaxed/simple; bh=uae5V1T54mMZVBXPfWqdPxGd95+bgq8V6VITT9f8SnY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nU7td9tfNNzGOj0QoJbrkJ3Vms1ItBJpjPpu4plDrDn4qJTFsPX8C7avcrEgV45RmO2XWGvOJeFfqz158SaJQ/AA+0cTQF9IV5gsBE6cU35jlUResdHXBPLUiXmCWGhWXJCVh+8RS2tmDvl33kPRO/OBp2P4flVBJ1hCWfzUsK4= 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=GjElyTY3; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=fBViJULf; 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="GjElyTY3"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="fBViJULf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778489683; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=GjElyTY3j90RwEWYxzzepmKV6GKaf8ucO2BvGzcGy3Yyr4bFP7sVqOFoE5u+kFLm25LyXB /8+no/wdIseuqzJ8pihnkKGJRaXY8nnpUIOad07jhgSuXuSzwxSMIp7yNvWXcaGKx3TEzX IVfjqO0GR/heh4FDzTU7UT0q74PmVM8= 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-573-kTsu38kFMJypaotS2lpVjw-1; Mon, 11 May 2026 04:54:41 -0400 X-MC-Unique: kTsu38kFMJypaotS2lpVjw-1 X-Mimecast-MFC-AGG-ID: kTsu38kFMJypaotS2lpVjw_1778489680 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-48e6af7a9cdso16389655e9.3 for ; Mon, 11 May 2026 01:54:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1778489680; x=1779094480; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=fBViJULfSF/Hb4+75xYROw2/kJewapd3jsKWoFoOoaO/6svrjbOUd31KAJqcgvfgfc O30HJNmDvw3LrXqBythfaA1d2AGBgwsUC1T36KQBNBUB8XvZja50r+bnchzfOJLrfh0W G9oT8tLfmNByXtIhtJ7kpXdR9EWGt5m+882PmJCcfTancwKTZk3gR0Iqklc0d0USEq3L kw0JDCiIhtqdfh5HBq1anIyEpRPg+vHDUpNmBYWoyBcG9xmwI/oB+GcMeGX0cPP+6AcH mJujhak5ED2jChLAEVLUacgDKsNr1CVXVGSLoXN8Xb/lc4kjGAnmMKHuQt1Hf1SMOod/ wHfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778489680; x=1779094480; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=m92A2I/jicMkaPNY4Tn9ljSI4niSD2U3T0XkIDkMw4bz6PWmBDWvCdurp2qVTa3zZ7 qa3fr3xlivDr6p9qCRRftzadNb5AS2vdgAtAMCEedW1cc/2/rRryk1rtPargKM9OvZEu x6j0Ojt6ZqqPYzwyJ4detacS7CtzHXIUlPcz4lwygOzThw+TV2riygq0UbaZqDv4kGIZ oO3qdLJiZKlZqpYL9E+FgoUx+UJA9BIAR43aJZ+ZQzNsQ1nEv22CDP11hNII0NpUBhkv SEKfVRoP44hcuTY8od5dxoepJxPvHDI1FovzTmEJcZ8VEmgPg5knsy4FQXFzIGGVBK00 V4yA== X-Gm-Message-State: AOJu0Yx2z8UAeMlRtCEtyyxR+toKMwBO3zCixo/bU8ENxp3i8TcW8ci0 H1ED6nyte9WslgK3fEn7nVDZHD4QMjpfpn1jIUpvIKauVBJjQiOXbEWmzheIsebrhyohIiHy7/o pu2e/svH5oJUrQOlUWkQscpnshJyvqXDGprROWDjydJDkfYquP2LJUyMQzOspvyJ/o+484n1eFN F4Ei3PSAj1+vBhS1HHwcTgoXXPfHLytZ4l/rLn+OxndcXPpg== X-Gm-Gg: Acq92OHgPVRA0EIkPD5dw7KoCGz91jEb1iTeXr0I2xEXXym4uPUFrXc6rfVKqFBuqaw d13SmuQG+oHZaHRJ3v0g8HBd+sAkfj3gEGGD1X1NPU/7ojwry+yiuzCSfokOHTpz8gBRdvCaCQk yF8j8IWSV03JlX8sFpn4/umtkYJrbqbo3EH5nl7513/+Xrvro/pRrFz+nlw478a2YdmIl1jU3Ef nWDX+ix9oWuu8NB7W0Cb3NnFaWzxbhOaeKyx6cHeeOHxc9mrweaEUXTEKv+q/YJkHx8FpcaUTZ5 cZh9ds0Ns09bTms+TTdE3i30q/RcUfKzLTgLSvgWNXmssTuBOENLUk2k+wPtgLRj0n59oz8atKA u/qJyHLqOlD8IpGPs5n6Lvq89HFKIS9d1zgwxZkuJ X-Received: by 2002:a05:600c:c094:b0:48a:599a:3716 with SMTP id 5b1f17b1804b1-48e51f40a29mr268729505e9.23.1778489680078; Mon, 11 May 2026 01:54:40 -0700 (PDT) X-Received: by 2002:a05:600c:c094:b0:48a:599a:3716 with SMTP id 5b1f17b1804b1-48e51f40a29mr268728595e9.23.1778489679389; Mon, 11 May 2026 01:54:39 -0700 (PDT) Received: from redhat.com (IGLD-80-230-48-7.inter.net.il. [80.230.48.7]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e6dafa6c2sm81289945e9.5.2026.05.11.01.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 01:54:38 -0700 (PDT) Date: Mon, 11 May 2026 04:54:34 -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 v6 14/30] mm: memfd: skip zeroing for zeroed hugetlb pool pages Message-ID: <8f111e92b512bf59570021fc0e71eef013289992.1778487000.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 gather_surplus_pages() pre-allocates hugetlb pages into the pool during mmap. Pass __GFP_ZERO so these pages are zeroed by the buddy allocator, and HPG_zeroed is set by alloc_surplus_hugetlb_folio. 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/hugetlb.h | 6 ++++-- mm/hugetlb.c | 11 +++++++++-- mm/memfd.c | 14 ++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 950e1702fbd8..c4e66a371fce 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); @@ -1142,7 +1143,8 @@ static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, 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/hugetlb.c b/mm/hugetlb.c index 8710366d14b7..03ad5c1e0655 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2205,7 +2205,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; @@ -2221,6 +2221,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; } @@ -2305,7 +2311,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) | __GFP_ZERO, NUMA_NO_NODE, &alloc_nodemask, USER_ADDR_NONE); if (!folio) { 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