Skip to contentSkip to conversation

ftrace: simplify ftrace hash lookup code

PATCH Superseded Changbin Du 2 participants 6 messages Sep 9, 2019 – Sep 10, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ftrace: simplify ftrace hash lookup code

Function ftrace_lookup_ip() will check empty hash table. So we don't need extra check outside.

---
v2: fix incorrect code remove.

---
 kernel/trace/ftrace.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +2 -7
index f9821a3374e9..92aab854d3b1 100644
@@ -1463,8 +1463,7 @@ static bool hash_contains_ip(unsigned long ip,
 	 */
 	return (ftrace_hash_empty(hash->filter_hash) ||
 		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
-		(ftrace_hash_empty(hash->notrace_hash) ||
-		 !__ftrace_lookup_ip(hash->notrace_hash, ip));
+	       !ftrace_lookup_ip(hash->notrace_hash, ip);
 }
 
 /*
@@ -6036,11 +6035,7 @@ clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
 {
 	struct ftrace_func_entry *entry;
 
-	if (ftrace_hash_empty(hash))
-		return;
-
-	entry = __ftrace_lookup_ip(hash, func->ip);
-
+	entry = ftrace_lookup_ip(hash, func->ip);
 	/*
 	 * Do not allow this rec to match again.
 	 * Yeah, it may waste some memory, but will be removed
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 16 lines · On Mon, 9 Sep 2019 08:31:59 +0800 Changbin Du <[email protected]> wrote:

Function ftrace_lookup_ip() will check empty hash table. So we don't need extra check outside.

---
v2: fix incorrect code remove.

---
 kernel/trace/ftrace.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +0 -0
index f9821a3374e9..92aab854d3b1 100644

kernel/trace/ftrace.c static bool hash_contains_ip(unsigned long ip, · line 1466

Quoted 6 lines
kernel/trace/ftrace.c +1 -2
@@ -1463,8 +1463,7 @@ static bool hash_contains_ip(unsigned long ip,
 	 */
 	return (ftrace_hash_empty(hash->filter_hash) ||
 		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
-		(ftrace_hash_empty(hash->notrace_hash) ||
-		 !__ftrace_lookup_ip(hash->notrace_hash, ip));
+	       !ftrace_lookup_ip(hash->notrace_hash, ip);

I don't care for this part. I've nacked this change in the past. Why? let's compare the changes:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		(ftrace_hash_empty(hash->notrace_hash) ||
		 !__ftrace_lookup_ip(hash->notrace_hash, ip));

vs:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		!ftrace_lookup_ip(hash->notrace_hash, ip);

The issue I have with this is that it abstracts out the difference between the filter_hash and the notrace_hash. Sometimes open coded works better if it is compared to something that is similar.

The current code I see:

Return true if (filter_hash is empty or ip exists in filter_hash
and notrace_hash is empty or it does not exist in notrace_hash

With your update I see:

Return true if filter_hash is empty or ip exists in filter_hash
and ip does not exist in notrace_hash

It makes it not easy to see if what happens if notrace_hash is empty

Hmm, come to think of it, perhaps we should change ftrace_lookup_ip() to include what to do on empty.

Maybe:

bool ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return __ftrace_lookup_ip(hash, ip);
}

Then we can change the above to:

	return ftrace_lookup_ip(hash->filter_hash, ip, true) &&
	       !ftrace_lookup_ip(hash->notrace_hash, ip, false);

That would probably work better.

Want to send that update?

-- Steve

Quoted 16 lines
kernel/trace/ftrace.c +1 -5
 }
 
 /*
@@ -6036,11 +6035,7 @@ clear_func_from_hash(struct ftrace_init_func
 *func, struct ftrace_hash *hash) {
 	struct ftrace_func_entry *entry;
 
-	if (ftrace_hash_empty(hash))
-		return;
-
-	entry = __ftrace_lookup_ip(hash, func->ip);
-
+	entry = ftrace_lookup_ip(hash, func->ip);
 	/*
 	 * Do not allow this rec to match again.
 	 * Yeah, it may waste some memory, but will be removed
#2 lore raw
Quoted 49 lines · Steven Rostedt
Quoted 16 lines · On Mon, 9 Sep 2019 08:31:59 +0800 Changbin Du <[email protected]> wrote:

Function ftrace_lookup_ip() will check empty hash table. So we don't need extra check outside.

---
v2: fix incorrect code remove.

---
 kernel/trace/ftrace.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +0 -0
index f9821a3374e9..92aab854d3b1 100644

kernel/trace/ftrace.c static bool hash_contains_ip(unsigned long ip, · line 1466

Quoted 6 lines
kernel/trace/ftrace.c +1 -2
@@ -1463,8 +1463,7 @@ static bool hash_contains_ip(unsigned long ip,
 	 */
 	return (ftrace_hash_empty(hash->filter_hash) ||
 		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
-		(ftrace_hash_empty(hash->notrace_hash) ||
-		 !__ftrace_lookup_ip(hash->notrace_hash, ip));
+	       !ftrace_lookup_ip(hash->notrace_hash, ip);

I don't care for this part. I've nacked this change in the past. Why? let's compare the changes:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		(ftrace_hash_empty(hash->notrace_hash) ||
		 !__ftrace_lookup_ip(hash->notrace_hash, ip));

