Skip to contentSkip to conversation

lib: sbi: Fix Smrnmi/RNMI trap handling issues

PATCH In review Nia Su 2 participants 7 messages Aug 28 – Sep 3

lore
Share & tools
lore

Nia Su

author
#0 lore raw

cover lib: sbi: Fix Smrnmi/RNMI trap handling issues

This series fixes two issues in the Smrnmi extension and RNMI trap handling code:

Patch 1 moves the Smrnmi CSR setup into a dedicated sbi_smrnmi_hart_init() helper so it also runs on the non-retentive suspend resume path (sbi_hart_reinit()), and relaxes smrnmi_handlers_init to allow a NULL callback and to report failure via an int return value instead of void.

Patch 2 fixes sbi_trap_rnmi_handler(), which never updates prev_context and can inherit a stale value from an earlier trap that reused the same M-mode exception-stack slot. This can make sbi_trap_error() loop indefinitely instead of printing diagnostics. The fix maintains the trap-context pointer the same way sbi_trap_handler() does, so nested contexts always link correctly.

---
Nia Su (1):
lib: sbi: Fix stale prev_context in RNMI handler

Nylon Chen (1):
lib: sbi: Fix Smrnmi init and non-retentive suspend handling

 include/sbi/sbi_hart.h     |  1 +
 include/sbi/sbi_platform.h |  2 +-
 lib/sbi/sbi_hart.c         | 55 +++++++++++++++++++++++++++++-----------------
 lib/sbi/sbi_trap.c         | 10 +++++++--
 4 files changed, 45 insertions(+), 23 deletions(-)

---
base-commit: 4e79fd7de59f1b2899092c1a84ce68c8ebc68f93
change-id: 20260826-rnmi-trap-fixes-91347b53b0dd

--

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

PATCH 1/2 lib: sbi: Fix Smrnmi init and non-retentive suspend handling

From: Nylon Chen <[email protected]>

Two fixes for the Smrnmi extension infrastructure:

1. Extract Smrnmi CSR setup into sbi_smrnmi_hart_init() and call it from both hart_detect_features() (early, before trap-based probing) and sbi_hart_reinit() (covers non-retentive suspend resume via init_warm_resume()). This removes the need for save/restore of CSR_MNSCRATCH and MNSTATUS in sbi_hsm_data.

2. Change smrnmi_handlers_init callback return type from void to int. Allow NULL callback for platforms that do not need to program a vendor-specific NMI vector register. Only set MNSTATUS.NMIE after the callback succeeds.

Fixes 2d211fe6f9d5 ("lib: sbi: hart: Detect and enable Smrnmi before trap-based feature detection")
---
 include/sbi/sbi_hart.h     |  1 +
 include/sbi/sbi_platform.h |  2 +-
 lib/sbi/sbi_hart.c         | 55 +++++++++++++++++++++++++++++-----------------
 3 files changed, 37 insertions(+), 21 deletions(-)
include/sbi/sbi_hart.h +1 -0
index 543393bba41526017cd95596348e26eaa897850a..03f12717572f402d77e4e78f8d29c93a5fa0343d 100644
@@ -132,6 +132,7 @@ extern unsigned long hart_features_offset;
 
 struct sbi_scratch;
 
+int sbi_smrnmi_hart_init(struct sbi_scratch *scratch);
 int sbi_hart_reinit(struct sbi_scratch *scratch);
 int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot);
 
