2022-11-30 12:36:20

by liujing

[permalink] [raw]
Subject: [PATCH] KVM: x86: Redundant variable assignments can be merged

When reading kvm code, find the 'r' variable declaration
and then assign the value in kvm_vm_ioctl_get_irqchip and
kvm_vm_ioctl_set_irqchip function, It can be combined into one sentence.

Signed-off-by: liujing <[email protected]>
---
arch/x86/kvm/x86.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2835bd796639..4be353d5c09d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6002,9 +6002,8 @@ static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
{
struct kvm_pic *pic = kvm->arch.vpic;
- int r;
+ int r = 0;

- r = 0;
switch (chip->chip_id) {
case KVM_IRQCHIP_PIC_MASTER:
memcpy(&chip->chip.pic, &pic->pics[0],
@@ -6027,9 +6026,8 @@ static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
{
struct kvm_pic *pic = kvm->arch.vpic;
- int r;
+ int r = 0;

- r = 0;
switch (chip->chip_id) {
case KVM_IRQCHIP_PIC_MASTER:
spin_lock(&pic->lock);
--
2.18.2




2022-11-30 16:53:52

by Sean Christopherson

[permalink] [raw]
Subject: Re: [PATCH] KVM: x86: Redundant variable assignments can be merged

State what the patch does, not what can be done. And "redundant" isn't the right
word to describe this.

On Wed, Nov 30, 2022, liujing wrote:
> When reading kvm code, find the 'r' variable declaration
> and then assign the value in kvm_vm_ioctl_get_irqchip and
> kvm_vm_ioctl_set_irqchip function, It can be combined into one sentence.

Why though? I actually kinda like the existing code.

That said, what about removing the variable entirely? That'd also avoid the
unnecessary call to kvm_pic_update_irq() in set's error path.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d2ad383da998..898fce19430a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6011,9 +6011,7 @@ static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
{
struct kvm_pic *pic = kvm->arch.vpic;
- int r;

- r = 0;
switch (chip->chip_id) {
case KVM_IRQCHIP_PIC_MASTER:
memcpy(&chip->chip.pic, &pic->pics[0],
@@ -6027,18 +6025,15 @@ static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
kvm_get_ioapic(kvm, &chip->chip.ioapic);
break;
default:
- r = -EINVAL;
- break;
+ return -EINVAL;
}
- return r;
+ return 0;
}

static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
{
struct kvm_pic *pic = kvm->arch.vpic;
- int r;

- r = 0;
switch (chip->chip_id) {
case KVM_IRQCHIP_PIC_MASTER:
spin_lock(&pic->lock);
@@ -6056,11 +6051,10 @@ static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
kvm_set_ioapic(kvm, &chip->chip.ioapic);
break;
default:
- r = -EINVAL;
- break;
+ return -EINVAL;
}
kvm_pic_update_irq(pic);
- return r;
+ return 0;
}

static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)