vs:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		!ftrace_lookup_ip(hash->notrace_hash, ip);

The issue I have with this is that it abstracts out the difference between the filter_hash and the notrace_hash. Sometimes open coded works better if it is compared to something that is similar.

The current code I see:

Return true if (filter_hash is empty or ip exists in filter_hash
and notrace_hash is empty or it does not exist in notrace_hash

Quoted 7 lines

With your update I see:

Return true if filter_hash is empty or ip exists in filter_hash
and ip does not exist in notrace_hash

It makes it not easy to see if what happens if notrace_hash is empty

Yes, I agree with you entirly.

Quoted 13 lines

Hmm, come to think of it, perhaps we should change ftrace_lookup_ip() to include what to do on empty.

Maybe:

bool ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return __ftrace_lookup_ip(hash, ip);
}
Quoted 9 lines

Then we can change the above to:

	return ftrace_lookup_ip(hash->filter_hash, ip, true) &&
	       !ftrace_lookup_ip(hash->notrace_hash, ip, false);

That would probably work better.

Want to send that update?

Yes, let me update it with your idea. Thanks!

Quoted 20 lines

-- Steve

Quoted 16 lines
kernel/trace/ftrace.c +1 -5
 }
 
 /*
@@ -6036,11 +6035,7 @@ clear_func_from_hash(struct ftrace_init_func
 *func, struct ftrace_hash *hash) {
 	struct ftrace_func_entry *entry;
 
-	if (ftrace_hash_empty(hash))
-		return;
-
-	entry = __ftrace_lookup_ip(hash, func->ip);
-
+	entry = ftrace_lookup_ip(hash, func->ip);
 	/*
 	 * Do not allow this rec to match again.
 	 * Yeah, it may waste some memory, but will be removed
Signature · 2 lines
Cheers,
Changbin Du
#3 lore raw
In reply to Steven Rostedt
Quoted 61 lines · Steven Rostedt
Quoted 16 lines · On Mon, 9 Sep 2019 08:31:59 +0800 Changbin Du <[email protected]> wrote:

Function ftrace_lookup_ip() will check empty hash table. So we don't need extra check outside.

---
v2: fix incorrect code remove.

---
 kernel/trace/ftrace.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +0 -0
index f9821a3374e9..92aab854d3b1 100644

kernel/trace/ftrace.c static bool hash_contains_ip(unsigned long ip, · line 1466

Quoted 6 lines
kernel/trace/ftrace.c +1 -2
@@ -1463,8 +1463,7 @@ static bool hash_contains_ip(unsigned long ip,
 	 */
 	return (ftrace_hash_empty(hash->filter_hash) ||
 		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
-		(ftrace_hash_empty(hash->notrace_hash) ||
-		 !__ftrace_lookup_ip(hash->notrace_hash, ip));
+	       !ftrace_lookup_ip(hash->notrace_hash, ip);

I don't care for this part. I've nacked this change in the past. Why? let's compare the changes:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		(ftrace_hash_empty(hash->notrace_hash) ||
		 !__ftrace_lookup_ip(hash->notrace_hash, ip));

vs:

	return (ftrace_hash_empty(hash->filter_hash) ||
		__ftrace_lookup_ip(hash->filter_hash, ip)) &&
		!ftrace_lookup_ip(hash->notrace_hash, ip);

The issue I have with this is that it abstracts out the difference between the filter_hash and the notrace_hash. Sometimes open coded works better if it is compared to something that is similar.

The current code I see:

Return true if (filter_hash is empty or ip exists in filter_hash
and notrace_hash is empty or it does not exist in notrace_hash

With your update I see:

Return true if filter_hash is empty or ip exists in filter_hash
and ip does not exist in notrace_hash

It makes it not easy to see if what happens if notrace_hash is empty

Hmm, come to think of it, perhaps we should change ftrace_lookup_ip() to include what to do on empty.

Maybe:

Quoted 8 lines

bool ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return __ftrace_lookup_ip(hash, ip);
}

