From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 1600632B126 for ; Tue, 9 Jun 2026 14:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781015942; cv=none; b=o5WNkjk3+4ysBKsVNZ9rgXdFmkkbPavgixjkVyrzOSca2oHyYH9Fm0XuMZXYEG56TKUyw2rgQWCyrjkITo2BQnIaWdvwdGIwUUVbhIrd19HHJWdKqXIpCGzrnLZJwmj/nw0WKvySkHitVfo35PFogITNjj5+WsGs9X9KDrsRIeM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781015942; c=relaxed/simple; bh=FQwYe8pNIO676qrACCxHOIRwcQYpQb+HgQYeAAMjzXo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=meXzXOIN9jDbXvt+XFAV6MpzgLSUYUJY5Byj9OgAe8qe0E8xdFG0Io1+kic6eLBbsHAsOhj6IdqItz9YzeOMdST8GMVoYB7tvEE94hotCd1blzBUE8liEjVy64uSs3rwC3MhYD+SYm8LIAX3gAHk7k9CTGpBq43Zo+GsaWFPTqI= 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=luupJ8Gj; arc=none smtp.client-ip=209.85.215.172 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="luupJ8Gj" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-c85a2b8c95cso180906a12.0 for ; Tue, 09 Jun 2026 07:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781015940; x=1781620740; 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; bh=MyS7mKR1Pj5rWSjXZPjC1j5mlisxE9H8ozK2CuoOEmk=; b=luupJ8Gj0VMMdWZxYQTq7t/rnSsAvsIQVhtoj39QRm8WXNDfG/i+FHubxm6c5OKu8z rrJQkbHMELujpLl9ZOzoZ76WI6hJgoPrPJD2H24V3dOZ+Z3x5ElC+R2mO+FlJKddc8Z9 jw8RyTv3GokxZaEWqANri8wTE6W2By4YTLh1MBfTySbpCMAgqjTtxmgYPxVthRMzVy5n 3V40+DfyVfs4uUYLfaKt9QoR2lHhqK27vne7w0ksB1oaZkpUvzgLV5bOwcQKuMm91yFH uQ7BdjMM7Zv/kFQH5ldcBHarztFBxxt0kx3fT5ZPZRtGNUjWj1jOzTWmyw8X59rHajTM HkxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781015940; x=1781620740; 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; bh=MyS7mKR1Pj5rWSjXZPjC1j5mlisxE9H8ozK2CuoOEmk=; b=AuJ5HEypolmxBSLBaYj+5g/hbWGdTBDnGetEwwD+4HO6PbKdnID7+6uLzXaP7sP4AG JriPWjyaE7+Bp16Catx5DLb9m8EpiYwT6mMSNeVYq4G+rqDoAhR2YAUtvO8RGYKOcskX 0DaPsOgyeUbmr7rc+ca/jN2pByaLe8wbdXUQHFoIRpIs5cBamPzFYRfajhHQTzeFCqf5 94oCth13vxRIxxarNnhO8YojoY/knfhSoYkcJCXsrjOQ1YnD59Qff5DNVWOZWUGIIYvh u9eQpG7b2w0cYhDYlpljvb+OI7eHPDzz4uvHRWjjJ08oa2wSeL5RvgrMIGGWW93XFtc4 uAIQ== X-Gm-Message-State: AOJu0YzfBLxhDaBjRshJ3TD31OtECr+Zdac5lYd5r2TlHkuKQaCIWtuW 4zhpoJn4AYcHIsHLayBIYn7qNDjLWUCdyy7jI6MkHy0Yhggn6BRubm2L X-Gm-Gg: Acq92OFVS2YIqhqqeOGAoCYLDQUlddAoFV30ANWpiobRe0s2CGhRgkZhi+6qHVfHrsX 7tVm5DrxHHtohqUB1703hOJe3kT5t2v6UmLktv3HLVtciCEBAbe6vdkKuGIVj+y55oOxvYZJ3SP piq3LfGVOK4DA+Lfeay6A7rKDf5QKNSGKRHarCtPQ+3MHuqLUBUQCDZm4Kt+8rVu4s3en1P1S8U Jkunt2hv0m9hUPbURADzXazXDC1S4koZ8O4EoJoSqYYNo1GFi/VTi9xKHgXQ3l2RXyNKIqrRDZC fyNYd+C75HEmfetpsPa2133j6nM3EK0vFy+9Zn+FYAXeVr7zlLs0BeRNAId5c6/Jee+gQSGKkqk QFC0ss5jFzN7qMRGmAXtaJM2TFJFs//4Eqkr0WmjhihbvjWHvmdecJO76WalFtdgRbAhgbm64H1 wJLOdATTGAMjEM3f33+L9YcePWRekpjZ/qTGf6xuxMRGvOIYsWsMWW3X3wGkmWZlIRQwK8gNGMq lsYbCKyAwZy00UngWCfSUH1 X-Received: by 2002:a05:6a21:4784:b0:398:6e38:95e8 with SMTP id adf61e73a8af0-3b53b6e9085mr2088352637.2.1781015939654; Tue, 09 Jun 2026 07:38:59 -0700 (PDT) Received: from o6.lan ([188.253.112.242]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c85df0b315esm17939677a12.26.2026.06.09.07.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2026 07:38:59 -0700 (PDT) From: Xueyuan Chen To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, x86@kernel.org, catalin.marinas@arm.com, will@kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, david@kernel.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, yang@os.amperecomputing.com, jannh@google.com, Xueyuan Chen , Dave Hansen Subject: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only Date: Tue, 9 Jun 2026 22:37:59 +0800 Message-ID: <20260609143801.7917-2-xueyuan.chen21@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260609143801.7917-1-xueyuan.chen21@gmail.com> References: <20260609143801.7917-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 huge zero folio is shared globally, and its contents should never change after initialization. As Jann Horn pointed out[1], the kernel has had bugs, including security bugs, where read-only pages were later written to. If the persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents. Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing. This was inspired by Jann Horn's read-only zero page work[1] and follow-up discussion[2] 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/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/ Suggested-by: Dave Hansen Suggested-by: David Hildenbrand Co-developed-by: Lance Yang Signed-off-by: Lance Yang Signed-off-by: Xueyuan Chen --- include/linux/mm.h | 2 ++ mm/huge_memory.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0f2612a70fb1..02d33cf45b01 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2936,6 +2936,8 @@ static inline int arch_make_folio_accessible(struct folio *folio) } #endif +bool arch_make_pages_readonly(struct page *page, int nr_pages); + /* * Some inline functions in vmstat.h depend on page_zone() */ diff --git a/mm/huge_memory.c b/mm/huge_memory.c index b81d1ecb434b..2e9d01dc1197 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, return 0; } +bool __weak arch_make_pages_readonly(struct page *page, int nr_pages) +{ + return false; +} + static struct shrinker *huge_zero_folio_shrinker; #ifdef CONFIG_SYSFS @@ -982,8 +987,14 @@ 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; + } + + arch_make_pages_readonly(folio_page(huge_zero_folio, 0), + HPAGE_PMD_NR); + return 0; } -- 2.47.3