Skip to contentSkip to conversation

powerpc: Fix livepatch module re-patching issue

PATCH Applied Josh Poimboeuf 7 participants 18 messages Jan 25, 2023 – Feb 5, 2023

Subsystems (2)
lore
Share & tools
lore

Josh Poimboeuf

author maintainer
#0 lore raw

cover powerpc: Fix livepatch module re-patching issue

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

https://lkml.kernel.org/lkml/[email protected]

Josh Poimboeuf (2):
powerpc/module_64: Improve restore_r2() return semantics powerpc/module_64: Fix "expected nop" error on module re-patching

 arch/powerpc/kernel/module_64.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.0
author maintainer
#1 lore raw

PATCH 1/2 powerpc/module_64: Improve restore_r2() return semantics

restore_r2() returns 1 on success, which is surprising for a non-boolean function. Change it to return 0 on success and -errno on error to match kernel coding convention.

---
 arch/powerpc/kernel/module_64.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
arch/powerpc/kernel/module_64.c +6 -9
index 1096d6b3a62c..016e79bba531 100644
@@ -504,7 +504,7 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
 	u32 *prev_insn = instruction - 1;
 
 	if (is_mprofile_ftrace_call(name))
-		return 1;
+		return 0;
 
 	/*
 	 * Make sure the branch isn't a sibling call.  Sibling calls aren't
@@ -512,19 +512,16 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
 	 * restore afterwards.
 	 */
 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
-		return 1;
+		return 0;
 
 	if (*instruction != PPC_RAW_NOP()) {
 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
 			me->name, *instruction, instruction);
-		return 0;
+		return -ENOEXEC;
 	}
 
 	/* ld r2,R2_STACK_OFFSET(r1) */
-	if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
-		return 0;
-
-	return 1;
+	return patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC));
 }
 
 int apply_relocate_add(Elf64_Shdr *sechdrs,
@@ -648,8 +645,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 						strtab + sym->st_name);
 				if (!value)
 					return -ENOENT;
-				if (!restore_r2(strtab + sym->st_name,
-							(u32 *)location + 1, me))
+				if (restore_r2(strtab + sym->st_name,
+					       (u32 *)location + 1, me))
 					return -ENOEXEC;
 			} else
 				value += local_entry_offset(sym);
Signature · 1 line
2.39.0
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] powerpc/module_64: Improve restore_r2() return semantics

Quoted 6 lines · Josh Poimboeuf

restore_r2() returns 1 on success, which is surprising for a non-boolean function. Change it to return 0 on success and -errno on error to match kernel coding convention.

maintainer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] powerpc/module_64: Improve restore_r2() return semantics

In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

restore_r2() returns 1 on success, which is surprising for a non-boolean function. Change it to return 0 on success and -errno on error to match kernel coding convention.

Looks good:

It is in the right direction. Just note that there are more functions with the boolean semantic passed via int return value. But there are also other functions already using the 0/-E* return values so this is rather positive change.

Best Regards,
Petr

author maintainer
#5 lore raw

PATCH 2/2 powerpc/module_64: Fix "expected nop" error on module re-patching

In reply to Josh Poimboeuf

When a module with a livepatched function is unloaded and then reloaded, klp attempts to dynamically re-patch it. On ppc64, that fails with the following error:

module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'

The error happens because the restore r2 instruction had already previously been written into the klp module's replacement function when the original function was patched the first time. So the instruction wasn't a nop as expected.

When the restore r2 instruction has already been patched in, detect that and skip the warning and the instruction write.

---
 arch/powerpc/kernel/module_64.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/powerpc/kernel/module_64.c +12 -2
index 016e79bba531..bf1da99fff74 100644
@@ -502,6 +502,7 @@ static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
 static int restore_r2(const char *name, u32 *instruction, struct module *me)
 {
 	u32 *prev_insn = instruction - 1;
+	u32 insn_val = *instruction;
 
 	if (is_mprofile_ftrace_call(name))
 		return 0;
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
 		return 0;
 
-	if (*instruction != PPC_RAW_NOP()) {
+	/*
+	 * For livepatch, the restore r2 instruction might have already been
+	 * written previously, if the referenced symbol is in a previously
+	 * unloaded module which is now being loaded again.  In that case, skip
+	 * the warning and the instruction write.
+	 */
+	if (insn_val == PPC_INST_LD_TOC)
+		return 0;
+
+	if (insn_val != PPC_RAW_NOP()) {
 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
-			me->name, *instruction, instruction);
+			me->name, insn_val, instruction);
 		return -ENOEXEC;
 	}
 
