Skip to contentSkip to conversation

Introduce and use DO_ONCE statement expression macro

PATCH In review Joe Perches 3 participants 8 messages May 22, 2009 x86

lore
Share & tools
lore
#0 lore raw

v2 cover Introduce and use DO_ONCE statement expression macro

The printk_once macro in kernel.h is limited to printk

This generalizes the functionality of printk_once and allows statements like DO_ONCE(pr_info("foo\n")) and DO_ONCE(initialize(foo));

Uses atomic test_and_set_bit

Joe Perches (3):
kernel.h: Add DO_ONCE statement expression macro arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c: Use DO_ONCE & spelling fix kernel.h: Remove unused printk_once

 arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c |    4 ++--
 include/linux/kernel.h                     |   26 +++++++++++---------------
 2 files changed, 13 insertions(+), 17 deletions(-)
#1 lore raw

PATCH v2 1/3 kernel.h: Add DO_ONCE statement expression macro

Add a DO_ONCE statement expression analogous to printk_once that executes any arbitrary statement exactly once.

This will take the place of printk_once so that
DO_ONCE(pr_<foo>) can be more easily written.

---
 include/linux/kernel.h |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
include/linux/kernel.h +11 -0
index 883cd44..179fdac 100644
@@ -637,6 +637,17 @@ static inline void ftrace_dump(void) { }
 #define swap(a, b) \
 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
+/*
+ * Do something once (analogous to WARN_ONCE() et al):
+ */
+#define DO_ONCE(x...) ({			\
+	static unsigned long __done;		\
+						\
+	if (!test_and_set_bit(0, &__done)) {	\
+		x;				\
+	}					\
+})
+
 /**
  * container_of - cast a member of a structure out to the containing structure
  * @ptr:	the pointer to the member.
Signature · 1 line
1.6.3.1.10.g659a0.dirty
#2 lore raw

PATCH v2 2/3 arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c: Use DO_ONCE & spelling fix

In reply to Joe Perches

Allows removal of printk_once.

---
 arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c +2 -2
index 208ecf6..f569cff 100644
@@ -693,8 +693,8 @@ static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
 	if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
 	    policy->cpuinfo.transition_latency > 20 * 1000) {
 		policy->cpuinfo.transition_latency = 20 * 1000;
-			printk_once(KERN_INFO "Capping off P-state tranision"
-				    " latency at 20 uS\n");
+		DO_ONCE(printk(KERN_INFO
+			       "P-state transition latency capped at 20 uS\n"));
 	}
 
 	/* table init */
Signature · 1 line
1.6.3.1.10.g659a0.dirty
#3 lore raw

PATCH v2 3/3 kernel.h: Remove unused printk_once

In reply to Joe Perches
---
 include/linux/kernel.h |   15 ---------------
 1 files changed, 0 insertions(+), 15 deletions(-)
include/linux/kernel.h +0 -15
index 179fdac..46234af 100644
@@ -243,18 +243,6 @@ extern int printk_ratelimit(void);
 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
 				   unsigned int interval_msec);
 
-/*
- * Print a one-time message (analogous to WARN_ONCE() et al):
- */
-#define printk_once(x...) ({			\
-	static int __print_once = 1;		\
-						\
-	if (__print_once) {			\
-		__print_once = 0;		\
-		printk(x);			\
-	}					\
-})
-
 void log_buf_kexec_setup(void);
 #else
 static inline int vprintk(const char *s, va_list args)
@@ -268,9 +256,6 @@ static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
 					  unsigned int interval_msec)	\
 		{ return false; }
 
-/* No effect, but we still get type checking even in the !PRINTK case: */
-#define printk_once(x...) printk(x)
-
 static inline void log_buf_kexec_setup(void)
 {
 }
Signature · 1 line
1.6.3.1.10.g659a0.dirty
#4 lore raw
In reply to Joe Perches
Quoted 5 lines · Joe Perches

The printk_once macro in kernel.h is limited to printk

This generalizes the functionality of printk_once and allows statements like DO_ONCE(pr_info("foo\n")) and DO_ONCE(initialize(foo));

Sigh... Please, don't do introduce new control structures without extremely good reasons.

printk_once() has syntax of function call; it's far more tolerable for casual reader (e.g. somebody looking through the code while trying to localize a bug) since it doesn't interrupt the flow - it parses as "some function call, apparently doing some debugging output, let's see if there are any obvious side effects in the arguments and move on". Your DO_ONCE(....) parses as "what the fuck is that?" followed by grepping for definition, and the cost is much higher.

Don't do that. Staying close to normal syntax is a very good thing; preprocessor can be used in a lot of ways that are harmful and introduction of (effectively) new kinds of statements is a prime example. It *does* have its place, but it should be done very sparingly.

#5 lore raw
Quoted 2 lines · Al Viro

Your DO_ONCE(....) parses as "what the fuck is that?" followed by grepping for definition, and the cost is much higher.

So what do you suggest?

#define pr_info_once(fmt, args...) printk_once(KERN_INFO pr_fmt(fmt), ##args)
#define pr_warning_once(fmt, args...) printk_once(KERN_WARNING pr_fmt(fmt), ##args)
etc

#6 lore raw
Quoted 9 lines · Joe Perches
Quoted 2 lines · Al Viro

Your DO_ONCE(....) parses as "what the fuck is that?" followed by grepping for definition, and the cost is much higher.

So what do you suggest?

#define pr_info_once(fmt, args...) printk_once(KERN_INFO pr_fmt(fmt), ##args)
#define pr_warning_once(fmt, args...) printk_once(KERN_WARNING pr_fmt(fmt), ##args)
etc

That would be much saner.

#7 lore raw
Quoted 5 lines · Al Viro
Quoted 4 lines · Joe Perches
Quoted 2 lines · Al Viro

Your DO_ONCE(....) parses as "what the fuck is that?" followed by grepping for definition, and the cost is much higher.

Quoted 7 lines
Quoted 5 lines

So what do you suggest?

#define pr_info_once(fmt, args...) printk_once(KERN_INFO pr_fmt(fmt), ##args)
#define pr_warning_once(fmt, args...) printk_once(KERN_WARNING pr_fmt(fmt), ##args)
etc

That would be much saner.

Agreed.

The *_once() namespace meme is intuitive and easily understood, and we use it in a couple of places in the kernel and extend it on an as-needed basis for reoccuring (and boring and distracting) once-flag C code spam. We apply it to 'boring to begin with'
constructs: printing a message or a warning, etc. So if a kernel coder sees a _once() or _ONCE() construct it can be assumed almost straight away that the code there is largely uninteresting from a code logic POV.

DO_ONCE() on the other hand is non-intuitive as it is a control structure that can be applied to _any_ code construct - interesting and uninteresting alike. For anything truly interesting that is not a kernel library/facility i dont want it to be hidden and abstracted away in 98% of the cases, i want to see the raw C form of it.

Otherwise we might as well write the kernel in C++.

Ingo