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


[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_CRIT    "KERN_CRIT");
printk(KERN_ERR     "KERN_ERR");
printk(KERN_INFO    "KERN_INFO");

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

/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.

  • 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 Mar 29 '16 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.