Skip to contentSkip to conversation

TCP MD5 vs kmemleak

TEST Jakub Kicinski 6 participants 14 messages Jun 17, 2024 – Jul 8, 2024

Subsystems (5)
lore
Share & tools
lore
#1 lore raw

Hi Jakub,

thanks for pinging,

Quoted 10 lines · Jakub Kicinski

Hi Dmitry!

We added kmemleak checks to the selftest runners, TCP AO/MD5 tests seem to trip it:

https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/643761/4-unsigned-md5-ipv6/stdout

Could you take a look? kmemleak is not infallible, it could be a false positive.

Sure, that seems somewhat interesting, albeit at this moment not from the TCP side :D

There is some pre-history to the related issue here:
https://lore.kernel.org/lkml/[email protected]/

Which I was quite sure being addressed with what now is
https://git.kernel.org/linus/5f98fd034ca6

But now that I look at that commit, I see that kvfree_call_rcu() is defined to __kvfree_call_rcu() under CONFIG_KASAN_GENERIC=n. And I don't see the same kmemleak_ignore() on that path.

To double-check, you don't have kasan enabled on netdev runners, right?

And then straight away to another thought: the leak-report that you get currently is ao_info, which should not happen if kfree_rcu() is properly fixed.
But I'd expect kmemleak to be unhappy with ao keys freeing as well:
they are currently released with call_rcu(&key->rcu, tcp_ao_key_free_rcu), which doesn't have a hint for kmemleak, too.

I'm going to take a look at it this week. Just to let you know, I'm also looking at fixing those somewhat rare flakes on tcp-ao counters checks (but that may be net-next material together with tracepoint selftests).

Thanks,
Dmitry

author maintainer
#2 lore raw
Quoted 22 lines · On Tue, 18 Jun 2024 04:24:08 +0100 Dmitry Safonov wrote:

Hi Jakub,

thanks for pinging,

Quoted 10 lines · Jakub Kicinski

Hi Dmitry!

We added kmemleak checks to the selftest runners, TCP AO/MD5 tests seem to trip it:

https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/643761/4-unsigned-md5-ipv6/stdout

Could you take a look? kmemleak is not infallible, it could be a false positive.

Sure, that seems somewhat interesting, albeit at this moment not from the TCP side :D

There is some pre-history to the related issue here:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Which I was quite sure being addressed with what now is
https://git.kernel.org/linus/5f98fd034ca6

But now that I look at that commit, I see that kvfree_call_rcu() is defined to __kvfree_call_rcu() under CONFIG_KASAN_GENERIC=n. And I don't see the same kmemleak_ignore() on that path.

To double-check, you don't have kasan enabled on netdev runners, right?

We do:

CONFIG_KASAN=y
CONFIG_KASAN_GENERIC=y

here's the full config:
https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/645202/config

Quoted 11 lines

And then straight away to another thought: the leak-report that you get currently is ao_info, which should not happen if kfree_rcu() is properly fixed.
But I'd expect kmemleak to be unhappy with ao keys freeing as well:
they are currently released with call_rcu(&key->rcu, tcp_ao_key_free_rcu), which doesn't have a hint for kmemleak, too.

I'm going to take a look at it this week. Just to let you know, I'm also looking at fixing those somewhat rare flakes on tcp-ao counters checks (but that may be net-next material together with tracepoint selftests).

Let me add rcu@ to CC, perhaps folks there can guide us on known false positives with KASAN + kmemleak?

maintainer
#3 lore raw
Quoted 47 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 18 Jun 2024 04:24:08 +0100 Dmitry Safonov wrote:

Hi Jakub,

thanks for pinging,

Quoted 10 lines · Jakub Kicinski

Hi Dmitry!

We added kmemleak checks to the selftest runners, TCP AO/MD5 tests seem to trip it:

https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/643761/4-unsigned-md5-ipv6/stdout

Could you take a look? kmemleak is not infallible, it could be a false positive.

Sure, that seems somewhat interesting, albeit at this moment not from the TCP side :D

There is some pre-history to the related issue here:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Which I was quite sure being addressed with what now is
https://git.kernel.org/linus/5f98fd034ca6

But now that I look at that commit, I see that kvfree_call_rcu() is defined to __kvfree_call_rcu() under CONFIG_KASAN_GENERIC=n. And I don't see the same kmemleak_ignore() on that path.

To double-check, you don't have kasan enabled on netdev runners, right?

We do:

CONFIG_KASAN=y
CONFIG_KASAN_GENERIC=y

here's the full config:
https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/645202/config

Quoted 7 lines

And then straight away to another thought: the leak-report that you get currently is ao_info, which should not happen if kfree_rcu() is properly fixed.
But I'd expect kmemleak to be unhappy with ao keys freeing as well:
they are currently released with call_rcu(&key->rcu, tcp_ao_key_free_rcu), which doesn't have a hint for kmemleak, too.

Quoted 7 lines
Quoted 4 lines

I'm going to take a look at it this week. Just to let you know, I'm also looking at fixing those somewhat rare flakes on tcp-ao counters checks (but that may be net-next material together with tracepoint selftests).

Let me add rcu@ to CC, perhaps folks there can guide us on known false positives with KASAN + kmemleak?

I myself don't run KASAN with kmemleak, but maybe some of the others on this list do.

What sort of hint should be we add to call_rcu()? The memory is still reachable in the garbage-collector sense.

Thanx, Paul

maintainer
#4 lore raw
In reply to Jakub Kicinski
Quoted 47 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 18 Jun 2024 04:24:08 +0100 Dmitry Safonov wrote:

Hi Jakub,

thanks for pinging,

Quoted 10 lines · Jakub Kicinski

Hi Dmitry!

We added kmemleak checks to the selftest runners, TCP AO/MD5 tests seem to trip it:

https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/643761/4-unsigned-md5-ipv6/stdout

Could you take a look? kmemleak is not infallible, it could be a false positive.

Sure, that seems somewhat interesting, albeit at this moment not from the TCP side :D

There is some pre-history to the related issue here:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Which I was quite sure being addressed with what now is
https://git.kernel.org/linus/5f98fd034ca6

But now that I look at that commit, I see that kvfree_call_rcu() is defined to __kvfree_call_rcu() under CONFIG_KASAN_GENERIC=n. And I don't see the same kmemleak_ignore() on that path.

To double-check, you don't have kasan enabled on netdev runners, right?

We do:

CONFIG_KASAN=y
CONFIG_KASAN_GENERIC=y

here's the full config:
https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/645202/config

Quoted 7 lines

And then straight away to another thought: the leak-report that you get currently is ao_info, which should not happen if kfree_rcu() is properly fixed.
But I'd expect kmemleak to be unhappy with ao keys freeing as well:
they are currently released with call_rcu(&key->rcu, tcp_ao_key_free_rcu), which doesn't have a hint for kmemleak, too.

Quoted 7 lines
Quoted 4 lines

I'm going to take a look at it this week. Just to let you know, I'm also looking at fixing those somewhat rare flakes on tcp-ao counters checks (but that may be net-next material together with tracepoint selftests).

Let me add rcu@ to CC, perhaps folks there can guide us on known false positives with KASAN + kmemleak?

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Cheers,

Paolo

maintainer
#5 lore raw
Quoted 59 lines · Paolo Abeni
Quoted 47 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 18 Jun 2024 04:24:08 +0100 Dmitry Safonov wrote:

Hi Jakub,

thanks for pinging,

Quoted 10 lines · Jakub Kicinski

Hi Dmitry!

We added kmemleak checks to the selftest runners, TCP AO/MD5 tests seem to trip it:

https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/643761/4-unsigned-md5-ipv6/stdout

Could you take a look? kmemleak is not infallible, it could be a false positive.

Sure, that seems somewhat interesting, albeit at this moment not from the TCP side :D

There is some pre-history to the related issue here:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Which I was quite sure being addressed with what now is
https://git.kernel.org/linus/5f98fd034ca6

But now that I look at that commit, I see that kvfree_call_rcu() is defined to __kvfree_call_rcu() under CONFIG_KASAN_GENERIC=n. And I don't see the same kmemleak_ignore() on that path.

To double-check, you don't have kasan enabled on netdev runners, right?

We do:

CONFIG_KASAN=y
CONFIG_KASAN_GENERIC=y

here's the full config:
https://netdev-3.bots.linux.dev/vmksft-tcp-ao-dbg/results/645202/config

Quoted 7 lines

And then straight away to another thought: the leak-report that you get currently is ao_info, which should not happen if kfree_rcu() is properly fixed.
But I'd expect kmemleak to be unhappy with ao keys freeing as well:
they are currently released with call_rcu(&key->rcu, tcp_ao_key_free_rcu), which doesn't have a hint for kmemleak, too.

Quoted 7 lines
Quoted 4 lines

I'm going to take a look at it this week. Just to let you know, I'm also looking at fixing those somewhat rare flakes on tcp-ao counters checks (but that may be net-next material together with tracepoint selftests).

Let me add rcu@ to CC, perhaps folks there can guide us on known false positives with KASAN + kmemleak?

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

Quoted 8 lines

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

There are similar challenges with synchronize_rcu() and friends.

Thanx, Paul

author maintainer
#6 lore raw
Quoted 11 lines · On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:
Quoted 11 lines

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

Quoted 9 lines

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

There are similar challenges with synchronize_rcu() and friends.

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

maintainer
#7 lore raw
Quoted 25 lines · Jakub Kicinski
Quoted 11 lines · On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:
Quoted 11 lines

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

Quoted 8 lines

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

Quoted 6 lines
Quoted 1 line

There are similar challenges with synchronize_rcu() and friends.

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

In the kfree_rcu() there is "an ignore" injection:

<snip>

	/*
	 * The kvfree_rcu() caller considers the pointer freed at this point
	 * and likely removes any references to it. Since the actual slab
	 * freeing (and kmemleak_free()) is deferred, tell kmemleak to ignore
	 * this object (no scanning or false positives reporting).
	 */
	kmemleak_ignore(ptr);

	// Set timer to drain after KFREE_DRAIN_JIFFIES.
	if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING)
		schedule_delayed_monitor_work(krcp);

<snip>

--
Uladzislau Rezki

maintainer
#8 lore raw
In reply to Jakub Kicinski
Quoted 25 lines · Jakub Kicinski
Quoted 11 lines · On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:
Quoted 11 lines

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

Quoted 8 lines

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

Quoted 5 lines
Quoted 1 line

There are similar challenges with synchronize_rcu() and friends.

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

Whew!!! ;-)

Thanx, Paul

#9 lore raw
Quoted 3 lines · Paul E. McKenney
Quoted 1 line · Jakub Kicinski

On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:

[..]

Quoted 4 lines
Quoted 4 lines

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

[..]

Quoted 6 lines
Quoted 4 lines

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

Whew!!! ;-)

I'm sorry guys, that was me being inadequate. That's a real issue, rather than a false-positive:
https://lore.kernel.org/netdev/[email protected]/

Thanks,
Dmitry

maintainer
#10 lore raw
Quoted 11 lines · Dmitry Safonov
Quoted 3 lines · Paul E. McKenney
Quoted 1 line · Jakub Kicinski

On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:

[..]

Quoted 4 lines
Quoted 4 lines

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

[..]

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

Whew!!! ;-)

I'm sorry guys, that was me being inadequate.

I know that feeling!

Quoted 2 lines

That's a real issue, rather than a false-positive:
https://lore.kernel.org/netdev/[email protected]/

So we need call_rcu() to mark memory flowing through it? If so, we need help from callers of call_rcu() in the case where more than one object is being freed.

Thanx, Paul

#11 lore raw

Hi Paul,

Sorry for the delayed answer, I got a respiratory infection, so was in bed last week,

Quoted 2 lines · Paul E. McKenney

On Wed, Jun 19, 2024 at 01:33:36AM +0100, Dmitry Safonov wrote:

[..]

Quoted 3 lines
Quoted 1 line

I'm sorry guys, that was me being inadequate.

I know that feeling!

Thanks :)

[adding/quoting back the context of the thread that I cut previously]

