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 C84B039B481 for ; Thu, 7 May 2026 22:23:13 +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=1778192595; cv=none; b=aBiEdGIg/LDtBkBcuXlv5QwpDDfRkd0bdGbtFHIgJZ1pU+B0CHm1REzGpQhTHzeooolMQ7pkG/qYQ63dq71vkSeSBG1s4LjzcGYUFk3TI93qNNB2lIK6P8Tgm+cuPUfQRXIoW37j/mI7qup6GDFljlY17ghnNig/I6hkB5Hi13Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778192595; c=relaxed/simple; bh=VEpka7eQ+qjoP3Y8krH9qirwfLCY6yv7XpuwQILKeLY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RFRjXm/ilU4iR5mt/i6CVVGN4BZ9/jEldJlkH29DOc0xJp5APuZ4BJ45drjjkNZs09NN20+e5Waa+npfWRnHf/NnRAj00Pud/2lr95Flh/RQs23FpbkUW6Lq9lObnd4HaOqpE96diMvuH7DKoYBWvZ72ZKrlmCLJ4EtXWQfi54Y= 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=c8ySwf06; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=nEJ3rc81; 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="c8ySwf06"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="nEJ3rc81" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778192591; 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=ZzA2kE2SxyD+isdRkzcnXWm71tDaAigOtuMMAbID/Ow=; b=c8ySwf06LSTelCYmicfB/HpWzG+Y5SWh5ljqrKcKa/pQ1GNGqxfndej9wSRdnxrqbsIqyB 8F8Dh0L99GnoM9tfqXfgaFyH5Tfc9gi2MP7s2O4QY7XCk+tKtOCt/x3bnM3GObRM2om/g5 wfhIj37u+zMGi61qW7R3nf744zvwXjc= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-568-z60fbatVOkSfhmj0p7C4iA-1; Thu, 07 May 2026 18:23:06 -0400 X-MC-Unique: z60fbatVOkSfhmj0p7C4iA-1 X-Mimecast-MFC-AGG-ID: z60fbatVOkSfhmj0p7C4iA_1778192584 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-44d68ed8f95so912741f8f.1 for ; Thu, 07 May 2026 15:23:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1778192584; x=1778797384; 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=ZzA2kE2SxyD+isdRkzcnXWm71tDaAigOtuMMAbID/Ow=; b=nEJ3rc81wZu4Cyol7nr/1UZroRJHDQevxRROxzXXa3sAe+RVe4bQ1Zsm93ZrOcq7xy oO/rQmLsxu0ANXNW56w1sHyAOest7YyUVwW2ngpRYOnjcc+EQ15o4DUDrL+JuREz3CXF UFI3EgAl+hW7O4ZhZzP1DGn/CYIG9NF1j7Xd2OCc31UIPoxsVwW07Cw0LoGWbuIlMjRc JjHWVKO8sZHApREmd6CZDPrtTMRG+Zt/Gre+xM1vAWlKIobqQBHtXCrsQXbqB1Gnxijb 5rPk1+EkHyjpkMl/wQJWe5k5GBh4mcClzOapB0/jgWiUKmbyQBE9KhcKPY2rbFMKTJdt W3Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778192584; x=1778797384; 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=ZzA2kE2SxyD+isdRkzcnXWm71tDaAigOtuMMAbID/Ow=; b=G65hPO6Bsg12C1IUIp/t4gFwEM7qNzy7GSCqSrvq9I6SCA1M38Z/vn1L2TC+bh4XtB eHL/PCdKpN8i5GxmGNO6qWmTC0xgsSUc3x76IrL3GIeH2cIUhjHJGxHmxCPsGVVm+YK6 JoUyMq3Le6F9fQjbVfL4/wGpkI8jl28IDMB7hh30aZlQD1nJMnyFrBBONbpgdwPEQ3Ot OQJ8aXrqCL8tf1I0bKQMdEQzzk7x16k5ONkQZahcScV6vG+yZaqgfUrszmxDyunhjdew /sF1K5Q2M9HnNGKIEXv7YdSxSrCL7X0nLW46nK9n4jSLtr51xJep9JOmGFhY2ws+BCKx hk0g== X-Gm-Message-State: AOJu0Yw9SyLYloKvBGbtu6/4fR1s9WDmxIpVm/QBImeOU4ULHfEzVK6s EHKQYZ8mVpxcGanJg1hKalIlVL2aIySXe2Pl5tbcNc3TyyKQLRioP2ZWNtkeaI94OQYVBTTG5II 6nRUnPup1Z59bO78Uxv5OKqODMVyw23jRAe/lunT/CgLLATLkJkyOLs5zY/bFwABvU/m3lGAixO YoP9aOZ4mlsWv2NbZlkXYJWcBI8akCH76ukbWqBnt7jK0= X-Gm-Gg: Acq92OFN6ZK1IhGuFnoyjg5lSJEGvssHAD7CZvPeCqgpx8DfnW+hopWLAijGhKUaw02 ytH83vKWoUcfwxMojYXKBLyYGmS/70eMPwmBmWkUEocMstuRzRMa2GTc0aK+RFD70oP3Or9V9Y8 Me3E84YCrJyfne2KQBhbNwpth1Q9//WhK5Ee4D/X2bDvvbuUAcpwCB8BG2LxbQ2qkCIPQwWP5xu X4UVVSl1Av1j/RksZ08wiqKSn04+QJtJJaWcbyJa/kOaNLh94pv6UfESMGG83SJUNqny6JnYqWh eIpoLvxP3p+WBeb1YwX4iPoLqFEal0JO5xsRiPJmT93XhPOlSrpiE5fpc1suPvgENyCfxGIawty eoQoYbnZeglc2U1XjR+Qt0x79mG5GtewxDPkgWjMv X-Received: by 2002:a5d:5e01:0:b0:43e:531f:720c with SMTP id ffacd0b85a97d-452e96d51bbmr7220463f8f.11.1778192583675; Thu, 07 May 2026 15:23:03 -0700 (PDT) X-Received: by 2002:a5d:5e01:0:b0:43e:531f:720c with SMTP id ffacd0b85a97d-452e96d51bbmr7220413f8f.11.1778192582995; Thu, 07 May 2026 15:23:02 -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 ffacd0b85a97d-4541721bab8sm1995822f8f.27.2026.05.07.15.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2026 15:23:02 -0700 (PDT) Date: Thu, 7 May 2026 18:23:00 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , Hugh Dickins , Baolin Wang , linux-mm@kvack.org Subject: [PATCH v5 12/28] mm: memfd: skip zeroing for zeroed hugetlb pool pages Message-ID: 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 094714c607f9..93bb06a33f57 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -713,7 +713,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 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 b5bc2a9f5022..ad536d7aee59 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2241,7 +2241,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; @@ -2257,6 +2257,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; } @@ -2341,7 +2347,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 919c2a53eb96..4026fda71762 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