Skip to content

Commit 874f9c7

Browse files
JoePerchestorvalds
authored andcommitted
printk: create pr_<level> functions
Using functions instead of macros can reduce overall code size by eliminating unnecessary "KERN_SOH<digit>" prefixes from format strings. defconfig x86-64: $ size vmlinux* text data bss dec hex filename 10193570 4331464 1105920 15630954 ee826a vmlinux.new 10192623 4335560 1105920 1563410 ee8eb7 vmlinux.old As the return value are unimportant and unused in the kernel tree, these new functions return void. Miscellanea: - change pr_<level> macros to call new __pr_<level> functions - change vprintk_nmi and vprintk_default to add LOGLEVEL_<level> argument [akpm@linux-foundation.org: fix LOGLEVEL_INFO, per Joe] Link: http://lkml.kernel.org/r/e16cc34479dfefcae37c98b481e6646f0f69efc3.1466718827.git.joe@perches.com Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bebca05 commit 874f9c7

File tree

4 files changed

+78
-26
lines changed

4 files changed

+78
-26
lines changed

include/linux/printk.h

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,39 @@ extern asmlinkage void dump_stack(void) __cold;
257257
* and other debug macros are compiled out unless either DEBUG is defined
258258
* or CONFIG_DYNAMIC_DEBUG is set.
259259
*/
260-
#define pr_emerg(fmt, ...) \
261-
printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
262-
#define pr_alert(fmt, ...) \
263-
printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
264-
#define pr_crit(fmt, ...) \
265-
printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
266-
#define pr_err(fmt, ...) \
267-
printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
268-
#define pr_warning(fmt, ...) \
269-
printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
270-
#define pr_warn pr_warning
271-
#define pr_notice(fmt, ...) \
272-
printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
273-
#define pr_info(fmt, ...) \
274-
printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
260+
261+
#ifdef CONFIG_PRINTK
262+
263+
asmlinkage __printf(1, 2) __cold void __pr_emerg(const char *fmt, ...);
264+
asmlinkage __printf(1, 2) __cold void __pr_alert(const char *fmt, ...);
265+
asmlinkage __printf(1, 2) __cold void __pr_crit(const char *fmt, ...);
266+
asmlinkage __printf(1, 2) __cold void __pr_err(const char *fmt, ...);
267+
asmlinkage __printf(1, 2) __cold void __pr_warn(const char *fmt, ...);
268+
asmlinkage __printf(1, 2) __cold void __pr_notice(const char *fmt, ...);
269+
asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...);
270+
271+
#define pr_emerg(fmt, ...) __pr_emerg(pr_fmt(fmt), ##__VA_ARGS__)
272+
#define pr_alert(fmt, ...) __pr_alert(pr_fmt(fmt), ##__VA_ARGS__)
273+
#define pr_crit(fmt, ...) __pr_crit(pr_fmt(fmt), ##__VA_ARGS__)
274+
#define pr_err(fmt, ...) __pr_err(pr_fmt(fmt), ##__VA_ARGS__)
275+
#define pr_warn(fmt, ...) __pr_warn(pr_fmt(fmt), ##__VA_ARGS__)
276+
#define pr_notice(fmt, ...) __pr_notice(pr_fmt(fmt), ##__VA_ARGS__)
277+
#define pr_info(fmt, ...) __pr_info(pr_fmt(fmt), ##__VA_ARGS__)
278+
279+
#else
280+
281+
#define pr_emerg(fmt, ...) printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
282+
#define pr_alert(fmt, ...) printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
283+
#define pr_crit(fmt, ...) printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
284+
#define pr_err(fmt, ...) printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
285+
#define pr_warn(fmt, ...) printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
286+
#define pr_notice(fmt, ...) printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
287+
#define pr_info(fmt, ...) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
288+
289+
#endif
290+
291+
#define pr_warning pr_warn
292+
275293
/*
276294
* Like KERN_CONT, pr_cont() should only be used when continuing
277295
* a line with no newline ('\n') enclosed. Otherwise it defaults

kernel/printk/internal.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717
#include <linux/percpu.h>
1818

19-
typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
19+
typedef __printf(2, 0) int (*printk_func_t)(int level, const char *fmt,
20+
va_list args);
2021

21-
int __printf(1, 0) vprintk_default(const char *fmt, va_list args);
22+
__printf(2, 0)
23+
int vprintk_default(int level, const char *fmt, va_list args);
2224

2325
#ifdef CONFIG_PRINTK_NMI
2426

@@ -31,9 +33,10 @@ extern raw_spinlock_t logbuf_lock;
3133
* via per-CPU variable.
3234
*/
3335
DECLARE_PER_CPU(printk_func_t, printk_func);
34-
static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
36+
__printf(2, 0)
37+
static inline int vprintk_func(int level, const char *fmt, va_list args)
3538
{
36-
return this_cpu_read(printk_func)(fmt, args);
39+
return this_cpu_read(printk_func)(level, fmt, args);
3740
}
3841

3942
extern atomic_t nmi_message_lost;
@@ -44,9 +47,10 @@ static inline int get_nmi_message_lost(void)
4447

4548
#else /* CONFIG_PRINTK_NMI */
4649

47-
static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
50+
__printf(2, 0)
51+
static inline int vprintk_func(int level, const char *fmt, va_list args)
4852
{
49-
return vprintk_default(fmt, args);
53+
return vprintk_default(level, fmt, args);
5054
}
5155

5256
static inline int get_nmi_message_lost(void)

kernel/printk/nmi.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static DEFINE_PER_CPU(struct nmi_seq_buf, nmi_print_seq);
5858
* one writer running. But the buffer might get flushed from another
5959
* CPU, so we need to be careful.
6060
*/
61-
static int vprintk_nmi(const char *fmt, va_list args)
61+
static int vprintk_nmi(int level, const char *fmt, va_list args)
6262
{
6363
struct nmi_seq_buf *s = this_cpu_ptr(&nmi_print_seq);
6464
int add = 0;
@@ -79,7 +79,16 @@ static int vprintk_nmi(const char *fmt, va_list args)
7979
if (!len)
8080
smp_rmb();
8181

82-
add = vsnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, args);
82+
if (level != LOGLEVEL_DEFAULT) {
83+
add = snprintf(s->buffer + len, sizeof(s->buffer) - len,
84+
KERN_SOH "%c", '0' + level);
85+
add += vsnprintf(s->buffer + len + add,
86+
sizeof(s->buffer) - len - add,
87+
fmt, args);
88+
} else {
89+
add = vsnprintf(s->buffer + len, sizeof(s->buffer) - len,
90+
fmt, args);
91+
}
8392

8493
/*
8594
* Do it once again if the buffer has been flushed in the meantime.

kernel/printk/printk.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,28 @@ asmlinkage int printk_emit(int facility, int level,
18011801
}
18021802
EXPORT_SYMBOL(printk_emit);
18031803

1804-
int vprintk_default(const char *fmt, va_list args)
1804+
#ifdef CONFIG_PRINTK
1805+
#define define_pr_level(func, loglevel) \
1806+
asmlinkage __visible void func(const char *fmt, ...) \
1807+
{ \
1808+
va_list args; \
1809+
\
1810+
va_start(args, fmt); \
1811+
vprintk_default(loglevel, fmt, args); \
1812+
va_end(args); \
1813+
} \
1814+
EXPORT_SYMBOL(func)
1815+
1816+
define_pr_level(__pr_emerg, LOGLEVEL_EMERG);
1817+
define_pr_level(__pr_alert, LOGLEVEL_ALERT);
1818+
define_pr_level(__pr_crit, LOGLEVEL_CRIT);
1819+
define_pr_level(__pr_err, LOGLEVEL_ERR);
1820+
define_pr_level(__pr_warn, LOGLEVEL_WARNING);
1821+
define_pr_level(__pr_notice, LOGLEVEL_NOTICE);
1822+
define_pr_level(__pr_info, LOGLEVEL_INFO);
1823+
#endif
1824+
1825+
int vprintk_default(int level, const char *fmt, va_list args)
18051826
{
18061827
int r;
18071828

@@ -1811,7 +1832,7 @@ int vprintk_default(const char *fmt, va_list args)
18111832
return r;
18121833
}
18131834
#endif
1814-
r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
1835+
r = vprintk_emit(0, level, NULL, 0, fmt, args);
18151836

18161837
return r;
18171838
}
@@ -1844,7 +1865,7 @@ asmlinkage __visible int printk(const char *fmt, ...)
18441865
int r;
18451866

18461867
va_start(args, fmt);
1847-
r = vprintk_func(fmt, args);
1868+
r = vprintk_func(LOGLEVEL_DEFAULT, fmt, args);
18481869
va_end(args);
18491870

18501871
return r;

0 commit comments

Comments
 (0)