All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-21 14:28 Christophe Leroy
  2017-11-21 17:28 ` Meelis Roos
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Christophe Leroy @ 2017-11-21 14:28 UTC (permalink / raw)
  To: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Meelis Roos, Balbir Singh
  Cc: linux-kernel, linuxppc-dev

On powerpc32, patch_instruction() is called by apply_feature_fixups()
which is called from early_init()

There is the following note in front of early_init():
 * Note that the kernel may be running at an address which is different
 * from the address that it was linked at, so we must use RELOC/PTRRELOC
 * to access static data (including strings).  -- paulus

Therefore, slab_is_available() cannot be called yet, and
text_poke_area must be addressed with PTRRELOC()

Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
for patch_instruction()")
Reported-by: Meelis Roos <mroos@linux.ee>
Cc: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
---
 v2: Added missing asm/setup.h

 arch/powerpc/lib/code-patching.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index c9de03e0c1f1..d469224c4ada 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -21,6 +21,7 @@
 #include <asm/tlbflush.h>
 #include <asm/page.h>
 #include <asm/code-patching.h>
+#include <asm/setup.h>
 
 static int __patch_instruction(unsigned int *addr, unsigned int instr)
 {
@@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
 	 * During early early boot patch_instruction is called
 	 * when text_poke_area is not ready, but we still need
 	 * to allow patching. We just do the plain old patching
-	 * We use slab_is_available and per cpu read * via this_cpu_read
-	 * of text_poke_area. Per-CPU areas might not be up early
-	 * this can create problems with just using this_cpu_read()
 	 */
-	if (!slab_is_available() || !this_cpu_read(text_poke_area))
+	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
 		return __patch_instruction(addr, instr);
 
 	local_irq_save(flags);
-- 
2.13.3

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 14:28 [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX Christophe Leroy
@ 2017-11-21 17:28 ` Meelis Roos
  2017-11-21 22:22   ` Benjamin Herrenschmidt
  2017-11-21 23:07   ` Balbir Singh
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 18+ messages in thread
From: Meelis Roos @ 2017-11-21 17:28 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Balbir Singh, linux-kernel, linuxppc-dev


> On powerpc32, patch_instruction() is called by apply_feature_fixups()
> which is called from early_init()
> 
> There is the following note in front of early_init():
>  * Note that the kernel may be running at an address which is different
>  * from the address that it was linked at, so we must use RELOC/PTRRELOC
>  * to access static data (including strings).  -- paulus
> 
> Therefore, slab_is_available() cannot be called yet, and
> text_poke_area must be addressed with PTRRELOC()
> 
> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
> for patch_instruction()")
> Reported-by: Meelis Roos <mroos@linux.ee>
> Cc: Balbir Singh <bsingharora@gmail.com>
> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
> ---
>  v2: Added missing asm/setup.h

It works - thank you! Tested on top of 4.14.0.

For wider powerpc audience: this warning-like INFO bit is present 
independently of theis patch. Is it dangerous for some configuration?

INFO: Uncompressed kernel (size 0x5d6c54) overlaps the address of the wrapper(0x400000)
INFO: Fixing the link_address of wrapper to (0x600000)


-- 
Meelis Roos (mroos@linux.ee)

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 17:28 ` Meelis Roos
@ 2017-11-21 22:22   ` Benjamin Herrenschmidt
  0 siblings, 0 replies; 18+ messages in thread
From: Benjamin Herrenschmidt @ 2017-11-21 22:22 UTC (permalink / raw)
  To: Meelis Roos, Christophe Leroy
  Cc: Paul Mackerras, Michael Ellerman, Scott Wood, Balbir Singh,
	linux-kernel, linuxppc-dev

On Tue, 2017-11-21 at 19:28 +0200, Meelis Roos wrote:
> For wider powerpc audience: this warning-like INFO bit is present 
> independently of theis patch. Is it dangerous for some configuration?
> 
> INFO: Uncompressed kernel (size 0x5d6c54) overlaps the address of the wrapper(0x400000)
> INFO: Fixing the link_address of wrapper to (0x600000)

Mostly means you need more memory than the old default... I don't think
it's an issue unless you are hitting very small systems or your OF
implementation has that memory occupied.

