The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
@ 2023-07-17  9:07 walter.chang
  2023-07-17  9:19 ` Marc Zyngier
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: walter.chang @ 2023-07-17  9:07 UTC (permalink / raw)
  To: Mark Rutland, Marc Zyngier, Daniel Lezcano, Thomas Gleixner,
	Matthias Brugger, AngeloGioacchino Del Regno
  Cc: wsd_upstream, stanley.chu, Chun-hung.Wu, Freddy.Hsin,
	walter.chang, stable, linux-arm-kernel, linux-kernel,
	linux-mediatek

From: Walter Chang <walter.chang@mediatek.com>

Due to the fact that the use of `writeq_relaxed()` to program CVAL is
not guaranteed to be atomic, it is necessary to disable the timer before
programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired,
there is a possibility of unexpected behavior occurring: when the CPU
enters the idle state during this period, and if the CPU's local event
is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
  tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
    __tick_broadcast_oneshot_control()
      ___tick_broadcast_oneshot_control()
        tick_broadcast_set_event()
          clockevents_program_event()
            set_next_event_mem()

During this process, the MMIO timer remains enabled while programming
CVAL. To prevent such behavior, disable timer explicitly prior to
programming CVAL.

Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
Cc: stable@vger.kernel.org
Signed-off-by: Walter Chang <walter.chang@mediatek.com>
---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c
index e733a2a1927a..7dd2c615bce2 100644
--- a/drivers/clocksource/arm_arch_timer.c
+++ b/drivers/clocksource/arm_arch_timer.c
@@ -792,6 +792,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
 	u64 cnt;
 
 	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+	}
+
 	ctrl |= ARCH_TIMER_CTRL_ENABLE;
 	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
 
-- 
2.18.0


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

* Re: [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
  2023-07-17  9:07 [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL walter.chang
@ 2023-07-17  9:19 ` Marc Zyngier
  2023-07-17  9:22 ` AngeloGioacchino Del Regno
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Marc Zyngier @ 2023-07-17  9:19 UTC (permalink / raw)
  To: walter.chang
  Cc: Mark Rutland, Daniel Lezcano, Thomas Gleixner, Matthias Brugger,
	AngeloGioacchino Del Regno, wsd_upstream, stanley.chu,
	Chun-hung.Wu, Freddy.Hsin, stable, linux-arm-kernel, linux-kernel,
	linux-mediatek

On Mon, 17 Jul 2023 10:07:34 +0100,
<walter.chang@mediatek.com> wrote:
> 
> From: Walter Chang <walter.chang@mediatek.com>
> 
> Due to the fact that the use of `writeq_relaxed()` to program CVAL is
> not guaranteed to be atomic, it is necessary to disable the timer before
> programming CVAL.
> 
> However, if the MMIO timer is already enabled and has not yet expired,
> there is a possibility of unexpected behavior occurring: when the CPU
> enters the idle state during this period, and if the CPU's local event
> is earlier than the broadcast event, the following process occurs:
> 
> tick_broadcast_enter()
>   tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
>     __tick_broadcast_oneshot_control()
>       ___tick_broadcast_oneshot_control()
>         tick_broadcast_set_event()
>           clockevents_program_event()
>             set_next_event_mem()
> 
> During this process, the MMIO timer remains enabled while programming
> CVAL. To prevent such behavior, disable timer explicitly prior to
> programming CVAL.
> 
> Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
> Cc: stable@vger.kernel.org
> Signed-off-by: Walter Chang <walter.chang@mediatek.com>

Acked-by: Marc Zyngier <maz@kernel.org>

	M.

-- 
Without deviation from the norm, progress is not possible.

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

* Re: [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
  2023-07-17  9:07 [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL walter.chang
  2023-07-17  9:19 ` Marc Zyngier
@ 2023-07-17  9:22 ` AngeloGioacchino Del Regno
  2023-08-18  9:29 ` Walter Chang (張維哲)
  2023-08-31  1:31 ` [tip: timers/core] " tip-bot2 for Walter Chang
  3 siblings, 0 replies; 6+ messages in thread
From: AngeloGioacchino Del Regno @ 2023-07-17  9:22 UTC (permalink / raw)
  To: walter.chang, Mark Rutland, Marc Zyngier, Daniel Lezcano,
	Thomas Gleixner, Matthias Brugger
  Cc: wsd_upstream, stanley.chu, Chun-hung.Wu, Freddy.Hsin, stable,
	linux-arm-kernel, linux-kernel, linux-mediatek

Il 17/07/23 11:07, walter.chang@mediatek.com ha scritto:
> From: Walter Chang <walter.chang@mediatek.com>
> 
> Due to the fact that the use of `writeq_relaxed()` to program CVAL is
> not guaranteed to be atomic, it is necessary to disable the timer before
> programming CVAL.
> 
> However, if the MMIO timer is already enabled and has not yet expired,
> there is a possibility of unexpected behavior occurring: when the CPU
> enters the idle state during this period, and if the CPU's local event
> is earlier than the broadcast event, the following process occurs:
> 
> tick_broadcast_enter()
>    tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
>      __tick_broadcast_oneshot_control()
>        ___tick_broadcast_oneshot_control()
>          tick_broadcast_set_event()
>            clockevents_program_event()
>              set_next_event_mem()
> 
> During this process, the MMIO timer remains enabled while programming
> CVAL. To prevent such behavior, disable timer explicitly prior to
> programming CVAL.
> 
> Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
> Cc: stable@vger.kernel.org
> Signed-off-by: Walter Chang <walter.chang@mediatek.com>

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>

> ---
>   drivers/clocksource/arm_arch_timer.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c
> index e733a2a1927a..7dd2c615bce2 100644
> --- a/drivers/clocksource/arm_arch_timer.c
> +++ b/drivers/clocksource/arm_arch_timer.c
> @@ -792,6 +792,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
>   	u64 cnt;
>   
>   	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
> +
> +	/* Timer must be disabled before programming CVAL */
> +	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
> +		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
> +		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
> +	}
> +
>   	ctrl |= ARCH_TIMER_CTRL_ENABLE;
>   	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
>   


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

* Re: [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
  2023-07-17  9:07 [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL walter.chang
  2023-07-17  9:19 ` Marc Zyngier
  2023-07-17  9:22 ` AngeloGioacchino Del Regno
@ 2023-08-18  9:29 ` Walter Chang (張維哲)
  2023-08-18 10:06   ` Daniel Lezcano
  2023-08-31  1:31 ` [tip: timers/core] " tip-bot2 for Walter Chang
  3 siblings, 1 reply; 6+ messages in thread
From: Walter Chang (張維哲) @ 2023-08-18  9:29 UTC (permalink / raw)
  To: maz@kernel.org, mark.rutland@arm.com, daniel.lezcano@linaro.org,
	tglx@linutronix.de, matthias.bgg@gmail.com,
	angelogioacchino.delregno@collabora.com
  Cc: linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org,
	wsd_upstream, stable@vger.kernel.org,
	Chun-Hung Wu (巫駿宏),
	linux-arm-kernel@lists.infradead.org,
	Stanley Chu (朱原陞),
	Freddy Hsin (辛恒豐)

On Mon, 2023-07-17 at 17:07 +0800, walter.chang@mediatek.com wrote:
> From: Walter Chang <walter.chang@mediatek.com>
> 
> Due to the fact that the use of `writeq_relaxed()` to program CVAL is
> not guaranteed to be atomic, it is necessary to disable the timer
> before
> programming CVAL.
> 
> However, if the MMIO timer is already enabled and has not yet
> expired,
> there is a possibility of unexpected behavior occurring: when the CPU
> enters the idle state during this period, and if the CPU's local
> event
> is earlier than the broadcast event, the following process occurs:
> 
> tick_broadcast_enter()
>   tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
>     __tick_broadcast_oneshot_control()
>       ___tick_broadcast_oneshot_control()
>         tick_broadcast_set_event()
>           clockevents_program_event()
>             set_next_event_mem()
> 
> During this process, the MMIO timer remains enabled while programming
> CVAL. To prevent such behavior, disable timer explicitly prior to
> programming CVAL.
> 
> Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO
> timer programming over to CVAL")
> Cc: stable@vger.kernel.org
> Signed-off-by: Walter Chang <walter.chang@mediatek.com>
> ---
>  drivers/clocksource/arm_arch_timer.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/clocksource/arm_arch_timer.c
> b/drivers/clocksource/arm_arch_timer.c
> index e733a2a1927a..7dd2c615bce2 100644
> --- a/drivers/clocksource/arm_arch_timer.c
> +++ b/drivers/clocksource/arm_arch_timer.c
> @@ -792,6 +792,13 @@ static __always_inline void
> set_next_event_mem(const int access, unsigned long e
>  	u64 cnt;
>  
>  	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
> +
> +	/* Timer must be disabled before programming CVAL */
> +	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
> +		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
> +		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl,
> clk);
> +	}
> +
>  	ctrl |= ARCH_TIMER_CTRL_ENABLE;
>  	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
>  

Gentle ping for this patch.

Thanks,
Walter Chang

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

