Skip to contentSkip to conversation

printk: Use a non-printable ASCII SOH for KERN_<LEVEL>

RFC PATCH New Joe Perches 1 participant 4 messages May 24, 2012

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

cover printk: Use a non-printable ASCII SOH for KERN_<LEVEL>

printk uses "<.>" as a start of record indicator. This can waste a bit of space and causes a bit of contortion to get a printk emitted that happens to starts with "<.>".

Use ASCII Start-of-Header (0x01) as the KERN_LEVEL initiator and save ~10K in an x86 defconfig.

Joe Perches (3):
printk: Use a non-printable ASCII KERN_<LEVEL> initiator
treewide: Fix declaration and uses of old KERN_<LEVEL> style
printk: Set KERN_CONT to ""

 arch/arm/lib/io-acorn.S              |    4 +++-
 arch/arm/vfp/vfphw.S                 |    8 +++++---
 arch/frv/kernel/kernel_thread.S      |    2 +-
 drivers/staging/wlags49_h2/hcf.c     |    8 ++++----
 drivers/staging/wlags49_h2/wl_main.c |    4 ++--
 fs/btrfs/super.c                     |    9 +++++----
 include/linux/printk.h               |   23 +++++++++++++----------
 kernel/printk.c                      |    6 +++---
 net/bridge/netfilter/ebt_log.c       |    2 +-
 sound/core/misc.c                    |    8 ++++----
 10 files changed, 41 insertions(+), 33 deletions(-)
Signature · 1 line
1.7.8.111.gad25c.dirty
#1 lore raw

RFC PATCH 1/3 printk: Use a non-printable ASCII KERN_<LEVEL> initiator

printk uses <.> to denote the message level. There are thousands of messages in default kernel.

Use ASCII SOH (hex: 01) with no terminator to save 1 byte per message.

This also means that KERN_CONT could be completely removed (#defined to "") as it is no longer serves any real purpose.

---
 include/linux/printk.h |   23 +++++++++++++----------
 kernel/printk.c        |    6 +++---
 2 files changed, 16 insertions(+), 13 deletions(-)
include/linux/printk.h +13 -10
index 1bec2f7..ad76855 100644
@@ -6,23 +6,26 @@
 extern const char linux_banner[];
 extern const char linux_proc_banner[];
 
-#define KERN_EMERG	"<0>"	/* system is unusable			*/
-#define KERN_ALERT	"<1>"	/* action must be taken immediately	*/
-#define KERN_CRIT	"<2>"	/* critical conditions			*/
-#define KERN_ERR	"<3>"	/* error conditions			*/
-#define KERN_WARNING	"<4>"	/* warning conditions			*/
-#define KERN_NOTICE	"<5>"	/* normal but significant condition	*/
-#define KERN_INFO	"<6>"	/* informational			*/
-#define KERN_DEBUG	"<7>"	/* debug-level messages			*/
+#define KERN_SOH	"\001"		/* ASCII Start Of Header */
+#define KERN_SOH_ASCII	'\001'
+
+#define KERN_EMERG	KERN_SOH "0"	/* system is unusable */
+#define KERN_ALERT	KERN_SOH "1"	/* action must be taken immediately */
+#define KERN_CRIT	KERN_SOH "2"	/* critical conditions */
+#define KERN_ERR	KERN_SOH "3"	/* error conditions */
+#define KERN_WARNING	KERN_SOH "4"	/* warning conditions */
+#define KERN_NOTICE	KERN_SOH "5"	/* normal but significant condition */
+#define KERN_INFO	KERN_SOH "6"	/* informational */
+#define KERN_DEBUG	KERN_SOH "7"	/* debug-level messages */
 
 /* Use the default kernel loglevel */
-#define KERN_DEFAULT	"<d>"
+#define KERN_DEFAULT	KERN_SOH "d"
 /*
  * Annotation for a "continued" line of log printout (only done after a
  * line that had no enclosing \n). Only to be used by core/arch code
  * during early bootup (a continued line is not SMP-safe otherwise).
  */
-#define KERN_CONT	"<c>"
+#define KERN_CONT	KERN_SOH "c"
 
 extern int console_printk[];
 
kernel/printk.c +3 -3
index 32462d2..e96e733 100644
@@ -1329,7 +1329,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 	}
 
 	/* strip syslog prefix and extract log level or control flags */
-	if (text[0] == '<' && text[1] && text[2] == '>') {
+	if (text[0] == KERN_SOH_ASCII && text[1]) {
 		switch (text[1]) {
 		case '0' ... '7':
 			if (level == -1)
@@ -1337,8 +1337,8 @@ asmlinkage int vprintk_emit(int facility, int level,
 		case 'd':	/* KERN_DEFAULT */
 			prefix = true;
 		case 'c':	/* KERN_CONT */
-			text += 3;
-			text_len -= 3;
+			text += 2;
+			text_len -= 2;
 		}
 	}
 
Signature · 1 line
1.7.8.111.gad25c.dirty
#2 lore raw

RFC PATCH 2/3 treewide: Fix declaration and uses of old KERN_<LEVEL> style

In reply to Joe Perches

Now that KERN_<LEVEL> is no longer "<.>",
some code that relied on that form needed to
be updated.

Replace direct declarations of <.> and fix
the parsing of <.> to use the new KERN_SOH
style.

---
 arch/arm/lib/io-acorn.S              |    4 +++-
 arch/arm/vfp/vfphw.S                 |    8 +++++---
 arch/frv/kernel/kernel_thread.S      |    2 +-
 drivers/staging/wlags49_h2/hcf.c     |    8 ++++----
 drivers/staging/wlags49_h2/wl_main.c |    4 ++--
 fs/btrfs/super.c                     |    9 +++++----
 net/bridge/netfilter/ebt_log.c       |    2 +-
 sound/core/misc.c                    |    8 ++++----
 8 files changed, 25 insertions(+), 20 deletions(-)
arch/arm/lib/io-acorn.S +3 -1
index 1b197ea..6b5b6ee 100644
@@ -13,11 +13,13 @@
 #include <linux/linkage.h>
 #include <asm/assembler.h>
 
+#define	KERN_WARNING	"\001" "4"
+
 		.text
 		.align
 
 .Liosl_warning:
-		.ascii	"<4>insl/outsl not implemented, called from %08lX\0"
+		.ascii	KERN_WARNING "insl/outsl not implemented, called from %08lX\0"
 		.align
 
 /*
arch/arm/vfp/vfphw.S +5 -3
index 2d30c7f..640f25e 100644
@@ -18,13 +18,15 @@
 #include <asm/vfpmacros.h>
 #include "../kernel/entry-header.S"
 
+#define KERN_DEBUG "\001" "7"
+
 	.macro	DBGSTR, str
 #ifdef DEBUG
 	stmfd	sp!, {r0-r3, ip, lr}
 	add	r0, pc, #4
 	bl	printk
 	b	1f
-	.asciz  "<7>VFP: \str\n"
+	.asciz  KERN_DEBUG "VFP: \str\n"
 	.balign 4
 1:	ldmfd	sp!, {r0-r3, ip, lr}
 #endif
@@ -37,7 +39,7 @@
 	add	r0, pc, #4
 	bl	printk
 	b	1f
-	.asciz  "<7>VFP: \str\n"
+	.asciz  KERN_DEBUG "VFP: \str\n"
 	.balign 4
 1:	ldmfd	sp!, {r0-r3, ip, lr}
 #endif
@@ -52,7 +54,7 @@
 	add	r0, pc, #4
 	bl	printk
 	b	1f
-	.asciz  "<7>VFP: \str\n"
+	.asciz  KERN_DEBUG "VFP: \str\n"
 	.balign 4
 1:	ldmfd	sp!, {r0-r3, ip, lr}
 #endif
arch/frv/kernel/kernel_thread.S +1 -1
index 4531c83..a3b69e9 100644
@@ -13,7 +13,7 @@
 #include <asm/unistd.h>
 
 #define CLONE_VM	0x00000100	/* set if VM shared between processes */
-#define	KERN_ERR	"<3>"
+#define	KERN_ERR	"\001" "3"
 
 	.section .rodata
 kernel_thread_emsg:
drivers/staging/wlags49_h2/hcf.c +4 -4
index 366e4a4..4235446 100644
@@ -705,7 +705,7 @@ hcf_action( IFBP ifbp, hcf_16 action )
 			// 800 us latency before FW switches to high power
 			MSF_WAIT(800);                              // MSF-defined function to wait n microseconds.
 //OOR			if ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_DS_OOR ) { // OutOfRange
-//				printk( "<5>ACT_INT_OFF: Deepsleep phase terminated, enable and go to AwaitConnection\n" );     //;?remove me 1 day
+//				printk(KERN_NOTICE "ACT_INT_OFF: Deepsleep phase terminated, enable and go to AwaitConnection\n" );     //;?remove me 1 day
 //				hcf_cntl( ifbp, HCF_CNTL_ENABLE );
 //			}
 //			ifbp->IFB_DSLinkStat &= ~( CFG_LINK_STAT_DS_IR | CFG_LINK_STAT_DS_OOR); //clear IR/OOR state
@@ -2979,7 +2979,7 @@ hcf_service_nic( IFBP ifbp, wci_bufp bufp, unsigned int len )
 			ltv.typ = CFG_DDS_TICK_TIME;
 			ltv.tick_time = ( ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_TIMER ) + 0x10 ) *64; //78 is more right
 			hcf_put_info( ifbp, (LTVP)&ltv );
-			printk( "<5>Preparing for sleep, link_status: %04X, timer : %d\n",
+			printk(KERN_NOTICE "Preparing for sleep, link_status: %04X, timer : %d\n",
 				ifbp->IFB_DSLinkStat, ltv.tick_time );//;?remove me 1 day
 			ifbp->IFB_TickCnt++; //;?just to make sure we do not keep on printing above message
 			if ( ltv.tick_time < 300 * 125 ) ifbp->IFB_DSLinkStat += 0x0010;
@@ -4221,11 +4221,11 @@ isr_info( IFBP ifbp )
 // /*4*/    if ( info[1] == CFG_LINK_STAT ) {
 //          ifbp->IFB_DSLinkStat = IPW( HREG_DATA_1 ) | CFG_LINK_STAT_CHANGE;   //corrupts BAP !! ;?
 //          ifbp->IFB_LinkStat = ifbp->IFB_DSLinkStat & CFG_LINK_STAT_FW; //;? to be obsoleted
-//          printk( "<4>linkstatus: %04x\n", ifbp->IFB_DSLinkStat );        //;?remove me 1 day
+//          printk(KERN_ERR "linkstatus: %04x\n", ifbp->IFB_DSLinkStat );        //;?remove me 1 day
 // #if (HCF_SLEEP) & HCF_DDS
 //          if ( ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_CONNECTED ) == 0 ) {    //even values are disconnected etc.
 //              ifbp->IFB_TickCnt = 0;              //start 2 second period (with 1 tick uncertanty)
-//              printk( "<5>isr_info: AwaitConnection phase started, IFB_TickCnt = 0\n" );      //;?remove me 1 day
+//              printk(KERN_NOTICE "isr_info: AwaitConnection phase started, IFB_TickCnt = 0\n" );      //;?remove me 1 day
 //          }
 // #endif // HCF_DDS
 //      }
drivers/staging/wlags49_h2/wl_main.c +2 -2
index d5bf0a7..2041078 100644
@@ -3822,7 +3822,7 @@ static int write_int(struct file *file, const char *buffer, unsigned long count,
 		lp->timer_oor.data = (unsigned long)lp;
 		lp->timer_oor.expires = RUN_AT( 3 * HZ );
 		add_timer( &lp->timer_oor );
-		printk( "<5>wl_enable: %ld\n", jiffies );		//;?remove me 1 day
+		printk(KERN_NOTICE "wl_enable: %ld\n", jiffies );		//;?remove me 1 day
 #endif //DN554
 #ifdef DN554
 /*******************************************************************************
@@ -3852,7 +3852,7 @@ void timer_oor( u_long arg )
     DBG_ENTER( DbgInfo );
     DBG_PARAM( DbgInfo, "arg", "0x%08lx", arg );
 
-	printk( "<5>timer_oor: %ld 0x%04X\n", jiffies, lp->timer_oor_cnt );		//;?remove me 1 day
+	printk(KERN_NOTICE "timer_oor: %ld 0x%04X\n", jiffies, lp->timer_oor_cnt );		//;?remove me 1 day
 	lp->timer_oor_cnt += 10;
     if ( (lp->timer_oor_cnt & ~DS_OOR) > 300 ) {
 		lp->timer_oor_cnt = 300;
fs/btrfs/super.c +5 -4
index c5f8fca..daf2e47 100644
@@ -180,16 +180,17 @@ const char *logtypes[] = {
 void btrfs_printk(struct btrfs_fs_info *fs_info, const char *fmt, ...)
 {
 	struct super_block *sb = fs_info->sb;
-	char lvl[4];
+	char lvl[3];
 	struct va_format vaf;
 	va_list args;
 	const char *type = logtypes[4];
 
 	va_start(args, fmt);
 
-	if (fmt[0] == '<' && isdigit(fmt[1]) && fmt[2] == '>') {
-		strncpy(lvl, fmt, 3);
-		fmt += 3;
+	if (fmt[0] == KERN_SOH_ASCII && isdigit(fmt[1])) {
+		lvl[0] = fmt[0];
+		lvl[1] = fmt[1];
+		lvl[2] = 0;
 		type = logtypes[fmt[1] - '0'];
 	} else
 		*lvl = '\0';
net/bridge/netfilter/ebt_log.c +1 -1
index f88ee53..31286d3 100644
@@ -80,7 +80,7 @@ ebt_log_packet(u_int8_t pf, unsigned int hooknum,
 	unsigned int bitmask;
 
 	spin_lock_bh(&ebt_log_lock);
-	printk("<%c>%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
+	printk(KERN_SOH "%c" "%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
 	       '0' + loginfo->u.log.level, prefix,
 	       in ? in->name : "", out ? out->name : "",
 	       eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
sound/core/misc.c +4 -4
index 7681679..4e47cd8 100644
@@ -81,11 +81,11 @@ void __snd_printk(unsigned int level, const char *path, int line,
 #ifdef CONFIG_SND_VERBOSE_PRINTK
 	vaf.fmt = format;
 	vaf.va = &args;
-	if (format[0] == '<' && format[2] == '>') {
-		memcpy(verbose_fmt, format, 3);
-		vaf.fmt = format + 3;
+	if (format[0] == KERN_SOH_ASCII) {
+		memcpy(verbose_fmt, format, 2);
+		vaf.fmt = format + 2;
 	} else if (level)
-		memcpy(verbose_fmt, KERN_DEBUG, 3);
+		memcpy(verbose_fmt, KERN_DEBUG, 2);
 	printk(verbose_fmt, sanity_file_name(path), line, &vaf);
 #else
 	vprintk(format, args);
Signature · 1 line
1.7.8.111.gad25c.dirty
#3 lore raw

RFC PATCH 3/3 printk: Set KERN_CONT to ""

In reply to Joe Perches

Logging content is only printable ASCII.

Now that all KERN_<LEVEL> uses are specified by ASCII SOH, KERN_CONT doesn't have any real purpose.

---
 include/linux/printk.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/printk.h +1 -1
index ad76855..482121b 100644
@@ -25,7 +25,7 @@ extern const char linux_proc_banner[];
  * line that had no enclosing \n). Only to be used by core/arch code
  * during early bootup (a continued line is not SMP-safe otherwise).
  */
-#define KERN_CONT	KERN_SOH "c"
+#define KERN_CONT	""
 
 extern int console_printk[];
 
Signature · 1 line
1.7.8.111.gad25c.dirty