Skip to contentSkip to conversation

mm/lruvec: trace LRU add drains and drain-all queuing

PATCH Superseded JP Kobryn 3 participants 8 messages Jun 9 – Jun 10

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

JP Kobryn

author
#0 lore raw

PATCH 1/1 mm/lruvec: trace LRU add drains and drain-all queuing

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
 	TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+	TP_PROTO(int cpu, unsigned int nr),
+
+	TP_ARGS(cpu, nr),
+
+	TP_STRUCT__entry(
+		__field(int,		cpu	)
+		__field(unsigned int,	nr	)
+	),
+
+	TP_fast_assign(
+		__entry->cpu	= cpu;
+		__entry->nr	= nr;
+	),
+
+	TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+	TP_PROTO(int target_cpu, bool force_all_cpus),
+
+	TP_ARGS(target_cpu, force_all_cpus),
+
+	TP_STRUCT__entry(
+		__field(int,	target_cpu	)
+		__field(bool,	force_all_cpus	)
+	),
+
+	TP_fast_assign(
+		__entry->target_cpu	= target_cpu;
+		__entry->force_all_cpus	= force_all_cpus;
+	),
+
+	TP_printk("target_cpu=%d force_all_cpus=%s",
+		__entry->target_cpu,
+		__entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +5 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
 	struct folio_batch *fbatch = &fbatches->lru_add;
+	unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-	if (folio_batch_count(fbatch))
+	if (nr_folios_add) {
 		folio_batch_move_lru(fbatch, lru_add);
+		trace_mm_lru_add_drain(cpu, nr_folios_add);
+	}
 
 	fbatch = &fbatches->lru_move_tail;
 	/* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
 		if (cpu_needs_drain(cpu)) {
 			INIT_WORK(work, lru_add_drain_per_cpu);
 			queue_work_on(cpu, mm_percpu_wq, work);
+			trace_mm_lru_drain_all_queue(cpu, force_all_cpus);
 			__cpumask_set_cpu(cpu, &has_work);
 		}
 	}
Signature · 1 line
2.54.0
#1 lore raw
Quoted 90 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 4 lines
mm/swap.c +1 -0
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

Do you also need this_gen and lru_drain_gen to be traced?

By the way, I'm not sure drain_all_queue is the best name here. Why not simply use add_drain_all()? It would match the existing function name better.

Best Regards
Barry

author
#2 lore raw
Quoted 88 lines · Barry Song
Quoted 87 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
Quoted 10 lines

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 7 lines
mm/swap.c +1 -0
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Quoted 2 lines

Do you also need this_gen and lru_drain_gen to be traced?

As trace parameters, I don't think they give meaningful info on who is making requests to drain all. But since I'm going to move the trace call from within the CPU loop to earlier in the function we can still see requestors even if the function exits early because a parallel drain generation already satisfied the request.

Quoted 4 lines

By the way, I'm not sure drain_all_queue is the best name here. Why not simply use add_drain_all()? It would match the existing function name better.

The new tracepoint name can be mm_lru_add_drain_all.

author
#3 lore raw
Quoted 100 lines · JP Kobryn
Quoted 88 lines · Barry Song
Quoted 87 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
Quoted 10 lines

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 7 lines
mm/swap.c +1 -0
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

Quoted 4 lines

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Actually if it's just a single invocation and the only event data is the force flag, a tracepoint may not even be needed. Other probes can be installed on function invocation and read the single argument. I can drop this from v2 and keep the single mm_lru_add_drain tracepoint.

#4 lore raw
Quoted 106 lines · JP Kobryn
Quoted 100 lines · JP Kobryn
Quoted 88 lines · Barry Song
Quoted 87 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
Quoted 10 lines

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 7 lines
mm/swap.c +1 -0
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

Quoted 4 lines

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Quoted 4 lines

Actually if it's just a single invocation and the only event data is the force flag, a tracepoint may not even be needed. Other probes can be installed on function invocation and read the single argument. I can drop this from v2 and keep the single mm_lru_add_drain tracepoint.

No we do want to trace the callers requesting to drain from all the CPUs. If you trace just lru_add_drain_cpu() then you will only see that the drain is requested for a given CPU but no information on the requester.

Also as Barry said, I think single trace for whole __lru_add_drain_all() is good enough.

author
#5 lore raw
Quoted 112 lines · Shakeel Butt
Quoted 106 lines · JP Kobryn
Quoted 100 lines · JP Kobryn
Quoted 88 lines · Barry Song
Quoted 87 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
Quoted 10 lines

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 7 lines
mm/swap.c +1 -0
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

Quoted 4 lines

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Quoted 4 lines

Actually if it's just a single invocation and the only event data is the force flag, a tracepoint may not even be needed. Other probes can be installed on function invocation and read the single argument. I can drop this from v2 and keep the single mm_lru_add_drain tracepoint.

Quoted 6 lines

No we do want to trace the callers requesting to drain from all the CPUs. If you trace just lru_add_drain_cpu() then you will only see that the drain is requested for a given CPU but no information on the requester.

Also as Barry said, I think single trace for whole __lru_add_drain_all() is good enough.

Right, but couldn't that already be done with fentry or kprobe? If we only need the calling stack and the argument value of force_all_cpus I don't see a strong need for a dedicated tracepoint.

author
#6 lore raw
Quoted 117 lines · JP Kobryn
Quoted 116 lines · Shakeel Butt
Quoted 106 lines · JP Kobryn
Quoted 100 lines · JP Kobryn
Quoted 88 lines · Barry Song
Quoted 87 lines · JP Kobryn

LRU add batches can be drained before they reach capacity. This can be a source of LRU lock contention, but it is not currently possible to attribute these drains to callers with existing tracepoints.

Add mm_lru_add_drain to report the CPU and lru_add batch count when an lru_add batch is drained. This allows tracing to distinguish full drains from partial drains and attribute them to the calling stack.

Add mm_lru_drain_all_queue to report when lru_add_drain_all() queues per-CPU drain work. This captures the requester stack and target CPU for remote drain work. The event is named as a drain-all queue event because the queued work can be needed for batches other than lru_add.

---
 include/trace/events/pagemap.h | 40 ++++++++++++++++++++++++++++++++++
 mm/swap.c                      |  6 ++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/trace/events/pagemap.h +40 -0
index 171524d3526d..ea8fc46bedb0 100644
@@ -77,6 +77,46 @@ TRACE_EVENT(mm_lru_activate,
        TP_printk("folio=%p pfn=0x%lx", __entry->folio, __entry->pfn)
 );
 
+TRACE_EVENT(mm_lru_add_drain,
+
+       TP_PROTO(int cpu, unsigned int nr),
+
+       TP_ARGS(cpu, nr),
+
+       TP_STRUCT__entry(
+               __field(int,            cpu     )
+               __field(unsigned int,   nr      )
+       ),
+
+       TP_fast_assign(
+               __entry->cpu    = cpu;
+               __entry->nr     = nr;
+       ),
+
+       TP_printk("cpu=%d nr=%u", __entry->cpu, __entry->nr)
+);
+
+TRACE_EVENT(mm_lru_drain_all_queue,
+
+       TP_PROTO(int target_cpu, bool force_all_cpus),
+
+       TP_ARGS(target_cpu, force_all_cpus),
+
+       TP_STRUCT__entry(
+               __field(int,    target_cpu      )
+               __field(bool,   force_all_cpus  )
+       ),
+
+       TP_fast_assign(
+               __entry->target_cpu     = target_cpu;
+               __entry->force_all_cpus = force_all_cpus;
+       ),
+
+       TP_printk("target_cpu=%d force_all_cpus=%s",
+               __entry->target_cpu,
+               __entry->force_all_cpus ? "true" : "false")
+);
+
 #endif /* _TRACE_PAGEMAP_H */
 
 /* This part must be outside protection */
mm/swap.c +4 -1
index 588f50d8f1a8..c385b93582eb 100644
@@ -694,9 +694,12 @@ void lru_add_drain_cpu(int cpu)
 {
        struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
        struct folio_batch *fbatch = &fbatches->lru_add;
+       unsigned int nr_folios_add = folio_batch_count(fbatch);
 
-       if (folio_batch_count(fbatch))
+       if (nr_folios_add) {
                folio_batch_move_lru(fbatch, lru_add);
+               trace_mm_lru_add_drain(cpu, nr_folios_add);
+       }
 
Quoted 10 lines

mm/swap.c static inline void __lru_add_drain_all(bool force_all_cpus) · line 934

Quoted 7 lines
mm/swap.c +1 -0
        fbatch = &fbatches->lru_move_tail;
        /* Disabling interrupts below acts as a compiler barrier. */
@@ -928,6 +931,7 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
                if (cpu_needs_drain(cpu)) {
                        INIT_WORK(work, lru_add_drain_per_cpu);
                        queue_work_on(cpu, mm_percpu_wq, work);
+                       trace_mm_lru_drain_all_queue(cpu, force_all_cpus);

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

Quoted 4 lines

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Quoted 4 lines

Actually if it's just a single invocation and the only event data is the force flag, a tracepoint may not even be needed. Other probes can be installed on function invocation and read the single argument. I can drop this from v2 and keep the single mm_lru_add_drain tracepoint.

No we do want to trace the callers requesting to drain from all the CPUs. If you trace just lru_add_drain_cpu() then you will only see that the drain is requested for a given CPU but no information on the requester.

Quoted 6 lines
Quoted 2 lines

Also as Barry said, I think single trace for whole __lru_add_drain_all() is good enough.

Right, but couldn't that already be done with fentry or kprobe? If we only need the calling stack and the argument value of force_all_cpus I don't see a strong need for a dedicated tracepoint.

Nevermind that. I see it's declared inline so I'll add a tracepoint and send v3.

#7 lore raw
Quoted 7 lines · JP Kobryn
Quoted 6 lines · JP Kobryn
Quoted 5 lines · Shakeel Butt
Quoted 4 lines · JP Kobryn
Quoted 3 lines · JP Kobryn
Quoted 2 lines · Barry Song
Quoted 1 line · JP Kobryn

[...]

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Do you need tracing on each CPU individually, or is tracing the entire __lru_add_drain_all() invocation sufficient?

I think the latter would be fine. The remote work will invoke the mm_lru_add_drain tracepoint, which will show up as kworker stacks. Since the event already has the CPU, we could see where queued drains actually ran.

Quoted 4 lines

Actually if it's just a single invocation and the only event data is the force flag, a tracepoint may not even be needed. Other probes can be installed on function invocation and read the single argument. I can drop this from v2 and keep the single mm_lru_add_drain tracepoint.

No we do want to trace the callers requesting to drain from all the CPUs. If you trace just lru_add_drain_cpu() then you will only see that the drain is requested for a given CPU but no information on the requester.

Also as Barry said, I think single trace for whole __lru_add_drain_all() is good enough.

Quoted 6 lines
Quoted 3 lines

Right, but couldn't that already be done with fentry or kprobe? If we only need the calling stack and the argument value of force_all_cpus I don't see a strong need for a dedicated tracepoint.

Nevermind that. I see it's declared inline so I'll add a tracepoint and send v3.

Thanks. BTW even without inline keyword, compiler can still decide to inline a function, so kprobe/fentry are not always reliable.

Quoted 1 line