From patchwork Sun Jun 20 13:14:13 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Kivity X-Patchwork-Id: 107048 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o5KDEKpc027453 for ; Sun, 20 Jun 2010 13:14:21 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753894Ab0FTNOS (ORCPT ); Sun, 20 Jun 2010 09:14:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45335 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753838Ab0FTNOQ (ORCPT ); Sun, 20 Jun 2010 09:14:16 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o5KDEF60028061 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 20 Jun 2010 09:14:15 -0400 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o5KDEEWD014584; Sun, 20 Jun 2010 09:14:15 -0400 Received: from file.tlv.redhat.com (file.tlv.redhat.com [10.35.255.8]) by cleopatra.tlv.redhat.com (Postfix) with ESMTP id 5DC7E250ADB; Sun, 20 Jun 2010 16:14:14 +0300 (IDT) From: Avi Kivity To: Marcelo Tosatti , Sheng Yang Cc: kvm@vger.kernel.org Subject: [PATCH 2/2] KVM: Consolidate load/save temporary buffer allocation and freeing Date: Sun, 20 Jun 2010 16:14:13 +0300 Message-Id: <1277039653-26056-3-git-send-email-avi@redhat.com> In-Reply-To: <1277039653-26056-1-git-send-email-avi@redhat.com> References: <1277039653-26056-1-git-send-email-avi@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Sun, 20 Jun 2010 13:14:21 +0000 (UTC) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d513e57..33156a3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2436,25 +2436,29 @@ long kvm_arch_vcpu_ioctl(struct file *filp, struct kvm_vcpu *vcpu = filp->private_data; void __user *argp = (void __user *)arg; int r; - struct kvm_lapic_state *lapic = NULL; - struct kvm_xsave *xsave = NULL; - struct kvm_xcrs *xcrs = NULL; + union { + struct kvm_lapic_state *lapic; + struct kvm_xsave *xsave; + struct kvm_xcrs *xcrs; + void *buffer; + } u; + u.buffer = NULL; switch (ioctl) { case KVM_GET_LAPIC: { r = -EINVAL; if (!vcpu->arch.apic) goto out; - lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); + u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); r = -ENOMEM; - if (!lapic) + if (!u.lapic) goto out; - r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); + r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); if (r) goto out; r = -EFAULT; - if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) + if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) goto out; r = 0; break; @@ -2463,14 +2467,14 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r = -EINVAL; if (!vcpu->arch.apic) goto out; - lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); + u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); r = -ENOMEM; - if (!lapic) + if (!u.lapic) goto out; r = -EFAULT; - if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) + if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state))) goto out; - r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); + r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); if (r) goto out; r = 0; @@ -2634,68 +2638,66 @@ long kvm_arch_vcpu_ioctl(struct file *filp, break; } case KVM_GET_XSAVE: { - xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); + u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); r = -ENOMEM; - if (!xsave) + if (!u.xsave) break; - kvm_vcpu_ioctl_x86_get_xsave(vcpu, xsave); + kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); r = -EFAULT; - if (copy_to_user(argp, xsave, sizeof(struct kvm_xsave))) + if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) break; r = 0; break; } case KVM_SET_XSAVE: { - xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); + u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); r = -ENOMEM; - if (!xsave) + if (!u.xsave) break; r = -EFAULT; - if (copy_from_user(xsave, argp, sizeof(struct kvm_xsave))) + if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave))) break; - r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, xsave); + r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); break; } case KVM_GET_XCRS: { - xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); + u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); r = -ENOMEM; - if (!xcrs) + if (!u.xcrs) break; - kvm_vcpu_ioctl_x86_get_xcrs(vcpu, xcrs); + kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); r = -EFAULT; - if (copy_to_user(argp, xcrs, + if (copy_to_user(argp, u.xcrs, sizeof(struct kvm_xcrs))) break; r = 0; break; } case KVM_SET_XCRS: { - xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); + u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); r = -ENOMEM; - if (!xcrs) + if (!u.xcrs) break; r = -EFAULT; - if (copy_from_user(xcrs, argp, + if (copy_from_user(u.xcrs, argp, sizeof(struct kvm_xcrs))) break; - r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, xcrs); + r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); break; } default: r = -EINVAL; } out: - kfree(lapic); - kfree(xsave); - kfree(xcrs); + kfree(u.buffer); return r; }