Skip to contentSkip to conversation

riscv: unaligned: stop using kthread for vector speed probe

PATCH In review Nam Cao 4 participants 8 messages Jun 17 – Jul 29

Subsystems (2)
lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover riscv: unaligned: stop using kthread for vector speed probe

Hi,

This series is follow-up to the discussion at:
https://lore.kernel.org/linux-riscv/20260612-vec_unaligned_drop_init-v1-1-df969210ae34@oss.tenstorrent.com/

It removes the kthread usage for unaligned vector access speed probe, avoiding a bug that the kthread may still be excuting a __init function that have already been freed.

It also allows removing some vdso synchronization, simplify the code.

This kthread has been bothering me for a while now, and the recent bug report pushed me to post this series.

Nam Cao (2):
riscv: unaligned: stop using kthread for
check_vector_unaligned_access()
Revert "riscv: hwprobe: Fix stale vDSO data for late-initialized keys
at boot"

 arch/riscv/include/asm/hwprobe.h           |  7 ---
 arch/riscv/include/asm/vdso/arch_data.h    |  6 --
 arch/riscv/kernel/sys_hwprobe.c            | 70 ++++------------------
 arch/riscv/kernel/unaligned_access_speed.c | 19 +-----
 arch/riscv/kernel/vdso/hwprobe.c           |  2 +-
 5 files changed, 15 insertions(+), 89 deletions(-)
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 1/2 riscv: unaligned: stop using kthread for check_vector_unaligned_access()

A kthread is used to run check_vector_unaligned_access() to optimize boot time, allowing the kernel to continue booting without waiting for the unaligned vector speed probe to finish.

However, this asynchronous approach introduces several complications. First, the kthread may not complete before a user reads vDSO data, resulting in incorrect values. This was previously addressed by commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"), which added complex synchronization between the kthread and vDSO reads.

Second, it was discovered that the kthread may not finish before vec_check_unaligned_access_speed_all_cpus() (marked with __init) is freed, triggering a page fault.

These issues raise the question of whether the kthread is worth the added complexity. A past boot time regression report was actually unrelated to synchronous probing; it was caused by the probe running serially. Since switching to a parallel probe, no further complaints have been made. Furthermore, the unaligned scalar access speed probe takes the same amount of time, runs synchronously, and has caused no issues.

Testing shows no noticeable boot time slowdown when running the vector probe synchronously (0.464474s with kthread vs. 0.457991s without).

Remove the kthread usage and run the probe synchronously. This simplifies the boot flow and allows for the revert of commit 5d15d2ad36b0 ("riscv:
hwprobe: Fix stale vDSO data for late-initialized keys at boot")

---
 arch/riscv/kernel/unaligned_access_speed.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
arch/riscv/kernel/unaligned_access_speed.c +2 -17
index bb57eb5d19df..6e35bca568de 100644
@@ -6,7 +6,6 @@
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
 #include <linux/jump_label.h>
-#include <linux/kthread.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/types.h>
@@ -288,18 +287,9 @@ static void check_vector_unaligned_access(struct work_struct *work __always_unus
 	__free_pages(page, MISALIGNED_BUFFER_ORDER);
 }
 
-/* Measure unaligned access speed on all CPUs present at boot in parallel. */
-static int __init vec_check_unaligned_access_speed_all_cpus(void *unused __always_unused)
-{
-	schedule_on_each_cpu(check_vector_unaligned_access);
-	riscv_hwprobe_complete_async_probe();
-
-	return 0;
-}
 #else /* CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS */
-static int __init vec_check_unaligned_access_speed_all_cpus(void *unused __always_unused)
+static void check_vector_unaligned_access(struct work_struct *work __always_unused)
 {
-	return 0;
 }
 #endif
 
@@ -387,12 +377,7 @@ static int __init check_unaligned_access_all_cpus(void)
 			per_cpu(vector_misaligned_access, cpu) = unaligned_vector_speed_param;
 	} else if (!check_vector_unaligned_access_emulated_all_cpus() &&
 		   IS_ENABLED(CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS)) {
-		riscv_hwprobe_register_async_probe();
-		if (IS_ERR(kthread_run(vec_check_unaligned_access_speed_all_cpus,
-				       NULL, "vec_check_unaligned_access_speed_all_cpus"))) {
-			pr_warn("Failed to create vec_unalign_check kthread\n");
-			riscv_hwprobe_complete_async_probe();
-		}
+		schedule_on_each_cpu(check_vector_unaligned_access);
 	}
 
 	/*
Signature · 1 line
2.47.3
author
#2 lore raw

Re: [PATCH 1/2] riscv: unaligned: stop using kthread for check_vector_unaligned_access()

Quoted 15 lines · Nam Cao

A kthread is used to run check_vector_unaligned_access() to optimize boot time, allowing the kernel to continue booting without waiting for the unaligned vector speed probe to finish.

However, this asynchronous approach introduces several complications. First, the kthread may not complete before a user reads vDSO data, resulting in incorrect values. This was previously addressed by commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"), which added complex synchronization between the kthread and vDSO reads.

Second, it was discovered that the kthread may not finish before vec_check_unaligned_access_speed_all_cpus() (marked with __init) is freed, triggering a page fault.

Quoted 6 lines

These issues raise the question of whether the kthread is worth the added complexity. A past boot time regression report was actually unrelated to synchronous probing; it was caused by the probe running serially. Since switching to a parallel probe, no further complaints have been made. Furthermore, the unaligned scalar access speed probe takes the same amount of time, runs synchronously, and has caused no issues.

Another point I forgot to include. We start the kthread to run asynchronously, but the probe is executed on all CPUs including the boot CPU. Therefore if the kthread is executed before boot is completed, asynchronous probe will actually slow down boot time due to the overhead with kthread. If the kthread is executed after boot is completed, we run into the two race conditions mentioned above.

Quoted 12 lines

Testing shows no noticeable boot time slowdown when running the vector probe synchronously (0.464474s with kthread vs. 0.457991s without).

Remove the kthread usage and run the probe synchronously. This simplifies the boot flow and allows for the revert of commit 5d15d2ad36b0 ("riscv:
hwprobe: Fix stale vDSO data for late-initialized keys at boot")

Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] riscv: unaligned: stop using kthread for check_vector_unaligned_access()

In reply to Nam Cao
Quoted 30 lines · Nam Cao

A kthread is used to run check_vector_unaligned_access() to optimize boot time, allowing the kernel to continue booting without waiting for the unaligned vector speed probe to finish.

However, this asynchronous approach introduces several complications. First, the kthread may not complete before a user reads vDSO data, resulting in incorrect values. This was previously addressed by commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"), which added complex synchronization between the kthread and vDSO reads.

Second, it was discovered that the kthread may not finish before vec_check_unaligned_access_speed_all_cpus() (marked with __init) is freed, triggering a page fault.

These issues raise the question of whether the kthread is worth the added complexity. A past boot time regression report was actually unrelated to synchronous probing; it was caused by the probe running serially. Since switching to a parallel probe, no further complaints have been made. Furthermore, the unaligned scalar access speed probe takes the same amount of time, runs synchronously, and has caused no issues.

Testing shows no noticeable boot time slowdown when running the vector probe synchronously (0.464474s with kthread vs. 0.457991s without).

Remove the kthread usage and run the probe synchronously. This simplifies the boot flow and allows for the revert of commit 5d15d2ad36b0 ("riscv:
hwprobe: Fix stale vDSO data for late-initialized keys at boot")

Quoted 5 lines
Quoted 59 lines
---
 arch/riscv/kernel/unaligned_access_speed.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
arch/riscv/kernel/unaligned_access_speed.c +2 -18
index bb57eb5d19df..6e35bca568de 100644
@@ -6,7 +6,6 @@
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
 #include <linux/jump_label.h>
-#include <linux/kthread.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/types.h>
@@ -288,18 +287,9 @@ static void check_vector_unaligned_access(struct work_struct *work __always_unus
        __free_pages(page, MISALIGNED_BUFFER_ORDER);
 }
 
-/* Measure unaligned access speed on all CPUs present at boot in parallel. */
-static int __init vec_check_unaligned_access_speed_all_cpus(void *unused __always_unused)
-{
-       schedule_on_each_cpu(check_vector_unaligned_access);
-       riscv_hwprobe_complete_async_probe();
-
-       return 0;
-}
 #else /* CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS */
-static int __init vec_check_unaligned_access_speed_all_cpus(void *unused __always_unused)
+static void check_vector_unaligned_access(struct work_struct *work __always_unused)
 {
-       return 0;
 }
 #endif
 
@@ -387,12 +377,7 @@ static int __init check_unaligned_access_all_cpus(void)
                        per_cpu(vector_misaligned_access, cpu) = unaligned_vector_speed_param;
        } else if (!check_vector_unaligned_access_emulated_all_cpus() &&
                   IS_ENABLED(CONFIG_RISCV_PROBE_VECTOR_UNALIGNED_ACCESS)) {
-               riscv_hwprobe_register_async_probe();
-               if (IS_ERR(kthread_run(vec_check_unaligned_access_speed_all_cpus,
-                                      NULL, "vec_check_unaligned_access_speed_all_cpus"))) {
-                       pr_warn("Failed to create vec_unalign_check kthread\n");
-                       riscv_hwprobe_complete_async_probe();
-               }
+               schedule_on_each_cpu(check_vector_unaligned_access);
        }
 
        /*
--

2.47.3

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

maintainer
#4 lore raw

Re: [PATCH 1/2] riscv: unaligned: stop using kthread for check_vector_unaligned_access()

In reply to Nam Cao

Hi,

Quoted 29 lines · Nam Cao

A kthread is used to run check_vector_unaligned_access() to optimize boot time, allowing the kernel to continue booting without waiting for the unaligned vector speed probe to finish.

However, this asynchronous approach introduces several complications. First, the kthread may not complete before a user reads vDSO data, resulting in incorrect values. This was previously addressed by commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"), which added complex synchronization between the kthread and vDSO reads.

Second, it was discovered that the kthread may not finish before vec_check_unaligned_access_speed_all_cpus() (marked with __init) is freed, triggering a page fault.

These issues raise the question of whether the kthread is worth the added complexity. A past boot time regression report was actually unrelated to synchronous probing; it was caused by the probe running serially. Since switching to a parallel probe, no further complaints have been made. Furthermore, the unaligned scalar access speed probe takes the same amount of time, runs synchronously, and has caused no issues.

Testing shows no noticeable boot time slowdown when running the vector probe synchronously (0.464474s with kthread vs. 0.457991s without).

Remove the kthread usage and run the probe synchronously. This simplifies the boot flow and allows for the revert of commit 5d15d2ad36b0 ("riscv:
hwprobe: Fix stale vDSO data for late-initialized keys at boot")

Quoted 5 lines

Thanks, will queue this for v7.3, but I think the Fixes: tag should point to e7c9d66e313b ("RISC-V: Report vector unaligned access speed hwprobe"), since the problems that you mention are due to the use of kthreads. So I'll plan to update this accordingly when it's queued.

- Paul

author
#5 lore raw

PATCH 2/2 Revert "riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"

In reply to Nam Cao

This reverts commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"). The commit ensures synchronization between the unaligned vector access speed probe kthread and vDSO data read. But now that the kthread has been removed, this commit can be reverted.

---
 arch/riscv/include/asm/hwprobe.h        |  7 ---
 arch/riscv/include/asm/vdso/arch_data.h |  6 ---
 arch/riscv/kernel/sys_hwprobe.c         | 70 +++++--------------------
 arch/riscv/kernel/vdso/hwprobe.c        |  2 +-
 4 files changed, 13 insertions(+), 72 deletions(-)
arch/riscv/include/asm/hwprobe.h +0 -7
index 8b9f5e1cf4cb..9b04377c0f98 100644
@@ -43,11 +43,4 @@ static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,
 	return pair->value == other_pair->value;
 }
 
-#ifdef CONFIG_MMU
-void riscv_hwprobe_register_async_probe(void);
-void riscv_hwprobe_complete_async_probe(void);
-#else
-static inline void riscv_hwprobe_register_async_probe(void) {}
-static inline void riscv_hwprobe_complete_async_probe(void) {}
-#endif
 #endif
arch/riscv/include/asm/vdso/arch_data.h +0 -6
index 88b37af55175..da57a3786f7a 100644
@@ -12,12 +12,6 @@ struct vdso_arch_data {
 
 	/* Boolean indicating all CPUs have the same static hwprobe values. */
 	__u8 homogeneous_cpus;
