x86/apic: Fix the issues in x2apic_disable()
Subsystems (2)
PATCH 1/1 x86/apic: Fix the issues in x2apic_disable()
There are two issues in x2apic_disable().
The first issue is that the 'pr_warn' in 'if (x2apic_hw_locked())' will never be executed, because when x2apic_hw_locked() evaluates to true, x2apic_state should be X2APIC_ON_LOCKED. However, the current logic in x2apic_disable() is that if x2apic_state is not X2APIC_ON, it returns early, so the subsequent logic will not be executed.
Therefore, 'if (state != X2APIC_ON)' should be changed to 'if (state < X2APIC_ON)', so that when x2apic_state is X2APIC_ON_LOCKED, the corresponding warning log can be printed.
The second issue is that the current logic of x2apic_disable() first sets x2apic_mode and x2apic_state to 0 and X2APIC_DISABLED, respectively, and then tries to disable x2APIC. However, when the APIC is locked in x2APIC mode, that is, x2apic_state is X2APIC_ON_LOCKED, x2APIC cannot be disabled, which causes the final values of the x2apic_mode/x2apic_state variables to be inconsistent with the actual state of x2APIC.
Let's fix these issues.
--- arch/x86/kernel/apic/apic.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/x86/kernel/apic/apic.c +7 -6
@@ -1775,13 +1775,10 @@ static __init void apic_set_fixmap(bool read_apic);static __init void x2apic_disable (void ) { -u32 x2apic_id, state = x2apic_state; +u32 x2apic_id; - x2apic_mode =0 ; - x2apic_state =X2APIC_DISABLED ; - -if (state !=X2APIC_ON ) -return ; +if (x2apic_state <X2APIC_ON ) + goto out; x2apic_id =read_apic_id ();if (x2apic_id >=255 )
@@ -1799,6 +1796,10 @@ static __init void x2apic_disable(void)* which fails to do the read after x2APIC was disabled. */ apic_set_fixmap (false); + +out: + x2apic_mode =0 ; + x2apic_state =X2APIC_DISABLED ; }static __init void x2apic_enable (void )
Signature · 1 line
2.46.0
arch/x86/kernel/apic/apic.c static __init void apic_set_fixmap(bool read_apic); · line 1781
Quoted 12 lines · Yuntao Wang
static
__initvoidx2apic_disable(void)
{arch/x86/kernel/apic/apic.c +3
-6-u32 x2apic_id, state = x2apic_state; +u32 x2apic_id; - x2apic_mode =0 ; - x2apic_state =X2APIC_DISABLED ; - -if (state !=X2APIC_ON ) -return ; +if (x2apic_state <X2APIC_ON ) + goto out;
There is no point in overwriting the state in case it is < ON, no?
Thanks,
tglx
Quoted 6 lines · Thomas Gleixner
Quoted 5 lines · Yuntao Wang
static
__initvoidx2apic_disable(void)
{arch/x86/kernel/apic/apic.c +1
-1-u32 x2apic_id, state = x2apic_state; +u32 x2apic_id;
Quoted 9 lines
arch/x86/kernel/apic/apic.c static __init void apic_set_fixmap(bool read_apic); · line 1781
Quoted 7 lines
arch/x86/kernel/apic/apic.c +2
-5- x2apic_mode =0 ; - x2apic_state =X2APIC_DISABLED ; - -if (state !=X2APIC_ON ) -return ; +if (x2apic_state <X2APIC_ON ) + goto out;There is no point in overwriting the state in case it is < ON, no?
Are you saying that we should replace 'goto out' with a return statement?
However, when x2apic_disable() is called, it's possible that x2apic_state is X2APIC_OFF. In that case, we should set x2apic_state to X2APIC_DISABLED.
So, I think overwriting the state is necessary.
Thanks,
Yuntao
Quoted 3 lines
Thanks,
tglx
Quoted 7 lines · Yuntao Wang
Quoted 1 line · Thomas Gleixner
There is no point in overwriting the state in case it is < ON, no?
Are you saying that we should replace 'goto out' with a return statement?
However, when
x2apic_disable()is called, it's possible thatx2apic_stateisX2APIC_OFF. In that case, we should setx2apic_statetoX2APIC_DISABLED.
What for? It can't be enabled later on and for the rest of the system it does not matter whether the state is OFF or DISABLED. Either case says:
X2APIC is not enabled.
Thanks,
tglx
PATCH v2 1/1 x86/apic: Fix the issues in x2apic_disable()
There are two issues in x2apic_disable().
The first issue is that the 'pr_warn' in 'if (x2apic_hw_locked())' will never be executed, because when x2apic_hw_locked() evaluates to true, x2apic_state should be X2APIC_ON_LOCKED. However, the current logic in x2apic_disable() is that if x2apic_state is not X2APIC_ON, it returns early, so the subsequent logic will not be executed.
Therefore, 'if (state != X2APIC_ON)' should be changed to 'if (state < X2APIC_ON)', so that when x2apic_state is X2APIC_ON_LOCKED, the corresponding warning log can be printed.
The second issue is that the current logic of x2apic_disable() first sets x2apic_mode and x2apic_state to 0 and X2APIC_DISABLED, respectively, and then tries to disable x2APIC. However, when the APIC is locked in x2APIC mode, that is, x2apic_state is X2APIC_ON_LOCKED, x2APIC cannot be disabled, which causes the final values of the x2apic_mode/x2apic_state variables to be inconsistent with the actual state of x2APIC.
Let's fix these issues.
---v1->v2: Modify according to Thomas Gleixner's suggestion.
arch/x86/kernel/apic/apic.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kernel/apic/apic.c +5 -5
@@ -1775,12 +1775,9 @@ static __init void apic_set_fixmap(bool read_apic);static __init void x2apic_disable (void ) { -u32 x2apic_id, state = x2apic_state; +u32 x2apic_id; - x2apic_mode =0 ; - x2apic_state =X2APIC_DISABLED ; - -if (state !=X2APIC_ON ) +if (x2apic_state <X2APIC_ON )return ; x2apic_id =read_apic_id ();
@@ -1799,6 +1796,9 @@ static __init void x2apic_disable(void)* which fails to do the read after x2APIC was disabled. */ apic_set_fixmap (false); + + x2apic_mode =0 ; + x2apic_state =X2APIC_DISABLED ; }static __init void x2apic_enable (void )
Signature · 1 line
2.46.0
[tip: x86/urgent] x86/apic: Make x2apic_disable() work correctly
The following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 0ecc5be200c84e67114f3640064ba2bae3ba2f5a Gitweb: https://git.kernel.org/tip/0ecc5be200c84e67114f3640064ba2bae3ba2f5a Author: Yuntao Wang <[email protected]> AuthorDate: Tue, 13 Aug 2024 09:48:27 +08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Tue, 13 Aug 2024 15:15:19 +02:00
x86/apic: Make x2apic_disable() work correctly
x2apic_disable() clears x2apic_state and x2apic_mode unconditionally, even when the state is X2APIC_ON_LOCKED, which prevents the kernel to disable it thereby creating inconsistent state.
Due to the early state check for X2APIC_ON, the code path which warns about a locked X2APIC cannot be reached.
Test for state < X2APIC_ON instead and move the clearing of the state and mode variables to the place which actually disables X2APIC.
[ tglx: Massaged change log. Added Fixes tag. Moved clearing so it's at the
right place for back ports ]
--- arch/x86/kernel/apic/apic.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
arch/x86/kernel/apic/apic.c +6 -5
@@ -1775,12 +1775,9 @@ static __init void apic_set_fixmap(bool read_apic);static __init void x2apic_disable (void ) { -u32 x2apic_id, state = x2apic_state; +u32 x2apic_id; - x2apic_mode =0 ; - x2apic_state =X2APIC_DISABLED ; - -if (state !=X2APIC_ON ) +if (x2apic_state <X2APIC_ON )return ; x2apic_id =read_apic_id ();
@@ -1793,6 +1790,10 @@ static __init void x2apic_disable(void) }__x2apic_disable (); + + x2apic_mode =0 ; + x2apic_state =X2APIC_DISABLED ; +/* * Don't reread the APIC ID as it was already done from * check_x2apic() and the APIC driver still is a x2APIC variant,