include/sbi/sbi_platform.h +1 -1
index 1e9a23c1252558879c683633ad000828fbfa8f3f..d55805defafb0a7935d0d95653aefcc622078f69 100644
@@ -155,7 +155,7 @@ struct sbi_platform_operations {
 	void (*pmp_disable)(unsigned int n);
 
 	/** platform specific Smrnmi handlers init on current HART */
-	void (*smrnmi_handlers_init)(void (*rnmi_handler)(void),
+	int (*smrnmi_handlers_init)(void (*rnmi_handler)(void),
 			void (*rnme_handler)(void));
 
 	/** platform specific Smrnmi NMI handler.
lib/sbi/sbi_hart.c +35 -20

73 lines not on this page · raw message

Signature · 7 lines
2.43.7


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

PATCH 2/2 lib: sbi: Fix stale prev_context in RNMI handler

In reply to Nia Su

Before an RNMI is taken, an earlier SBI trap may have already used the same slot on the M-mode exception stack to save its trap context. When the RNMI later comes in, it reuses that same stack slot for its own trap context, but sbi_trap_rnmi_handler() never links prev_context to the previously active trap context, so it still holds the value left there by that earlier trap context.

sbi_trap_error() then walks this chain with
for (tc = tcntx; tc; tc = tc->prev_context) depth++; to count the depth before printing anything. If this stale prev_context happens to point back to tcntx itself, the loop never terminates, hanging the hart before any diagnostics are even printed.

Update sbi_trap_rnmi_handler() so that sbi_scratch points to the new trap context on entry, and points back to the previous context on exit, the same way sbi_trap_handler() does. This serves two
purposes: it makes tcntx->prev_context always point to the correct previous trap context (or NULL when there isn't one) instead of stale data, fixing the hang described above; and it lets an exception taken while already inside RNMI handling correctly chain back to the RNMI's trap context, instead of the RNMI context being silently dropped from the chain and never printed. Together, these let sbi_trap_error() print every nested trap context in the chain, including the RNMI's own state.

Fixes 8cdb5b1023df ("firmware: Add RNMI handler infrastructure")
---
 lib/sbi/sbi_trap.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/sbi/sbi_trap.c +8 -2
index 16774fd2f73059c1268bfb9f43fdf8e9c460c0b6..e694188c25b7b1715df660206af0a501290389fc 100644
@@ -394,9 +394,14 @@ trap_done:
 struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx)
 {
 	int rc;
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
 	const struct sbi_platform_operations *ops = sbi_platform_ops(plat);
 
+	/* Update trap context pointer so nested traps chain correctly */
+	tcntx->prev_context = sbi_trap_get_context(scratch);
+	sbi_trap_set_context(scratch, tcntx);
+
 	/* Call platform-specific NMI handler if registered */
 	if (ops && ops->rnmi_handler) {
 		rc = ops->rnmi_handler(tcntx);
@@ -404,14 +409,15 @@ struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx)
 			/* Platform handler failed to handle NMI */
 			sbi_trap_error("platform NMI handler failed", rc, tcntx);
 		}
-		return tcntx;
+		goto done;
 	}
 
 	/* No platform handler - treat as unhandled NMI */
 	sbi_trap_error("unhandled NMI (no platform rnmi_handler)",
 		       SBI_ENOTSUPP, tcntx);
 
-	/* Never returns */
+done:
+	sbi_trap_set_context(scratch, tcntx->prev_context);
 	return tcntx;
 }
 
Signature · 7 lines
2.43.7


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw
In reply to Nia Su

I tried this patch set on artificial setup on Whisper SW system simulator and it seems to solve issues discussed in [1].

But I'd suggest to split this series to separate changes structurally and make backporting easier if needed:

1. Restore the Smrnmi context on non-retentive resume: extract sbi_smrnmi_hart_init() and call it from sbi_hart_reinit(). This is the actual fix and it is self contained, so anyone on a v1.9 based tree can pick just this one.

2. The sbi_trap.c fix for the clobbered trap context (your patch 2/2). It touches a different file and has its own Fixes tag, so it does not depend on the rest.

3. Change smrnmi_handlers_init from void to int, but keep the panic when the platform does not provide the callback. Today a callback which fails has no way to report it and NMIE is set anyway, so this is a fix on its own.

4. Allow the NULL callback. This one changes what a platform is allowed to skip, so I would prefer to discuss it separately: as was mentioned earlier in the previous email thread [2], it would be nice to have a level of control of a situation when platform forgot to initialize the callback rather than just silently set NMIE and proceed. I can see these distinct options to handle this:

a) Boot time output: based on a flag just print whether Smrnmi vectors are initialized by platform explicitly or not

Smrnmi Vectors : NULL <-- or "Platform"

