Re: [PATCH 0/3] mm: add per-migratetype counts to buddy allocator and optimize pagetypeinfo access
From: Hongru Zhang
Date: Sun Apr 05 2026 - 05:33:09 EST
> On Fri, Nov 28, 2025 at 11:11 AM Hongru Zhang <zhanghongru06@xxxxxxxxx> wrote:
> >
> > On mobile devices, some user-space memory management components check
> > memory pressure and fragmentation status periodically or via PSI, and
> > take actions such as killing processes or performing memory compaction
> > based on this information.
> >
> > Under high load scenarios, reading /proc/pagetypeinfo causes memory
> > management components or memory allocation/free paths to be blocked
> > for extended periods waiting for the zone lock, leading to the following
> > issues:
> > 1. Long interrupt-disabled spinlocks - occasionally exceeding 10ms on Qcom
> > 8750 platforms, reducing system real-time performance
> > 2. Memory management components being blocked for extended periods,
> > preventing rapid acquisition of memory fragmentation information for
> > critical memory management decisions and actions
> > 3. Increased latency in memory allocation and free paths due to prolonged
> > zone lock contention
>
> Do you have an idea how long each seq_printf call takes?
>
> Assuming seq_printf is costly, printing while holding
> zone->lock may be suboptimal. A further optimization might be:
>
> diff --git a/mm/vmstat.c b/mm/vmstat.c
> index 2370c6fb1fcd..f501ca2840a6 100644
> --- a/mm/vmstat.c
> +++ b/mm/vmstat.c
> @@ -1570,6 +1570,7 @@ static int frag_show(struct seq_file *m, void *arg)
> return 0;
> }
>
> +#if 0
> static void pagetypeinfo_showfree_print(struct seq_file *m,
> pg_data_t *pgdat, struct zone *zone)
> {
> @@ -1611,6 +1612,63 @@ static void pagetypeinfo_showfree_print(struct seq_file *m,
> seq_putc(m, '\n');
> }
> }
> +#endif
> +
> +static void pagetypeinfo_showfree_print(struct seq_file *m,
> + pg_data_t *pgdat,
> + struct zone *zone)
> +{
> + unsigned long freecounts[MIGRATE_TYPES][NR_PAGE_ORDERS] = { 0 };
> + bool overflow[MIGRATE_TYPES][NR_PAGE_ORDERS] = { 0 };
> + int order, mtype;
> +
> + for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
> + for (order = 0; order < NR_PAGE_ORDERS; ++order) {
> + struct free_area *area;
> + struct list_head *curr;
> + unsigned long freecount = 0;
> +
> + area = &zone->free_area[order];
> +
> + list_for_each(curr, &area->free_list[mtype]) {
> + /*
> + * Cap the free_list iteration because it might
> + * be really large and we are under a spinlock
> + * so a long time spent here could trigger a
> + * hard lockup detector. Anyway this is a
> + * debugging tool so knowing there is a handful
> + * of pages of this order should be more than
> + * sufficient.
> + */
> + if (++freecount >= 100000) {
> + overflow[mtype][order] = true;
> + break;
> + }
> + }
> + freecounts[mtype][order] = freecount;
> + spin_unlock_irq(&zone->lock);
> + cond_resched();
> + spin_lock_irq(&zone->lock);
> + }
> + }
> +
> + /* printing completely outside the lock */
> + spin_unlock_irq(&zone->lock);
> + for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
> + seq_printf(m, "Node %4d, zone %8s, type %12s ",
> + pgdat->node_id,
> + zone->name,
> + migratetype_names[mtype]);
> +
> + for (order = 0; order < NR_PAGE_ORDERS; ++order) {
> + seq_printf(m, "%s%6lu ",
> + overflow[mtype][order] ? ">" : "",
> + freecounts[mtype][order]);
> + }
> + seq_putc(m, '\n');
> + }
> + spin_lock_irq(&zone->lock);
> +}
>
> /* Print out the free pages at each order for each migratetype */
> static void pagetypeinfo_showfree(struct seq_file *m, void *arg)
Yes. I included the data in [1]. This seq_printf loop costs about 5 us on my
machine. I'll look into optimizing this in the next revision.
[1] https://lore.kernel.org/linux-mm/20251201122912.348142-1-zhanghongru@xxxxxxxxxx/