Skip to contentSkip to conversation

lib: sbi_ipi: Validate hartids against domain, not HSM state

PATCH Applied Chen Pei 2 participants 3 messages Jun 12 – Jul 21

lore
Share & tools
lore

Chen Pei

author
#0 lore raw

PATCH 1/1 lib: sbi_ipi: Validate hartids against domain, not HSM state

Commit 37eaca4ab33d ("lib: sbi_ipi: Return error for invalid hartids") added a weight check against the HSM-interruptible mask, which excludes harts in STOPPED state. This causes sbi_ipi_send_many() to return SBI_EINVAL when the kernel sends a remote fence (e.g. TLB shootdown) whose hmask includes a hart that was stopped by CPU hotplug:

__sbi_rfence_v02_call: hbase = [0] hmask = [0x3] failed (error [-22])

Validate requested hartids against the domain's assigned hartmask instead. This still rejects truly invalid or cross-domain hartids, but allows HSM-STOPPED harts to be silently skipped — which is safe because a stopped hart will do a full local TLB flush on its next HSM start before re-entering S-mode.

Fixes 37eaca4ab33d ("lib: sbi_ipi: Return error for invalid hartids")
---
 lib/sbi/sbi_ipi.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/sbi/sbi_ipi.c +9 -2
index 5b2d2f72..b04a5877 100644
@@ -136,6 +136,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 
 	if (hbase != -1UL) {
 		struct sbi_hartmask tmp_mask = { 0 };
+		struct sbi_hartmask domain_mask;
 		int count = sbi_popcount(hmask);
 
 		for (i = hbase; hmask; i++, hmask >>= 1) {
@@ -143,10 +144,16 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 				sbi_hartmask_set_hartid(i, &tmp_mask);
 		}
 
-		sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
+		/* Validate hartids against domain assignment, not HSM state */
+		rc = sbi_domain_get_assigned_hartmask(dom, &domain_mask);
+		if (rc)
+			return rc;
 
-		if (sbi_hartmask_weight(&target_mask) != count)
+		sbi_hartmask_and(&domain_mask, &domain_mask, &tmp_mask);
+		if (sbi_hartmask_weight(&domain_mask) != count)
 			return SBI_EINVAL;
+
+		sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
 	}
 
 	/* Send IPIs */
Signature · 7 lines
2.50.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#1 lore raw

Hello everyone,

Friendly ping on this patch. Is there any comment on this fix? Thanks.

To summarize, commit 37eaca4ab33d ("lib: sbi_ipi: Return error for invalid hartids") added a weight check against the HSM-interruptible mask. This causes sbi_ipi_send_many() to return SBI_EINVAL when the kernel sends a remote fence whose hmask includes a hart that was stopped by CPU hotplug, because STOPPED harts are excluded from the HSM-interruptible mask:

__sbi_rfence_v02_call: hbase = [0] hmask = [0x3] failed (error [-22])

This patch changes the validation to use the domain's assigned hartmask instead, which still rejects truly invalid or cross-domain hartids while allowing HSM-STOPPED harts to be silently skipped.

Thanks,
Pei

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#2 lore raw
In reply to Chen Pei
Quoted 9 lines · Chen Pei

Commit 37eaca4ab33d ("lib: sbi_ipi: Return error for invalid hartids") added a weight check against the HSM-interruptible mask, which excludes harts in STOPPED state. This causes sbi_ipi_send_many() to return SBI_EINVAL when the kernel sends a remote fence (e.g. TLB shootdown) whose hmask includes a hart that was stopped by CPU hotplug:

__sbi_rfence_v02_call: hbase = [0] hmask = [0x3] failed (error [-22])

Quoted 8 lines

Validate requested hartids against the domain's assigned hartmask instead. This still rejects truly invalid or cross-domain hartids, but allows HSM-STOPPED harts to be silently skipped — which is safe because a stopped hart will do a full local TLB flush on its next HSM start before re-entering S-mode.

Fixes 37eaca4ab33d ("lib: sbi_ipi: Return error for invalid hartids")

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 43 lines
---
 lib/sbi/sbi_ipi.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/sbi/sbi_ipi.c +9 -3
index 5b2d2f72..b04a5877 100644
@@ -136,6 +136,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 
        if (hbase != -1UL) {
                struct sbi_hartmask tmp_mask = { 0 };
+               struct sbi_hartmask domain_mask;
                int count = sbi_popcount(hmask);
 
                for (i = hbase; hmask; i++, hmask >>= 1) {
@@ -143,10 +144,16 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
                                sbi_hartmask_set_hartid(i, &tmp_mask);
                }
 
-               sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
+               /* Validate hartids against domain assignment, not HSM state */
+               rc = sbi_domain_get_assigned_hartmask(dom, &domain_mask);
+               if (rc)
+                       return rc;
 
-               if (sbi_hartmask_weight(&target_mask) != count)
+               sbi_hartmask_and(&domain_mask, &domain_mask, &tmp_mask);
+               if (sbi_hartmask_weight(&domain_mask) != count)
                        return SBI_EINVAL;
+
+               sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
        }
 
        /* Send IPIs */
--

2.50.1

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi