public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>
Cc: linux-kernel@vger.kernel.org, Len Brown <len.brown@intel.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org,
	Andrew Morton <akpm@linuxfoundation.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>
Subject: Re: [PATCH 06/15] timers: Update function descriptions of sleep/delay related functions
Date: Thu, 05 Sep 2024 08:59:20 +0200	[thread overview]
Message-ID: <87frqe60xj.ffs@tglx> (raw)
In-Reply-To: <20240904-devel-anna-maria-b4-timers-flseep-v1-6-e98760256370@linutronix.de>

On Wed, Sep 04 2024 at 15:04, Anna-Maria Behnsen wrote:
> +/**
> + * udelay - Inserting a delay based on microseconds with busy waiting
> + * @n:	requested delay in microseconds

....

> + * Impelementation details for udelay() only:

Implementation

> + * * The weird n/20000 thing suppresses a "comparison is always false due to
> + *   limited range of data type" warning with non-const 8-bit arguments.
> + * * 0x10c7 is 2**32 / 1000000 (rounded up)
>   */

That spello aside, I don't see how this information is interesting for
the user of udelay(). It's really a implementation detail and the user
does not care about this piece of art at all.

Though that made me look at this voodoo and the magic constants in
detail.  The division was added in a87e553fabe8 ("asm-generic: delay.h
fix udelay and ndelay for 8 bit args") to work around a compiler which
is upset about the comparision even when __builtin_constant_p(arg) is
false:

   warning: comparison is always false due to limited range of data type

The changelog is silent about the compiler version. I assume it's clang
because clang still complains on a plain (n) > 20000 when udelay() is
invoked with a u8 variable as argument:

   warning: result of comparison of constant 20000 with
            expression of type 'unsigned char' is always false
            [-Wtautological-constant-out-of-range-compare]

while gcc does not care.

The change log explains further that type casting 'n' in the comparison
does not cure it. Contemporary clang seems to be less stupid and

	if ((unsigned long)(n) >= 20000)			\

compiles just fine. Though assumed that some older clang version failed
and is still allowed to be used for compiling the kernel we have to work
around it.

However, instead of proliferating this voodoo can we please convert it
into something comprehensible?

/*
 * The microseconds delay multiplicator is used to convert a constant
 * microseconds value to a <INSERT COHERENT EXPLANATION>.
 */
#define UDELAY_CONST_MULT  ((unsigned long)DIV_ROUND_UP(1ULL << 32, USEC_PER_SEC))

/*
 * The maximum constant udelay value picked out of thin air
 * to avoid <INSERT COHERENT EXPLANATION>.
 */
#define UDELAY_CONST_MAX   20000

/**
 * udelay - .....
 */
static __always_inline void udelay(unsigned long usec)
{
        /*
	 * <INSERT COHERENT EXPLANATION> for this construct
         */
	if (__builtin_constant_p(usec)) {
		if (usec >= UDELAY_CONST_MAX)
			__bad_udelay();
		else
			__const_udelay(usec * UDELAY_CONST_MULT);
	} else {
		__udelay(usec);
	}
}

Both gcc and clang optimize this correctly with -O2. If there are
ancient compilers which fail to do so: *shrug*. 

> + * See udelay() for basic information about ndelay() and it's variants.
> + *
> + * Impelmentation details for ndelay():

vs.

> + * Impelementation details for udelay() only:

above. Can you please make your mind up which mis-spelled variant to
pick? :)

>  /**
>   * msleep_interruptible - sleep waiting for signals
> - * @msecs: Time in milliseconds to sleep for
> + * @msecs:	Requested sleep duration in milliseconds
> + *
> + * See msleep() for some basic information.
> + *
> + * The difference between msleep() and msleep_interruptible() is that the sleep
> + * could be interrupted by a signal delivery and then returns early.
> + *
> + * Returns the remaining time of the sleep duration transformed to msecs (see
> + * schedule_timeout() for details).

  Returns: The remaining ...

Thanks,

        tglx

  parent reply	other threads:[~2024-09-05  6:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04 13:04 [PATCH 00/15] timers: Cleanup delay/sleep related mess Anna-Maria Behnsen
2024-09-04 13:04 ` [PATCH 06/15] timers: Update function descriptions of sleep/delay related functions Anna-Maria Behnsen
2024-09-04 14:30   ` Arnd Bergmann
2024-09-05  6:59   ` Thomas Gleixner [this message]
2024-09-05 16:07     ` Thomas Gleixner
2024-09-05 19:49       ` Anna-Maria Behnsen
2024-09-04 14:44 ` [PATCH 00/15] timers: Cleanup delay/sleep related mess Rafael J. Wysocki
2024-10-17 14:19 ` (subset) " Mark Brown

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=87frqe60xj.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=akpm@linuxfoundation.org \
    --cc=anna-maria@linutronix.de \
    --cc=arnd@arndb.de \
    --cc=corbet@lwn.net \
    --cc=frederic@kernel.org \
    --cc=len.brown@intel.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rafael@kernel.org \
    /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