Skip to contentSkip to conversation

arch: patch_text: Fixup last cpu should be master

PATCH In review [email protected] 2 participants 6 messages Apr 7, 2022 – Apr 8, 2022

Subsystems (7)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v4 cover arch: patch_text: Fixup last cpu should be master

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

Changes in v4:
- Add "Fixes:" for stable fixup
- Add cover letter for the patch series

Changes in v3:
- Separate the patch for different arch.

Changes in v2:
- Fixup num_online_cpus() - 1 is not last cpu

Guo Ren (4):
arm64: patch_text: Fixup last cpu should be master
riscv: patch_text: Fixup last cpu should be master
xtensa: patch_text: Fixup last cpu should be master
csky: patch_text: Fixup last cpu should be master

 arch/arm64/kernel/patching.c      | 4 ++--
 arch/csky/kernel/probes/kprobes.c | 2 +-
 arch/riscv/kernel/patch.c         | 2 +-
 arch/xtensa/kernel/jump_label.c   | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v4 1/4 arm64: patch_text: Fixup last cpu should be master

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

Fixes ae16480785de ("arm64: introduce interfaces to hotpatch kernel and module code")
---
 arch/arm64/kernel/patching.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/patching.c +2 -2
index 771f543464e0..33e0fabc0b79 100644
@@ -117,8 +117,8 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg)
 	int i, ret = 0;
 	struct aarch64_insn_patch *pp = arg;
 
-	/* The first CPU becomes master */
-	if (atomic_inc_return(&pp->cpu_count) == 1) {
+	/* The last CPU becomes master */
+	if (atomic_inc_return(&pp->cpu_count) == num_online_cpus()) {
 		for (i = 0; ret == 0 && i < pp->insn_cnt; i++)
 			ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i],
 							     pp->new_insns[i]);
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v4 2/4 riscv: patch_text: Fixup last cpu should be master

In reply to [email protected]

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

Fixes 043cb41a85de ("riscv: introduce interfaces to patch kernel code")
---
 arch/riscv/kernel/patch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/patch.c +1 -1
index 0b552873a577..765004b60513 100644
@@ -104,7 +104,7 @@ static int patch_text_cb(void *data)
 	struct patch_insn *patch = data;
 	int ret = 0;
 
-	if (atomic_inc_return(&patch->cpu_count) == 1) {
+	if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
 		ret =
 		    patch_text_nosync(patch->addr, &patch->insn,
 					    GET_INSN_LENGTH(patch->insn));
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH v4 3/4 xtensa: patch_text: Fixup last cpu should be master

In reply to [email protected]

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

Fixes 64711f9a47d4 ("xtensa: implement jump_label support")
---
 arch/xtensa/kernel/jump_label.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/xtensa/kernel/jump_label.c +1 -1
index 0dde21e0d3de..ad1841cecdfb 100644
@@ -40,7 +40,7 @@ static int patch_text_stop_machine(void *data)
 {
 	struct patch *patch = data;
 
-	if (atomic_inc_return(&patch->cpu_count) == 1) {
+	if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
 		local_patch_text(patch->addr, patch->data, patch->sz);
 		atomic_inc(&patch->cpu_count);
 	} else {
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH v4 4/4 csky: patch_text: Fixup last cpu should be master

In reply to [email protected]

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

Fixes 33e53ae1ce41 ("csky: Add kprobes supported")
---
 arch/csky/kernel/probes/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/csky/kernel/probes/kprobes.c +1 -1
index 42920f25e73c..34ba684d5962 100644
@@ -30,7 +30,7 @@ static int __kprobes patch_text_cb(void *priv)
 	struct csky_insn_patch *param = priv;
 	unsigned int addr = (unsigned int)param->addr;
 
-	if (atomic_inc_return(&param->cpu_count) == 1) {
+	if (atomic_inc_return(&param->cpu_count) == num_online_cpus()) {
 		*(u16 *) addr = cpu_to_le16(param->opcode);
 		dcache_wb_range(addr, addr + 2);
 		atomic_inc(&param->cpu_count);
Signature · 1 line
2.25.1
maintainer
#5 lore raw
In reply to [email protected]
Quoted 10 lines · On Thu, 7 Apr 2022 15:33:19 +0800, [email protected] wrote:

From: Guo Ren <[email protected]>

These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The original idea: When the master CPU patch_text, the others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee the remaining CPUs are waiting. This patch changes the last CPU as the master to solve the potential risk.

[...]

Applied first patch only to arm64 (for-next/fixes), thanks!

[1/4] arm64: patch_text: Fixup last cpu should be master

      https://git.kernel.org/arm64/c/31a099dbd91e

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev