tracing: Add a way to have custom events in the tracefs directory
Steven Rostedt
maintainerPATCH v3 2/4 tracing: Add sample code for custom trace events
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add sample code to show how to create custom trace events in the tracefs directory that can be enabled and modified like any event in tracefs (including triggers, histograms, synthetic events and event probes).
The example is creating a custom sched_switch and a sched_waking to limit what is recorded:
If the custom sched switch only records the prev_prio, next_prio and next_pid, it can bring the size from 64 bytes per event, down to just 16 bytes!
If sched_waking only records the prio and pid of the woken event, it will bring the size down from 36 bytes to 12 bytes per event.
This will allow for a much smaller footprint into the ring buffer and keep more events from dropping.
--- arch/x86/kernel/kprobes/core.c | 4 +- kernel/trace/ftrace.c | 33 ++- samples/Kconfig | 8 +- samples/Makefile | 1 + samples/trace_events/Makefile | 2 + samples/trace_events/trace_custom_sched.c | 271 ++++++++++++++++++++++ 6 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 samples/trace_events/trace_custom_sched.c
arch/x86/kernel/kprobes/core.c +3 -1
@@ -202,8 +202,10 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)* arch_check_ftrace_location(). Something went terribly wrong * if such an address is checked here. */ -if (WARN_ON (faddr && faddr != addr)) +if (WARN_ON (faddr && faddr != addr)) { +printk ("faddr=%pS (%lx) addr=%pS (%lx)\n" , (void *)faddr, faddr, (void *)addr, addr);return 0UL ; + }/* * Use the current code if it is not modified by Kprobe * and it cannot be modified by ftrace.
kernel/trace/ftrace.c +30 -3
@@ -1578,7 +1578,24 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end)*/ unsigned long ftrace_location (unsigned long ip) { -return ftrace_location_range (ip, ip); +struct dyn_ftrace *rec; +unsigned long offset; +unsigned long size; + + rec =lookup_rec (ip, ip); +if (!rec) { +if (!kallsyms_lookup_size_offset (ip, &size, &offset)) + goto out; + +if (!offset) + rec =lookup_rec (ip - offset, (ip - offset) + size -1 ); + } + +if (rec) +return rec->ip; + +out: +return 0 ; }/**
@@ -4962,7 +4979,8 @@ ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) {struct ftrace_func_entry *entry; -if (!ftrace_location (ip)) + ip =ftrace_location (ip); +if (!ip)return -EINVAL ;if (remove) {
@@ -5110,11 +5128,16 @@ int register_ftrace_direct(unsigned long ip, unsigned long addr)struct ftrace_func_entry *entry;struct ftrace_hash *free_hash =NULL ;struct dyn_ftrace *rec; -int ret = -EBUSY ; +int ret = -ENODEV ;mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; +/* See if there's a direct function at @ip already */ + ret = -EBUSY ;if (ftrace_find_rec_direct (ip)) goto out_unlock;
@@ -5222,6 +5245,10 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr)mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; + entry =find_direct_entry (&ip,NULL );if (!entry) goto out_unlock;
samples/Kconfig +7 -1
@@ -14,7 +14,13 @@ config SAMPLE_TRACE_EVENTS tristate "Build trace_events examples -- loadable modules only" depends on EVENT_TRACING && m help - This build trace event example modules. + This builds the trace event example module. + +config SAMPLE_TRACE_CUSTOM_EVENTS + tristate "Build custom trace event example -- loadable modules only" + depends on EVENT_TRACING && m + help + This builds the custom trace event example module. config SAMPLE_TRACE_PRINTK tristate "Build trace_printk module - tests various trace_printk formats"
samples/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT) += rpmsg/ subdir-$(CONFIG_SAMPLE_SECCOMP) += seccomp subdir-$(CONFIG_SAMPLE_TIMER) += timers obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace_events/ +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_events/ obj-$(CONFIG_SAMPLE_TRACE_PRINTK) += trace_printk/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace/
samples/trace_events/Makefile +2 -0
@@ -13,3 +13,5 @@ CFLAGS_trace-events-sample.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o + +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_custom_sched.o
samples/trace_events/trace_custom_sched.c new file +271 -0
271 lines not on this page · raw message
Signature · 1 line
2.34.1
Steven Rostedt
maintainerPATCH v3 1/4 tracing: Allow custom events to be added to the tracefs directory
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Allow custom events to be added to the events directory in the tracefs file system. For example, a module could be installed that attaches to an event and wants to be enabled and disabled via the tracefs file system. It would use trace_add_event_call() to add the event to the tracefs directory, and trace_remove_event_call() to remove it.
Make both those functions EXPORT_SYMBOL_GPL().
--- kernel/trace/trace_events.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/trace/trace_events.c +2 -0
@@ -2758,6 +2758,7 @@ int trace_add_event_call(struct trace_event_call *call)mutex_unlock (&trace_types_lock);return ret; } +EXPORT_SYMBOL_GPL (trace_add_event_call);/* * Must be called under locking of trace_types_lock, event_mutex and
@@ -2819,6 +2820,7 @@ int trace_remove_event_call(struct trace_event_call *call)return ret; } +EXPORT_SYMBOL_GPL (trace_remove_event_call);#define for_each_event (event, start, end) \ for (event = start; \
Signature · 1 line
2.34.1
Steven Rostedt
maintainerPATCH v3 3/4 tracing: Move the defines to create TRACE_EVENTS into their own files
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
In an effort to add custom event macros that can be used to create your own custom events based on existing tracepoints, move the defines of the special macros used in TRACE_EVENT() into their own files such that they can be reused for TRACE_CUSTOM_EVENT().
--- include/trace/stages/init.h | 37 ++ include/trace/stages/stage1_defines.h | 46 +++ include/trace/stages/stage2_defines.h | 48 +++ include/trace/stages/stage3_defines.h | 129 +++++++ include/trace/stages/stage4_defines.h | 57 +++ include/trace/stages/stage5_defines.h | 83 +++++ include/trace/stages/stage6_defines.h | 86 +++++ include/trace/stages/stage7_defines.h | 34 ++ include/trace/trace_events.h | 499 +------------------------- 9 files changed, 528 insertions(+), 491 deletions(-) create mode 100644 include/trace/stages/init.h create mode 100644 include/trace/stages/stage1_defines.h create mode 100644 include/trace/stages/stage2_defines.h create mode 100644 include/trace/stages/stage3_defines.h create mode 100644 include/trace/stages/stage4_defines.h create mode 100644 include/trace/stages/stage5_defines.h create mode 100644 include/trace/stages/stage6_defines.h create mode 100644 include/trace/stages/stage7_defines.h
include/trace/stages/init.h new file +37 -0
@@ -0,0 +1,37 @@ + +#define __app__ (x, y) str__##x##y +#define __app (x, y) __app__(x, y) + +#define TRACE_SYSTEM_STRING __app(TRACE_SYSTEM_VAR,__trace_system_name) + +#define TRACE_MAKE_SYSTEM_STR () \ + static const char TRACE_SYSTEM_STRING[] = \ + __stringify(TRACE_SYSTEM) + +TRACE_MAKE_SYSTEM_STR (); + +#undef TRACE_DEFINE_ENUM +#define TRACE_DEFINE_ENUM (a) \ + static struct trace_eval_map __used __initdata \ + __##TRACE_SYSTEM##_##a = \ + { \ + .system = TRACE_SYSTEM_STRING, \ + .eval_string = #a, \ + .eval_value = a \ + }; \ + static struct trace_eval_map __used \ + __section("_ftrace_eval_map") \ + *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a + +#undef TRACE_DEFINE_SIZEOF +#define TRACE_DEFINE_SIZEOF (a) \ + static struct trace_eval_map __used __initdata \ + __##TRACE_SYSTEM##_##a = \ + { \ + .system = TRACE_SYSTEM_STRING, \ + .eval_string = "sizeof(" #a ")", \ + .eval_value = sizeof(a) \ + }; \ + static struct trace_eval_map __used \ + __section("_ftrace_eval_map") \ + *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
include/trace/stages/stage1_defines.h new file +46 -0
46 lines not on this page · raw message
include/trace/stages/stage2_defines.h new file +48 -0
48 lines not on this page · raw message
include/trace/stages/stage3_defines.h new file +129 -0
129 lines not on this page · raw message
include/trace/stages/stage4_defines.h new file +57 -0
57 lines not on this page · raw message
include/trace/stages/stage5_defines.h new file +83 -0
83 lines not on this page · raw message
include/trace/stages/stage6_defines.h new file +86 -0
86 lines not on this page · raw message
include/trace/stages/stage7_defines.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* Stage 7 definitions for creating trace events */ + +#undef __entry +#define __entry REC + +#undef __print_flags +#undef __print_symbolic +#undef __print_hex +#undef __print_hex_str +#undef __get_dynamic_array +#undef __get_dynamic_array_len +#undef __get_str +#undef __get_bitmask +#undef __get_rel_dynamic_array +#undef __get_rel_dynamic_array_len +#undef __get_rel_str +#undef __get_rel_bitmask +#undef __print_array +#undef __print_hex_dump + +/* +* The below is not executed in the kernel. It is only what is +* displayed in the print format for userspace to parse. +*/ +#undef __print_ns_to_secs +#define __print_ns_to_secs (val) (val) / 1000000000UL + +#undef __print_ns_without_secs +#define __print_ns_without_secs (val) (val) % 1000000000UL + +#undef TP_printk +#define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
include/trace/trace_events.h +8 -491
547 lines not on this page · raw message
Signature · 1 line
2.34.1
Steven Rostedt
maintainerv3 cover tracing: Add a way to have custom events in the tracefs directory
We would like to have in production a way to record sched wakeups and sched switch, and be able to save the information in a small file with as much available as possible. Currently the wake up and sched switch events are 36 and 64 bytes each (plus a 4 byte ring buffer event header).
By having a custom module tap into the sched switch and waking trace points we can bring those events down to 16 and 14 bytes respectively.
This version adds the new TRACE_CUSTOM_EVENT() which makes creating a custom event as easy as creating a TRACE_EVENT()!
The TRACE_CUSTOM_EVENT() macro does all the work to create the event, and has the same format as the TRACE_EVENT() does.
Note, currently perf and bpf do not hook to this, but we can add that later.
I kept patch 2 that has the complex way of hand coding the custom event just to keep the histor of it.
But now, to add a custom event for sched_switch, all that needs to be done is:
trace_custom_sched.c:
-------------------------%<------------------------- #include <linux/trace_events.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <trace/events/sched.h>
#define CREATE_CUSTOM_TRACE_EVENTS
#include "trace_custom_sched.h"
static void fct(struct tracepoint *tp, void *priv)
{
trace_custom_event_sched_switch_update(tp);
}static int __init trace_sched_init(void)
{
for_each_kernel_tracepoint(fct, NULL);
return 0;
}static void __exit trace_sched_exit(void)
{
}
module_init(trace_sched_init); module_exit(trace_sched_exit); ------------------------->%-------------------------
-------------------------%<-------------------------
#if !defined(_TRACE_CUSTOM_SCHED_H) || defined(TRACE_CUSTOM_MULTI_READ) #define _TRACE_CUSTOM_SCHED_H
#include <linux/trace_events.h>
TRACE_CUSTOM_EVENT(sched_switch,
/* The below must be the same as the original sched_switch */
TP_PROTO(bool preempt,
struct task_struct *prev,
struct task_struct *next),
TP_ARGS(preempt, prev, next),
/* The below is the customization */
TP_STRUCT__entry(
__field( unsigned short, prev_prio )
__field( unsigned short, next_prio )
__field( pid_t, next_pid )
),
TP_fast_assign(
__entry->prev_prio = prev->prio;
__entry->next_pid = next->pid;
__entry->next_prio = next->prio;
),
TP_printk("prev_prio=%d next_pid=%d next_prio=%d",
__entry->prev_prio, __entry->next_pid, __entry->next_prio))
#endif
#undef TRACE_INCLUDE_PATH
#undef TRACE_INCLUDE_FILE
#define TRACE_INCLUDE_PATH .
#define TRACE_INCLUDE_FILE trace_custom_sched #include <trace/define_custom_trace.h> ------------------------->%-------------------------
And update the Makefile to have:
CFLAGS_trace_custom_sched.o := -I$(src)
Changes since v2: https://lore.kernel.org/all/[email protected]/
Forgot to do a:
git add include/trace/trace_custom_events.h include/trace/define_custom_trace.h
in v2.
Steven Rostedt (Google) (4):
tracing: Allow custom events to be added to the tracefs directory
tracing: Add sample code for custom trace events
tracing: Move the defines to create TRACE_EVENTS into their own files
tracing: Add TRACE_CUSTOM_EVENT() macro
----
arch/x86/kernel/kprobes/core.c | 4 +- include/linux/trace_events.h | 24 +- include/trace/define_custom_trace.h | 77 +++++ include/trace/stages/init.h | 37 +++ include/trace/stages/stage1_defines.h | 46 +++ include/trace/stages/stage2_defines.h | 48 +++ include/trace/stages/stage3_defines.h | 129 ++++++++ include/trace/stages/stage4_defines.h | 57 ++++ include/trace/stages/stage5_defines.h | 83 +++++ include/trace/stages/stage6_defines.h | 86 +++++ include/trace/stages/stage7_defines.h | 34 ++ include/trace/trace_custom_events.h | 221 +++++++++++++ include/trace/trace_events.h | 499 +----------------------------- kernel/trace/ftrace.c | 33 +- kernel/trace/trace_events.c | 2 + samples/Kconfig | 8 +- samples/Makefile | 1 + samples/trace_events/Makefile | 2 + samples/trace_events/trace_custom_sched.c | 60 ++++ samples/trace_events/trace_custom_sched.h | 95 ++++++ 20 files changed, 1049 insertions(+), 497 deletions(-) create mode 100644 include/trace/define_custom_trace.h create mode 100644 include/trace/stages/init.h create mode 100644 include/trace/stages/stage1_defines.h create mode 100644 include/trace/stages/stage2_defines.h create mode 100644 include/trace/stages/stage3_defines.h create mode 100644 include/trace/stages/stage4_defines.h create mode 100644 include/trace/stages/stage5_defines.h create mode 100644 include/trace/stages/stage6_defines.h create mode 100644 include/trace/stages/stage7_defines.h create mode 100644 include/trace/trace_custom_events.h create mode 100644 samples/trace_events/trace_custom_sched.c create mode 100644 samples/trace_events/trace_custom_sched.h
Steven Rostedt
maintainerPATCH v3 4/4 tracing: Add TRACE_CUSTOM_EVENT() macro
From: "Steven Rostedt (Google)" <[email protected]>
To make it really easy to add custom events from modules, add a TRACE_CUSTOM_EVENT() macro that acts just like the TRACE_EVENT() macro, but creates a custom event to an already existing tracepoint.
The trace_custom_sched.[ch] has been updated to use this new macro to show how simple it is.
--- include/linux/trace_events.h | 24 +- include/trace/define_custom_trace.h | 77 +++++++ include/trace/trace_custom_events.h | 221 ++++++++++++++++++ samples/trace_events/Makefile | 2 +- samples/trace_events/trace_custom_sched.c | 259 ++-------------------- samples/trace_events/trace_custom_sched.h | 95 ++++++++ 6 files changed, 441 insertions(+), 237 deletions(-) create mode 100644 include/trace/define_custom_trace.h create mode 100644 include/trace/trace_custom_events.h create mode 100644 samples/trace_events/trace_custom_sched.h
include/linux/trace_events.h +23 -1
@@ -315,6 +315,7 @@ enum {TRACE_EVENT_FL_KPROBE_BIT ,TRACE_EVENT_FL_UPROBE_BIT ,TRACE_EVENT_FL_EPROBE_BIT , + TRACE_EVENT_FL_CUSTOM_BIT, };/*
@@ -328,6 +329,9 @@ enum {* KPROBE - Event is a kprobe * UPROBE - Event is a uprobe * EPROBE - Event is an event probe +* CUSTOM - Event is a custom event (to be attached to an exsiting tracepoint) +* This is set when the custom event has not been attached +* to a tracepoint yet, then it is cleared when it is. */ enum {TRACE_EVENT_FL_FILTERED = (1 <<TRACE_EVENT_FL_FILTERED_BIT ),
@@ -339,6 +343,7 @@ enum {TRACE_EVENT_FL_KPROBE = (1 <<TRACE_EVENT_FL_KPROBE_BIT ),TRACE_EVENT_FL_UPROBE = (1 <<TRACE_EVENT_FL_UPROBE_BIT ),TRACE_EVENT_FL_EPROBE = (1 <<TRACE_EVENT_FL_EPROBE_BIT ), + TRACE_EVENT_FL_CUSTOM = (1 << TRACE_EVENT_FL_CUSTOM_BIT), };#define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
@@ -440,7 +445,9 @@ static inline bool bpf_prog_array_valid(struct trace_event_call *call)static inline const char *trace_event_name (struct trace_event_call *call) { -if (call->flags &TRACE_EVENT_FL_TRACEPOINT ) +if (call->flags &TRACE_EVENT_FL_CUSTOM ) +return call->name; +else if (call->flags &TRACE_EVENT_FL_TRACEPOINT )return call->tp ? call->tp->name :NULL ;else return call->name;
@@ -903,3 +910,18 @@ perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,#endif #endif /* _LINUX_TRACE_EVENT_H */ + +/* +* Note: we keep the TRACE_CUSTOM_EVENT outside the include file ifdef protection. +* This is due to the way trace custom events work. If a file includes two +* trace event headers under one "CREATE_CUSTOM_TRACE_EVENTS" the first include +* will override the TRACE_CUSTOM_EVENT and break the second include. +*/ + +#ifndef TRACE_CUSTOM_EVENT + +#define DECLARE_CUSTOM_EVENT_CLASS (name, proto, args, tstruct, assign, print) +#define DEFINE_CUSTOM_EVENT (template, name, proto, args) +#define TRACE_CUSTOM_EVENT (name, proto, args, struct, assign, print) + +#endif /* ifdef TRACE_CUSTOM_EVENT (see note above) */
include/trace/define_custom_trace.h new file +77 -0
77 lines not on this page · raw message
include/trace/trace_custom_events.h new file +221 -0
221 lines not on this page · raw message
samples/trace_events/Makefile +1 -1
@@ -11,7 +11,7 @@ # Here trace-events-sample.c does the CREATE_TRACE_POINTS. # CFLAGS_trace-events-sample.o := -I$(src) +CFLAGS_trace_custom_sched.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o - obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_custom_sched.o
samples/trace_events/trace_custom_sched.c +24 -235
280 lines not on this page · raw message
samples/trace_events/trace_custom_sched.h new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.34.1
Masami Hiramatsu
maintainerRe: [PATCH 2/4 v3] tracing: Add sample code for custom trace events
In reply to Steven RostedtQuoted 36 lines · On Thu, 03 Mar 2022 17:05:32 -0500 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (Google)" <[email protected]>
Add sample code to show how to create custom trace events in the tracefs directory that can be enabled and modified like any event in tracefs (including triggers, histograms, synthetic events and event probes).
The example is creating a custom
sched_switchand asched_wakingto limit what is recorded:If the custom sched switch only records the
prev_prio,next_prioandnext_pid, it can bring the size from 64 bytes per event, down to just 16 bytes!If
sched_wakingonly records the prio and pid of the woken event, it will bring the size down from 36 bytes to 12 bytes per event.This will allow for a much smaller footprint into the ring buffer and keep more events from dropping.
Suggested-by Joel Fernandes <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>--- arch/x86/kernel/kprobes/core.c | 4 +- kernel/trace/ftrace.c | 33 ++- samples/Kconfig | 8 +- samples/Makefile | 1 + samples/trace_events/Makefile | 2 + samples/trace_events/trace_custom_sched.c | 271 ++++++++++++++++++++++ 6 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 samples/trace_events/trace_custom_sched.carch/x86/kernel/kprobes/core.c +0
-0
arch/x86/kernel/kprobes/core.c __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) · line 208
Quoted 8 lines
arch/x86/kernel/kprobes/core.c +3
-1@@ -202,8 +202,10 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)* arch_check_ftrace_location(). Something went terribly wrong * if such an address is checked here. */ -if (WARN_ON (faddr && faddr != addr)) +if (WARN_ON (faddr && faddr != addr)) { +printk ("faddr=%pS (%lx) addr=%pS (%lx)\n" , (void *)faddr, faddr, (void *)addr, addr);return 0UL ; + }
This part seems not related to this patch.
Can you drop this?
Thank you,
Quoted 392 lines
arch/x86/kernel/kprobes/core.c +0
-0/* * Use the current code if it is not modified by Kprobe * and it cannot be modified by ftrace. kernel/trace/ftrace.c +30
-3@@ -1578,7 +1578,24 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end)*/ unsigned long ftrace_location (unsigned long ip) { -return ftrace_location_range (ip, ip); +struct dyn_ftrace *rec; +unsigned long offset; +unsigned long size; + + rec =lookup_rec (ip, ip); +if (!rec) { +if (!kallsyms_lookup_size_offset (ip, &size, &offset)) + goto out; + +if (!offset) + rec =lookup_rec (ip - offset, (ip - offset) + size -1 ); + } + +if (rec) +return rec->ip; + +out: +return 0 ; }/** @@ -4962,7 +4979,8 @@ ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) {struct ftrace_func_entry *entry; -if (!ftrace_location (ip)) + ip =ftrace_location (ip); +if (!ip)return -EINVAL ;if (remove) {@@ -5110,11 +5128,16 @@ int register_ftrace_direct(unsigned long ip, unsigned long addr)struct ftrace_func_entry *entry;struct ftrace_hash *free_hash =NULL ;struct dyn_ftrace *rec; -int ret = -EBUSY ; +int ret = -ENODEV ;mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; +/* See if there's a direct function at @ip already */ + ret = -EBUSY ;if (ftrace_find_rec_direct (ip)) goto out_unlock;@@ -5222,6 +5245,10 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr)mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; + entry =find_direct_entry (&ip,NULL );if (!entry) goto out_unlock;samples/Kconfig +7
-1@@ -14,7 +14,13 @@ config SAMPLE_TRACE_EVENTS tristate "Build trace_events examples -- loadable modules only" depends on EVENT_TRACING && m help - This build trace event example modules. + This builds the trace event example module. + +config SAMPLE_TRACE_CUSTOM_EVENTS + tristate "Build custom trace event example -- loadable modules only" + depends on EVENT_TRACING && m + help + This builds the custom trace event example module. config SAMPLE_TRACE_PRINTK tristate "Build trace_printk module - tests various trace_printk formats"samples/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT) += rpmsg/ subdir-$(CONFIG_SAMPLE_SECCOMP) += seccomp subdir-$(CONFIG_SAMPLE_TIMER) += timers obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace_events/ +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_events/ obj-$(CONFIG_SAMPLE_TRACE_PRINTK) += trace_printk/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace/samples/trace_events/Makefile +2
-0@@ -13,3 +13,5 @@ CFLAGS_trace-events-sample.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o + +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_custom_sched.osamples/trace_events/trace_custom_sched.c new file +271
-0@@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* event tracer +* +* Copyright (C) 2022 Google Inc, Steven Rostedt <[email protected]> +*/ + +#define pr_fmt (fmt) fmt + +#include <linux/trace_events.h> +#include <linux/version.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <trace/events/sched.h> + +#define THIS_SYSTEM "custom_sched" + +#define SCHED_PRINT_FMT \ + C("prev_prio=%d next_pid=%d next_prio=%d", REC->prev_prio, REC->next_pid, \ + REC->next_prio) + +#define SCHED_WAKING_FMT \ + C("pid=%d prio=%d", REC->pid, REC->prio) + +#undef C +#define C(a, b...) a, b + +static struct trace_event_fields sched_switch_fields[] = { + { + .type ="unsigned short" , + .name ="prev_prio" , + .size =sizeof (short ), + .align = __alignof__(short ), + .is_signed =0 , + .filter_type =FILTER_OTHER , + }, + { + .type ="unsigned short" , + .name ="next_prio" , + .size =sizeof (short ), + .align = __alignof__(short ), + .is_signed =0 , + .filter_type =FILTER_OTHER , + }, + { + .type ="unsigned int" , + .name ="next_prio" , + .size =sizeof (int ), + .align = __alignof__(int ), + .is_signed =0 , + .filter_type =FILTER_OTHER , + }, + {} +}; + +struct sched_event { +struct trace_entry ent; +unsigned short prev_prio; +unsigned short next_prio; +unsigned int next_pid; +}; + +static struct trace_event_fields sched_waking_fields[] = { + { + .type ="unsigned int" , + .name ="pid" , + .size =sizeof (int ), + .align = __alignof__(int ), + .is_signed =0 , + .filter_type =FILTER_OTHER , + }, + { + .type ="unsigned short" , + .name ="prio" , + .size =sizeof (short ), + .align = __alignof__(short ), + .is_signed =0 , + .filter_type =FILTER_OTHER , + }, + {} +}; + +struct wake_event { +struct trace_entry ent; +unsigned int pid; +unsigned short prio; +}; + +static void sched_switch_probe (void *data,bool preempt,struct task_struct *prev, +struct task_struct *next) +{ +struct trace_event_file *trace_file = data; +struct trace_event_buffer fbuffer; +struct sched_event *entry; + +if (trace_trigger_soft_disabled (trace_file)) +return ; + + entry =trace_event_buffer_reserve (&fbuffer, trace_file, +sizeof (*entry)); + +if (!entry) +return ; + + entry->prev_prio = prev->prio; + entry->next_prio = next->prio; + entry->next_pid = next->pid; + +trace_event_buffer_commit (&fbuffer); +} + +static struct trace_event_class sched_switch_class = { + .system =THIS_SYSTEM , + .reg = trace_event_reg, + .fields_array = sched_switch_fields, + .fields =LIST_HEAD_INIT (sched_switch_class.fields), + .probe = sched_switch_probe, +}; + +static void sched_waking_probe (void *data,struct task_struct *t) +{ +struct trace_event_file *trace_file = data; +struct trace_event_buffer fbuffer; +struct wake_event *entry; + +if (trace_trigger_soft_disabled (trace_file)) +return ; + + entry =trace_event_buffer_reserve (&fbuffer, trace_file, +sizeof (*entry)); + +if (!entry) +return ; + + entry->prio = t->prio; + entry->pid = t->pid; + +trace_event_buffer_commit (&fbuffer); +} + +static struct trace_event_class sched_waking_class = { + .system =THIS_SYSTEM , + .reg = trace_event_reg, + .fields_array = sched_waking_fields, + .fields =LIST_HEAD_INIT (sched_waking_class.fields), + .probe = sched_waking_probe, +}; + +static enum print_line_t sched_switch_output (struct trace_iterator *iter,int flags, +struct trace_event *trace_event) +{ +struct trace_seq *s = &iter->seq; +struct sched_event *REC = (struct sched_event *)iter->ent; +int ret; + + ret =trace_raw_output_prep (iter, trace_event); +if (ret !=TRACE_TYPE_HANDLED ) +return ret; + +trace_seq_printf (s,SCHED_PRINT_FMT ); +trace_seq_putc (s,'\n' ); + +return trace_handle_return (s); +} + +static struct trace_event_functions sched_switch_funcs = { + .trace = sched_switch_output, +}; + +static enum print_line_t sched_waking_output (struct trace_iterator *iter,int flags, +struct trace_event *trace_event) +{ +struct trace_seq *s = &iter->seq; +struct wake_event *REC = (struct wake_event *)iter->ent; +int ret; + + ret =trace_raw_output_prep (iter, trace_event); +if (ret !=TRACE_TYPE_HANDLED ) +return ret; + +trace_seq_printf (s,SCHED_WAKING_FMT ); +trace_seq_putc (s,'\n' ); + +return trace_handle_return (s); +} + +static struct trace_event_functions sched_waking_funcs = { + .trace = sched_waking_output, +}; + +#undef C +#define C(a, b...) #a "," __stringify(b) + +static struct trace_event_call sched_switch_call = { + .class = &sched_switch_class, + .event = { + .funcs = &sched_switch_funcs, + }, + .print_fmt =SCHED_PRINT_FMT , + .module =THIS_MODULE , + .flags =TRACE_EVENT_FL_TRACEPOINT , +}; + +static struct trace_event_call sched_waking_call = { + .class = &sched_waking_class, + .event = { + .funcs = &sched_waking_funcs, + }, + .print_fmt =SCHED_WAKING_FMT , + .module =THIS_MODULE , + .flags =TRACE_EVENT_FL_TRACEPOINT , +}; + +static void fct (struct tracepoint *tp,void *priv) +{ +if (tp->name &&strcmp (tp->name,"sched_switch" ) ==0 ) + sched_switch_call.tp = tp; +else if (tp->name &&strcmp (tp->name,"sched_waking" ) ==0 ) + sched_waking_call.tp = tp; +} + +static int add_event (struct trace_event_call *call) +{ +int ret; + + ret =register_trace_event (&call->event); +if (WARN_ON (!ret)) +return -ENODEV ; + + ret =trace_add_event_call (call); +if (WARN_ON (ret)) +unregister_trace_event (&call->event); + +return ret; +} + +static int __init trace_sched_init (void ) +{ +int ret; + +check_trace_callback_type_sched_switch (sched_switch_probe); +check_trace_callback_type_sched_waking (sched_waking_probe); + +for_each_kernel_tracepoint (fct,NULL ); + + ret =add_event (&sched_switch_call); +if (ret) +return ret; + + ret =add_event (&sched_waking_call); +if (ret) +trace_remove_event_call (&sched_switch_call); + +return ret; +} + +static void __exit trace_sched_exit(void) +{ +trace_set_clr_event (THIS_SYSTEM ,"sched_switch" ,0 ); +trace_set_clr_event (THIS_SYSTEM ,"sched_waking" ,0 ); + +trace_remove_event_call (&sched_switch_call); +trace_remove_event_call (&sched_waking_call); +} + +module_init (trace_sched_init); +module_exit (trace_sched_exit); + +MODULE_AUTHOR ("Steven Rostedt" ); +MODULE_DESCRIPTION ("Custom scheduling events" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.34.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerRe: [PATCH 2/4 v3] tracing: Add sample code for custom trace events
Quoted 6 lines · On Sat, 5 Mar 2022 01:09:30 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 6 lines
arch/x86/kernel/kprobes/core.c +0
-0@@ -202,8 +202,10 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)* arch_check_ftrace_location(). Something went terribly wrong * if such an address is checked here. */
Quoted 8 lines
arch/x86/kernel/kprobes/core.c __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) · line 208
Quoted 5 lines
arch/x86/kernel/kprobes/core.c +3
-1-if (WARN_ON (faddr && faddr != addr)) +if (WARN_ON (faddr && faddr != addr)) { +printk ("faddr=%pS (%lx) addr=%pS (%lx)\n" , (void *)faddr, faddr, (void *)addr, addr);return 0UL ; + }This part seems not related to this patch.
Can you drop this?
Ug, yeah. Thanks for spotting this. This was used to help debug the IBT code from Peter Z. I didn't even notice it.
-- Steve