mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Anna-Maria Behnsen" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: timers/core] delay: Rework udelay and ndelay
Date: Tue, 15 Oct 2024 22:41:24 -0000	[thread overview]
Message-ID: <172903208422.1442.9805747352086183936.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20241014-devel-anna-maria-b4-timers-flseep-v3-6-dc8b907cb62f@linutronix.de>

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     19e2d91d8cb1f333adf04731f2788ff6ca06cebd
Gitweb:        https://git.kernel.org/tip/19e2d91d8cb1f333adf04731f2788ff6ca06cebd
Author:        Anna-Maria Behnsen <anna-maria@linutronix.de>
AuthorDate:    Mon, 14 Oct 2024 10:22:23 +02:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Wed, 16 Oct 2024 00:36:47 +02:00

delay: Rework udelay and ndelay

udelay() as well as ndelay() are defines and no functions and are using
constants to be able to transform a sleep time into loops and to prevent
too long udelays/ndelays. There was a compiler error with non-const 8 bit
arguments which was fixed by commit a87e553fabe8 ("asm-generic: delay.h fix
udelay and ndelay for 8 bit args"). When using a function, the non-const 8
bit argument is type casted and the problem would be gone.

Transform udelay() and ndelay() into proper functions, remove the no longer
and confusing division, add defines for the magic values and add some
explanations as well.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
Link: https://lore.kernel.org/all/20241014-devel-anna-maria-b4-timers-flseep-v3-6-dc8b907cb62f@linutronix.de

---
 include/asm-generic/delay.h | 65 ++++++++++++++++++++----------------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/include/asm-generic/delay.h b/include/asm-generic/delay.h
index a8cee41..76cf237 100644
--- a/include/asm-generic/delay.h
+++ b/include/asm-generic/delay.h
@@ -2,6 +2,9 @@
 #ifndef __ASM_GENERIC_DELAY_H
 #define __ASM_GENERIC_DELAY_H
 
+#include <linux/math.h>
+#include <vdso/time64.h>
+
 /* Undefined functions to get compile-time errors */
 extern void __bad_udelay(void);
 extern void __bad_ndelay(void);
@@ -12,13 +15,18 @@ extern void __const_udelay(unsigned long xloops);
 extern void __delay(unsigned long loops);
 
 /*
- * Implementation details:
- *
- * * 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) -> udelay
- * * 0x5 is 2**32 / 1000000000 (rounded up) -> ndelay
+ * The microseconds/nanosecond delay multiplicators are used to convert a
+ * constant microseconds/nanoseconds value to a value which can be used by the
+ * architectures specific implementation to transform it into loops.
+ */
+#define UDELAY_CONST_MULT	((unsigned long)DIV_ROUND_UP(1ULL << 32, USEC_PER_SEC))
+#define NDELAY_CONST_MULT	((unsigned long)DIV_ROUND_UP(1ULL << 32, NSEC_PER_SEC))
+
+/*
+ * The maximum constant udelay/ndelay value picked out of thin air to prevent
+ * too long constant udelays/ndelays.
  */
+#define DELAY_CONST_MAX   20000
 
 /**
  * udelay - Inserting a delay based on microseconds with busy waiting
@@ -45,17 +53,17 @@ extern void __delay(unsigned long loops);
  * #. cache behaviour affecting the time it takes to execute the loop function.
  * #. CPU clock rate changes.
  */
-#define udelay(n)							\
-	({								\
-		if (__builtin_constant_p(n)) {				\
-			if ((n) / 20000 >= 1)				\
-				 __bad_udelay();			\
-			else						\
-				__const_udelay((n) * 0x10c7ul);		\
-		} else {						\
-			__udelay(n);					\
-		}							\
-	})
+static __always_inline void udelay(unsigned long usec)
+{
+	if (__builtin_constant_p(usec)) {
+		if (usec >= DELAY_CONST_MAX)
+			__bad_udelay();
+		else
+			__const_udelay(usec * UDELAY_CONST_MULT);
+	} else {
+		__udelay(usec);
+	}
+}
 
 /**
  * ndelay - Inserting a delay based on nanoseconds with busy waiting
@@ -63,16 +71,17 @@ extern void __delay(unsigned long loops);
  *
  * See udelay() for basic information about ndelay() and it's variants.
  */
-#define ndelay(n)							\
-	({								\
-		if (__builtin_constant_p(n)) {				\
-			if ((n) / 20000 >= 1)				\
-				__bad_ndelay();				\
-			else						\
-				__const_udelay((n) * 5ul);		\
-		} else {						\
-			__ndelay(n);					\
-		}							\
-	})
+static __always_inline void ndelay(unsigned long nsec)
+{
+	if (__builtin_constant_p(nsec)) {
+		if (nsec >= DELAY_CONST_MAX)
+			__bad_udelay();
+		else
+			__const_udelay(nsec * NDELAY_CONST_MULT);
+	} else {
+		__udelay(nsec);
+	}
+}
+#define ndelay(x) ndelay(x)
 
 #endif /* __ASM_GENERIC_DELAY_H */

  reply	other threads:[~2024-10-15 22:41 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14  8:22 [PATCH v3 00/16] timers: Cleanup delay/sleep related mess Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 01/16] MAINTAINERS: Add missing file include/linux/delay.h Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 02/16] timers: Move *sleep*() and timeout functions into a separate file Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 03/16] timers: Update schedule_[hr]timeout*() related function descriptions Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 04/16] timers: Rename usleep_idle_range() to usleep_range_idle() Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 05/16] timers: Update function descriptions of sleep/delay related functions Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 06/16] delay: Rework udelay and ndelay Anna-Maria Behnsen
2024-10-15 22:41   ` tip-bot2 for Anna-Maria Behnsen [this message]
2024-10-14  8:22 ` [PATCH v3 07/16] timers: Adjust flseep() to reflect reality Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 08/16] mm/damon/core: Use generic upper bound recommondation for usleep_range() Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 09/16] timers: Add a warning to usleep_range_state() for wrong order of arguments Anna-Maria Behnsen
2024-10-15 13:54   ` Frederic Weisbecker
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 10/16] checkpatch: Remove links to outdated documentation Anna-Maria Behnsen
2024-10-15 14:08   ` Frederic Weisbecker
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 11/16] regulator: core: Use fsleep() to get best sleep mechanism Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 12/16] iopoll/regmap/phy/snd: Fix comment referencing outdated timer documentation Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 13/16] powerpc/rtas: Use fsleep() to minimize additional sleep duration Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 14/16] media: anysee: Fix and remove outdated comment Anna-Maria Behnsen
2024-10-15 14:11   ` Frederic Weisbecker
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 15/16] timers/Documentation: Cleanup delay/sleep documentation Anna-Maria Behnsen
2024-10-15 22:41   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-10-14  8:22 ` [PATCH v3 16/16] checkpatch: Remove broken sleep/delay related checks Anna-Maria Behnsen
2024-10-15 14:22   ` Frederic Weisbecker
2024-10-16  9:55     ` Anna-Maria Behnsen
2024-10-16 10:05   ` [PATCH v4] " Anna-Maria Behnsen
2024-10-18 12:31     ` Frederic Weisbecker
2024-10-14 16:22 ` (subset) [PATCH v3 00/16] timers: Cleanup delay/sleep related mess Mark Brown
2024-10-18  8:06   ` Anna-Maria Behnsen
2024-10-18 18:57     ` Mark Brown
2024-10-18 18:58     ` 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=172903208422.1442.9805747352086183936.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=anna-maria@linutronix.de \
    --cc=frederic@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@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

all inboxes | Powered by JetHome®