2016-12-13 03:57:08

by Nicholas Mc Guire

[permalink] [raw]
Subject: [PATCH] doc: add note on usleep_range range

useleep_range() with a delta of 0 makes no sense and only prevents the
timer subsystem from optimizing interrupts. As any user of usleep_range()
is in non-atomic context the timer jitter is in the range of 10s of
microseconds anyway.

This adds a note making it clear that a range of 0 is a bad idea.

Signed-off-by: Nicholas Mc Guire <[email protected]>
---

as of 4.9.0 there are about 20 cases of usleep_ranges() that have
min==max and none of them really look like they are necessary, so
it does seem like a relatively common misunderstanding worth
noting in the documentation.

Patch is against 4.9.0 (localversion-next is 20161212)

Documentation/timers/timers-howto.txt | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/Documentation/timers/timers-howto.txt b/Documentation/timers/timers-howto.txt
index 038f8c7..b5cdf82 100644
--- a/Documentation/timers/timers-howto.txt
+++ b/Documentation/timers/timers-howto.txt
@@ -93,6 +93,13 @@ NON-ATOMIC CONTEXT:
tolerances here are very situation specific, thus it
is left to the caller to determine a reasonable range.

+ A range of 0, that is usleep_range(100,100) or the
+ like, do not make sense as this code is in a
+ non-atomic section and a system can not be expected
+ to have jitter 0. For any non-RT code any delta
+ less than 50 microseconds probably is only preventing
+ timer subsystem optimization but providing no benefit.
+
SLEEPING FOR LARGER MSECS ( 10ms+ )
* Use msleep or possibly msleep_interruptible

--
2.1.4


2016-12-13 09:11:00

by Jani Nikula

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> useleep_range() with a delta of 0 makes no sense and only prevents the
> timer subsystem from optimizing interrupts. As any user of usleep_range()
> is in non-atomic context the timer jitter is in the range of 10s of
> microseconds anyway.
>
> This adds a note making it clear that a range of 0 is a bad idea.

So I don't really have anything to do with the timer subsystem, I'm just
their "consumer", so take this with a grain of salt.

Documentation is good, but I don't think this will be enough.

I think the only thing that will work is to detect and complain about
things like this automatically. Some ideas:

* WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
might be drastic, but it would get the job done eventually.

* If you want to avoid the runtime overhead (and complaints about the
backtraces), you could wrap usleep_range() in a macro that does
BUILD_BUG_ON(min == max) if the parameters are build time constants
(they usually are). But you'd have to fix all the problem cases first.

* You could try (to persuade Julia or Dan) to come up with a
cocci/smatch check for usleep_range() calls where min == max, so we
could get bug reports for this. This probably works on expressions, so
this would catch also cases where the parameters aren't built time
constants.

BR,
Jani.


>
> Signed-off-by: Nicholas Mc Guire <[email protected]>
> ---
>
> as of 4.9.0 there are about 20 cases of usleep_ranges() that have
> min==max and none of them really look like they are necessary, so
> it does seem like a relatively common misunderstanding worth
> noting in the documentation.
>
> Patch is against 4.9.0 (localversion-next is 20161212)
>
> Documentation/timers/timers-howto.txt | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/Documentation/timers/timers-howto.txt b/Documentation/timers/timers-howto.txt
> index 038f8c7..b5cdf82 100644
> --- a/Documentation/timers/timers-howto.txt
> +++ b/Documentation/timers/timers-howto.txt
> @@ -93,6 +93,13 @@ NON-ATOMIC CONTEXT:
> tolerances here are very situation specific, thus it
> is left to the caller to determine a reasonable range.
>
> + A range of 0, that is usleep_range(100,100) or the
> + like, do not make sense as this code is in a
> + non-atomic section and a system can not be expected
> + to have jitter 0. For any non-RT code any delta
> + less than 50 microseconds probably is only preventing
> + timer subsystem optimization but providing no benefit.
> +
> SLEEPING FOR LARGER MSECS ( 10ms+ )
> * Use msleep or possibly msleep_interruptible

--
Jani Nikula, Intel Open Source Technology Center

2016-12-13 09:28:24

by Nicholas Mc Guire

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > useleep_range() with a delta of 0 makes no sense and only prevents the
> > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > is in non-atomic context the timer jitter is in the range of 10s of
> > microseconds anyway.
> >
> > This adds a note making it clear that a range of 0 is a bad idea.
>
> So I don't really have anything to do with the timer subsystem, I'm just
> their "consumer", so take this with a grain of salt.
>
> Documentation is good, but I don't think this will be enough.
>
> I think the only thing that will work is to detect and complain about
> things like this automatically. Some ideas:
>
> * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> might be drastic, but it would get the job done eventually.
>
> * If you want to avoid the runtime overhead (and complaints about the
> backtraces), you could wrap usleep_range() in a macro that does
> BUILD_BUG_ON(min == max) if the parameters are build time constants
> (they usually are). But you'd have to fix all the problem cases first.
>
> * You could try (to persuade Julia or Dan) to come up with a
> cocci/smatch check for usleep_range() calls where min == max, so we
> could get bug reports for this. This probably works on expressions, so
> this would catch also cases where the parameters aren't built time
> constants.
>

I fully agree - without automation it is almost usless
the coccinelle spatch is a seperate patch and it is tested butnot yet
submitted.

the spatch for this iss actually trivial

@nulldelta@
constant C;
position p;
@@

* usleep_range@p(C,C)

thx!
hofrat

2016-12-13 10:18:59

by Jani Nikula

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> I fully agree - without automation it is almost usless
> the coccinelle spatch is a seperate patch and it is tested butnot yet
> submitted.

Good, good! Sorry for the noise.

BR,
Jani.

--
Jani Nikula, Intel Open Source Technology Center

2016-12-13 12:05:16

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range



On Tue, 13 Dec 2016, Nicholas Mc Guire wrote:

> On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> > On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > > useleep_range() with a delta of 0 makes no sense and only prevents the
> > > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > > is in non-atomic context the timer jitter is in the range of 10s of
> > > microseconds anyway.
> > >
> > > This adds a note making it clear that a range of 0 is a bad idea.
> >
> > So I don't really have anything to do with the timer subsystem, I'm just
> > their "consumer", so take this with a grain of salt.
> >
> > Documentation is good, but I don't think this will be enough.
> >
> > I think the only thing that will work is to detect and complain about
> > things like this automatically. Some ideas:
> >
> > * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> > might be drastic, but it would get the job done eventually.
> >
> > * If you want to avoid the runtime overhead (and complaints about the
> > backtraces), you could wrap usleep_range() in a macro that does
> > BUILD_BUG_ON(min == max) if the parameters are build time constants
> > (they usually are). But you'd have to fix all the problem cases first.
> >
> > * You could try (to persuade Julia or Dan) to come up with a
> > cocci/smatch check for usleep_range() calls where min == max, so we
> > could get bug reports for this. This probably works on expressions, so
> > this would catch also cases where the parameters aren't built time
> > constants.
> >
>
> I fully agree - without automation it is almost usless
> the coccinelle spatch is a seperate patch and it is tested butnot yet
> submitted.
>
> the spatch for this iss actually trivial
>
> @nulldelta@
> constant C;
> position p;
> @@
>
> * usleep_range@p(C,C)

People never use more complex expressions?

julia

2016-12-13 12:24:22

by Nicholas Mc Guire

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue, Dec 13, 2016 at 01:05:12PM +0100, Julia Lawall wrote:
>
>
> On Tue, 13 Dec 2016, Nicholas Mc Guire wrote:
>
> > On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> > > On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > > > useleep_range() with a delta of 0 makes no sense and only prevents the
> > > > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > > > is in non-atomic context the timer jitter is in the range of 10s of
> > > > microseconds anyway.
> > > >
> > > > This adds a note making it clear that a range of 0 is a bad idea.
> > >
> > > So I don't really have anything to do with the timer subsystem, I'm just
> > > their "consumer", so take this with a grain of salt.
> > >
> > > Documentation is good, but I don't think this will be enough.
> > >
> > > I think the only thing that will work is to detect and complain about
> > > things like this automatically. Some ideas:
> > >
> > > * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> > > might be drastic, but it would get the job done eventually.
> > >
> > > * If you want to avoid the runtime overhead (and complaints about the
> > > backtraces), you could wrap usleep_range() in a macro that does
> > > BUILD_BUG_ON(min == max) if the parameters are build time constants
> > > (they usually are). But you'd have to fix all the problem cases first.
> > >
> > > * You could try (to persuade Julia or Dan) to come up with a
> > > cocci/smatch check for usleep_range() calls where min == max, so we
> > > could get bug reports for this. This probably works on expressions, so
> > > this would catch also cases where the parameters aren't built time
> > > constants.
> > >
> >
> > I fully agree - without automation it is almost usless
> > the coccinelle spatch is a seperate patch and it is tested butnot yet
> > submitted.
> >
> > the spatch for this iss actually trivial
> >
> > @nulldelta@
> > constant C;
> > position p;
> > @@
> >
> > * usleep_range@p(C,C)
>
> People never use more complex expressions?
>
well yes
@nulldelta@
expression E;
position p;
@@

* usleep_range@p(E,E)

but that seems to be it.
and the vast majority is simply constants

thx!
hofrat

2016-12-14 00:27:40

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

a, On Tue, 2016-12-13 at 09:19 +0000, Nicholas Mc Guire wrote:
> On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> > On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > > useleep_range() with a delta of 0 makes no sense and only prevents the
> > > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > > is in non-atomic context the timer jitter is in the range of 10s of
> > > microseconds anyway.
> > >
> > > This adds a note making it clear that a range of 0 is a bad idea.
> >
> > So I don't really have anything to do with the timer subsystem, I'm just
> > their "consumer", so take this with a grain of salt.
> >
> > Documentation is good, but I don't think this will be enough.
> >
> > I think the only thing that will work is to detect and complain about
> > things like this automatically. Some ideas:
> >
> > * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> > might be drastic, but it would get the job done eventually.
> >
> > * If you want to avoid the runtime overhead (and complaints about the
> > backtraces), you could wrap usleep_range() in a macro that does
> > BUILD_BUG_ON(min == max) if the parameters are build time constants
> > (they usually are). But you'd have to fix all the problem cases first.
> >
> > * You could try (to persuade Julia or Dan) to come up with a
> > cocci/smatch check for usleep_range() calls where min == max, so we
> > could get bug reports for this. This probably works on expressions, so
> > this would catch also cases where the parameters aren't built timea,
> > constants.

You could also add a macro for usleep_range like

#define usleep_range(a, b) \
({ \
if (__builtin_constant_p(a) && __builtin_constant_p(b)) { \
if (a == b) \
__compiletime_warning("Better to use usleep_range with different values"); \
else if (a > b) \
__compiletime_error("usleep_range uses smaller value first"); \
} \
usleep_range(a, b); \
})

and add parentheses around the actual function
definition for usleep_range in kernel/time/timer.c
so the macro works and these messages get emitted
at compile-time.


2016-12-14 00:37:52

by Nicholas Mc Guire

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue, Dec 13, 2016 at 04:27:32PM -0800, Joe Perches wrote:
> a, On Tue, 2016-12-13 at 09:19 +0000, Nicholas Mc Guire wrote:
> > On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> > > On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > > > useleep_range() with a delta of 0 makes no sense and only prevents the
> > > > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > > > is in non-atomic context the timer jitter is in the range of 10s of
> > > > microseconds anyway.
> > > >
> > > > This adds a note making it clear that a range of 0 is a bad idea.
> > >
> > > So I don't really have anything to do with the timer subsystem, I'm just
> > > their "consumer", so take this with a grain of salt.
> > >
> > > Documentation is good, but I don't think this will be enough.
> > >
> > > I think the only thing that will work is to detect and complain about
> > > things like this automatically. Some ideas:
> > >
> > > * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> > > might be drastic, but it would get the job done eventually.
> > >
> > > * If you want to avoid the runtime overhead (and complaints about the
> > > backtraces), you could wrap usleep_range() in a macro that does
> > > BUILD_BUG_ON(min == max) if the parameters are build time constants
> > > (they usually are). But you'd have to fix all the problem cases first.
> > >
> > > * You could try (to persuade Julia or Dan) to come up with a
> > > cocci/smatch check for usleep_range() calls where min == max, so we
> > > could get bug reports for this. This probably works on expressions, so
> > > this would catch also cases where the parameters aren't built timea,
> > > constants.
>
> You could also add a macro for usleep_range like
>
> #define usleep_range(a, b) \
> ({ \
> if (__builtin_constant_p(a) && __builtin_constant_p(b)) { \
> if (a == b) \
> __compiletime_warning("Better to use usleep_range with different values"); \
> else if (a > b) \
> __compiletime_error("usleep_range uses smaller value first"); \
> } \
> usleep_range(a, b); \
> })
>

thanks for that "template"

> and add parentheses around the actual function
> definition for usleep_range in kernel/time/timer.c
> so the macro works and these messages get emitted
> at compile-time.
>
while compiletime warnings are a way to go I think that an
external tool is more effective than anoying eveyone during
build - ideally this type of issue is filtered out in the
subsystem trees or -next latest so getting it into a
coccinelle spatch and into one of the CI seems the most
resonable way to go. And as a side-effect tools external
to the build process allow analysis into the history of the
kernel development (like statistics on API usage and bug
history).

thx!
hofrat

2016-12-14 06:11:56

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Wed, 2016-12-14 at 00:37 +0000, Nicholas Mc Guire wrote:
> On Tue, Dec 13, 2016 at 04:27:32PM -0800, Joe Perches wrote:
> > a, On Tue, 2016-12-13 at 09:19 +0000, Nicholas Mc Guire wrote:
> > > On Tue, Dec 13, 2016 at 11:10:50AM +0200, Jani Nikula wrote:
> > > > On Tue, 13 Dec 2016, Nicholas Mc Guire <[email protected]> wrote:
> > > > > useleep_range() with a delta of 0 makes no sense and only prevents the
> > > > > timer subsystem from optimizing interrupts. As any user of usleep_range()
> > > > > is in non-atomic context the timer jitter is in the range of 10s of
> > > > > microseconds anyway.
> > > > >
> > > > > This adds a note making it clear that a range of 0 is a bad idea.
> > > >
> > > > So I don't really have anything to do with the timer subsystem, I'm just
> > > > their "consumer", so take this with a grain of salt.
> > > >
> > > > Documentation is good, but I don't think this will be enough.
> > > >
> > > > I think the only thing that will work is to detect and complain about
> > > > things like this automatically. Some ideas:
> > > >
> > > > * WARN_ON(min == max) or WARN_ON_ONCE(min == max) in usleep_range()
> > > > might be drastic, but it would get the job done eventually.
> > > >
> > > > * If you want to avoid the runtime overhead (and complaints about the
> > > > backtraces), you could wrap usleep_range() in a macro that does
> > > > BUILD_BUG_ON(min == max) if the parameters are build time constants
> > > > (they usually are). But you'd have to fix all the problem cases first.
> > > >
> > > > * You could try (to persuade Julia or Dan) to come up with a
> > > > cocci/smatch check for usleep_range() calls where min == max, so we
> > > > could get bug reports for this. This probably works on expressions, so
> > > > this would catch also cases where the parameters aren't built timea,
> > > > constants.
> >
> > You could also add a macro for usleep_range like
> >
> > #define usleep_range(a, b) \
> > ({ \
> > if (__builtin_constant_p(a) && __builtin_constant_p(b)) { \
> > if (a == b) \
> > __compiletime_warning("Better to use usleep_range with different values"); \
> > else if (a > b) \
> > __compiletime_error("usleep_range uses smaller value first"); \
> > } \
> > usleep_range(a, b); \
> > })
> >
>
> thanks for that "template"
>
> > and add parentheses around the actual function
> > definition for usleep_range in kernel/time/timer.c
> > so the macro works and these messages get emitted
> > at compile-time.
> >
>
> while compiletime warnings are a way to go I think that an
> external tool is more effective than anoying eveyone during
> build

I don't.

Annoying people at build-time is probably _the single most_
effective way to get source code defects fixed.

2016-12-27 21:56:42

by Pavel Machek

[permalink] [raw]
Subject: Re: [PATCH] doc: add note on usleep_range range

On Tue 2016-12-13 04:58:43, Nicholas Mc Guire wrote:
> useleep_range() with a delta of 0 makes no sense and only prevents the
> timer subsystem from optimizing interrupts. As any user of usleep_range()
> is in non-atomic context the timer jitter is in the range of 10s of
> microseconds anyway.
>
> This adds a note making it clear that a range of 0 is a bad idea.
>
> Signed-off-by: Nicholas Mc Guire <[email protected]>
> ---
>
> as of 4.9.0 there are about 20 cases of usleep_ranges() that have
> min==max and none of them really look like they are necessary, so
> it does seem like a relatively common misunderstanding worth
> noting in the documentation.
>
> Patch is against 4.9.0 (localversion-next is 20161212)
>
> Documentation/timers/timers-howto.txt | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/Documentation/timers/timers-howto.txt b/Documentation/timers/timers-howto.txt
> index 038f8c7..b5cdf82 100644
> --- a/Documentation/timers/timers-howto.txt
> +++ b/Documentation/timers/timers-howto.txt
> @@ -93,6 +93,13 @@ NON-ATOMIC CONTEXT:
> tolerances here are very situation specific, thus it
> is left to the caller to determine a reasonable range.
>
> + A range of 0, that is usleep_range(100,100) or the
> + like, do not make sense as this code is in a
> + non-atomic section and a system can not be expected
> + to have jitter 0. For any non-RT code any delta

Would it be possible to fix english here?

"to have zero jitter" at least. I believe it is "does not".

I don't see how atomic vs. non-atomic context makes difference. There
are sources of jitter that affect atomic context...

> + less than 50 microseconds probably is only preventing
> + timer subsystem optimization but providing no benefit.

And I don't trust you here. _If_ it prevents timer optimalization,
_then_ it provides benefit, at least in the average case.

Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html


Attachments:
(No filename) (2.00 kB)
signature.asc (181.00 B)
Digital signature
Download all attachments