2023-01-25 03:38:25

by Josh Poimboeuf

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

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.

Signed-off-by: Josh Poimboeuf <[email protected]>
---
arch/powerpc/kernel/module_64.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c
index 016e79bba531..bf1da99fff74 100644
--- a/arch/powerpc/kernel/module_64.c
+++ b/arch/powerpc/kernel/module_64.c
@@ -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;
}

--
2.39.0



2023-01-25 06:10:17

by Song Liu

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

On Tue, Jan 24, 2023 at 7:38 PM Josh Poimboeuf <[email protected]> wrote:
>
> 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.
>
> Signed-off-by: Josh Poimboeuf <[email protected]>
> ---
> arch/powerpc/kernel/module_64.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c
> index 016e79bba531..bf1da99fff74 100644
> --- a/arch/powerpc/kernel/module_64.c
> +++ b/arch/powerpc/kernel/module_64.c
> @@ -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;

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

Thanks,
Song


> +
> + 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
>

2023-01-25 13:31:37

by Petr Mladek

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

On Tue 2023-01-24 19:38:05, Josh Poimboeuf wrote:
> 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.
>
> Signed-off-by: Josh Poimboeuf <[email protected]>

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.

Reviewed-by: Petr Mladek <[email protected]>

Best Regards,
Petr

2023-01-25 16:46:23

by Josh Poimboeuf

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

On Tue, Jan 24, 2023 at 10:09:56PM -0800, Song Liu wrote:
> > @@ -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;
>
> 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.

--
Josh

2023-01-25 17:36:21

by Song Liu

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

On Wed, Jan 25, 2023 at 8:46 AM Josh Poimboeuf <[email protected]> wrote:
>
> On Tue, Jan 24, 2023 at 10:09:56PM -0800, Song Liu wrote:
> > > @@ -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;
> >
> > 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.

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

2023-01-25 18:53:29

by Josh Poimboeuf

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

On Wed, Jan 25, 2023 at 09:36:02AM -0800, Song Liu wrote:
> On Wed, Jan 25, 2023 at 8:46 AM Josh Poimboeuf <[email protected]> wrote:
> >
> > On Tue, Jan 24, 2023 at 10:09:56PM -0800, Song Liu wrote:
> > > > @@ -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;
> > >
> > > 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.
>
> 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.

--
Josh

2023-01-25 18:59:18

by Song Liu

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

On Wed, Jan 25, 2023 at 10:53 AM Josh Poimboeuf <[email protected]> wrote:
>
> On Wed, Jan 25, 2023 at 09:36:02AM -0800, Song Liu wrote:
> > On Wed, Jan 25, 2023 at 8:46 AM Josh Poimboeuf <[email protected]> wrote:
> > >
> > > On Tue, Jan 24, 2023 at 10:09:56PM -0800, Song Liu wrote:
> > > > > @@ -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;
> > > >
> > > > 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.
> >
> > 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.

>
> 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.

Thanks for the explanation!

Acked-by: Song Liu <[email protected]>

2023-01-27 12:50:06

by Miroslav Benes

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

On Tue, 24 Jan 2023, Josh Poimboeuf wrote:

> 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.
>
> Signed-off-by: Josh Poimboeuf <[email protected]>

Reviewed-by: Miroslav Benes <[email protected]>

M