Skip to content

Commit 91edde2

Browse files
cunctatorrostedt
authored andcommitted
ftrace: Implement fs notification for tracing_max_latency
This patch implements the feature that the tracing_max_latency file, e.g. /sys/kernel/debug/tracing/tracing_max_latency will receive notifications through the fsnotify framework when a new latency is available. One particularly interesting use of this facility is when enabling threshold tracing, through /sys/kernel/debug/tracing/tracing_thresh, together with the preempt/irqsoff tracers. This makes it possible to implement a user space program that can, with equal probability, obtain traces of latencies that occur immediately after each other in spite of the fact that the preempt/irqsoff tracers operate in overwrite mode. This facility works with the hwlat, preempt/irqsoff, and wakeup tracers. The tracers may call the latency_fsnotify() from places such as __schedule() or do_idle(); this makes it impossible to call queue_work() directly without risking a deadlock. The same would happen with a softirq, kernel thread or tasklet. For this reason we use the irq_work mechanism to call queue_work(). This patch creates a new workqueue. The reason for doing this is that I wanted to use the WQ_UNBOUND and WQ_HIGHPRI flags. My thinking was that WQ_UNBOUND might help with the latency in some important cases. If we use: queue_work(system_highpri_wq, &tr->fsnotify_work); then the work will (almost) always execute on the same CPU but if we are unlucky that CPU could be too busy while there could be another CPU in the system that would be able to process the work soon enough. queue_work_on() could be used to queue the work on another CPU but it seems difficult to select the right CPU. Link: http://lkml.kernel.org/r/20191008220824.7911-2-viktor.rosendahl@gmail.com Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Signed-off-by: Viktor Rosendahl (BMW) <viktor.rosendahl@gmail.com> [ Added max() to have one compare for max latency ] Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent da537f0 commit 91edde2

3 files changed

Lines changed: 98 additions & 6 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#include <linux/trace.h>
4646
#include <linux/sched/clock.h>
4747
#include <linux/sched/rt.h>
48+
#include <linux/fsnotify.h>
49+
#include <linux/irq_work.h>
50+
#include <linux/workqueue.h>
4851

4952
#include "trace.h"
5053
#include "trace_output.h"
@@ -1497,6 +1500,74 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
14971500
}
14981501

14991502
unsigned long __read_mostly tracing_thresh;
1503+
static const struct file_operations tracing_max_lat_fops;
1504+
1505+
#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1506+
defined(CONFIG_FSNOTIFY)
1507+
1508+
static struct workqueue_struct *fsnotify_wq;
1509+
1510+
static void latency_fsnotify_workfn(struct work_struct *work)
1511+
{
1512+
struct trace_array *tr = container_of(work, struct trace_array,
1513+
fsnotify_work);
1514+
fsnotify(tr->d_max_latency->d_inode, FS_MODIFY,
1515+
tr->d_max_latency->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
1516+
}
1517+
1518+
static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1519+
{
1520+
struct trace_array *tr = container_of(iwork, struct trace_array,
1521+
fsnotify_irqwork);
1522+
queue_work(fsnotify_wq, &tr->fsnotify_work);
1523+
}
1524+
1525+
static void trace_create_maxlat_file(struct trace_array *tr,
1526+
struct dentry *d_tracer)
1527+
{
1528+
INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1529+
init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1530+
tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
1531+
d_tracer, &tr->max_latency,
1532+
&tracing_max_lat_fops);
1533+
}
1534+
1535+
__init static int latency_fsnotify_init(void)
1536+
{
1537+
fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1538+
WQ_UNBOUND | WQ_HIGHPRI, 0);
1539+
if (!fsnotify_wq) {
1540+
pr_err("Unable to allocate tr_max_lat_wq\n");
1541+
return -ENOMEM;
1542+
}
1543+
return 0;
1544+
}
1545+
1546+
late_initcall_sync(latency_fsnotify_init);
1547+
1548+
void latency_fsnotify(struct trace_array *tr)
1549+
{
1550+
if (!fsnotify_wq)
1551+
return;
1552+
/*
1553+
* We cannot call queue_work(&tr->fsnotify_work) from here because it's
1554+
* possible that we are called from __schedule() or do_idle(), which
1555+
* could cause a deadlock.
1556+
*/
1557+
irq_work_queue(&tr->fsnotify_irqwork);
1558+
}
1559+
1560+
/*
1561+
* (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1562+
* defined(CONFIG_FSNOTIFY)
1563+
*/
1564+
#else
1565+
1566+
#define trace_create_maxlat_file(tr, d_tracer) \
1567+
trace_create_file("tracing_max_latency", 0644, d_tracer, \
1568+
&tr->max_latency, &tracing_max_lat_fops)
1569+
1570+
#endif
15001571

15011572
#ifdef CONFIG_TRACER_MAX_TRACE
15021573
/*
@@ -1536,6 +1607,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
15361607

15371608
/* record this tasks comm */
15381609
tracing_record_cmdline(tsk);
1610+
latency_fsnotify(tr);
15391611
}
15401612

15411613
/**
@@ -8594,8 +8666,7 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
85948666
create_trace_options_dir(tr);
85958667

85968668
#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
8597-
trace_create_file("tracing_max_latency", 0644, d_tracer,
8598-
&tr->max_latency, &tracing_max_lat_fops);
8669+
trace_create_maxlat_file(tr, d_tracer);
85998670
#endif
86008671

86018672
if (ftrace_create_function_files(tr, d_tracer))

‎kernel/trace/trace.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/trace_events.h>
1717
#include <linux/compiler.h>
1818
#include <linux/glob.h>
19+
#include <linux/irq_work.h>
20+
#include <linux/workqueue.h>
1921

2022
#ifdef CONFIG_FTRACE_SYSCALLS
2123
#include <asm/unistd.h> /* For NR_SYSCALLS */
@@ -264,6 +266,11 @@ struct trace_array {
264266
#endif
265267
#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
266268
unsigned long max_latency;
269+
#ifdef CONFIG_FSNOTIFY
270+
struct dentry *d_max_latency;
271+
struct work_struct fsnotify_work;
272+
struct irq_work fsnotify_irqwork;
273+
#endif
267274
#endif
268275
struct trace_pid_list __rcu *filtered_pids;
269276
/*
@@ -786,6 +793,17 @@ void update_max_tr_single(struct trace_array *tr,
786793
struct task_struct *tsk, int cpu);
787794
#endif /* CONFIG_TRACER_MAX_TRACE */
788795

796+
#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
797+
defined(CONFIG_FSNOTIFY)
798+
799+
void latency_fsnotify(struct trace_array *tr);
800+
801+
#else
802+
803+
static void latency_fsnotify(struct trace_array *tr) { }
804+
805+
#endif
806+
789807
#ifdef CONFIG_STACKTRACE
790808
void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
791809
int pc);

‎kernel/trace/trace_hwlat.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ static int get_sample(void)
237237
/* If we exceed the threshold value, we have found a hardware latency */
238238
if (sample > thresh || outer_sample > thresh) {
239239
struct hwlat_sample s;
240+
u64 latency;
240241

241242
ret = 1;
242243

@@ -253,11 +254,13 @@ static int get_sample(void)
253254
s.nmi_count = nmi_count;
254255
trace_hwlat_sample(&s);
255256

257+
latency = max(sample, outer_sample);
258+
256259
/* Keep a running maximum ever recorded hardware latency */
257-
if (sample > tr->max_latency)
258-
tr->max_latency = sample;
259-
if (outer_sample > tr->max_latency)
260-
tr->max_latency = outer_sample;
260+
if (latency > tr->max_latency) {
261+
tr->max_latency = latency;
262+
latency_fsnotify(tr);
263+
}
261264
}
262265

263266
out:

0 commit comments

Comments
 (0)