We must add another similar function since ftrace_lookup_ip() returns a pointer.

bool ftrace_contains_ip(struct ftrace_hash *hash, unsigned long ip,
bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return !!__ftrace_lookup_ip(hash, ip);
}

But after this, it's a little overkill I think. It is not much simpler than before. Do you still want this then?

Quoted 29 lines

Then we can change the above to:

	return ftrace_lookup_ip(hash->filter_hash, ip, true) &&
	       !ftrace_lookup_ip(hash->notrace_hash, ip, false);

That would probably work better.

Want to send that update?

-- Steve

Quoted 16 lines
kernel/trace/ftrace.c +1 -5
 }
 
 /*
@@ -6036,11 +6035,7 @@ clear_func_from_hash(struct ftrace_init_func
 *func, struct ftrace_hash *hash) {
 	struct ftrace_func_entry *entry;
 
-	if (ftrace_hash_empty(hash))
-		return;
-
-	entry = __ftrace_lookup_ip(hash, func->ip);
-
+	entry = ftrace_lookup_ip(hash, func->ip);
 	/*
 	 * Do not allow this rec to match again.
 	 * Yeah, it may waste some memory, but will be removed
Signature · 2 lines
Cheers,
Changbin Du
maintainer
#4 lore raw
Quoted 20 lines · On Tue, 10 Sep 2019 08:33:23 +0800 Changbin Du <[email protected]> wrote:
Quoted 9 lines

bool ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return __ftrace_lookup_ip(hash, ip);
}

We must add another similar function since ftrace_lookup_ip() returns a pointer.

bool ftrace_contains_ip(struct ftrace_hash *hash, unsigned long ip,
bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return !!__ftrace_lookup_ip(hash, ip);
}
Quoted 4 lines

But after this, it's a little overkill I think. It is not much simpler than before. Do you still want this then?

Or...

static struct ftrace_func_entry empty_func_entry;
#define EMPTY_FUNC_ENTRY = &empty_func_entry;

[..]
* @empty_result: return NULL if false or EMPTY_FUNC_ENTRY on true
[..]
* @empty_result should be false, unless this is used for testing if the ip
* exists in the hash, and an empty hash should be considered true.
* This is useful when the empty hash is considered to contain all addresses.
[..]
struct ftrace_func_entry *
ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result ? EMPTY_FUNC_ENTRY : NULL;

	return __ftrace_lookup_ip(hash, ip);
}

But looking at this more, I'm going back to not touching the code in this location, because __ftrace_lookup_ip() is static, where as ftrace_lookup_ip() is not, and this is in a very fast path, and I rather keep it open coded.

Lets just drop the first hunk of your patch. The second hunk is fine.

-- Steve

#5 lore raw
Quoted 49 lines · Steven Rostedt
Quoted 20 lines · On Tue, 10 Sep 2019 08:33:23 +0800 Changbin Du <[email protected]> wrote:
Quoted 9 lines

bool ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return __ftrace_lookup_ip(hash, ip);
}

We must add another similar function since ftrace_lookup_ip() returns a pointer.

bool ftrace_contains_ip(struct ftrace_hash *hash, unsigned long ip,
bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result;

	return !!__ftrace_lookup_ip(hash, ip);
}
Quoted 4 lines

But after this, it's a little overkill I think. It is not much simpler than before. Do you still want this then?

Or...

static struct ftrace_func_entry empty_func_entry;
#define EMPTY_FUNC_ENTRY = &empty_func_entry;

[..]
* @empty_result: return NULL if false or EMPTY_FUNC_ENTRY on true
[..]
* @empty_result should be false, unless this is used for testing if the ip
* exists in the hash, and an empty hash should be considered true.
* This is useful when the empty hash is considered to contain all addresses.
[..]
struct ftrace_func_entry *
ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip, bool empty_result)

{
	if (ftrace_hash_empty(hash))
		return empty_result ? EMPTY_FUNC_ENTRY : NULL;

	return __ftrace_lookup_ip(hash, ip);
}
Quoted 7 lines

But looking at this more, I'm going back to not touching the code in this location, because __ftrace_lookup_ip() is static, where as ftrace_lookup_ip() is not, and this is in a very fast path, and I rather keep it open coded.

Lets just drop the first hunk of your patch. The second hunk is fine.

Sure, I will send a update short later. Thanks for your suggestions.

Quoted 2 lines

-- Steve

Signature · 2 lines
Cheers,
Changbin Du