-
-	/*
-	 * A gate to check and see if the hwprobe data is actually ready, as
-	 * probing is deferred to avoid boot slowdowns.
-	 */
-	__u8 ready;
 };
 
 #endif /* __RISCV_ASM_VDSO_ARCH_DATA_H */
arch/riscv/kernel/sys_hwprobe.c +12 -58

105 lines not on this page · raw message

arch/riscv/kernel/vdso/hwprobe.c +1 -1
index 8f45500d0a6e..2ddeba6c68dd 100644
@@ -27,7 +27,7 @@ static int riscv_vdso_get_values(struct riscv_hwprobe *pairs, size_t pair_count,
 	 * homogeneous, then this function can handle requests for arbitrary
 	 * masks.
 	 */
-	if (flags != 0 || (!all_cpus && !avd->homogeneous_cpus) || unlikely(!avd->ready))
+	if ((flags != 0) || (!all_cpus && !avd->homogeneous_cpus))
 		return riscv_hwprobe(pairs, pair_count, cpusetsize, cpus, flags);
 
 	/* This is something we can handle, fill out the pairs. */
Signature · 1 line
2.47.3
maintainer
#6 lore raw

Re: [PATCH 2/2] Revert "riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"

Quoted 6 lines · Nam Cao

This reverts commit 5d15d2ad36b0 ("riscv: hwprobe: Fix stale vDSO data for late-initialized keys at boot"). The commit ensures synchronization between the unaligned vector access speed probe kthread and vDSO data read. But now that the kthread has been removed, this commit can be reverted.

Thanks, will queue for v7.3.

- Paul

Tested-by on the v1 series
#7 lore raw
In reply to Nam Cao

Hi,

Quoted 21 lines · Nam Cao

Hi,

This series is follow-up to the discussion at:
https://lore.kernel.org/linux-riscv/20260612-vec_unaligned_drop_init-v1-1-df969210ae34@oss.tenstorrent.com/

It removes the kthread usage for unaligned vector access speed probe, avoiding a bug that the kthread may still be excuting a __init function that have already been freed.

It also allows removing some vdso synchronization, simplify the code.

This kthread has been bothering me for a while now, and the recent bug report pushed me to post this series.

Nam Cao (2):
riscv: unaligned: stop using kthread for
check_vector_unaligned_access()
Revert "riscv: hwprobe: Fix stale vDSO data for late-initialized keys
at boot"

Quoted 6 lines
 arch/riscv/include/asm/hwprobe.h           |  7 ---
 arch/riscv/include/asm/vdso/arch_data.h    |  6 --
 arch/riscv/kernel/sys_hwprobe.c            | 70 ++++------------------
 arch/riscv/kernel/unaligned_access_speed.c | 19 +-----
 arch/riscv/kernel/vdso/hwprobe.c           |  2 +-
 5 files changed, 15 insertions(+), 89 deletions(-)

Tested on Tenstorrent Blackhole with Sifive x280 cores. Slowing down the unaligned vector access speed probe doesn't cause boot to fail now.

Quoted 3 lines

--
2.47.3