printk.devkmsg: Ratelimit it by default
Subsystems (4)
cover printk.devkmsg: Ratelimit it by default
From: Borislav Petkov <[email protected]>
Hi all,
here's v3 integrating Ingo's comments. The thing is called printk.devkmsg= or printk_devkmsg now, depending on cmdline option or sysctl.
Changelog:
----------
v2:
here's v2 with the requested sysctl option kernel.printk_kmsg and locking of the setting when printk.kmsg= is supplied on the command line.
Patch 1 is unchanged.
Patch 2 has grown the sysctl addition.
v1:
Rostedt is busy so I took Linus' old patch and Steven's last v2 and split and extended them with the comments people had on the last thread:
https://lkml.kernel.org/r/[email protected]
I hope, at least.
So it is ratelimiting by default, with "on" and "off" cmdline options. I called the option somewhat a bit shorter too: "printk.kmsg"
The current use cases of this and of which I'm aware are:
* debug the kernel and thus shut up all interfering input from userspace, i.e. boot with "printk.kmsg=off"
* debug userspace (and by that I mean systemd) by booting with "printk.kmsg=on" so that the ratelimiting is disabled and the kernel log gets all the spew.
Thoughts?
Thanks.
Borislav Petkov (2):
ratelimit: Extend to print suppressed messages on release
printk: Add kernel parameter to control writes to /dev/kmsg
Documentation/kernel-parameters.txt | 6 +++ Documentation/sysctl/kernel.txt | 14 +++++++ include/linux/printk.h | 7 ++++ include/linux/ratelimit.h | 36 +++++++++++++--- kernel/printk/printk.c | 83 +++++++++++++++++++++++++++++++++---- kernel/sysctl.c | 9 ++++ lib/ratelimit.c | 6 ++- 7 files changed, 146 insertions(+), 15 deletions(-)
Signature · 1 line
2.7.3
PATCH 1/2 ratelimit: Extend to print suppressed messages on release
From: Borislav Petkov <[email protected]>
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released.
Separated from a previous patch by Linus.
Also, make the ON_RELEASE image not use "callbacks" as it is misleading.
--- include/linux/ratelimit.h | 36 +++++++++++++++++++++++++++++++----- lib/ratelimit.c | 6 ++++-- 2 files changed, 35 insertions(+), 7 deletions(-)
include/linux/ratelimit.h +31 -5
@@ -2,11 +2,15 @@#define _LINUX_RATELIMIT_H#include <linux/param.h> +#include <linux/sched.h> #include <linux/spinlock.h> #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)#define DEFAULT_RATELIMIT_BURST 10 +/* issue num suppressed message on exit */ +#define RATELIMIT_MSG_ON_RELEASE BIT(0) +struct ratelimit_state {raw_spinlock_t lock;/* protect the state */
@@ -15,6 +19,7 @@ struct ratelimit_state {int printed;int missed;unsigned long begin; +unsigned long flags; };#define RATELIMIT_STATE_INIT (name, interval_init, burst_init) { \
@@ -34,12 +39,33 @@ struct ratelimit_state {static inline void ratelimit_state_init (struct ratelimit_state *rs,int interval,int burst) { +memset (rs,0 ,sizeof (*rs)); +raw_spin_lock_init (&rs->lock); - rs->interval = interval; - rs->burst = burst; - rs->printed =0 ; - rs->missed =0 ; - rs->begin =0 ; + rs->interval = interval; + rs->burst = burst; +} + +static inline void ratelimit_default_init (struct ratelimit_state *rs) +{ +return ratelimit_state_init (rs,DEFAULT_RATELIMIT_INTERVAL , +DEFAULT_RATELIMIT_BURST ); +} + +static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed); +} + +static inline void +ratelimit_set_flags (struct ratelimit_state *rs,unsigned long flags) +{ + rs->flags = flags; }extern struct ratelimit_state printk_ratelimit_state;
lib/ratelimit.c +4 -2
@@ -46,12 +46,14 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) +if (rs->missed && !(rs->flags &RATELIMIT_MSG_ON_RELEASE ))printk (KERN_WARNING "%s: %d callbacks suppressed\n" , func, rs->missed); rs->begin = jiffies; rs->printed =0 ; - rs->missed =0 ; + +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) + rs->missed =0 ; }if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Signature · 1 line
2.7.3
PATCH 2/2 printk: Add kernel parameter to control writes to /dev/kmsg
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Add a "printk.devkmsg" kernel command line parameter which controls how userspace writes into /dev/kmsg. It has three options:
* ratelimit - ratelimit logging from userspace.
* on - unlimited logging from userspace
* off - logging from userspace gets ignored
The default setting is to ratelimit the messages written to it.
It additionally does not limit logging to /dev/kmsg while the system is booting if we haven't disabled it on the command line.
This patch is based on previous patches from Linus and Steven.
In addition, we can control the logging from a lower priority sysctl interface - kernel.printk_devkmsg={0,1,2} - with numeric values corresponding to the options above.
That interface will succeed only if printk.devkmsg *hasn't* been supplied on the command line. If it has, then printk.devkmsg is a one-time setting which remains for the duration of the system lifetime.
--- Documentation/kernel-parameters.txt | 6 +++ Documentation/sysctl/kernel.txt | 14 +++++++ include/linux/printk.h | 7 ++++ kernel/printk/printk.c | 83 +++++++++++++++++++++++++++++++++---- kernel/sysctl.c | 9 ++++ 5 files changed, 111 insertions(+), 8 deletions(-)
Documentation/kernel-parameters.txt +6 -0
@@ -3150,6 +3150,12 @@ bytes respectively. Such letter suffixes can also be entirely omitted. Format: <bool> (1/Y/y=enable, 0/N/n=disable) default: disabled + printk.devkmsg={on,off} + Control writing to /dev/kmsg. + on - unlimited logging to /dev/kmsg from userspace + off - logging to /dev/kmsg disabled + Default: ratelimited logging. + printk.time= Show timing data prefixed to each printk message line Format: <bool> (1/Y/y=enable, 0/N/n=disable)
Documentation/sysctl/kernel.txt +14 -0
@@ -752,6 +752,20 @@ send before ratelimiting kicks in. ============================================================== +printk_devkmsg: + +Control the logging to /dev/kmsg from userspace: + +0: default, ratelimited +1: unlimited logging to /dev/kmsg from userspace +2: logging to /dev/kmsg disabled + +The kernel command line parameter printk.devkmsg= overrides this and is +a one-time setting until next reboot: once set, it cannot be changed by +this sysctl interface anymore. + +============================================================== + randomize_va_space: This option can be used to select the type of process address
include/linux/printk.h +7 -0
@@ -171,6 +171,13 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,extern int printk_delay_msec;extern int dmesg_restrict;extern int kptr_restrict; +extern unsigned int devkmsg_log; + +struct ctl_table ; + +extern int +devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write,void __user *buf, +size_t *lenp,loff_t *ppos);extern void wake_up_klogd (void );
kernel/printk/printk.c +75 -8
122 lines not on this page · raw message
kernel/sysctl.c +9 -0
@@ -814,6 +814,15 @@ static struct ctl_table kern_table[] = { .extra2 = &ten_thousand, }, { + .procname = "printk_devkmsg", + .data = &devkmsg_log, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = devkmsg_sysctl_set_loglvl, + .extra1 = &zero, + .extra2 = &two, + }, + { .procname = "dmesg_restrict", .data = &dmesg_restrict, .maxlen = sizeof(int),
Signature · 1 line
2.7.3
Steven Rostedt
reviewerRe: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
In reply to Borislav PetkovQuoted 72 lines · On Mon, 4 Jul 2016 16:24:51 +0200 Borislav Petkov <[email protected]> wrote:
From: Borislav Petkov <[email protected]>
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released.
Separated from a previous patch by Linus.
Also, make the
ON_RELEASEimage not use "callbacks" as it is misleading.Signed-off-by Borislav Petkov <[email protected]>Acked-by Linus Torvalds <[email protected]>Cc Andrew Morton <[email protected]>Cc Franck Bui <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Ingo Molnar <[email protected]>Cc Linus Torvalds <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>Cc Uwe Kleine-König <[email protected]>--- include/linux/ratelimit.h | 36 +++++++++++++++++++++++++++++++----- lib/ratelimit.c | 6 ++++-- 2 files changed, 35 insertions(+), 7 deletions(-)include/linux/ratelimit.h +15
-5@@ -2,11 +2,15 @@#define _LINUX_RATELIMIT_H#include <linux/param.h> +#include <linux/sched.h> #include <linux/spinlock.h> #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)#define DEFAULT_RATELIMIT_BURST 10 +/* issue num suppressed message on exit */ +#define RATELIMIT_MSG_ON_RELEASE BIT(0) +struct ratelimit_state {raw_spinlock_t lock;/* protect the state */ @@ -15,6 +19,7 @@ struct ratelimit_state {int printed;int missed;unsigned long begin; +unsigned long flags; };#define RATELIMIT_STATE_INIT (name, interval_init, burst_init) { \@@ -34,12 +39,33 @@ struct ratelimit_state {static inline void ratelimit_state_init (struct ratelimit_state *rs,int interval,int burst) { +memset (rs,0 ,sizeof (*rs)); +raw_spin_lock_init (&rs->lock); - rs->interval = interval; - rs->burst = burst; - rs->printed =0 ; - rs->missed =0 ; - rs->begin =0 ; + rs->interval = interval; + rs->burst = burst; +} + +static inline void ratelimit_default_init (struct ratelimit_state *rs) +{ +return ratelimit_state_init (rs,DEFAULT_RATELIMIT_INTERVAL , +DEFAULT_RATELIMIT_BURST );
include/linux/ratelimit.h struct ratelimit_state { · line 62
Quoted 10 lines
include/linux/ratelimit.h +10
-0+} + +static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed);
Is the comm important? Maybe add the function that called it?
"%pS", _THIS_IP_
Perhaps add __always_inline, as _THIS_IP_ will point into the function that calls this?
-- Steve
Quoted 30 lines
include/linux/ratelimit.h +6
-0+} + +static inline void +ratelimit_set_flags (struct ratelimit_state *rs,unsigned long flags) +{ + rs->flags = flags; }extern struct ratelimit_state printk_ratelimit_state;lib/ratelimit.c +4
-2@@ -46,12 +46,14 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) +if (rs->missed && !(rs->flags &RATELIMIT_MSG_ON_RELEASE ))printk (KERN_WARNING "%s: %d callbacks suppressed\n" , func, rs->missed); rs->begin = jiffies; rs->printed =0 ; - rs->missed =0 ; + +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) + rs->missed =0 ; }if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 5 lines · Steven Rostedt
include/linux/ratelimit.h struct ratelimit_state { · line 62
Quoted 3 lines
include/linux/ratelimit.h +3
-0+if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed);Is the comm important?
Yes, we wanna dump the task name which called devkmsg_release().
Quoted 6 lines
Maybe add the function that called it?
"%pS", _THIS_IP_Perhaps add
__always_inline, as _THIS_IP_ will point into the function that calls this?
That would inadvertently be devkmsg_release() in this use case. But we want to dump the task name which opened and spat so much crap into /dev/kmsg so as to cause the ratelimiting to hit.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 11 lines · On Tue, 5 Jul 2016 20:45:17 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 5 lines · Steven Rostedt
include/linux/ratelimit.h struct ratelimit_state { · line 62
Quoted 3 lines
include/linux/ratelimit.h +3
-0+if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed);Is the comm important?
Yes, we wanna dump the task name which called
devkmsg_release().Quoted 2 lines
Maybe add the function that called it?
Quoted 9 lines
Quoted 4 lines
"%pS", _THIS_IP_Perhaps add
__always_inline, as _THIS_IP_ will point into the function that calls this?That would inadvertently be
devkmsg_release()in this use case. But we want to dump the task name which opened and spat so much crap into/dev/kmsgso as to cause the ratelimiting to hit.
Perhaps we should show both, unless you don't think this will ever be used by anything other than devkmsg?
-- Steve
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 2 lines · Steven Rostedt
Perhaps we should show both, unless you don't think this will ever be used by anything other than devkmsg?
I'd say let's do it only when we go down that road and start using it for something else.
Because, for example, the ratelimiting thing is, in fact, generic but it is used primarily to ratelimit printks. Even though it could be used for something else, theoretically...
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 11 lines · On Tue, 5 Jul 2016 21:42:45 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 2 lines · Steven Rostedt
Perhaps we should show both, unless you don't think this will ever be used by anything other than devkmsg?
I'd say let's do it only when we go down that road and start using it for something else.
Because, for example, the ratelimiting thing is, in fact, generic but it is used primarily to ratelimit printks. Even though it could be used for something else, theoretically...
But you know... Build it and they will come.
OK, I'm fine then.
-- Steve
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 4 lines · Steven Rostedt
Quoted 3 lines · On Tue, 5 Jul 2016 21:42:45 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 2 lines · Steven Rostedt
Perhaps we should show both, unless you don't think this will ever be used by anything other than devkmsg?
Quoted 8 lines
Quoted 7 lines
I'd say let's do it only when we go down that road and start using it for something else.
Because, for example, the ratelimiting thing is, in fact, generic but it is used primarily to ratelimit printks. Even though it could be used for something else, theoretically...
But you know... Build it and they will come.
arch/s390/kvm/kvm-s390.c: ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
As far as I know, _ratelimit is used for non-printk purposes in arch/s390/kvm/sthyi.c
int handle_sthyi(struct kvm_vcpu *vcpu)
{
int reg1, reg2, r = 0;
u64 code, addr, cc = 0;
struct sthyi_sctns *sctns = NULL;
/*
* STHYI requires extensive locking in the higher hypervisors
* and is very computational/memory expensive. Therefore we
* ratelimit the executions per VM.
*/
if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
kvm_s390_retry_instr(vcpu);
return 0;
}Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 20 lines · Joe Perches
Quoted 4 lines · Steven Rostedt
Quoted 3 lines · On Tue, 5 Jul 2016 21:42:45 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 2 lines · Steven Rostedt
Perhaps we should show both, unless you don't think this will ever be used by anything other than devkmsg?
Quoted 8 lines
Quoted 7 lines
I'd say let's do it only when we go down that road and start using it for something else.
Because, for example, the ratelimiting thing is, in fact, generic but it is used primarily to ratelimit printks. Even though it could be used for something else, theoretically...
But you know... Build it and they will come.
arch/s390/kvm/kvm-s390.c:ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);As far as I know,
_ratelimitis used for non-printk purposes inarch/s390/kvm/sthyi.cint
handle_sthyi(struct kvm_vcpu *vcpu)
Quoted 14 lines
{
int reg1, reg2, r = 0;
u64 code, addr, cc = 0;
struct sthyi_sctns *sctns = NULL;
/*
* STHYI requires extensive locking in the higher hypervisors
* and is very computational/memory expensive. Therefore we
* ratelimit the executions per VM.
*/
if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
kvm_s390_retry_instr(vcpu);
return 0;
}Yes, this is new in next. As far as I can see, the new message would only appear if we would call ratelimit_state_exit. Correct? We do not call this - I assume this is ok?
We really only want to reuse the rate limit base code (to avoid writing the same code twice) and being in lib indicated that this can indeed be used outside printk.
Now: your patch 1 would allow me to get rid of the messages completely by setting the flag and by not calling ratelimit_state_exit. Which is probably what we should do in our code.
Christian
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 10 lines · Christian Borntraeger
Yes, this is new in next. As far as I can see, the new message would only appear if we would call
ratelimit_state_exit. Correct? We do not call this - I assume this is ok?We really only want to reuse the rate limit base code (to avoid writing the same code twice) and being in lib indicated that this can indeed be used outside printk.
Now: your patch 1 would allow me to get rid of the messages completely by setting the flag and by not callingratelimit_state_exit. Which is probably what we should do in our code.
Can we delay fixing this after the code is merged in Linus's tree?
Paolo
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Christian Borntraeger
Yes, this is new in next. As far as I can see, the new message would only appear if we would call
ratelimit_state_exit. Correct? We do not call this - I assume this is ok?
Quoted 8 lines
Quoted 6 lines
We really only want to reuse the rate limit base code (to avoid writing the same code twice) and being in lib indicated that this can indeed be used outside printk.
Now: your patch 1 would allow me to get rid of the messages completely by setting the flag and by not callingratelimit_state_exit. Which is probably what we should do in our code.Can we delay fixing this after the code is merged in Linus's tree?
Absolutely. We already have 2 smaller conflicts in next and I certainly do not want to add another one.
The current ratelimit print does not hurt - it is just not necessary for us. so my statement was just a "statement of direction" to write some IBM speak ;-)
Re: [PATCH -v3 1/2] ratelimit: Extend to print suppressed messages on release
In reply to Christian BorntraegerQuoted 3 lines · Christian Borntraeger
Yes, this is new in next. As far as I can see, the new message would only appear if we would call
ratelimit_state_exit. Correct? We do not call this - I assume this is ok?
Right, the idea for the /dev/kmsg use case was to issue the suppressed count only when we release the ratelimit state.
Quoted 6 lines
We really only want to reuse the rate limit base code (to avoid writing the same code twice) and being in lib indicated that this can indeed be used outside printk.
Now: your patch 1 would allow me to get rid of the messages completely by setting the flag and by not callingratelimit_state_exit. Which is probably what we should do in our code.
Yeah, that should work for your usecase.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3 2/2] printk: Add kernel parameter to control writes to /dev/kmsg
In reply to Borislav PetkovQuoted 14 lines · On Mon, 4 Jul 2016 16:24:52 +0200 Borislav Petkov <[email protected]> wrote:
kernel/printk/printk.c +3
-0@@ -614,6 +663,7 @@ struct devkmsg_user {u64 seq;u32 idx;enum log_flags prev; +struct ratelimit_state rs;struct mutex lock;char buf[CONSOLE_EXT_LOG_MAX ]; };@@ -623,11 +673,24 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)char *buf, *line;int level = default_message_loglevel;int facility =1 ;/* LOG_USER */ +struct file *file = iocb->ki_filp; +struct devkmsg_user *user = file->private_data;
kernel/printk/printk.c static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from) · line 686
Quoted 10 lines
kernel/printk/printk.c +5
-1size_t len =iov_iter_count (from);ssize_t ret = len; -if (len >LOG_LINE_MAX ) +if (!user || len >LOG_LINE_MAX )return -EINVAL ; + +/* Ignore when user logging is disabled. */ +if (devkmsg_log &DEVKMSG_LOG_MASK_OFF ) +return len;
I wonder if we should return some sort of error message here? ENODEV?
Quoted 17 lines
kernel/printk/printk.c +7
-3+ +/* Ratelimit when not explicitly enabled or when we're not booting. */ +if ((system_state !=SYSTEM_BOOTING ) && !(devkmsg_log &DEVKMSG_LOG_MASK_ON )) { +if (!___ratelimit (&user->rs, current->comm)) +return ret; + } + buf =kmalloc (len+1 ,GFP_KERNEL );if (buf ==NULL )return -ENOMEM ;@@ -801,18 +864,20 @@ static int devkmsg_open(struct inode *inode, struct file *file)int err;/* write-only does not need any file context */ -if ((file->f_flags &O_ACCMODE ) ==O_WRONLY ) -return 0 ; -
kernel/printk/printk.c static int devkmsg_open(struct inode *inode, struct file *file) · line 872
Quoted 10 lines
kernel/printk/printk.c +6
-4- err =check_syslog_permissions (SYSLOG_ACTION_READ_ALL , -SYSLOG_FROM_READER ); -if (err) -return err; +if ((file->f_flags &O_ACCMODE ) !=O_WRONLY ) { + err =check_syslog_permissions (SYSLOG_ACTION_READ_ALL , +SYSLOG_FROM_READER ); +if (err) +return err; + }
Hmm, there's no error message when it is disabled? I'm not sure that is what we want. I specifically had the return be an error on open if it was disabled, because (surprisingly) systemd does the right thing and uses another utility for syslogging.
If you silently fail here, then we lose all logging because systemd thinks this is working when it is not. That's not what I want.
-- Steve
Quoted 40 lines
kernel/printk/printk.c +5
-0user =kmalloc (sizeof (struct devkmsg_user ),GFP_KERNEL );if (!user)return -ENOMEM ; +ratelimit_default_init (&user->rs); +ratelimit_set_flags (&user->rs,RATELIMIT_MSG_ON_RELEASE ); +mutex_init (&user->lock);raw_spin_lock_irq (&logbuf_lock);@@ -831,6 +896,8 @@ static int devkmsg_release(struct inode *inode, struct file *file)if (!user)return 0 ; +ratelimit_state_exit (&user->rs); +mutex_destroy (&user->lock);kfree (user);return 0 ;kernel/sysctl.c +9
-0@@ -814,6 +814,15 @@ static struct ctl_table kern_table[] = { .extra2 = &ten_thousand, }, { + .procname = "printk_devkmsg", + .data = &devkmsg_log, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = devkmsg_sysctl_set_loglvl, + .extra1 = &zero, + .extra2 = &two, + }, + { .procname = "dmesg_restrict", .data = &dmesg_restrict, .maxlen = sizeof(int),
Re: [PATCH -v3 2/2] printk: Add kernel parameter to control writes to /dev/kmsg
Quoted 1 line · Steven Rostedt
I wonder if we should return some sort of error message here? ENODEV?
What for?
We're silently ignoring it. If we start returning an error here, we might break doofus if it checks the write retval.
And it's not like we care - we're ignoring all writes whatsoever.
Quoted 2 lines
If you silently fail here, then we lose all logging because systemd thinks this is working when it is not. That's not what I want.
Hmm, ok, you're making sense to me.
Do you want an error message too or only an -ENODEV returned or somesuch?
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3 2/2] printk: Add kernel parameter to control writes to /dev/kmsg
Quoted 8 lines · On Wed, 6 Jul 2016 00:02:21 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 2 lines
If you silently fail here, then we lose all logging because systemd thinks this is working when it is not. That's not what I want.
Hmm, ok, you're making sense to me.
Do you want an error message too or only an
-ENODEVreturned or somesuch?
My patch returned -EPERM and it works on my box. That is, systemd finds something else to log to.
-- Steve
PATCH 1/2 ratelimit: Extend to print suppressed messages on release
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released.
Separated from a previous patch by Linus.
Also, make the ON_RELEASE image not use "callbacks" as it is misleading.
---
v3.1: Simplify testing of !RATELIMIT_MSG_ON_RELEASE in ___ratelimit()
include/linux/ratelimit.h | 36 +++++++++++++++++++++++++++++++----- lib/ratelimit.c | 15 +++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-)
include/linux/ratelimit.h +31 -5
@@ -2,11 +2,15 @@#define _LINUX_RATELIMIT_H#include <linux/param.h> +#include <linux/sched.h> #include <linux/spinlock.h> #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)#define DEFAULT_RATELIMIT_BURST 10 +/* issue num suppressed message on exit */ +#define RATELIMIT_MSG_ON_RELEASE BIT(0) +struct ratelimit_state {raw_spinlock_t lock;/* protect the state */
@@ -15,6 +19,7 @@ struct ratelimit_state {int printed;int missed;unsigned long begin; +unsigned long flags; };#define RATELIMIT_STATE_INIT (name, interval_init, burst_init) { \
@@ -34,12 +39,33 @@ struct ratelimit_state {static inline void ratelimit_state_init (struct ratelimit_state *rs,int interval,int burst) { +memset (rs,0 ,sizeof (*rs)); +raw_spin_lock_init (&rs->lock); - rs->interval = interval; - rs->burst = burst; - rs->printed =0 ; - rs->missed =0 ; - rs->begin =0 ; + rs->interval = interval; + rs->burst = burst; +} + +static inline void ratelimit_default_init (struct ratelimit_state *rs) +{ +return ratelimit_state_init (rs,DEFAULT_RATELIMIT_INTERVAL , +DEFAULT_RATELIMIT_BURST ); +} + +static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed); +} + +static inline void +ratelimit_set_flags (struct ratelimit_state *rs,unsigned long flags) +{ + rs->flags = flags; }extern struct ratelimit_state printk_ratelimit_state;
lib/ratelimit.c +9 -6
@@ -46,12 +46,15 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) -printk (KERN_WARNING "%s: %d callbacks suppressed\n" , - func, rs->missed); - rs->begin = jiffies; - rs->printed =0 ; - rs->missed =0 ; +if (rs->missed) { +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) { +pr_warn ("%s: %d callbacks suppressed\n" , func, rs->missed); + rs->missed =0 ; + } + + rs->begin = jiffies; + rs->printed =0 ; + } }if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Signature · 7 lines
2.7.3
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.PATCH 2/2 printk: Add kernel parameter to control writes to /dev/kmsg
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Add a "printk.devkmsg" kernel command line parameter which controls how userspace writes into /dev/kmsg. It has three options:
* ratelimit - ratelimit logging from userspace.
* on - unlimited logging from userspace
* off - logging from userspace gets ignored
The default setting is to ratelimit the messages written to it.
It additionally does not limit logging to /dev/kmsg while the system is booting if we haven't disabled it on the command line.
This patch is based on previous patches from Linus and Steven.
In addition, we can control the logging from a lower priority sysctl interface - kernel.printk_devkmsg={0,1,2} - with numeric values corresponding to the options above.
That interface will succeed only if printk.devkmsg *hasn't* been supplied on the command line. If it has, then printk.devkmsg is a one-time setting which remains for the duration of the system lifetime.
---
v3.1: Return error (-EPERM) on open when devkmsg logging disabled, as Steve requested.
Documentation/kernel-parameters.txt | 6 +++ Documentation/sysctl/kernel.txt | 14 ++++++ include/linux/printk.h | 7 +++ kernel/printk/printk.c | 86 +++++++++++++++++++++++++++++++++---- kernel/sysctl.c | 9 ++++ 5 files changed, 114 insertions(+), 8 deletions(-)
Documentation/kernel-parameters.txt +6 -0
@@ -3150,6 +3150,12 @@ bytes respectively. Such letter suffixes can also be entirely omitted. Format: <bool> (1/Y/y=enable, 0/N/n=disable) default: disabled + printk.devkmsg={on,off} + Control writing to /dev/kmsg. + on - unlimited logging to /dev/kmsg from userspace + off - logging to /dev/kmsg disabled + Default: ratelimited logging. + printk.time= Show timing data prefixed to each printk message line Format: <bool> (1/Y/y=enable, 0/N/n=disable)
Documentation/sysctl/kernel.txt +14 -0
@@ -752,6 +752,20 @@ send before ratelimiting kicks in. ============================================================== +printk_devkmsg: + +Control the logging to /dev/kmsg from userspace: + +0: default, ratelimited +1: unlimited logging to /dev/kmsg from userspace +2: logging to /dev/kmsg disabled + +The kernel command line parameter printk.devkmsg= overrides this and is +a one-time setting until next reboot: once set, it cannot be changed by +this sysctl interface anymore. + +============================================================== + randomize_va_space: This option can be used to select the type of process address
include/linux/printk.h +7 -0
@@ -171,6 +171,13 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,extern int printk_delay_msec;extern int dmesg_restrict;extern int kptr_restrict; +extern unsigned int devkmsg_log; + +struct ctl_table ; + +extern int +devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write,void __user *buf, +size_t *lenp,loff_t *ppos);extern void wake_up_klogd (void );
kernel/printk/printk.c +78 -8
126 lines not on this page · raw message
kernel/sysctl.c +9 -0
@@ -814,6 +814,15 @@ static struct ctl_table kern_table[] = { .extra2 = &ten_thousand, }, { + .procname = "printk_devkmsg", + .data = &devkmsg_log, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = devkmsg_sysctl_set_loglvl, + .extra1 = &zero, + .extra2 = &two, + }, + { .procname = "dmesg_restrict", .data = &dmesg_restrict, .maxlen = sizeof(int),
Signature · 8 lines
2.7.3
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3.1 1/2] ratelimit: Extend to print suppressed messages on release
In reply to Borislav PetkovQuoted 14 lines · On Wed, 6 Jul 2016 15:28:00 +0200 Borislav Petkov <[email protected]> wrote:
extern struct
ratelimit_stateprintk_ratelimit_state;lib/ratelimit.c +0
-5@@ -46,12 +46,15 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) -printk (KERN_WARNING "%s: %d callbacks suppressed\n" , - func, rs->missed); - rs->begin = jiffies; - rs->printed =0 ;
lib/ratelimit.c int ___ratelimit(struct ratelimit_state *rs, const char *func) · line 57
Quoted 10 lines
lib/ratelimit.c +9
-1- rs->missed =0 ; +if (rs->missed) { +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) { +pr_warn ("%s: %d callbacks suppressed\n" , func, rs->missed); + rs->missed =0 ; + } + + rs->begin = jiffies; + rs->printed =0 ; + }
Hmm, the above changes the previous logic. That seems wrong.
Before:
if (rs->missed) [...] rs->begin = jiffies; rs->printed = 0;
after:
if (rs->missed) {
rs->begin = jiffies;
rs->printed = 0;
}I think you wanted:
if (rs->missed) {
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
[..]
rs->missed = 0;
}
}
rs->begin = jiffies;
rs->printed = 0;-- Steve
Quoted 3 lines
lib/ratelimit.c +0
-0}if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Re: [PATCH -v3.1 1/2] ratelimit: Extend to print suppressed messages on release
In reply to Borislav PetkovQuoted 2 lines · Borislav Petkov
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released.
[]
include/linux/ratelimit.h
Quoted 1 line
include/linux/ratelimit.h +0
-0
[]
include/linux/ratelimit.h struct ratelimit_state { · line 62
Quoted 8 lines
include/linux/ratelimit.h +8
-0+static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed);
Please use pr_warn as it will use whateverpr_fmt prefix is specified by the subsystem.
Maybe:
if (rs->missed)
pr_warn("ratelimit: %s: %d output lines suppressed\n",
current->comm, rs->missed);to be more similar to the callback suppressed message in lib/ratelimit.c
pr_warn("%s: %d callbacks suppressed\n", func, rs->missed);PATCH 1/2 ratelimit: Extend to print suppressed messages on release
In reply to Steven RostedtQuoted 4 lines · Steven Rostedt
if (rs->missed) {
rs->begin = jiffies;
rs->printed = 0;
}This is what I get when I'm trying to juggle 10 things at the same time :-\
Ok, here's v3.2. Thanks dude!
---
From: Borislav Petkov <[email protected]>
Date: Tue, 14 Jun 2016 11:51:04 +0200
Subject: [PATCH -v3.2 1/2] ratelimit: Extend to print suppressed messages on release
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released.
Separated from a previous patch by Linus.
Also, make the ON_RELEASE image not use "callbacks" as it is misleading.
---
v3.2: Simplify testing of !RATELIMIT_MSG_ON_RELEASE in ___ratelimit() correctly!
include/linux/ratelimit.h | 36 +++++++++++++++++++++++++++++++----- lib/ratelimit.c | 10 ++++++---- 2 files changed, 37 insertions(+), 9 deletions(-)
include/linux/ratelimit.h +31 -5
@@ -2,11 +2,15 @@#define _LINUX_RATELIMIT_H#include <linux/param.h> +#include <linux/sched.h> #include <linux/spinlock.h> #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)#define DEFAULT_RATELIMIT_BURST 10 +/* issue num suppressed message on exit */ +#define RATELIMIT_MSG_ON_RELEASE BIT(0) +struct ratelimit_state {raw_spinlock_t lock;/* protect the state */
@@ -15,6 +19,7 @@ struct ratelimit_state {int printed;int missed;unsigned long begin; +unsigned long flags; };#define RATELIMIT_STATE_INIT (name, interval_init, burst_init) { \
@@ -34,12 +39,33 @@ struct ratelimit_state {static inline void ratelimit_state_init (struct ratelimit_state *rs,int interval,int burst) { +memset (rs,0 ,sizeof (*rs)); +raw_spin_lock_init (&rs->lock); - rs->interval = interval; - rs->burst = burst; - rs->printed =0 ; - rs->missed =0 ; - rs->begin =0 ; + rs->interval = interval; + rs->burst = burst; +} + +static inline void ratelimit_default_init (struct ratelimit_state *rs) +{ +return ratelimit_state_init (rs,DEFAULT_RATELIMIT_INTERVAL , +DEFAULT_RATELIMIT_BURST ); +} + +static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed); +} + +static inline void +ratelimit_set_flags (struct ratelimit_state *rs,unsigned long flags) +{ + rs->flags = flags; }extern struct ratelimit_state printk_ratelimit_state;
lib/ratelimit.c +6 -4
@@ -46,12 +46,14 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) -printk (KERN_WARNING "%s: %d callbacks suppressed\n" , - func, rs->missed); +if (rs->missed) { +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) { +pr_warn ("%s: %d callbacks suppressed\n" , func, rs->missed); + rs->missed =0 ; + } + } rs->begin = jiffies; rs->printed =0 ; - rs->missed =0 ; }if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Signature · 7 lines
2.7.3
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3.1 2/2] printk: Add kernel parameter to control writes to /dev/kmsg
In reply to Borislav Petkovkernel/sysctl.c
Quoted 42 lines · On Wed, 6 Jul 2016 15:29:24 +0200 Borislav Petkov <[email protected]> wrote:
kernel/printk/printk.c +14
-7@@ -800,19 +863,24 @@ static int devkmsg_open(struct inode *inode, struct file *file)struct devkmsg_user *user;int err; -/* write-only does not need any file context */ -if ((file->f_flags &O_ACCMODE ) ==O_WRONLY ) -return 0 ; +if (devkmsg_log &DEVKMSG_LOG_MASK_OFF ) +return -EPERM ; - err =check_syslog_permissions (SYSLOG_ACTION_READ_ALL , -SYSLOG_FROM_READER ); -if (err) -return err; +/* write-only does not need any file context */ +if ((file->f_flags &O_ACCMODE ) !=O_WRONLY ) { + err =check_syslog_permissions (SYSLOG_ACTION_READ_ALL , +SYSLOG_FROM_READER ); +if (err) +return err; + } user =kmalloc (sizeof (struct devkmsg_user ),GFP_KERNEL );if (!user)return -ENOMEM ; +ratelimit_default_init (&user->rs); +ratelimit_set_flags (&user->rs,RATELIMIT_MSG_ON_RELEASE ); +mutex_init (&user->lock);raw_spin_lock_irq (&logbuf_lock);@@ -831,6 +899,8 @@ static int devkmsg_release(struct inode *inode, struct file *file)if (!user)return 0 ; +ratelimit_state_exit (&user->rs); +mutex_destroy (&user->lock);kfree (user);return 0 ;kernel/sysctl.c +0
-0
Hmm, this does nothing to stop user space from doing the following:
while :; do echo '5,0,0,-;hello' > /dev/kmsg; done
But at least it's a start.
-- Steve
Re: [PATCH -v3.1 2/2] printk: Add kernel parameter to control writes to /dev/kmsg
Quoted 3 lines · Steven Rostedt
Hmm, this does nothing to stop user space from doing the following:
while :; do echo '5,0,0,-;hello' >
/dev/kmsg; done
I wouldn't worry too much if /dev/kmsg has root:root perms elsewhere too, like it is the case in my guest here:
crw-r--r-- 1 root root 1, 11 Jul 6 22:28 /dev/kmsg
root can shoot herself in the foot in countless ways anyway.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Steven Rostedt
reviewerRe: [PATCH -v3.2 1/2] ratelimit: Extend to print suppressed messages on release
In reply to Borislav Petkovinclude/linux/ratelimit.h struct ratelimit_state { · line 62
Quoted 8 lines · On Wed, 6 Jul 2016 16:59:29 +0200 Borislav Petkov <[email protected]> wrote:
include/linux/ratelimit.h +8
-0+static inline void ratelimit_state_exit (struct ratelimit_state *rs) +{ +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) +return ; + +if (rs->missed) +printk (KERN_WARNING "%s: %d output lines suppressed due to ratelimiting\n" , + current->comm, rs->missed);
Hmm, should this clear the missed flag? Especially since it isn't cleared below.
-- Steve
Quoted 32 lines
include/linux/ratelimit.h +6
-0+} + +static inline void +ratelimit_set_flags (struct ratelimit_state *rs,unsigned long flags) +{ + rs->flags = flags; }extern struct ratelimit_state printk_ratelimit_state;lib/ratelimit.c +6
-4@@ -46,12 +46,14 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func) rs->begin = jiffies;if (time_is_before_jiffies (rs->begin + rs->interval)) { -if (rs->missed) -printk (KERN_WARNING "%s: %d callbacks suppressed\n" , - func, rs->missed); +if (rs->missed) { +if (!(rs->flags &RATELIMIT_MSG_ON_RELEASE )) { +pr_warn ("%s: %d callbacks suppressed\n" , func, rs->missed); + rs->missed =0 ; + } + } rs->begin = jiffies; rs->printed =0 ; - rs->missed =0 ; }if (rs->burst && rs->burst > rs->printed) { rs->printed++;
Re: [PATCH -v3.2 1/2] ratelimit: Extend to print suppressed messages on release
Quoted 2 lines · Steven Rostedt
Hmm, should this clear the missed flag? Especially since it isn't cleared below.
The expectation is that after you call exit on something, you don't need it anymore. But I know exactly why you're asking for this so I'll do the change. :-)
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.