Skip to contentSkip to conversation

locking/rwsem: Reinit wake_q after use

PATCH New Waiman Long 3 participants 5 messages Jan 22, 2017 – Feb 1, 2017

Subsystems (2)
lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 1/1 locking/rwsem: Reinit wake_q after use

In __rwsem_down_write_failed_common(), the same wake_q variable name is defined twice, with the inner wake_q hiding the one in outer scope. We can either use different names for the two wake_q's. Even better, we can use the same wake_q twice, if necessary. To enable the latter change, we need to define a new helper function wake_q_init() to enable reinitalization of wake_q after use.

---
 include/linux/sched.h       | 6 ++++++
 kernel/locking/rwsem-xadd.c | 7 +++++--
 2 files changed, 11 insertions(+), 2 deletions(-)
include/linux/sched.h +6 -0
index 8a9bbb6..3640bde 100644
@@ -1019,6 +1019,12 @@ struct wake_q_head {
 #define DEFINE_WAKE_Q(name)				\
 	struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
 
+static inline void wake_q_init(struct wake_q_head *head)
+{
+	head->first = WAKE_Q_TAIL;
+	head->lastp = &head->first;
+}
+
 extern void wake_q_add(struct wake_q_head *head,
 		       struct task_struct *task);
 extern void wake_up_q(struct wake_q_head *head);
kernel/locking/rwsem-xadd.c +5 -2
index a3a381a..2ad8d8d 100644
@@ -502,8 +502,6 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)
 		 * wake any read locks that were queued ahead of us.
 		 */
 		if (count > RWSEM_WAITING_BIAS) {
-			DEFINE_WAKE_Q(wake_q);
-
 			__rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q);
 			/*
 			 * The wakeup is normally called _after_ the wait_lock
@@ -513,6 +511,11 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)
 			 * for attempting rwsem_try_write_lock().
 			 */
 			wake_up_q(&wake_q);
+
+			/*
+			 * Reinitialize wake_q after use.
+			 */
+			wake_q_init(&wake_q);
 		}
 
 	} else
Signature · 1 line
1.8.3.1
#1 lore raw
locking/rwsem: Reinit wake_q after use
Commit-ID:  bcc9a76d5ac426bc45c9e863b1830347827ca77a
Gitweb:     http://git.kernel.org/tip/bcc9a76d5ac426bc45c9e863b1830347827ca77a
Author:     Waiman Long <[email protected]>
AuthorDate: Sat, 21 Jan 2017 21:33:35 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sun, 22 Jan 2017 09:54:00 +0100

locking/rwsem: Reinit wake_q after use

In __rwsem_down_write_failed_common(), the same wake_q variable name is defined twice, with the inner wake_q hiding the one in outer scope. We can either use different names for the two wake_q's.

Even better, we can use the same wake_q twice, if necessary.

To enable the latter change, we need to define a new helper function wake_q_init() to enable reinitalization of wake_q after use.

Cc Andrew Morton <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/sched.h       | 6 ++++++
 kernel/locking/rwsem-xadd.c | 7 +++++--
 2 files changed, 11 insertions(+), 2 deletions(-)
include/linux/sched.h +6 -0
index f4f9d32..4e62b37 100644
@@ -1021,6 +1021,12 @@ struct wake_q_head {
 #define DEFINE_WAKE_Q(name)				\
 	struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
 
+static inline void wake_q_init(struct wake_q_head *head)
+{
+	head->first = WAKE_Q_TAIL;
+	head->lastp = &head->first;
+}
+
 extern void wake_q_add(struct wake_q_head *head,
 		       struct task_struct *task);
 extern void wake_up_q(struct wake_q_head *head);
kernel/locking/rwsem-xadd.c +5 -2
index a3a381a..2ad8d8d 100644
@@ -502,8 +502,6 @@ __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state)
 		 * wake any read locks that were queued ahead of us.
 		 */
 		if (count > RWSEM_WAITING_BIAS) {
-			DEFINE_WAKE_Q(wake_q);
-
 			__rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q);
 			/*
 			 * The wakeup is normally called _after_ the wait_lock
@@ -513,6 +511,11 @@ __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state)
 			 * for attempting rwsem_try_write_lock().
 			 */
 			wake_up_q(&wake_q);
