From: NeilBrown <neilb@suse.de>
To: Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 4/7] sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up()
Date: Wed, 25 Sep 2024 15:31:41 +1000 [thread overview]
Message-ID: <20240925053405.3960701-5-neilb@suse.de> (raw)
In-Reply-To: <20240925053405.3960701-1-neilb@suse.de>
There are common patterns in the kernel of using test_and_clear_bit()
before wake_up_bit(), and atomic_dec_and_test() before wake_up_var().
These combinations don't need extra barriers but sometimes include them
unnecessarily.
To help avoid the unnecessary barriers and to help discourage the
general use of wake_up_bit/var (which is a fragile interface) introduce
two combined functions which implement these patterns.
Also add store_release_wake_up() which supports the task of simply
setting a non-atomic variable and sending a wakeup. This pattern
requires barriers which are often omitted.
Signed-off-by: NeilBrown <neilb@suse.de>
---
include/linux/wait_bit.h | 60 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h
index 06ec99b90bf3..0272629b590a 100644
--- a/include/linux/wait_bit.h
+++ b/include/linux/wait_bit.h
@@ -419,4 +419,64 @@ static inline void clear_and_wake_up_bit(int bit, unsigned long *word)
wake_up_bit(word, bit);
}
+/**
+ * test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit
+ * @bit: the bit of the word being waited on
+ * @word: the address of memory containing that bit
+ *
+ * If the bit is set and can be atomically cleared, any tasks waiting in
+ * wait_on_bit() or similar will be woken. This call has the same
+ * complete ordering semantics as test_and_clear_bit(). Any changes to
+ * memory made before this call are guaranteed to be visible after the
+ * corresponding wait_on_bit() completes.
+ *
+ * Returns %true if the bit was successfully set and the wake up was sent.
+ */
+static inline bool test_and_clear_wake_up_bit(int bit, unsigned long *word)
+{
+ if (!test_and_clear_bit(bit, word))
+ return false;
+ /* no extra barrier required */
+ wake_up_bit(word, bit);
+ return true;
+}
+
+/**
+ * atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters
+ * @var: the variable to dec and test
+ *
+ * Decrements the atomic variable and if it reaches zero, send a wake_up to any
+ * processes waiting on the variable.
+ *
+ * This function has the same complete ordering semantics as atomic_dec_and_test.
+ *
+ * Returns %true is the variable reaches zero and the wake up was sent.
+ */
+
+static inline bool atomic_dec_and_wake_up(atomic_t *var)
+{
+ if (!atomic_dec_and_test(var))
+ return false;
+ /* No extra barrier required */
+ wake_up_var(var);
+ return true;
+}
+
+/**
+ * store_release_wake_up - update a variable and send a wake_up
+ * @var: the address of the variable to be updated and woken
+ * @val: the value to store in the variable.
+ *
+ * Store the given value in the variable send a wake up to any tasks
+ * waiting on the variable. All necessary barriers are included to ensure
+ * the task calling wait_var_event() sees the new value and all values
+ * written to memory before this call.
+ */
+#define store_release_wake_up(var, val) \
+do { \
+ smp_store_release(var, val); \
+ smp_mb(); \
+ wake_up_var(var); \
+} while (0)
+
#endif /* _LINUX_WAIT_BIT_H */
--
2.46.0
next prev parent reply other threads:[~2024-09-25 5:36 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-25 5:31 [PATCH 0/7 v3] Make wake_up_{bit,var} less fragile NeilBrown
2024-09-25 5:31 ` [PATCH 1/7] sched: change wake_up_bit() and related function to expect unsigned long * NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-25 5:31 ` [PATCH 2/7] sched: Improve documentation for wake_up_bit/wait_on_bit family of functions NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-25 5:31 ` [PATCH 3/7] sched: Document wait_var_event() family of functions and wake_up_var() NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-25 5:31 ` NeilBrown [this message]
2024-10-08 7:56 ` [tip: sched/core] sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up() tip-bot2 for NeilBrown
2024-09-25 5:31 ` [PATCH 5/7] sched: Add wait/wake interface for variable updated under a lock NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-25 5:31 ` [PATCH 6/7] sched: add wait_var_event_io() NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-25 5:31 ` [PATCH 7/7] softirq: use bit waits instead of var waits NeilBrown
2024-10-08 7:56 ` [tip: sched/core] " tip-bot2 for NeilBrown
2024-09-26 17:13 ` [PATCH 0/7 v3] Make wake_up_{bit,var} less fragile Thomas Gleixner
2024-09-26 21:14 ` NeilBrown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240925053405.3960701-5-neilb@suse.de \
--to=neilb@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox