Re: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text

2024-04-15 Thread Mike Rapoport
On Mon, Apr 15, 2024 at 12:43:16PM +0200, Peter Zijlstra wrote:
> On Thu, Apr 11, 2024 at 07:05:24PM +0300, Mike Rapoport wrote:
> > diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> > index 45a280f2161c..b4d6868df573 100644
> > --- a/arch/x86/kernel/alternative.c
> > +++ b/arch/x86/kernel/alternative.c
> 
> > @@ -504,17 +513,17 @@ void __init_or_module noinline 
> > apply_alternatives(struct alt_instr *start,
> >  *   patch if feature is *NOT* present.
> >  */
> > if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
> > -   optimize_nops_inplace(instr, a->instrlen);
> > +   optimize_nops_inplace(wr_instr, a->instrlen);
> > continue;
> > }
> >  
> > -   DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: 
> > (%px, len: %d) flags: 0x%x",
> > +   DPRINTK(ALT, "feat: %d*32+%d, old: (%px (%px) len: %d), repl: 
> > (%px (%px), len: %d) flags: 0x%x",
> > a->cpuid >> 5,
> > a->cpuid & 0x1f,
> > -   instr, instr, a->instrlen,
> > -   replacement, a->replacementlen, a->flags);
> > +   instr, wr_instr, a->instrlen,
> > +   replacement, wr_replacement, a->replacementlen, 
> > a->flags);
> 
> I think this, and

I've found printing both address handy when I debugged it, but no strong
feelings here.
 
> >  
> > -   memcpy(insn_buff, replacement, a->replacementlen);
> > +   memcpy(insn_buff, wr_replacement, a->replacementlen);
> > insn_buff_sz = a->replacementlen;
> >  
> > if (a->flags & ALT_FLAG_DIRECT_CALL) {
> > @@ -528,11 +537,11 @@ void __init_or_module noinline 
> > apply_alternatives(struct alt_instr *start,
> >  
> > apply_relocation(insn_buff, a->instrlen, instr, replacement, 
> > a->replacementlen);
> >  
> > -   DUMP_BYTES(ALT, instr, a->instrlen, "%px:   old_insn: ", instr);
> > +   DUMP_BYTES(ALT, wr_instr, a->instrlen, "%px:   old_insn: ", 
> > instr);
> 
> this, want to remain as is. 

here wr_instr is the buffer to dump:

DUMP_BYTES(type, buf, len, fmt, args...)

rather than an address, which remained 'instr'.
 
> > DUMP_BYTES(ALT, replacement, a->replacementlen, "%px:   
> > rpl_insn: ", replacement);
> > DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", 
> > instr);
> >  
> > -   text_poke_early(instr, insn_buff, insn_buff_sz);
> > +   text_poke_early(wr_instr, insn_buff, insn_buff_sz);
> > }
> >  
> > kasan_enable_current();
> 
> The rationale being that we then print an address that can be correlated
> to the kernel image (provided one either kills kaslr or adjusts for it).

-- 
Sincerely yours,
Mike.


Re: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text

2024-04-15 Thread Peter Zijlstra
On Thu, Apr 11, 2024 at 07:05:24PM +0300, Mike Rapoport wrote:
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index 45a280f2161c..b4d6868df573 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c

> @@ -504,17 +513,17 @@ void __init_or_module noinline 
> apply_alternatives(struct alt_instr *start,
>*   patch if feature is *NOT* present.
>*/
>   if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
> - optimize_nops_inplace(instr, a->instrlen);
> + optimize_nops_inplace(wr_instr, a->instrlen);
>   continue;
>   }
>  
> - DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: 
> (%px, len: %d) flags: 0x%x",
> + DPRINTK(ALT, "feat: %d*32+%d, old: (%px (%px) len: %d), repl: 
> (%px (%px), len: %d) flags: 0x%x",
>   a->cpuid >> 5,
>   a->cpuid & 0x1f,
> - instr, instr, a->instrlen,
> - replacement, a->replacementlen, a->flags);
> + instr, wr_instr, a->instrlen,
> + replacement, wr_replacement, a->replacementlen, 
> a->flags);

I think this, and

>  
> - memcpy(insn_buff, replacement, a->replacementlen);
> + memcpy(insn_buff, wr_replacement, a->replacementlen);
>   insn_buff_sz = a->replacementlen;
>  
>   if (a->flags & ALT_FLAG_DIRECT_CALL) {
> @@ -528,11 +537,11 @@ void __init_or_module noinline 
> apply_alternatives(struct alt_instr *start,
>  
>   apply_relocation(insn_buff, a->instrlen, instr, replacement, 
> a->replacementlen);
>  
> - DUMP_BYTES(ALT, instr, a->instrlen, "%px:   old_insn: ", instr);
> + DUMP_BYTES(ALT, wr_instr, a->instrlen, "%px:   old_insn: ", 
> instr);

this, want to remain as is. 

>   DUMP_BYTES(ALT, replacement, a->replacementlen, "%px:   
> rpl_insn: ", replacement);
>   DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", 
> instr);
>  
> - text_poke_early(instr, insn_buff, insn_buff_sz);
> + text_poke_early(wr_instr, insn_buff, insn_buff_sz);
>   }
>  
>   kasan_enable_current();

The rationale being that we then print an address that can be correlated
to the kernel image (provided one either kills kaslr or adjusts for it).


Re: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text

2024-04-14 Thread Mike Rapoport
On Fri, Apr 12, 2024 at 11:08:00AM +0200, Ingo Molnar wrote:
> 
> * Mike Rapoport  wrote:
> 
> > for (s = start; s < end; s++) {
> > void *addr = (void *)s + *s;
> > +   void *wr_addr = addr + module_writable_offset(mod, addr);
> 
> So instead of repeating this pattern in a dozen of places, why not use a 
> simpler method:
> 
>   void *wr_addr = module_writable_address(mod, addr);
> 
> or so, since we have to pass 'addr' to the module code anyway.

Agree.
 
> The text patching code is pretty complex already.
> 
> Thanks,
> 
>   Ingo

-- 
Sincerely yours,
Mike.


Re: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text

2024-04-12 Thread Ingo Molnar


* Mike Rapoport  wrote:

>   for (s = start; s < end; s++) {
>   void *addr = (void *)s + *s;
> + void *wr_addr = addr + module_writable_offset(mod, addr);

So instead of repeating this pattern in a dozen of places, why not use a 
simpler method:

void *wr_addr = module_writable_address(mod, addr);

or so, since we have to pass 'addr' to the module code anyway.

The text patching code is pretty complex already.

Thanks,

Ingo