All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <andrew.jones@linux.dev>
To: Qingwei Hu <qingwei.hu@bytedance.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 James Raphael Tiovalen <jamestiotio@gmail.com>
Subject: Re: [kvm-unit-tests PATCH] riscv: sbi: Fix timer delay measurement race
Date: Fri, 23 Oct 2026 12:13:23 +0200	[thread overview]
Message-ID: <atsy0DXLV-WNdi-c@orel> (raw)
In-Reply-To: <20261023030442.3709880-1-qingwei.hu@bytedance.com>

On Fri, Oct 23, 2026 at 11:04:42AM +0800, Qingwei Hu wrote:
> The timer test samples the counter before checking timer_works in the
> wait loop. If the timer interrupt arrives between these operations, the
> handler sets timer_works and the loop exits with a timestamp taken before
> the interrupt. That timestamp can precede the deadline, causing a false
> "timer delay honored" failure in both the normal and masked IRQ tests.
> 
> Record the counter at the start of timer_irq_handler() and use it to
> calculate the elapsed time. Keep the loop timestamp for timeout detection.
> 
> Fixes: 14fa12b1351c ("riscv: sbi: Add test for timer extension")
> Signed-off-by: Qingwei Hu <qingwei.hu@bytedance.com>
> ---
>  riscv/sbi.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/riscv/sbi.c b/riscv/sbi.c
> index 3b8aadce..a298228b 100644
> --- a/riscv/sbi.c
> +++ b/riscv/sbi.c
> @@ -245,6 +245,7 @@ struct timer_info {
>  	bool timer_irq_set;
>  	bool timer_irq_cleared;
>  	unsigned long timer_irq_count;
> +	unsigned long irq_cycles;
>  };
>  
>  static struct timer_info timer_info;
> @@ -256,6 +257,7 @@ static bool timer_irq_pending(void)
>  
>  static void timer_irq_handler(struct pt_regs *regs)
>  {
> +	timer_info.irq_cycles = timer_get_cycles();
>  	timer_info.timer_works = true;
>  
>  	if (timer_info.timer_irq_count < ULONG_MAX)
> @@ -304,7 +306,7 @@ static void timer_check_set_timer(bool mask_timer_irq)
>  	}
>  
>  	if (timer_info.timer_works) {
> -		duration = end - begin;
> +		duration = timer_info.irq_cycles - begin;
>  		report(duration >= d && duration <= (d + margin), "timer delay honored%s", mask_test_str);
>  	}
>  
> -- 
> 2.39.5
> 

Hi Qingwei,

Your patch is fine, but I think we could also just change the loop a bit

    while (!timer_info.timer_works && timer_get_cycles() <= begin + d + margin)
        cpu_relax();
    end = timer_get_cycles();

Thanks,
drew

WARNING: multiple messages have this Message-ID (diff)
From: Andrew Jones <andrew.jones@linux.dev>
To: Qingwei Hu <qingwei.hu@bytedance.com>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 James Raphael Tiovalen <jamestiotio@gmail.com>
Subject: Re: [kvm-unit-tests PATCH] riscv: sbi: Fix timer delay measurement race
Date: Fri, 23 Oct 2026 12:13:23 +0200	[thread overview]
Message-ID: <atsy0DXLV-WNdi-c@orel> (raw)
In-Reply-To: <20261023030442.3709880-1-qingwei.hu@bytedance.com>

On Fri, Oct 23, 2026 at 11:04:42AM +0800, Qingwei Hu wrote:
> The timer test samples the counter before checking timer_works in the
> wait loop. If the timer interrupt arrives between these operations, the
> handler sets timer_works and the loop exits with a timestamp taken before
> the interrupt. That timestamp can precede the deadline, causing a false
> "timer delay honored" failure in both the normal and masked IRQ tests.
> 
> Record the counter at the start of timer_irq_handler() and use it to
> calculate the elapsed time. Keep the loop timestamp for timeout detection.
> 
> Fixes: 14fa12b1351c ("riscv: sbi: Add test for timer extension")
> Signed-off-by: Qingwei Hu <qingwei.hu@bytedance.com>
> ---
>  riscv/sbi.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/riscv/sbi.c b/riscv/sbi.c
> index 3b8aadce..a298228b 100644
> --- a/riscv/sbi.c
> +++ b/riscv/sbi.c
> @@ -245,6 +245,7 @@ struct timer_info {
>  	bool timer_irq_set;
>  	bool timer_irq_cleared;
>  	unsigned long timer_irq_count;
> +	unsigned long irq_cycles;
>  };
>  
>  static struct timer_info timer_info;
> @@ -256,6 +257,7 @@ static bool timer_irq_pending(void)
>  
>  static void timer_irq_handler(struct pt_regs *regs)
>  {
> +	timer_info.irq_cycles = timer_get_cycles();
>  	timer_info.timer_works = true;
>  
>  	if (timer_info.timer_irq_count < ULONG_MAX)
> @@ -304,7 +306,7 @@ static void timer_check_set_timer(bool mask_timer_irq)
>  	}
>  
>  	if (timer_info.timer_works) {
> -		duration = end - begin;
> +		duration = timer_info.irq_cycles - begin;
>  		report(duration >= d && duration <= (d + margin), "timer delay honored%s", mask_test_str);
>  	}
>  
> -- 
> 2.39.5
> 

Hi Qingwei,

Your patch is fine, but I think we could also just change the loop a bit

    while (!timer_info.timer_works && timer_get_cycles() <= begin + d + margin)
        cpu_relax();
    end = timer_get_cycles();

Thanks,
drew

-- 
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

  reply	other threads:[~2026-10-23 10:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23  3:04 [kvm-unit-tests PATCH] riscv: sbi: Fix timer delay measurement race Qingwei Hu
2026-10-23  3:04 ` Qingwei Hu
2026-10-23 10:13 ` Andrew Jones [this message]
2026-10-23 10:13   ` Andrew Jones
2026-10-26  2:54   ` [External] " Qingwei Hu
2026-10-26  2:54     ` Qingwei Hu

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=atsy0DXLV-WNdi-c@orel \
    --to=andrew.jones@linux.dev \
    --cc=jamestiotio@gmail.com \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=qingwei.hu@bytedance.com \
    /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 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.