All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hrtimer: Fix the incorrect initialization of timer->is_hard
@ 2025-03-21  2:03 cuiguoqi
  2025-03-21 16:45 ` Thomas Gleixner
  0 siblings, 1 reply; 6+ messages in thread
From: cuiguoqi @ 2025-03-21  2:03 UTC (permalink / raw)
  To: info, anna-maria
  Cc: sales, frederic, tglx, linux-kernel, bigeasy, clrkwllms, rostedt,
	linux-rt-devel, cuiguoqi

When PREEMPT_RT is disabled,there is a possibility that
timer->is_hard will be incorrectly initialized.
When creating a high-resolution timer and setting the mode to
HRTIMER_MODE_ABS,the timer->is_hard will be incorrectly initialized.

Pseudocode logic:
1. mode
   >> HRTIMER_MODE_ABS	= 0x00

2. bool softtimer = !!(mode & HRTIMER_MODE_SOFT);	//!PREEMPT_RT
   base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;	//!PREEMPT_RT
   > softtimer = false;				//!PREEMPT_RT
   > base = 0;						//hard mode

3. timer->is_soft = softtimer;
   > timer->is_soft = flase;

4. timer->is_hard = !!(mode & HRTIMER_MODE_HARD);	!PREEMPT_RT
   >  timer->is_hard = flase;				//error
   >  Based on point 2, by default, the hard mode is selected.

Of course, if PREEMPT_RT is enabled, timer->is_hard is correctly set.
   bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
   if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
	softtimer = true;
   timer->is_soft = softtimer;				//true
   timer->is_hard = !!(mode & HRTIMER_MODE_HARD);	//flase

Signed-off-by: cuiguoqi <cuiguoqi@kylinos.cn>
---
 kernel/time/hrtimer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 80fe3749d2db..9d8defb1e9b1 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1571,7 +1571,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
 	base += hrtimer_clockid_to_base(clock_id);
 	timer->is_soft = softtimer;
-	timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
+	timer->is_hard = !softtimer;
 	timer->base = &cpu_base->clock_base[base];
 	timerqueue_init(&timer->node);
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH] hrtimer: Fix the incorrect initialization of timer->is_hard
@ 2025-03-21  1:51 cuiguoqi
  0 siblings, 0 replies; 6+ messages in thread
From: cuiguoqi @ 2025-03-21  1:51 UTC (permalink / raw)
  To: anna-maria
  Cc: frederic, tglx, linux-kernel, bigeasy, clrkwllms, rostedt,
	linux-rt-devel, cuiguoqi

When PREEMPT_RT is disabled,there is a possibility that
timer->is_hard will be incorrectly initialized.
When creating a high-resolution timer and setting the mode to
HRTIMER_MODE_ABS,the timer->is_hard will be incorrectly initialized.

Pseudocode logic:
1. mode
   >> HRTIMER_MODE_ABS	= 0x00

2. bool softtimer = !!(mode & HRTIMER_MODE_SOFT);	//!PREEMPT_RT
   base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;	//!PREEMPT_RT
   > softtimer = false;				//!PREEMPT_RT
   > base = 0;						//hard mode

3. timer->is_soft = softtimer;
   > timer->is_soft = flase;

4. timer->is_hard = !!(mode & HRTIMER_MODE_HARD);	!PREEMPT_RT
   >  timer->is_hard = flase;				//error
   >  Based on point 2, by default, the hard mode is selected.

Of course, if PREEMPT_RT is enabled, timer->is_hard is correctly set.
   bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
   if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
	softtimer = true;
   timer->is_soft = softtimer;				//true
   timer->is_hard = !!(mode & HRTIMER_MODE_HARD);	//flase

Signed-off-by: cuiguoqi <cuiguoqi@kylinos.cn>
---
 kernel/time/hrtimer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 80fe3749d2db..9d8defb1e9b1 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1571,7 +1571,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
 	base += hrtimer_clockid_to_base(clock_id);
 	timer->is_soft = softtimer;
-	timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
+	timer->is_hard = !softtimer;
 	timer->base = &cpu_base->clock_base[base];
 	timerqueue_init(&timer->node);
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-24 13:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-21  2:03 [PATCH] hrtimer: Fix the incorrect initialization of timer->is_hard cuiguoqi
2025-03-21 16:45 ` Thomas Gleixner
2025-03-22  8:24   ` cuiguoqi
2025-03-24  8:49     ` Sebastian Andrzej Siewior
2025-03-24 13:15       ` cuiguoqi
  -- strict thread matches above, loose matches on Subject: below --
2025-03-21  1:51 cuiguoqi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.