address violations of MISRA C Rule 16.2
PATCH v2 5/9 xen/domctl: address violations of MISRA C Rule 16.2
In reply to Nicola VetriniRefactor the first clauses so that a violation of
MISRA C Rule 16.2 is resolved (a switch label should be immediately enclosed in the compound statement of the switch).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
Convert fallthrough comments in other clauses to the pseudo-keyword while at it.
No functional change.
--- xen/common/domctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
xen/common/domctl.c +5 -3
@@ -293,6 +293,9 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)switch ( op->cmd ) { +case XEN_DOMCTL_createdomain: + d =NULL ; +break ;case XEN_DOMCTL_assign_device:case XEN_DOMCTL_deassign_device:if ( op->domain ==DOMID_IO )
@@ -302,16 +305,15 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) }else if ( op->domain ==DOMID_INVALID )return -ESRCH ; -/* fall through */ + fallthrough;case XEN_DOMCTL_test_assign_device:case XEN_DOMCTL_vm_event_op:if ( op->domain ==DOMID_INVALID ) { -case XEN_DOMCTL_createdomain: d =NULL ;break ; } -/* fall through */ + fallthrough;default : d =rcu_lock_domain_by_id (op->domain);if ( !d )
Signature · 1 line
2.34.1
v2 cover address violations of MISRA C Rule 16.2
Continuing the work done in [1], these patches make this rule clean (i.e., no residual violation of the rule). Some switches are refactored to avoid interleaving a case label inside an if statement. In some cases this leads to some duplication, but the files where this would have had the greatest impact (the x86 emulator files and svm/emulate.c) are deviated, in order to have a reasonable balance between code changes and deviations.
Nicola Vetrini (9):
x86/vlapic: tidy switch statement and address MISRA violation x86/cpuid: address violation of MISRA C Rule 16.2 x86/irq: tidy switch statement and address MISRA violation x86/efi: tidy switch statement and address MISRA violation xen/domctl: address violations of MISRA C Rule 16.2 x86/hvm: address violations of MISRA C Rule 16.2 xen/xsm: address violation of MISRA C Rule 16.2 xen/domain: deviate MISRA C Rule 16.2 violation
eclair_analysis: deviate x86 emulator for Rule 16.2
.../eclair_analysis/ECLAIR/deviations.ecl | 7 +++++ docs/misra/deviations.rst | 6 ++++ docs/misra/safe.json | 8 +++++ xen/arch/x86/cpuid.c | 31 +++++++++---------- xen/arch/x86/efi/efi-boot.h | 8 +++-- xen/arch/x86/hvm/hypercall.c | 10 +++--- xen/arch/x86/hvm/vlapic.c | 16 ++-------- xen/arch/x86/irq.c | 9 ++---- xen/common/domain.c | 1 + xen/common/domctl.c | 8 +++-- xen/include/xsm/dummy.h | 12 +++---- 11 files changed, 64 insertions(+), 52 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 2/9 x86/cpuid: address violation of MISRA C Rule 16.2
Refactor the switch so that a violation of MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo
keyword "fallthrough" is an allowed exception to Rule 16.3.
No functional change.
--- xen/arch/x86/cpuid.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
xen/arch/x86/cpuid.c +15 -16
@@ -331,23 +331,22 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,switch ( subleaf ) {case 1 : -if ( p->xstate.xsavec || p->xstate.xsaves ) - { -/* -* TODO: Figure out what to do for XSS state. VT-x manages -* host vs guest MSR_XSS automatically, so as soon as we start -* supporting any XSS states, the wrong XSS will be in -* context. -*/ -BUILD_BUG_ON (XSTATE_XSAVES_ONLY !=0 ); - -/* -* Read CPUID[0xD,0/1].EBX from hardware. They vary with -* enabled XSTATE, and appropraite XCR0|XSS are in context. -*/ +if ( !(p->xstate.xsavec || p->xstate.xsaves) ) +break ; +/* +* TODO: Figure out what to do for XSS state. VT-x manages +* host vs guest MSR_XSS automatically, so as soon as we start +* supporting any XSS states, the wrong XSS will be in +* context. +*/ +BUILD_BUG_ON (XSTATE_XSAVES_ONLY !=0 ); + fallthrough;case 0 : - res->b =cpuid_count_ebx (leaf, subleaf); - } +/* +* Read CPUID[0xD,0/1].EBX from hardware. They vary with +* enabled XSTATE, and appropriate XCR0|XSS are in context. +*/ + res->b =cpuid_count_ebx (leaf, subleaf);break ; }break ;
Signature · 1 line
2.34.1
PATCH v2 3/9 x86/irq: tidy switch statement and address MISRA violation
In reply to Nicola VetriniRemove unneded blank lines between switch clauses.
Refactor the clauses so that a MISRA C Rule 16.2 violation is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
No functional change.
---
The initialization of irq is due to gcc thinking that irq may be used uninitizalied in the test after MAP_PIRQ_TYPE_MULTI_MSI
--- xen/arch/x86/irq.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
xen/arch/x86/irq.c +3 -6
@@ -2882,7 +2882,7 @@ int allocate_and_map_gsi_pirq(struct domain *d, int index, int *pirq_p)int allocate_and_map_msi_pirq (struct domain *d,int index,int *pirq_p,int type,struct msi_info *msi) { -int irq, pirq, ret; +int irq =-1 , pirq, ret;ASSERT_PDEV_LIST_IS_READ_LOCKED (d);
@@ -2892,12 +2892,10 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,if ( !msi->table_base ) msi->entry_nr =1 ; irq = index; -if ( irq ==-1 ) - { + fallthrough;case MAP_PIRQ_TYPE_MULTI_MSI : +if ( type ==MAP_PIRQ_TYPE_MULTI_MSI || irq ==-1 ) irq =create_irq (NUMA_NO_NODE , true); - } -if ( irq < nr_irqs_gsi || irq >= nr_irqs ) { dprintk(XENLOG_G_ERR, "dom%d: can't create irq for msi!\n",
@@ -2905,7 +2903,6 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,return -EINVAL ; }break ; -default :dprintk (XENLOG_G_ERR ,"dom%d: wrong pirq type %x\n" , d->domain_id, type);
Signature · 1 line
2.34.1
PATCH v2 6/9 x86/hvm: address violations of MISRA C Rule 16.2
In reply to Nicola VetriniRefactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (a switch label should be immediately enclosed in the compound statement of the switch).
The switch clause ending with the pseudo
keyword "fallthrough" is an allowed exception to Rule 16.3.
--- xen/arch/x86/hvm/hypercall.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hypercall.c +5 -5
@@ -119,12 +119,12 @@ int hvm_hypercall(struct cpu_user_regs *regs) (mode == 8 ? regs->rdi : regs->ebx) == HVMOP_guest_request_vm_event ) break; -if (unlikely (hvm_get_cpl (curr)) ) - { +if ( !unlikely (hvm_get_cpl (curr)) ) +break ; + fallthrough;default : - regs->rax = -EPERM ; -return HVM_HCALL_completed; - } + regs->rax = -EPERM ; +return HVM_HCALL_completed;case 0 :break ; }
Signature · 1 line
2.34.1
PATCH v2 9/9 eclair_analysis: deviate x86 emulator for Rule 16.2
In reply to Nicola VetriniMISRA C Rule 16.2 states:
"A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement".
Since complying with this rule of the x86 emulator would lead to a lot of code duplication, it is deemed better to exempt those files for this guideline.
No functional change.
--- automation/eclair_analysis/ECLAIR/deviations.ecl | 7 +++++++ docs/misra/deviations.rst | 6 ++++++ 2 files changed, 13 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +7 -0
@@ -358,6 +358,13 @@ therefore have the same behavior of a boolean" # Series 16. # +-doc_begin="Complying with the Rule would entail a lot of code duplication in the implementation of the x86 emulator, +therefore it is deemed better to leave such files as is." +-file_tag+={x86_emulate,"^xen/arch/x86/x86_emulate/.*$"} +-file_tag+={x86_svm_emulate,"^xen/arch/x86/hvm/svm/emulate\\.c$"} +-config=MC3R1.R16.2,reports+={deliberate, "any_area(any_loc(file(x86_emulate||x86_svm_emulate)))"} +-doc_end + -doc_begin="Switch clauses ending with continue, goto, return statements are safe." -config=MC3R1.R16.3,terminals+={safe, "node(continue_stmt||goto_stmt||return_stmt)"}
docs/misra/deviations.rst +6 -0
@@ -296,6 +296,12 @@ Deviations related to MISRA C:2012 Rules: therefore have the same behavior of a boolean. - Project-wide deviation; tagged as `deliberate` for ECLAIR. + * - R16.2 + - Complying with the Rule would entail a lot of code duplication in the + implementation of the x86 emulator, therefore it is deemed better to + leave such files as is. + - Tagged as `deliberate` for ECLAIR. + * - R16.3 - Switch clauses ending with continue, goto, return statements are safe. - Tagged as `safe` for ECLAIR.
Signature · 1 line
2.34.1
PATCH v2 1/9 x86/vlapic: tidy switch statement and address MISRA violation
In reply to Nicola VetriniRemove unneded blank lines between switch clauses.
Refactor the last clauses so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement). The switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
No functional change.
--- xen/arch/x86/hvm/vlapic.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
xen/arch/x86/hvm/vlapic.c +3 -13
@@ -976,7 +976,6 @@ int guest_wrmsr_x2apic(struct vcpu *v, uint32_t msr, uint64_t val)if ( val & ~APIC_TPRI_MASK )return X86EMUL_EXCEPTION ;break ; -case APIC_SPIV : if ( val & ~(APIC_VECTOR_MASK | APIC_SPIV_APIC_ENABLED | APIC_SPIV_FOCUS_DISABLED |
@@ -984,38 +983,31 @@ int guest_wrmsr_x2apic(struct vcpu *v, uint32_t msr, uint64_t val) ? APIC_SPIV_DIRECTED_EOI : 0)) )return X86EMUL_EXCEPTION ;break ; -case APIC_LVTT :if ( val & ~(LVT_MASK |APIC_TIMER_MODE_MASK ) )return X86EMUL_EXCEPTION ;break ; -case APIC_LVTTHMR :case APIC_LVTPC :case APIC_CMCI :if ( val & ~(LVT_MASK |APIC_DM_MASK ) )return X86EMUL_EXCEPTION ;break ; -case APIC_LVT0 :case APIC_LVT1 :if ( val & ~LINT_MASK )return X86EMUL_EXCEPTION ;break ; -case APIC_LVTERR :if ( val & ~LVT_MASK )return X86EMUL_EXCEPTION ;break ; -case APIC_TMICT :break ; -case APIC_TDCR :if ( val & ~APIC_TDR_DIV_MASK )return X86EMUL_EXCEPTION ;break ; -case APIC_ICR : if ( (uint32_t)val & ~(APIC_VECTOR_MASK | APIC_DM_MASK | APIC_DEST_MASK | APIC_INT_ASSERT |
@@ -1023,21 +1015,19 @@ int guest_wrmsr_x2apic(struct vcpu *v, uint32_t msr, uint64_t val)return X86EMUL_EXCEPTION ;vlapic_set_reg (vlapic,APIC_ICR2 , val >>32 );break ; -case APIC_SELF_IPI :if ( val & ~APIC_VECTOR_MASK )return X86EMUL_EXCEPTION ; offset =APIC_ICR ; val =APIC_DEST_SELF | (val &APIC_VECTOR_MASK );break ; -case APIC_EOI :case APIC_ESR : -if ( val ) - { +if ( !val ) +break ; + fallthrough;default :return X86EMUL_EXCEPTION ; - } }vlapic_reg_write (v,array_index_nospec (offset,PAGE_SIZE ), val);
Signature · 1 line
2.34.1
PATCH v2 4/9 x86/efi: tidy switch statement and address MISRA violation
In reply to Nicola VetriniRefactor the first clauses so that a violation of
MISRA C Rule 16.2 is resolved (a switch label, "default" in this case, should be immediately enclosed in the compound statement of the switch). Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
Convert fallthrough comments in other clauses to the pseudo-keyword while at it.
No functional change.
---
There is some degree of duplication here between the default clause and the others, but I don't think there is a way to avoid it.
--- xen/arch/x86/efi/efi-boot.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
xen/arch/x86/efi/efi-boot.h +5 -3
@@ -169,20 +169,22 @@ static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable,switch ( desc->Type ) { +default : + type =E820_RESERVED ; +break ;case EfiBootServicesCode:case EfiBootServicesData:if ( map_bs ) { -default : type =E820_RESERVED ;break ; } -/* fall through */ + fallthrough;case EfiConventionalMemory:if ( !trampoline_phys && desc->PhysicalStart + len <=0x100000 && len >= cfg.size && desc->PhysicalStart + len > cfg.addr ) cfg.addr = (desc->PhysicalStart + len - cfg.size) &PAGE_MASK ; -/* fall through */ + fallthrough;case EfiLoaderCode:case EfiLoaderData:if ( desc->Attribute &EFI_MEMORY_RUNTIME )
Signature · 1 line
2.34.1
PATCH v2 7/9 xen/xsm: address violation of MISRA C Rule 16.2
In reply to Nicola VetriniRefactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
---
The second switch is not the best in terms of readability, so it may be best to deviate this particular instance.
--- xen/include/xsm/dummy.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
xen/include/xsm/dummy.h +6 -6
@@ -83,17 +83,17 @@ static always_inline int xsm_default_action(return 0 ;case XSM_TARGET :if (evaluate_nospec (src == target) ) - {return 0 ; + fallthrough;case XSM_XS_PRIV : -if (evaluate_nospec (is_xenstore_domain (src)) ) -return 0 ; - } -/* fall through */ +if ( (action ==XSM_XS_PRIV ) && +evaluate_nospec (is_xenstore_domain (src)) ) +return 0 ; + fallthrough;case XSM_DM_PRIV :if ( target &&evaluate_nospec (src->target == target) )return 0 ; -/* fall through */ + fallthrough;case XSM_PRIV :if (is_control_domain (src) )return 0 ;
Signature · 1 line
2.34.1
PATCH v2 8/9 xen/domain: deviate MISRA C Rule 16.2 violation
In reply to Nicola VetriniMISRA C Rule 16.2 states:
"A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement".
The PROGRESS_VCPU local helper specifies a case that is directly inside the compound statement of a for loop, hence violating the rule. To avoid this, the construct is deviated with a text-based deviation.
No functional change.
---
I chose a text-based deviation, rather than exempting PROGRESS_VCPU, because it's more refined and it's unlikely that more violations are introduced, since the rule has only very few violations left on both ARM and x86.
--- docs/misra/safe.json | 8 ++++++++ xen/common/domain.c | 1 + 2 files changed, 9 insertions(+)
docs/misra/safe.json +8 -0
@@ -36,6 +36,14 @@ }, { "id": "SAF-4-safe", + "analyser": { + "eclair": "MC3R1.R16.2" + }, + "name": "MC3R1.R16.2: using a case label when the most closely-enclosing compound statement is not a switch statement", + "text": "A switch label enclosed by some compound statement that is not the body of a switch is permitted within local helper macros that are unlikely to be misused or misunderstood." + }, + { + "id": "SAF-5-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"
xen/common/domain.c +1 -0
@@ -457,6 +457,7 @@ static int domain_teardown(struct domain *d) for_each_vcpu ( d, v ) { +/* SAF-4-safe MISRA C Rule 16.2: switch label enclosed by for loop*/ PROGRESS_VCPU (teardown); rc =vcpu_teardown (v);
Signature · 1 line
2.34.1
Re: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
In reply to Nicola VetriniQuoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of large switch() statements by adding such blank lines (at least) between non- fall-through case blocks, and you go and remove them?
Jan
Re: [XEN PATCH v2 2/9] x86/cpuid: address violation of MISRA C Rule 16.2
In reply to Nicola VetriniQuoted 9 lines · Nicola Vetrini
Refactor the switch so that a violation of MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo
keyword "fallthrough" is an allowed exception to Rule 16.3.No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
albeit once again with remarks:
Quoted 11 lines
xen/arch/x86/cpuid.c +0
-5@@ -331,23 +331,22 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,switch ( subleaf ) {case 1 : -if ( p->xstate.xsavec || p->xstate.xsaves ) - { -/* -* TODO: Figure out what to do for XSS state. VT-x manages -* host vs guest MSR_XSS automatically, so as soon as we start
xen/arch/x86/cpuid.c void guest_cpuid(const struct vcpu *v, uint32_t leaf, · line 334
Quoted 10 lines
xen/arch/x86/cpuid.c +1
-9-* supporting any XSS states, the wrong XSS will be in -* context. -*/ -BUILD_BUG_ON (XSTATE_XSAVES_ONLY !=0 ); - -/* -* Read CPUID[0xD,0/1].EBX from hardware. They vary with -* enabled XSTATE, and appropraite XCR0|XSS are in context. -*/ +if ( !(p->xstate.xsavec || p->xstate.xsaves) )
Personally I dislike with for of writing such. It may not be overly much of a problem for simple cases like here, but the more complex the expression gets, the less helpful it is that somewhere far away there's an enclosing !(...). I may take the liberty to adjust this, should I end up committing this change.
xen/arch/x86/cpuid.c void guest_cpuid(const struct vcpu *v, uint32_t leaf, · line 341
Quoted 7 lines
xen/arch/x86/cpuid.c +7
-0+break ; +/* +* TODO: Figure out what to do for XSS state. VT-x manages +* host vs guest MSR_XSS automatically, so as soon as we start +* supporting any XSS states, the wrong XSS will be in +* context. +*/
Much like one actually needs to consider re-flowing when increasing indentation of a comment, it is generally desirable to also to so when decreasing indentation, which in this case surely would allow at least "context" to be moved to the earlier line.
Jan
Re: [XEN PATCH v2 3/9] x86/irq: tidy switch statement and address MISRA violation
In reply to Nicola VetriniQuoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
NAK for this part again.
Quoted 12 lines
xen/arch/x86/irq.c +1
-1@@ -2882,7 +2882,7 @@ int allocate_and_map_gsi_pirq(struct domain *d, int index, int *pirq_p)int allocate_and_map_msi_pirq (struct domain *d,int index,int *pirq_p,int type,struct msi_info *msi) { -int irq, pirq, ret; +int irq =-1 , pirq, ret;ASSERT_PDEV_LIST_IS_READ_LOCKED (d);
xen/arch/x86/irq.c int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, · line 2898
Quoted 9 lines
xen/arch/x86/irq.c +2
-2@@ -2892,12 +2892,10 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,if ( !msi->table_base ) msi->entry_nr =1 ; irq = index; -if ( irq ==-1 ) - { + fallthrough;case MAP_PIRQ_TYPE_MULTI_MSI : +if ( type ==MAP_PIRQ_TYPE_MULTI_MSI || irq ==-1 ) irq =create_irq (NUMA_NO_NODE , true);
It may seem small, but this extra comparison already is duplication I'd rather see avoided. At the very least though you want to clarify in the description whether the compiler manages to eliminate it again.
xen/arch/x86/irq.c int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, · line 2899
Quoted 3 lines
xen/arch/x86/irq.c +0
-2- } -if ( irq < nr_irqs_gsi || irq >= nr_irqs )
Why would the blank line above here need removing?
Jan
Re: [XEN PATCH v2 4/9] x86/efi: tidy switch statement and address MISRA violation
In reply to Nicola Vetrinixen/arch/x86/efi/efi-boot.h static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable, · line 174
Quoted 9 lines · Nicola Vetrini
xen/arch/x86/efi/efi-boot.h +3
-0@@ -169,20 +169,22 @@ static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable,switch ( desc->Type ) { +default : + type =E820_RESERVED ; +break ;
This one I guess is tolerable duplication-wise, and I might guess others would even have preferred it like that from the beginning. A blank line below here would be nice, though (and at some point ahead of and between the two ACPI- related case labels blank lines would want adding, too).
However, ...
Quoted 9 lines
xen/arch/x86/efi/efi-boot.h +0
-2case EfiBootServicesCode:case EfiBootServicesData:if ( map_bs ) { -default : type =E820_RESERVED ;break ; } -/* fall through */
xen/arch/x86/efi/efi-boot.h static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable, · line 190
Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2
-1+ fallthrough;case EfiConventionalMemory:if ( !trampoline_phys && desc->PhysicalStart + len <=0x100000 && len >= cfg.size && desc->PhysicalStart + len > cfg.addr ) cfg.addr = (desc->PhysicalStart + len - cfg.size) &PAGE_MASK ; -/* fall through */ + fallthrough;case EfiLoaderCode:case EfiLoaderData:if ( desc->Attribute &EFI_MEMORY_RUNTIME )
... below here there is
else
case EfiUnusableMemory:
type = E820_UNUSABLE;
break;I understand there are no figure braces here, and hence be the letter this isn't an issue with the Misra rule. But (a) some (e.g. Andrew, I guess) would likely argue for there wanting to be braces and (b) we don't want to be leaving this as is, when the spirit of the rule still suggests it should be done differently.
Jan
Re: [XEN PATCH v2 5/9] xen/domctl: address violations of MISRA C Rule 16.2
In reply to Nicola VetriniQuoted 6 lines · Nicola Vetrini
Refactor the first clauses so that a violation of
MISRA C Rule 16.2 is resolved (a switch label should be immediately enclosed in the compound statement of the switch).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
Quoted 6 lines
Convert fallthrough comments in other clauses to the pseudo-keyword while at it.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
preferably ...
xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 299
Quoted 10 lines
xen/common/domctl.c +3
-0@@ -293,6 +293,9 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)switch ( op->cmd ) { +case XEN_DOMCTL_createdomain: + d =NULL ; +break ;case XEN_DOMCTL_assign_device:
... again with a blank line added above this last one (even if this switch() isn't all so big).
Jan
Re: [XEN PATCH v2 6/9] x86/hvm: address violations of MISRA C Rule 16.2
In reply to Nicola Vetrinixen/arch/x86/hvm/hypercall.c int hvm_hypercall(struct cpu_user_regs *regs) · line 122
Quoted 9 lines · Nicola Vetrini
xen/arch/x86/hvm/hypercall.c +1
-2@@ -119,12 +119,12 @@ int hvm_hypercall(struct cpu_user_regs *regs) (mode == 8 ? regs->rdi : regs->ebx) == HVMOP_guest_request_vm_event )break ; -if (unlikely (hvm_get_cpl (curr)) ) - { +if ( !unlikely (hvm_get_cpl (curr)) )
likely() / unlikely() want to be the outermost part of a conditional like this (things are different with && or ||), i.e.
if ( likely(!hvm_get_cpl(curr)) )
Then:
Jan
Re: [XEN PATCH v2 8/9] xen/domain: deviate MISRA C Rule 16.2 violation
In reply to Nicola VetriniQuoted 11 lines · Nicola Vetrini
MISRA C Rule 16.2 states:
"A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement".The
PROGRESS_VCPUlocal helper specifies a case that is directly inside the compound statement of a for loop, hence violating the rule. To avoid this, the construct is deviated with a text-based deviation.No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
Quoted 4 lines
I chose a text-based deviation, rather than exempting
PROGRESS_VCPU, because it's more refined and it's unlikely that more violations are introduced, since the rule has only very few violations left on both ARM and x86.
This same kind of construct is likely to appear in arch-specific domain cleanup code, sooner or later. The same SAF comment can then be used there.
Jan
Re: [XEN PATCH v2 9/9] eclair_analysis: deviate x86 emulator for Rule 16.2
In reply to Nicola VetriniQuoted 11 lines · Nicola Vetrini
MISRA C Rule 16.2 states:
"A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement".Since complying with this rule of the x86 emulator would lead to a lot of code duplication, it is deemed better to exempt those files for this guideline.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
Re: [XEN PATCH v2 7/9] xen/xsm: address violation of MISRA C Rule 16.2
In reply to Nicola VetriniQuoted 8 lines · Nicola Vetrini
Refactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.Signed-off-by Nicola Vetrini <[email protected]>
I had to read this a few times, but it is correct
Quoted 38 lines
---
The second switch is not the best in terms of readability, so it may be best to deviate this particular instance.--- xen/include/xsm/dummy.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)xen/include/xsm/dummy.h +6
-6@@ -83,17 +83,17 @@ static always_inline int xsm_default_action(return 0 ;case XSM_TARGET :if (evaluate_nospec (src == target) ) - {return 0 ; + fallthrough;case XSM_XS_PRIV : -if (evaluate_nospec (is_xenstore_domain (src)) ) -return 0 ; - } -/* fall through */ +if ( (action ==XSM_XS_PRIV ) && +evaluate_nospec (is_xenstore_domain (src)) ) +return 0 ; + fallthrough;case XSM_DM_PRIV :if ( target &&evaluate_nospec (src->target == target) )return 0 ; -/* fall through */ + fallthrough;case XSM_PRIV :if (is_control_domain (src) )return 0 ;Signature · 1 line
2.34.1
Stefano Stabellini
Reviewed-byRe: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
In reply to Jan BeulichQuoted 6 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of large
switch()statements by adding such blank lines (at least) between non- fall-through case blocks, and you go and remove them?
To be fair it is almost impossible to figure out the correct coding style in Xen by looking at existing code and/or CODING_STYLE.
Except for the blank lines, the change itself looks correct to me
Re: [XEN PATCH v2 3/9] x86/irq: tidy switch statement and address MISRA violation
In reply to Jan BeulichQuoted 16 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
NAK for this part again.
Quoted 11 lines
xen/arch/x86/irq.c +1
-1@@ -2882,7 +2882,7 @@ int allocate_and_map_gsi_pirq(struct domain *d, int index, int *pirq_p)int allocate_and_map_msi_pirq (struct domain *d,int index,int *pirq_p,int type,struct msi_info *msi) { -int irq, pirq, ret; +int irq =-1 , pirq, ret;ASSERT_PDEV_LIST_IS_READ_LOCKED (d);
Quoted 14 lines
xen/arch/x86/irq.c int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, · line 2898
Quoted 10 lines
xen/arch/x86/irq.c +2
-2@@ -2892,12 +2892,10 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,if ( !msi->table_base ) msi->entry_nr =1 ; irq = index; -if ( irq ==-1 ) - { + fallthrough;case MAP_PIRQ_TYPE_MULTI_MSI : +if ( type ==MAP_PIRQ_TYPE_MULTI_MSI || irq ==-1 ) irq =create_irq (NUMA_NO_NODE , true);It may seem small, but this extra comparison already is duplication I'd rather see avoided. At the very least though you want to clarify in the description whether the compiler manages to eliminate it again.
It could just be:
if ( irq == -1 )
because in the MAP_PIRQ_TYPE_MULTI_MSI case we know the irq will be -1. No duplication needed.
Re: [XEN PATCH v2 4/9] x86/efi: tidy switch statement and address MISRA violation
In reply to Jan BeulichQuoted 45 lines · Jan Beulich
xen/arch/x86/efi/efi-boot.h static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable, · line 174
Quoted 9 lines · Nicola Vetrini
xen/arch/x86/efi/efi-boot.h +3
-0@@ -169,20 +169,22 @@ static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable,switch ( desc->Type ) { +default : + type =E820_RESERVED ; +break ;This one I guess is tolerable duplication-wise, and I might guess others would even have preferred it like that from the beginning. A blank line below here would be nice, though (and at some point ahead of and between the two ACPI- related case labels blank lines would want adding, too).
However, ...
Quoted 9 lines
xen/arch/x86/efi/efi-boot.h +0
-2case EfiBootServicesCode:case EfiBootServicesData:if ( map_bs ) { -default : type =E820_RESERVED ;break ; } -/* fall through */ xen/arch/x86/efi/efi-boot.h static void __init efi_arch_process_memory_map(EFI_SYSTEM_TABLE *SystemTable, · line 190
Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2
-1+ fallthrough;case EfiConventionalMemory:if ( !trampoline_phys && desc->PhysicalStart + len <=0x100000 && len >= cfg.size && desc->PhysicalStart + len > cfg.addr ) cfg.addr = (desc->PhysicalStart + len - cfg.size) &PAGE_MASK ; -/* fall through */ + fallthrough;case EfiLoaderCode:case EfiLoaderData:if ( desc->Attribute &EFI_MEMORY_RUNTIME )... below here there is
else case EfiUnusableMemory: type = E820_UNUSABLE; break;
Quoted 5 lines
I understand there are no figure braces here, and hence be the letter this isn't an issue with the Misra rule. But (a) some (e.g. Andrew, I guess) would likely argue for there wanting to be braces and (b) we don't want to be leaving this as is, when the spirit of the rule still suggests it should be done differently.
I agree. For clarity I would go with the following because it is easier to read:
case EfiLoaderData:
if ( desc->Attribute & EFI_MEMORY_RUNTIME )
type = E820_RESERVED;
else if ( desc->Attribute & EFI_MEMORY_WB )
type = E820_RAM;
else
type = E820_UNUSABLE;
break;
case EfiUnusableMemory:
type = E820_UNUSABLE;
break;Re: [XEN PATCH v2 3/9] x86/irq: tidy switch statement and address MISRA violation
In reply to Stefano StabelliniQuoted 32 lines · Stefano Stabellini
Quoted 16 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
NAK for this part again.
Quoted 11 lines
xen/arch/x86/irq.c +1
-1@@ -2882,7 +2882,7 @@ int allocate_and_map_gsi_pirq(struct domain *d, int index, int *pirq_p)int allocate_and_map_msi_pirq (struct domain *d,int index,int *pirq_p,int type,struct msi_info *msi) { -int irq, pirq, ret; +int irq =-1 , pirq, ret;ASSERT_PDEV_LIST_IS_READ_LOCKED (d);Quoted 14 lines
xen/arch/x86/irq.c int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, · line 2898
Quoted 10 lines
xen/arch/x86/irq.c +2
-2@@ -2892,12 +2892,10 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,if ( !msi->table_base ) msi->entry_nr =1 ; irq = index; -if ( irq ==-1 ) - { + fallthrough;case MAP_PIRQ_TYPE_MULTI_MSI : +if ( type ==MAP_PIRQ_TYPE_MULTI_MSI || irq ==-1 ) irq =create_irq (NUMA_NO_NODE , true);It may seem small, but this extra comparison already is duplication I'd rather see avoided. At the very least though you want to clarify in the description whether the compiler manages to eliminate it again.
Quoted 6 lines
It could just be:
if ( irq == -1 )because in the
MAP_PIRQ_TYPE_MULTI_MSIcase we know the irq will be -1. No duplication needed.
Ok then. Didn't know whether I could avoid the check to get this structuring.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH v2 7/9] xen/xsm: address violation of MISRA C Rule 16.2
In reply to Nicola VetriniQuoted 7 lines · Nicola Vetrini
Refactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
To give you a quick response, on cursory review, I do not believe the two are equivalent. I have never been a fan of the Duff's device in use here, as it makes reasoning about all the variations difficult. I unrolled all of this once before, and I recall it being a bit more intricate than this.
v/r,
dps
Re: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
In reply to Jan BeulichQuoted 8 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of large
switch()statements by adding such blank lines (at least) between non- fall-through case blocks, and you go and remove them?Jan
I wrote that based on this earlier suggestion [1]. If I misunderstood the suggestion, then I apologize and feel free to strip them if you want.
[1] https://lore.kernel.org/xen-devel/[email protected]/
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH v2 2/9] x86/cpuid: address violation of MISRA C Rule 16.2
In reply to Jan BeulichQuoted 38 lines · Jan Beulich
Quoted 9 lines · Nicola Vetrini
Refactor the switch so that a violation of MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo
keyword "fallthrough" is an allowed exception to Rule 16.3.No functional change.
Signed-off-by Nicola Vetrini <[email protected]>Acked-by Jan Beulich <[email protected]>albeit once again with remarks:
Quoted 24 lines
xen/arch/x86/cpuid.c +0
-4@@ -331,23 +331,22 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf, switch ( subleaf ) {case 1 : -if ( p->xstate.xsavec || p->xstate.xsaves ) - { -/* -* TODO: Figure out what to do for XSS state. VT-x manages
- * host vs guestMSR_XSSautomatically, so as soon as
we start
- * supporting any XSS states, the wrong XSS will be
inxen/arch/x86/cpuid.c +0
-6-* context. -*/ -BUILD_BUG_ON (XSTATE_XSAVES_ONLY !=0 ); - -/* -* Read CPUID[0xD,0/1].EBX from hardware. They vary with
- * enabled XSTATE, and appropraite XCR0|XSS are in
Quoted 9 lines
xen/arch/x86/cpuid.c
Quoted 3 lines
context.
xen/arch/x86/cpuid.c +1
-1-*/ +if ( !(p->xstate.xsavec || p->xstate.xsaves) )Personally I dislike with for of writing such. It may not be overly much of a problem for simple cases like here, but the more complex the expression gets, the less helpful it is that somewhere far away there's an enclosing !(...). I may take the liberty to adjust this, should I end up committing this change.
Ok, makes sense. I didn't think about that style aspect.
Quoted 16 lines
xen/arch/x86/cpuid.c void guest_cpuid(const struct vcpu *v, uint32_t leaf, · line 341
Quoted 9 lines
xen/arch/x86/cpuid.c +3
-0+break ; +/* +* TODO: Figure out what to do for XSS state. VT-x manages
+ * host vs guestMSR_XSSautomatically, so as soon as we
startxen/arch/x86/cpuid.c +3
-0+* supporting any XSS states, the wrong XSS will be in +* context. +*/ Much like one actually needs to consider re-flowing when increasing indentation of a comment, it is generally desirable to also to so when decreasing indentation, which in this case surely would allow at least "context" to be moved to the earlier line.
Jan
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH v2 6/9] x86/hvm: address violations of MISRA C Rule 16.2
In reply to Jan BeulichQuoted 8 lines · Jan Beulich
Quoted 7 lines · Nicola Vetrini
xen/arch/x86/hvm/hypercall.c +0
-0@@ -119,12 +119,12 @@ int hvm_hypercall(struct cpu_user_regs *regs) (mode ==8 ? regs->rdi : regs->ebx) == HVMOP_guest_request_vm_event ) break;
Quoted 9 lines
xen/arch/x86/hvm/hypercall.c int hvm_hypercall(struct cpu_user_regs *regs) · line 122
Quoted 3 lines
xen/arch/x86/hvm/hypercall.c +1
-2-if (unlikely (hvm_get_cpl (curr)) ) - { +if ( !unlikely (hvm_get_cpl (curr)) )
likely()/unlikely()want to be the outermost part of a conditional like this (things are different with && or ||), i.e.if ( likely(!hvm_get_cpl(curr)) )
Ok, LGTM
Quoted 4 lines
Then:
Acked-by Jan Beulich <[email protected]>Jan
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH v2 7/9] xen/xsm: address violation of MISRA C Rule 16.2
In reply to Daniel P. SmithQuoted 9 lines · Daniel P. Smith
Quoted 7 lines · Nicola Vetrini
Refactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.
Quoted 4 lines
To give you a quick response, on cursory review, I do not believe the two are equivalent. I have never been a fan of the Duff's device in use here, as it makes reasoning about all the variations difficult. I unrolled all of this once before, and I recall it being a bit more intricate than this.
v/r,
dps
Hi,
basically what I was trying to accomplish is this: XSM_TARGET will either enter the if and return, or fallthrough to XSM_XS_PRIV, but then it won't enter the if (as it did before because in order to reach this second case's if statement it had to also satisfy the first condition (evaluate_nospec(src == target)), which would then cause the XSM_TARGET phase to return 0). And then if either the action != XSM_XS_PRIV or the other condition is not satisfied we go down to XSM_DM_PRIV, just like before. I may have made a logical mistake somewhere, of course, but it seems the same thing semantically, by relying on the fact that
case XSM_TARGET:
if (...) {
return 0;
case XSM_XS_PRIV:
...
}
can be rewritten as
case XSM_TARGET:
if (...) { return 0; }
fallthrough;
case XSM_XS_PRIV:
if (action == XSM_XS_PRIV && ...) { ... }
case XSM_DM_PRIV:
because there wasn't other code after the closed bracket of the if in the XSM_XS_PRIV case (only a fallthough that remained as is).
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Daniel P. Smith
Acked-byRe: [XEN PATCH v2 7/9] xen/xsm: address violation of MISRA C Rule 16.2
Quoted 28 lines · Nicola Vetrini
Quoted 9 lines · Daniel P. Smith
Quoted 7 lines · Nicola Vetrini
Refactor the switch so that a violation of
MISRA C Rule 16.2 is resolved (A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement).
Note that the switch clause ending with the pseudo keyword "fallthrough" is an allowed exception to Rule 16.3.Quoted 4 lines
To give you a quick response, on cursory review, I do not believe the two are equivalent. I have never been a fan of the Duff's device in use here, as it makes reasoning about all the variations difficult. I unrolled all of this once before, and I recall it being a bit more intricate than this.
v/r,
dpsHi,
basically what I was trying to accomplish is this:
XSM_TARGETwill either enter the if and return, or fallthrough toXSM_XS_PRIV, but then it won't enter the if (as it did before because in order to reach this second case's if statement it had to also satisfy the first condition (evaluate_nospec(src == target)), which would then cause theXSM_TARGETphase to return 0). And then if either the action !=XSM_XS_PRIVor the other condition is not satisfied we go down toXSM_DM_PRIV, just like before. I may have made a logical mistake somewhere, of course, but it seems the same thing semantically, by relying on the fact thatcase
XSM_TARGET:
if (...) {
return 0;
caseXSM_XS_PRIV:
...
}can be rewritten as
Quoted 8 lines
case
XSM_TARGET:
if (...) { return 0; }
fallthrough;
caseXSM_XS_PRIV:
if (action ==XSM_XS_PRIV&& ...) { ... }
caseXSM_DM_PRIV:because there wasn't other code after the closed bracket of the if in the
XSM_XS_PRIVcase (only a fallthough that remained as is).
Yes, upon a closer look, you are correct. I just did a quick read earlier since this has been sitting for a few days. I missed that you did in fact have it falling all the way down through all the subsequent checks for each case "entry point" into the privilege hierarchy. Honestly, I probably should look into a comment to document the hierarchy of the checks that is buried in there.
Re: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
In reply to Nicola VetriniQuoted 10 lines · Nicola Vetrini
Quoted 9 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of
largeswitch()statements by adding such blank lines (at least) between non-
fall-through case blocks, and you go and remove them?
Quoted 7 lines
Quoted 1 line
Jan
I wrote that based on this earlier suggestion [1]. If I misunderstood the suggestion, then I apologize and feel free to strip them if you want.
I'm afraid I also can't figure out what that suggestion was supposed to be, but we definitely do want to keep blank lines. They're specifically for improved legibility.
But fighting over spacing like this is a waste of everyone's time. I've taken patches 1 thru 7, accounting for the suggestions made so far, and adjusted to retain the blank lines.
Please double check carefully.
Patch 8 didn't apply because SAF-4-safe has been used for something else now. You'll need to rebase and resubmit patches 8 and 9.
~Andrew
Re: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Nicola Vetrini
Quoted 10 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of
largeswitch()statements by adding such blank lines (at least) between non-
fall-through case blocks, and you go and remove them?Jan
I wrote that based on this earlier suggestion [1]. If I misunderstood the suggestion, then I apologize and feel free to strip them if you want.
Quoted 7 lines
Quoted 2 lines
I'm afraid I also can't figure out what that suggestion was supposed to be, but we definitely do want to keep blank lines. They're specifically for improved legibility.
I interpreted that message as being a suggestion to eliminate blank lines, which was obviously incorrect. Anyways, thanks for the effort on adjusting and committing the earlier patches.
Quoted 9 lines
But fighting over spacing like this is a waste of everyone's time. I've taken patches 1 thru 7, accounting for the suggestions made so far, and adjusted to retain the blank lines.
Please double check carefully.
Patch 8 didn't apply because SAF-4-safe has been used for something else now. You'll need to rebase and resubmit patches 8 and 9.
I'll certainly do so when I'm fully back to work next week.
Quoted 1 line
~Andrew
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH v2 1/9] x86/vlapic: tidy switch statement and address MISRA violation
In reply to Andrew CooperQuoted 13 lines · Andrew Cooper
Quoted 12 lines · Nicola Vetrini
Quoted 10 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
Remove unneded blank lines between switch clauses.
"Unneeded" based on what? We're carefully trying to improve readability of
largeswitch()statements by adding such blank lines (at least) between non-
fall-through case blocks, and you go and remove them?Jan
Quoted 8 lines
Quoted 5 lines
I wrote that based on this earlier suggestion [1]. If I misunderstood the suggestion, then I apologize and feel free to strip them if you want.
I'm afraid I also can't figure out what that suggestion was supposed to be,
The main point of missing clarity there is that we still need to settle on whether blank lines should also be between blocks where the earlier falls through to the latter. Iirc you'd like to have blank lines in such cases nevertheless, while I'd prefer to make the falling-through visually easy to recognize by not putting blanks lines between the "fallthrough;" / fall-through comment and the successive "case ...":.
Jan
Quoted 2 lines
but we definitely do want to keep blank lines. They're specifically for improved legibility.