2023-10-01 01:19:12

by Joel Fernandes

[permalink] [raw]
Subject: [PATCH] rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

From: Catalin Marinas <[email protected]>

Since the actual slab freeing is deferred when calling kvfree_rcu(), so
is the kmemleak_free() callback informing kmemleak of the object
deletion. From the perspective of the kvfree_rcu() caller, the object is
freed and it may remove any references to it. Since kmemleak does not
scan RCU internal data storing the pointer, it will report such objects
as leaks during the grace period.

Tell kmemleak to ignore such objects on the kvfree_call_rcu() path. Note
that the tiny RCU implementation does not have such issue since the
objects can be tracked from the rcu_ctrlblk structure.

Signed-off-by: Catalin Marinas <[email protected]>
Reported-by: Christoph Paasch <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]/
Cc: <[email protected]>
Tested-by: Christoph Paasch <[email protected]>
Signed-off-by: Joel Fernandes (Google) <[email protected]>
---
kernel/rcu/tree.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index cb1caefa8bd0..24423877962c 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -31,6 +31,7 @@
#include <linux/bitops.h>
#include <linux/export.h>
#include <linux/completion.h>
+#include <linux/kmemleak.h>
#include <linux/moduleparam.h>
#include <linux/panic.h>
#include <linux/panic_notifier.h>
@@ -3388,6 +3389,14 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
success = true;
}

+ /*
+ * 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);
--
2.42.0.582.g8ccd20d70d-goog


2023-10-02 20:21:46

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH] rcu: kmemleak: Ignore kmemleak false positives when RCU-freeing objects

On Sat, Sep 30, 2023 at 05:46:56PM +0000, Joel Fernandes (Google) wrote:
> From: Catalin Marinas <[email protected]>
>
> Since the actual slab freeing is deferred when calling kvfree_rcu(), so
> is the kmemleak_free() callback informing kmemleak of the object
> deletion. From the perspective of the kvfree_rcu() caller, the object is
> freed and it may remove any references to it. Since kmemleak does not
> scan RCU internal data storing the pointer, it will report such objects
> as leaks during the grace period.
>
> Tell kmemleak to ignore such objects on the kvfree_call_rcu() path. Note
> that the tiny RCU implementation does not have such issue since the
> objects can be tracked from the rcu_ctrlblk structure.
>
> Signed-off-by: Catalin Marinas <[email protected]>
> Reported-by: Christoph Paasch <[email protected]>
> Closes: https://lore.kernel.org/all/[email protected]/
> Cc: <[email protected]>
> Tested-by: Christoph Paasch <[email protected]>
> Signed-off-by: Joel Fernandes (Google) <[email protected]>

Reviewed-by: Paul E. McKenney <[email protected]>

> ---
> kernel/rcu/tree.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
> index cb1caefa8bd0..24423877962c 100644
> --- a/kernel/rcu/tree.c
> +++ b/kernel/rcu/tree.c
> @@ -31,6 +31,7 @@
> #include <linux/bitops.h>
> #include <linux/export.h>
> #include <linux/completion.h>
> +#include <linux/kmemleak.h>
> #include <linux/moduleparam.h>
> #include <linux/panic.h>
> #include <linux/panic_notifier.h>
> @@ -3388,6 +3389,14 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
> success = true;
> }
>
> + /*
> + * 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);
> --
> 2.42.0.582.g8ccd20d70d-goog
>