Skip to contentSkip to conversation

ftrace: properly merge notrace hash

PATCH In review Andy Chiu 2 participants 3 messages Apr 8, 2025

Subsystems (2)
lore
Share & tools
lore

Andy Chiu

author
#0 lore raw

PATCH v2 1/1 ftrace: properly merge notrace hash

The global notrace hash should be jointly decided by the intersection of each subops's notrace hash, but not the filter hash.

Fixes 5fccc7552ccb ("ftrace: Add subops logic to allow one ops to manage many")

---
Changelog v2:
- free both filter and notrace hash when intersect_hash() fails

---
 kernel/trace/ftrace.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/ftrace.c +5 -5
index 1a48aedb5255..bb9e1bf4fe86 100644
@@ -3526,16 +3526,16 @@ int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int
 	    ftrace_hash_empty(subops->func_hash->notrace_hash)) {
 		notrace_hash = EMPTY_HASH;
 	} else {
-		size_bits = max(ops->func_hash->filter_hash->size_bits,
-				subops->func_hash->filter_hash->size_bits);
+		size_bits = max(ops->func_hash->notrace_hash->size_bits,
+				subops->func_hash->notrace_hash->size_bits);
 		notrace_hash = alloc_ftrace_hash(size_bits);
 		if (!notrace_hash) {
-			free_ftrace_hash(filter_hash);
+			free_ftrace_hash(notrace_hash);
 			return -ENOMEM;
 		}
 
-		ret = intersect_hash(&notrace_hash, ops->func_hash->filter_hash,
-				     subops->func_hash->filter_hash);
+		ret = intersect_hash(&notrace_hash, ops->func_hash->notrace_hash,
+				     subops->func_hash->notrace_hash);
 		if (ret < 0) {
 			free_ftrace_hash(filter_hash);
 			free_ftrace_hash(notrace_hash);
Signature · 1 line
2.39.3 (Apple Git-145)
maintainer
#1 lore raw
Quoted 6 lines · On Wed, 9 Apr 2025 00:02:57 +0800 Andy Chiu <[email protected]> wrote:

The global notrace hash should be jointly decided by the intersection of each subops's notrace hash, but not the filter hash.

Fixes 5fccc7552ccb ("ftrace: Add subops logic to allow one ops to manage many")

Thanks.

I'll apply this (currently testing it along with other fixes), but I realized that this isn't working as expected.

I did the following:

  # trace-cmd start -B foo -p function_graph -l '*lock*' -n '*clock*'
  # trace-cmd start -B bar -p function_graph -l '*sched*' -n '*time*'

And then looked at:

  # cat /sys/kernel/tracing/enabled_functions  |grep clock | wc -l
  176
  # cat /sys/kernel/tracing/enabled_functions  |grep time | wc -l
  37

Which means those functions are still having callbacks even though nothing is tracing them.

What needs to be done is to filter out the filter ops from the notrace ops before adding them to the main ops.

The main ops shouldn't need any notrace hash unless all the subops hashes are the empty set.

What needs to be done when adding a new subops is to:

Copy subops filter hash:

If subops filter hash is not empty:

1) Remove all the nohash functions from the copy.

2) If the main ops notrace hash is not empty, remove any of the functions in the copy from it.

If subops filter hash is empty

1) intersect the notrace ops with the current notrace ops

I'll write up a patch on top of this one.

-- Steve

maintainer
#2 lore raw
In reply to Andy Chiu
Quoted 18 lines · On Wed, 9 Apr 2025 00:02:57 +0800 Andy Chiu <[email protected]> wrote:

The global notrace hash should be jointly decided by the intersection of each subops's notrace hash, but not the filter hash.

Fixes 5fccc7552ccb ("ftrace: Add subops logic to allow one ops to manage many")

---
Changelog v2:
- free both filter and notrace hash when intersect_hash() fails

---
 kernel/trace/ftrace.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/ftrace.c +0 -0
index 1a48aedb5255..bb9e1bf4fe86 100644
@@ -3526,16 +3526,16 @@ int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int
 	    ftrace_hash_empty(subops->func_hash->notrace_hash)) {

kernel/trace/ftrace.c int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int · line 3533

Quoted 10 lines
kernel/trace/ftrace.c +3 -3
 		notrace_hash = EMPTY_HASH;
 	} else {
-		size_bits = max(ops->func_hash->filter_hash->size_bits,
-				subops->func_hash->filter_hash->size_bits);
+		size_bits = max(ops->func_hash->notrace_hash->size_bits,
+				subops->func_hash->notrace_hash->size_bits);
 		notrace_hash = alloc_ftrace_hash(size_bits);
 		if (!notrace_hash) {
-			free_ftrace_hash(filter_hash);
+			free_ftrace_hash(notrace_hash);

Um, this should have stayed filter_hash.

-- Steve

Quoted 10 lines
kernel/trace/ftrace.c +2 -2
 			return -ENOMEM;
 		}
 
-		ret = intersect_hash(&notrace_hash, ops->func_hash->filter_hash,
-				     subops->func_hash->filter_hash);
+		ret = intersect_hash(&notrace_hash, ops->func_hash->notrace_hash,
+				     subops->func_hash->notrace_hash);
 		if (ret < 0) {
 			free_ftrace_hash(filter_hash);
 			free_ftrace_hash(notrace_hash);