Skip to contentSkip to conversation

clocksource: riscv: Patch riscv_clock_next_event() jump before first use

PATCH Applied Matt Evans 4 participants 4 messages Feb 1, 2023 – Feb 13, 2023

Subsystems (2)
lore
Share & tools
lore

Matt Evans

author
#0 lore raw

PATCH v2 1/1 clocksource: riscv: Patch riscv_clock_next_event() jump before first use

A static key is used to select between SBI and Sstc timer usage in riscv_clock_next_event(), but currently the direction is resolved after cpuhp_setup_state() is called (which sets the next event). The first event will therefore fall through the sbi_set_timer() path; this breaks Sstc-only systems. So, apply the jump patching before first use.

Fixes 9f7a8ff6391f ("RISC-V: Prefer sstc extension if available")

---

V1 -> V2: Commit msg tweak.

 drivers/clocksource/timer-riscv.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/clocksource/timer-riscv.c +5 -5
index 969a552da8d2..a36d173fd6cd 100644
@@ -177,6 +177,11 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		return error;
 	}
 
+	if (riscv_isa_extension_available(NULL, SSTC)) {
+		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
+		static_branch_enable(&riscv_sstc_available);
+	}
+
 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
 			 "clockevents/riscv/timer:starting",
 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
@@ -184,11 +189,6 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
 		       error);
 
-	if (riscv_isa_extension_available(NULL, SSTC)) {
-		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
-		static_branch_enable(&riscv_sstc_available);
-	}
-
 	return error;
 }
 
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Matt Evans

A static key is used to select between SBI and Sstc timer usage in riscv_clock_next_event(), but currently the direction is resolved after cpuhp_setup_state() is called (which sets the next event). The first event will therefore fall through the sbi_set_timer() path; this breaks Sstc-only systems. So, apply the jump patching before first use.

Fixes 9f7a8ff6391f ("RISC-V: Prefer sstc extension if available")

Looks good to me.

Regards,
Anup

Quoted 39 lines

---

V1 -> V2: Commit msg tweak.

 drivers/clocksource/timer-riscv.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/clocksource/timer-riscv.c +5 -6
index 969a552da8d2..a36d173fd6cd 100644
@@ -177,6 +177,11 @@ static int __init riscv_timer_init_dt(struct device_node *n)
                return error;
        }
 
+       if (riscv_isa_extension_available(NULL, SSTC)) {
+               pr_info("Timer interrupt in S-mode is available via sstc extension\n");
+               static_branch_enable(&riscv_sstc_available);
+       }
+
        error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
                         "clockevents/riscv/timer:starting",
                         riscv_timer_starting_cpu, riscv_timer_dying_cpu);
@@ -184,11 +189,6 @@ static int __init riscv_timer_init_dt(struct device_node *n)
                pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
                       error);
 
-       if (riscv_isa_extension_available(NULL, SSTC)) {
-               pr_info("Timer interrupt in S-mode is available via sstc extension\n");
-               static_branch_enable(&riscv_sstc_available);
-       }
-
        return error;
 }
 
--

2.30.2

maintainer Reviewed-by Acked-by on v2 1/1
#2 lore raw
In reply to Matt Evans
Quoted 46 lines · Matt Evans

A static key is used to select between SBI and Sstc timer usage in riscv_clock_next_event(), but currently the direction is resolved after cpuhp_setup_state() is called (which sets the next event). The first event will therefore fall through the sbi_set_timer() path; this breaks Sstc-only systems. So, apply the jump patching before first use.

Fixes 9f7a8ff6391f ("RISC-V: Prefer sstc extension if available")

---

V1 -> V2: Commit msg tweak.

 drivers/clocksource/timer-riscv.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/clocksource/timer-riscv.c +5 -5
index 969a552da8d2..a36d173fd6cd 100644
@@ -177,6 +177,11 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		return error;
 	}
 
+	if (riscv_isa_extension_available(NULL, SSTC)) {
+		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
+		static_branch_enable(&riscv_sstc_available);
+	}
+
 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
 			 "clockevents/riscv/timer:starting",
 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
@@ -184,11 +189,6 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
 		       error);
 
-	if (riscv_isa_extension_available(NULL, SSTC)) {
-		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
-		static_branch_enable(&riscv_sstc_available);
-	}
-
 	return error;
 }
 
Signature · 1 line
2.30.2

Looks like I've taken stuff here before, but I'm also happy to have the clocksource folks pick this up so

I don't think this is a particularly critical fix, as IIUC there's still no Sstc-only hardware in the real world. Given how late we are I'd err towards putting this on for-next anyway, so I'll give the clocksource folks a bit more time to chime in. Otherwise I'll put it on for-next before the merge window (assuming we're got an rc8 coming).

Thanks!

merge bot Applied
#3 lore raw

[tip: timers/core] clocksource/drivers/riscv: Patch riscv_clock_next_event() jump before first use

In reply to Matt Evans
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     225b9596cb0227c1c1b1e4a836dad43595c3e61a
Gitweb:        https://git.kernel.org/tip/225b9596cb0227c1c1b1e4a836dad43595c3e61a
Author:        Matt Evans <[email protected]>
AuthorDate:    Wed, 01 Feb 2023 19:49:42 
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Mon, 13 Feb 2023 13:10:17 +01:00

clocksource/drivers/riscv: Patch riscv_clock_next_event() jump before first use

A static key is used to select between SBI and Sstc timer usage in riscv_clock_next_event(), but currently the direction is resolved after cpuhp_setup_state() is called (which sets the next event). The first event will therefore fall through the sbi_set_timer() path; this breaks Sstc-only systems. So, apply the jump patching before first use.

---
 drivers/clocksource/timer-riscv.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/clocksource/timer-riscv.c +5 -5
index d8cb629..5f0f10c 100644
@@ -192,6 +192,11 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		return error;
 	}
 
+	if (riscv_isa_extension_available(NULL, SSTC)) {
+		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
+		static_branch_enable(&riscv_sstc_available);
+	}
+
 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
 			 "clockevents/riscv/timer:starting",
 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
@@ -199,11 +204,6 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
 		       error);
 
-	if (riscv_isa_extension_available(NULL, SSTC)) {
-		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
-		static_branch_enable(&riscv_sstc_available);
-	}
-
 	return error;
 }