Skip to contentSkip to conversation

jiffies: Cast to unsigned long for secs_to_jiffies() conversion

PATCH Applied Easwar Hariharan 4 participants 6 messages Jan 30, 2025 – Jan 31, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 jiffies: Cast to unsigned long for secs_to_jiffies() conversion

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing. Fix this by casting secs_to_jiffies() values as unsigned long.

Fixes b35108a51cf7ba ("jiffies: Define secs_to_jiffies()")
CC Andrew Morton <[email protected]>
---
 include/linux/jiffies.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/jiffies.h +1 -1
index ed945f42e064..0ea8c9887429 100644
@@ -537,7 +537,7 @@ static __always_inline unsigned long msecs_to_jiffies(const unsigned int m)
  *
  * Return: jiffies value
  */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)
 
 extern unsigned long __usecs_to_jiffies(const unsigned int u);
 #if !(USEC_PER_SEC % HZ)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 21 lines · Easwar Hariharan

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing. Fix this by casting secs_to_jiffies() values as unsigned long.

Fixes b35108a51cf7ba ("jiffies: Define secs_to_jiffies()")
CC Andrew Morton <[email protected]>
---
  include/linux/jiffies.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/jiffies.h +0 -0
index ed945f42e064..0ea8c9887429 100644

include/linux/jiffies.h static __always_inline unsigned long msecs_to_jiffies(const unsigned int m) · line 540

Quoted 5 lines
include/linux/jiffies.h +1 -1
@@ -537,7 +537,7 @@ static __always_inline unsigned long msecs_to_jiffies(const unsigned int m)
   *
   * Return: jiffies value
   */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)

Could you just switch the fun to an inline instead?

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 17 lines · Jiri Slaby
Quoted 16 lines · Easwar Hariharan

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing. Fix this by casting secs_to_jiffies() values as unsigned long.

Fixes b35108a51cf7ba ("jiffies: Define secs_to_jiffies()")
CC Andrew Morton <[email protected]>
---
  include/linux/jiffies.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

include/linux/jiffies.h static __always_inline unsigned long msecs_to_jiffies(const unsigned int m) · line 540

Quoted 10 lines
include/linux/jiffies.h +1 -1
index ed945f42e064..0ea8c9887429 100644
@@ -537,7 +537,7 @@ static __always_inline unsigned long msecs_to_jiffies(const unsigned int m)
   *
   * Return: jiffies value
   */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)

Could you just switch the fun to an inline instead?

It's a macro so it can be used in static initializers.

#3 lore raw
Quoted 31 lines · Thomas Gleixner
Quoted 17 lines · Jiri Slaby
Quoted 16 lines · Easwar Hariharan

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing. Fix this by casting secs_to_jiffies() values as unsigned long.

Fixes b35108a51cf7ba ("jiffies: Define secs_to_jiffies()")
CC Andrew Morton <[email protected]>
---
   include/linux/jiffies.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

include/linux/jiffies.h static __always_inline unsigned long msecs_to_jiffies(const unsigned int m) · line 540

Quoted 10 lines
include/linux/jiffies.h +1 -1
index ed945f42e064..0ea8c9887429 100644
@@ -537,7 +537,7 @@ static __always_inline unsigned long msecs_to_jiffies(const unsigned int m)
    *
    * Return: jiffies value
    */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)

Could you just switch the fun to an inline instead?

Quoted 1 line

It's a macro so it can be used in static initializers.

It's the only one from the *_to_jiffies() family we offer. And I fail to find such a use (by a quick grep, it only might be hidden)? People apparently use "* HZ" in initializers...

So sure, iff there is this intention for this very one, keep it as macro.

Signature · 2 lines
js
suse labs
#4 lore raw
Quoted 34 lines · Jiri Slaby
Quoted 33 lines · Thomas Gleixner
Quoted 31 lines · Jiri Slaby
Quoted 22 lines · Easwar Hariharan

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing. Fix this by casting secs_to_jiffies() values as unsigned long.

Fixes b35108a51cf7ba ("jiffies: Define secs_to_jiffies()")
CC Andrew Morton <[email protected]>

[email protected]/

---
   include/linux/jiffies.h | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/jiffies.h +0 -0
index ed945f42e064..0ea8c9887429 100644

include/linux/jiffies.h static __always_inline unsigned long · line 541

Quoted 6 lines
include/linux/jiffies.h +1 -1
@@ -537,7 +537,7 @@ static __always_inline unsigned long
 msecs_to_jiffies(const unsigned int m)
     *
     * Return: jiffies value
     */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)

Could you just switch the fun to an inline instead?

Quoted 8 lines
Quoted 1 line

It's a macro so it can be used in static initializers.

It's the only one from the *_to_jiffies() family we offer. And I fail to find such a use (by a quick grep, it only might be hidden)? People apparently use "* HZ" in initializers...

So sure, iff there is this intention for this very one, keep it as macro.

Yes, the intent is to convert those usages of "* HZ" to use secs_to_jiffies() for better readability.

Thanks,
Easwar

#5 lore raw

[tip: timers/urgent] jiffies: Cast to unsigned long in secs_to_jiffies() conversion

In reply to Easwar Hariharan
The following commit has been merged into the timers/urgent branch of tip:

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

Commit-ID:     bb2784d9ab49587ba4fbff37a319fff2924db289
Gitweb:        https://git.kernel.org/tip/bb2784d9ab49587ba4fbff37a319fff2924db289
Author:        Easwar Hariharan <[email protected]>
AuthorDate:    Thu, 30 Jan 2025 19:26:58 
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 31 Jan 2025 09:30:49 +01:00

jiffies: Cast to unsigned long in secs_to_jiffies() conversion

While converting users of msecs_to_jiffies(), lkp reported that some range checks would always be true because of the mismatch between the implied int value of secs_to_jiffies() vs the unsigned long return value of the msecs_to_jiffies() calls it was replacing.

Fix this by casting the secs_to_jiffies() input value to unsigned long.

---
 include/linux/jiffies.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/jiffies.h +1 -1
index ed945f4..0ea8c98 100644
@@ -537,7 +537,7 @@ static __always_inline unsigned long msecs_to_jiffies(const unsigned int m)
  *
  * Return: jiffies value
  */
-#define secs_to_jiffies(_secs) ((_secs) * HZ)
+#define secs_to_jiffies(_secs) (unsigned long)((_secs) * HZ)
 
 extern unsigned long __usecs_to_jiffies(const unsigned int u);
 #if !(USEC_PER_SEC % HZ)