All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tanner Love <tannerlove.kernel@gmail.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, Tanner Love <tannerlove@google.com>,
	Eric Dumazet <edumazet@google.com>,
	Mahesh Bandewar <maheshb@google.com>
Subject: [PATCH net-next 1/3] once: implement DO_ONCE_LITE for non-fast-path "do once" functionality
Date: Thu, 22 Apr 2021 15:47:36 -0400	[thread overview]
Message-ID: <20210422194738.2175542-2-tannerlove.kernel@gmail.com> (raw)
In-Reply-To: <20210422194738.2175542-1-tannerlove.kernel@gmail.com>

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


  reply	other threads:[~2021-04-22 19:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22 19:47 [PATCH net-next 0/3] net: update netdev_rx_csum_fault() print dump only once Tanner Love
2021-04-22 19:47 ` Tanner Love [this message]
2021-04-22 19:47 ` [PATCH net-next 2/3] once: replace uses of __section(".data.once") with DO_ONCE_LITE(_IF)? Tanner Love
2021-04-22 21:23   ` kernel test robot
2021-04-22 21:23     ` kernel test robot
2021-04-22 22:28   ` kernel test robot
2021-04-22 22:28     ` kernel test robot
2021-04-22 22:28   ` kernel test robot
2021-04-22 22:28     ` kernel test robot
2021-04-22 19:47 ` [PATCH net-next 3/3] net: update netdev_rx_csum_fault() print dump only once Tanner Love
2021-04-23  0:42   ` Yunsheng Lin

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=20210422194738.2175542-2-tannerlove.kernel@gmail.com \
    --to=tannerlove.kernel@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=maheshb@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=tannerlove@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.