Skip to content

Commit 150e18d

Browse files
Andy Rosscarlescufi
authored andcommitted
kernel/timeout: Fix 32 bit rollover conditions
There were two spots where CONFIG_TIMEOUT_64BIT wasn't being correctly honored, leading to the possibility of long timeouts overflowing internally and doing weird stuff. Fixes #26248 Signed-off-by: Andy Ross <[email protected]>
1 parent 95e4eb8 commit 150e18d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

include/kernel_structs.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ typedef void (*_timeout_func_t)(struct _timeout *t);
220220

221221
struct _timeout {
222222
sys_dnode_t node;
223-
int32_t dticks;
224223
_timeout_func_t fn;
224+
#ifdef CONFIG_TIMEOUT_64BIT
225+
/* Can't use k_ticks_t for header dependency reasons */
226+
uint64_t dticks;
227+
#else
228+
uint32_t dticks;
229+
#endif
225230
};
226231

227232
/* kernel spinlock type */

include/sys_clock.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,18 @@ typedef struct {
8484
#define Z_TIMEOUT_NO_WAIT ((k_timeout_t) {})
8585
#define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { .ticks = (t) })
8686
#define Z_FOREVER Z_TIMEOUT_TICKS(K_TICKS_FOREVER)
87-
#define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS(k_ms_to_ticks_ceil32(MAX(t, 0)))
88-
#define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS(k_us_to_ticks_ceil32(MAX(t, 0)))
89-
#define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS(k_ns_to_ticks_ceil32(MAX(t, 0)))
90-
#define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS(k_cyc_to_ticks_ceil32(MAX(t, 0)))
87+
88+
#ifdef CONFIG_TIMEOUT_64BIT
89+
# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS(k_ms_to_ticks_ceil64(MAX(t, 0)))
90+
# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS(k_us_to_ticks_ceil64(MAX(t, 0)))
91+
# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS(k_ns_to_ticks_ceil64(MAX(t, 0)))
92+
# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS(k_cyc_to_ticks_ceil64(MAX(t, 0)))
93+
#else
94+
# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS(k_ms_to_ticks_ceil32(MAX(t, 0)))
95+
# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS(k_us_to_ticks_ceil32(MAX(t, 0)))
96+
# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS(k_ns_to_ticks_ceil32(MAX(t, 0)))
97+
# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS(k_cyc_to_ticks_ceil32(MAX(t, 0)))
98+
#endif
9199

92100
/* Converts between absolute timeout expiration values (packed into
93101
* the negative space below K_TICKS_FOREVER) and (non-negative) delta

0 commit comments

Comments
 (0)