Signature · 1 line
2.39.0
maintainer
#6 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

Quoted 37 lines · Josh Poimboeuf

When a module with a livepatched function is unloaded and then reloaded, klp attempts to dynamically re-patch it. On ppc64, that fails with the following error:

module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'

The error happens because the restore r2 instruction had already previously been written into the klp module's replacement function when the original function was patched the first time. So the instruction wasn't a nop as expected.

When the restore r2 instruction has already been patched in, detect that and skip the warning and the instruction write.

---
 arch/powerpc/kernel/module_64.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/powerpc/kernel/module_64.c +1 -0
index 016e79bba531..bf1da99fff74 100644
@@ -502,6 +502,7 @@ static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
 static int restore_r2(const char *name, u32 *instruction, struct module *me)
 {
        u32 *prev_insn = instruction - 1;
+       u32 insn_val = *instruction;
 
        if (is_mprofile_ftrace_call(name))
                return 0;
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
        if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
                return 0;

arch/powerpc/kernel/module_64.c static int restore_r2(const char *name, u32 *instruction, struct module *me) · line 525

Quoted 10 lines
arch/powerpc/kernel/module_64.c +8 -1
 
-       if (*instruction != PPC_RAW_NOP()) {
+       /*
+        * For livepatch, the restore r2 instruction might have already been
+        * written previously, if the referenced symbol is in a previously
+        * unloaded module which is now being loaded again.  In that case, skip
+        * the warning and the instruction write.
+        */
+       if (insn_val == PPC_INST_LD_TOC)
+               return 0;

Do we need "sym->st_shndx == SHN_LIVEPATCH" here?

Thanks,
Song

Quoted 11 lines
arch/powerpc/kernel/module_64.c +3 -2
+
+       if (insn_val != PPC_RAW_NOP()) {
                pr_err("%s: Expected nop after call, got %08x at %pS\n",
-                       me->name, *instruction, instruction);
+                       me->name, insn_val, instruction);
                return -ENOEXEC;
        }
 
--

2.39.0

author maintainer
#7 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

Quoted 4 lines · Song Liu
Quoted 4 lines
arch/powerpc/kernel/module_64.c +0 -0
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
        if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
                return 0;
 
Quoted 11 lines

arch/powerpc/kernel/module_64.c static int restore_r2(const char *name, u32 *instruction, struct module *me) · line 525

Quoted 9 lines
arch/powerpc/kernel/module_64.c +8 -1
-       if (*instruction != PPC_RAW_NOP()) {
+       /*
+        * For livepatch, the restore r2 instruction might have already been
+        * written previously, if the referenced symbol is in a previously
+        * unloaded module which is now being loaded again.  In that case, skip
+        * the warning and the instruction write.
+        */
+       if (insn_val == PPC_INST_LD_TOC)
+               return 0;

Do we need "sym->st_shndx == SHN_LIVEPATCH" here?

My original patch had that check, but I dropped it for simplicity.

In the non-livepatch case, the condition should never be true, but it doesn't hurt to check it anyway.

Signature · 1 line
Josh
maintainer
#8 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

Quoted 18 lines · Josh Poimboeuf
Quoted 4 lines · Song Liu
Quoted 4 lines
arch/powerpc/kernel/module_64.c +0 -0
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
        if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
                return 0;
 
Quoted 11 lines

arch/powerpc/kernel/module_64.c static int restore_r2(const char *name, u32 *instruction, struct module *me) · line 525

Quoted 9 lines
arch/powerpc/kernel/module_64.c +8 -1
-       if (*instruction != PPC_RAW_NOP()) {
+       /*
+        * For livepatch, the restore r2 instruction might have already been
+        * written previously, if the referenced symbol is in a previously
+        * unloaded module which is now being loaded again.  In that case, skip
+        * the warning and the instruction write.
+        */
+       if (insn_val == PPC_INST_LD_TOC)
+               return 0;

Do we need "sym->st_shndx == SHN_LIVEPATCH" here?

Quoted 4 lines

My original patch had that check, but I dropped it for simplicity.

In the non-livepatch case, the condition should never be true, but it doesn't hurt to check it anyway.

While this is the only place we use PPC_INST_LD_TOC, there is another place we use "PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET)", which is identical to PPC_INST_LD_TOC. So I am not quite sure whether this happens for non-livepatch.

Thanks,
Song

author maintainer
#9 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

Quoted 21 lines · Song Liu
Quoted 20 lines · Josh Poimboeuf
Quoted 4 lines · Song Liu
Quoted 4 lines
arch/powerpc/kernel/module_64.c +0 -0
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
        if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
                return 0;
 
Quoted 11 lines

arch/powerpc/kernel/module_64.c static int restore_r2(const char *name, u32 *instruction, struct module *me) · line 525

Quoted 9 lines
arch/powerpc/kernel/module_64.c +8 -1
-       if (*instruction != PPC_RAW_NOP()) {
+       /*
+        * For livepatch, the restore r2 instruction might have already been
+        * written previously, if the referenced symbol is in a previously
+        * unloaded module which is now being loaded again.  In that case, skip
+        * the warning and the instruction write.
+        */
+       if (insn_val == PPC_INST_LD_TOC)
+               return 0;

Do we need "sym->st_shndx == SHN_LIVEPATCH" here?

My original patch had that check, but I dropped it for simplicity.

Quoted 7 lines
Quoted 2 lines

In the non-livepatch case, the condition should never be true, but it doesn't hurt to check it anyway.

While this is the only place we use PPC_INST_LD_TOC, there is another place we use "PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET)", which is identical to PPC_INST_LD_TOC. So I am not quite sure whether this happens for non-livepatch.

It's not actually identical. That's the "store r2 to the stack" counterpart to the load in PPC_INST_LD_TOC, which loads r2 from the stack.

For R_PPC_REL24 relocations, when calling a function which lives outside the module, 24 bits isn't enough to encode the relative branch target address. So it has to save r2 (TOC pointer) to the stack, and branch to a stub, which then branches to the external function.

When the external function returns execution to the instruction after the original branch, that instruction needs to restore the TOC pointer from the stack to r2.

The compiler knows this, and emits the instruction after the branch as a NOP. The module code replaces that NOP with a "restore r2 from the stack". That's what restore_r2() does.

Long story short, restore_r2() needs to ensure the instruction after the branch restores r2 from the stack. If that instruction is already there, it doesn't need to do anything.

Signature · 1 line
Josh
maintainer Acked-by
#10 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

Quoted 26 lines · Josh Poimboeuf
Quoted 24 lines · Song Liu
Quoted 22 lines · Josh Poimboeuf
Quoted 4 lines · Song Liu
Quoted 4 lines
arch/powerpc/kernel/module_64.c +0 -0
@@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
        if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
                return 0;
 
Quoted 11 lines

arch/powerpc/kernel/module_64.c static int restore_r2(const char *name, u32 *instruction, struct module *me) · line 525

Quoted 9 lines
arch/powerpc/kernel/module_64.c +8 -1
-       if (*instruction != PPC_RAW_NOP()) {
+       /*
+        * For livepatch, the restore r2 instruction might have already been
+        * written previously, if the referenced symbol is in a previously
+        * unloaded module which is now being loaded again.  In that case, skip
+        * the warning and the instruction write.
+        */
+       if (insn_val == PPC_INST_LD_TOC)
+               return 0;

Do we need "sym->st_shndx == SHN_LIVEPATCH" here?

My original patch had that check, but I dropped it for simplicity.

In the non-livepatch case, the condition should never be true, but it doesn't hurt to check it anyway.

Quoted 8 lines
Quoted 4 lines

While this is the only place we use PPC_INST_LD_TOC, there is another place we use "PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET)", which is identical to PPC_INST_LD_TOC. So I am not quite sure whether this happens for non-livepatch.

It's not actually identical. That's the "store r2 to the stack" counterpart to the load in PPC_INST_LD_TOC, which loads r2 from the stack.

Ooops.. I misread the code.

Quoted 10 lines

For R_PPC_REL24 relocations, when calling a function which lives outside the module, 24 bits isn't enough to encode the relative branch target address. So it has to save r2 (TOC pointer) to the stack, and branch to a stub, which then branches to the external function.

When the external function returns execution to the instruction after the original branch, that instruction needs to restore the TOC pointer from the stack to r2.

Quoted 7 lines

The compiler knows this, and emits the instruction after the branch as a NOP. The module code replaces that NOP with a "restore r2 from the stack". That's what restore_r2() does.

Long story short, restore_r2() needs to ensure the instruction after the branch restores r2 from the stack. If that instruction is already there, it doesn't need to do anything.

Thanks for the explanation!

maintainer Reviewed-by on v1 2/2
#11 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

In reply to Josh Poimboeuf
Quoted 13 lines · Josh Poimboeuf

When a module with a livepatched function is unloaded and then reloaded, klp attempts to dynamically re-patch it. On ppc64, that fails with the following error:

module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'

The error happens because the restore r2 instruction had already previously been written into the klp module's replacement function when the original function was patched the first time. So the instruction wasn't a nop as expected.

Quoted 4 lines

When the restore r2 instruction has already been patched in, detect that and skip the warning and the instruction write.

It seems that the function does what it says. And it seems to be the only location where an instruction is checked before it is modified. I am fine with this approach.

Best Regards,
Petr

maintainer Reviewed-by on v1 2/2
#12 lore raw

Re: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching

In reply to Josh Poimboeuf
Quoted 13 lines · Josh Poimboeuf

When a module with a livepatched function is unloaded and then reloaded, klp attempts to dynamically re-patch it. On ppc64, that fails with the following error:

module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'

The error happens because the restore r2 instruction had already previously been written into the klp module's replacement function when the original function was patched the first time. So the instruction wasn't a nop as expected.

Quoted 4 lines

When the restore r2 instruction has already been patched in, detect that and skip the warning and the instruction write.

M

maintainer
#13 lore raw
In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

Quoted 12 lines

https://lkml.kernel.org/lkml/[email protected]

Josh Poimboeuf (2):
powerpc/module_64: Improve restore_r2() return semantics powerpc/module_64: Fix "expected nop" error on module re-patching

 arch/powerpc/kernel/module_64.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.0

For the series,

Reviewed-and-tested-by Joe Lawrence <[email protected]>

--
Joe

author maintainer
#14 lore raw
In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

Quoted 8 lines

https://lkml.kernel.org/lkml/[email protected]

Josh Poimboeuf (2):
powerpc/module_64: Improve restore_r2() return semantics powerpc/module_64: Fix "expected nop" error on module re-patching

 arch/powerpc/kernel/module_64.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

Hi Michael,

Ping? Any objections to this?

The x86 counterpart to this is queued for 6.3, it would be nice if this also landed. We could take it through the livepatch tree if needed.

Signature · 1 line
Josh
#15 lore raw
Quoted 15 lines · Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

Quoted 8 lines

https://lkml.kernel.org/lkml/[email protected]

Josh Poimboeuf (2):
powerpc/module_64: Improve restore_r2() return semantics powerpc/module_64: Fix "expected nop" error on module re-patching

 arch/powerpc/kernel/module_64.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
Quoted 6 lines

Hi Michael,

Ping? Any objections to this?

The x86 counterpart to this is queued for 6.3, it would be nice if this also landed. We could take it through the livepatch tree if needed.

It's in my next since about a week. Sorry I forgot to send the "accepted" emails (which I still don't have automated :/ ).

337251c7114e1 ("powerpc/module_64: Fix "expected nop" error on module re-patching")

cheers

author maintainer
#16 lore raw
Quoted 20 lines · Michael Ellerman
Quoted 19 lines · Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

Quoted 8 lines

https://lkml.kernel.org/lkml/[email protected]

Josh Poimboeuf (2):
powerpc/module_64: Improve restore_r2() return semantics powerpc/module_64: Fix "expected nop" error on module re-patching

 arch/powerpc/kernel/module_64.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

Hi Michael,

Ping? Any objections to this?

Quoted 7 lines
Quoted 2 lines

The x86 counterpart to this is queued for 6.3, it would be nice if this also landed. We could take it through the livepatch tree if needed.

It's in my next since about a week. Sorry I forgot to send the "accepted" emails (which I still don't have automated :/ ).

337251c7114e1 ("powerpc/module_64: Fix "expected nop" error on module re-patching")

Ah, I didn't think to look in -next. Thanks!

Signature · 1 line
Josh
#17 lore raw
In reply to Josh Poimboeuf
Quoted 8 lines · Josh Poimboeuf

Fix a livepatch bug seen when reloading a patched module.

This is the powerpc counterpart to Song Liu's fix for a similar issue on x86:

https://lkml.kernel.org/lkml/[email protected]

[...]

Applied to powerpc/next.

[1/2] powerpc/module_64: Improve restore_r2() return semantics

      https://git.kernel.org/powerpc/c/bc2c6f5695ffa05c838b8b6fc5cd581a672151a1
[2/2] powerpc/module_64: Fix "expected nop" error on module re-patching
      https://git.kernel.org/powerpc/c/37251c7114e1b743b077ca74b93557c1ad92a97e

cheers