b) Warning print when Smrnmi is present and no callback is registered

c) Explicit opt out: platform which doesn't need to program vectors explicitly sets a corresponding flag in sbi_platform to indicate this. Without this flag set and when handlers are missing we panic like now, and when the flag is set we go ahead and proceed to setting the NMIE bit.

I personally would go with a) and c) as this allows full control of what's going on and catches errors early. What do you think?

[1] https://lore.kernel.org/opensbi/CAHh=Yk_Sbd1MEH8tVshAOFQptEVKm0QZv+D7bJwMC80qvarVew@mail.gmail.com/
[2] https://lore.kernel.org/opensbi/[email protected]/

Thanks,
Evgeny

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#4 lore raw
Quoted 7 lines · Evgeny Voevodin

I tried this patch set on artificial setup on Whisper SW system simulator and it seems to solve issues discussed in [1].

But I'd suggest to split this series to separate changes structurally and make backporting easier if needed:

Quoted 8 lines

1. Restore the Smrnmi context on non-retentive resume: extract sbi_smrnmi_hart_init() and call it from sbi_hart_reinit(). This is the actual fix and it is self contained, so anyone on a v1.9 based tree can pick just this one.

2. The sbi_trap.c fix for the clobbered trap context (your patch 2/2). It touches a different file and has its own Fixes tag, so it does not depend on the rest.

Good suggestion. I’ll submit this change as a separate patch.

Quoted 24 lines

3. Change smrnmi_handlers_init from void to int, but keep the panic when the platform does not provide the callback. Today a callback which fails has no way to report it and NMIE is set anyway, so this is a fix on its own.

4. Allow the NULL callback. This one changes what a platform is allowed to skip, so I would prefer to discuss it separately: as was mentioned earlier in the previous email thread [2], it would be nice to have a level of control of a situation when platform forgot to initialize the callback rather than just silently set NMIE and proceed. I can see these distinct options to handle this:

a) Boot time output: based on a flag just print whether Smrnmi vectors are initialized by platform explicitly or not

Smrnmi Vectors : NULL <-- or "Platform"

b) Warning print when Smrnmi is present and no callback is registered

c) Explicit opt out: platform which doesn't need to program vectors explicitly sets a corresponding flag in sbi_platform to indicate this. Without this flag set and when handlers are missing we panic like now, and when the flag is set we go ahead and proceed to setting the NMIE bit.

Quoted 8 lines

I personally would go with a) and c) as this allows full control of what's going on and catches errors early. What do you think?

[1] https://lore.kernel.org/opensbi/CAHh=Yk_Sbd1MEH8tVshAOFQptEVKm0QZv+D7bJwMC80qvarVew@mail.gmail.com/
[2] https://lore.kernel.org/opensbi/[email protected]/

Thanks,
Evgeny

I agree with your proposal to adopt options a) and c), and I will follow the implementation suggested in [2]

Thanks,
Nia

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

Just to be clear about the split - my suggestion was to keep the current void return type of ops->smrnmi_handlers_init in patch 1 and change it to int in a separate patch, so that a platform can indicate that Smrnmi handlers initialization went wrong. This will make backporting to v1.9 easier if needed. Is this aligned with what you were going to proceed with?

Thanks,
Evgeny

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#6 lore raw
Quoted 9 lines · Evgeny Voevodin

Just to be clear about the split - my suggestion was to keep the current void return type of ops->smrnmi_handlers_init in patch 1 and change it to int in a separate patch, so that a platform can indicate that Smrnmi handlers initialization went wrong. This will make backporting to v1.9 easier if needed. Is this aligned with what you were going to proceed with?

Thanks,
Evgeny

Yes, I fully understand your suggestion, and this is also how I plan to proceed. I will split the series into separate patches:

1. Reinitialize the RNMI after non-retentive suspend/resume.
2. Change smrnmi_handlers_init from void to int.
3. Allow a NULL callback and report the RNMI vector mode at boot.

This matches your proposal and should make the patches easier to backport. Thanks for the clarification.

Thanks,
Nia

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