Skip to contentSkip to conversation

Fix bytecount result from printk()

PATCH In review Mark Lord 6 participants 11 messages Dec 1, 2005 – Dec 2, 2005

lore
Share & tools
lore

Mark Lord

author
#0 lore raw

PATCH 1/1 Fix bytecount result from printk()

printk() returns a bytecount, which nothing actually appears to use.

This count is generated internally in vprintk(),
and is off-by-3 for one particular path through that function.

This patch fixes it to be consistent with how it is calculated for the other paths through that same function (vprintk).

Whether or not the count should even exist in the first place is still a question for examination -- nothing appears to use it.

On a related note, WHY does the LOG LEVEL format <6> not get interpreted correctly for the first printk() after an oops report? As in this example -- the <6> is printed, instead of being interpreted:

kernel: <6>note: insmod[31060] exited with preempt_count 1

Here's the off-by-3 fix patch.

kernel/printk.c 2005-12-01 10:01:39.000000000 -0500 renamed from kernel/printk.c.orig 2005-11-29 23:24:19.000000000 -0500 +2 -2
@@ -592,8 +592,8 @@
                                        emit_log_char(default_message_loglevel
                                                + '0');
                                        emit_log_char('>');
-                               }
-                               printed_len += 3;
+                               } else
+                                       printed_len += 3;
                        }
                        log_level_unknown = 0;
                        if (!*p)
kernel/printk.c renamed from kernel/printk.c.orig +2 -2
@@ -592,8 +592,8 @@
 					emit_log_char(default_message_loglevel
 						+ '0');
 					emit_log_char('>');
-				}
-				printed_len += 3;
+				} else
+					printed_len += 3;
 			}
 			log_level_unknown = 0;
 			if (!*p)
#1 lore raw
Quoted 3 lines · Mark Lord

On a related note, WHY does the LOG LEVEL format <6> not get interpreted correctly for the first printk() after an oops report?

It never gets interpreted except at the behinning of a line. Sounds like the oops report perhaps prints a " " without a newline or something at the end, so that the next message after that isn't a new line?

Linus

author
#2 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 2 lines · Mark Lord

On a related note, WHY does the LOG LEVEL format <6> not get interpreted correctly for the first printk() after an oops report?

It never gets interpreted except at the behinning of a line. Sounds like the oops report perhaps prints a " " without a newline or something at the end, so that the next message after that isn't a new line?

The oops report always ends with a simple newline: printk("\n");

#3 lore raw
In reply to Mark Lord
Quoted 1 line · Mark Lord

printk() returns a bytecount, which nothing actually appears to use.

We do check it in a few places.

arch/x86_64/kernel/traps.c:                             i += printk(" "); \
arch/x86_64/kernel/traps.c:                     i += printk(" <%s> ", id);
arch/x86_64/kernel/traps.c:                     i += printk(" <EOE> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <IRQ> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <EOI> ");
drivers/char/mem.c:             ret = printk("%s", tmp);

Dave

#4 lore raw

From: Dave Jones <[email protected]>
Date: Thu, 1 Dec 2005 12:57:32 -0500

Quoted 11 lines · Mark Lord
Quoted 1 line

printk() returns a bytecount, which nothing actually appears to use.

We do check it in a few places.

arch/x86_64/kernel/traps.c:                             i += printk(" "); \
arch/x86_64/kernel/traps.c:                     i += printk(" <%s> ", id);
arch/x86_64/kernel/traps.c:                     i += printk(" <EOE> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <IRQ> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <EOI> ");
drivers/char/mem.c:             ret = printk("%s", tmp);

Wow, that's amazing. :)

I bet these can easily be removed, and since printk() is such a core thing, simplifying it should trump whatever benfits these few call sites have from getting a return byte count.

#5 lore raw
Quoted 8 lines · David S. Miller

From: Dave Jones <[email protected]>
Date: Thu, 1 Dec 2005 12:57:32 -0500

Quoted 5 lines · Mark Lord
Quoted 1 line

printk() returns a bytecount, which nothing actually appears to use.

We do check it in a few places.

Quoted 8 lines
Quoted 6 lines
arch/x86_64/kernel/traps.c:                             i += printk(" "); \
arch/x86_64/kernel/traps.c:                     i += printk(" <%s> ", id);
arch/x86_64/kernel/traps.c:                     i += printk(" <EOE> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <IRQ> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <EOI> ");
drivers/char/mem.c:             ret = printk("%s", tmp);

Wow, that's amazing. :)

Taking the blame.

Quoted 3 lines

I bet these can easily be removed, and since printk() is such a core thing, simplifying it should trump whatever benfits these few call sites have from getting a return byte count.

I used it for linewrapping in the oops output.

Actually I would expect more users from sprintf and snprintf (e.g. common in /proc output to compute the return value of the read) and that is exactly the same code path.

If you do the same grep for sn?printf I bet there will be much more hits.

-Andi

author
#6 lore raw
Quoted 1 line · Andi Kleen

"David S. Miller" <[email protected]> writes:

..

Quoted 9 lines
Quoted 1 line

Wow, that's amazing. :)

Taking the blame.

Quoted 3 lines

I bet these can easily be removed, and since printk() is such a core thing, simplifying it should trump whatever benfits these few call sites have from getting a return byte count.

I used it for linewrapping in the oops output.

...

Quoted 2 lines

Actually I would expect more users from sprintf and snprintf
(e.g. common in /proc output to compute the return value of the read) and that is exactly the same code path.

When I grep the 2.6.15-rc3 kernel tree, the *only* use of vprintk seems to be for doing printk(). It does not seem to be used for the sprintf/snprintf functions. Actually it is the other way around, where vprintk() calls those functions.

So no problem there, and vprintk() really doesn't need to return anything.

Cheers

#7 lore raw
Quoted 6 lines

When I grep the 2.6.15-rc3 kernel tree, the *only* use of vprintk seems to be for doing printk(). It does not seem to be used for the sprintf/snprintf functions. Actually it is the other way around, where vprintk() calls those functions.

So no problem there, and vprintk() really doesn't need to return anything.

That seems wrong. I'm pretty sure we don't have two different printf formatting engines.

-Andi

#8 lore raw
In reply to Dave Jones
Quoted 4 lines · Dave Jones
Quoted 1 line · Mark Lord

printk() returns a bytecount, which nothing actually appears to use.

Quoted 9 lines

We do check it in a few places.

arch/x86_64/kernel/traps.c:                             i += printk(" "); \
arch/x86_64/kernel/traps.c:                     i += printk(" <%s> ", id);
arch/x86_64/kernel/traps.c:                     i += printk(" <EOE> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <IRQ> ");
arch/x86_64/kernel/traps.c:                             i += printk(" <EOI> ");
drivers/char/mem.c:             ret = printk("%s", tmp);

And if you don't fix kmsg_write(), this patch will actually break /dev/kmsg
- userspace complains about the write() return value.

Please review these patches, queued since 2.6.15-rc1-mm1:

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.15-rc3/2.6.15-rc3-mm1/broken-out/printk-return-value-fix-it.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.15-rc3/2.6.15-rc3-mm1/broken-out/kmsg_write-dont-return-printk-return-value.patch

author
#9 lore raw
Quoted 5 lines · Andrew Morton

Please review these patches, queued since 2.6.15-rc1-mm1:

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.15-rc3/2.6.15-rc3-mm1/broken-out/printk-return-value-fix-it.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.15-rc3/2.6.15-rc3-mm1/broken-out/kmsg_write-dont-return-printk-return-value.patch

Yes, those patches actually appear to fix the counting.

The patch I submitted merely makes it internally consistent within printk/vprintk(), but does not change it to actually be correct.

Cheers

#10 lore raw
In reply to Mark Lord

From: Mark Lord <[email protected]>
Date: Thu, 01 Dec 2005 10:55:49 -0500

Quoted 2 lines

Whether or not the count should even exist in the first place is still a question for examination -- nothing appears to use it.

I think it should be killed, in all of my Linux kernel hacking I've never seen a user of this return value. :-)