lib: sbi: Fix shift bug in sbi_system_reset
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
There is a problem with judging whether the current hart belongs to hmask. If cur_hartid minus hbase is greater than BITS_PER_LONG, the previous hmask will also have a bit cleared incorrectly, which will cause some harts to lose ipi.
--- lib/sbi/sbi_system.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/sbi/sbi_system.c +2 -1
@@ -72,7 +72,8 @@ void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)/* Send HALT IPI to every hart other than the current hart */ while (!sbi_hsm_hart_interruptible_mask (dom, hbase, &hmask)) { -if (hbase <= cur_hartid) +if ((hbase <= cur_hartid) + && (cur_hartid < hbase +BITS_PER_LONG )) hmask &= ~(1UL << (cur_hartid - hbase));if (hmask)sbi_ipi_send_halt (hmask, hbase);
Signature · 1 line
2.43.0
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
Quoted 16 lines · On Thu, Dec 21, 2023 at 7:14?AM Xiang W <[email protected]> wrote:
There is a problem with judging whether the current hart belongs to hmask. If
cur_hartidminus hbase is greater thanBITS_PER_LONG, the previous hmask will also have a bit cleared incorrectly, which will cause some harts to lose ipi.Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_system.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/sbi/sbi_system.c +0
-0
lib/sbi/sbi_system.c void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason) · line 76
Quoted 6 lines
lib/sbi/sbi_system.c +2
-1@@ -72,7 +72,8 @@ void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)/* Send HALT IPI to every hart other than the current hart */ while (!sbi_hsm_hart_interruptible_mask (dom, hbase, &hmask)) { -if (hbase <= cur_hartid) +if ((hbase <= cur_hartid) + && (cur_hartid < hbase +BITS_PER_LONG ))
If "cur_hartid < hbase + BITS_PER_LONG" then
"1UL << (cur_hartid - hbase) == 0x0" so hmask
won't be impacted.
Do we still need this additional check ?
Quoted 11 lines
lib/sbi/sbi_system.c +0
-1hmask &= ~(1UL << (cur_hartid - hbase));if (hmask)sbi_ipi_send_halt (hmask, hbase); --2.43.0
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
? 2023-12-25???? 14:54 +0530?Anup Patel???
Quoted 28 lines · On Thu, Dec 21, 2023 at 7:14?AM Xiang W <[email protected]> wrote:
Quoted 17 lines
There is a problem with judging whether the current hart belongs to hmask. If
cur_hartidminus hbase is greater thanBITS_PER_LONG, the previous hmask will also have a bit cleared incorrectly, which will cause some harts to lose ipi.Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_system.c | 3 ++-
?1 file changed, 2 insertions(+), 1 deletion(-)lib/sbi/sbi_system.c +0
-0@@ -72,7 +72,8 @@ void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)Quoted 5 lines
??????? /* Send HALT IPI to every hart other than the current hart */ ??????? while (!sbi_hsm_hart_interruptible_mask(dom, hbase, &hmask)) { -?????????????? if (hbase <=
cur_hartid)
+?????????????? if ((hbase <=cur_hartid) +???????????????????????? && (cur_hartid< hbase +BITS_PER_LONG))If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0" so hmask
won't be impacted.
Quoted 1 line
Do we still need this additional check ?
Yes, needed.
1UL << (cur_hartid - hbase) is equal to 1UL << ((cur_hartid - hbase) % XLEN) is not equal to 0.
Regards,
Xiang W
Quoted 15 lines
Quoted 8 lines
??????????????????????? hmask &= ~(1UL << (
cur_hartid- hbase)); ??????????????? if (hmask)
???????????????????????sbi_ipi_send_halt(hmask, hbase); --
2.43.0Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
In reply to Anup PatelQuoted 2 lines · Anup Patel
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"
If cur_hartid - hbase < BITS_PER_LONG, then 1UL << (cur_hartid - hbase) will not be 0. If cur_hartid - hbase >= BITS_PER_LONG, then 1UL << (cur_hartid - hbase) will overflow and be undefined.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
? 2023-12-25???? 10:48 +0100?Andreas Schwab???
Quoted 9 lines · Anup Patel
Quoted 3 lines
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"If
cur_hartid- hbase <BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will not be 0.? Ifcur_hartid- hbase >=BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will overflow and be undefined.
Andreas is right. C99 semantics require that the result of a shift overflow is undefined. Under RISCV, 1<< n is equal to 1<<(n%XLAN).
Regards,
Xiang W
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
In reply to Andreas SchwabQuoted 9 lines · On Mon, Dec 25, 2023 at 3:19?PM Andreas Schwab <[email protected]> wrote:
Quoted 2 lines · Anup Patel
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"If
cur_hartid- hbase <BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will not be 0. Ifcur_hartid- hbase >=BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will overflow and be undefined.
I meant "cur_hartid >= hbase + BITS_PER_LONG" which is the overflow case.
Since the overflow behavior is undefined, we have the explicit check added by this patch.
Regards,
Anup
Quoted 10 lines
--
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
Quoted 11 lines · Anup Patel
Quoted 9 lines · On Mon, Dec 25, 2023 at 3:19?PM Andreas Schwab <[email protected]> wrote:
Quoted 2 lines · Anup Patel
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"If
cur_hartid- hbase <BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will not be 0. Ifcur_hartid- hbase >=BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will overflow and be undefined.
Quoted 5 lines
I meant "
cur_hartid>= hbase +BITS_PER_LONG" which is the overflow case.Since the overflow behavior is undefined, we have the explicit check added by this patch.
As pointed out by Andreas, "cur_hartid - hbase < BITS_PER_LONG" is the only check needed here. Unsigned wraparound will take care of the case where cur_hartid < hbase.
Regards,
Samuel
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
? 2023-12-26???? 12:03 -0600?Samuel Holland???
Quoted 15 lines · Anup Patel
Quoted 14 lines · On Mon, Dec 25, 2023 at 3:19?PM Andreas Schwab <[email protected]> wrote:
Quoted 9 lines · Anup Patel
Quoted 3 lines
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"If
cur_hartid- hbase <BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will not be 0.? Ifcur_hartid- hbase >=BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will overflow and be undefined.I meant "
cur_hartid>= hbase +BITS_PER_LONG" which is the overflow case.
Quoted 6 lines
Quoted 2 lines
Since the overflow behavior is undefined, we have the explicit check added by this patch.
As pointed out by Andreas, "
cur_hartid- hbase <BITS_PER_LONG" is the only check needed here. Unsigned wraparound will take care of the case wherecur_hartid< hbase.
cur_hartid < hbase is not always true.
For example, cur_hartid is 3 and 0-100 belongs to a domain. Here the loop is executed twice, the first time with hbase 0 and the second time with hbase 64. The second loop will have cur_hartid < hbase.
Regards,
Xiang W
Quoted 3 lines
Regards,
Samuel
PATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
Quoted 20 lines · On Wed, Dec 27, 2023 at 7:25?AM Xiang W <[email protected]> wrote:
? 2023-12-26???? 12:03 -0600?Samuel Holland???
Quoted 18 lines · Anup Patel
Quoted 16 lines · On Mon, Dec 25, 2023 at 3:19?PM Andreas Schwab <[email protected]> wrote:
Quoted 9 lines · Anup Patel
Quoted 3 lines
If "
cur_hartid< hbase +BITS_PER_LONG" then
"1UL << (cur_hartid- hbase) ==0x0"If
cur_hartid- hbase <BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will not be 0. Ifcur_hartid- hbase >=BITS_PER_LONG, then 1UL << (cur_hartid- hbase) will overflow and be undefined.I meant "
cur_hartid>= hbase +BITS_PER_LONG" which is the overflow case.Since the overflow behavior is undefined, we have the explicit check added by this patch.
Quoted 8 lines
Quoted 3 lines
As pointed out by Andreas, "
cur_hartid- hbase <BITS_PER_LONG" is the only check needed here. Unsigned wraparound will take care of the case wherecur_hartid< hbase.
cur_hartid< hbase is not always true.For example,
cur_hartidis 3 and 0-100 belongs to a domain. Here the loop is executed twice, the first time with hbase 0 and the second time with hbase 64. The second loop will havecur_hartid< hbase.
The real issue is that overflow behaviour of the left shift operator is undefined. No need for further justification on this issue.
Regards,
Anup
Quoted 12 lines
Regards,
Xiang WQuoted 3 lines
Regards,
Samuel--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Anup Patel
AppliedPATCH 1/1 lib: sbi: Fix shift bug in sbi_system_reset
In reply to Xiang WQuoted 7 lines · On Thu, Dec 21, 2023 at 7:14?AM Xiang W <[email protected]> wrote:
There is a problem with judging whether the current hart belongs to hmask. If
cur_hartidminus hbase is greater thanBITS_PER_LONG, the previous hmask will also have a bit cleared incorrectly, which will cause some harts to lose ipi.Signed-off-by Xiang W <[email protected]>
Looks good to me.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 26 lines
--- lib/sbi/sbi_system.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/sbi/sbi_system.c +2
-2@@ -72,7 +72,8 @@ void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)/* Send HALT IPI to every hart other than the current hart */ while (!sbi_hsm_hart_interruptible_mask (dom, hbase, &hmask)) { -if (hbase <= cur_hartid) +if ((hbase <= cur_hartid) + && (cur_hartid < hbase +BITS_PER_LONG )) hmask &= ~(1UL << (cur_hartid - hbase));if (hmask)sbi_ipi_send_halt (hmask, hbase); --2.43.0
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi