Re: [PATCH 4.5 142/238] watchdog: dont run proc_watchdog_update if new value is same as old

From: Don Zickus
Date: Wed Apr 13 2016 - 11:56:50 EST


On Tue, Apr 12, 2016 at 11:41:43PM +0100, Ben Hutchings wrote:
> On Sun, 2016-04-10 at 11:35 -0700, Greg Kroah-Hartman wrote:
>
> > 4.5-stable review patch.ÂÂIf anyone has any objections, please let me know.
> >
> > ------------------
> >
> > From: Joshua Hunt <johunt@xxxxxxxxxx>
> >
> > commit a1ee1932aa6bea0bb074f5e3ced112664e4637ed upstream.
> >
> > While working on a script to restore all sysctl params before a series of
> > tests I found that writing any value into the
> > /proc/sys/kernel/{nmi_watchdog,soft_watchdog,watchdog,watchdog_thresh}
> > causes them to call proc_watchdog_update().
> >
> > Â NMI watchdog: enabled on all CPUs, permanently consumes one hw-PMU counter.
> > Â NMI watchdog: enabled on all CPUs, permanently consumes one hw-PMU counter.
> > Â NMI watchdog: enabled on all CPUs, permanently consumes one hw-PMU counter.
> > Â NMI watchdog: enabled on all CPUs, permanently consumes one hw-PMU counter.
> >
> > There doesn't appear to be a reason for doing this work every time a write
> > occurs, so only do it when the values change.
> >
> > Signed-off-by: Josh Hunt <johunt@xxxxxxxxxx>
> > Acked-by: Don Zickus <dzickus@xxxxxxxxxx>
> > Reviewed-by: Aaron Tomlin <atomlin@xxxxxxxxxx>
> > Cc: Ulrich Obergfell <uobergfe@xxxxxxxxxx>
> > Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> >
> > ---
> > Âkernel/watchdog.c |ÂÂÂÂ9 ++++++++-
> > Â1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > --- a/kernel/watchdog.c
> > +++ b/kernel/watchdog.c
> [...]
> > @@ -967,7 +970,7 @@ int proc_soft_watchdog(struct ctl_table
> > Âint proc_watchdog_thresh(struct ctl_table *table, int write,
> > Â Âvoid __user *buffer, size_t *lenp, loff_t *ppos)
> > Â{
> > - int err, old;
> > + int err, old, new;
> > Â
> > Â get_online_cpus();
> > Â mutex_lock(&watchdog_proc_mutex);
> > @@ -987,6 +990,10 @@ int proc_watchdog_thresh(struct ctl_tabl
> > Â /*
> > Â Â* Update the sample period. Restore on failure.
> > Â Â*/
> > + new = ACCESS_ONCE(watchdog_thresh);
>

Hi Ben,

> This ACCESS_ONCE() doesn't make any sense to me. ÂIsn't watchdog_thresh
> protected by watchdog_proc_mutex? ÂIf a race on watchdog_thresh is

The write accesses are, but not all the reads.

> still possible then the check for old == new isn't a valid
> optimisation, and if it isn't possible then ACCESS_ONCE() shouldn't be
> used here.

The irq and nmi handlers may read it, but not write. So there should not be
any race of overwriting watchdog_thresh, just a race to read stale data.

I don't fully understand the use case for ACCESS_ONCE, so it is hard for me
to comment on whether or not the code paths satisfy the use cases or not.

The check for 'old == new' is a needed optimization and should not race
because of the mutex protection.

So, I don't have a good answer for you without understanding ACCESS_ONCE
better.

Cheers,
Don