3

I'm writing a driver and usually I implement my own log-level scheme. However, this time I figured that I would try to utilize the printk scheme that is built into Linux.

I see that I can adjust the printk log-level settings displayed by

[sri@localhost ~]$ sudo cat /proc/sys/kernel/printk
4   4   1   7

I can set them by running

dmesg -n 3

or

[sri@localhost ~]$ sudo bash -c 'echo 3 > /proc/sys/kernel/printk'
[sri@localhost ~]$ sudo cat /proc/sys/kernel/printk
3   4   1   7

In the my driver code I have

printk(KERN_EMERG   "KERN_EMERG");
printk(KERN_ALERT   "KERN_ALERT");
printk(KERN_CRIT    "KERN_CRIT");
printk(KERN_ERR     "KERN_ERR");
printk(KERN_WARNING "KERN_WARNING");
printk(KERN_NOTICE  "KERN_NOTICE");
printk(KERN_INFO    "KERN_INFO");
printk(KERN_DEBUG   "KERN_DEBUG");

which results in

[sri@localhost scull]$ sudo dmesg | grep KERN
[ 3072.247079] KERN_EMERG
[ 3072.247084] KERN_ALERT
[ 3072.247087] KERN_CRIT
[ 3072.247089] KERN_ERR
[ 3072.247092] KERN_WARNING
[ 3072.247093] KERN_NOTICE
[ 3072.247096] KERN_INFO
[ 3072.247097] KERN_DEBUG

So, despite the log-level being 3 (aka KERN_ERR) WARNING, NOTICE, INFO, and DEBUG are showing up in the log (dmesg and /var/log/messages).

I am beginning to suspect that /proc/sys/kernel/printk has no effect on dmesg or /var/log/messages.

What am I not understanding or doing wrong? If this is the expected behavior, are there any other built in methods for adjusting the verbosity of a driver?

I am running Fedora Core 20:

[sri@localhost scull]$ uname -a
Linux localhost.localdomain 3.16.6-203.fc20.x86_64 #1 SMP Sat Oct 25 12:44:32 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

1 Answer 1

1

/proc/sys/kernel/printk controls what is printed to the system's console, so the prints showing up with dmesg is expected behaviour. You can filter dmesg output with e.g. dmesg --level=warn (see dmesg --help).

With a dynamic logging level, something has to filter out the printk messages. It seems the design decision is to not have the kernel do this, but leave it to userspace programs.

1
  • Will the kernel keep saving the KERN_DEBUG message after we change the dmesg --level? If kernel does so, it means the printk KERN_DEBUG does cause some overhead in the kernel?
    – Mike
    Commented Mar 29, 2016 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .