diff mbox series

[v1,3/4] powerpc/code-patching: Use jump_label for testing freed initmem

Message ID 0aee964721cab7316cffde21a2ca223cee14d373.1647962456.git.christophe.leroy@csgroup.eu (mailing list archive)
State Accepted
Headers show
Series Kill the time spent in patch_instruction() | expand

Commit Message

Christophe Leroy March 22, 2022, 3:40 p.m. UTC
Once init is done, initmem is freed forever so no need to
test system_state at every call to patch_instruction().

Use jump_label.

This reduces by 2% the time needed to activate ftrace on an 8xx.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
 arch/powerpc/include/asm/code-patching.h | 2 ++
 arch/powerpc/lib/code-patching.c         | 5 ++++-
 arch/powerpc/mm/mem.c                    | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

Comments

Guenter Roeck May 19, 2022, 2:17 a.m. UTC | #1
On Tue, Mar 22, 2022 at 04:40:20PM +0100, Christophe Leroy wrote:
> Once init is done, initmem is freed forever so no need to
> test system_state at every call to patch_instruction().
> 
> Use jump_label.
> 
> This reduces by 2% the time needed to activate ftrace on an 8xx.
> 

It also causes the qemu mpc8544ds emulation to crash.

BUG: Unable to handle kernel data access on write at 0xc122eb34
Faulting instruction address: 0xc001b580
Oops: Kernel access of bad area, sig: 11 [#1]
BE PAGE_SIZE=4K MPC8544 DS
Modules linked in:
CPU: 0 PID: 1 Comm: swapper Not tainted 5.18.0-rc7-next-20220518 #1
NIP:  c001b580 LR: c001b560 CTR: 00000003
REGS: c5107dd0 TRAP: 0300   Not tainted  (5.18.0-rc7-next-20220518)
MSR:  00009000 <EE,ME>  CR: 24000882  XER: 00000000
DEAR: c122eb34 ESR: 00800000
GPR00: c001b560 c5107ec0 c5120020 10000000 00000000 00000078 0c000000 cfffffff
GPR08: c001e9ec 00000001 00000007 00000000 44000882 00000000 c0005178 00000000
GPR16: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
GPR24: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 c1230000
NIP [c001b580] free_initmem+0x48/0xa8
LR [c001b560] free_initmem+0x28/0xa8
Call Trace:
[c5107ec0] [c001b560] free_initmem+0x28/0xa8 (unreliable)
[c5107ee0] [c00051b0] kernel_init+0x38/0x150
[c5107f00] [c001626c] ret_from_kernel_thread+0x5c/0x64
Instruction dump:
3fe0c123 912a00dc 90010024 48000665 3d20c218 8929fa65 2c090000 41820058
813feb34 2c090000 4082003c 39200001 <913feb34> 80010024 3cc0c114 83e1001c

Reverting this patch fixes the problem.

Guenter

---
# bad: [736ee37e2e8eed7fe48d0a37ee5a709514d478b3] Add linux-next specific files for 20220518
# good: [42226c989789d8da4af1de0c31070c96726d990c] Linux 5.18-rc7
git bisect start 'HEAD' 'v5.18-rc7'
# bad: [555b5fa93f08980ccb6bc8e196226046fe047901] Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
git bisect bad 555b5fa93f08980ccb6bc8e196226046fe047901
# bad: [8f5ef5e622d3f217d6542779723566099f370c31] Merge branch 'for-next' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux.git
git bisect bad 8f5ef5e622d3f217d6542779723566099f370c31
# good: [2b7d17d4b7c1ff40f58b0d32be40fc0bb6c582fb] soc: document merges
git bisect good 2b7d17d4b7c1ff40f58b0d32be40fc0bb6c582fb
# good: [4964f9250fbf76cb0b9c1124d5b9ab65de9bfd0e] Merge branch 'clk-next' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git
git bisect good 4964f9250fbf76cb0b9c1124d5b9ab65de9bfd0e
# bad: [18fae10a22071ccd0a2c44df2749ff482132774e] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git
git bisect bad 18fae10a22071ccd0a2c44df2749ff482132774e
# bad: [b4a5aaaa51e4ab7f03eec509d3710d50e52e87a6] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux.git
git bisect bad b4a5aaaa51e4ab7f03eec509d3710d50e52e87a6
# bad: [b6b1c3ce06ca438eb24e0f45bf0e63ecad0369f5] powerpc/rtas: Keep MSR[RI] set when calling RTAS
git bisect bad b6b1c3ce06ca438eb24e0f45bf0e63ecad0369f5
# good: [87ccc6684d3b57e3073f77cf28396b3037154193] powerpc/book3e: Fix sparse report in mm/nohash/fsl_book3e.c
git bisect good 87ccc6684d3b57e3073f77cf28396b3037154193
# good: [f31c618373f2051a32e30002d8eacad7bbbd3885] powerpc: Sort and de-dup primary opcodes in ppc-opcode.h
git bisect good f31c618373f2051a32e30002d8eacad7bbbd3885
# good: [9290c379d19774d8de6e2b895d756004dbad9ce5] powerpc/8xx: Simplify flush_tlb_kernel_range()
git bisect good 9290c379d19774d8de6e2b895d756004dbad9ce5
# bad: [d8d2af70b98109418bb16ff6638d7c1c4336f7fe] cxl/ocxl: Prepare cleanup of powerpc's asm/prom.h
git bisect bad d8d2af70b98109418bb16ff6638d7c1c4336f7fe
# bad: [b033767848c4115e486b1a51946de3bee2ac0fa6] powerpc/code-patching: Use jump_label for testing freed initmem
git bisect bad b033767848c4115e486b1a51946de3bee2ac0fa6
# good: [cb3ac45214c03852430979a43180371a44b74596] powerpc/code-patching: Don't call is_vmalloc_or_module_addr() without CONFIG_MODULES
git bisect good cb3ac45214c03852430979a43180371a44b74596
# first bad commit: [b033767848c4115e486b1a51946de3bee2ac0fa6] powerpc/code-patching: Use jump_label for testing freed initmem
Christophe Leroy May 19, 2022, 6:27 a.m. UTC | #2
Le 19/05/2022 à 04:17, Guenter Roeck a écrit :
> On Tue, Mar 22, 2022 at 04:40:20PM +0100, Christophe Leroy wrote:
>> Once init is done, initmem is freed forever so no need to
>> test system_state at every call to patch_instruction().
>>
>> Use jump_label.
>>
>> This reduces by 2% the time needed to activate ftrace on an 8xx.
>>
> 
> It also causes the qemu mpc8544ds emulation to crash.
> 
> BUG: Unable to handle kernel data access on write at 0xc122eb34
> Faulting instruction address: 0xc001b580
> Oops: Kernel access of bad area, sig: 11 [#1]
> BE PAGE_SIZE=4K MPC8544 DS
> Modules linked in:
> CPU: 0 PID: 1 Comm: swapper Not tainted 5.18.0-rc7-next-20220518 #1
> NIP:  c001b580 LR: c001b560 CTR: 00000003
> REGS: c5107dd0 TRAP: 0300   Not tainted  (5.18.0-rc7-next-20220518)
> MSR:  00009000 <EE,ME>  CR: 24000882  XER: 00000000
> DEAR: c122eb34 ESR: 00800000
> GPR00: c001b560 c5107ec0 c5120020 10000000 00000000 00000078 0c000000 cfffffff
> GPR08: c001e9ec 00000001 00000007 00000000 44000882 00000000 c0005178 00000000
> GPR16: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
> GPR24: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 c1230000
> NIP [c001b580] free_initmem+0x48/0xa8
> LR [c001b560] free_initmem+0x28/0xa8
> Call Trace:
> [c5107ec0] [c001b560] free_initmem+0x28/0xa8 (unreliable)
> [c5107ee0] [c00051b0] kernel_init+0x38/0x150
> [c5107f00] [c001626c] ret_from_kernel_thread+0x5c/0x64
> Instruction dump:
> 3fe0c123 912a00dc 90010024 48000665 3d20c218 8929fa65 2c090000 41820058
> 813feb34 2c090000 4082003c 39200001 <913feb34> 80010024 3cc0c114 83e1001c
> 
> Reverting this patch fixes the problem.
> 

That's strange.

I was able to reproduce the problem.

Removing the __ro_after_init in front of 
DEFINE_STATIC_KEY_FALSE(init_mem_is_free) fixes the problem.

I can't understand why, mark_readonly() is called after free_initmem().

Christophe
Christophe Leroy May 19, 2022, 6:53 a.m. UTC | #3
Le 19/05/2022 à 08:27, Christophe Leroy a écrit :
> 
> 
> Le 19/05/2022 à 04:17, Guenter Roeck a écrit :
>> On Tue, Mar 22, 2022 at 04:40:20PM +0100, Christophe Leroy wrote:
>>> Once init is done, initmem is freed forever so no need to
>>> test system_state at every call to patch_instruction().
>>>
>>> Use jump_label.
>>>
>>> This reduces by 2% the time needed to activate ftrace on an 8xx.
>>>
>>
>> It also causes the qemu mpc8544ds emulation to crash.
>>
>> BUG: Unable to handle kernel data access on write at 0xc122eb34
>> Faulting instruction address: 0xc001b580
>> Oops: Kernel access of bad area, sig: 11 [#1]
>> BE PAGE_SIZE=4K MPC8544 DS
>> Modules linked in:
>> CPU: 0 PID: 1 Comm: swapper Not tainted 5.18.0-rc7-next-20220518 #1
>> NIP:  c001b580 LR: c001b560 CTR: 00000003
>> REGS: c5107dd0 TRAP: 0300   Not tainted  (5.18.0-rc7-next-20220518)
>> MSR:  00009000 <EE,ME>  CR: 24000882  XER: 00000000
>> DEAR: c122eb34 ESR: 00800000
>> GPR00: c001b560 c5107ec0 c5120020 10000000 00000000 00000078 0c000000 cfffffff
>> GPR08: c001e9ec 00000001 00000007 00000000 44000882 00000000 c0005178 00000000
>> GPR16: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
>> GPR24: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 c1230000
>> NIP [c001b580] free_initmem+0x48/0xa8
>> LR [c001b560] free_initmem+0x28/0xa8
>> Call Trace:
>> [c5107ec0] [c001b560] free_initmem+0x28/0xa8 (unreliable)
>> [c5107ee0] [c00051b0] kernel_init+0x38/0x150
>> [c5107f00] [c001626c] ret_from_kernel_thread+0x5c/0x64
>> Instruction dump:
>> 3fe0c123 912a00dc 90010024 48000665 3d20c218 8929fa65 2c090000 41820058
>> 813feb34 2c090000 4082003c 39200001 <913feb34> 80010024 3cc0c114 83e1001c
>>
>> Reverting this patch fixes the problem.
>>
> 
> That's strange.
> 
> I was able to reproduce the problem.
> 
> Removing the __ro_after_init in front of
> DEFINE_STATIC_KEY_FALSE(init_mem_is_free) fixes the problem.
> 
> I can't understand why, mark_readonly() is called after free_initmem().
> 

Moving static_branch_enable(&init_mem_is_free) before mark_initmem_nx() 
also solves the problem.

There must be something wrong with mark_initmem_nx().

Christophe
Christophe Leroy May 19, 2022, 5:27 p.m. UTC | #4
Le 19/05/2022 à 08:53, Christophe Leroy a écrit :
> 
> 
> Le 19/05/2022 à 08:27, Christophe Leroy a écrit :
>>
>>
>> Le 19/05/2022 à 04:17, Guenter Roeck a écrit :
>>> On Tue, Mar 22, 2022 at 04:40:20PM +0100, Christophe Leroy wrote:
>>>> Once init is done, initmem is freed forever so no need to
>>>> test system_state at every call to patch_instruction().
>>>>
>>>> Use jump_label.
>>>>
>>>> This reduces by 2% the time needed to activate ftrace on an 8xx.
>>>>
>>>
>>> It also causes the qemu mpc8544ds emulation to crash.
>>>
>>> BUG: Unable to handle kernel data access on write at 0xc122eb34
>>> Faulting instruction address: 0xc001b580
>>> Oops: Kernel access of bad area, sig: 11 [#1]
>>> BE PAGE_SIZE=4K MPC8544 DS
>>> Modules linked in:
>>> CPU: 0 PID: 1 Comm: swapper Not tainted 5.18.0-rc7-next-20220518 #1
>>> NIP:  c001b580 LR: c001b560 CTR: 00000003
>>> REGS: c5107dd0 TRAP: 0300   Not tainted  (5.18.0-rc7-next-20220518)
>>> MSR:  00009000 <EE,ME>  CR: 24000882  XER: 00000000
>>> DEAR: c122eb34 ESR: 00800000
>>> GPR00: c001b560 c5107ec0 c5120020 10000000 00000000 00000078 0c000000 
>>> cfffffff
>>> GPR08: c001e9ec 00000001 00000007 00000000 44000882 00000000 c0005178 
>>> 00000000
>>> GPR16: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
>>> 00000000
>>> GPR24: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
>>> c1230000
>>> NIP [c001b580] free_initmem+0x48/0xa8
>>> LR [c001b560] free_initmem+0x28/0xa8
>>> Call Trace:
>>> [c5107ec0] [c001b560] free_initmem+0x28/0xa8 (unreliable)
>>> [c5107ee0] [c00051b0] kernel_init+0x38/0x150
>>> [c5107f00] [c001626c] ret_from_kernel_thread+0x5c/0x64
>>> Instruction dump:
>>> 3fe0c123 912a00dc 90010024 48000665 3d20c218 8929fa65 2c090000 41820058
>>> 813feb34 2c090000 4082003c 39200001 <913feb34> 80010024 3cc0c114 
>>> 83e1001c
>>>
>>> Reverting this patch fixes the problem.
>>>
>>
>> That's strange.
>>
>> I was able to reproduce the problem.
>>
>> Removing the __ro_after_init in front of
>> DEFINE_STATIC_KEY_FALSE(init_mem_is_free) fixes the problem.
>>
>> I can't understand why, mark_readonly() is called after free_initmem().
>>
> 
> Moving static_branch_enable(&init_mem_is_free) before mark_initmem_nx() 
> also solves the problem.
> 
> There must be something wrong with mark_initmem_nx().
> 


Fixing patch sent, see 
https://patchwork.ozlabs.org/project/linuxppc-dev/patch/2e35f0fd649c83c5add17a99514ac040767be93a.1652981047.git.christophe.leroy@csgroup.eu/

Christophe
diff mbox series

Patch

diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h
index 409483b2d0ce..bccc3a538b9f 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -22,6 +22,8 @@ 
 #define BRANCH_SET_LINK	0x1
 #define BRANCH_ABSOLUTE	0x2
 
+DECLARE_STATIC_KEY_FALSE(init_mem_is_free);
+
 bool is_offset_in_branch_range(long offset);
 bool is_offset_in_cond_branch_range(long offset);
 int create_branch(ppc_inst_t *instr, const u32 *addr,
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 62692c6031bc..ab434c3853c9 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -8,6 +8,7 @@ 
 #include <linux/init.h>
 #include <linux/cpuhotplug.h>
 #include <linux/uaccess.h>
+#include <linux/jump_label.h>
 
 #include <asm/tlbflush.h>
 #include <asm/page.h>
@@ -193,10 +194,12 @@  static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
 
 #endif /* CONFIG_STRICT_KERNEL_RWX */
 
+__ro_after_init DEFINE_STATIC_KEY_FALSE(init_mem_is_free);
+
 int patch_instruction(u32 *addr, ppc_inst_t instr)
 {
 	/* Make sure we aren't patching a freed init section */
-	if (system_state >= SYSTEM_FREEING_INITMEM && init_section_contains(addr, 4))
+	if (static_branch_likely(&init_mem_is_free) && init_section_contains(addr, 4))
 		return 0;
 
 	return do_patch_instruction(addr, instr);
diff --git a/arch/powerpc/mm/mem.c b/arch/powerpc/mm/mem.c
index 8e301cd8925b..9710d4e0bf08 100644
--- a/arch/powerpc/mm/mem.c
+++ b/arch/powerpc/mm/mem.c
@@ -22,6 +22,7 @@ 
 #include <asm/kasan.h>
 #include <asm/svm.h>
 #include <asm/mmzone.h>
+#include <asm/code-patching.h>
 
 #include <mm/mmu_decl.h>
 
@@ -311,6 +312,7 @@  void free_initmem(void)
 {
 	ppc_md.progress = ppc_printk_progress;
 	mark_initmem_nx();
+	static_branch_enable(&init_mem_is_free);
 	free_initmem_default(POISON_FREE_INITMEM);
 }