* Re: [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
  2023-08-18  9:29 ` Walter Chang (張維哲)
@ 2023-08-18 10:06   ` Daniel Lezcano
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Lezcano @ 2023-08-18 10:06 UTC (permalink / raw)
  To: Walter Chang (張維哲), maz@kernel.org,
	mark.rutland@arm.com, tglx@linutronix.de, matthias.bgg@gmail.com,
	angelogioacchino.delregno@collabora.com
  Cc: linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org,
	wsd_upstream, stable@vger.kernel.org,
	Chun-Hung Wu (巫駿宏),
	linux-arm-kernel@lists.infradead.org,
	Stanley Chu (朱原陞),
	Freddy Hsin (辛恒豐)

On 18/08/2023 11:29, Walter Chang (張維哲) wrote:
> On Mon, 2023-07-17 at 17:07 +0800, walter.chang@mediatek.com wrote:
>> From: Walter Chang <walter.chang@mediatek.com>
>> 
>> Due to the fact that the use of `writeq_relaxed()` to program CVAL is
>> not guaranteed to be atomic, it is necessary to disable the timer
>> before
>> programming CVAL.
>> 
>> However, if the MMIO timer is already enabled and has not yet
>> expired,
>> there is a possibility of unexpected behavior occurring: when the CPU
>> enters the idle state during this period, and if the CPU's local
>> event
>> is earlier than the broadcast event, the following process occurs:
>> 
>> tick_broadcast_enter()
>>   tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
>>     __tick_broadcast_oneshot_control()
>>       ___tick_broadcast_oneshot_control()
>>         tick_broadcast_set_event()
>>           clockevents_program_event()
>>             set_next_event_mem()
>> 
>> During this process, the MMIO timer remains enabled while programming
>> CVAL. To prevent such behavior, disable timer explicitly prior to
>> programming CVAL.
>> 
>> Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO
>> timer programming over to CVAL")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Walter Chang <walter.chang@mediatek.com>
>> ---
>>  drivers/clocksource/arm_arch_timer.c | 7 +++++++
>>  1 file changed, 7 insertions(+)
>> 
>> diff --git a/drivers/clocksource/arm_arch_timer.c
>> b/drivers/clocksource/arm_arch_timer.c
>> index e733a2a1927a..7dd2c615bce2 100644
>> --- a/drivers/clocksource/arm_arch_timer.c
>> +++ b/drivers/clocksource/arm_arch_timer.c
>> @@ -792,6 +792,13 @@ static __always_inline void
>> set_next_event_mem(const int access, unsigned long e
>>  u64 cnt;
>>  
>>  ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
>> +
>> +/* Timer must be disabled before programming CVAL */
>> +if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
>> +ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
>> +arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl,
>> clk);
>> +}
>> +
>>  ctrl |= ARCH_TIMER_CTRL_ENABLE;
>>  ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
>>  
> 
> Gentle ping for this patch.

Applied, thanks

-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog


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

* [tip: timers/core] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL
  2023-07-17  9:07 [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL walter.chang
                   ` (2 preceding siblings ...)
  2023-08-18  9:29 ` Walter Chang (張維哲)
@ 2023-08-31  1:31 ` tip-bot2 for Walter Chang
  3 siblings, 0 replies; 6+ messages in thread
From: tip-bot2 for Walter Chang @ 2023-08-31  1:31 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: stable, Walter Chang, Marc Zyngier, AngeloGioacchino Del Regno,
	Daniel Lezcano, x86, linux-kernel

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

Commit-ID:     e7d65e40ab5a5940785c5922f317602d0268caaf
Gitweb:        https://git.kernel.org/tip/e7d65e40ab5a5940785c5922f317602d0268caaf
Author:        Walter Chang <walter.chang@mediatek.com>
AuthorDate:    Mon, 17 Jul 2023 17:07:34 +08:00
Committer:     Daniel Lezcano <daniel.lezcano@linaro.org>
CommitterDate: Fri, 18 Aug 2023 12:06:16 +02:00

clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL

Due to the fact that the use of `writeq_relaxed()` to program CVAL is
not guaranteed to be atomic, it is necessary to disable the timer before
programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired,
there is a possibility of unexpected behavior occurring: when the CPU
enters the idle state during this period, and if the CPU's local event
is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
  tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
    __tick_broadcast_oneshot_control()
      ___tick_broadcast_oneshot_control()
        tick_broadcast_set_event()
          clockevents_program_event()
            set_next_event_mem()

During this process, the MMIO timer remains enabled while programming
CVAL. To prevent such behavior, disable timer explicitly prior to
programming CVAL.

Fixes: 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
Cc: stable@vger.kernel.org
Signed-off-by: Walter Chang <walter.chang@mediatek.com>
Acked-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Link: https://lore.kernel.org/r/20230717090735.19370-1-walter.chang@mediatek.com
---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c
index e09d442..f6c5f89 100644
--- a/drivers/clocksource/arm_arch_timer.c
+++ b/drivers/clocksource/arm_arch_timer.c
@@ -774,6 +774,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
 	u64 cnt;
 
 	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+	}
+
 	ctrl |= ARCH_TIMER_CTRL_ENABLE;
 	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
 

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

end of thread, other threads:[~2023-08-31  1:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-17  9:07 [PATCH] clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL walter.chang
2023-07-17  9:19 ` Marc Zyngier
2023-07-17  9:22 ` AngeloGioacchino Del Regno
2023-08-18  9:29 ` Walter Chang (張維哲)
2023-08-18 10:06   ` Daniel Lezcano
2023-08-31  1:31 ` [tip: timers/core] " tip-bot2 for Walter Chang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox