[PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds

Sebastian Ott posted 3 patches 1 year, 5 months ago
There is a newer version of this series
[PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Sebastian Ott 1 year, 5 months ago
Remove double initializations in cases where that's easily possible
- like extra NULL initialization in static global structures. In the
other cases just silence -Woverride-init.

To fix warnings like the following:
arch/arm64/kvm/hyp/vhe/switch.c:271:43: warning: initialized field overwritten [-Woverride-init]
  271 |         [ESR_ELx_EC_CP15_32]            = kvm_hyp_handle_cp15_32,
      |                                           ^~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Sebastian Ott <sebott@redhat.com>
---
 arch/arm64/kvm/handle_exit.c     | 5 +++++
 arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++----
 arch/arm64/kvm/hyp/vhe/switch.c  | 3 +--
 arch/arm64/kvm/sys_regs.c        | 5 +++++
 4 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index d7c2990e7c9e..2c049746657c 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -291,6 +291,9 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+__diag_push();
+__diag_ignore_all("-Woverride-init", "Allow field overrides in exit_handlers");
+
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_WFx]	= kvm_handle_wfx,
@@ -319,6 +322,8 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_PAC]	= kvm_handle_ptrauth,
 };
 
+__diag_pop();
+
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
index 6af179c6356d..bf49afc01542 100644
--- a/arch/arm64/kvm/hyp/nvhe/switch.c
+++ b/arch/arm64/kvm/hyp/nvhe/switch.c
@@ -200,8 +200,7 @@ static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
 	}
 }
 
