Skip to contentSkip to conversation

Harden list_for_each_entry_rcu() and family

PATCH In review Joel Fernandes (Google) 3 participants 30 messages Jul 12, 2019 – Jul 16, 2019

Subsystems (13)
lore
Share & tools
lore
#0 lore raw

v2 cover Harden list_for_each_entry_rcu() and family

Hi,
This series aims to provide lockdep checking to RCU list macros for additional kernel hardening.

RCU has a number of primitives for "consumption" of an RCU protected pointer. Most of the time, these consumers make sure that such accesses are under a RCU reader-section (such as rcu_dereference{,sched,bh} or under a lock, such as with rcu_dereference_protected()).

However, there are other ways to consume RCU pointers, such as by list_for_each_entry_rcu or hlist_for_each_enry_rcu. Unlike the rcu_dereference family, these consumers do no lockdep checking at all. And with the growing number of RCU list uses (1000+), it is possible for bugs to creep in and go unnoticed which lockdep checks can catch.

Since RCU consolidation efforts last year, the different traditional RCU flavors (preempt, bh, sched) are all consolidated. In other words, any of these flavors can cause a reader section to occur and all of them must cease before the reader section is considered to be unlocked. Thanks to this, we can generically check if we are in an RCU reader. This is what patch 1 does. Note that the list_for_each_entry_rcu and family are different from the rcu_dereference family in that, there is no _bh or _sched version of this macro. They are used under many different RCU reader flavors, and also SRCU. Patch 1 adds a new internal function rcu_read_lock_any_held() which checks if any reader section is active at all, when these macros are called. If no reader section exists, then the optional fourth argument to list_for_each_entry_rcu() can be a lockdep expression which is evaluated (similar to how rcu_dereference_check() works). If no lockdep expression is passed, and we are not in a reader, then a splat occurs. Just take off the lockdep expression after applying the patches, by using the following diff and see what happens:

+++ b/arch/x86/pci/mmconfig-shared.c
@@ -55,7 +55,7 @@ static void list_add_sorted(struct pci_mmcfg_region *new)

        struct pci_mmcfg_region *cfg;

        /* keep list sorted by segment and starting bus number */