Ben.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 14:28 [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX Christophe Leroy
@ 2017-11-21 23:07   ` Balbir Singh
  2017-11-21 23:07   ` Balbir Singh
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 18+ messages in thread
From: Balbir Singh @ 2017-11-21 23:07 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
<christophe.leroy@c-s.fr> wrote:
> On powerpc32, patch_instruction() is called by apply_feature_fixups()
> which is called from early_init()
>
> There is the following note in front of early_init():
>  * Note that the kernel may be running at an address which is different
>  * from the address that it was linked at, so we must use RELOC/PTRRELOC
>  * to access static data (including strings).  -- paulus
>
> Therefore, slab_is_available() cannot be called yet, and
> text_poke_area must be addressed with PTRRELOC()
>
> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
> for patch_instruction()")
> Reported-by: Meelis Roos <mroos@linux.ee>
> Cc: Balbir Singh <bsingharora@gmail.com>
> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
> ---
>  v2: Added missing asm/setup.h
>
>  arch/powerpc/lib/code-patching.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
> index c9de03e0c1f1..d469224c4ada 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -21,6 +21,7 @@
>  #include <asm/tlbflush.h>
>  #include <asm/page.h>
>  #include <asm/code-patching.h>
> +#include <asm/setup.h>
>
>  static int __patch_instruction(unsigned int *addr, unsigned int instr)
>  {
> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>          * During early early boot patch_instruction is called
>          * when text_poke_area is not ready, but we still need
>          * to allow patching. We just do the plain old patching
> -        * We use slab_is_available and per cpu read * via this_cpu_read
> -        * of text_poke_area. Per-CPU areas might not be up early
> -        * this can create problems with just using this_cpu_read()
>          */
> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>                 return __patch_instruction(addr, instr);

On ppc64, we call apply_feature_fixups() in early_setup() after we've
relocated ourselves. Sorry for missing the ppc32 case. I would like to
avoid PTRRELOC when unnecessary.

Balbir Singh.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-21 23:07   ` Balbir Singh
  0 siblings, 0 replies; 18+ messages in thread
From: Balbir Singh @ 2017-11-21 23:07 UTC (permalink / raw)
  To: Christophe Leroy
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
<christophe.leroy@c-s.fr> wrote:
> On powerpc32, patch_instruction() is called by apply_feature_fixups()
> which is called from early_init()
>
> There is the following note in front of early_init():
>  * Note that the kernel may be running at an address which is different
>  * from the address that it was linked at, so we must use RELOC/PTRRELOC
>  * to access static data (including strings).  -- paulus
>
> Therefore, slab_is_available() cannot be called yet, and
> text_poke_area must be addressed with PTRRELOC()
>
> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
> for patch_instruction()")
> Reported-by: Meelis Roos <mroos@linux.ee>
> Cc: Balbir Singh <bsingharora@gmail.com>
> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
> ---
>  v2: Added missing asm/setup.h
>
>  arch/powerpc/lib/code-patching.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
> index c9de03e0c1f1..d469224c4ada 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -21,6 +21,7 @@
>  #include <asm/tlbflush.h>
>  #include <asm/page.h>
>  #include <asm/code-patching.h>
> +#include <asm/setup.h>
>
>  static int __patch_instruction(unsigned int *addr, unsigned int instr)
>  {
> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>          * During early early boot patch_instruction is called
>          * when text_poke_area is not ready, but we still need
>          * to allow patching. We just do the plain old patching
> -        * We use slab_is_available and per cpu read * via this_cpu_read
> -        * of text_poke_area. Per-CPU areas might not be up early
> -        * this can create problems with just using this_cpu_read()
>          */
> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>                 return __patch_instruction(addr, instr);

On ppc64, we call apply_feature_fixups() in early_setup() after we've
relocated ourselves. Sorry for missing the ppc32 case. I would like to
avoid PTRRELOC when unnecessary.

Balbir Singh.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 23:07   ` Balbir Singh
@ 2017-11-22  7:16     ` Christophe LEROY
  -1 siblings, 0 replies; 18+ messages in thread
From: Christophe LEROY @ 2017-11-22  7:16 UTC (permalink / raw)
  To: Balbir Singh
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)



Le 22/11/2017 à 00:07, Balbir Singh a écrit :
> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
> <christophe.leroy@c-s.fr> wrote:
>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>> which is called from early_init()
>>
>> There is the following note in front of early_init():
>>   * Note that the kernel may be running at an address which is different
>>   * from the address that it was linked at, so we must use RELOC/PTRRELOC
>>   * to access static data (including strings).  -- paulus
>>
>> Therefore, slab_is_available() cannot be called yet, and
>> text_poke_area must be addressed with PTRRELOC()
>>
>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>> for patch_instruction()")
>> Reported-by: Meelis Roos <mroos@linux.ee>
>> Cc: Balbir Singh <bsingharora@gmail.com>
>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>> ---
>>   v2: Added missing asm/setup.h
>>
>>   arch/powerpc/lib/code-patching.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>> index c9de03e0c1f1..d469224c4ada 100644
>> --- a/arch/powerpc/lib/code-patching.c
>> +++ b/arch/powerpc/lib/code-patching.c
>> @@ -21,6 +21,7 @@
>>   #include <asm/tlbflush.h>
>>   #include <asm/page.h>
>>   #include <asm/code-patching.h>
>> +#include <asm/setup.h>
>>
>>   static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>   {
>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>           * During early early boot patch_instruction is called
>>           * when text_poke_area is not ready, but we still need
>>           * to allow patching. We just do the plain old patching
>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>> -        * of text_poke_area. Per-CPU areas might not be up early
>> -        * this can create problems with just using this_cpu_read()
>>           */
>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>                  return __patch_instruction(addr, instr);
> 
> On ppc64, we call apply_feature_fixups() in early_setup() after we've
> relocated ourselves. Sorry for missing the ppc32 case. I would like to
> avoid PTRRELOC when unnecessary.

What do you suggest then ?

Some #ifdef PPC32 around that ?

Christophe


> 
> Balbir Singh.
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-22  7:16     ` Christophe LEROY
  0 siblings, 0 replies; 18+ messages in thread
From: Christophe LEROY @ 2017-11-22  7:16 UTC (permalink / raw)
  To: Balbir Singh
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)



Le 22/11/2017 à 00:07, Balbir Singh a écrit :
> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
> <christophe.leroy@c-s.fr> wrote:
>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>> which is called from early_init()
>>
>> There is the following note in front of early_init():
>>   * Note that the kernel may be running at an address which is different
>>   * from the address that it was linked at, so we must use RELOC/PTRRELOC
>>   * to access static data (including strings).  -- paulus
>>
>> Therefore, slab_is_available() cannot be called yet, and
>> text_poke_area must be addressed with PTRRELOC()
>>
>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>> for patch_instruction()")
>> Reported-by: Meelis Roos <mroos@linux.ee>
>> Cc: Balbir Singh <bsingharora@gmail.com>
>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>> ---
>>   v2: Added missing asm/setup.h
>>
>>   arch/powerpc/lib/code-patching.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>> index c9de03e0c1f1..d469224c4ada 100644
>> --- a/arch/powerpc/lib/code-patching.c
>> +++ b/arch/powerpc/lib/code-patching.c
>> @@ -21,6 +21,7 @@
>>   #include <asm/tlbflush.h>
>>   #include <asm/page.h>
>>   #include <asm/code-patching.h>
>> +#include <asm/setup.h>
>>
>>   static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>   {
>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>           * During early early boot patch_instruction is called
>>           * when text_poke_area is not ready, but we still need
>>           * to allow patching. We just do the plain old patching
>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>> -        * of text_poke_area. Per-CPU areas might not be up early
>> -        * this can create problems with just using this_cpu_read()
>>           */
>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>                  return __patch_instruction(addr, instr);
> 
> On ppc64, we call apply_feature_fixups() in early_setup() after we've
> relocated ourselves. Sorry for missing the ppc32 case. I would like to
> avoid PTRRELOC when unnecessary.

What do you suggest then ?

Some #ifdef PPC32 around that ?

Christophe


> 
> Balbir Singh.
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-22  7:16     ` Christophe LEROY
@ 2017-11-22 11:48       ` Michael Ellerman
  -1 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-22 11:48 UTC (permalink / raw)
  To: Christophe LEROY, Balbir Singh
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Scott Wood, Meelis Roos,
	linux-kernel, open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

Christophe LEROY <christophe.leroy@c-s.fr> writes:

> Le 22/11/2017 à 00:07, Balbir Singh a écrit :
>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>> <christophe.leroy@c-s.fr> wrote:
>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>> which is called from early_init()
>>>
>>> There is the following note in front of early_init():
>>>   * Note that the kernel may be running at an address which is different
>>>   * from the address that it was linked at, so we must use RELOC/PTRRELOC
>>>   * to access static data (including strings).  -- paulus
>>>
>>> Therefore, slab_is_available() cannot be called yet, and
>>> text_poke_area must be addressed with PTRRELOC()
>>>
>>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>>> for patch_instruction()")
>>> Reported-by: Meelis Roos <mroos@linux.ee>
>>> Cc: Balbir Singh <bsingharora@gmail.com>
>>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>>> ---
>>>   v2: Added missing asm/setup.h
>>>
>>>   arch/powerpc/lib/code-patching.c | 6 ++----
>>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>>> index c9de03e0c1f1..d469224c4ada 100644
>>> --- a/arch/powerpc/lib/code-patching.c
>>> +++ b/arch/powerpc/lib/code-patching.c
>>> @@ -21,6 +21,7 @@
>>>   #include <asm/tlbflush.h>
>>>   #include <asm/page.h>
>>>   #include <asm/code-patching.h>
>>> +#include <asm/setup.h>
>>>
>>>   static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>>   {
>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>>           * During early early boot patch_instruction is called
>>>           * when text_poke_area is not ready, but we still need
>>>           * to allow patching. We just do the plain old patching
>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>> -        * this can create problems with just using this_cpu_read()
>>>           */
>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>                  return __patch_instruction(addr, instr);
>> 
>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>> avoid PTRRELOC when unnecessary.
>
> What do you suggest then ?
>
> Some #ifdef PPC32 around that ?

No I don't think that improves anything.

I think the comment about per-cpu not being up is wrong, you'll just get
the static version of text_poke_area, which should be NULL. So we don't
need the slab_available() check anyway.

So I'll take this as-is.

Having said that I absolutely hate PTRRELOC, so if it starts spreading
we will have to come up with something less bug prone.

cheers

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-22 11:48       ` Michael Ellerman
  0 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-22 11:48 UTC (permalink / raw)
  To: Christophe LEROY, Balbir Singh
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Scott Wood, Meelis Roos,
	linux-kernel, open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

Christophe LEROY <christophe.leroy@c-s.fr> writes:

> Le 22/11/2017 =C3=A0 00:07, Balbir Singh a =C3=A9crit=C2=A0:
>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>> <christophe.leroy@c-s.fr> wrote:
>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>> which is called from early_init()
>>>
>>> There is the following note in front of early_init():
>>>   * Note that the kernel may be running at an address which is different
>>>   * from the address that it was linked at, so we must use RELOC/PTRREL=
OC
>>>   * to access static data (including strings).  -- paulus
>>>
>>> Therefore, slab_is_available() cannot be called yet, and
>>> text_poke_area must be addressed with PTRRELOC()
>>>
>>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>>> for patch_instruction()")
>>> Reported-by: Meelis Roos <mroos@linux.ee>
>>> Cc: Balbir Singh <bsingharora@gmail.com>
>>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>>> ---
>>>   v2: Added missing asm/setup.h
>>>
>>>   arch/powerpc/lib/code-patching.c | 6 ++----
>>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-p=
atching.c
>>> index c9de03e0c1f1..d469224c4ada 100644
>>> --- a/arch/powerpc/lib/code-patching.c
>>> +++ b/arch/powerpc/lib/code-patching.c
>>> @@ -21,6 +21,7 @@
>>>   #include <asm/tlbflush.h>
>>>   #include <asm/page.h>
>>>   #include <asm/code-patching.h>
>>> +#include <asm/setup.h>
>>>
>>>   static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>>   {
>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned=
 int instr)
>>>           * During early early boot patch_instruction is called
>>>           * when text_poke_area is not ready, but we still need
>>>           * to allow patching. We just do the plain old patching
>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>> -        * this can create problems with just using this_cpu_read()
>>>           */
>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>                  return __patch_instruction(addr, instr);
>>=20
>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>> avoid PTRRELOC when unnecessary.
>
> What do you suggest then ?
>
> Some #ifdef PPC32 around that ?

No I don't think that improves anything.

I think the comment about per-cpu not being up is wrong, you'll just get
the static version of text_poke_area, which should be NULL. So we don't
need the slab_available() check anyway.

So I'll take this as-is.

Having said that I absolutely hate PTRRELOC, so if it starts spreading
we will have to come up with something less bug prone.

cheers

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 14:28 [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX Christophe Leroy
  2017-11-21 17:28 ` Meelis Roos
  2017-11-21 23:07   ` Balbir Singh
@ 2017-11-22 11:55 ` Michael Ellerman
  2017-11-24  9:46 ` [v2] " Michael Ellerman
  3 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-22 11:55 UTC (permalink / raw)
  To: Christophe Leroy, Benjamin Herrenschmidt, Paul Mackerras,
	Scott Wood, Meelis Roos, Balbir Singh
  Cc: linux-kernel, linuxppc-dev

Christophe Leroy <christophe.leroy@c-s.fr> writes:

> On powerpc32, patch_instruction() is called by apply_feature_fixups()
> which is called from early_init()
>
> There is the following note in front of early_init():
>  * Note that the kernel may be running at an address which is different
>  * from the address that it was linked at, so we must use RELOC/PTRRELOC
>  * to access static data (including strings).  -- paulus
>
> Therefore, slab_is_available() cannot be called yet, and
> text_poke_area must be addressed with PTRRELOC()
>
> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
> for patch_instruction()")

I changed this to:

  Fixes: 95902e6c8864 ("powerpc/mm: Implement STRICT_KERNEL_RWX on PPC32")
  Cc: stable@vger.kernel.org # v4.14+

Because although the code was added in 37bc3e5fd764f, at that point it
couldn't be enabled on 32-bit, so there was no bug.

I'm not saying as the author of 95902e6c8864 that the bug is your fault,
but that is the first commit where the bug actually existed for someone
to hit.

cheers

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-22 11:48       ` Michael Ellerman
@ 2017-11-23  7:49         ` Christophe LEROY
  -1 siblings, 0 replies; 18+ messages in thread
From: Christophe LEROY @ 2017-11-23  7:49 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Balbir Singh, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood,
	Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)



Le 22/11/2017 à 12:48, Michael Ellerman a écrit :
> Christophe LEROY <christophe.leroy@c-s.fr> writes:
> 
>> Le 22/11/2017 à 00:07, Balbir Singh a écrit :
>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>> <christophe.leroy@c-s.fr> wrote:
>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>>> which is called from early_init()
>>>>
>>>> There is the following note in front of early_init():
>>>>    * Note that the kernel may be running at an address which is different
>>>>    * from the address that it was linked at, so we must use RELOC/PTRRELOC
>>>>    * to access static data (including strings).  -- paulus
>>>>
>>>> Therefore, slab_is_available() cannot be called yet, and
>>>> text_poke_area must be addressed with PTRRELOC()
>>>>
>>>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>>>> for patch_instruction()")
>>>> Reported-by: Meelis Roos <mroos@linux.ee>
>>>> Cc: Balbir Singh <bsingharora@gmail.com>
>>>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>>>> ---
>>>>    v2: Added missing asm/setup.h
>>>>
>>>>    arch/powerpc/lib/code-patching.c | 6 ++----
>>>>    1 file changed, 2 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>> --- a/arch/powerpc/lib/code-patching.c
>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>> @@ -21,6 +21,7 @@
>>>>    #include <asm/tlbflush.h>
>>>>    #include <asm/page.h>
>>>>    #include <asm/code-patching.h>
>>>> +#include <asm/setup.h>
>>>>
>>>>    static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>>>    {
>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>>>            * During early early boot patch_instruction is called
>>>>            * when text_poke_area is not ready, but we still need
>>>>            * to allow patching. We just do the plain old patching
>>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>> -        * this can create problems with just using this_cpu_read()
>>>>            */
>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>                   return __patch_instruction(addr, instr);
>>>
>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>>> avoid PTRRELOC when unnecessary.
>>
>> What do you suggest then ?
>>
>> Some #ifdef PPC32 around that ?
> 
> No I don't think that improves anything.
> 
> I think the comment about per-cpu not being up is wrong, you'll just get
> the static version of text_poke_area, which should be NULL. So we don't
> need the slab_available() check anyway.
> 
> So I'll take this as-is.
> 
> Having said that I absolutely hate PTRRELOC, so if it starts spreading
> we will have to come up with something less bug prone.

Would something like that be the solution ?

diff --git a/arch/powerpc/include/asm/code-patching.h 
b/arch/powerpc/include/asm/code-patching.h
index abef812de7f8..1c8dd340f5fc 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -30,7 +30,11 @@ unsigned int create_branch(const unsigned int *addr,
  unsigned int create_cond_branch(const unsigned int *addr,
  				unsigned long target, int flags);
  int patch_branch(unsigned int *addr, unsigned long target, int flags);
-int patch_instruction(unsigned int *addr, unsigned int instr);
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early);
+static inline int patch_instruction(unsigned int *addr, unsigned int instr)
+{
+	return patch_instruction_early(addr, instr, false);
+}

  int instr_is_relative_branch(unsigned int instr);
  int instr_is_branch_to_addr(const unsigned int *instr, unsigned long 
addr);
diff --git a/arch/powerpc/lib/code-patching.c 
b/arch/powerpc/lib/code-patching.c
index d469224c4ada..84ebf9203e40 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -135,7 +135,7 @@ static inline int unmap_patch_area(unsigned long addr)
  	return 0;
  }

-int patch_instruction(unsigned int *addr, unsigned int instr)
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early)
  {
  	int err;
  	unsigned int *dest = NULL;
@@ -148,7 +148,7 @@ int patch_instruction(unsigned int *addr, unsigned 
int instr)
  	 * when text_poke_area is not ready, but we still need
  	 * to allow patching. We just do the plain old patching
  	 */
-	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
+	if (early || !this_cpu_read(text_poke_area))
  		return __patch_instruction(addr, instr);

  	local_irq_save(flags);
@@ -182,13 +182,13 @@ int patch_instruction(unsigned int *addr, unsigned 
int instr)
  }
  #else /* !CONFIG_STRICT_KERNEL_RWX */

-int patch_instruction(unsigned int *addr, unsigned int instr)
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early)
  {
  	return __patch_instruction(addr, instr);
  }

  #endif /* CONFIG_STRICT_KERNEL_RWX */
-NOKPROBE_SYMBOL(patch_instruction);
+NOKPROBE_SYMBOL(patch_instruction_early);

  int patch_branch(unsigned int *addr, unsigned long target, int flags)
  {
diff --git a/arch/powerpc/lib/feature-fixups.c 
b/arch/powerpc/lib/feature-fixups.c
index 41cf5ae273cf..4c98ece9e558 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -45,7 +45,8 @@ static unsigned int *calc_addr(struct fixup_entry 
*fcur, long offset)
  }

  static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
-				 unsigned int *alt_start, unsigned int *alt_end)
+				 unsigned int *alt_start, unsigned int *alt_end,
+				 bool early)
  {
  	unsigned int instr;

@@ -62,12 +63,13 @@ static int patch_alt_instruction(unsigned int *src, 
unsigned int *dest,
  		}
  	}

-	patch_instruction(dest, instr);
+	patch_instruction_early(dest, instr, early);

  	return 0;
  }

-static int patch_feature_section(unsigned long value, struct 
fixup_entry *fcur)
+static int __patch_feature_section(unsigned long value, struct 
fixup_entry *fcur,
+				   bool early)
  {
  	unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;

@@ -86,17 +88,18 @@ static int patch_feature_section(unsigned long 
value, struct fixup_entry *fcur)
  	dest = start;

  	for (; src < alt_end; src++, dest++) {
-		if (patch_alt_instruction(src, dest, alt_start, alt_end))
+		if (patch_alt_instruction(src, dest, alt_start, alt_end, early))
  			return 1;
  	}

  	for (; dest < end; dest++)
-		patch_instruction(dest, PPC_INST_NOP);
+		patch_instruction_early(dest, PPC_INST_NOP, early);

  	return 0;
  }

-void do_feature_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+static void __do_feature_fixups(unsigned long value, void *fixup_start,
+				void *fixup_end, bool early)
  {
  	struct fixup_entry *fcur, *fend;

@@ -104,7 +107,7 @@ void do_feature_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)
  	fend = fixup_end;

  	for (; fcur < fend; fcur++) {
-		if (patch_feature_section(value, fcur)) {
+		if (__patch_feature_section(value, fcur, early)) {
  			WARN_ON(1);
  			printk("Unable to patch feature section at %p - %p" \
  				" with %p - %p\n",
@@ -116,7 +119,13 @@ void do_feature_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)
  	}
  }

-void do_lwsync_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+void do_feature_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+{
+	__do_feature_fixups(value, fixup_start, fixup_end, false);
+}
+
+static void __do_lwsync_fixups(unsigned long value, void *fixup_start,
+			       void *fixup_end, bool early)
  {
  	long *start, *end;
  	unsigned int *dest;
@@ -129,10 +138,15 @@ void do_lwsync_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)

  	for (; start < end; start++) {
  		dest = (void *)start + *start;
-		patch_instruction(dest, PPC_INST_LWSYNC);
+		patch_instruction_early(dest, PPC_INST_LWSYNC, early);
  	}
  }

+void do_lwsync_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+{
+	__do_lwsync_fixups(value, fixup_start, fixup_end, false);
+}
+
  static void do_final_fixups(void)
  {
  #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
@@ -147,7 +161,7 @@ static void do_final_fixups(void)
  	length = (__end_interrupts - _stext) / sizeof(int);

  	while (length--) {
-		patch_instruction(dest, *src);
+		patch_instruction_early(dest, *src, true);
  		src++;
  		dest++;
  	}
@@ -171,22 +185,23 @@ void __init apply_feature_fixups(void)
  	 * Apply the CPU-specific and firmware specific fixups to kernel text
  	 * (nop out sections not relevant to this CPU or this firmware).
  	 */
-	do_feature_fixups(spec->cpu_features,
+	__do_feature_fixups(spec->cpu_features,
  			  PTRRELOC(&__start___ftr_fixup),
-			  PTRRELOC(&__stop___ftr_fixup));
+			  PTRRELOC(&__stop___ftr_fixup), true);

-	do_feature_fixups(spec->mmu_features,
+	__do_feature_fixups(spec->mmu_features,
  			  PTRRELOC(&__start___mmu_ftr_fixup),
-			  PTRRELOC(&__stop___mmu_ftr_fixup));
+			  PTRRELOC(&__stop___mmu_ftr_fixup), true);

-	do_lwsync_fixups(spec->cpu_features,
+	__do_lwsync_fixups(spec->cpu_features,
  			 PTRRELOC(&__start___lwsync_fixup),
-			 PTRRELOC(&__stop___lwsync_fixup));
+			 PTRRELOC(&__stop___lwsync_fixup), true);

  #ifdef CONFIG_PPC64
  	saved_firmware_features = powerpc_firmware_features;
  	do_feature_fixups(powerpc_firmware_features,
-			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
+			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup,
+			  true);
  #endif
  	do_final_fixups();
  }
@@ -226,6 +241,11 @@ late_initcall(check_features);
  /* This must be after the text it fixes up, vmlinux.lds.S enforces 
that atm */
  static struct fixup_entry fixup;

+static int patch_feature_section(unsigned long value, struct 
fixup_entry *fcur)
+{
+	return __patch_feature_section(value, fcur, false);
+}
+
  static long calc_offset(struct fixup_entry *entry, unsigned int *p)
  {
  	return (unsigned long)p - (unsigned long)entry;


Christophe

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-23  7:49         ` Christophe LEROY
  0 siblings, 0 replies; 18+ messages in thread
From: Christophe LEROY @ 2017-11-23  7:49 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Balbir Singh, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood,
	Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)



Le 22/11/2017 à 12:48, Michael Ellerman a écrit :
> Christophe LEROY <christophe.leroy@c-s.fr> writes:
> 
>> Le 22/11/2017 à 00:07, Balbir Singh a écrit :
>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>> <christophe.leroy@c-s.fr> wrote:
>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>>> which is called from early_init()
>>>>
>>>> There is the following note in front of early_init():
>>>>    * Note that the kernel may be running at an address which is different
>>>>    * from the address that it was linked at, so we must use RELOC/PTRRELOC
>>>>    * to access static data (including strings).  -- paulus
>>>>
>>>> Therefore, slab_is_available() cannot be called yet, and
>>>> text_poke_area must be addressed with PTRRELOC()
>>>>
>>>> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
>>>> for patch_instruction()")
>>>> Reported-by: Meelis Roos <mroos@linux.ee>
>>>> Cc: Balbir Singh <bsingharora@gmail.com>
>>>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>>>> ---
>>>>    v2: Added missing asm/setup.h
>>>>
>>>>    arch/powerpc/lib/code-patching.c | 6 ++----
>>>>    1 file changed, 2 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>> --- a/arch/powerpc/lib/code-patching.c
>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>> @@ -21,6 +21,7 @@
>>>>    #include <asm/tlbflush.h>
>>>>    #include <asm/page.h>
>>>>    #include <asm/code-patching.h>
>>>> +#include <asm/setup.h>
>>>>
>>>>    static int __patch_instruction(unsigned int *addr, unsigned int instr)
>>>>    {
>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>>>            * During early early boot patch_instruction is called
>>>>            * when text_poke_area is not ready, but we still need
>>>>            * to allow patching. We just do the plain old patching
>>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>> -        * this can create problems with just using this_cpu_read()
>>>>            */
>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>                   return __patch_instruction(addr, instr);
>>>
>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>>> avoid PTRRELOC when unnecessary.
>>
>> What do you suggest then ?
>>
>> Some #ifdef PPC32 around that ?
> 
> No I don't think that improves anything.
> 
> I think the comment about per-cpu not being up is wrong, you'll just get
> the static version of text_poke_area, which should be NULL. So we don't
> need the slab_available() check anyway.
> 
> So I'll take this as-is.
> 
> Having said that I absolutely hate PTRRELOC, so if it starts spreading
> we will have to come up with something less bug prone.

Would something like that be the solution ?

diff --git a/arch/powerpc/include/asm/code-patching.h 
b/arch/powerpc/include/asm/code-patching.h
index abef812de7f8..1c8dd340f5fc 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -30,7 +30,11 @@ unsigned int create_branch(const unsigned int *addr,
  unsigned int create_cond_branch(const unsigned int *addr,
  				unsigned long target, int flags);
  int patch_branch(unsigned int *addr, unsigned long target, int flags);
-int patch_instruction(unsigned int *addr, unsigned int instr);
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early);
+static inline int patch_instruction(unsigned int *addr, unsigned int instr)
+{
+	return patch_instruction_early(addr, instr, false);
+}

  int instr_is_relative_branch(unsigned int instr);
  int instr_is_branch_to_addr(const unsigned int *instr, unsigned long 
addr);
diff --git a/arch/powerpc/lib/code-patching.c 
b/arch/powerpc/lib/code-patching.c
index d469224c4ada..84ebf9203e40 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -135,7 +135,7 @@ static inline int unmap_patch_area(unsigned long addr)
  	return 0;
  }

-int patch_instruction(unsigned int *addr, unsigned int instr)
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early)
  {
  	int err;
  	unsigned int *dest = NULL;
@@ -148,7 +148,7 @@ int patch_instruction(unsigned int *addr, unsigned 
int instr)
  	 * when text_poke_area is not ready, but we still need
  	 * to allow patching. We just do the plain old patching
  	 */
-	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
+	if (early || !this_cpu_read(text_poke_area))
  		return __patch_instruction(addr, instr);

  	local_irq_save(flags);
@@ -182,13 +182,13 @@ int patch_instruction(unsigned int *addr, unsigned 
int instr)
  }
  #else /* !CONFIG_STRICT_KERNEL_RWX */

-int patch_instruction(unsigned int *addr, unsigned int instr)
+int patch_instruction_early(unsigned int *addr, unsigned int instr, 
bool early)
  {
  	return __patch_instruction(addr, instr);
  }

  #endif /* CONFIG_STRICT_KERNEL_RWX */
-NOKPROBE_SYMBOL(patch_instruction);
+NOKPROBE_SYMBOL(patch_instruction_early);

  int patch_branch(unsigned int *addr, unsigned long target, int flags)
  {
diff --git a/arch/powerpc/lib/feature-fixups.c 
b/arch/powerpc/lib/feature-fixups.c
index 41cf5ae273cf..4c98ece9e558 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -45,7 +45,8 @@ static unsigned int *calc_addr(struct fixup_entry 
*fcur, long offset)
  }

  static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
-				 unsigned int *alt_start, unsigned int *alt_end)
+				 unsigned int *alt_start, unsigned int *alt_end,
+				 bool early)
  {
  	unsigned int instr;

@@ -62,12 +63,13 @@ static int patch_alt_instruction(unsigned int *src, 
unsigned int *dest,
  		}
  	}

