Skip to contentSkip to conversation

printk: Convert pr_<level> macros to functions

RFC PATCH In review Joe Perches 2 participants 6 messages Mar 3, 2010 – Mar 4, 2010

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 printk: Convert pr_<level> macros to functions

printk is defined asmlinkage (extern "C")
For this RFC patch, pr_<level> calls are as well. Is this declaration style really necessary? Maybe moving printed_len to file scope is racy somehow?

Save 3 bytes per pr_<level> use after printk overhead

Does not store the KERN_<level> string as constant string when using pr_<level> calls

printk.o increases ~200 bytes
defconfig decreases ~700 bytes

Minor printk neatening, moving comments above function declaration Renamed vprint to __vprintk, added vprintk wrapper Moved automatic printed_len to file static
Moved EXPORT_SYMBOL after functions

---
 include/linux/kernel.h |   57 ++++++++++++--
 kernel/printk.c        |  195 +++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 206 insertions(+), 46 deletions(-)
include/linux/kernel.h +48 -9
index 1221d23..afb7b03 100644
@@ -240,6 +240,22 @@ asmlinkage int vprintk(const char *fmt, va_list args)
 	__attribute__ ((format (printf, 1, 0)));
 asmlinkage int printk(const char * fmt, ...)
 	__attribute__ ((format (printf, 1, 2))) __cold;
+asmlinkage int pr_emerg(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_crit(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_alert(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_err(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_warning(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_notice(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_info(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+asmlinkage int pr_cont(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
 
 extern int __printk_ratelimit(const char *func);
 #define printk_ratelimit() __printk_ratelimit(__func__)
@@ -268,6 +284,31 @@ static inline int vprintk(const char *s, va_list args) { return 0; }
 static inline int printk(const char *s, ...)
 	__attribute__ ((format (printf, 1, 2)));
 static inline int __cold printk(const char *s, ...) { return 0; }
+static inline int pr_emerg(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_emerg(const char *s, ...) { return 0; }
+static inline int pr_crit(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_crit(const char *s, ...) { return 0; }
+static inline int pr_alert(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_alert(const char *s, ...) { return 0; }
+static inline int pr_err(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_err(const char *s, ...) { return 0; }
+static inline int pr_warning(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_warning(const char *s, ...) { return 0; }
+static inline int pr_notice(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_notice(const char *s, ...) { return 0; }
+static inline int pr_info(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_info(const char *s, ...) { return 0; }
+static inline int pr_cont(const char *s, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static inline int pr_cont(const char *s, ...) { return 0; }
+
 static inline int printk_ratelimit(void) { return 0; }
 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
 					  unsigned int interval_msec)	\
@@ -367,21 +408,19 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 #endif
 
 #define pr_emerg(fmt, ...) \
-        printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
+	pr_emerg(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_alert(fmt, ...) \
-        printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
+	pr_alert(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_crit(fmt, ...) \
-        printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
+	pr_crit(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_err(fmt, ...) \
-        printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
+	pr_err(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_warning(fmt, ...) \
-        printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
+	pr_warning(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_notice(fmt, ...) \
-        printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
+	pr_notice(pr_fmt(fmt), ##__VA_ARGS__)
 #define pr_info(fmt, ...) \
-        printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
-#define pr_cont(fmt, ...) \
-	printk(KERN_CONT fmt, ##__VA_ARGS__)
+	pr_info(pr_fmt(fmt), ##__VA_ARGS__)
 
 /* pr_devel() should produce zero code unless DEBUG is defined */
 #ifdef DEBUG
kernel/printk.c +158 -37

222 lines not on this page · raw message

Signature · 1 line
1.7.0.14.g7e948
#1 lore raw
Quoted 4 lines · On Wed, 03 Mar 2010 07:20:18 -0800 Joe Perches <[email protected]> wrote:

printk is defined asmlinkage (extern "C")
For this RFC patch, pr_<level> calls are as well. Is this declaration style really necessary? Maybe moving printed_len to file scope is racy somehow?

Yes, printed_len will get corrupted when multiple CPU's are running printk/vprintk simultaneously. That'll need to be fixed.

Quoted 5 lines

Save 3 bytes per pr_<level> use after printk overhead

Does not store the KERN_<level> string as constant string when using pr_<level> calls

Quoted 8 lines

printk.o increases ~200 bytes
defconfig decreases ~700 bytes

Minor printk neatening, moving comments above function declaration Renamed vprint to __vprintk, added vprintk wrapper Moved automatic printed_len to file static
Moved EXPORT_SYMBOL after functions

Looks good at the first peek. Is it possible to reduce the amount of code movement in the patch, make it a bit easier to follow? Maybe do it as two patches, with the move-stuff-around patch being [1/2]?

Quoted 97 lines

...

kernel/printk.c +94 -0
+asmlinkage int pr_emerg(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('0', fmt, args);
+	r = __vprintk(fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_emerg);
+
+asmlinkage int pr_alert(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('1', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_alert);
+
+asmlinkage int pr_crit(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('2', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_crit);
+
+asmlinkage int pr_err(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('3', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_err);
+
+asmlinkage int pr_warning(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('4', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_warning);
+
+asmlinkage int pr_notice(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('5', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_notice);
+
+asmlinkage int pr_info(const char *fmt, ...)
+{
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('6', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_info);
+
+asmlinkage int pr_cont(const char *fmt, ...)
+{

kernel/printk.c out_restore_irqs: · line 925

Quoted 10 lines
kernel/printk.c +10 -0
+	va_list args;
+	int r;
+
+	va_start(args, fmt);
+	r = pr_printk('c', fmt, args);
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(pr_cont);

I think it would be justifiable to cook up a freaky macro and expand it eight times to avoid this duplication. Ugly, but better than lots of duplication.

Or perhaps we can do it via a helper function which takes the additional argument?

asmlinkage int pr_everything(char levelchar, const char *fmt, ...)

?

#2 lore raw
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Wed, 03 Mar 2010 07:20:18 -0800 Joe Perches <[email protected]> wrote:

Maybe moving printed_len to file scope is racy somehow?

Yes, printed_len will get corrupted when multiple CPU's are running printk/vprintk simultaneously. That'll need to be fixed.

I think it'll be easier to use the %pV construct that I posted today:

http://lkml.org/lkml/2010/3/4/19

Quoted 2 lines

Is it possible to reduce the amount of
code movement in the patch, make it a bit easier to follow?

I can do the move-around separately.

I think it'll be better as:

asmlinkage int pr_emerg(const char *fmt, ...)

{
	struct va_format vaf;
	va_list args;

	va_start(args, fmt);
	vaf.fmt = fmt;
	vaf.va = &args;
	r = printk(KERN_EMERG "%pV", &vaf);
	va_end(args);

	return r;
}

EXPORT_SYMBOL(pr_emerg);

I believe that avoids any racing.

Quoted 3 lines

I think it would be justifiable to cook up a freaky macro and expand it eight times to avoid this duplication. Ugly, but better than lots of duplication.

Maybe something like:

#define define_pr_level(function, level) \
asmlinkage int function(const char *fmt, ...) \

{						\
	struct va_format vaf;			\
	va_list args;				\
						\
	va_start(args, fmt);			\
	vaf.fmt = fmt;				\
	vaf.va = &args;				\
	r = printk(level "%pV", &vaf);		\
	va_end(args);				\
						\
	return r;				\
}						\

EXPORT_SYMBOL(function)

define_pr_level(pr_emerg, KERN_EMERG);
define_pr_level(pr_alert, KERN_ALERT);
define_pr_level(pr_crit, KERN_CRIT);
define_pr_level(pr_err, KERN_ERR);
define_pr_level(pr_warning, KERN_WARNING);
define_pr_level(pr_notice, KERN_NOTICE);
define_pr_level(pr_info, KERN_INFO);

I think that's a bit ugly though myself.

Quoted 3 lines

Or perhaps we can do it via a helper function which takes the additional argument?
asmlinkage int pr_everything(char levelchar, const char *fmt, ...)

I believe that could not work.
The symbol names to link against wouldn't be found.

#3 lore raw
Quoted 6 lines · On Thu, 04 Mar 2010 13:29:52 -0800 Joe Perches <[email protected]> wrote:
Quoted 3 lines

Or perhaps we can do it via a helper function which takes the additional argument?
asmlinkage int pr_everything(char levelchar, const char *fmt, ...)

I believe that could not work.
The symbol names to link against wouldn't be found.

Obviously one would need eight little functions, each of which calls pr_everything().

#4 lore raw
Quoted 2 lines · Andrew Morton

Obviously one would need eight little functions, each of which calls pr_everything().

Those little function still need to individually use va_list so it's just as easy to call printk directly as long as %pV is OK.

What do you think of the %pV use?

#5 lore raw
Quoted 1 line · On Thu, 04 Mar 2010 13:40:26 -0800 Joe Perches <[email protected]> wrote:

What do you think of the %pV use?

I don't know what it all ends up looking like.