Skip to contentSkip to conversation

kvm:Return -ENOMEM directly for the function, kvm_create_lapic

PATCH Ingo Molnar 2 participants 2 messages May 21, 2015 1 message not in the archive KVM

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ingo Molnar

author NAK
#1 lore raw
Quoted 26 lines · Nicholas Krause

In order to make code paths easier to read in the function, kvm_create_lapic we return -ENOMEM when unable to allocate memory for a kvm_lapic structure pointer directly. This makes the code easier to read and cleaner then jumping to a goto label at the end of the function's body for returning just the error code, -ENOMEM.

---
 arch/x86/kvm/lapic.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/lapic.c +1 -1
index 629af0f..88d0cce 100644
@@ -1687,7 +1687,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
 
 	apic = kzalloc(sizeof(*apic), GFP_KERNEL);
 	if (!apic)
-		goto nomem;
+		return -ENOMEM;
 
 	vcpu->arch.apic = apic;
 

arch/x86/kvm/lapic.c int kvm_create_lapic(struct kvm_vcpu *vcpu) · line 1722

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -1
@@ -1718,7 +1718,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
 	return 0;
 nomem_free_apic:
 	kfree(apic);
-nomem:
 	return -ENOMEM;
 }

NAK!

You just half destroyed the nice error handling cascade of labels.

Thanks,

Ingo

maintainer
#2 lore raw
Quoted 38 lines · Ingo Molnar
Quoted 26 lines · Nicholas Krause

In order to make code paths easier to read in the function, kvm_create_lapic we return -ENOMEM when unable to allocate memory for a kvm_lapic structure pointer directly. This makes the code easier to read and cleaner then jumping to a goto label at the end of the function's body for returning just the error code, -ENOMEM.

---
 arch/x86/kvm/lapic.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/lapic.c +1 -1
index 629af0f..88d0cce 100644
@@ -1687,7 +1687,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
 
 	apic = kzalloc(sizeof(*apic), GFP_KERNEL);
 	if (!apic)
-		goto nomem;
+		return -ENOMEM;
 
 	vcpu->arch.apic = apic;
 

arch/x86/kvm/lapic.c int kvm_create_lapic(struct kvm_vcpu *vcpu) · line 1722

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -1
@@ -1718,7 +1718,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
 	return 0;
 nomem_free_apic:
 	kfree(apic);
-nomem:
 	return -ENOMEM;
 }

NAK!

Quoted 1 line

You just half destroyed the nice error handling cascade of labels.

Right.

What could be done, is always going through kfree(apic), because it is okay to free NULL. So the nomem label moves up, and the nomem_free_apic label is not necessary anymore.

Paolo