- list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held()) {
+ list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list) {

The optional argument trick to list_for_each_entry_rcu() can also be used in the future to possibly remove rcu_dereference_{,bh,sched}_protected() API and we can pass an optional lockdep expression to rcu_dereference() itself. Thus eliminating 3 more RCU APIs.

Note that some list macro wrappers already do their own lockdep checking in the caller side. These can be eliminated in favor of the built-in lockdep checking in the list macro that this series adds. For example, workqueue code has a assert_rcu_or_wq_mutex() function which is called in for_each_wq(). This series replaces that in favor of the built-in check.

Also in the future, we can extend these checks to list_entry_rcu() and other list macros as well, if needed.

Please note that I have kept this option default-disabled under a new config:
CONFIG_PROVE_RCU_LIST. This is so that until all users are converted to pass the optional argument, we should keep the check disabled. There are about a 1000 or so users and it is not possible to pass in the optional lockdep expression in a single series since it is done on a case-by-case basis. I did convert a few users in this series itself.

v1->v2: Have assert_rcu_or_wq_mutex deleted (Daniel Jordan)
Simplify rcu_read_lock_any_held() (Peter Zijlstra)
Simplified rcu-sync logic (Oleg Nesterov)
Updated documentation and rculist comments. Added GregKH ack.

RFC->v1:
Simplify list checking macro (Rasmus Villemoes)

Joel Fernandes (Google) (9):
rcu/update: Remove useless check for debug_locks
rcu: Add support for consolidated-RCU reader checking rcu/sync: Remove custom check for reader-section
ipv4: add lockdep condition to fix for_each_entry driver/core: Convert to use built-in RCU list checking
workqueue: Convert for_each_wq to use built-in list check x86/pci: Pass lockdep condition to pcm_mmcfg_list iterator
acpi: Use built-in RCU list checking for acpi_ioremaps list
doc: Update documentation about list_for_each_entry_rcu

Documentation/RCU/lockdep.txt   | 15 +++++++++++----
Documentation/RCU/whatisRCU.txt |  9 ++++++++-
arch/x86/pci/mmconfig-shared.c  |  5 +++--
drivers/acpi/osl.c              |  6 ++++--
drivers/base/base.h             |  1 +
drivers/base/core.c             | 10 ++++++++++
drivers/base/power/runtime.c    | 15 ++++++++++-----
include/linux/rcu_sync.h        |  5 ++---
include/linux/rculist.h         | 28 +++++++++++++++++++++++-----
include/linux/rcupdate.h        |  7 +++++++
kernel/rcu/Kconfig.debug        | 11 +++++++++++
kernel/rcu/sync.c               | 22 ----------------------
kernel/rcu/update.c             | 20 +++++++++++++++-----
kernel/workqueue.c              | 10 ++--------
net/ipv4/fib_frontend.c         |  3 ++-
15 files changed, 109 insertions(+), 58 deletions(-)

--
2.22.0.510.g264f2c817a-goog

#1 lore raw

PATCH v2 1/9 rcu/update: Remove useless check for debug_locks

In rcu_read_lock_sched_held(), debug_locks can never be true at the point we check it because we already check debug_locks in debug_lockdep_rcu_enabled() in the beginning. Remove the check.

---
 kernel/rcu/update.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/rcu/update.c +1 -5
index c3bf44ba42e5..bb961cd89e76 100644
@@ -93,17 +93,13 @@ module_param(rcu_normal_after_boot, int, 0);
  */
 int rcu_read_lock_sched_held(void)
 {
-	int lockdep_opinion = 0;
-
 	if (!debug_lockdep_rcu_enabled())
 		return 1;
 	if (!rcu_is_watching())
 		return 0;
 	if (!rcu_lockdep_current_cpu_online())
 		return 0;
-	if (debug_locks)
-		lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
-	return lockdep_opinion || !preemptible();
+	return lock_is_held(&rcu_sched_lock_map) || !preemptible();
 }
 EXPORT_SYMBOL(rcu_read_lock_sched_held);
 #endif
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#2 lore raw

PATCH v2 2/9 rcu: Add support for consolidated-RCU reader checking

In reply to Joel Fernandes (Google)

This patch adds support for checking RCU reader sections in list traversal macros. Optionally, if the list macro is called under SRCU or other lock/mutex protection, then appropriate lockdep expressions can be passed to make the checks pass.

Existing list_for_each_entry_rcu() invocations don't need to pass the optional fourth argument (cond) unless they are under some non-RCU protection and needs to make lockdep check pass.

---
 include/linux/rculist.h  | 28 +++++++++++++++++++++++-----
 include/linux/rcupdate.h |  7 +++++++
 kernel/rcu/Kconfig.debug | 11 +++++++++++
 kernel/rcu/update.c      | 14 ++++++++++++++
 4 files changed, 55 insertions(+), 5 deletions(-)
include/linux/rculist.h +23 -5
index e91ec9ddcd30..1048160625bb 100644
@@ -40,6 +40,20 @@ static inline void INIT_LIST_HEAD_RCU(struct list_head *list)
  */
 #define list_next_rcu(list)	(*((struct list_head __rcu **)(&(list)->next)))
 
+/*
+ * Check during list traversal that we are within an RCU reader
+ */
+
+#ifdef CONFIG_PROVE_RCU_LIST
+#define __list_check_rcu(dummy, cond, ...)				\
+	({								\
+	RCU_LOCKDEP_WARN(!cond && !rcu_read_lock_any_held(),		\
+			 "RCU-list traversed in non-reader section!");	\
+	 })
+#else
+#define __list_check_rcu(dummy, cond, ...) ({})
+#endif
+
 /*
  * Insert a new entry between two known consecutive entries.
  *
@@ -343,14 +357,16 @@ static inline void list_splice_tail_init_rcu(struct list_head *list,
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
  * @member:	the name of the list_head within the struct.
+ * @cond:	optional lockdep expression if called from non-RCU protection.
  *
  * This list-traversal primitive may safely run concurrently with
  * the _rcu list-mutation primitives such as list_add_rcu()
  * as long as the traversal is guarded by rcu_read_lock().
  */
-#define list_for_each_entry_rcu(pos, head, member) \
-	for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
-		&pos->member != (head); \
+#define list_for_each_entry_rcu(pos, head, member, cond...)		\
+	for (__list_check_rcu(dummy, ## cond, 0),			\
+	     pos = list_entry_rcu((head)->next, typeof(*pos), member);	\
+		&pos->member != (head);					\
 		pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
 
 /**
@@ -616,13 +632,15 @@ static inline void hlist_add_behind_rcu(struct hlist_node *n,
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
  * @member:	the name of the hlist_node within the struct.
+ * @cond:	optional lockdep expression if called from non-RCU protection.
  *
  * This list-traversal primitive may safely run concurrently with
  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  * as long as the traversal is guarded by rcu_read_lock().
  */
-#define hlist_for_each_entry_rcu(pos, head, member)			\
-	for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
+#define hlist_for_each_entry_rcu(pos, head, member, cond...)		\
+	for (__list_check_rcu(dummy, ## cond, 0),			\
+	     pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
 			typeof(*(pos)), member);			\
 		pos;							\
 		pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
include/linux/rcupdate.h +7 -0
index 922bb6848813..712b464ab960 100644
@@ -223,6 +223,7 @@ int debug_lockdep_rcu_enabled(void);
 int rcu_read_lock_held(void);
 int rcu_read_lock_bh_held(void);
 int rcu_read_lock_sched_held(void);
+int rcu_read_lock_any_held(void);
 
 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
@@ -243,6 +244,12 @@ static inline int rcu_read_lock_sched_held(void)
 {
 	return !preemptible();
 }
+
+static inline int rcu_read_lock_any_held(void)
+{
+	return !preemptible();
+}
+
 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
 #ifdef CONFIG_PROVE_RCU
kernel/rcu/Kconfig.debug +11 -0
index 0ec7d1d33a14..b20d0e2903d1 100644
@@ -7,6 +7,17 @@ menu "RCU Debugging"
 config PROVE_RCU
 	def_bool PROVE_LOCKING
 
+config PROVE_RCU_LIST
+	bool "RCU list lockdep debugging"
+	depends on PROVE_RCU
+	default n
+	help
+	  Enable RCU lockdep checking for list usages. By default it is
+	  turned off since there are several list RCU users that still
+	  need to be converted to pass a lockdep expression. To prevent
+	  false-positive splats, we keep it default disabled but once all
+	  users are converted, we can remove this config option.
+
 config TORTURE_TEST
 	tristate
 	default n
kernel/rcu/update.c +14 -0
index bb961cd89e76..0cc7be0fb6b5 100644
@@ -294,6 +294,20 @@ int rcu_read_lock_bh_held(void)
 }
 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 
+int rcu_read_lock_any_held(void)
+{
+	if (!debug_lockdep_rcu_enabled())
+		return 1;
+	if (!rcu_is_watching())
+		return 0;
+	if (!rcu_lockdep_current_cpu_online())
+		return 0;
+	if (lock_is_held(&rcu_lock_map) || lock_is_held(&rcu_sched_lock_map))
+		return 1;
+	return !preemptible();
+}
+EXPORT_SYMBOL_GPL(rcu_read_lock_any_held);
+
 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
 /**
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#3 lore raw

Re: [PATCH v2 2/9] rcu: Add support for consolidated-RCU reader checking

Quoted 10 lines · Joel Fernandes (Google)

This patch adds support for checking RCU reader sections in list traversal macros. Optionally, if the list macro is called under SRCU or other lock/mutex protection, then appropriate lockdep expressions can be passed to make the checks pass.

Existing list_for_each_entry_rcu() invocations don't need to pass the optional fourth argument (cond) unless they are under some non-RCU protection and needs to make lockdep check pass.

If you fold in the checks for extra parameters, I will take this one and also 1/9.

Thanx, Paul

Quoted 145 lines
---
 include/linux/rculist.h  | 28 +++++++++++++++++++++++-----
 include/linux/rcupdate.h |  7 +++++++
 kernel/rcu/Kconfig.debug | 11 +++++++++++
 kernel/rcu/update.c      | 14 ++++++++++++++
 4 files changed, 55 insertions(+), 5 deletions(-)
include/linux/rculist.h +23 -5
index e91ec9ddcd30..1048160625bb 100644
@@ -40,6 +40,20 @@ static inline void INIT_LIST_HEAD_RCU(struct list_head *list)
  */
 #define list_next_rcu(list)	(*((struct list_head __rcu **)(&(list)->next)))
 
+/*
+ * Check during list traversal that we are within an RCU reader
+ */
+
+#ifdef CONFIG_PROVE_RCU_LIST
+#define __list_check_rcu(dummy, cond, ...)				\
+	({								\
+	RCU_LOCKDEP_WARN(!cond && !rcu_read_lock_any_held(),		\
+			 "RCU-list traversed in non-reader section!");	\
+	 })
+#else
+#define __list_check_rcu(dummy, cond, ...) ({})
+#endif
+
 /*
  * Insert a new entry between two known consecutive entries.
  *
@@ -343,14 +357,16 @@ static inline void list_splice_tail_init_rcu(struct list_head *list,
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
  * @member:	the name of the list_head within the struct.
+ * @cond:	optional lockdep expression if called from non-RCU protection.
  *
  * This list-traversal primitive may safely run concurrently with
  * the _rcu list-mutation primitives such as list_add_rcu()
  * as long as the traversal is guarded by rcu_read_lock().
  */
-#define list_for_each_entry_rcu(pos, head, member) \
-	for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
-		&pos->member != (head); \
+#define list_for_each_entry_rcu(pos, head, member, cond...)		\
+	for (__list_check_rcu(dummy, ## cond, 0),			\
+	     pos = list_entry_rcu((head)->next, typeof(*pos), member);	\
+		&pos->member != (head);					\
 		pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
 
 /**
@@ -616,13 +632,15 @@ static inline void hlist_add_behind_rcu(struct hlist_node *n,
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
  * @member:	the name of the hlist_node within the struct.
+ * @cond:	optional lockdep expression if called from non-RCU protection.
  *
  * This list-traversal primitive may safely run concurrently with
  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  * as long as the traversal is guarded by rcu_read_lock().
  */
-#define hlist_for_each_entry_rcu(pos, head, member)			\
-	for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
+#define hlist_for_each_entry_rcu(pos, head, member, cond...)		\
+	for (__list_check_rcu(dummy, ## cond, 0),			\
+	     pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
 			typeof(*(pos)), member);			\
 		pos;							\
 		pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
include/linux/rcupdate.h +7 -0
index 922bb6848813..712b464ab960 100644
@@ -223,6 +223,7 @@ int debug_lockdep_rcu_enabled(void);
 int rcu_read_lock_held(void);
 int rcu_read_lock_bh_held(void);
 int rcu_read_lock_sched_held(void);
+int rcu_read_lock_any_held(void);
 
 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
@@ -243,6 +244,12 @@ static inline int rcu_read_lock_sched_held(void)
 {
 	return !preemptible();
 }
+
+static inline int rcu_read_lock_any_held(void)
+{
+	return !preemptible();
+}
+
 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
 #ifdef CONFIG_PROVE_RCU
kernel/rcu/Kconfig.debug +11 -0
index 0ec7d1d33a14..b20d0e2903d1 100644
@@ -7,6 +7,17 @@ menu "RCU Debugging"
 config PROVE_RCU
 	def_bool PROVE_LOCKING
 
+config PROVE_RCU_LIST
+	bool "RCU list lockdep debugging"
+	depends on PROVE_RCU
+	default n
+	help
+	  Enable RCU lockdep checking for list usages. By default it is
+	  turned off since there are several list RCU users that still
+	  need to be converted to pass a lockdep expression. To prevent
+	  false-positive splats, we keep it default disabled but once all
+	  users are converted, we can remove this config option.
+
 config TORTURE_TEST
 	tristate
 	default n
kernel/rcu/update.c +14 -0
index bb961cd89e76..0cc7be0fb6b5 100644
@@ -294,6 +294,20 @@ int rcu_read_lock_bh_held(void)
 }
 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 
+int rcu_read_lock_any_held(void)
+{
+	if (!debug_lockdep_rcu_enabled())
+		return 1;
+	if (!rcu_is_watching())
+		return 0;
+	if (!rcu_lockdep_current_cpu_online())
+		return 0;
+	if (lock_is_held(&rcu_lock_map) || lock_is_held(&rcu_sched_lock_map))
+		return 1;
+	return !preemptible();
+}
+EXPORT_SYMBOL_GPL(rcu_read_lock_any_held);
+
 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
 /**
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#4 lore raw

Re: [PATCH v2 2/9] rcu: Add support for consolidated-RCU reader checking

Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Joel Fernandes (Google)

This patch adds support for checking RCU reader sections in list traversal macros. Optionally, if the list macro is called under SRCU or other lock/mutex protection, then appropriate lockdep expressions can be passed to make the checks pass.

Quoted 8 lines
Quoted 5 lines

Existing list_for_each_entry_rcu() invocations don't need to pass the optional fourth argument (cond) unless they are under some non-RCU protection and needs to make lockdep check pass.

If you fold in the checks for extra parameters, I will take this one and also 1/9.

I folded the checks in and also threw in the rcu-sync with Oleg's ack:

Could you pull into /dev branch?

git pull https://github.com/joelagnel/linux-kernel.git list-first-three (Based on your dev branch)

#5 lore raw

Re: [PATCH v2 2/9] rcu: Add support for consolidated-RCU reader checking

Quoted 16 lines · Joel Fernandes
Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Joel Fernandes (Google)

This patch adds support for checking RCU reader sections in list traversal macros. Optionally, if the list macro is called under SRCU or other lock/mutex protection, then appropriate lockdep expressions can be passed to make the checks pass.

Quoted 8 lines
Quoted 5 lines

Existing list_for_each_entry_rcu() invocations don't need to pass the optional fourth argument (cond) unless they are under some non-RCU protection and needs to make lockdep check pass.

If you fold in the checks for extra parameters, I will take this one and also 1/9.

Quoted 6 lines

I folded the checks in and also threw in the rcu-sync with Oleg's ack:

Could you pull into /dev branch?

git pull https://github.com/joelagnel/linux-kernel.git list-first-three (Based on your dev branch)

Given that I am going to have to rebase these a few times, please email a v4.

Thanx, Paul

#6 lore raw

PATCH v2 3/9 rcu/sync: Remove custom check for reader-section

In reply to Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
Please note: Only build and boot tested this particular patch so far.

 include/linux/rcu_sync.h |  5 ++---
 kernel/rcu/sync.c        | 22 ----------------------
 2 files changed, 2 insertions(+), 25 deletions(-)
include/linux/rcu_sync.h +2 -3
index 6fc53a1345b3..c954f1efc919 100644
@@ -39,9 +39,8 @@ extern void rcu_sync_lockdep_assert(struct rcu_sync *);
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-#ifdef CONFIG_PROVE_RCU
-	rcu_sync_lockdep_assert(rsp);
-#endif
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
+			 "suspicious rcu_sync_is_idle() usage");
 	return !rsp->gp_state; /* GP_IDLE */
 }
 
kernel/rcu/sync.c +0 -22
index a8304d90573f..535e02601f56 100644
@@ -10,37 +10,25 @@
 #include <linux/rcu_sync.h>
 #include <linux/sched.h>
 
-#ifdef CONFIG_PROVE_RCU
-#define __INIT_HELD(func)	.held = func,
-#else
-#define __INIT_HELD(func)
-#endif
-
 static const struct {
 	void (*sync)(void);
 	void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
 	void (*wait)(void);
-#ifdef CONFIG_PROVE_RCU
-	int  (*held)(void);
-#endif
 } gp_ops[] = {
 	[RCU_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_held)
 	},
 	[RCU_SCHED_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_sched_held)
 	},
 	[RCU_BH_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_bh_held)
 	},
 };
 
@@ -49,16 +37,6 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
 
 #define	rss_lock	gp_wait.lock
 
-#ifdef CONFIG_PROVE_RCU
-void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
-{
-	RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
-			 "suspicious rcu_sync_is_idle() usage");
-}
-
-EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
-#endif
-
 /**
  * rcu_sync_init() - Initialize an rcu_sync structure
  * @rsp: Pointer to rcu_sync structure to be initialized
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#7 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

thanks!

- Joel

Quoted 85 lines

---
Please note: Only build and boot tested this particular patch so far.

 include/linux/rcu_sync.h |  5 ++---
 kernel/rcu/sync.c        | 22 ----------------------
 2 files changed, 2 insertions(+), 25 deletions(-)
include/linux/rcu_sync.h +2 -3
index 6fc53a1345b3..c954f1efc919 100644
@@ -39,9 +39,8 @@ extern void rcu_sync_lockdep_assert(struct rcu_sync *);
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-#ifdef CONFIG_PROVE_RCU
-	rcu_sync_lockdep_assert(rsp);
-#endif
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
+			 "suspicious rcu_sync_is_idle() usage");
 	return !rsp->gp_state; /* GP_IDLE */
 }
 
kernel/rcu/sync.c +0 -22
index a8304d90573f..535e02601f56 100644
@@ -10,37 +10,25 @@
 #include <linux/rcu_sync.h>
 #include <linux/sched.h>
 
-#ifdef CONFIG_PROVE_RCU
-#define __INIT_HELD(func)	.held = func,
-#else
-#define __INIT_HELD(func)
-#endif
-
 static const struct {
 	void (*sync)(void);
 	void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
 	void (*wait)(void);
-#ifdef CONFIG_PROVE_RCU
-	int  (*held)(void);
-#endif
 } gp_ops[] = {
 	[RCU_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_held)
 	},
 	[RCU_SCHED_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_sched_held)
 	},
 	[RCU_BH_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_bh_held)
 	},
 };
 
@@ -49,16 +37,6 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
 
 #define	rss_lock	gp_wait.lock
 
-#ifdef CONFIG_PROVE_RCU
-void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
-{
-	RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
-			 "suspicious rcu_sync_is_idle() usage");
-}
-
-EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
-#endif
-
 /**
  * rcu_sync_init() - Initialize an rcu_sync structure
  * @rsp: Pointer to rcu_sync structure to be initialized
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#8 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Thanx, Paul

Quoted 91 lines

thanks!

- Joel

Quoted 85 lines

---
Please note: Only build and boot tested this particular patch so far.

 include/linux/rcu_sync.h |  5 ++---
 kernel/rcu/sync.c        | 22 ----------------------
 2 files changed, 2 insertions(+), 25 deletions(-)
include/linux/rcu_sync.h +2 -3
index 6fc53a1345b3..c954f1efc919 100644
@@ -39,9 +39,8 @@ extern void rcu_sync_lockdep_assert(struct rcu_sync *);
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-#ifdef CONFIG_PROVE_RCU
-	rcu_sync_lockdep_assert(rsp);
-#endif
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
+			 "suspicious rcu_sync_is_idle() usage");
 	return !rsp->gp_state; /* GP_IDLE */
 }
 
