From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (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 1F26C3E6387 for ; Wed, 3 Jun 2026 10:47:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780483626; cv=none; b=Uh4KgVMVTUbhm6UcXJF3eeOYXjjldtosaUucKsgXm5cF1Z4sTdELxmtSRdCFwQWMqDPJOxRlRgJYQRtiZ29P1lsYcze/EoDUGKSuTS3tGekh9Xhbafio/6i+woxZ0qP5YMwrl3kxSjOQ0aWh300cMBh9B/+IBaIvRRUXNeC0RFE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780483626; c=relaxed/simple; bh=240t7hPkntlxBNqY/y60TPWf6svHw6mrl6KpWkVVIUQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nHBwjdgRUJuakLZ3MqvYDNVfsofLmpj8i/FCVWkKVUV/DEn7b7JyHMXJpDx7SuFKscN2f0f1Lal++Mw0hQ4DCQlxtFE5BXEKKp0wXaJqLiM8Lz+HZyNKsg3I8m4qcJgajRK495tzJwrwAZs/epXlkebVlVWNFolCcjQ5SBkKUgU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rOGK+hjB; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="rOGK+hjB" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1780483623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uDivKNl50/oglC97Ig2qp3GHfbl7ogmTnLA9RISadv8=; b=rOGK+hjBTX7M1kHbQ2XvvKqnp1jett+Tj8n+9qKo5Zv3H4e7/UqA2vmrI/T5yoFdeZuONF s03n+ItMVdgigxDknLtYsctpJUmvx/njkgApQzgszPqIMOlLBj8XDhudjTBEYQHD+s35v4 Luvt6hhSwL3O36lOBXhUgAHFcCyOKYY= From: Lance Yang To: akpm@linux-foundation.org Cc: rppt@kernel.org, david@kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, luto@kernel.org, peterz@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, xueyuan.chen21@gmail.com, ioworker0@gmail.com, Lance Yang Subject: [RFC PATCH 2/2] x86/mm: restore large page mappings for secretmem Date: Wed, 3 Jun 2026 18:46:24 +0800 Message-ID: <20260603104624.36390-3-lance.yang@linux.dev> In-Reply-To: <20260603104624.36390-1-lance.yang@linux.dev> References: <20260603104624.36390-1-lance.yang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Lance Yang secretmem can ask the architecture to restore large page mappings in the direct map after a page has been restored. On x86, set_direct_map_invalid_noflush() clears _PAGE_PRESENT, _PAGE_RW and _PAGE_DIRTY through CPA. CPA also clears _PAGE_GLOBAL after the PTE becomes non-present, because that bit is used for PROTNONE. set_direct_map_default_noflush() only restores _PAGE_PRESENT and _PAGE_RW, so the restored PTE can still differ from the rest of the direct map. Put back the missing PAGE_KERNEL bits before trying to collapse the range. Use PAGE_KERNEL so the restored PTE keeps _PAGE_GLOBAL when PAGE_KERNEL has it, and does not get it when PTI clears _PAGE_GLOBAL from __default_kernel_pte_mask. Tested-by: Xueyuan Chen Signed-off-by: Lance Yang --- arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index 4362c26aa992..f9e94bcd83df 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -89,6 +89,8 @@ int set_pages_rw(struct page *page, int numpages); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); +void arch_try_collapse_direct_map(struct page *page); +#define arch_try_collapse_direct_map arch_try_collapse_direct_map bool kernel_page_present(struct page *page); extern int kernel_set_to_readonly; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index d023a40a1e03..b118619fa83c 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2654,6 +2654,33 @@ int set_direct_map_default_noflush(struct page *page) return __set_pages_p(page, 1); } +void arch_try_collapse_direct_map(struct page *page) +{ + pgprotval_t mask = pgprot_val(PAGE_KERNEL) & (_PAGE_DIRTY | _PAGE_GLOBAL); + unsigned long addr; + struct cpa_data cpa = { .vaddr = &addr, + .pgd = NULL, + .numpages = 1, + .mask_set = __pgprot(mask), + .mask_clr = __pgprot(0), + .flags = CPA_NO_CHECK_ALIAS }; + + if (PageHighMem(page) || debug_pagealloc_enabled()) + return; + + addr = (unsigned long)page_address(page); + + /* + * set_direct_map_default_noflush() only restores _PAGE_PRESENT and + * _PAGE_RW. Put back the other PAGE_KERNEL bits needed for a restored + * direct-map PTE to match the rest of the range. + */ + if (__change_page_attr_set_clr(&cpa, 1)) + return; + + cpa_collapse_large_pages(&cpa); +} + int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) { if (valid) -- 2.49.0