mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Grant Likely <grant.likely@secretlab.ca>
To: Timur Tabi <timur@freescale.com>
Cc: linux-kernel@vger.kernel.org, rdreier@cisco.com,
	jirislaby@gmail.com, peterz@infradead.org, will.newton@gmail.com,
	hancockrwd@gmail.com, jeremy@goop.org
Subject: Re: [PATCH v4] introduce macro spin_event_timeout()
Date: Tue, 10 Mar 2009 12:41:47 -0600	[thread overview]
Message-ID: <fa686aa40903101141l33fbfc7cha0d2d85f6e1cb795@mail.gmail.com> (raw)
In-Reply-To: <1236699004-1863-1-git-send-email-timur@freescale.com>

On Tue, Mar 10, 2009 at 9:30 AM, Timur Tabi <timur@freescale.com> wrote:
> The macro spin_event_timeout() takes a condition and timeout value
> (in microseconds) as parameters.  It spins until either the condition is true
> or the timeout expires.  It returns zero if the timeout expires first, non-zero
> otherwise.
>
> This primary purpose of this macro is to poll on a hardware register until a
> status bit changes.  The timeout ensures that the loop still terminates if the
> bit doesn't change as expected.  This macro makes it easier for driver
> developers to perform this kind of operation properly.
>
> Signed-off-by: Timur Tabi <timur@freescale.com>
> ---
>
> v4: removed cpu_relax (redundant), changed timeout to unsigned long
>
> v3: eliminated secondary evaluation of condition, replaced jiffies with udelay
>
> v2: added cpu_relax and time_before
>
>  include/linux/delay.h |   27 +++++++++++++++++++++++++++
>  1 files changed, 27 insertions(+), 0 deletions(-)
>
> diff --git a/include/linux/delay.h b/include/linux/delay.h
> index fd832c6..4af6df6 100644
> --- a/include/linux/delay.h
> +++ b/include/linux/delay.h
> @@ -51,4 +51,31 @@ static inline void ssleep(unsigned int seconds)
>        msleep(seconds * 1000);
>  }
>
> +/**
> + * spin_event_timeout - spin until a condition gets true or a timeout elapses
> + * @condition: a C expression to evalate
> + * @timeout: timeout, in microseconds
> + *
> + * The process spins until the @condition evaluates to true (non-zero) or
> + * the @timeout elapses.
> + *
> + * This primary purpose of this macro is to poll on a hardware register
> + * until a status bit changes.  The timeout ensures that the loop still
> + * terminates if the bit never changes.
> + *
> + * The return value is the number of microseconds left in the timeout, so if
> + * the return value is non-zero, then it means the condition is true.
> + *
> + * Short-circuit evaluation in the while-loop ensures that if the condition
> + * becomes true exactly when the timeout expires, non-zero will still be
> + * returned.
> + */
> +#define spin_event_timeout(condition, timeout)                         \
> +({                                                                     \
> +       unsigned long __timeout = timeout;                              \
> +       while (!(condition) && --__timeout)                             \
> +               udelay(1);                                              \

Do you really want to do a udelay() here?  Or any other kind of delay
for that matter?  When using a delay then, unless the condition is
immediately true, the code will burn a minimum of 1us of cycles, even
if the condition does become true right after the first read.  If the
CPU is spinning anyway, then it should be doing using those cycles to
try and bail out as soon as possible.

I typically use something in the form of this pattern on powerpc code
when I absolutely have to busywait.
    int end_ts = get_tbl() + some_delay;  /* note that this is signed */
    while (end_ts - get_tbl() > 0)
        if (condition)
            break;

This macro also looks like a tempting unbounded latency tarpit to fall
into when people start using it in critical regions,  but that goes
for code using a non-timed-out busywait also.

g.

-- 
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.

  parent reply	other threads:[~2009-03-10 18:48 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-10 15:30 Timur Tabi
2009-03-10 15:35 ` Alan Cox
2009-03-10 15:50   ` Timur Tabi
2009-03-10 16:05     ` Will Newton
2009-03-10 16:11       ` Timur Tabi
2009-03-11  0:01   ` Benjamin Herrenschmidt
2009-03-11  0:37     ` Alan Cox
2009-03-11 16:48       ` Timur Tabi
2009-03-11 16:58         ` Alan Cox
2009-03-11 18:18           ` Timur Tabi
2009-03-11 21:58             ` Benjamin Herrenschmidt
2009-03-12  2:45               ` Grant Likely
2009-03-12 15:54                 ` Timur Tabi
2009-03-12 16:01                   ` Grant Likely
2009-03-12 16:19                     ` Timur Tabi
2009-03-12 16:50                       ` Peter Zijlstra
2009-03-12 19:05                         ` Timur Tabi
2009-03-13  3:03                           ` Benjamin Herrenschmidt
2009-03-13  4:51                             ` Grant Likely
2009-03-10 18:41 ` Grant Likely [this message]
2009-03-10 19:04   ` Timur Tabi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fa686aa40903101141l33fbfc7cha0d2d85f6e1cb795@mail.gmail.com \
    --to=grant.likely@secretlab.ca \
    --cc=hancockrwd@gmail.com \
    --cc=jeremy@goop.org \
    --cc=jirislaby@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rdreier@cisco.com \
    --cc=timur@freescale.com \
    --cc=will.newton@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome