Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8192837BE8E for ; Sat, 18 Jul 2026 09:57:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784368625; cv=none; b=P1z5x8Daodh3n4y2HGGS5keLyTxvTNzBjalOnkjwR7IMJm6ejr1mvr+EgOJprdt9WvO+kSU5UYtcNXzhgaqgypu1t79NgaCRtiVy5v0ZvxHaASXMUtYxZLuyrwFeOX4Br5IQK4HN12KSTcRJW5RNzWq1bnVfa9iFGRMhz4iL8mk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784368625; c=relaxed/simple; bh=BkhvyAl41lLtYiFI8zJ9qADiaAA2a0URAAzIw0G5FzI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s3MH30gheB/NQZ49MoHdtx2BMe3XPtoGEClARZfam7q5/sjcTm7Iabxvyf09KxFR+Luc0ZGr4yTz+xCVrorWtDYHZ5G7bV/q5OiCGryJi9/x58kIgwu7Orptp6hdx/l46CtHBfK53x9+vZe53gBQThifCCfCVQYYiLBLS3s75L4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LbQjlLHa; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LbQjlLHa" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-c9e3c0b6480so453664a12.0 for ; Sat, 18 Jul 2026 02:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784368624; x=1784973424; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NM59zTITSAnSgD8fALdagpl6xYMLVePtK5I+sk28JTE=; b=LbQjlLHa39blSNw0WgGm9rt3xMQXAN3qbxZsiUkSWXtMEheIOYEPFZFyDgMNo0QV9r JL7U+V2vJbJbuyGlVBxgzJMz0Op5SBOY+83lGJYLUz9MICNiZzQtzr6fzoNKE8iisqw5 /3+qedqqVV0q1Ovfmu+vccjuRCrXtSKuz7geq9J+hSfTUVxwqDzKo6EcTcNJjGQL4ouZ Zg0aiGH4jgyzVIVu88AOKhe4Cdlb8kEL2iZIyX2UR5JgbVghWfW3Rq8PUuQcbgmYj+yI ldDIFOasXH7IxRGi//izIZ2pLPklQPjZ3/PAYKN0o7vr7Jn8YpBwNNDLpc64obLNwxnW /V/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784368624; x=1784973424; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=NM59zTITSAnSgD8fALdagpl6xYMLVePtK5I+sk28JTE=; b=RMurIbeUz6M2kFDRElKGjRtvvHOKj3XEmDBkBv0t4KMQ1RLb8Z3vbevmtUmPYOEvv4 N1r/P4/kmyb4m7DzI+jLqRQV3AkavCWhfAAYSypCFZZ8tIkJAfdBoBoNtiS9t/i5lm5v jG9n65bRihwMF4te/LOAbrn5Q/INpV/WF2VVblLhOZrFV1JuQ1x77MkZUU1P8UUcMDbH FZ1sEdxZhFEoC/Y41dUJOmrRTlg9nNd7zfSep+Eo0Mqz92kTTUP7hKhgbq07x69a2div jFiFS2Bm17Imxw9RKmApNRtlCT2lWozf42UKVutK3wgiyFStTijNfUyk41JBmrWaLhfp WdJg== X-Gm-Message-State: AOJu0YywQdRCmjo9kJk7ulBTbwOHd3Tysqx+NzLQurBX2jFGH1rc3LN3 3tpYJ+JZUHdUf5HPLD2ydsP2PkgMbsgBTP2qYB9ESEP9PbABDKwbbSe1 X-Gm-Gg: AfdE7ckpHFJXGwPYkWh5larVBwockyGCWgmS0jiouDpcb3KihSrGd4uPxxe6L9UdSWz y4K2AX+4TGe6C2Trq2hf0RehRnBBkSiluOG9mL/VnA02nxxH5JsieloYKjm4PW7wj9zoL0uvJ9i OJ5Lopr5WjLfynZEz3yj7jBCFBG5rE9uOmqgT1w2PhqVNqA0kSRcH0gnREbdrSWE1FqOXwm7Qu+ JYx5cIyDkdyX7Vc8f4f50wJ18IY/q9bACbHSGxxG67veA+y8E3hvgdQoI6/fwo3bzfo1N+1fHE4 dpItIS9i/6W2UrED1zSJrX6GHfFVbDjwzAUS9XuRvA7b/W3NVN68mrYM/ezyV6oF4uZvPkJ+pDB 3ghHTFy1AxnqRnSSUMLhGoXNGdEpMIBhBMFiNrE8qV5EnVCURZwJIb3q6h7op90E4pf+oxfjzOP p90RPvHrt4UCbGw+JGWaTs7Akq7rbKHOhgKG+XcyuOofKz X-Received: by 2002:a05:6a00:7287:b0:847:88aa:4f4f with SMTP id d2e1a72fcca58-84c29510c82mr3438599b3a.7.1784368623754; Sat, 18 Jul 2026 02:57:03 -0700 (PDT) Received: from debian.lan ([240e:391:eb4:a240::1]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84c2af317c8sm2417248b3a.30.2026.07.18.02.56.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jul 2026 02:57:03 -0700 (PDT) From: Xueyuan Chen To: linux-mm@kvack.org, Andrew Morton , David Hildenbrand , Lorenzo Stoakes Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Lance Yang , Usama Arif , Jann Horn , Yang Shi , Mike Rapoport , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Xueyuan Chen Subject: [RFC PATCH v4 1/3] mm: make persistent huge zero folio read-only Date: Sat, 18 Jul 2026 17:56:45 +0800 Message-ID: <20260718095647.182592-2-xueyuan.chen21@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260718095647.182592-1-xueyuan.chen21@gmail.com> References: <20260718095647.182592-1-xueyuan.chen21@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The persistent huge zero folio is shared globally and should stay zero after initialization. As Jann Horn pointed out[1], kernel bugs have ended up writing to pages that were meant to be read-only, including in security-sensitive cases. Making the persistent huge zero folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption. Add set_direct_map_ro_noflush() so mm code can make a direct-map range read-only. Use an address-based signature to match ongoing direct-map helper work[2], where existing page-based helpers may move the same way. The helper is direct-map specific and leaves TLB invalidation to its caller. Architectures without direct-map permission support keep existing behavior through the generic stub. The folio is allocated and zeroed through the writable direct map before thp_shrinker_init() changes its permissions. thp_shrinker_init() is called from hugepage_init(), which is registered as a subsys_initcall and runs after SMP initialization. Stale writable kernel TLB entries may therefore exist. Flush the direct-map range immediately after the page-table update so they cannot bypass the read-only mapping. Treat the direct-map permission change as best-effort. Architectures that do not implement the helper keep the existing behavior via the generic stub. Inspired by Jann Horn's read-only zero page work[1] and follow-up discussion[3] with Yang Shi. [1] https://lore.kernel.org/linux-mm/20260508-ro-zeropage-v1-1-9808abc20b49@google.com/ [2] https://lore.kernel.org/linux-mm/0e5b23a6-4895-454a-9dfa-6dc21adc2991@kernel.org/ [3] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/ Suggested-by: David Hildenbrand Suggested-by: Usama Arif Co-developed-by: Lance Yang Signed-off-by: Lance Yang Signed-off-by: Xueyuan Chen --- include/linux/set_memory.h | 29 +++++++++++++++++++++++++++++ mm/huge_memory.c | 16 +++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h index 3030d9245f5a..e83ced6a3827 100644 --- a/include/linux/set_memory.h +++ b/include/linux/set_memory.h @@ -40,6 +40,24 @@ static inline int set_direct_map_valid_noflush(struct page *page, return 0; } +/** + * set_direct_map_ro_noflush - make a direct-map range read-only + * @addr: start address in the direct map + * @nr_pages: number of pages starting at @addr + * + * Make the direct-map range starting at @addr read-only without invalidating + * TLBs. Callers must either ensure that no stale writable translations can + * be used, or treat the permission change as a best-effort hardening step. + * + * Return: 0 on success or when direct-map permission changes are unsupported, + * or a negative errno on failure. + */ +static inline int set_direct_map_ro_noflush(const void *addr, + unsigned long nr_pages) +{ + return 0; +} + static inline bool kernel_page_present(struct page *page) { return true; @@ -56,6 +74,17 @@ static inline bool can_set_direct_map(void) } #define can_set_direct_map can_set_direct_map #endif + +#ifndef set_direct_map_ro_noflush +/* See the comment above the generic fallback for the _noflush contract. */ +static inline int set_direct_map_ro_noflush(const void *addr, + unsigned long nr_pages) +{ + return 0; +} + +#define set_direct_map_ro_noflush set_direct_map_ro_noflush +#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ #ifdef CONFIG_X86_64 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 970e077019b7..4425ae5560cb 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -40,8 +40,10 @@ #include #include #include +#include #include +#include #include "internal.h" #include "swap.h" @@ -932,6 +934,8 @@ static int __init thp_shrinker_init(void) shrinker_register(deferred_split_shrinker); if (IS_ENABLED(CONFIG_PERSISTENT_HUGE_ZERO_FOLIO)) { + unsigned long addr; + /* * Bump the reference of the huge_zero_folio and do not * initialize the shrinker. @@ -940,8 +944,18 @@ static int __init thp_shrinker_init(void) * that get_huge_zero_folio() will most likely not fail as * thp_shrinker_init() is invoked early on during boot. */ - if (!get_huge_zero_folio()) + if (!get_huge_zero_folio()) { pr_warn("Allocating persistent huge zero folio failed\n"); + return 0; + } + + addr = (unsigned long)folio_address(huge_zero_folio); + /* + * The folio was zeroed through the writable direct map. Flush + * after the page-table update to invalidate stale translations. + */ + set_direct_map_ro_noflush((void *)addr, HPAGE_PMD_NR); + flush_tlb_kernel_range(addr, addr + HPAGE_PMD_SIZE); return 0; } -- 2.47.3