Quoted 31 lines · Jakub Kicinski
Quoted 30 lines · On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:
Quoted 11 lines
Quoted 11 lines

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

Quoted 9 lines

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

There are similar challenges with synchronize_rcu() and friends.

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

Quoted 6 lines · Paul E. McKenney
Quoted 2 lines

That's a real issue, rather than a false-positive:
https://lore.kernel.org/netdev/[email protected]/

So we need call_rcu() to mark memory flowing through it? If so, we need help from callers of call_rcu() in the case where more than one object is being freed.

Not sure, I think one way to avoid explicitly marking pointers for call_rcu() or even avoiding the patch above would be a hackery like this:

mm/kmemleak.c +0 -0
index d5b6fba44fc9..7a5eb55a155c 100644
@@ -1587,6 +1587,7 @@ static void kmemleak_cond_resched(struct

kmemleak_object *object)
static void kmemleak_scan(void)

 {
        struct kmemleak_object *object;
+       unsigned long gp_start_state;
        struct zone *zone;
        int __maybe_unused i;
        int new_leaks = 0;
@@ -1630,6 +1631,7 @@ static void kmemleak_scan(void)
                        kmemleak_cond_resched(object);
        }
        rcu_read_unlock();

+ gp_start_state = get_state_synchronize_rcu();

 #ifdef CONFIG_SMP
        /* per-cpu sections scanning */
@@ -1690,6 +1692,13 @@ static void kmemleak_scan(void)
         */
        scan_gray_list();

+ /*

+        * Wait for the greylist objects potentially migrating from
+        * RCU callbacks or maybe getting freed.
+        */

+ cond_synchronize_rcu(gp_start_state);
+ rcu_barrier();
+
/*
* Check for new or unreferenced objects modified since the previous
* scan and color them gray until the next scan.

-->8--

Not quite sure if this makes sense, my first time at kmemleak code, adding Catalin.
But then if I didn't mess up, it's going to work only for one RCU period, so in case some object calls rcu/kfree_rcu() from the callback, it's going to be yet a false-positive.

Some other options/ideas:
- more RCU-invasive option which would be adding a mapping function to segmented lists of callbacks, which would allow kmemleak to request from RCU if the pointer is yet referenced by RCU.
- the third option is for kmemleak to trust RCU and generalize the commit above, adding kmemleak_object::flags of OBJECT_RCU, which will be set by call_rcu()/kfree_rcu() and unset once the callback is invoked for RCU_DONE_TAIL.
- add kmemleak_object::update_jiffies or just directly touch kmemleak_object::jiffies whenever the object has been modified (see update_checksum()), that will ignore recently changed objects. As rcu_head should be updated, that is going to automagically ignore those deferred to RCU objects.

Not sure if I mis-looked anything and it seems there is no hurry in fixing anything yet, as this is a theoretical issue at this moment.

Hopefully, these ideas don't look like a nonsense,
Dmitry

maintainer
#12 lore raw
Quoted 48 lines · Dmitry Safonov
Quoted 3 lines · Paul E. McKenney

So we need call_rcu() to mark memory flowing through it? If so, we need help from callers of call_rcu() in the case where more than one object is being freed.

Not sure, I think one way to avoid explicitly marking pointers for call_rcu() or even avoiding the patch above would be a hackery like this:

mm/kmemleak.c +1 -0
index d5b6fba44fc9..7a5eb55a155c 100644
@@ -1587,6 +1587,7 @@ static void kmemleak_cond_resched(struct
 kmemleak_object *object)
 static void kmemleak_scan(void)
 {
        struct kmemleak_object *object;
+       unsigned long gp_start_state;
        struct zone *zone;
        int __maybe_unused i;
        int new_leaks = 0;
mm/kmemleak.c +8 -1
@@ -1630,6 +1631,7 @@ static void kmemleak_scan(void)
                        kmemleak_cond_resched(object);
        }
        rcu_read_unlock();
+       gp_start_state = get_state_synchronize_rcu();
 
 #ifdef CONFIG_SMP
        /* per-cpu sections scanning */
@@ -1690,6 +1692,13 @@ static void kmemleak_scan(void)
         */
        scan_gray_list();
 
+       /*
+        * Wait for the greylist objects potentially migrating from
+        * RCU callbacks or maybe getting freed.
+        */
+       cond_synchronize_rcu(gp_start_state);
+       rcu_barrier();
+
        /*
         * Check for new or unreferenced objects modified since the previous
         * scan and color them gray until the next scan.
 
 
-->8--
Quoted 5 lines

Not quite sure if this makes sense, my first time at kmemleak code, adding Catalin.
But then if I didn't mess up, it's going to work only for one RCU period, so in case some object calls rcu/kfree_rcu() from the callback, it's going to be yet a false-positive.

Yeah, I don't think this fully solves the problem.

Quoted 4 lines

Some other options/ideas:
- more RCU-invasive option which would be adding a mapping function to segmented lists of callbacks, which would allow kmemleak to request from RCU if the pointer is yet referenced by RCU.

This looks too invasive to me plus additional scanning cost.

Quoted 4 lines

- the third option is for kmemleak to trust RCU and generalize the commit above, adding kmemleak_object::flags of OBJECT_RCU, which will be set by call_rcu()/kfree_rcu() and unset once the callback is invoked for RCU_DONE_TAIL.

This could work, the downside being a kmemleak object lookup every time call_rcu() is invoked. Well, we do this now for kfree_rcu() already, though we just mark the object as ignored once.

Quoted 5 lines

- add kmemleak_object::update_jiffies or just directly touch kmemleak_object::jiffies whenever the object has been modified (see update_checksum()), that will ignore recently changed objects. As rcu_head should be updated, that is going to automagically ignore those deferred to RCU objects.

This looks the simplest, reset the jiffies every time it detected the object being touched. However, I wonder whether we should introduce a new variable to track this. 'jiffies' currently stores the creation time (or thereabouts). We can change the 'age' reported in the sysfs as long as no user script parses that.

I added the min age to kmemleak to avoid the early object being added to some lists and getting reported as false positive. It looks like we have a similar scenario when the object is getting freed: it is added to RCU lists, disappears temporarily from kmemleak's view. So it does make sense to employ similar delay as for creation.

That said, for the default kmemleak operation with scanning every 10min, this wouldn't be needed. During one scan it detects the checksum changed due to the rcu_head update. 10min later it should have been freed already.

Signature · 1 line
Catalin
maintainer
#13 lore raw
In reply to Dmitry Safonov
Quoted 83 lines · Dmitry Safonov

Hi Paul,

Sorry for the delayed answer, I got a respiratory infection, so was in bed last week,

Quoted 2 lines · Paul E. McKenney

On Wed, Jun 19, 2024 at 01:33:36AM +0100, Dmitry Safonov wrote:

[..]

Quoted 3 lines
Quoted 1 line

I'm sorry guys, that was me being inadequate.

I know that feeling!

Thanks :)

[adding/quoting back the context of the thread that I cut previously]

Quoted 31 lines · Jakub Kicinski
Quoted 30 lines · On Tue, 18 Jun 2024 09:42:35 -0700 Paul E. McKenney wrote:
Quoted 11 lines
Quoted 11 lines

FTR, with mptcp self-tests we hit a few kmemleak false positive on RCU freed pointers, that where addressed by to this patch:

commit 5f98fd034ca6fd1ab8c91a3488968a0e9caaabf6

Author: Catalin Marinas <[email protected]>
Date:   Sat Sep 30 17:46:56 2023 +0000

rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

I'm wondering if this is hitting something similar? Possibly due to lazy RCU callbacks invoked after MSECS_MIN_AGE???

Dmitry mentioned this commit, too, but we use the same config for MPTCP tests, and while we repro TCP AO failures quite frequently, mptcp doesn't seem to have failed once.

Quoted 9 lines

Fun! ;-)

This commit handles memory passed to kfree_rcu() and friends, but not memory passed to call_rcu() and friends. Of course, call_rcu() does not necessarily know the full extent of the memory passed to it, for example, if passed a linked list, call_rcu() will know only about the head of that list.

There are similar challenges with synchronize_rcu() and friends.

To be clear I think Dmitry was suspecting kfree_rcu(), he mentioned call_rcu() as something he was expecting to have a similar issue but it in fact appeared immune.

Quoted 6 lines · Paul E. McKenney
Quoted 2 lines

That's a real issue, rather than a false-positive:
https://lore.kernel.org/netdev/[email protected]/

So we need call_rcu() to mark memory flowing through it? If so, we need help from callers of call_rcu() in the case where more than one object is being freed.

Not sure, I think one way to avoid explicitly marking pointers for call_rcu() or even avoiding the patch above would be a hackery like this:

mm/kmemleak.c +1 -0
index d5b6fba44fc9..7a5eb55a155c 100644
@@ -1587,6 +1587,7 @@ static void kmemleak_cond_resched(struct
 kmemleak_object *object)
 static void kmemleak_scan(void)
 {
        struct kmemleak_object *object;
+       unsigned long gp_start_state;
        struct zone *zone;
        int __maybe_unused i;
        int new_leaks = 0;
mm/kmemleak.c +1 -0
@@ -1630,6 +1631,7 @@ static void kmemleak_scan(void)
                        kmemleak_cond_resched(object);
        }
        rcu_read_unlock();
+       gp_start_state = get_state_synchronize_rcu();
 
 #ifdef CONFIG_SMP
        /* per-cpu sections scanning */

