Skip to content

Commit 8be9fbd

Browse files
mrutland-armrostedt
authored andcommitted
ftrace: Export ftrace_free_filter() to modules
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() 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(). Link: https://lkml.kernel.org/r/[email protected] Cc: [email protected] Cc: Florent Revest <[email protected]> Cc: Masami Hiramatsu <[email protected]> Fixes: e1067a0 ("ftrace/samples: Add module to test multi direct modify interface") Fixes: 5fae941 ("ftrace/samples: Add multi direct interface test module") Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Reviewed-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Mark Rutland <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 2241ab5 commit 8be9fbd

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

kernel/trace/ftrace.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,12 +1248,17 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
12481248
call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
12491249
}
12501250

1251+
/**
1252+
* ftrace_free_filter - remove all filters for an ftrace_ops
1253+
* @ops - the ops to remove the filters from
1254+
*/
12511255
void ftrace_free_filter(struct ftrace_ops *ops)
12521256
{
12531257
ftrace_ops_init(ops);
12541258
free_ftrace_hash(ops->func_hash->filter_hash);
12551259
free_ftrace_hash(ops->func_hash->notrace_hash);
12561260
}
1261+
EXPORT_SYMBOL_GPL(ftrace_free_filter);
12571262

12581263
static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
12591264
{
@@ -5839,6 +5844,10 @@ EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
58395844
*
58405845
* Filters denote which functions should be enabled when tracing is enabled
58415846
* If @ip is NULL, it fails to update filter.
5847+
*
5848+
* This can allocate memory which must be freed before @ops can be freed,
5849+
* either by removing each filtered addr or by using
5850+
* ftrace_free_filter(@ops).
58425851
*/
58435852
int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
58445853
int remove, int reset)
@@ -5858,7 +5867,11 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
58585867
*
58595868
* Filters denote which functions should be enabled when tracing is enabled
58605869
* If @ips array or any ip specified within is NULL , it fails to update filter.
5861-
*/
5870+
*
5871+
* This can allocate memory which must be freed before @ops can be freed,
5872+
* either by removing each filtered addr or by using
5873+
* ftrace_free_filter(@ops).
5874+
*/
58625875
int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
58635876
unsigned int cnt, int remove, int reset)
58645877
{
@@ -5900,6 +5913,10 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
59005913
*
59015914
* Filters denote which functions should be enabled when tracing is enabled.
59025915
* If @buf is NULL and reset is set, all functions will be enabled for tracing.
5916+
*
5917+
* This can allocate memory which must be freed before @ops can be freed,
5918+
* either by removing each filtered addr or by using
5919+
* ftrace_free_filter(@ops).
59035920
*/
59045921
int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
59055922
int len, int reset)
@@ -5919,6 +5936,10 @@ EXPORT_SYMBOL_GPL(ftrace_set_filter);
59195936
* Notrace Filters denote which functions should not be enabled when tracing
59205937
* is enabled. If @buf is NULL and reset is set, all functions will be enabled
59215938
* for tracing.
5939+
*
5940+
* This can allocate memory which must be freed before @ops can be freed,
5941+
* either by removing each filtered addr or by using
5942+
* ftrace_free_filter(@ops).
59225943
*/
59235944
int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
59245945
int len, int reset)

samples/ftrace/ftrace-direct-multi-modify.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static void __exit ftrace_direct_multi_exit(void)
152152
{
153153
kthread_stop(simple_tsk);
154154
unregister_ftrace_direct_multi(&direct, my_tramp);
155+
ftrace_free_filter(&direct);
155156
}
156157

157158
module_init(ftrace_direct_multi_init);

samples/ftrace/ftrace-direct-multi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static int __init ftrace_direct_multi_init(void)
7979
static void __exit ftrace_direct_multi_exit(void)
8080
{
8181
unregister_ftrace_direct_multi(&direct, (unsigned long) my_tramp);
82+
ftrace_free_filter(&direct);
8283
}
8384

8485
module_init(ftrace_direct_multi_init);

0 commit comments

Comments
 (0)