kernel/rcu/sync.c +0 -22
index a8304d90573f..535e02601f56 100644
@@ -10,37 +10,25 @@
 #include <linux/rcu_sync.h>
 #include <linux/sched.h>
 
-#ifdef CONFIG_PROVE_RCU
-#define __INIT_HELD(func)	.held = func,
-#else
-#define __INIT_HELD(func)
-#endif
-
 static const struct {
 	void (*sync)(void);
 	void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
 	void (*wait)(void);
-#ifdef CONFIG_PROVE_RCU
-	int  (*held)(void);
-#endif
 } gp_ops[] = {
 	[RCU_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_held)
 	},
 	[RCU_SCHED_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_sched_held)
 	},
 	[RCU_BH_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_bh_held)
 	},
 };
 
@@ -49,16 +37,6 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
 
 #define	rss_lock	gp_wait.lock
 
-#ifdef CONFIG_PROVE_RCU
-void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
-{
-	RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
-			 "suspicious rcu_sync_is_idle() usage");
-}
-
-EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
-#endif
-
 /**
  * rcu_sync_init() - Initialize an rcu_sync structure
  * @rsp: Pointer to rcu_sync structure to be initialized
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#9 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

thanks,

- Joel

#10 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001
From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
 			 "suspicious rcu_sync_is_idle() usage");
 	return !READ_ONCE(rsp->gp_state); /* GP_IDLE */
 }
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#11 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 83 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/rcu_sync.h +0 -0
index 9b83865d24f9..0027d4c8087c 100644

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 7 lines
include/linux/rcu_sync.h +1 -3
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Thanx, Paul

Quoted 6 lines
include/linux/rcu_sync.h +0 -0
 			 "suspicious rcu_sync_is_idle() usage");
 	return !READ_ONCE(rsp->gp_state); /* GP_IDLE */
 }
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#12 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Which would mean no splats. Or, did I miss the point?

thanks,

- Joel

#13 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 106 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Quoted 7 lines

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Thanx, Paul

#14 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 110 lines · Paul E. McKenney
Quoted 109 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

Quoted 6 lines
Quoted 4 lines

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

#15 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 114 lines · Joel Fernandes
Quoted 113 lines · Paul E. McKenney
Quoted 112 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

I am not faulting you for taking the weekend off, actually. ;-)

Thanx, Paul

#16 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 115 lines · Paul E. McKenney
Quoted 114 lines · Joel Fernandes
Quoted 113 lines · Paul E. McKenney
Quoted 112 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

I am not faulting you for taking the weekend off, actually. ;-)