mm/kmemleak.c static void kmemleak_scan(void) · line 1700

Quoted 9 lines
mm/kmemleak.c +6 -0
@@ -1690,6 +1692,13 @@ static void kmemleak_scan(void)
         */
        scan_gray_list();
 
+       /*
+        * Wait for the greylist objects potentially migrating from
+        * RCU callbacks or maybe getting freed.
+        */
+       cond_synchronize_rcu(gp_start_state);
+       rcu_barrier();

You lost me on this one. If you are waiting only on RCU callbacks, the rcu_barrier() suffices. If you are also waiting on objects to be freed after a synchronize_rcu(), kfree_rcu() or similar, you are waiting only for the corresponding grace period to end, not necessarily for the freeing of objects following that synchronize_rcu().

So what exactly needs to be waited upon?

I should hasten to add that the fact that there is currently no way to wait on kfree_rcu()'s frees is considered to be a bug, but that bug is currently only known to be a problem in the case of a module that uses kfree_rcu() on objects obtained from a kmem_cache structure, and that also passes that kmem_cache structure to kmem_cache_free() at module-unload time.

In the meantime, the workaround is to use call_rcu() instead of kfree_rcu() in that case. Which existing code does.

Quoted 13 lines
mm/kmemleak.c +0 -1
        /*
         * Check for new or unreferenced objects modified since the previous
         * scan and color them gray until the next scan.
 
 
-->8--

Not quite sure if this makes sense, my first time at kmemleak code, adding Catalin.
But then if I didn't mess up, it's going to work only for one RCU period, so in case some object calls rcu/kfree_rcu() from the callback, it's going to be yet a false-positive.

Quoted 13 lines

Some other options/ideas:
- more RCU-invasive option which would be adding a mapping function to segmented lists of callbacks, which would allow kmemleak to request from RCU if the pointer is yet referenced by RCU.
- the third option is for kmemleak to trust RCU and generalize the commit above, adding kmemleak_object::flags of OBJECT_RCU, which will be set by call_rcu()/kfree_rcu() and unset once the callback is invoked for RCU_DONE_TAIL.
- add kmemleak_object::update_jiffies or just directly touch kmemleak_object::jiffies whenever the object has been modified (see update_checksum()), that will ignore recently changed objects. As rcu_head should be updated, that is going to automagically ignore those deferred to RCU objects.

You could also make greater use of the polled RCU API, marking an object (or, perhaps more space-efficiently, a group of objects) using get_state_synchronize_rcu_full(), then using poll_state_synchronize_rcu_full() to see when all readers should be done. There are additional tricks that can be used if needed.

Thanx, Paul

Quoted 5 lines

Not sure if I mis-looked anything and it seems there is no hurry in fixing anything yet, as this is a theoretical issue at this moment.

Hopefully, these ideas don't look like a nonsense,
Dmitry