-	patch_instruction(dest, instr);
+	patch_instruction_early(dest, instr, early);

  	return 0;
  }

-static int patch_feature_section(unsigned long value, struct 
fixup_entry *fcur)
+static int __patch_feature_section(unsigned long value, struct 
fixup_entry *fcur,
+				   bool early)
  {
  	unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;

@@ -86,17 +88,18 @@ static int patch_feature_section(unsigned long 
value, struct fixup_entry *fcur)
  	dest = start;

  	for (; src < alt_end; src++, dest++) {
-		if (patch_alt_instruction(src, dest, alt_start, alt_end))
+		if (patch_alt_instruction(src, dest, alt_start, alt_end, early))
  			return 1;
  	}

  	for (; dest < end; dest++)
-		patch_instruction(dest, PPC_INST_NOP);
+		patch_instruction_early(dest, PPC_INST_NOP, early);

  	return 0;
  }

-void do_feature_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+static void __do_feature_fixups(unsigned long value, void *fixup_start,
+				void *fixup_end, bool early)
  {
  	struct fixup_entry *fcur, *fend;

@@ -104,7 +107,7 @@ void do_feature_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)
  	fend = fixup_end;

  	for (; fcur < fend; fcur++) {
-		if (patch_feature_section(value, fcur)) {
+		if (__patch_feature_section(value, fcur, early)) {
  			WARN_ON(1);
  			printk("Unable to patch feature section at %p - %p" \
  				" with %p - %p\n",
@@ -116,7 +119,13 @@ void do_feature_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)
  	}
  }

-void do_lwsync_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+void do_feature_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+{
+	__do_feature_fixups(value, fixup_start, fixup_end, false);
+}
+
+static void __do_lwsync_fixups(unsigned long value, void *fixup_start,
+			       void *fixup_end, bool early)
  {
  	long *start, *end;
  	unsigned int *dest;
@@ -129,10 +138,15 @@ void do_lwsync_fixups(unsigned long value, void 
*fixup_start, void *fixup_end)

  	for (; start < end; start++) {
  		dest = (void *)start + *start;
-		patch_instruction(dest, PPC_INST_LWSYNC);
+		patch_instruction_early(dest, PPC_INST_LWSYNC, early);
  	}
  }

+void do_lwsync_fixups(unsigned long value, void *fixup_start, void 
*fixup_end)
+{
+	__do_lwsync_fixups(value, fixup_start, fixup_end, false);
+}
+
  static void do_final_fixups(void)
  {
  #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
@@ -147,7 +161,7 @@ static void do_final_fixups(void)
  	length = (__end_interrupts - _stext) / sizeof(int);

  	while (length--) {
-		patch_instruction(dest, *src);
+		patch_instruction_early(dest, *src, true);
  		src++;
  		dest++;
  	}
@@ -171,22 +185,23 @@ void __init apply_feature_fixups(void)
  	 * Apply the CPU-specific and firmware specific fixups to kernel text
  	 * (nop out sections not relevant to this CPU or this firmware).
  	 */
-	do_feature_fixups(spec->cpu_features,
+	__do_feature_fixups(spec->cpu_features,
  			  PTRRELOC(&__start___ftr_fixup),
-			  PTRRELOC(&__stop___ftr_fixup));
+			  PTRRELOC(&__stop___ftr_fixup), true);

-	do_feature_fixups(spec->mmu_features,
+	__do_feature_fixups(spec->mmu_features,
  			  PTRRELOC(&__start___mmu_ftr_fixup),
-			  PTRRELOC(&__stop___mmu_ftr_fixup));
+			  PTRRELOC(&__stop___mmu_ftr_fixup), true);

-	do_lwsync_fixups(spec->cpu_features,
+	__do_lwsync_fixups(spec->cpu_features,
  			 PTRRELOC(&__start___lwsync_fixup),
-			 PTRRELOC(&__stop___lwsync_fixup));
+			 PTRRELOC(&__stop___lwsync_fixup), true);

  #ifdef CONFIG_PPC64
  	saved_firmware_features = powerpc_firmware_features;
  	do_feature_fixups(powerpc_firmware_features,
-			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
+			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup,
+			  true);
  #endif
  	do_final_fixups();
  }
@@ -226,6 +241,11 @@ late_initcall(check_features);
  /* This must be after the text it fixes up, vmlinux.lds.S enforces 
that atm */
  static struct fixup_entry fixup;

+static int patch_feature_section(unsigned long value, struct 
fixup_entry *fcur)
+{
+	return __patch_feature_section(value, fcur, false);
+}
+
  static long calc_offset(struct fixup_entry *entry, unsigned int *p)
  {
  	return (unsigned long)p - (unsigned long)entry;


Christophe

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-23  7:49         ` Christophe LEROY
@ 2017-11-23 12:04           ` Michael Ellerman
  -1 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-23 12:04 UTC (permalink / raw)
  To: Christophe LEROY
  Cc: Balbir Singh, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood,
	Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

Christophe LEROY <christophe.leroy@c-s.fr> writes:
> Le 22/11/2017 à 12:48, Michael Ellerman a écrit :
>> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>>> Le 22/11/2017 à 00:07, Balbir Singh a écrit :
>>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>>> <christophe.leroy@c-s.fr> wrote:
>>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>>>> which is called from early_init()
...
>>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>>> --- a/arch/powerpc/lib/code-patching.c
>>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>>>>            * During early early boot patch_instruction is called
>>>>>            * when text_poke_area is not ready, but we still need
>>>>>            * to allow patching. We just do the plain old patching
>>>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>>> -        * this can create problems with just using this_cpu_read()
>>>>>            */
>>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>>                   return __patch_instruction(addr, instr);
>>>>
>>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>>>> avoid PTRRELOC when unnecessary.
>>>
>>> What do you suggest then ?
>>>
>>> Some #ifdef PPC32 around that ?
>> 
>> No I don't think that improves anything.
>> 
>> I think the comment about per-cpu not being up is wrong, you'll just get
>> the static version of text_poke_area, which should be NULL. So we don't
>> need the slab_available() check anyway.
>> 
>> So I'll take this as-is.
>> 
>> Having said that I absolutely hate PTRRELOC, so if it starts spreading
>> we will have to come up with something less bug prone.
>
> Would something like that be the solution ?

I don't love that actual patch, there's a lot of churn just for one
flag.

But the idea is not so bad.

In fact I don't think we ever need to use the text_poke_area when we
call do_feature_fixups().

Most of the calls are in early boot.

The exception is for modules, but when we do the fixups *of the module*,
the module text is not mapped read only yet.

So I think we can just do something like below.

cheers


diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h
index abef812de7f8..1090024e8519 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -31,6 +31,7 @@ unsigned int create_cond_branch(const unsigned int *addr,
 				unsigned long target, int flags);
 int patch_branch(unsigned int *addr, unsigned long target, int flags);
 int patch_instruction(unsigned int *addr, unsigned int instr);
+int raw_patch_instruction(unsigned int *addr, unsigned int instr);
 
 int instr_is_relative_branch(unsigned int instr);
 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr);
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index d469224c4ada..d1eb24cbef58 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -23,7 +23,7 @@
 #include <asm/code-patching.h>
 #include <asm/setup.h>
 
-static int __patch_instruction(unsigned int *addr, unsigned int instr)
+int raw_patch_instruction(unsigned int *addr, unsigned int instr)
 {
 	int err;
 
@@ -148,8 +148,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
 	 * when text_poke_area is not ready, but we still need
 	 * to allow patching. We just do the plain old patching
 	 */
-	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
-		return __patch_instruction(addr, instr);
+	if (!this_cpu_read(text_poke_area))
+		return raw_patch_instruction(addr, instr);
 
 	local_irq_save(flags);
 
@@ -184,7 +184,7 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
 
 int patch_instruction(unsigned int *addr, unsigned int instr)
 {
-	return __patch_instruction(addr, instr);
+	return raw_patch_instruction(addr, instr);
 }
 
 #endif /* CONFIG_STRICT_KERNEL_RWX */
diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index 41cf5ae273cf..0872d60ede10 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -62,7 +62,7 @@ static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
 		}
 	}
 
