ftrace: add sample code with dynamic ftrace_ops
Mark Rutland
reviewercover ftrace: add sample code with dynamic ftrace_ops
This series adds sample code to manipulate dynamic ftrace_ops, which I've been using to benchmark/test some changes I've been making in this area for arm64.
In the process of writing that I spotted a couple of minor issues, addressed by the first two patches.
I'm not sure whether this should be a sample or something under lib/; I'm happy to change that if folk have strong opinions.
Thanks;
Mark.
Mark Rutland (3):
ftrace: maintain samples/ftrace
ftrace: export ftrace_free_filter() to modules
ftrace: add sample with custom ops
MAINTAINERS | 1 + kernel/trace/ftrace.c | 23 +- samples/Kconfig | 7 + samples/Makefile | 1 + samples/ftrace/Makefile | 1 + samples/ftrace/ftrace-direct-multi-modify.c | 1 + samples/ftrace/ftrace-direct-multi.c | 1 + samples/ftrace/ftrace-ops.c | 252 ++++++++++++++++++++ 8 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 samples/ftrace/ftrace-ops.c
Signature · 1 line
2.30.2
Mark Rutland
reviewerPATCH 1/3 ftrace: maintain samples/ftrace
There's no entry in MAINTAINERS for samples/ftrace. Add one so that the FTRACE maintainers are kept in the loop.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -8462,6 +8462,7 @@ F: kernel/trace/fgraph.c F: arch/*/*/*/*ftrace* F: arch/*/*/*ftrace* F: include/*/ftrace.h +F: samples/ftrace FUNGIBLE ETHERNET DRIVERS M: Dimitris Michailidis <[email protected]>
Signature · 1 line
2.30.2
Mark Rutland
reviewerPATCH 2/3 ftrace: export ftrace_free_filter() to modules
In reply to Mark RutlandSetting filters on an ftrace ops results in some memory being allocated for the filter hashes, which must be freed before the ops can be freed. This can be done by removing every individual element of the hash by calling ftrace_set_filter_ip() or ftrace_set_filter_ips() with remove set, but this is somewhat error prone as it's easy to forget to remove an element.
Make it easier to clean this up by exporting ftrace_free_filter(), which can be used to clean up all of the filter hashes after an ftrace_ops has been unregistered.
Using this, fix the ftrace-direct* samples to free hashes prior to being unloaded. All other code either removes individual filters explicitly or is built-in and already calls ftrace_free_filter().
--- kernel/trace/ftrace.c | 23 ++++++++++++++++++++- samples/ftrace/ftrace-direct-multi-modify.c | 1 + samples/ftrace/ftrace-direct-multi.c | 1 + 3 files changed, 24 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +22 -1
@@ -1248,12 +1248,17 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)call_rcu (&hash->rcu, __free_ftrace_hash_rcu); } +/** +* ftrace_free_filter - remove all filters for an ftrace_ops +* @ops - the ops to remove the filters from +*/ void ftrace_free_filter (struct ftrace_ops *ops) {ftrace_ops_init (ops);free_ftrace_hash (ops->func_hash->filter_hash);free_ftrace_hash (ops->func_hash->notrace_hash); } +EXPORT_SYMBOL_GPL (ftrace_free_filter);static struct ftrace_hash *alloc_ftrace_hash (int size_bits) {
@@ -5837,6 +5842,10 @@ EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);* * Filters denote which functions should be enabled when tracing is enabled * If @ip is NULL, it fails to update filter. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_filter_ip (struct ftrace_ops *ops,unsigned long ip,int remove,int reset)
@@ -5856,7 +5865,11 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);* * Filters denote which functions should be enabled when tracing is enabled * If @ips array or any ip specified within is NULL , it fails to update filter. -*/ +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). +*/ int ftrace_set_filter_ips (struct ftrace_ops *ops,unsigned long *ips,unsigned int cnt,int remove,int reset) {
@@ -5898,6 +5911,10 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,* * Filters denote which functions should be enabled when tracing is enabled. * If @buf is NULL and reset is set, all functions will be enabled for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_filter (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)
@@ -5917,6 +5934,10 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter);* Notrace Filters denote which functions should not be enabled when tracing * is enabled. If @buf is NULL and reset is set, all functions will be enabled * for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_notrace (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)
samples/ftrace/ftrace-direct-multi-modify.c +1 -0
@@ -152,6 +152,7 @@ static void __exit ftrace_direct_multi_exit(void) {kthread_stop (simple_tsk);unregister_ftrace_direct_multi (&direct, my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);
samples/ftrace/ftrace-direct-multi.c +1 -0
@@ -79,6 +79,7 @@ static int __init ftrace_direct_multi_init(void) static void __exit ftrace_direct_multi_exit(void) {unregister_ftrace_direct_multi (&direct, (unsigned long ) my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);
Signature · 1 line
2.30.2
Steven Rostedt
maintainerRe: [PATCH 2/3] ftrace: export ftrace_free_filter() to modules
On Thu, 3 Nov 2022 17:09:06 +0000
Mark Rutland <[email protected]> wrote:
FYI, the subject should start with a capital letter.
ftrace: Export ftrace_free_filter() to modules
Quoted 83 lines
Setting filters on an ftrace ops results in some memory being allocated for the filter hashes, which must be freed before the ops can be freed. This can be done by removing every individual element of the hash by calling
ftrace_set_filter_ip()orftrace_set_filter_ips()withremoveset, but this is somewhat error prone as it's easy to forget to remove an element.Make it easier to clean this up by exporting
ftrace_free_filter(), which can be used to clean up all of the filter hashes after anftrace_opshas been unregistered.Using this, fix the ftrace-direct* samples to free hashes prior to being unloaded. All other code either removes individual filters explicitly or is built-in and already calls
ftrace_free_filter().Signed-off-by Mark Rutland <[email protected]>Cc Florent Revest <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Steven Rostedt <[email protected]>--- kernel/trace/ftrace.c | 23 ++++++++++++++++++++- samples/ftrace/ftrace-direct-multi-modify.c | 1 + samples/ftrace/ftrace-direct-multi.c | 1 + 3 files changed, 24 insertions(+), 1 deletion(-)kernel/trace/ftrace.c +18
-1@@ -1248,12 +1248,17 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)call_rcu (&hash->rcu, __free_ftrace_hash_rcu); } +/** +* ftrace_free_filter - remove all filters for an ftrace_ops +* @ops - the ops to remove the filters from +*/ void ftrace_free_filter (struct ftrace_ops *ops) {ftrace_ops_init (ops);free_ftrace_hash (ops->func_hash->filter_hash);free_ftrace_hash (ops->func_hash->notrace_hash); } +EXPORT_SYMBOL_GPL (ftrace_free_filter);static struct ftrace_hash *alloc_ftrace_hash (int size_bits) {@@ -5837,6 +5842,10 @@ EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);* * Filters denote which functions should be enabled when tracing is enabled * If @ip is NULL, it fails to update filter. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_filter_ip (struct ftrace_ops *ops,unsigned long ip,int remove,int reset)@@ -5856,7 +5865,11 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);* * Filters denote which functions should be enabled when tracing is enabled * If @ips array or any ip specified within is NULL , it fails to update filter. -*/ +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). +*/ int ftrace_set_filter_ips (struct ftrace_ops *ops,unsigned long *ips,unsigned int cnt,int remove,int reset) {@@ -5898,6 +5911,10 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,* * Filters denote which functions should be enabled when tracing is enabled. * If @buf is NULL and reset is set, all functions will be enabled for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_filter (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)
kernel/trace/ftrace.c EXPORT_SYMBOL_GPL(ftrace_set_filter); · line 5939
Quoted 6 lines
kernel/trace/ftrace.c +3
-0@@ -5917,6 +5934,10 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter);* Notrace Filters denote which functions should not be enabled when tracing * is enabled. If @buf is NULL and reset is set, all functions will be enabled * for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using
I love how you cut and pasted two typos four times. ;-)
-- Steve
Quoted 28 lines
kernel/trace/ftrace.c +1
-0+* ftrace_free_filter(@ops). */ int ftrace_set_notrace (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)samples/ftrace/ftrace-direct-multi-modify.c +1
-0@@ -152,6 +152,7 @@ static void __exit ftrace_direct_multi_exit(void) {kthread_stop (simple_tsk);unregister_ftrace_direct_multi (&direct, my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);samples/ftrace/ftrace-direct-multi.c +1
-0@@ -79,6 +79,7 @@ static int __init ftrace_direct_multi_init(void) static void __exit ftrace_direct_multi_exit(void) {unregister_ftrace_direct_multi (&direct, (unsigned long ) my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);
Re: [PATCH 2/3] ftrace: export ftrace_free_filter() to modules
Quoted 4 lines · On Tue, 22 Nov 2022 16:59:38 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines
+ * This can allocate memory which must be freed before @ops can be freed,
+ * either by remvoing eached filtered addr or by usingI love how you cut and pasted two typos four times. ;-)
Besides the "remvoing" and "eached"...
But please send a v2.
-- Steve
Mark Rutland
reviewerRe: [PATCH 2/3] ftrace: export ftrace_free_filter() to modules
In reply to Steven RostedtQuoted 6 lines · Steven Rostedt
On Thu, 3 Nov 2022 17:09:06 +0000
Mark Rutland <[email protected]> wrote:FYI, the subject should start with a capital letter.
ftrace: Export
ftrace_free_filter()to modules
Ah; I'll fix all three patches for v2.
[...]
Quoted 35 lines
Quoted 35 lines
*
* Filters denote which functions should be enabled when tracing is enabled
* If @ip is NULL, it fails to update filter.
+ *
+ * This can allocate memory which must be freed before @ops can be freed,
+ * either by remvoing eached filtered addr or by using
+ *ftrace_free_filter(@ops).
*/
int ftrace_set_filter_ip(structftrace_ops*ops, unsigned long ip,kernel/trace/ftrace.c +9
-1int remove, int reset)@@ -5856,7 +5865,11 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);* * Filters denote which functions should be enabled when tracing is enabled * If @ips array or any ip specified within is NULL , it fails to update filter. -*/ +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). +*/ int ftrace_set_filter_ips (struct ftrace_ops *ops,unsigned long *ips,unsigned int cnt,int remove,int reset) {@@ -5898,6 +5911,10 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,* * Filters denote which functions should be enabled when tracing is enabled. * If @buf is NULL and reset is set, all functions will be enabled for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using +* ftrace_free_filter(@ops). */ int ftrace_set_filter (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)@@ -5917,6 +5934,10 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter);
Quoted 8 lines
kernel/trace/ftrace.c EXPORT_SYMBOL_GPL(ftrace_set_filter); · line 5939
Quoted 6 lines
kernel/trace/ftrace.c +3
-0* Notrace Filters denote which functions should not be enabled when tracing * is enabled. If @buf is NULL and reset is set, all functions will be enabled * for tracing. +* +* This can allocate memory which must be freed before @ops can be freed, +* either by remvoing eached filtered addr or by using I love how you cut and pasted two typos four times. ;-)
Ugh; that is the power of automation and little sleep...
I'll fix that up.
Mark.
Quoted 32 lines
-- Steve
Quoted 28 lines
kernel/trace/ftrace.c +1
-0+* ftrace_free_filter(@ops). */ int ftrace_set_notrace (struct ftrace_ops *ops,unsigned char *buf,int len,int reset)samples/ftrace/ftrace-direct-multi-modify.c +1
-0@@ -152,6 +152,7 @@ static void __exit ftrace_direct_multi_exit(void) {kthread_stop (simple_tsk);unregister_ftrace_direct_multi (&direct, my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);samples/ftrace/ftrace-direct-multi.c +1
-0@@ -79,6 +79,7 @@ static int __init ftrace_direct_multi_init(void) static void __exit ftrace_direct_multi_exit(void) {unregister_ftrace_direct_multi (&direct, (unsigned long ) my_tramp); +ftrace_free_filter (&direct); }module_init (ftrace_direct_multi_init);
Mark Rutland
reviewerPATCH 3/3 ftrace: add sample with custom ops
In reply to Mark RutlandWhen reworking core ftrace code or architectural ftrace code, it's often necessary to test/analyse/benchmark a number of ftrace_ops configurations. This patch adds a module which can be used to explore some of those configurations.
I'm using this to benchmark various options for changing the way trampolines and handling of ftrace_ops work on arm64.
--- samples/Kconfig | 7 + samples/Makefile | 1 + samples/ftrace/Makefile | 1 + samples/ftrace/ftrace-ops.c | 252 ++++++++++++++++++++++++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 samples/ftrace/ftrace-ops.c
samples/Kconfig +7 -0
@@ -46,6 +46,13 @@ config SAMPLE_FTRACE_DIRECT_MULTI that hooks to wake_up_process and schedule, and prints the function addresses. +config SAMPLE_FTRACE_OPS + tristate "Build custom ftrace ops example" + depends on FUNCTION_TRACER + help + This builds an ftrace ops example that hooks two functions and + measures the time taken to invoke one function a number of times. + config SAMPLE_TRACE_ARRAY tristate "Build sample module for kernel access to Ftrace instancess" depends on EVENT_TRACING && m
samples/Makefile +1 -0
@@ -24,6 +24,7 @@ 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/ +obj-$(CONFIG_SAMPLE_FTRACE_OPS) += ftrace/ obj-$(CONFIG_SAMPLE_TRACE_ARRAY) += ftrace/ subdir-$(CONFIG_SAMPLE_UHID) += uhid obj-$(CONFIG_VIDEO_PCI_SKELETON) += v4l/
samples/ftrace/Makefile +1 -0
@@ -5,6 +5,7 @@ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace-direct-too.o obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace-direct-modify.o obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace-direct-multi.o obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace-direct-multi-modify.o +obj-$(CONFIG_SAMPLE_FTRACE_OPS) += ftrace-ops.o CFLAGS_sample-trace-array.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_ARRAY) += sample-trace-array.o
samples/ftrace/ftrace-ops.c new file +252 -0
252 lines not on this page · raw message
Signature · 1 line
2.30.2