On Thu, Nov 26, 2026 at 3:23 AM Mike Rapoport (Microsoft)
<[email protected]> wrote:
>
> The core keeps a counter of the direct map entries at every page table
> level. A split moves an entry to the level below and counts a
> direct_map_level2_splits or direct_map_level3_splits event in /proc/vmstat.
> The counters start from what the architecture mapped.
>
> arm64 never counts its linear map, so the core finds no large pages to take
> a split from, and the split events stay at zero.
>
> Count the linear map when it is created at boot and on memory hotplug, and
> when memory is removed. map_mem() maps the kernel alias and the KFENCE pool
> before it maps all of memory over them, so only the entries that were empty
> count. The split of the linear map at boot, when not all CPUs support
> BBML3, bypasses the core, so count its splits as well.
>
> Report the counters in /proc/meminfo.

Hi Mike,

I had been working on this a couple of months ago. Please refer to
this patch: 
https://lore.kernel.org/linux-arm-kernel/[email protected]/

There are three tricky things for ARM64 if I remember correctly (my
memory is a little vague):
1. ARM64 supports CONT_PTE/CONT_PMD, we do prefer show the counters
for them too.
2. Memory hotunplug. The current memory hotunplug code doesn't handle
CONT_PTE/CONT_PMD because it is unnecessary if we don't need to count
the direct mapping entries.
3. Locking. IIRC, x86 protects the counters update with pgd_lock.
IIUC, ARM64 should need such a lock too.

Anyway I don't think I'm going to have time to work on this. So please
feel free to take the patch.

Thanks,
Yang

>
> Signed-off-by: Mike Rapoport (Microsoft) <[email protected]>
> ---
>  arch/arm64/mm/mmu.c      | 28 ++++++++++++++++++++++++++++
>  arch/arm64/mm/pageattr.c | 13 +++++++++++++
>  2 files changed, 41 insertions(+)
>
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index 40c5ac95f574..c415b81fb6f3 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -160,9 +160,19 @@ static void init_clear_pgtable(void *table)
>         dsb(ishst);
>  }
>
> +static void linear_map_count(unsigned long addr, enum pgtable_level level,
> +                            long nr)
> +{
> +       if (__is_lm_address(addr))
> +               direct_map_count_update(level, nr);
> +}
> +
>  static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
>                      phys_addr_t phys, pgprot_t prot)
>  {
> +       unsigned long start = addr;
> +       long nr = 0;
> +
>         do {
>                 pte_t old_pte = __ptep_get(ptep);
>
> @@ -179,8 +189,12 @@ static void init_pte(pte_t *ptep, unsigned long addr, 
> unsigned long end,
>                 BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
>                                               pte_val(__ptep_get(ptep))));
>
> +               if (pte_none(old_pte))
> +                       nr++;
>                 phys += PAGE_SIZE;
>         } while (ptep++, addr += PAGE_SIZE, addr != end);
> +
> +       linear_map_count(start, PGTABLE_LEVEL_PTE, nr);
>  }
>
>  static bool pte_range_has_valid_noncont(pte_t *ptep)
> @@ -274,6 +288,9 @@ static int init_pmd(pmd_t *pmdp, unsigned long addr, 
> unsigned long end,
>                          */
>                         BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
>                                                       
> READ_ONCE(pmd_val(*pmdp))));
> +
> +                       if (pmd_none(old_pmd))
> +                               linear_map_count(addr, PGTABLE_LEVEL_PMD, 1);
>                 } else {
>                         int ret;
>
> @@ -410,6 +427,9 @@ static int alloc_init_pud(p4d_t *p4dp, unsigned long 
> addr, unsigned long end,
>                          */
>                         BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
>                                                       
> READ_ONCE(pud_val(*pudp))));
> +
> +                       if (pud_none(old_pud))
> +                               linear_map_count(addr, PGTABLE_LEVEL_PUD, 1);
>                 } else {
>                         ret = alloc_init_cont_pmd(pudp, addr, next, phys, 
> prot,
>                                                   pgtable_alloc, flags);
> @@ -750,6 +770,7 @@ static int split_to_ptes_pud_entry(pud_t *pudp, unsigned 
> long addr,
>                 return -ENOMEM;
>
>         split_pud(pudp, pud, pmdp, false);
> +       direct_map_count_split(PGTABLE_LEVEL_PUD);
>
>         return 0;
>  }
> @@ -771,6 +792,7 @@ static int split_to_ptes_pmd_entry(pmd_t *pmdp, unsigned 
> long addr,
>         if (pmd_cont(pmd))
>                 split_contpmd(pmdp);
>         split_pmd(pmdp, pmd, ptep, false);
> +       direct_map_count_split(PGTABLE_LEVEL_PMD);
>
>         /*
>          * We have split the pmd directly to ptes so there is no need to
> @@ -1424,6 +1446,8 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, 
> unsigned long addr,
>                         flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
>                         free_hotplug_page_range(pte_page(pte),
>                                                 PAGE_SIZE, altmap);
> +               } else {
> +                       direct_map_count_update(PGTABLE_LEVEL_PTE, -1);
>                 }
>                 /* unmap_hotplug_range() flushes TLB for !free_mapped */
>         } while (addr += PAGE_SIZE, addr < end);
> @@ -1458,6 +1482,8 @@ static void unmap_hotplug_pmd_range(pud_t *pudp, 
> unsigned long addr,
>                                 flush_tlb_kernel_range(addr, addr + 
> PAGE_SIZE);
>                                 free_hotplug_page_range(pmd_page(pmd),
>                                                         PMD_SIZE, altmap);
> +                       } else {
> +                               direct_map_count_update(PGTABLE_LEVEL_PMD, 
> -1);
>                         }
>                         /* unmap_hotplug_range() flushes TLB for !free_mapped 
> */
>                         continue;
> @@ -1489,6 +1515,8 @@ static void unmap_hotplug_pud_range(p4d_t *p4dp, 
> unsigned long addr,
>                                 flush_tlb_kernel_range(addr, addr + 
> PAGE_SIZE);
>                                 free_hotplug_page_range(pud_page(pud),
>                                                         PUD_SIZE, altmap);
> +                       } else {
> +                               direct_map_count_update(PGTABLE_LEVEL_PUD, 
> -1);
>                         }
>                         /* unmap_hotplug_range() flushes TLB for !free_mapped 
> */
>                         continue;
> diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
> index a2f5266b77b2..5e6782ef4b6c 100644
> --- a/arch/arm64/mm/pageattr.c
> +++ b/arch/arm64/mm/pageattr.c
> @@ -9,6 +9,7 @@
>  #include <linux/sched.h>
>  #include <linux/vmalloc.h>
>  #include <linux/pageattr.h>
> +#include <linux/proc_fs.h>
>
>  #include <asm/cacheflush.h>
>  #include <asm/pgtable-prot.h>
> @@ -16,6 +17,18 @@
>  #include <asm/tlbflush.h>
>  #include <asm/kfence.h>
>
> +#ifdef CONFIG_PROC_FS
> +void arch_report_meminfo(struct seq_file *m)
> +{
> +       unsigned long levels = BIT(PGTABLE_LEVEL_PTE) | 
> BIT(PGTABLE_LEVEL_PMD);
> +
> +       if (pud_sect_supported())
> +               levels |= BIT(PGTABLE_LEVEL_PUD);
> +
> +       cpa_report_meminfo(m, levels);
> +}
> +#endif
> +
>  void arch_cpa_decode_leaf(struct cpa_leaf *leaf)
>  {
>         enum pgtable_level level = leaf->level;
>
> --
> 2.53.0
>
>

Reply via email to