;-)

I tried doing RCU_LOCKDEP_WARN(preemptible(), ...) in this code path and I don't get any splats. I also disassembled the code and it seems to me RCU_LOCKDEP_WARN() becomes a NOOP which also the above reasoning confirms.

thanks,

- Joel

#17 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 123 lines · Joel Fernandes
Quoted 122 lines · Paul E. McKenney
Quoted 120 lines · Joel Fernandes
Quoted 110 lines · Paul E. McKenney
Quoted 109 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

Quoted 6 lines
Quoted 4 lines

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

Quoted 7 lines
Quoted 1 line

I am not faulting you for taking the weekend off, actually. ;-)

;-)

I tried doing RCU_LOCKDEP_WARN(preemptible(), ...) in this code path and I don't get any splats. I also disassembled the code and it seems to me RCU_LOCKDEP_WARN() becomes a NOOP which also the above reasoning confirms.

OK, very good. Could you do the same thing for the RCU_LOCKDEP_WARN() in synchronize_rcu()? Why or why not?

(No need to work this on your Sunday.)

Thanx, Paul

#18 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 126 lines · Paul E. McKenney
Quoted 125 lines · Joel Fernandes
Quoted 123 lines · Paul E. McKenney
Quoted 114 lines · Joel Fernandes
Quoted 113 lines · Paul E. McKenney
Quoted 112 lines · Joel Fernandes
Quoted 79 lines · Paul E. McKenney
Quoted 78 lines · Joel Fernandes
Quoted 50 lines · Joel Fernandes
Quoted 36 lines · Paul E. McKenney
Quoted 30 lines · Joel Fernandes
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Hi Oleg,
Slightly unrelated to the patch,
I tried hard to understand this comment below in percpu_down_read() but no dice.

I do understand how rcu sync and percpu rwsem works, however the comment below didn't make much sense to me. For one, there's no readers_fast anymore so I did not follow what readers_fast means. Could the comment be updated to reflect latest changes?
Also could you help understand how is a writer not able to change sem->state and count the per-cpu read counters at the same time as the comment tries to say?

/*
* We are in an RCU-sched read-side critical section, so the writer
* cannot both change sem->state from readers_fast and start checking
* counters while we are here. So if we see !sem->state, we know that
* the writer won't be checking until we're past the preempt_enable()
* and that once the synchronize_rcu() is done, the writer will see
* anything we did within this RCU-sched read-size critical section. */

Quoted 4 lines

Also,
I guess we could get rid of all of the gp_ops struct stuff now that since all the callbacks are the same now. I will post that as a follow-up patch to this series.

Quoted 7 lines

Hello, Joel,

Oleg has a set of patches updating this code that just hit mainline this week. These patches get rid of the code that previously handled RCU's multiple flavors. Or are you looking at current mainline and me just missing your point?

Hi Paul,
You are right on point. I have a bad habit of not rebasing my trees. In this case the feature branch of mine in concern was based on v5.1. Needless to say, I need to rebase my tree.

Quoted 6 lines

Yes, this sync clean up patch does conflict when I rebase, but other patches rebase just fine.

The 2 options I see are:
1. Let us drop this patch for now and I resend it later.
2. I resend all patches based on Linus's master branch.

Below is the updated patch based on Linus master branch:

---8<-----------------------

Quoted 1 line

From 5f40c9a07fcf3d6dafc2189599d0ba9443097d0f Mon Sep 17 00:00:00 2001

From: "Joel Fernandes (Google)" <[email protected]>
Date: Fri, 12 Jul 2019 12:13:27 -0400
Subject: [PATCH v2.1 3/9] rcu/sync: Remove custom check for reader-section

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

I am not faulting you for taking the weekend off, actually. ;-)

Quoted 9 lines
Quoted 5 lines

;-)

I tried doing RCU_LOCKDEP_WARN(preemptible(), ...) in this code path and I don't get any splats. I also disassembled the code and it seems to me RCU_LOCKDEP_WARN() becomes a NOOP which also the above reasoning confirms.

OK, very good. Could you do the same thing for the RCU_LOCKDEP_WARN() in synchronize_rcu()? Why or why not?

Hi Paul,

Yes synchronize_rcu() can also make use of this technique since it is strictly illegal to call synchronize_rcu() within a reader section.

I will add this to the set of my patches as well and send them all out next week, along with the rcu-sync and bh clean ups we discussed.

thanks,

- Joel

#19 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 1 line · Joel Fernandes

On Sat, Jul 13, 2019 at 02:28:12PM -0700, Paul E. McKenney wrote:

[snip]

Quoted 58 lines
Quoted 48 lines
Quoted 48 lines
Quoted 47 lines
Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Quoted 7 lines
Quoted 7 lines
---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

I am not faulting you for taking the weekend off, actually. ;-)

Quoted 9 lines
Quoted 5 lines

;-)

I tried doing RCU_LOCKDEP_WARN(preemptible(), ...) in this code path and I don't get any splats. I also disassembled the code and it seems to me RCU_LOCKDEP_WARN() becomes a NOOP which also the above reasoning confirms.

OK, very good. Could you do the same thing for the RCU_LOCKDEP_WARN() in synchronize_rcu()? Why or why not?

Quoted 7 lines

Hi Paul,

Yes synchronize_rcu() can also make use of this technique since it is strictly illegal to call synchronize_rcu() within a reader section.

I will add this to the set of my patches as well and send them all out next week, along with the rcu-sync and bh clean ups we discussed.

After sending this email, it occurs to me it wont work in synchronize_rcu() for !CONFIG_PREEMPT kernels. This is because in a !CONFIG_PREEMPT kernel, executing in kernel mode itself looks like being in an RCU reader. So we should leave that as is. However it will work fine for rcu_sync_is_idle (for CONFIG_PREEMPT=n kernels) as I mentioned earlier.

Were trying to throw me a Quick-Quiz ? ;-) In that case, hope I passed!

thanks,

- Joel

#20 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 69 lines · Joel Fernandes
Quoted 1 line · Joel Fernandes

On Sat, Jul 13, 2019 at 02:28:12PM -0700, Paul E. McKenney wrote:

[snip]

Quoted 58 lines
Quoted 48 lines
Quoted 48 lines
Quoted 47 lines
Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Quoted 7 lines
Quoted 7 lines
---
 include/linux/rcu_sync.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 12 lines