-static const exit_handler_fn hyp_exit_handlers[] = {
-	[0 ... ESR_ELx_EC_MAX]		= NULL,
+static const exit_handler_fn hyp_exit_handlers[ESR_ELx_EC_MAX + 1] = {
 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
 	[ESR_ELx_EC_SYS64]		= kvm_hyp_handle_sysreg,
 	[ESR_ELx_EC_SVE]		= kvm_hyp_handle_fpsimd,
@@ -212,8 +211,7 @@ static const exit_handler_fn hyp_exit_handlers[] = {
 	[ESR_ELx_EC_MOPS]		= kvm_hyp_handle_mops,
 };
 
-static const exit_handler_fn pvm_exit_handlers[] = {
-	[0 ... ESR_ELx_EC_MAX]		= NULL,
+static const exit_handler_fn pvm_exit_handlers[ESR_ELx_EC_MAX + 1] = {
 	[ESR_ELx_EC_SYS64]		= kvm_handle_pvm_sys64,
 	[ESR_ELx_EC_SVE]		= kvm_handle_pvm_restricted,
 	[ESR_ELx_EC_FP_ASIMD]		= kvm_hyp_handle_fpsimd,
diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
index 77010b76c150..4e4ff2bd51c9 100644
--- a/arch/arm64/kvm/hyp/vhe/switch.c
+++ b/arch/arm64/kvm/hyp/vhe/switch.c
@@ -415,8 +415,7 @@ static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code)
 	return kvm_hyp_handle_sysreg(vcpu, exit_code);
 }
 
-static const exit_handler_fn hyp_exit_handlers[] = {
-	[0 ... ESR_ELx_EC_MAX]		= NULL,
+static const exit_handler_fn hyp_exit_handlers[ESR_ELx_EC_MAX + 1] = {
 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
 	[ESR_ELx_EC_SYS64]		= kvm_hyp_handle_sysreg_vhe,
 	[ESR_ELx_EC_SVE]		= kvm_hyp_handle_fpsimd,
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index c90324060436..00fb6f1e9a48 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -2255,6 +2255,9 @@ static bool access_zcr_el2(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+__diag_push();
+__diag_ignore_all("-Woverride-init", "Allow field overrides in sys_reg_descs");
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
@@ -2803,6 +2806,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	EL2_REG(SP_EL2, NULL, reset_unknown, 0),
 };
 
+__diag_pop();
+
 static bool kvm_supported_tlbi_s12_op(struct kvm_vcpu *vpcu, u32 instr)
 {
 	struct kvm *kvm = vpcu->kvm;
-- 
2.42.0
Re: [PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Marc Zyngier 1 year, 5 months ago
On Fri, 12 Jul 2024 12:03:30 +0100,
Sebastian Ott <sebott@redhat.com> wrote:
> 
> Remove double initializations in cases where that's easily possible
> - like extra NULL initialization in static global structures. In the
> other cases just silence -Woverride-init.
> 
> To fix warnings like the following:
> arch/arm64/kvm/hyp/vhe/switch.c:271:43: warning: initialized field overwritten [-Woverride-init]
>   271 |         [ESR_ELx_EC_CP15_32]            = kvm_hyp_handle_cp15_32,
>       |                                           ^~~~~~~~~~~~~~~~~~~~~~
> 
> Signed-off-by: Sebastian Ott <sebott@redhat.com>
> ---
>  arch/arm64/kvm/handle_exit.c     | 5 +++++
>  arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++----
>  arch/arm64/kvm/hyp/vhe/switch.c  | 3 +--
>  arch/arm64/kvm/sys_regs.c        | 5 +++++
>  4 files changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
> index d7c2990e7c9e..2c049746657c 100644
> --- a/arch/arm64/kvm/handle_exit.c
> +++ b/arch/arm64/kvm/handle_exit.c
> @@ -291,6 +291,9 @@ static int handle_svc(struct kvm_vcpu *vcpu)
>  	return 1;
>  }
>  
> +__diag_push();
> +__diag_ignore_all("-Woverride-init", "Allow field overrides in exit_handlers");

The wording you are looking for is "Silence stupid warning". I really
mean it. There is really nothing wrong with this code, and if the
compiler doesn't understand the purpose of a default initialiser, then
*maybe* it should be fixed rather than polluting the kernel with this
stuff.

> +
>  static exit_handle_fn arm_exit_handlers[] = {
>  	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
>  	[ESR_ELx_EC_WFx]	= kvm_handle_wfx,
> @@ -319,6 +322,8 @@ static exit_handle_fn arm_exit_handlers[] = {
>  	[ESR_ELx_EC_PAC]	= kvm_handle_ptrauth,
>  };
>  
> +__diag_pop();
> +
>  static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
>  {
>  	u64 esr = kvm_vcpu_get_esr(vcpu);
> diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
> index 6af179c6356d..bf49afc01542 100644
> --- a/arch/arm64/kvm/hyp/nvhe/switch.c
> +++ b/arch/arm64/kvm/hyp/nvhe/switch.c
> @@ -200,8 +200,7 @@ static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
>  	}
>  }
>  
> -static const exit_handler_fn hyp_exit_handlers[] = {
> -	[0 ... ESR_ELx_EC_MAX]		= NULL,
> +static const exit_handler_fn hyp_exit_handlers[ESR_ELx_EC_MAX + 1] = {

Is this really any better? I don't think so. It makes the intent
disappear instead of making it explicit. Intent matters *a lot*.

	M.

-- 
Without deviation from the norm, progress is not possible.
Re: [PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Sebastian Ott 1 year, 5 months ago
On Fri, 12 Jul 2024, Marc Zyngier wrote:
> On Fri, 12 Jul 2024 12:03:30 +0100,
> Sebastian Ott <sebott@redhat.com> wrote:
>>
>> Remove double initializations in cases where that's easily possible
>> - like extra NULL initialization in static global structures. In the
>> other cases just silence -Woverride-init.
>>
>> To fix warnings like the following:
>> arch/arm64/kvm/hyp/vhe/switch.c:271:43: warning: initialized field overwritten [-Woverride-init]
>>   271 |         [ESR_ELx_EC_CP15_32]            = kvm_hyp_handle_cp15_32,
>>       |                                           ^~~~~~~~~~~~~~~~~~~~~~
>>
>> Signed-off-by: Sebastian Ott <sebott@redhat.com>
>> ---
>>  arch/arm64/kvm/handle_exit.c     | 5 +++++
>>  arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++----
>>  arch/arm64/kvm/hyp/vhe/switch.c  | 3 +--
>>  arch/arm64/kvm/sys_regs.c        | 5 +++++
>>  4 files changed, 13 insertions(+), 6 deletions(-)
>>
>> diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
>> index d7c2990e7c9e..2c049746657c 100644
>> --- a/arch/arm64/kvm/handle_exit.c
>> +++ b/arch/arm64/kvm/handle_exit.c
>> @@ -291,6 +291,9 @@ static int handle_svc(struct kvm_vcpu *vcpu)
>>  	return 1;
>>  }
>>
>> +__diag_push();
>> +__diag_ignore_all("-Woverride-init", "Allow field overrides in exit_handlers");
>
> The wording you are looking for is "Silence stupid warning". I really
> mean it. There is really nothing wrong with this code, and if the
> compiler doesn't understand the purpose of a default initialiser, then
> *maybe* it should be fixed rather than polluting the kernel with this
> stuff.
>
[...]
>> -static const exit_handler_fn hyp_exit_handlers[] = {
>> -	[0 ... ESR_ELx_EC_MAX]		= NULL,
>> +static const exit_handler_fn hyp_exit_handlers[ESR_ELx_EC_MAX + 1] = {
>
> Is this really any better? I don't think so. It makes the intent
> disappear instead of making it explicit. Intent matters *a lot*.

I'm not claiming that it's an improvement to the code. But yea, I see your
point. How about disabling that flag in the makefile?

Thanks,
Sebastian
Re: [PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Marc Zyngier 1 year, 5 months ago
On Mon, 15 Jul 2024 11:28:06 +0100,
Sebastian Ott <sebott@redhat.com> wrote:
> 
> On Fri, 12 Jul 2024, Marc Zyngier wrote:
> > On Fri, 12 Jul 2024 12:03:30 +0100,
> > Sebastian Ott <sebott@redhat.com> wrote:
> >> 
> >> -static const exit_handler_fn hyp_exit_handlers[] = {
> >> -	[0 ... ESR_ELx_EC_MAX]		= NULL,
> >> +static const exit_handler_fn hyp_exit_handlers[ESR_ELx_EC_MAX + 1] = {
> > 
> > Is this really any better? I don't think so. It makes the intent
> > disappear instead of making it explicit. Intent matters *a lot*.
> 
> I'm not claiming that it's an improvement to the code.

<rant>
Silencing pointless warnings should never have priority over keeping
the code maintainable and understandable. I hope we can agree we are
not in the business of making the kernel *worse* than it already is on
that front, right?
</rant>

I like good tooling as much as the next kernel tinkerer. But W=1 is,
in its current form, quite the opposite. Mark posted a link to a 5
year old thread, showing a number of ways compilers could use extra
annotation to lift the multiple initialisation ambiguity. This
approach has seen no traction, which is a bit sad.

> But yea, I see your point. How about disabling that flag in the
> makefile?

Yes, that'd be a reasonable workaround until someone fixes the
toolchains. I see that Loongarch is already doing it for the exact
same purpose.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.
Re: [PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Mark Rutland 1 year, 5 months ago
On Fri, Jul 12, 2024 at 10:55:16PM +0100, Marc Zyngier wrote:
> On Fri, 12 Jul 2024 12:03:30 +0100,
> Sebastian Ott <sebott@redhat.com> wrote:
> > 
> > Remove double initializations in cases where that's easily possible
> > - like extra NULL initialization in static global structures. In the
> > other cases just silence -Woverride-init.
> > 
> > To fix warnings like the following:
> > arch/arm64/kvm/hyp/vhe/switch.c:271:43: warning: initialized field overwritten [-Woverride-init]
> >   271 |         [ESR_ELx_EC_CP15_32]            = kvm_hyp_handle_cp15_32,
> >       |                                           ^~~~~~~~~~~~~~~~~~~~~~
> > 
> > Signed-off-by: Sebastian Ott <sebott@redhat.com>
> > ---
> >  arch/arm64/kvm/handle_exit.c     | 5 +++++
> >  arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++----
> >  arch/arm64/kvm/hyp/vhe/switch.c  | 3 +--
> >  arch/arm64/kvm/sys_regs.c        | 5 +++++
> >  4 files changed, 13 insertions(+), 6 deletions(-)
> > 
> > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
> > index d7c2990e7c9e..2c049746657c 100644
> > --- a/arch/arm64/kvm/handle_exit.c
> > +++ b/arch/arm64/kvm/handle_exit.c
> > @@ -291,6 +291,9 @@ static int handle_svc(struct kvm_vcpu *vcpu)
> >  	return 1;
> >  }
> >  
> > +__diag_push();
> > +__diag_ignore_all("-Woverride-init", "Allow field overrides in exit_handlers");
> 
> The wording you are looking for is "Silence stupid warning". I really
> mean it. There is really nothing wrong with this code, and if the
> compiler doesn't understand the purpose of a default initialiser, then
> *maybe* it should be fixed rather than polluting the kernel with this
> stuff.

IMO this would be a lot more palatable if this were an attribute on the
struct or assignment, like we asked for in the past:

  https://lore.kernel.org/lkml/20190809083251.GA48423@lakrids.cambridge.arm.com/

Having something that we could put specifically on the default assignment would
make this a lot more legible and better capture the intent.

Mark.
Re: [PATCH 1/3] KVM: arm64: fix override-init warnings in W=1 builds
Posted by Sebastian Ott 1 year, 5 months ago
On Mon, 15 Jul 2024, Mark Rutland wrote:
> On Fri, Jul 12, 2024 at 10:55:16PM +0100, Marc Zyngier wrote:
>> On Fri, 12 Jul 2024 12:03:30 +0100,
>> Sebastian Ott <sebott@redhat.com> wrote:
>>>
>>> Remove double initializations in cases where that's easily possible
>>> - like extra NULL initialization in static global structures. In the
>>> other cases just silence -Woverride-init.
>>>
>>> To fix warnings like the following:
>>> arch/arm64/kvm/hyp/vhe/switch.c:271:43: warning: initialized field overwritten [-Woverride-init]
>>>   271 |         [ESR_ELx_EC_CP15_32]            = kvm_hyp_handle_cp15_32,
>>>       |                                           ^~~~~~~~~~~~~~~~~~~~~~
>>>
>>> Signed-off-by: Sebastian Ott <sebott@redhat.com>
>>> ---
>>>  arch/arm64/kvm/handle_exit.c     | 5 +++++
>>>  arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++----
>>>  arch/arm64/kvm/hyp/vhe/switch.c  | 3 +--
>>>  arch/arm64/kvm/sys_regs.c        | 5 +++++
>>>  4 files changed, 13 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
>>> index d7c2990e7c9e..2c049746657c 100644
>>> --- a/arch/arm64/kvm/handle_exit.c
>>> +++ b/arch/arm64/kvm/handle_exit.c
>>> @@ -291,6 +291,9 @@ static int handle_svc(struct kvm_vcpu *vcpu)
>>>  	return 1;
>>>  }
>>>
>>> +__diag_push();
>>> +__diag_ignore_all("-Woverride-init", "Allow field overrides in exit_handlers");
>>
>> The wording you are looking for is "Silence stupid warning". I really
>> mean it. There is really nothing wrong with this code, and if the
>> compiler doesn't understand the purpose of a default initialiser, then
>> *maybe* it should be fixed rather than polluting the kernel with this
>> stuff.
>
> IMO this would be a lot more palatable if this were an attribute on the
> struct or assignment, like we asked for in the past:
>
>  https://lore.kernel.org/lkml/20190809083251.GA48423@lakrids.cambridge.arm.com/
>
> Having something that we could put specifically on the default assignment would
> make this a lot more legible and better capture the intent.

Yea, I double checked in the gcc doc - there seems to be no other means to
silence this per var or assignment. We can disable that in the makefile
for the whole file or dir or we keep it as it is.

Thanks,
Sebastian