diff mbox series

[net-next,1/3] once: implement DO_ONCE_LITE for non-fast-path "do once" functionality

Message ID 20210422194738.2175542-2-tannerlove.kernel@gmail.com
State New
Headers show
Series net: update netdev_rx_csum_fault() print dump only once | expand

Commit Message

Tanner Love April 22, 2021, 7:47 p.m. UTC
From: Tanner Love <tannerlove@google.com>

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 <tannerlove@google.com>
Acked-by: Eric Dumazet <edumazet@google.com>
Acked-by: Mahesh Bandewar <maheshb@google.com>
---
 include/linux/once.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff mbox series

Patch

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)                                    \