mm/page_alloc: add tracepoints for zone->lock acquisitions
Subsystems (5)
PATCH 1/2 mm/page_alloc: add tracepoints for zone->lock acquisitions
From: Jesper Dangaard Brouer <[email protected]>
Add tracepoints to the page allocator fast paths that acquire zone->lock, allowing diagnosis of lock contention in production.
Three tracepoints are introduced:
kmem:mm_zone_lock_contended - fires when trylock fails (lock is held)
kmem:mm_zone_locked - fires on every acquisition
kmem:mm_zone_lock_unlock - fires on every release
Each event records the NUMA node, zone name, batch count, and caller. The mm_zone_locked event additionally records wait_ns: the time spent spinning when contended, measured via local_clock() with IRQs disabled to ensure accurate same-CPU timestamps.
The lock/unlock paths are wrapped in __zone_lock()/__zone_unlock() helpers that use trylock-first to separate the contended and uncontended cases. Only the fast paths (free_pcppages_bulk, rmqueue_bulk, free_one_page) are covered. Other zone->lock holders such as compaction, page isolation, and memory hotplug are not instrumented.
For minimum overhead in production, enable only mm_zone_lock_contended which fires only on actual contention. Enable mm_zone_locked for wait-time analysis, and add mm_zone_lock_unlock for hold-time measurement.
--- include/trace/events/kmem.h | 101 ++++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 50 +++++++++++++++--- 2 files changed, 145 insertions(+), 6 deletions(-)
include/trace/events/kmem.h +101 -0
@@ -458,6 +458,107 @@ TRACE_EVENT(rss_stat,__print_symbolic (__entry->member,TRACE_MM_PAGES ), __entry->size) ); + +/* +* Tracepoints for zone->lock on the page allocator fast paths only. +* Other code paths that acquire zone->lock (compaction, isolation, +* memory hotplug, vmstat, etc.) are not covered here. +* +* Three events: +* mm_zone_lock_contended - trylock failed, about to spin +* mm_zone_locked - lock acquired, includes wait_ns when +* contended (zero otherwise) +* mm_zone_lock_unlock - lock released +* +* For production use with minimum overhead, enable only +* mm_zone_lock_contended -- it fires only when trylock detects the +* lock is already held. +* +* For wait-time analysis, enable mm_zone_locked -- its wait_ns +* field gives the spin duration directly. Adding unlock allows +* hold-time measurement, at the cost of one event per acquisition. +*/ +TRACE_EVENT (mm_zone_lock_contended, + + TP_PROTO(struct zone *zone, int count, unsigned long caller), + +TP_ARGS (zone, count, caller), + +TP_STRUCT__entry ( + __field( int, node_id ) + __string( name, zone->name ) + __field( int, count ) + __field( unsigned long, caller ) + ), + +TP_fast_assign ( + __entry->node_id = zone_to_nid(zone); + __assign_str(name); + __entry->count = count; + __entry->caller = caller; + ), + +TP_printk ("node=%d zone=%-8s count=%-5d caller=%pS" , + __entry->node_id,__get_str (name), + __entry->count, (void *)__entry->caller) +); + +TRACE_EVENT (mm_zone_locked, + + TP_PROTO(struct zone *zone, int count, bool contended, + unsigned long caller, u64 wait_ns), + +TP_ARGS (zone, count, contended, caller, wait_ns), + +TP_STRUCT__entry ( + __field( int, node_id ) + __string( name, zone->name ) + __field( int, count ) + __field( bool, contended ) + __field( unsigned long, caller ) +__field ( u64, wait_ns ) + ), + +TP_fast_assign ( + __entry->node_id = zone_to_nid(zone); + __assign_str(name); + __entry->count = count; + __entry->contended = contended; + __entry->caller = caller; + __entry->wait_ns = wait_ns; + ), + +TP_printk ("node=%d zone=%-8s count=%-5d contended=%d caller=%pS wait=%llu ns" , + __entry->node_id,__get_str (name), + __entry->count, __entry->contended, + (void *)__entry->caller, __entry->wait_ns) +); + +TRACE_EVENT (mm_zone_lock_unlock, + + TP_PROTO(struct zone *zone, int count, unsigned long caller), + +TP_ARGS (zone, count, caller), + +TP_STRUCT__entry ( + __field( int, node_id ) + __string( name, zone->name ) + __field( int, count ) + __field( unsigned long, caller ) + ), + +TP_fast_assign ( + __entry->node_id = zone_to_nid(zone); + __assign_str(name); + __entry->count = count; + __entry->caller = caller; + ), + +TP_printk ("node=%d zone=%-8s count=%-5d caller=%pS" , + __entry->node_id,__get_str (name), + __entry->count, (void *)__entry->caller) +); +#endif /* _TRACE_KMEM_H */ /* This part must be outside protection */
mm/page_alloc.c +44 -6
98 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/2 selftests/mm: add zone->lock tracepoint verification test
From: Jesper Dangaard Brouer <[email protected]>
Add a selftest to verify the kmem:mm_zone_lock_contended, kmem:mm_zone_locked, and kmem:mm_zone_lock_unlock tracepoints.
The test has two components:
zone_lock_contention.c - a workload that spawns threads doing rapid page allocation and freeing to generate zone->lock contention. It shrinks PCP lists via percpu_pagelist_high_fraction to force frequent free_pcppages_bulk() and rmqueue_bulk() calls.
test_zone_lock_tracepoints.sh - uses bpftrace to verify tracepoints exist, have the expected fields, fire under load, and that wait_ns is populated when contention occurs.
--- tools/testing/selftests/mm/Makefile | 2 + .../mm/test_zone_lock_tracepoints.sh | 212 ++++++++++++++++++ .../selftests/mm/zone_lock_contention.c | 166 ++++++++++++++ 3 files changed, 380 insertions(+) create mode 100755 tools/testing/selftests/mm/test_zone_lock_tracepoints.sh create mode 100644 tools/testing/selftests/mm/zone_lock_contention.c
tools/testing/selftests/mm/Makefile +2 -0
@@ -106,6 +106,7 @@ TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap TEST_GEN_FILES += folio_split_race_test +TEST_GEN_FILES += zone_lock_contention ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
@@ -173,6 +174,7 @@ TEST_PROGS += ksft_thp.sh TEST_PROGS += ksft_userfaultfd.sh TEST_PROGS += ksft_vma_merge.sh TEST_PROGS += ksft_vmalloc.sh +TEST_PROGS += test_zone_lock_tracepoints.sh TEST_FILES := test_vmalloc.sh TEST_FILES += test_hmm.sh
tools/testing/selftests/mm/test_zone_lock_tracepoints.sh new file +212 -0
212 lines not on this page · raw message
tools/testing/selftests/mm/zone_lock_contention.c new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
2.43.0
Andrew Morton
maintainerQuoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.
Thanks, I'm surprised we haven't done this yet.
Unfortunately "mm: use spinlock guards for zone lock" messed this up
(https://lore.kernel.org/all/[email protected]/).
So please let's give it a few days for reviewers to comment then redo against mm.git's mm-unstable branch?
Vlastimil Babka (SUSE)
maintainerQuoted 6 lines · Andrew Morton
Quoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.Thanks, I'm surprised we haven't done this yet.
There was a recent attempt [1]. Not being a generic solution wasn't welcome.
[1] https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Unfortunately "mm: use spinlock guards for zone lock" messed this up
(https://lore.kernel.org/all/[email protected]/).So please let's give it a few days for reviewers to comment then redo against mm.git's mm-unstable branch?
Vlastimil Babka (SUSE)
maintainerQuoted 11 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Andrew Morton
Quoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.Thanks, I'm surprised we haven't done this yet.
There was a recent attempt [1]. Not being a generic solution wasn't welcome.
And this is the generic solution I think?
https://lore.kernel.org/all/[email protected]/
Quoted 6 lines
Quoted 5 lines
Unfortunately "mm: use spinlock guards for zone lock" messed this up
(https://lore.kernel.org/all/[email protected]/).So please let's give it a few days for reviewers to comment then redo against mm.git's mm-unstable branch?
Quoted 9 lines · Vlastimil Babka (SUSE)
Quoted 8 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Andrew Morton
Quoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.Thanks, I'm surprised we haven't done this yet.
Quoted 7 lines
Quoted 3 lines
There was a recent attempt [1]. Not being a generic solution wasn't welcome.
And this is the generic solution I think?
Thanks for cc'ing me, Vlastimil.
Yes, this is an attempt at a generic solution for tracing contended locks, including spinlocks, so it should also cover the use case proposed in this patchset.
In fact, zone->lock contention was one of the primary motivations for this work.
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/2] selftests/mm: add zone->lock tracepoint verification test
In reply to [email protected]Quoted 16 lines · On 5/8/26 18:22, [email protected] wrote:
From: Jesper Dangaard Brouer <[email protected]>
Add a selftest to verify the kmem:mm_zone_lock_contended, kmem:mm_zone_locked, and kmem:mm_zone_lock_unlock tracepoints.
The test has two components:
zone_lock_contention.c- a workload that spawns threads doing rapid page allocation and freeing to generatezone->lockcontention. It shrinks PCP lists viapercpu_pagelist_high_fractionto force frequentfree_pcppages_bulk()andrmqueue_bulk()calls.
test_zone_lock_tracepoints.sh- uses bpftrace to verify tracepoints exist, have the expected fields, fire under load, and thatwait_nsis populated when contention occurs.
Quoted 5 lines
Signed-off-by Jesper Dangaard Brouer <[email protected]>--- tools/testing/selftests/mm/Makefile | 2 + .../mm/test_zone_lock_tracepoints.sh | 212 ++++++++++++++++++ .../selftests/mm/zone_lock_contention.c | 166 ++++++++++++++
This really looks excessive and ... not really how we usually treat tracepoints?
I don't know about others, but I don't think this is really what we want as a MM selftest.
Signature · 3 lines
Cheers, David
Re: [PATCH 2/2] selftests/mm: add zone->lock tracepoint verification test
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 16 lines · On 5/8/26 18:22, [email protected] wrote:
From: Jesper Dangaard Brouer <[email protected]>
Add a selftest to verify the kmem:mm_zone_lock_contended, kmem:mm_zone_locked, and kmem:mm_zone_lock_unlock tracepoints.
The test has two components:
zone_lock_contention.c- a workload that spawns threads doing rapid page allocation and freeing to generatezone->lockcontention. It shrinks PCP lists viapercpu_pagelist_high_fractionto force frequentfree_pcppages_bulk()andrmqueue_bulk()calls.
test_zone_lock_tracepoints.sh- uses bpftrace to verify tracepoints exist, have the expected fields, fire under load, and thatwait_nsis populated when contention occurs.Quoted 5 lines
Signed-off-by Jesper Dangaard Brouer <[email protected]>--- tools/testing/selftests/mm/Makefile | 2 + .../mm/test_zone_lock_tracepoints.sh | 212 ++++++++++++++++++ .../selftests/mm/zone_lock_contention.c | 166 ++++++++++++++
Quoted 5 lines
This really looks excessive and ... not really how we usually treat tracepoints?
I don't know about others, but I don't think this is really what we want as a MM selftest.
I wanted to have a program that tested the code I changed, so I simply made AI write a verification test and asked it to create as a selftest, that I've run to verify my code change was correctly implemented. As I needed to trigger lock contention the test is more advanced, but luckily AI solved it in only the 2nd attempt.
It makes sense to drop this patch. We shouldn't keep this code in the kernel tree, it simply verified that my code works. There is little chance that this test will catch meaningful regressions for these tracepoints.
--Jesper
Quoted 16 lines · Dmitry Ilvokhin
Quoted 15 lines · Vlastimil Babka (SUSE)
Quoted 11 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Andrew Morton
Quoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.Thanks, I'm surprised we haven't done this yet.
There was a recent attempt [1]. Not being a generic solution wasn't welcome.
And this is the generic solution I think?
Quoted 8 lines
Quoted 1 line
Thanks for cc'ing me, Vlastimil.
Yes, this is an attempt at a generic solution for tracing contended locks, including spinlocks, so it should also cover the use case proposed in this patchset.
I'm aware of the generic solution and often use perf lock contention. And the tool libbpf-tools/klockstat. My experience is unfortunately that enabling these tracepoint is prohibitive expensive on production server, and production suffers when I run these tools.
I'm very happy to see a patchset adding a contended case. But I worry that tracing all contented locks in the system is also too much to have enabled continuously for production.
This patch is carefully constructed to minimize overhead, such that I can enable this continuously on production to catch issues. If I identify issue I will use the generic tracpoints for further debugging.
Quoted 2 lines
In fact,
zone->lockcontention was one of the primary motivations for this work.
In the generic solution I'm loosing the "zone" and pages "count". I need this information to get the answers I'm looking for. Specifically I'm looking at reducing CONFIG_PCP_BATCH_SCALE_MAX, but I want to this to be a data-driven decision (my first principle is: if you cannot measure it you cannot improve it).
I'm likely going to apply this patch to our production system, such that I can get my data-driven decision. I need to deploy it widely enough to get enough server experiencing direct-reclaim. I'll report back if people are interested in these learning?
--Jesper
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 16 lines · Dmitry Ilvokhin
Quoted 15 lines · Vlastimil Babka (SUSE)
Quoted 11 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Andrew Morton
Quoted 2 lines · e .configOn Fri, 8 May 2026 18:22:06 +0200 [email protected] wrote:
Add tracepoints to the page allocator fast paths that acquire
zone->lock, allowing diagnosis of lock contention in production.Thanks, I'm surprised we haven't done this yet.
There was a recent attempt [1]. Not being a generic solution wasn't welcome.
And this is the generic solution I think?
Quoted 8 lines
Quoted 1 line
Thanks for cc'ing me, Vlastimil.
Yes, this is an attempt at a generic solution for tracing contended locks, including spinlocks, so it should also cover the use case proposed in this patchset.
Quoted 4 lines
I'm aware of the generic solution and often use
perf lock contention. And the tool libbpf-tools/klockstat. My experience is unfortunately that enabling these tracepoint is prohibitive expensive on production server, and production suffers when I run these tools.
I think it depends on the workload: in particular how lock heavy it is.
At Meta we have a lock contention profiler (uses contention_begin and contention_end tracepoints under the hood) running continiously in the fleet. It is heavily sampled and each profilling session runs only for few seconds, but in practice it is usually enough to get a pretty good understanding what is going on.
That said, I understand the concern, and I can absolutely imagine workloads where the overhead is still unacceptably high.
Quoted 19 lines
I'm very happy to see a patchset adding a contended case. But I worry that tracing all contented locks in the system is also too much to have enabled continuously for production.
This patch is carefully constructed to minimize overhead, such that I can enable this continuously on production to catch issues. If I identify issue I will use the generic tracpoints for further debugging.
Quoted 2 lines
In fact,
zone->lockcontention was one of the primary motivations for this work.In the generic solution I'm loosing the "zone" and pages "count". I need this information to get the answers I'm looking for. Specifically I'm looking at reducing
CONFIG_PCP_BATCH_SCALE_MAX, but I want to this to be a data-driven decision (my first principle is: if you cannot measure it you cannot improve it).
Quoted 4 lines
I'm likely going to apply this patch to our production system, such that I can get my data-driven decision. I need to deploy it widely enough to get enough server experiencing direct-reclaim. I'll report back if people are interested in these learning?
I would definitely be interested in hearing about your findings.
Quoted 2 lines
--Jesper