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 919963074B1 for ; Sun, 12 Apr 2026 22:50:58 +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=1776034259; cv=none; b=cbTtLL1IfmUVNf7sm8Bb7AQAho/ZNiyX9MtGYqkEiY157Hbt0IXtPqJ36NODf7B26QSJlYnXrnKEX3/J2kaLf3/D9SBLPY74/fJhBZJxryK5K0KD32ny/se9Ypk1BIA37+cydl58Xjz4cKbuwHrB8VI6ixwRuhKHmSlyUfDcdbY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776034259; c=relaxed/simple; bh=tyevfehojZyLx0rvZbifr9tVTcR+LX8179KCtTitZQc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=abPBsTrGvmeFaBofwP8PidwkFJS+tGuzDOyfSid0zT39YBAmkxt/p6D+3G+mRPh+JnIOfsSgQvsiKQevXv1W5G7qbGEZakvk/HyZe5q0a6dlQjnFhXyzuN/qEosCLdmMDYXFvjRgPPB5psRo/EkWi+EfwDMhhpHtqWmY7Z4ePRw= 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=TfpkEU1b; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=cc8yJWsj; 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="TfpkEU1b"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="cc8yJWsj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776034254; 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=jGJCJcDO5/5MpQsdc/D5wN/ajh0fH6KE567pAXfVmHs=; b=TfpkEU1bj76XO8igyArXdG7UaZjYbdUeNqR8kEFdZl92+wwqF4nCsI0kiD33CE2E457IRI GLsjACjFcXS7c1mr68TVqVB/BAOFS3p2yUTWTasYJ4yfZOnGq9k3AnQke8KcJLjuZLfAAC LsRH5R3JqGbHpQsVpdZaRjcQiC+oYbg= 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-611-kgIiLtx_MGW1Qa6eeQzUJg-1; Sun, 12 Apr 2026 18:50:52 -0400 X-MC-Unique: kgIiLtx_MGW1Qa6eeQzUJg-1 X-Mimecast-MFC-AGG-ID: kgIiLtx_MGW1Qa6eeQzUJg_1776034252 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-43d03065782so2219027f8f.0 for ; Sun, 12 Apr 2026 15:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1776034251; x=1776639051; 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=jGJCJcDO5/5MpQsdc/D5wN/ajh0fH6KE567pAXfVmHs=; b=cc8yJWsjEObCvyz1KeiOdf7aPnzmVQot1eBio1fYVuFj3MR/uRJM3c7gPQW23KdrOV ywuBHv8tPKAJ4Ce9Su4rOyE8n3wIqoQAjSu8pqNgLDXvUFshiRvfhizaV7TJRJWSDbzt yT1OOgBwImVMvzdXzmqyQlc8jBUxO90HLCVgSNJeHnmdNRm86adTs/QPPGeN0WRWc0hO 9I0RIq+3aC+lEurzcfpUwMZW/OT2ZmPg1k9MZI2lKxT7EB4lQNtvYT+yecreX0ZJsdZN DtDyiJP+0T+G+U4OTSpGdadbh2EOSTIHjD7kg0a5ofLqpgXr2a1eEWOlG5YMVB+XEOhz oZfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776034251; x=1776639051; 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=jGJCJcDO5/5MpQsdc/D5wN/ajh0fH6KE567pAXfVmHs=; b=rkj7QfuZ508u3vLGplUwMBee5U1g/NxDxYx48DyqJ/g433Ny8iYiC5/yNMx08HCgqR 3YFtSYQIHGmceiGfYFAduqugpqWTuWXsD5l2GeoFxEBNANKlBiZ+aOPZq7Ti3oaB00aU S5ASNoL0UtA2ybHAmVpjD73I7TWQJ0f0HwHlVo4UaMjyMosy+M2jRGJZUqqwCXyrHedV /BLjrZOnXLAuzPDz5T6Vl9IK3jVgK6Ur0R+ZTd2j1vfSI4uZxtiCjDasIYKcrTug4Nj7 Z4saQ2PCcCBMGLA2L9jcS61LTFbJhSSC9AlP5I3nSlfAZ4KJBuwdKRp1dDUTNiqWMHo0 W4Fg== X-Gm-Message-State: AOJu0YzMFVr3tBECA2Jd+ub3i9CZob5/o9VqPbdqoDygrM5ZQdlMuezA 9y+wPT8NyAVQYYrMrb4XGPs3Hd7yu6hx0lBv6GNMLYnKlrXetBsNUEV6IhUGNdMWt9pt9lC28q3 YPJmQNOQaAfr3KsPDn/Kzmxmxp/P3sxJdge5G5STUowjHs9WPcnq8jaSXTBdAs2nnFBjAbyTiF8 ONgdO5Ydu1aXZ+1vjt+j7cD1n2RDEzT0R9PZF0LbO1ZVw= X-Gm-Gg: AeBDieu8oIPm45GzK8bjrGdTHBWGyTlDlxxEpCojv/0XejBBD5zwuQ/GbuH2W6pfNWh XVYWMCZfL5vTmiMUBWGG1AkhAJ51EWZ+/dlMY0jF/0z90blvld+zdCC6AgBVPoznNUmAO8PogUX 656Re4Lag3omNNQJS90D5g/POSFLc+i4ijHA3Bth3hhftHhvjoXFNtJVV0YOFfdCGVJxGSc90NB sUjAZlSN11ztbREocSANRJWHEjhrMeYAACFqJc1WsECGbrerMQiAXmAjyNpb9iO07XnWqGFR+No Y6+jc724ePc/kJM83NEEXRZ623gT6AxxjN7aIN7vGZpW+Ao2vu0uyZ1RO4e4pweeEhVKXDb0oyc PP34sU0E7fOde0sSadztbD0W+Tz/8VO4u9tlxtusrfjw= X-Received: by 2002:a5d:5f46:0:b0:43d:1cec:4766 with SMTP id ffacd0b85a97d-43d642b6b9cmr16170673f8f.23.1776034251263; Sun, 12 Apr 2026 15:50:51 -0700 (PDT) X-Received: by 2002:a5d:5f46:0:b0:43d:1cec:4766 with SMTP id ffacd0b85a97d-43d642b6b9cmr16170628f8f.23.1776034250679; Sun, 12 Apr 2026 15:50:50 -0700 (PDT) Received: from redhat.com (IGLD-80-230-25-21.inter.net.il. [80.230.25.21]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d63e46713sm28673200f8f.21.2026.04.12.15.50.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Apr 2026 15:50:49 -0700 (PDT) Date: Sun, 12 Apr 2026 18:50:46 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Andrew Morton , David Hildenbrand , Vlastimil Babka , Brendan Jackman , Michal Hocko , Suren Baghdasaryan , Jason Wang , Andrea Arcangeli , linux-mm@kvack.org, virtualization@lists.linux.dev, Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Johannes Weiner , Zi Yan Subject: [PATCH RFC 3/9] mm: add __GFP_PREZEROED flag and folio_test_clear_prezeroed() 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 The previous patch skips zeroing in post_alloc_hook() when __GFP_ZERO is used. However, several page allocation paths zero pages via folio_zero_user() or clear_user_highpage() after allocation, not via __GFP_ZERO. Add __GFP_PREZEROED gfp flag that tells post_alloc_hook() to preserve the MAGIC_PAGE_ZEROED sentinel in page->private so the caller can detect pre-zeroed pages and skip its own zeroing. Add folio_test_clear_prezeroed() helper to check and clear the sentinel. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- include/linux/gfp_types.h | 5 +++++ include/linux/mm.h | 16 ++++++++++++++++ mm/page_alloc.c | 8 +++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h index 6c75df30a281..903f87c7fec9 100644 --- a/include/linux/gfp_types.h +++ b/include/linux/gfp_types.h @@ -56,6 +56,7 @@ enum { ___GFP_NOLOCKDEP_BIT, #endif ___GFP_NO_OBJ_EXT_BIT, + ___GFP_PREZEROED_BIT, ___GFP_LAST_BIT }; @@ -97,6 +98,7 @@ enum { #define ___GFP_NOLOCKDEP 0 #endif #define ___GFP_NO_OBJ_EXT BIT(___GFP_NO_OBJ_EXT_BIT) +#define ___GFP_PREZEROED BIT(___GFP_PREZEROED_BIT) /* * Physical address zone modifiers (see linux/mmzone.h - low four bits) @@ -292,6 +294,9 @@ enum { #define __GFP_SKIP_ZERO ((__force gfp_t)___GFP_SKIP_ZERO) #define __GFP_SKIP_KASAN ((__force gfp_t)___GFP_SKIP_KASAN) +/* Caller handles pre-zeroed pages; preserve MAGIC_PAGE_ZEROED in private */ +#define __GFP_PREZEROED ((__force gfp_t)___GFP_PREZEROED) + /* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP) diff --git a/include/linux/mm.h b/include/linux/mm.h index 59fc77c4c90e..caa1de31bbca 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4820,6 +4820,22 @@ static inline bool user_alloc_needs_zeroing(void) */ #define MAGIC_PAGE_ZEROED 0x5A45524FU /* ZERO */ +/** + * folio_test_clear_prezeroed - test and clear the pre-zeroed marker. + * @folio: the folio to test. + * + * Returns true if the folio was pre-zeroed by the host, and clears + * the marker. Callers can skip their own zeroing. + */ +static inline bool folio_test_clear_prezeroed(struct folio *folio) +{ + if (page_private(&folio->page) == MAGIC_PAGE_ZEROED) { + set_page_private(&folio->page, 0); + return true; + } + return false; +} + int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index efb65eee826b..fba8321c45ed 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1867,7 +1867,13 @@ inline void post_alloc_hook(struct page *page, unsigned int order, bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS); int i; - set_page_private(page, 0); + /* + * If the page is pre-zeroed and the caller opted in via + * __GFP_PREZEROED, preserve the marker so the caller can + * skip its own zeroing. Otherwise always clear private. + */ + if (!(prezeroed && (gfp_flags & __GFP_PREZEROED))) + set_page_private(page, 0); /* * If the page is pre-zeroed, skip memory initialization. -- MST