arch: patch_text: Fixup last cpu should be master
Subsystems (5)
[email protected]
maintainerRFC PATCH 1/1 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 origin idea is that when the master CPU patch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the last CPU as the master to solve the potaintial risk.
--- 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(-)
arch/arm64/kernel/patching.c +2 -2
@@ -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 () -1 )) {for (i =0 ; ret ==0 && i < pp->insn_cnt; i++) ret =aarch64_insn_patch_text_nosync (pp->text_addrs[i], pp->new_insns[i]);
arch/csky/kernel/probes/kprobes.c +1 -1
@@ -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 (¶m->cpu_count) ==1 ) { +if (atomic_inc_return (¶m->cpu_count) == (num_online_cpus () -1 )) { *(u16 *) addr =cpu_to_le16 (param->opcode);dcache_wb_range (addr, addr +2 );atomic_inc (¶m->cpu_count);
arch/riscv/kernel/patch.c +1 -1
@@ -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 () -1 )) { ret =patch_text_nosync (patch->addr, &patch->insn,GET_INSN_LENGTH (patch->insn));
arch/xtensa/kernel/jump_label.c +1 -1
@@ -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 () -1 )) {local_patch_text (patch->addr, patch->data, patch->sz);atomic_inc (&patch->cpu_count); }else {
Signature · 1 line
2.25.1
Hi Guo Ren,
Quoted 5 lines · On Sat, Mar 12, 2022 at 7:56 AM <[email protected]> wrote:
From: Guo Ren <[email protected]>
These
patch_textimplementations are usingstop_machine_cpuslockedinfrastructure with atomiccpu_count. The origin idea is that when
The original
Quoted 3 lines
the master CPU
patch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the last
guarantee that remaining CPUs are waiting.
Quoted 1 line
CPU as the master to solve the potaintial risk.
potential
Quoted 11 lines
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Peter Zijlstra <[email protected]Cc Masami Hiramatsu <[email protected]>Cc Chris Zankel <[email protected]>Cc Max Filippov <[email protected]>Cc Arnd Bergmann <[email protected]>
Quoted 6 lines
--- 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(-)
I'm curious, is there a specific issue that prompted this patch?
Signature · 2 lines
Thanks. -- Max
Max Filippov
maintainerQuoted 32 lines · On Sat, Mar 12, 2022 at 7:56 AM <[email protected]> wrote:
From: Guo Ren <[email protected]>
These
patch_textimplementations are usingstop_machine_cpuslockedinfrastructure with atomiccpu_count. The origin idea is that when the master CPUpatch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the last CPU as the master to solve the potaintial risk.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Peter Zijlstra <[email protected]Cc Masami Hiramatsu <[email protected]>Cc Chris Zankel <[email protected]>Cc Max Filippov <[email protected]>Cc Arnd Bergmann <[email protected]>--- 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(-)arch/arm64/kernel/patching.c +0
-0
arch/arm64/kernel/patching.c static int __kprobes aarch64_insn_patch_text_cb(void *arg) · line 121
Quoted 7 lines
arch/arm64/kernel/patching.c +2
-2@@ -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 () -1 )) {
atomic_inc_return returns the incremented value, so the last CPU gets num_online_cpus(), not (num_online_cpus() - 1).
arch/csky/kernel/probes/kprobes.c static int __kprobes patch_text_cb(void *priv) · line 33
Quoted 13 lines
arch/arm64/kernel/patching.c +0
-0for (i =0 ; ret ==0 && i < pp->insn_cnt; i++) ret =aarch64_insn_patch_text_nosync (pp->text_addrs[i], pp->new_insns[i]);arch/csky/kernel/probes/kprobes.c +1
-1@@ -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 (¶m->cpu_count) ==1 ) { +if (atomic_inc_return (¶m->cpu_count) == (num_online_cpus () -1 )) {
Ditto.
arch/riscv/kernel/patch.c static int patch_text_cb(void *data) · line 107
Quoted 13 lines
arch/csky/kernel/probes/kprobes.c +0
-0*(u16 *) addr =cpu_to_le16 (param->opcode);dcache_wb_range (addr, addr +2 );atomic_inc (¶m->cpu_count);arch/riscv/kernel/patch.c +1
-1@@ -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 () -1 )) {
Ditto.
arch/xtensa/kernel/jump_label.c static int patch_text_stop_machine(void *data) · line 43
Quoted 13 lines
arch/riscv/kernel/patch.c +0
-0ret =patch_text_nosync (patch->addr, &patch->insn,GET_INSN_LENGTH (patch->insn));arch/xtensa/kernel/jump_label.c +1
-1@@ -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 () -1 )) {
Ditto.
Quoted 6 lines
arch/xtensa/kernel/jump_label.c +0
-1local_patch_text (patch->addr, patch->data, patch->sz);atomic_inc (&patch->cpu_count); }else { --2.25.1
Signature · 2 lines
Thanks. -- Max
Guo Ren
maintainerQuoted 37 lines · Max Filippov
Quoted 35 lines · On Sat, Mar 12, 2022 at 7:56 AM <[email protected]> wrote:
From: Guo Ren <[email protected]>
These
patch_textimplementations are usingstop_machine_cpuslockedinfrastructure with atomiccpu_count. The origin idea is that when the master CPUpatch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the last CPU as the master to solve the potaintial risk.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Peter Zijlstra <[email protected]Cc Masami Hiramatsu <[email protected]>Cc Chris Zankel <[email protected]>Cc Max Filippov <[email protected]>Cc Arnd Bergmann <[email protected]>--- 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(-)arch/arm64/kernel/patching.c +0
-0@@ -117,8 +117,8 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg)int i, ret =0 ;struct aarch64_insn_patch *pp = arg;
Quoted 7 lines
arch/arm64/kernel/patching.c static int __kprobes aarch64_insn_patch_text_cb(void *arg) · line 121
Quoted 4 lines
arch/arm64/kernel/patching.c +2
-2-/* 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 () -1 )) {
atomic_inc_returnreturns the incremented value, so the last CPU getsnum_online_cpus(), not (num_online_cpus()- 1).
Oops! You are right, thx.
Quoted 60 lines
Quoted 9 lines
arch/arm64/kernel/patching.c +0
-0for (i =0 ; ret ==0 && i < pp->insn_cnt; i++) ret =aarch64_insn_patch_text_nosync (pp->text_addrs[i], pp->new_insns[i]);arch/csky/kernel/probes/kprobes.c +0
-0arch/csky/kernel/probes/kprobes.c static int __kprobes patch_text_cb(void *priv) · line 33
Quoted 5 lines
arch/csky/kernel/probes/kprobes.c +1
-1@@ -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 (¶m->cpu_count) ==1 ) { +if (atomic_inc_return (¶m->cpu_count) == (num_online_cpus () -1 )) {Ditto.
arch/riscv/kernel/patch.c static int patch_text_cb(void *data) · line 107
Quoted 13 lines
arch/csky/kernel/probes/kprobes.c +0
-0*(u16 *) addr =cpu_to_le16 (param->opcode);dcache_wb_range (addr, addr +2 );atomic_inc (¶m->cpu_count);arch/riscv/kernel/patch.c +1
-1@@ -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 () -1 )) {Ditto.
arch/xtensa/kernel/jump_label.c static int patch_text_stop_machine(void *data) · line 43
Quoted 13 lines
arch/riscv/kernel/patch.c +0
-0ret =patch_text_nosync (patch->addr, &patch->insn,GET_INSN_LENGTH (patch->insn));arch/xtensa/kernel/jump_label.c +1
-1@@ -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 () -1 )) {Ditto.
Quoted 6 lines
arch/xtensa/kernel/jump_label.c +0
-1local_patch_text (patch->addr, patch->data, patch->sz);atomic_inc (&patch->cpu_count); }else { --2.25.1
--
Thanks.
-- Max
Signature · 4 lines
Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/
Guo Ren
maintainerQuoted 40 lines · Max Filippov
Hi Guo Ren,
Quoted 5 lines · On Sat, Mar 12, 2022 at 7:56 AM <[email protected]> wrote:
From: Guo Ren <[email protected]>
These
patch_textimplementations are usingstop_machine_cpuslockedinfrastructure with atomiccpu_count. The origin idea is that whenThe original
Quoted 3 lines
the master CPU
patch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the lastguarantee that remaining CPUs are waiting.
Quoted 1 line
CPU as the master to solve the potaintial risk.
potential
Quoted 11 lines
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Peter Zijlstra <[email protected]Cc Masami Hiramatsu <[email protected]>Cc Chris Zankel <[email protected]>Cc Max Filippov <[email protected]>Cc Arnd Bergmann <[email protected]>Quoted 6 lines
--- 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(-)
Quoted 3 lines
Reviewed-by Max Filippov <[email protected]>I'm curious, is there a specific issue that prompted this patch?
No, theoretical risk.
Quoted 4 lines
--
Thanks.
-- Max
Signature · 4 lines
Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/