qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Leonid Komarianskyi <Leonid_Komarianskyi@epam.com>
Cc: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"qemu-arm@nongnu.org" <qemu-arm@nongnu.org>,
	 Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>,
	Dmytro Firsov <Dmytro_Firsov@epam.com>
Subject: Re: [PATCH] target/arm: Add overflow check for gt_recalc_timer
Date: Thu, 13 Apr 2023 14:24:29 +0100	[thread overview]
Message-ID: <CAFEAcA_koyJSR_7kF6TByA_rm2nJQr-8UnXrtu3JqwgpqUEctw@mail.gmail.com> (raw)
In-Reply-To: <20230406151441.1027662-1-leonid_komarianskyi@epam.com>

On Thu, 6 Apr 2023 at 16:16, Leonid Komarianskyi
<Leonid_Komarianskyi@epam.com> wrote:
>
> If gt_timer is enabled before cval initialization on a virtualized
> setup on QEMU, cval equals (UINT64_MAX - 1). Adding an offset value
> to this causes an overflow that sets timer into the past, which leads
> to infinite loop, because this timer fires immediately and calls
> gt_recalc_timer() once more, which in turn sets the timer into the
> past again and as a result, QEMU hangs. This patch adds check for
> overflowing of the nexttick variable.

This is https://gitlab.com/qemu-project/qemu/-/issues/60 --
thanks for sending a patch.

> Suggested-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
> Co-Authored-By: Dmytro Firsov <dmytro_firsov@epam.com>
> Signed-off-by: Leonid Komarianskyi <leonid_komarianskyi@epam.com>
> ---
>  target/arm/helper.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/target/arm/helper.c b/target/arm/helper.c
> index 2297626bfb..2fbba15040 100644
> --- a/target/arm/helper.c
> +++ b/target/arm/helper.c
> @@ -2618,6 +2618,7 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
>          int istatus = count - offset >= gt->cval;
>          uint64_t nexttick;
>          int irqstate;
> +        bool nexttick_overflow = false;
>
>          gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
>
> @@ -2630,6 +2631,16 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
>          } else {
>              /* Next transition is when we hit cval */
>              nexttick = gt->cval + offset;
> +            if (nexttick < offset) {
> +                /*
> +                 * If gt->cval value is close to UINT64_MAX then adding
> +                 * to it offset can lead to overflow of nexttick variable.
> +                 * So, this check tests that arguments sum is less than any
> +                 * addend, and in case it is overflowed we have to mod timer
> +                 * to INT64_MAX.
> +                 */
> +                nexttick_overflow = true;
> +            }

Rather than adding in a bool, I think I prefer the version
of the patch in one of the comments to the bug report:

             /* Next transition is when we hit cval */
             nexttick = gt->cval + offset;
+            if (nexttick < gt->cval) {
+                nexttick = UINT64_MAX;
+            }

i.e. we just saturate nexttick, and then let the existing handling
of "turns out nexttick is too big" handle things.

There is also a comment or two from me in the bug report pointing
out that the handling of wraparound is also wrong in the other
half of this if(); we should look at that too.

>          }
>          /*
>           * Note that the desired next expiry time might be beyond the
> @@ -2637,7 +2648,8 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
>           * set the timer for as far in the future as possible. When the
>           * timer expires we will reset the timer for any remaining period.
>           */
> -        if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
> +        if ((nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu))
> +             || nexttick_overflow) {
>              timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
>          } else {
>              timer_mod(cpu->gt_timer[timeridx], nexttick);
> --
> 2.25.1

thanks
-- PMM


  reply	other threads:[~2023-04-13 13:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06 15:16 [PATCH] target/arm: Add overflow check for gt_recalc_timer Leonid Komarianskyi
2023-04-13 13:24 ` Peter Maydell [this message]
2023-11-09 13:55   ` [PATCH v2] " Leonid Komarianskyi
2023-11-09 15:13     ` Leonid Komarianskyi
2023-11-20 17:40       ` Peter Maydell

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=CAFEAcA_koyJSR_7kF6TByA_rm2nJQr-8UnXrtu3JqwgpqUEctw@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=Dmytro_Firsov@epam.com \
    --cc=Leonid_Komarianskyi@epam.com \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).