-	patch_instruction(dest, instr);
+	raw_patch_instruction(dest, instr);
 
 	return 0;
 }
@@ -91,7 +91,7 @@ static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
 	}
 
 	for (; dest < end; dest++)
-		patch_instruction(dest, PPC_INST_NOP);
+		raw_patch_instruction(dest, PPC_INST_NOP);
 
 	return 0;
 }
@@ -129,7 +129,7 @@ void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
 
 	for (; start < end; start++) {
 		dest = (void *)start + *start;
-		patch_instruction(dest, PPC_INST_LWSYNC);
+		raw_patch_instruction(dest, PPC_INST_LWSYNC);
 	}
 }
 
@@ -147,7 +147,7 @@ static void do_final_fixups(void)
 	length = (__end_interrupts - _stext) / sizeof(int);
 
 	while (length--) {
-		patch_instruction(dest, *src);
+		raw_patch_instruction(dest, *src);
 		src++;
 		dest++;
 	}

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-23 12:04           ` Michael Ellerman
  0 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-23 12:04 UTC (permalink / raw)
  To: Christophe LEROY
  Cc: Balbir Singh, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood,
	Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

Christophe LEROY <christophe.leroy@c-s.fr> writes:
> Le 22/11/2017 =C3=A0 12:48, Michael Ellerman a =C3=A9crit=C2=A0:
>> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>>> Le 22/11/2017 =C3=A0 00:07, Balbir Singh a =C3=A9crit=C2=A0:
>>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>>> <christophe.leroy@c-s.fr> wrote:
>>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>>>> which is called from early_init()
...
>>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code=
-patching.c
>>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>>> --- a/arch/powerpc/lib/code-patching.c
>>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsign=
ed int instr)
>>>>>            * During early early boot patch_instruction is called
>>>>>            * when text_poke_area is not ready, but we still need
>>>>>            * to allow patching. We just do the plain old patching
>>>>> -        * We use slab_is_available and per cpu read * via this_cpu_r=
ead
>>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>>> -        * this can create problems with just using this_cpu_read()
>>>>>            */
>>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>>                   return __patch_instruction(addr, instr);
>>>>
>>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>>>> avoid PTRRELOC when unnecessary.
>>>
>>> What do you suggest then ?
>>>
>>> Some #ifdef PPC32 around that ?
>>=20
>> No I don't think that improves anything.
>>=20
>> I think the comment about per-cpu not being up is wrong, you'll just get
>> the static version of text_poke_area, which should be NULL. So we don't
>> need the slab_available() check anyway.
>>=20
>> So I'll take this as-is.
>>=20
>> Having said that I absolutely hate PTRRELOC, so if it starts spreading
>> we will have to come up with something less bug prone.
>
> Would something like that be the solution ?

I don't love that actual patch, there's a lot of churn just for one
flag.

But the idea is not so bad.

In fact I don't think we ever need to use the text_poke_area when we
call do_feature_fixups().

Most of the calls are in early boot.

The exception is for modules, but when we do the fixups *of the module*,
the module text is not mapped read only yet.

So I think we can just do something like below.

cheers


diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/includ=
e/asm/code-patching.h
index abef812de7f8..1090024e8519 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -31,6 +31,7 @@ unsigned int create_cond_branch(const unsigned int *addr,
 				unsigned long target, int flags);
 int patch_branch(unsigned int *addr, unsigned long target, int flags);
 int patch_instruction(unsigned int *addr, unsigned int instr);
+int raw_patch_instruction(unsigned int *addr, unsigned int instr);
=20
 int instr_is_relative_branch(unsigned int instr);
 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr);
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patch=
ing.c
index d469224c4ada..d1eb24cbef58 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -23,7 +23,7 @@
 #include <asm/code-patching.h>
 #include <asm/setup.h>
=20
-static int __patch_instruction(unsigned int *addr, unsigned int instr)
+int raw_patch_instruction(unsigned int *addr, unsigned int instr)
 {
 	int err;
=20
@@ -148,8 +148,8 @@ int patch_instruction(unsigned int *addr, unsigned int =
instr)
 	 * when text_poke_area is not ready, but we still need
 	 * to allow patching. We just do the plain old patching
 	 */
-	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
-		return __patch_instruction(addr, instr);
+	if (!this_cpu_read(text_poke_area))
+		return raw_patch_instruction(addr, instr);
=20
 	local_irq_save(flags);
=20
@@ -184,7 +184,7 @@ int patch_instruction(unsigned int *addr, unsigned int =
instr)
=20
 int patch_instruction(unsigned int *addr, unsigned int instr)
 {
-	return __patch_instruction(addr, instr);
+	return raw_patch_instruction(addr, instr);
 }
=20
 #endif /* CONFIG_STRICT_KERNEL_RWX */
diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-f=
ixups.c
index 41cf5ae273cf..0872d60ede10 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -62,7 +62,7 @@ static int patch_alt_instruction(unsigned int *src, unsig=
ned int *dest,
 		}
 	}
=20
-	patch_instruction(dest, instr);
+	raw_patch_instruction(dest, instr);
=20
 	return 0;
 }
@@ -91,7 +91,7 @@ static int patch_feature_section(unsigned long value, str=
uct fixup_entry *fcur)
 	}
=20
 	for (; dest < end; dest++)
-		patch_instruction(dest, PPC_INST_NOP);
+		raw_patch_instruction(dest, PPC_INST_NOP);
=20
 	return 0;
 }
@@ -129,7 +129,7 @@ void do_lwsync_fixups(unsigned long value, void *fixup_=
start, void *fixup_end)
=20
 	for (; start < end; start++) {
 		dest =3D (void *)start + *start;
-		patch_instruction(dest, PPC_INST_LWSYNC);
+		raw_patch_instruction(dest, PPC_INST_LWSYNC);
 	}
 }
=20
@@ -147,7 +147,7 @@ static void do_final_fixups(void)
 	length =3D (__end_interrupts - _stext) / sizeof(int);
=20
 	while (length--) {
-		patch_instruction(dest, *src);
+		raw_patch_instruction(dest, *src);
 		src++;
 		dest++;
 	}

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-21 14:28 [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX Christophe Leroy
                   ` (2 preceding siblings ...)
  2017-11-22 11:55 ` Michael Ellerman
@ 2017-11-24  9:46 ` Michael Ellerman
  3 siblings, 0 replies; 18+ messages in thread
From: Michael Ellerman @ 2017-11-24  9:46 UTC (permalink / raw)
  To: Christophe Leroy, Benjamin Herrenschmidt, Paul Mackerras,
	Scott Wood, Meelis Roos, Balbir Singh
  Cc: linuxppc-dev, linux-kernel

On Tue, 2017-11-21 at 14:28:20 UTC, Christophe Leroy wrote:
> On powerpc32, patch_instruction() is called by apply_feature_fixups()
> which is called from early_init()
> 
> There is the following note in front of early_init():
>  * Note that the kernel may be running at an address which is different
>  * from the address that it was linked at, so we must use RELOC/PTRRELOC
>  * to access static data (including strings).  -- paulus
> 
> Therefore, slab_is_available() cannot be called yet, and
> text_poke_area must be addressed with PTRRELOC()
> 
> Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
> for patch_instruction()")
> Reported-by: Meelis Roos <mroos@linux.ee>
> Cc: Balbir Singh <bsingharora@gmail.com>
> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/252eb55816a6f69ef9464cad303cdb

cheers

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
  2017-11-23 12:04           ` Michael Ellerman
@ 2017-11-25 23:57             ` Balbir Singh
  -1 siblings, 0 replies; 18+ messages in thread
From: Balbir Singh @ 2017-11-25 23:57 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Christophe LEROY, Benjamin Herrenschmidt, Paul Mackerras,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

On Thu, Nov 23, 2017 at 11:04 PM, Michael Ellerman <mpe@ellerman.id.au> wrote:
> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>> Le 22/11/2017 à 12:48, Michael Ellerman a écrit :
>>> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>>>> Le 22/11/2017 à 00:07, Balbir Singh a écrit :
>>>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>>>> <christophe.leroy@c-s.fr> wrote:
>>>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups()
>>>>>> which is called from early_init()
> ...
>>>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
>>>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>>>> --- a/arch/powerpc/lib/code-patching.c
>>>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>>>>>>            * During early early boot patch_instruction is called
>>>>>>            * when text_poke_area is not ready, but we still need
>>>>>>            * to allow patching. We just do the plain old patching
>>>>>> -        * We use slab_is_available and per cpu read * via this_cpu_read
>>>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>>>> -        * this can create problems with just using this_cpu_read()
>>>>>>            */
>>>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>>>                   return __patch_instruction(addr, instr);
>>>>>
>>>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>>>> relocated ourselves. Sorry for missing the ppc32 case. I would like to
>>>>> avoid PTRRELOC when unnecessary.
>>>>
>>>> What do you suggest then ?
>>>>
>>>> Some #ifdef PPC32 around that ?
>>>
>>> No I don't think that improves anything.
>>>
>>> I think the comment about per-cpu not being up is wrong, you'll just get
>>> the static version of text_poke_area, which should be NULL. So we don't
>>> need the slab_available() check anyway.
>>>
>>> So I'll take this as-is.
>>>
>>> Having said that I absolutely hate PTRRELOC, so if it starts spreading
>>> we will have to come up with something less bug prone.
>>
>> Would something like that be the solution ?
>
> I don't love that actual patch, there's a lot of churn just for one
> flag.
>
> But the idea is not so bad.
>
> In fact I don't think we ever need to use the text_poke_area when we
> call do_feature_fixups().
>
> Most of the calls are in early boot.
>
> The exception is for modules, but when we do the fixups *of the module*,
> the module text is not mapped read only yet.
>
> So I think we can just do something like below.
>
> cheers
>
>
> diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h
> index abef812de7f8..1090024e8519 100644
> --- a/arch/powerpc/include/asm/code-patching.h
> +++ b/arch/powerpc/include/asm/code-patching.h
> @@ -31,6 +31,7 @@ unsigned int create_cond_branch(const unsigned int *addr,
>                                 unsigned long target, int flags);
>  int patch_branch(unsigned int *addr, unsigned long target, int flags);
>  int patch_instruction(unsigned int *addr, unsigned int instr);
> +int raw_patch_instruction(unsigned int *addr, unsigned int instr);
>
>  int instr_is_relative_branch(unsigned int instr);
>  int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr);
> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
> index d469224c4ada..d1eb24cbef58 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -23,7 +23,7 @@
>  #include <asm/code-patching.h>
>  #include <asm/setup.h>
>
> -static int __patch_instruction(unsigned int *addr, unsigned int instr)
> +int raw_patch_instruction(unsigned int *addr, unsigned int instr)
>  {
>         int err;
>
> @@ -148,8 +148,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>          * when text_poke_area is not ready, but we still need
>          * to allow patching. We just do the plain old patching
>          */
> -       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
> -               return __patch_instruction(addr, instr);
> +       if (!this_cpu_read(text_poke_area))
> +               return raw_patch_instruction(addr, instr);
>
>         local_irq_save(flags);
>
> @@ -184,7 +184,7 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
>
>  int patch_instruction(unsigned int *addr, unsigned int instr)
>  {
> -       return __patch_instruction(addr, instr);
> +       return raw_patch_instruction(addr, instr);
>  }
>
>  #endif /* CONFIG_STRICT_KERNEL_RWX */
> diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
> index 41cf5ae273cf..0872d60ede10 100644
> --- a/arch/powerpc/lib/feature-fixups.c
> +++ b/arch/powerpc/lib/feature-fixups.c
> @@ -62,7 +62,7 @@ static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
>                 }
>         }
>
> -       patch_instruction(dest, instr);
> +       raw_patch_instruction(dest, instr);
>
>         return 0;
>  }
> @@ -91,7 +91,7 @@ static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
>         }
>
>         for (; dest < end; dest++)
> -               patch_instruction(dest, PPC_INST_NOP);
> +               raw_patch_instruction(dest, PPC_INST_NOP);
>
>         return 0;
>  }
> @@ -129,7 +129,7 @@ void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
>
>         for (; start < end; start++) {
>                 dest = (void *)start + *start;
> -               patch_instruction(dest, PPC_INST_LWSYNC);
> +               raw_patch_instruction(dest, PPC_INST_LWSYNC);
>         }
>  }
>
> @@ -147,7 +147,7 @@ static void do_final_fixups(void)
>         length = (__end_interrupts - _stext) / sizeof(int);
>
>         while (length--) {
> -               patch_instruction(dest, *src);
> +               raw_patch_instruction(dest, *src);
>                 src++;
>                 dest++;
>         }

This looks more promising, but there is a subtle dependence between
marking areas as R/O/X and the raw_patch_ins* bits

I saw that Michael has merged that patch as is, I guess we get to
continue to optimise :)

Balbir

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-25 23:57             ` Balbir Singh
  0 siblings, 0 replies; 18+ messages in thread
From: Balbir Singh @ 2017-11-25 23:57 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Christophe LEROY, Benjamin Herrenschmidt, Paul Mackerras,
	Scott Wood, Meelis Roos, linux-kernel,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)

On Thu, Nov 23, 2017 at 11:04 PM, Michael Ellerman <mpe@ellerman.id.au> wro=
te:
> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>> Le 22/11/2017 =C3=A0 12:48, Michael Ellerman a =C3=A9crit :
>>> Christophe LEROY <christophe.leroy@c-s.fr> writes:
>>>> Le 22/11/2017 =C3=A0 00:07, Balbir Singh a =C3=A9crit :
>>>>> On Wed, Nov 22, 2017 at 1:28 AM, Christophe Leroy
>>>>> <christophe.leroy@c-s.fr> wrote:
>>>>>> On powerpc32, patch_instruction() is called by apply_feature_fixups(=
)
>>>>>> which is called from early_init()
> ...
>>>>>> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/cod=
e-patching.c
>>>>>> index c9de03e0c1f1..d469224c4ada 100644
>>>>>> --- a/arch/powerpc/lib/code-patching.c
>>>>>> +++ b/arch/powerpc/lib/code-patching.c
>>>>>> @@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsig=
ned int instr)
>>>>>>            * During early early boot patch_instruction is called
>>>>>>            * when text_poke_area is not ready, but we still need
>>>>>>            * to allow patching. We just do the plain old patching
>>>>>> -        * We use slab_is_available and per cpu read * via this_cpu_=
read
>>>>>> -        * of text_poke_area. Per-CPU areas might not be up early
>>>>>> -        * this can create problems with just using this_cpu_read()
>>>>>>            */
>>>>>> -       if (!slab_is_available() || !this_cpu_read(text_poke_area))
>>>>>> +       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
>>>>>>                   return __patch_instruction(addr, instr);
>>>>>
>>>>> On ppc64, we call apply_feature_fixups() in early_setup() after we've
>>>>> relocated ourselves. Sorry for missing the ppc32 case. I would like t=
o
>>>>> avoid PTRRELOC when unnecessary.
>>>>
>>>> What do you suggest then ?
>>>>
>>>> Some #ifdef PPC32 around that ?
>>>
>>> No I don't think that improves anything.
>>>
>>> I think the comment about per-cpu not being up is wrong, you'll just ge=
t
>>> the static version of text_poke_area, which should be NULL. So we don't
>>> need the slab_available() check anyway.
>>>
>>> So I'll take this as-is.
>>>
>>> Having said that I absolutely hate PTRRELOC, so if it starts spreading
>>> we will have to come up with something less bug prone.
>>
>> Would something like that be the solution ?
>
> I don't love that actual patch, there's a lot of churn just for one
> flag.
>
> But the idea is not so bad.
>
> In fact I don't think we ever need to use the text_poke_area when we
> call do_feature_fixups().
>
> Most of the calls are in early boot.
>
> The exception is for modules, but when we do the fixups *of the module*,
> the module text is not mapped read only yet.
>
> So I think we can just do something like below.
>
> cheers
>
>
> diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/incl=
ude/asm/code-patching.h
> index abef812de7f8..1090024e8519 100644
> --- a/arch/powerpc/include/asm/code-patching.h
> +++ b/arch/powerpc/include/asm/code-patching.h
> @@ -31,6 +31,7 @@ unsigned int create_cond_branch(const unsigned int *add=
r,
>                                 unsigned long target, int flags);
>  int patch_branch(unsigned int *addr, unsigned long target, int flags);
>  int patch_instruction(unsigned int *addr, unsigned int instr);
> +int raw_patch_instruction(unsigned int *addr, unsigned int instr);
>
>  int instr_is_relative_branch(unsigned int instr);
>  int instr_is_branch_to_addr(const unsigned int *instr, unsigned long add=
r);
> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-pat=
ching.c
> index d469224c4ada..d1eb24cbef58 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -23,7 +23,7 @@
>  #include <asm/code-patching.h>
>  #include <asm/setup.h>
>
> -static int __patch_instruction(unsigned int *addr, unsigned int instr)
> +int raw_patch_instruction(unsigned int *addr, unsigned int instr)
>  {
>         int err;
>
> @@ -148,8 +148,8 @@ int patch_instruction(unsigned int *addr, unsigned in=
t instr)
>          * when text_poke_area is not ready, but we still need
>          * to allow patching. We just do the plain old patching
>          */
> -       if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
> -               return __patch_instruction(addr, instr);
> +       if (!this_cpu_read(text_poke_area))
> +               return raw_patch_instruction(addr, instr);
>
>         local_irq_save(flags);
>
> @@ -184,7 +184,7 @@ int patch_instruction(unsigned int *addr, unsigned in=
t instr)
>
>  int patch_instruction(unsigned int *addr, unsigned int instr)
>  {
> -       return __patch_instruction(addr, instr);
> +       return raw_patch_instruction(addr, instr);
>  }
>
>  #endif /* CONFIG_STRICT_KERNEL_RWX */
> diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature=
-fixups.c
> index 41cf5ae273cf..0872d60ede10 100644
> --- a/arch/powerpc/lib/feature-fixups.c
> +++ b/arch/powerpc/lib/feature-fixups.c
> @@ -62,7 +62,7 @@ static int patch_alt_instruction(unsigned int *src, uns=
igned int *dest,
>                 }
>         }
>
> -       patch_instruction(dest, instr);
> +       raw_patch_instruction(dest, instr);
>
>         return 0;
>  }
> @@ -91,7 +91,7 @@ static int patch_feature_section(unsigned long value, s=
truct fixup_entry *fcur)
>         }
>
>         for (; dest < end; dest++)
> -               patch_instruction(dest, PPC_INST_NOP);
> +               raw_patch_instruction(dest, PPC_INST_NOP);
>
>         return 0;
>  }
> @@ -129,7 +129,7 @@ void do_lwsync_fixups(unsigned long value, void *fixu=
p_start, void *fixup_end)
>
>         for (; start < end; start++) {
>                 dest =3D (void *)start + *start;
> -               patch_instruction(dest, PPC_INST_LWSYNC);
> +               raw_patch_instruction(dest, PPC_INST_LWSYNC);
>         }
>  }
>
> @@ -147,7 +147,7 @@ static void do_final_fixups(void)
>         length =3D (__end_interrupts - _stext) / sizeof(int);
>
>         while (length--) {
> -               patch_instruction(dest, *src);
> +               raw_patch_instruction(dest, *src);
>                 src++;
>                 dest++;
>         }

This looks more promising, but there is a subtle dependence between
marking areas as R/O/X and the raw_patch_ins* bits

I saw that Michael has merged that patch as is, I guess we get to
continue to optimise :)

Balbir

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX
@ 2017-11-21 14:26 Christophe Leroy
  0 siblings, 0 replies; 18+ messages in thread
From: Christophe Leroy @ 2017-11-21 14:26 UTC (permalink / raw)
  To: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman, Scott Wood
  Cc: linux-kernel, linuxppc-dev

On powerpc32, patch_instruction() is called by apply_feature_fixups()
which is called from early_init()

There is the following note in front of early_init():
 * Note that the kernel may be running at an address which is different
 * from the address that it was linked at, so we must use RELOC/PTRRELOC
 * to access static data (including strings).  -- paulus

Therefore, slab_is_available() cannot be called yet, and
text_poke_area must be addressed with PTRRELOC()

Fixes: 37bc3e5fd764f ("powerpc/lib/code-patching: Use alternate map
for patch_instruction()")
Reported-by: Meelis Roos <mroos@linux.ee>
Cc: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
---
 v2: Added missing asm/setup.h

 arch/powerpc/lib/code-patching.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index c9de03e0c1f1..d469224c4ada 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -21,6 +21,7 @@
 #include <asm/tlbflush.h>
 #include <asm/page.h>
 #include <asm/code-patching.h>
+#include <asm/setup.h>
 
 static int __patch_instruction(unsigned int *addr, unsigned int instr)
 {
@@ -146,11 +147,8 @@ int patch_instruction(unsigned int *addr, unsigned int instr)
 	 * During early early boot patch_instruction is called
 	 * when text_poke_area is not ready, but we still need
 	 * to allow patching. We just do the plain old patching
-	 * We use slab_is_available and per cpu read * via this_cpu_read
-	 * of text_poke_area. Per-CPU areas might not be up early
-	 * this can create problems with just using this_cpu_read()
 	 */
-	if (!slab_is_available() || !this_cpu_read(text_poke_area))
+	if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
 		return __patch_instruction(addr, instr);
 
 	local_irq_save(flags);
-- 
2.13.3

^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2017-11-25 23:57 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-21 14:28 [PATCH v2] powerpc: fix boot on BOOK3S_32 with CONFIG_STRICT_KERNEL_RWX Christophe Leroy
2017-11-21 17:28 ` Meelis Roos
2017-11-21 22:22   ` Benjamin Herrenschmidt
2017-11-21 23:07 ` Balbir Singh
2017-11-21 23:07   ` Balbir Singh
2017-11-22  7:16   ` Christophe LEROY
2017-11-22  7:16     ` Christophe LEROY
2017-11-22 11:48     ` Michael Ellerman
2017-11-22 11:48       ` Michael Ellerman
2017-11-23  7:49       ` Christophe LEROY
2017-11-23  7:49         ` Christophe LEROY
2017-11-23 12:04         ` Michael Ellerman
2017-11-23 12:04           ` Michael Ellerman
2017-11-25 23:57           ` Balbir Singh
2017-11-25 23:57             ` Balbir Singh
2017-11-22 11:55 ` Michael Ellerman
2017-11-24  9:46 ` [v2] " Michael Ellerman
  -- strict thread matches above, loose matches on Subject: below --
2017-11-21 14:26 [PATCH v2] " Christophe Leroy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.