include/linux/rcu_sync.h +1 -3
index 9b83865d24f9..0027d4c8087c 100644
@@ -31,9 +31,7 @@ struct rcu_sync {
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

I believe that replacing rcu_read_lock_sched_held() with preemptible() in a CONFIG_PREEMPT=n kernel will give you false-positive splats here. If you have not already done so, could you please give it a try?

Hi Paul,
I don't think it will cause splats for !CONFIG_PREEMPT.

Currently, rcu_read_lock_any_held() introduced in this patch returns true if !preemptible(). This means that:

The following expression above:
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),...)

Becomes:
RCU_LOCKDEP_WARN(preemptible(), ...)

For, CONFIG_PREEMPT=n kernels, this means:
RCU_LOCKDEP_WARN(0, ...)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Which would mean no splats. Or, did I miss the point?

I suggest trying it out on a CONFIG_PREEMPT=n kernel.

Sure, will do, sorry did not try it out yet because was busy with weekend chores but will do soon, thanks!

I am not faulting you for taking the weekend off, actually. ;-)

Quoted 9 lines
Quoted 5 lines

;-)

I tried doing RCU_LOCKDEP_WARN(preemptible(), ...) in this code path and I don't get any splats. I also disassembled the code and it seems to me RCU_LOCKDEP_WARN() becomes a NOOP which also the above reasoning confirms.

OK, very good. Could you do the same thing for the RCU_LOCKDEP_WARN() in synchronize_rcu()? Why or why not?

Quoted 7 lines

Hi Paul,

Yes synchronize_rcu() can also make use of this technique since it is strictly illegal to call synchronize_rcu() within a reader section.

I will add this to the set of my patches as well and send them all out next week, along with the rcu-sync and bh clean ups we discussed.

Quoted 7 lines

After sending this email, it occurs to me it wont work in synchronize_rcu() for !CONFIG_PREEMPT kernels. This is because in a !CONFIG_PREEMPT kernel, executing in kernel mode itself looks like being in an RCU reader. So we should leave that as is. However it will work fine for rcu_sync_is_idle (for CONFIG_PREEMPT=n kernels) as I mentioned earlier.

Were trying to throw me a Quick-Quiz ? ;-) In that case, hope I passed!

You did pass. This time. ;-)

Thanx, Paul

Acked-by
#21 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

In reply to Joel Fernandes

include/linux/rcu_sync.h struct rcu_sync { · line 34

Quoted 7 lines · Joel Fernandes

static inline bool rcu_sync_is_idle(struct rcu_sync *rsp) {

include/linux/rcu_sync.h +1 -3
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
-			 !rcu_read_lock_bh_held() &&
-			 !rcu_read_lock_sched_held(),
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),

Yes, this is what I meant.

Sorry for confusion, I should have mentioned that rcu_sync_is_idle() was recently updated when I suggested to use the new helper.

#22 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

In reply to Joel Fernandes (Google)
Quoted 7 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

This needs to be forward-ported to current mainline. (Or, I believe equivalently for this file, to branch "dev" of -rcu.)

Especially given that you have Oleg's Ack, I would be happy to take the forward-ported version.

Thanx, Paul

Quoted 85 lines

---
Please note: Only build and boot tested this particular patch so far.

 include/linux/rcu_sync.h |  5 ++---
 kernel/rcu/sync.c        | 22 ----------------------
 2 files changed, 2 insertions(+), 25 deletions(-)
include/linux/rcu_sync.h +2 -3
index 6fc53a1345b3..c954f1efc919 100644
@@ -39,9 +39,8 @@ extern void rcu_sync_lockdep_assert(struct rcu_sync *);
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-#ifdef CONFIG_PROVE_RCU
-	rcu_sync_lockdep_assert(rsp);
-#endif
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
+			 "suspicious rcu_sync_is_idle() usage");
 	return !rsp->gp_state; /* GP_IDLE */
 }
 
kernel/rcu/sync.c +0 -22
index a8304d90573f..535e02601f56 100644
@@ -10,37 +10,25 @@
 #include <linux/rcu_sync.h>
 #include <linux/sched.h>
 
-#ifdef CONFIG_PROVE_RCU
-#define __INIT_HELD(func)	.held = func,
-#else
-#define __INIT_HELD(func)
-#endif
-
 static const struct {
 	void (*sync)(void);
 	void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
 	void (*wait)(void);
-#ifdef CONFIG_PROVE_RCU
-	int  (*held)(void);
-#endif
 } gp_ops[] = {
 	[RCU_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_held)
 	},
 	[RCU_SCHED_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_sched_held)
 	},
 	[RCU_BH_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_bh_held)
 	},
 };
 
@@ -49,16 +37,6 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
 
 #define	rss_lock	gp_wait.lock
 
-#ifdef CONFIG_PROVE_RCU
-void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
-{
-	RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
-			 "suspicious rcu_sync_is_idle() usage");
-}
-
-EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
-#endif
-
 /**
  * rcu_sync_init() - Initialize an rcu_sync structure
  * @rsp: Pointer to rcu_sync structure to be initialized
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#23 lore raw

Re: [PATCH v2 3/9] rcu/sync: Remove custom check for reader-section

Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Joel Fernandes (Google)

The rcu/sync code was doing its own check whether we are in a reader section. With RCU consolidating flavors and the generic helper added in this series, this is no longer need. We can just use the generic helper and it results in a nice cleanup.

Quoted 8 lines
Quoted 2 lines

This needs to be forward-ported to current mainline. (Or, I believe equivalently for this file, to branch "dev" of -rcu.)

Especially given that you have Oleg's Ack, I would be happy to take the forward-ported version.

Never mind, I am one version behind. Apologies for the noise!

Thanx, Paul

Quoted 85 lines
Quoted 85 lines

---
Please note: Only build and boot tested this particular patch so far.

 include/linux/rcu_sync.h |  5 ++---
 kernel/rcu/sync.c        | 22 ----------------------
 2 files changed, 2 insertions(+), 25 deletions(-)
include/linux/rcu_sync.h +2 -3
index 6fc53a1345b3..c954f1efc919 100644
@@ -39,9 +39,8 @@ extern void rcu_sync_lockdep_assert(struct rcu_sync *);
  */
 static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
 {
-#ifdef CONFIG_PROVE_RCU
-	rcu_sync_lockdep_assert(rsp);
-#endif
+	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
+			 "suspicious rcu_sync_is_idle() usage");
 	return !rsp->gp_state; /* GP_IDLE */
 }
 
kernel/rcu/sync.c +0 -22
index a8304d90573f..535e02601f56 100644
@@ -10,37 +10,25 @@
 #include <linux/rcu_sync.h>
 #include <linux/sched.h>
 
