KVM: x86: Clean up redundant macro definitions
cover KVM: x86: Clean up redundant macro definitions
In KVM/x86 code, there are macros with the same name that are defined and used separately in the evolving code, and if the scope of the code review is only on iterations of the patch set, it can be difficult to spot these fragmented macros being defined repeatedly.
IMO, it's necessary to clean this up to improve the consistency and readability of the code, and it also helps to avoid software defects caused by inconsistencies in the scope of influence of macros.
Like Xu (5):
KVM: x86: Clean up redundant mod_64(x, y) macro definition
KVM: x86: Clean up redundant CC macro definition
KVM: x86: Clean up redundant ROL16(val, n) macro definition
KVM: x86: Clean up redundant __ex(x) macro definition
KVM: x86: Clean up redundant pr_fmt(fmt) macro definition for svm
arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/i8254.c | 6 ------ arch/x86/kvm/lapic.c | 6 ------ arch/x86/kvm/svm/avic.c | 2 -- arch/x86/kvm/svm/nested.c | 4 ---- arch/x86/kvm/svm/sev.c | 2 -- arch/x86/kvm/svm/svm.c | 4 ---- arch/x86/kvm/svm/svm.h | 3 +++ arch/x86/kvm/vmx/evmcs.c | 1 - arch/x86/kvm/vmx/evmcs.h | 4 ---- arch/x86/kvm/vmx/nested.c | 2 -- arch/x86/kvm/vmx/vmcs.h | 2 ++ arch/x86/kvm/vmx/vmcs12.c | 1 - arch/x86/kvm/vmx/vmcs12.h | 4 ---- arch/x86/kvm/vmx/vmx_ops.h | 2 -- arch/x86/kvm/x86.h | 8 ++++++++ 16 files changed, 15 insertions(+), 38 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/5 KVM: x86: Clean up redundant mod_64(x, y) macro definition
From: Like Xu <[email protected]>
The mod_64(x, y) macro is only defined and used in the kvm/x86 context. It's safe to move the definition of mod_64(x, y) from x86/{i8254lapic}.c to the generic x86.h without any intended functional change.
--- arch/x86/kvm/i8254.c | 6 ------ arch/x86/kvm/lapic.c | 6 ------ arch/x86/kvm/x86.h | 6 ++++++ 3 files changed, 6 insertions(+), 12 deletions(-)
arch/x86/kvm/i8254.c +0 -6
@@ -40,12 +40,6 @@#include "i8254.h" #include "x86.h" -#ifndef CONFIG_X86_64 -#define mod_64 (x, y) ((x) - (y) * div64_u64(x, y)) -#else -#define mod_64 (x, y) ((x) % (y)) -#endif -#define RW_STATE_LSB 1#define RW_STATE_MSB 2#define RW_STATE_WORD0 3
arch/x86/kvm/lapic.c +0 -6
@@ -42,12 +42,6 @@#include "cpuid.h" #include "hyperv.h" -#ifndef CONFIG_X86_64 -#define mod_64 (x, y) ((x) - (y) * div64_u64(x, y)) -#else -#define mod_64 (x, y) ((x) % (y)) -#endif -#define PRId64 "d"#define PRIx64 "llx"#define PRIu64 "u"
arch/x86/kvm/x86.h +6 -0
@@ -8,6 +8,12 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" +#ifndef CONFIG_X86_64 +#define mod_64 (x, y) ((x) - (y) * div64_u64(x, y)) +#else +#define mod_64 (x, y) ((x) % (y)) +#endif + static __always_inline void kvm_guest_enter_irqoff(void) {/*
Signature · 1 line
2.32.0
PATCH 2/5 KVM: x86: Clean up redundant CC macro definition
In reply to Like XuFrom: Like Xu <[email protected]>
With the exception of drivers/dma/pl330.c, the CC macro is defined and used in {svm, vmx}/nested.c, and the KVM_NESTED_VMENTER_CONSISTENCY_CHECK macro it refers to is defined in x86.h, so it's safe to move it into x86.h without intended functional changes.
--- arch/x86/kvm/svm/nested.c | 2 -- arch/x86/kvm/vmx/nested.c | 2 -- arch/x86/kvm/x86.h | 2 ++ 3 files changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +0 -2
@@ -29,8 +29,6 @@#include "lapic.h" #include "svm.h" -#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK -static void nested_svm_inject_npf_exit (struct kvm_vcpu *vcpu,struct x86_exception *fault) {
arch/x86/kvm/vmx/nested.c +0 -2
@@ -22,8 +22,6 @@ module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);static bool __read_mostly nested_early_check =0 ;module_param (nested_early_check, bool,S_IRUGO ); -#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK -/* * Hyper-V requires all of these, so mark them as supported even though * they are just treated the same as all-context.
arch/x86/kvm/x86.h +2 -0
@@ -67,6 +67,8 @@ static __always_inline void kvm_guest_exit_irqoff(void) failed; \ }) +#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK +#define KVM_DEFAULT_PLE_GAP 128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096#define KVM_DEFAULT_PLE_WINDOW_GROW 2
Signature · 1 line
2.32.0
Paolo Bonzini
maintainerRe: [PATCH 2/5] KVM: x86: Clean up redundant CC macro definition
Quoted 8 lines · Like Xu
From: Like Xu <[email protected]>
With the exception of
drivers/dma/pl330.c, the CC macro is defined and used in {svm,vmx}/nested.c, and theKVM_NESTED_VMENTER_CONSISTENCY_CHECKmacro it refers to is defined inx86.h, so it's safe to move it intox86.hwithout intended functional changes.Signed-off-by Like Xu <[email protected]>
This one is just a shortcut that should not available outside nested.c, so I am not applying it.
Paolo
Quoted 46 lines
--- arch/x86/kvm/svm/nested.c | 2 -- arch/x86/kvm/vmx/nested.c | 2 -- arch/x86/kvm/x86.h | 2 ++ 3 files changed, 2 insertions(+), 4 deletions(-)arch/x86/kvm/svm/nested.c +0
-2@@ -29,8 +29,6 @@#include "lapic.h" #include "svm.h" -#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK -static void nested_svm_inject_npf_exit (struct kvm_vcpu *vcpu,struct x86_exception *fault) {arch/x86/kvm/vmx/nested.c +0
-2@@ -22,8 +22,6 @@ module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);static bool __read_mostly nested_early_check =0 ;module_param (nested_early_check, bool,S_IRUGO ); -#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK -/* * Hyper-V requires all of these, so mark them as supported even though * they are just treated the same as all-context. arch/x86/kvm/x86.h +2
-0@@ -67,6 +67,8 @@ static __always_inline void kvm_guest_exit_irqoff(void) failed; \ }) +#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK +#define KVM_DEFAULT_PLE_GAP 128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096#define KVM_DEFAULT_PLE_WINDOW_GROW 2
PATCH 3/5 KVM: x86: Clean up redundant ROL16(val, n) macro definition
In reply to Like XuFrom: Like Xu <[email protected]>
The ROL16(val, n) macro is repeatedly defined in several vmcs-related files, and it has never been used outside the KVM context.
Let's move it to vmcs.h without any intended functional changes.
--- arch/x86/kvm/vmx/evmcs.c | 1 - arch/x86/kvm/vmx/evmcs.h | 4 ---- arch/x86/kvm/vmx/vmcs.h | 2 ++ arch/x86/kvm/vmx/vmcs12.c | 1 - arch/x86/kvm/vmx/vmcs12.h | 4 ---- 5 files changed, 2 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx/evmcs.c +0 -1
@@ -14,7 +14,6 @@ DEFINE_STATIC_KEY_FALSE(enable_evmcs);#if IS_ENABLED (CONFIG_HYPERV ) -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))#define EVMCS1_OFFSET (x) offsetof(struct hv_enlightened_vmcs, x)#define EVMCS1_FIELD (number, name, clean_field)[ROL16(number, 6)] = \ {EVMCS1_OFFSET(name), clean_field}
arch/x86/kvm/vmx/evmcs.h +0 -4
@@ -73,8 +73,6 @@ struct evmcs_field {extern const struct evmcs_field vmcs_field_to_evmcs_1[];extern const unsigned int nr_evmcs_1_fields; -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) - static __always_inline int get_evmcs_offset(unsigned long field,u16 *clean_field) {
@@ -95,8 +93,6 @@ static __always_inline int get_evmcs_offset(unsigned long field,return evmcs_field->offset; } -#undef ROL16 -static inline void evmcs_write64 (unsigned long field,u64 value) {u16 clean_field;
arch/x86/kvm/vmx/vmcs.h +2 -0
@@ -11,6 +11,8 @@#include "capabilities.h" +#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) +struct vmcs_hdr {u32 revision_id:31 ;u32 shadow_vmcs:1 ;
arch/x86/kvm/vmx/vmcs12.c +0 -1
@@ -2,7 +2,6 @@#include "vmcs12.h" -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))#define VMCS12_OFFSET (x) offsetof(struct vmcs12, x)#define FIELD (number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)#define FIELD64 (number, name) \
arch/x86/kvm/vmx/vmcs12.h +0 -4
@@ -364,8 +364,6 @@ static inline void vmx_check_vmcs12_offsets(void)extern const unsigned short vmcs_field_to_offset_table[];extern const unsigned int nr_vmcs12_fields; -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) -static inline short vmcs_field_to_offset (unsigned long field) {unsigned short offset;
@@ -385,8 +383,6 @@ static inline short vmcs_field_to_offset(unsigned long field)return offset; } -#undef ROL16 -static inline u64 vmcs12_read_any (struct vmcs12 *vmcs12,unsigned long field,u16 offset) {
Signature · 1 line
2.32.0
Re: [PATCH 3/5] KVM: x86: Clean up redundant ROL16(val, n) macro definition
Quoted 90 lines · Like Xu
From: Like Xu <[email protected]>
The
ROL16(val, n)macro is repeatedly defined in several vmcs-related files, and it has never been used outside the KVM context.Let's move it to
vmcs.hwithout any intended functional changes.Signed-off-by Like Xu <[email protected]>--- arch/x86/kvm/vmx/evmcs.c | 1 - arch/x86/kvm/vmx/evmcs.h | 4 ---- arch/x86/kvm/vmx/vmcs.h | 2 ++ arch/x86/kvm/vmx/vmcs12.c | 1 - arch/x86/kvm/vmx/vmcs12.h | 4 ---- 5 files changed, 2 insertions(+), 10 deletions(-)arch/x86/kvm/vmx/evmcs.c +0
-1@@ -14,7 +14,6 @@ DEFINE_STATIC_KEY_FALSE(enable_evmcs);#if IS_ENABLED (CONFIG_HYPERV ) -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))#define EVMCS1_OFFSET (x) offsetof(struct hv_enlightened_vmcs, x)#define EVMCS1_FIELD (number, name, clean_field)[ROL16(number, 6)] = \ {EVMCS1_OFFSET(name), clean_field}arch/x86/kvm/vmx/evmcs.h +0
-4@@ -73,8 +73,6 @@ struct evmcs_field {extern const struct evmcs_field vmcs_field_to_evmcs_1[];extern const unsigned int nr_evmcs_1_fields; -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) - static __always_inline int get_evmcs_offset(unsigned long field,u16 *clean_field) {@@ -95,8 +93,6 @@ static __always_inline int get_evmcs_offset(unsigned long field,return evmcs_field->offset; } -#undef ROL16 -static inline void evmcs_write64 (unsigned long field,u64 value) {u16 clean_field;arch/x86/kvm/vmx/vmcs.h +2
-0@@ -11,6 +11,8 @@#include "capabilities.h" +#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) +struct vmcs_hdr {u32 revision_id:31 ;u32 shadow_vmcs:1 ;arch/x86/kvm/vmx/vmcs12.c +0
-1@@ -2,7 +2,6 @@#include "vmcs12.h" -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))#define VMCS12_OFFSET (x) offsetof(struct vmcs12, x)#define FIELD (number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)#define FIELD64 (number, name) \arch/x86/kvm/vmx/vmcs12.h +0
-2@@ -364,8 +364,6 @@ static inline void vmx_check_vmcs12_offsets(void)extern const unsigned short vmcs_field_to_offset_table[];extern const unsigned int nr_vmcs12_fields; -#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) -static inline short vmcs_field_to_offset (unsigned long field) {unsigned short offset;
arch/x86/kvm/vmx/vmcs12.h static inline short vmcs_field_to_offset(unsigned long field) · line 388
Quoted 8 lines
arch/x86/kvm/vmx/vmcs12.h +0
-2@@ -385,8 +383,6 @@ static inline short vmcs_field_to_offset(unsigned long field)return offset; } -#undef ROL16 -static inline u64 vmcs12_read_any (struct vmcs12 *vmcs12,unsigned long field,u16 offset) {
Queued, thanks.
Paolo
PATCH 4/5 KVM: x86: Clean up redundant __ex(x) macro definition
In reply to Like XuFrom: Like Xu <[email protected]>
The __ex(x) macro is repeatedly defined in sev.c, svm.c and vmx_ops.h, and it has never been used outside the KVM/x86 context.
Let's move it to kvm_host.h without any intended functional changes.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm/sev.c | 2 -- arch/x86/kvm/svm/svm.c | 2 -- arch/x86/kvm/vmx/vmx_ops.h | 2 -- 4 files changed, 2 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1823,6 +1823,8 @@ asmlinkage void kvm_spurious_fault(void);"668: \n\t" \ _ASM_EXTABLE(666b, 667b) +#define __ex (x) __kvm_handle_fault_on_reboot(x) +#define KVM_ARCH_WANT_MMU_NOTIFIER int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v);
arch/x86/kvm/svm/sev.c +0 -2
@@ -28,8 +28,6 @@#include "cpuid.h" #include "trace.h" -#define __ex (x) __kvm_handle_fault_on_reboot(x) -#ifndef CONFIG_KVM_AMD_SEV /* * When this config is not defined, SEV feature is not supported and APIs in
arch/x86/kvm/svm/svm.c +0 -2
@@ -46,8 +46,6 @@#include "kvm_onhyperv.h" #include "svm_onhyperv.h" -#define __ex (x) __kvm_handle_fault_on_reboot(x) -MODULE_AUTHOR ("Qumranet" );MODULE_LICENSE ("GPL" );
arch/x86/kvm/vmx/vmx_ops.h +0 -2
@@ -10,8 +10,6 @@#include "evmcs.h" #include "vmcs.h" -#define __ex (x) __kvm_handle_fault_on_reboot(x) - asmlinkage void vmread_error(unsigned long field, bool fault); __attribute__((regparm (0 )))void vmread_error_trampoline (unsigned long field,bool fault);
Signature · 1 line
2.32.0
PATCH 5/5 KVM: x86: Clean up redundant pr_fmt(fmt) macro definition for svm
In reply to Like XuFrom: Like Xu <[email protected]>
The svm specific pr_fmt(fmt) macro is repeatedly defined in svm code and the new one has never been used outside the svm context.
Let's move it to svm.h without any intended functional changes.
--- arch/x86/kvm/svm/avic.c | 2 -- arch/x86/kvm/svm/nested.c | 2 -- arch/x86/kvm/svm/svm.c | 2 -- arch/x86/kvm/svm/svm.h | 3 +++ 4 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/avic.c +0 -2
@@ -12,8 +12,6 @@* Avi Kivity <[email protected]> */ -#define pr_fmt (fmt) "SVM: " fmt -#include <linux/kvm_types.h> #include <linux/hashtable.h> #include <linux/amd-iommu.h>
arch/x86/kvm/svm/nested.c +0 -2
@@ -12,8 +12,6 @@* Avi Kivity <[email protected]> */ -#define pr_fmt (fmt) "SVM: " fmt -#include <linux/kvm_types.h> #include <linux/kvm_host.h> #include <linux/kernel.h>
arch/x86/kvm/svm/svm.c +0 -2
@@ -1,5 +1,3 @@ -#define pr_fmt (fmt) "SVM: " fmt -#include <linux/kvm_host.h> #include "irq.h"
arch/x86/kvm/svm/svm.h +3 -0
@@ -15,6 +15,9 @@#ifndef __SVM_SVM_H#define __SVM_SVM_H +#undef pr_fmt +#define pr_fmt (fmt) "SVM: " fmt +#include <linux/kvm_types.h> #include <linux/kvm_host.h> #include <linux/bits.h>
Signature · 1 line
2.32.0
Paolo Bonzini
maintainerRe: [PATCH 5/5] KVM: x86: Clean up redundant pr_fmt(fmt) macro definition for svm
arch/x86/kvm/svm/svm.h line 21
Quoted 4 lines · Like Xu
arch/x86/kvm/svm/svm.h +3
-0+#undef pr_fmt +#define pr_fmt (fmt) "SVM: " fmt +#include <linux/kvm_types.h>
Why do you need the #undef?
Paolo
Re: [PATCH 5/5] KVM: x86: Clean up redundant pr_fmt(fmt) macro definition for svm
Quoted 11 lines · Paolo Bonzini
Quoted 4 lines · Like Xu
arch/x86/kvm/svm/svm.h +3
-0+#undef pr_fmt +#define pr_fmt (fmt) "SVM: " fmt +#include <
linux/kvm_types.h>Why do you need the #undef?
Paolo
I've seen most of the redefinition code for 'pr_fmt' like this, for example:
- 3bfaf95cb1fe81872df884956c704469e68a5bee
- d157aa0fb241646e8818f699653ed983e6581b11
Quoted 6 lines · Like Xu
In KVM/x86 code, there are macros with the same name that are defined and used separately in the evolving code, and if the scope of the code review is only on iterations of the patch set, it can be difficult to spot these fragmented macros being defined repeatedly.
IMO, it's necessary to clean this up to improve the consistency and
Please use more specific shortlogs. "Clean up" is too ambiguous, e.g. I would expect it to mean "clean up the macro itself".
Quoted 6 lines
readability of the code, and it also helps to avoid software defects caused by inconsistencies in the scope of influence of macros.
Like Xu (5):
KVM: x86: Clean up redundant
mod_64(x, y)macro definition
Feels like we should find a home outside of KVM for mod_64(), it's a full generic and straightforward macro.
Quoted 1 line
KVM: x86: Clean up redundant CC macro definition
CC() should be kept where it is. It should never be used outside of nested code and is far too generic of a name to be exposed to the world at large. It was deliberately defined only in the nested.c files.
Quoted 1 line
KVM: x86: Clean up redundant
ROL16(val, n)macro definition
Moving ROL16 seems ok, though it scares me a bit that someone went through the trouble of #undef'ing the macros.
Quoted 1 line
KVM: x86: Clean up redundant
__ex(x)macro definition
__ex() and __kvm_handle_fault_on_reboot() were supposed to have been removed. The last two patches [8][9] of this series[*] got lost; I'll repost them as they no longer apply cleanly.
Quoted 1 line
KVM: x86: Clean up redundant
pr_fmt(fmt)macro definition for svm
NAK, this breaks sev.c's formatting. Arguably, nested.c and avic.c should use more specific print messages, though that gets tricky with nested code as it's not wholly contained in nested.c.