[RFC PATCH v2 17/21] riscv: mm: Apply Svnapot for base page mapping if possible

Xu Lu
2024-12-05T10:45:09Z

All hardware pages in the same software page point to the same contiguous
memory region (the region size is equal to the software page size) and
has same prots. Thus this commit uses Svnapot extension to optimize the
mapping to software page to reduce tlb pressure.

Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
---
arch/riscv/include/asm/pgtable.h | 16 +++++++++++++++-
arch/riscv/mm/pgtable.c | 6 ++++++
2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 5b2ca92ad833..9f347e5eefeb 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -483,13 +483,27 @@ static inline unsigned long __pte_mknapot(unsigned long pteval,
return pteval;
}

+static inline unsigned long __pte_denapot(unsigned long pteval)
+{
+ unsigned long prot_mask = ~(_PAGE_HW_PFN_MASK | _PAGE_NAPOT);
+ unsigned long res;
+
+ if (!__pte_napot(pteval))
+ return pteval;
+ res = __page_val_to_hwpfn(pteval);
+ res = res & (res - 1UL);
+ pteval = (res << _PAGE_HWPFN_SHIFT) | (pteval & prot_mask);
+
+ return pteval;
+}
+
#ifdef CONFIG_RISCV_USE_SW_PAGE
static inline pte_t pte_mknapot(pte_t pte, unsigned int order)
{
unsigned long pteval = pte_val(pte);
unsigned int i;

- pteval = __pte_mknapot(pteval, order);
+ pteval = __pte_denapot(pteval);
for (i = 0; i < HW_PAGES_PER_PAGE; i++)
pte.ptes[i] = pteval;

diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c
index 150aea8e2d7a..0bcaffe798d5 100644
--- a/arch/riscv/mm/pgtable.c
+++ b/arch/riscv/mm/pgtable.c
@@ -11,6 +11,12 @@ pte_t __pte(unsigned long pteval)
{
pte_t pte;
unsigned int i;
+ unsigned int order;
+
+ if (has_svnapot() && __pte_present(pteval) && !__pte_napot(pteval))
+ for_each_napot_order(order)
+ if (napot_cont_shift(order) == PAGE_SHIFT)
+ pteval = __pte_mknapot(pteval, order);

for (i = 0; i < HW_PAGES_PER_PAGE; i++) {
pte.ptes[i] = pteval;