-#ifdef CONFIG_PROVE_RCU
-#define __INIT_HELD(func)	.held = func,
-#else
-#define __INIT_HELD(func)
-#endif
-
 static const struct {
 	void (*sync)(void);
 	void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
 	void (*wait)(void);
-#ifdef CONFIG_PROVE_RCU
-	int  (*held)(void);
-#endif
 } gp_ops[] = {
 	[RCU_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_held)
 	},
 	[RCU_SCHED_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_sched_held)
 	},
 	[RCU_BH_SYNC] = {
 		.sync = synchronize_rcu,
 		.call = call_rcu,
 		.wait = rcu_barrier,
-		__INIT_HELD(rcu_read_lock_bh_held)
 	},
 };
 
@@ -49,16 +37,6 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
 
 #define	rss_lock	gp_wait.lock
 
-#ifdef CONFIG_PROVE_RCU
-void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
-{
-	RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
-			 "suspicious rcu_sync_is_idle() usage");
-}
-
-EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
-#endif
-
 /**
  * rcu_sync_init() - Initialize an rcu_sync structure
  * @rsp: Pointer to rcu_sync structure to be initialized
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#24 lore raw

PATCH v2 4/9 ipv4: add lockdep condition to fix for_each_entry

In reply to Joel Fernandes (Google)
---
 net/ipv4/fib_frontend.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/fib_frontend.c +2 -1
index b298255f6fdb..ef7c9f8e8682 100644
@@ -127,7 +127,8 @@ struct fib_table *fib_get_table(struct net *net, u32 id)
 	h = id & (FIB_TABLE_HASHSZ - 1);
 
 	head = &net->ipv4.fib_table_hash[h];
-	hlist_for_each_entry_rcu(tb, head, tb_hlist) {
+	hlist_for_each_entry_rcu(tb, head, tb_hlist,
+				 lockdep_rtnl_is_held()) {
 		if (tb->tb_id == id)
 			return tb;
 	}
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#25 lore raw

PATCH v2 5/9 driver/core: Convert to use built-in RCU list checking

In reply to Joel Fernandes (Google)

list_for_each_entry_rcu has built-in RCU and lock checking. Make use of it in driver core.

---
 drivers/base/base.h          |  1 +
 drivers/base/core.c          | 10 ++++++++++
 drivers/base/power/runtime.c | 15 ++++++++++-----
 3 files changed, 21 insertions(+), 5 deletions(-)
drivers/base/base.h +1 -0
index b405436ee28e..0d32544b6f91 100644
@@ -165,6 +165,7 @@ static inline int devtmpfs_init(void) { return 0; }
 /* Device links support */
 extern int device_links_read_lock(void);
 extern void device_links_read_unlock(int idx);
+extern int device_links_read_lock_held(void);
 extern int device_links_check_suppliers(struct device *dev);
 extern void device_links_driver_bound(struct device *dev);
 extern void device_links_driver_cleanup(struct device *dev);
drivers/base/core.c +10 -0
index fd7511e04e62..6c5ca9685647 100644
@@ -68,6 +68,11 @@ void device_links_read_unlock(int idx)
 {
 	srcu_read_unlock(&device_links_srcu, idx);
 }
+
+int device_links_read_lock_held(void)
+{
+	return srcu_read_lock_held(&device_links_srcu);
+}
 #else /* !CONFIG_SRCU */
 static DECLARE_RWSEM(device_links_lock);
 
@@ -91,6 +96,11 @@ void device_links_read_unlock(int not_used)
 {
 	up_read(&device_links_lock);
 }
+
+int device_links_read_lock_held(void)
+{
+	return lock_is_held(&device_links_lock);
+}
 #endif /* !CONFIG_SRCU */
 
 /**
drivers/base/power/runtime.c +10 -5
index 952a1e7057c7..7a10e8379a70 100644
@@ -287,7 +287,8 @@ static int rpm_get_suppliers(struct device *dev)
 {
 	struct device_link *link;
 
-	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
+	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
+				device_links_read_lock_held()) {
 		int retval;
 
 		if (!(link->flags & DL_FLAG_PM_RUNTIME) ||
@@ -309,7 +310,8 @@ static void rpm_put_suppliers(struct device *dev)
 {
 	struct device_link *link;
 
-	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
+	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
+				device_links_read_lock_held()) {
 		if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND)
 			continue;
 
@@ -1640,7 +1642,8 @@ void pm_runtime_clean_up_links(struct device *dev)
 
 	idx = device_links_read_lock();
 
-	list_for_each_entry_rcu(link, &dev->links.consumers, s_node) {
+	list_for_each_entry_rcu(link, &dev->links.consumers, s_node,
+				device_links_read_lock_held()) {
 		if (link->flags & DL_FLAG_STATELESS)
 			continue;
 
@@ -1662,7 +1665,8 @@ void pm_runtime_get_suppliers(struct device *dev)
 
 	idx = device_links_read_lock();
 
-	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
+	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
+				device_links_read_lock_held())
 		if (link->flags & DL_FLAG_PM_RUNTIME) {
 			link->supplier_preactivated = true;
 			refcount_inc(&link->rpm_active);
@@ -1683,7 +1687,8 @@ void pm_runtime_put_suppliers(struct device *dev)
 
 	idx = device_links_read_lock();
 
-	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
+	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
+				device_links_read_lock_held())
 		if (link->supplier_preactivated) {
 			link->supplier_preactivated = false;
 			if (refcount_dec_not_one(&link->rpm_active))
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#26 lore raw

PATCH v2 6/9 workqueue: Convert for_each_wq to use built-in list check

In reply to Joel Fernandes (Google)

list_for_each_entry_rcu now has support to check for RCU reader sections as well as lock. Just use the support in it, instead of explictly checking in the caller.

---
 kernel/workqueue.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
kernel/workqueue.c +2 -8
index 9657315405de..5e88449bdd83 100644
@@ -363,11 +363,6 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
 			 !lockdep_is_held(&wq_pool_mutex),		\
 			 "RCU or wq_pool_mutex should be held")
 
-#define assert_rcu_or_wq_mutex(wq)					\
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&			\
-			 !lockdep_is_held(&wq->mutex),			\
-			 "RCU or wq->mutex should be held")
-
 #define assert_rcu_or_wq_mutex_or_pool_mutex(wq)			\
 	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&			\
 			 !lockdep_is_held(&wq->mutex) &&		\
@@ -424,9 +419,8 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  * ignored.
  */
 #define for_each_pwq(pwq, wq)						\