+
+			/*
+			 * Reinitialize wake_q after use.
+			 */
+			wake_q_init(&wake_q);
 		}
 
 	} else
#2 lore raw

PATCH 1/1 sched/wake_q: Clarify queue reinit

As of bcc9a76d5ac (locking/rwsem: Reinit wake_q after use), the comment regarding the list reinitialization no longer applies, update it with the new wake_q_init() helper.

---
include/linux/sched.h | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sched.h +2 -2
index 3640bde9f982..c0bc626c531f 100644
@@ -998,8 +998,8 @@ enum cpu_idle_type {
 *
 * The DEFINE_WAKE_Q macro declares and initializes the list head.
 * wake_up_q() does NOT reinitialize the list; it's expected to be
- * called near the end of a function, where the fact that the queue is
- * not used again will be easy to see by inspection.
+ * called near the end of a function. Otherwise, the list can be
+ * re-initialized for later re-use by wake_q_init().
 *
 * Note that this can cause spurious wakeups. schedule() callers
 * must ensure the call is done inside a loop, confirming that the
Signature · 1 line
2.6.6
author reviewer Acked-by on v1 1/1
#3 lore raw

Re: [PATCH -tip] sched/wake_q: Clarify queue reinit

Quoted 14 lines · Davidlohr Bueso

As of bcc9a76d5ac (locking/rwsem: Reinit wake_q after use), the comment regarding the list reinitialization no longer applies, update it with the new wake_q_init() helper.

---
include/linux/sched.h | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sched.h +0 -0
index 3640bde9f982..c0bc626c531f 100644

include/linux/sched.h enum cpu_idle_type { · line 1005

Quoted 10 lines
include/linux/sched.h +2 -2
@@ -998,8 +998,8 @@ enum cpu_idle_type {
 *
 * The DEFINE_WAKE_Q macro declares and initializes the list head.
 * wake_up_q() does NOT reinitialize the list; it's expected to be
- * called near the end of a function, where the fact that the queue is
- * not used again will be easy to see by inspection.
+ * called near the end of a function. Otherwise, the list can be
+ * re-initialized for later re-use by wake_q_init().
 *
 * Note that this can cause spurious wakeups. schedule() callers
 * must ensure the call is done inside a loop, confirming that the
#4 lore raw

[tip:locking/core] sched/wake_q: Clarify queue reinit comment

In reply to Davidlohr Bueso
sched/wake_q: Clarify queue reinit comment
Commit-ID:  0754445d71c37a7afd4f0790a9be4cf53c1b8cc4
Gitweb:     http://git.kernel.org/tip/0754445d71c37a7afd4f0790a9be4cf53c1b8cc4
Author:     Davidlohr Bueso <[email protected]>
AuthorDate: Sun, 29 Jan 2017 07:15:31 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 1 Feb 2017 10:02:18 +0100

sched/wake_q: Clarify queue reinit comment

As of:

bcc9a76d5ac ("locking/rwsem: Reinit wake_q after use")

the comment regarding the list reinitialization no longer applies, update it with the new wake_q_init() helper.

---
 include/linux/sched.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sched.h +2 -2
index 4e62b37..1cc0ded 100644
@@ -1000,8 +1000,8 @@ enum cpu_idle_type {
  *
  * The DEFINE_WAKE_Q macro declares and initializes the list head.
  * wake_up_q() does NOT reinitialize the list; it's expected to be
- * called near the end of a function, where the fact that the queue is
- * not used again will be easy to see by inspection.
+ * called near the end of a function. Otherwise, the list can be
+ * re-initialized for later re-use by wake_q_init().
  *
  * Note that this can cause spurious wakeups. schedule() callers
  * must ensure the call is done inside a loop, confirming that the