From: "Joel Fernandes (Google)" <joel@joelfernandes.org>
To: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, rushikesh.s.kadam@intel.com,
urezki@gmail.com, neeraj.iitr10@gmail.com, frederic@kernel.org,
paulmck@kernel.org, rostedt@goodmis.org, youssefesmat@google.com,
surenb@google.com,
"Joel Fernandes (Google)" <joel@joelfernandes.org>
Subject: [PATCH v8 13/13] rcu/debug: Add wake-up debugging for lazy callbacks
Date: Tue, 11 Oct 2022 18:01:42 +0000 [thread overview]
Message-ID: <20221011180142.2742289-14-joel@joelfernandes.org> (raw)
In-Reply-To: <20221011180142.2742289-1-joel@joelfernandes.org>
This patch adds initial debugging for lazy callback: whether the
callback does a wake up or not. We see that callbacks doing wake ups are
usually associated with synchronous use cases (SCSI, rcu_sync,
synchronize_rcu() etc).
The code is not very intrusive as almost all the logic is in
'lazy-debug.h' with just a few calls from tree.c
In the future, we will add more functionality such as ensuring
callbacks execute in bounded time.
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
kernel/rcu/Kconfig | 7 ++
kernel/rcu/lazy-debug.h | 154 ++++++++++++++++++++++++++++++++++++++++
kernel/rcu/tree.c | 9 +++
3 files changed, 170 insertions(+)
create mode 100644 kernel/rcu/lazy-debug.h
diff --git a/kernel/rcu/Kconfig b/kernel/rcu/Kconfig
index edd632e68497..08c06f739187 100644
--- a/kernel/rcu/Kconfig
+++ b/kernel/rcu/Kconfig
@@ -322,4 +322,11 @@ config RCU_LAZY
To save power, batch RCU callbacks and flush after delay, memory
pressure or callback list growing too big.
+config RCU_LAZY_DEBUG
+ bool "RCU callback lazy invocation debugging"
+ depends on RCU_LAZY
+ default n
+ help
+ Debugging to catch issues caused by delayed RCU callbacks.
+
endmenu # "RCU Subsystem"
diff --git a/kernel/rcu/lazy-debug.h b/kernel/rcu/lazy-debug.h
new file mode 100644
index 000000000000..b8399b51d06a
--- /dev/null
+++ b/kernel/rcu/lazy-debug.h
@@ -0,0 +1,154 @@
+#include <linux/string.h>
+#include <linux/spinlock.h>
+
+#ifdef CONFIG_RCU_LAZY_DEBUG
+#include <linux/preempt.h>
+#include <trace/events/sched.h>
+
+static DEFINE_PER_CPU(bool, rcu_lazy_cb_exec) = false;
+static DEFINE_PER_CPU(void *, rcu_lazy_ip) = NULL;
+
+static DEFINE_RAW_SPINLOCK(lazy_funcs_lock);
+
+#define FUNC_SIZE 1024
+static unsigned long lazy_funcs[FUNC_SIZE];
+static int nr_funcs;
+
+static void __find_func(unsigned long ip, int *B, int *E, int *N)
+{
+ unsigned long *p;
+ int b, e, n;
+
+ b = n = 0;
+ e = nr_funcs - 1;
+
+ while (b <= e) {
+ n = (b + e) / 2;
+ p = &lazy_funcs[n];
+ if (ip > *p) {
+ b = n + 1;
+ } else if (ip < *p) {
+ e = n - 1;
+ } else
+ break;
+ }
+
+ *B = b;
+ *E = e;
+ *N = n;
+
+ return;
+}
+
+static bool lazy_func_exists(void* ip_ptr)
+{
+ int b, e, n;
+ unsigned long flags;
+ unsigned long ip = (unsigned long)ip_ptr;
+
+ raw_spin_lock_irqsave(&lazy_funcs_lock, flags);
+ __find_func(ip, &b, &e, &n);
+ raw_spin_unlock_irqrestore(&lazy_funcs_lock, flags);
+
+ return b <= e;
+}
+
+static int lazy_func_add(void* ip_ptr)
+{
+ int b, e, n;
+ unsigned long flags;
+ unsigned long ip = (unsigned long)ip_ptr;
+
+ raw_spin_lock_irqsave(&lazy_funcs_lock, flags);
+ if (nr_funcs >= FUNC_SIZE) {
+ raw_spin_unlock_irqrestore(&lazy_funcs_lock, flags);
+ return -1;
+ }
+
+ __find_func(ip, &b, &e, &n);
+
+ if (b > e) {
+ if (n != nr_funcs)
+ memmove(&lazy_funcs[n+1], &lazy_funcs[n],
+ (sizeof(*lazy_funcs) * (nr_funcs - n)));
+
+ lazy_funcs[n] = ip;
+ nr_funcs++;
+ }
+
+ raw_spin_unlock_irqrestore(&lazy_funcs_lock, flags);
+ return 0;
+}
+
+static void rcu_set_lazy_context(void *ip_ptr)
+{
+ bool *flag = this_cpu_ptr(&rcu_lazy_cb_exec);
+ *flag = lazy_func_exists(ip_ptr);
+
+ if (*flag) {
+ *this_cpu_ptr(&rcu_lazy_ip) = ip_ptr;
+ } else {
+ *this_cpu_ptr(&rcu_lazy_ip) = NULL;
+ }
+}
+
+static void rcu_reset_lazy_context(void)
+{
+ bool *flag = this_cpu_ptr(&rcu_lazy_cb_exec);
+ *flag = false;
+}
+
+static bool rcu_is_lazy_context(void)
+{
+ return *(this_cpu_ptr(&rcu_lazy_cb_exec));
+}
+
+static void
+probe_waking(void *ignore, struct task_struct *p)
+{
+ // kworker wake ups don't appear to cause performance issues.
+ // Ignore for now.
+ if (!strncmp(p->comm, "kworker", 7))
+ return;
+
+ if (WARN_ON(!in_nmi() && !in_hardirq() && rcu_is_lazy_context())) {
+ pr_err("*****************************************************\n");
+ pr_err("RCU: A wake up has been detected from a lazy callback!\n");
+ pr_err("The callback name is: %ps\n", *this_cpu_ptr(&rcu_lazy_ip));
+ pr_err("The task it woke up is: %s (%d)\n", p->comm, p->pid);
+ pr_err("This could cause performance issues! Check the stack.\n");
+ pr_err("*****************************************************\n");
+ }
+}
+
+static void rcu_lazy_debug_init(void)
+{
+ int ret;
+ pr_info("RCU Lazy CB debugging is turned on, system may be slow.\n");
+
+ ret = register_trace_sched_waking(probe_waking, NULL);
+ if (ret)
+ pr_info("RCU: Lazy debug ched_waking probe could not be registered.");
+}
+
+#else
+
+static int lazy_func_add(void* ip_ptr)
+{
+ return -1;
+}
+
+
+static void rcu_set_lazy_context(void *ip_ptr)
+{
+}
+
+static void rcu_reset_lazy_context(void)
+{
+}
+
+static void rcu_lazy_debug_init(void)
+{
+}
+
+#endif
diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index 37fe6ebc113a..ac34e1ed3ab2 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -67,6 +67,7 @@
#include "tree.h"
#include "rcu.h"
+#include "lazy-debug.h"
#ifdef MODULE_PARAM_PREFIX
#undef MODULE_PARAM_PREFIX
@@ -2245,7 +2246,10 @@ static void rcu_do_batch(struct rcu_data *rdp)
f = rhp->func;
WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
+
+ rcu_set_lazy_context(f);
f(rhp);
+ rcu_reset_lazy_context();
rcu_lock_release(&rcu_callback_map);
@@ -2770,6 +2774,10 @@ __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy)
}
check_cb_ovld(rdp);
+
+ if (lazy)
+ lazy_func_add(func);
+
if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags, lazy))
return; // Enqueued onto ->nocb_bypass, so just leave.
// If no-CBs CPU gets here, rcu_nocb_try_bypass() acquired ->nocb_lock.
@@ -4800,6 +4808,7 @@ void __init rcu_init(void)
rcu_early_boot_tests();
kfree_rcu_batch_init();
+ rcu_lazy_debug_init();
rcu_bootup_announce();
sanitize_kthread_prio();
rcu_init_geometry();
--
2.38.0.rc1.362.ged0d419d3c-goog
prev parent reply other threads:[~2022-10-11 18:02 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-11 18:01 [PATCH v8 00/13] rcu: call_rcu() power improvements Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 01/13] rcu: Fix missing nocb gp wake on rcu_barrier() Joel Fernandes (Google)
2022-10-14 14:21 ` Paul E. McKenney
2022-10-14 14:40 ` Frederic Weisbecker
2022-10-14 15:03 ` Paul E. McKenney
2022-10-14 15:19 ` Joel Fernandes
2022-10-14 15:46 ` Paul E. McKenney
2022-10-14 20:47 ` Frederic Weisbecker
2022-10-16 15:16 ` Paul E. McKenney
2022-10-16 15:56 ` Joel Fernandes
2022-10-11 18:01 ` [PATCH v8 02/13] rcu: Make call_rcu() lazy to save power Joel Fernandes (Google)
2022-10-11 23:10 ` Frederic Weisbecker
2022-10-12 19:54 ` Joel Fernandes
2022-10-14 15:45 ` Paul E. McKenney
2022-10-11 18:01 ` [PATCH v8 03/13] rcu: Refactor code a bit in rcu_nocb_do_flush_bypass() Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 04/13] rcu: shrinker for lazy rcu Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 05/13] rcuscale: Add laziness and kfree tests Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 06/13] percpu-refcount: Use call_rcu_flush() for atomic switch Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 07/13] rcu/sync: Use call_rcu_flush() instead of call_rcu Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 08/13] rcu/rcuscale: Use call_rcu_flush() for async reader test Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 09/13] rcu/rcutorture: Use call_rcu_flush() where needed Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 10/13] scsi/scsi_error: Use call_rcu_flush() instead of call_rcu() Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 11/13] workqueue: Make queue_rcu_work() use call_rcu_flush() Joel Fernandes (Google)
2022-10-11 18:01 ` [PATCH v8 12/13] rxrpc: Use call_rcu_flush() instead of call_rcu() Joel Fernandes (Google)
2022-10-11 18:01 ` Joel Fernandes (Google) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221011180142.2742289-14-joel@joelfernandes.org \
--to=joel@joelfernandes.org \
--cc=frederic@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=neeraj.iitr10@gmail.com \
--cc=paulmck@kernel.org \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=rushikesh.s.kadam@intel.com \
--cc=surenb@google.com \
--cc=urezki@gmail.com \
--cc=youssefesmat@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.