-	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node)		\
-		if (({ assert_rcu_or_wq_mutex(wq); false; })) { }	\
-		else
+	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node,		\
+				 lock_is_held(&(wq->mutex).dep_map))
 
 #ifdef CONFIG_DEBUG_OBJECTS_WORK
 
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#27 lore raw

PATCH v2 7/9 x86/pci: Pass lockdep condition to pcm_mmcfg_list iterator

In reply to Joel Fernandes (Google)

The pcm_mmcfg_list is traversed with list_for_each_entry_rcu without a reader-lock held, because the pci_mmcfg_lock is already held. Make this known to the list macro so that it fixes new lockdep warnings that trigger due to lockdep checks added to list_for_each_entry_rcu().

---
 arch/x86/pci/mmconfig-shared.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/pci/mmconfig-shared.c +3 -2
index 7389db538c30..6fa42e9c4e6f 100644
@@ -29,6 +29,7 @@
 static bool pci_mmcfg_running_state;
 static bool pci_mmcfg_arch_init_failed;
 static DEFINE_MUTEX(pci_mmcfg_lock);
+#define pci_mmcfg_lock_held() lock_is_held(&(pci_mmcfg_lock).dep_map)
 
 LIST_HEAD(pci_mmcfg_list);
 
@@ -54,7 +55,7 @@ static void list_add_sorted(struct pci_mmcfg_region *new)
 	struct pci_mmcfg_region *cfg;
 
 	/* keep list sorted by segment and starting bus number */
-	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list) {
+	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held()) {
 		if (cfg->segment > new->segment ||
 		    (cfg->segment == new->segment &&
 		     cfg->start_bus >= new->start_bus)) {
@@ -118,7 +119,7 @@ struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
 {
 	struct pci_mmcfg_region *cfg;
 
-	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
+	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held())
 		if (cfg->segment == segment &&
 		    cfg->start_bus <= bus && bus <= cfg->end_bus)
 			return cfg;
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#28 lore raw

PATCH v2 8/9 acpi: Use built-in RCU list checking for acpi_ioremaps list

In reply to Joel Fernandes (Google)

list_for_each_entry_rcu has built-in RCU and lock checking. Make use of it for acpi_ioremaps list traversal.

---
 drivers/acpi/osl.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/acpi/osl.c +4 -2
index f29e427d0d1d..c8b5d712c7ae 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/highmem.h>
+#include <linux/lockdep.h>
 #include <linux/pci.h>
 #include <linux/interrupt.h>
 #include <linux/kmod.h>
@@ -94,6 +95,7 @@ struct acpi_ioremap {
 
 static LIST_HEAD(acpi_ioremaps);
 static DEFINE_MUTEX(acpi_ioremap_lock);
+#define acpi_ioremap_lock_held() lock_is_held(&acpi_ioremap_lock.dep_map)
 
 static void __init acpi_request_region (struct acpi_generic_address *gas,
 	unsigned int length, char *desc)
@@ -220,7 +222,7 @@ acpi_map_lookup(acpi_physical_address phys, acpi_size size)
 {
 	struct acpi_ioremap *map;
 
-	list_for_each_entry_rcu(map, &acpi_ioremaps, list)
+	list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
 		if (map->phys <= phys &&
 		    phys + size <= map->phys + map->size)
 			return map;
@@ -263,7 +265,7 @@ acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
 {
 	struct acpi_ioremap *map;
 
-	list_for_each_entry_rcu(map, &acpi_ioremaps, list)
+	list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
 		if (map->virt <= virt &&
 		    virt + size <= map->virt + map->size)
 			return map;
Signature · 1 line
2.22.0.510.g264f2c817a-goog
#29 lore raw

PATCH v2 9/9 doc: Update documentation about list_for_each_entry_rcu

In reply to Joel Fernandes (Google)

This patch updates the documentation with information about usage of lockdep with list_for_each_entry_rcu().

---
 Documentation/RCU/lockdep.txt   | 15 +++++++++++----
 Documentation/RCU/whatisRCU.txt |  9 ++++++++-
 2 files changed, 19 insertions(+), 5 deletions(-)
Documentation/RCU/lockdep.txt +11 -4
index da51d3068850..3d967df3a801 100644
@@ -96,7 +96,14 @@ other flavors of rcu_dereference().  On the other hand, it is illegal
 to use rcu_dereference_protected() if either the RCU-protected pointer
 or the RCU-protected data that it points to can change concurrently.
 
-There are currently only "universal" versions of the rcu_assign_pointer()
-and RCU list-/tree-traversal primitives, which do not (yet) check for
-being in an RCU read-side critical section.  In the future, separate
-versions of these primitives might be created.
+Similar to rcu_dereference_protected, The RCU list and hlist traversal
+primitives also check for whether there are called from within a reader
+section. However, an optional lockdep expression can be passed to them as
+the last argument in case they are called under other non-RCU protection.
+
+For example, the workqueue for_each_pwq() macro is implemented as follows.
+It is safe to call for_each_pwq() outside a reader section but under protection
+of wq->mutex:
+#define for_each_pwq(pwq, wq)
+	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node,
+				lock_is_held(&(wq->mutex).dep_map))
Documentation/RCU/whatisRCU.txt +8 -1
index 981651a8b65d..a08c03735963 100644
@@ -290,7 +290,7 @@ rcu_dereference()
 	at any time, including immediately after the rcu_dereference().
 	And, again like rcu_assign_pointer(), rcu_dereference() is
 	typically used indirectly, via the _rcu list-manipulation
-	primitives, such as list_for_each_entry_rcu().
+	primitives, such as list_for_each_entry_rcu() [2].
 
 	[1] The variant rcu_dereference_protected() can be used outside
 	of an RCU read-side critical section as long as the usage is
@@ -305,6 +305,13 @@ rcu_dereference()
 	a lockdep splat is emitted.  See RCU/Design/Requirements/Requirements.html
 	and the API's code comments for more details and example usage.
 
+	[2] In case the list_for_each_entry_rcu() primitive is intended
+	to be used outside of an RCU reader section such as when
+	protected by a lock, then an additional lockdep expression can be
+	passed as the last argument to it so that RCU lockdep checking code
+	knows that the dereference of the list pointers are safe. If the
+	indicated protection is not provided, a lockdep splat is emitted.
+
 The following diagram shows how each API communicates among the
 reader, updater, and reclaimer.
 
Signature · 1 line
2.22.0.510.g264f2c817a-goog