[PATCH net-next 1/3] once: implement DO_ONCE_LITE for non-fast-path "do once" functionality
From: Tanner Love <hidden>
Date: 2021-04-22 19:47:54
Subsystem:
the rest · Maintainer:
Linus Torvalds
From: Tanner Love <redacted>
Certain uses of "do once" functionality (such as many occurrences of
static bool __section(".data.once")) reside outside of fast path, and
thus do not require jump label patching via static keys.
Implement DO_ONCE_LITE, which offers this "do once" functionality
without using static keys.
Implement DO_ONCE_LITE_IF, which offers the same functionality but
gated by a condition test. This is common in current uses of static
bool __section(".data.once").
Signed-off-by: Tanner Love <redacted>
Acked-by: Eric Dumazet <edumazet@google.com>
Acked-by: Mahesh Bandewar <redacted>
---
include/linux/once.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/include/linux/once.h b/include/linux/once.h
index 9225ee6d96c7..a92bb213f817 100644
--- a/include/linux/once.h
+++ b/include/linux/once.h@@ -52,6 +52,22 @@ void __do_once_done(bool *done, struct static_key_true *once_key, ___ret; \ }) +/* Call a function once. Similar to DO_ONCE(), but does not use jump label + * patching via static keys. + */ +#define DO_ONCE_LITE(func, ...) \ + DO_ONCE_LITE_IF(true, func, ##__VA_ARGS__) +#define DO_ONCE_LITE_IF(condition, func, ...) \ + ({ \ + static bool __section(".data.once") __already_done; \ + bool __ret_do_once = !!(condition); \ + \ + if (unlikely(__ret_do_once && !__already_done)) { \ + __already_done = true; \ + func(__VA_ARGS__); \ + } \ + unlikely(__ret_do_once); \ + }) #define get_random_once(buf, nbytes) \ DO_ONCE(get_random_bytes, (buf), (nbytes)) #define get_random_once_wait(buf, nbytes) \
--
2.31.1.498.g6c1eba8ee3d-goog