From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 946F6393DF9 for ; Wed, 3 Jun 2026 10:46:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780483617; cv=none; b=SLxHavzVSrhjmef/a7eB0m6hngF1BxUU9uRZmnAg/oJJpRkcrnnjLuVVwKQAwrinYQn6D0UU1EIPB+t/DCXjL9r2InuzfNQtKEfPhQCbnWEfDEy7H88Al0U1c0wLkQ81BojYquP8UBLBsonOt9aaFSZgy4yKNHSUc37VqkAPx4A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780483617; c=relaxed/simple; bh=arBLRd2RXbGU37kIlHpH8zRLqnB5OIV7b9T0SvTcAvI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jW1HC/9VQEah2W87e3ABr3paKQ4/QTQqB4CAHu/S7ep7Wsp6irE7PR77qro7YKBZmpfbt37uaDJhQngFSZIgYu3D/JunxGezbdTVm+dG8dUwX7SmdQg9Q2NxKLe4974bJFM+pcK5n0mQNp1Fvuyu3O3uX5JYGqdMchW+GSBrQzw= 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=SaLIqHE8; arc=none smtp.client-ip=95.215.58.180 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="SaLIqHE8" 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=1780483613; 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=9Db5QI22S+PmAf36h4l+5enVOhe4SLTSWaLegqKX8Kk=; b=SaLIqHE8MVrLhRMMW0UhnyHGHtygmT+8OPgIfrPnznSMLhbnKq1Dva1IY/qFMx5UbuTa7B SRRrqJmht8UHrPAlzA+ElUGKuRkKgGHDfJc3y8Frp/EvyIGBjpEgN5lZz3e1bVLojR5plw Awtl1jk5pJEwB6evELNS5wwPVEkxrBw= 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 1/2] mm/secretmem: try to restore large page mappings in direct map Date: Wed, 3 Jun 2026 18:46:23 +0800 Message-ID: <20260603104624.36390-2-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 removes the pages backing secretmem mappings from the direct map. Removing one base page from the direct map can split the covering large mapping down to PTE mappings. Repeated splits can leave more of the direct map mapped with PTEs, meaning more TLB entries for the same range and potentially more TLB pressure. So let's try to restore large page mappings whenever secretmem restores a folio to the direct map. Tested-by: Xueyuan Chen Signed-off-by: Lance Yang --- include/linux/set_memory.h | 6 ++++++ mm/secretmem.c | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h index 3030d9245f5a..ad2fa414a22d 100644 --- a/include/linux/set_memory.h +++ b/include/linux/set_memory.h @@ -58,6 +58,12 @@ static inline bool can_set_direct_map(void) #endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef arch_try_collapse_direct_map +static inline void arch_try_collapse_direct_map(struct page *page) +{ +} +#endif + #ifdef CONFIG_X86_64 int set_mce_nospec(unsigned long pfn); int clear_mce_nospec(unsigned long pfn); diff --git a/mm/secretmem.c b/mm/secretmem.c index 5f57ac4720d3..82634b99aa2c 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -47,6 +47,14 @@ bool secretmem_active(void) return !!atomic_read(&secretmem_users); } +static void secretmem_restore_direct_map(struct folio *folio) +{ + struct page *page = folio_page(folio, 0); + + if (!set_direct_map_default_noflush(page)) + arch_try_collapse_direct_map(page); +} + static vm_fault_t secretmem_fault(struct vm_fault *vmf) { struct address_space *mapping = vmf->vma->vm_file->f_mapping; @@ -87,7 +95,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) * already happened when we marked the page invalid * which guarantees that this call won't fail */ - set_direct_map_default_noflush(folio_page(folio, 0)); + secretmem_restore_direct_map(folio); folio_put(folio); if (err == -EEXIST) goto retry; @@ -151,7 +159,7 @@ static int secretmem_migrate_folio(struct address_space *mapping, static void secretmem_free_folio(struct folio *folio) { - set_direct_map_default_noflush(folio_page(folio, 0)); + secretmem_restore_direct_map(folio); folio_zero_segment(folio, 0, folio_size(folio)); } -- 2.49.0