From: Avi Kivity <avi@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, Dave Hansen <dave@linux.vnet.ibm.com>
Subject: [PATCH 37/40] KVM: Reduce kvm stack usage in kvm_arch_vm_ioctl()
Date: Tue, 23 Sep 2008 16:46:51 +0300 [thread overview]
Message-ID: <1222177614-26669-38-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1222177614-26669-1-git-send-email-avi@redhat.com>
From: Dave Hansen <dave@linux.vnet.ibm.com>
On my machine with gcc 3.4, kvm uses ~2k of stack in a few
select functions. This is mostly because gcc fails to
notice that the different case: statements could have their
stack usage combined. It overflows very nicely if interrupts
happen during one of these large uses.
This patch uses two methods for reducing stack usage.
1. dynamically allocate large objects instead of putting
on the stack.
2. Use a union{} member for all of the case variables. This
tricks gcc into combining them all into a single stack
allocation. (There's also a comment on this)
Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
arch/x86/kvm/x86.c | 72 ++++++++++++++++++++++++++++++++-------------------
1 files changed, 45 insertions(+), 27 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a97157c..87d4342 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1869,6 +1869,15 @@ long kvm_arch_vm_ioctl(struct file *filp,
struct kvm *kvm = filp->private_data;
void __user *argp = (void __user *)arg;
int r = -EINVAL;
+ /*
+ * This union makes it completely explicit to gcc-3.x
+ * that these two variables' stack usage should be
+ * combined, not added together.
+ */
+ union {
+ struct kvm_pit_state ps;
+ struct kvm_memory_alias alias;
+ } u;
switch (ioctl) {
case KVM_SET_TSS_ADDR:
@@ -1900,17 +1909,14 @@ long kvm_arch_vm_ioctl(struct file *filp,
case KVM_GET_NR_MMU_PAGES:
r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
break;
- case KVM_SET_MEMORY_ALIAS: {
- struct kvm_memory_alias alias;
-
+ case KVM_SET_MEMORY_ALIAS:
r = -EFAULT;
- if (copy_from_user(&alias, argp, sizeof alias))
+ if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
goto out;
- r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
+ r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
if (r)
goto out;
break;
- }
case KVM_CREATE_IRQCHIP:
r = -ENOMEM;
kvm->arch.vpic = kvm_create_pic(kvm);
@@ -1952,37 +1958,51 @@ long kvm_arch_vm_ioctl(struct file *filp,
}
case KVM_GET_IRQCHIP: {
/* 0: PIC master, 1: PIC slave, 2: IOAPIC */
- struct kvm_irqchip chip;
+ struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
- r = -EFAULT;
- if (copy_from_user(&chip, argp, sizeof chip))
+ r = -ENOMEM;
+ if (!chip)
goto out;
+ r = -EFAULT;
+ if (copy_from_user(chip, argp, sizeof *chip))
+ goto get_irqchip_out;
r = -ENXIO;
if (!irqchip_in_kernel(kvm))
- goto out;
- r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
+ goto get_irqchip_out;
+ r = kvm_vm_ioctl_get_irqchip(kvm, chip);
if (r)
- goto out;
+ goto get_irqchip_out;
r = -EFAULT;
- if (copy_to_user(argp, &chip, sizeof chip))
- goto out;
+ if (copy_to_user(argp, chip, sizeof *chip))
+ goto get_irqchip_out;
r = 0;
+ get_irqchip_out:
+ kfree(chip);
+ if (r)
+ goto out;
break;
}
case KVM_SET_IRQCHIP: {
/* 0: PIC master, 1: PIC slave, 2: IOAPIC */
- struct kvm_irqchip chip;
+ struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
- r = -EFAULT;
- if (copy_from_user(&chip, argp, sizeof chip))
+ r = -ENOMEM;
+ if (!chip)
goto out;
+ r = -EFAULT;
+ if (copy_from_user(chip, argp, sizeof *chip))
+ goto set_irqchip_out;
r = -ENXIO;
if (!irqchip_in_kernel(kvm))
- goto out;
- r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
+ goto set_irqchip_out;
+ r = kvm_vm_ioctl_set_irqchip(kvm, chip);
if (r)
- goto out;
+ goto set_irqchip_out;
r = 0;
+ set_irqchip_out:
+ kfree(chip);
+ if (r)
+ goto out;
break;
}
case KVM_ASSIGN_PCI_DEVICE: {
@@ -2008,31 +2028,29 @@ long kvm_arch_vm_ioctl(struct file *filp,
break;
}
case KVM_GET_PIT: {
- struct kvm_pit_state ps;
r = -EFAULT;
- if (copy_from_user(&ps, argp, sizeof ps))
+ if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
goto out;
r = -ENXIO;
if (!kvm->arch.vpit)
goto out;
- r = kvm_vm_ioctl_get_pit(kvm, &ps);
+ r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
if (r)
goto out;
r = -EFAULT;
- if (copy_to_user(argp, &ps, sizeof ps))
+ if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
goto out;
r = 0;
break;
}
case KVM_SET_PIT: {
- struct kvm_pit_state ps;
r = -EFAULT;
- if (copy_from_user(&ps, argp, sizeof ps))
+ if (copy_from_user(&u.ps, argp, sizeof u.ps))
goto out;
r = -ENXIO;
if (!kvm->arch.vpit)
goto out;
- r = kvm_vm_ioctl_set_pit(kvm, &ps);
+ r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
if (r)
goto out;
r = 0;
--
1.6.0.1
next prev parent reply other threads:[~2008-09-23 13:46 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-23 13:46 [PATCH 00/40] KVM Updates for 2.6.28 merge window (part 1 of 3) Avi Kivity
2008-09-23 13:46 ` [PATCH 01/40] KVM: VMX: Rename misnamed msr bits Avi Kivity
2008-09-23 13:46 ` [PATCH 02/40] KVM: x86: accessors for guest registers Avi Kivity
2008-09-23 13:46 ` [PATCH 03/40] KVM: Move KVM TRACE DEFINITIONS to common header Avi Kivity
2008-09-23 13:46 ` [PATCH 04/40] KVM: Introduce kvm_set_irq to inject interrupts in guests Avi Kivity
2008-09-23 13:46 ` [PATCH 05/40] KVM: MMU: Separate the code for unlinking a shadow page from its parents Avi Kivity
2008-09-23 13:46 ` [PATCH 06/40] KVM: MMU: Simplify kvm_mmu_zap_page() Avi Kivity
2008-09-23 13:46 ` [PATCH 07/40] KVM: Move NMI IRET fault processing to new vmx_complete_interrupts() Avi Kivity
2008-09-23 13:46 ` [PATCH 08/40] KVM: VMX: Move nmi injection failure processing to vm exit path Avi Kivity
2008-09-23 13:46 ` [PATCH 09/40] KVM: Clear exception queue before emulating an instruction Avi Kivity
2008-09-23 13:46 ` [PATCH 10/40] KVM: VMX: Fix pending exception processing Avi Kivity
2008-09-23 13:46 ` [PATCH 11/40] KVM: Add a pending interrupt queue Avi Kivity
2008-09-23 13:46 ` [PATCH 12/40] KVM: VMX: Move interrupt post-processing to vmx_complete_interrupts() Avi Kivity
2008-09-23 13:46 ` [PATCH 13/40] KVM: VMX: Remove redundant check in handle_rmode_exception Avi Kivity
2008-09-23 13:46 ` [PATCH 14/40] KVM: Consolidate PIC isr clearing into a function Avi Kivity
2008-09-23 13:46 ` [PATCH 15/40] KVM: Consolidate XX_VECTOR defines Avi Kivity
2008-09-23 13:46 ` [PATCH 16/40] KVM: VMX: Reinject real mode exception Avi Kivity
2008-09-23 13:46 ` [PATCH 17/40] KVM: VMX: Unify register save/restore across 32 and 64 bit hosts Avi Kivity
2008-09-23 13:46 ` [PATCH 18/40] KVM: SVM: " Avi Kivity
2008-09-23 13:46 ` [PATCH 19/40] KVM: kvmtrace: Remove use of bit fields in kvm trace structure Avi Kivity
2008-09-23 13:46 ` [PATCH 20/40] KVM: kvmtrace: replace get_cycles with ktime_get v3 Avi Kivity
2008-09-23 13:46 ` [PATCH 21/40] KVM: ppc: enable KVM_TRACE building for powerpc Avi Kivity
2008-09-23 13:46 ` [PATCH 22/40] KVM: ppc: adds trace points for ppc tlb activity Avi Kivity
2008-09-23 13:46 ` [PATCH 23/40] KVM: ppc: trace powerpc instruction emulation Avi Kivity
2008-09-23 13:46 ` [PATCH 24/40] KVM: VMX: Avoid vmwrite(HOST_RSP) when possible Avi Kivity
2008-09-23 13:46 ` [PATCH 25/40] KVM: Ignore DEBUGCTL MSRs with no effect Avi Kivity
2008-09-23 13:46 ` [PATCH 26/40] KVM: ppc: guest breakpoint support Avi Kivity
2008-09-23 13:46 ` [PATCH 27/40] KVM: ppc: Stop saving host TLB state Avi Kivity
2008-09-23 13:46 ` [PATCH 28/40] KVM: ppc: Write only modified shadow entries into the TLB on exit Avi Kivity
2008-09-23 13:46 ` [PATCH 29/40] KVM: powerpc: Map guest userspace with TID=0 mappings Avi Kivity
2008-09-23 13:46 ` [PATCH 30/40] KVM: Add irq ack notifier list Avi Kivity
2008-09-23 13:46 ` [PATCH 31/40] KVM: irq ack notification Avi Kivity
2008-09-23 13:46 ` [PATCH 32/40] KVM: PIT: fix injection logic and count Avi Kivity
2008-09-23 13:46 ` [PATCH 33/40] x86: paravirt: factor out cpu_khz to common code Avi Kivity
2008-09-23 13:46 ` [PATCH 34/40] x86: KVM guest: use paravirt function to calculate cpu khz Avi Kivity
2008-09-23 13:46 ` [PATCH 35/40] KVM: direct mmio pfn check Avi Kivity
2008-09-23 13:46 ` [PATCH 36/40] KVM: pci device assignment Avi Kivity
2008-09-23 13:46 ` Avi Kivity [this message]
2008-09-23 13:46 ` [PATCH 38/40] KVM: Reduce stack usage in kvm_vcpu_ioctl() Avi Kivity
2008-09-23 13:46 ` [PATCH 39/40] KVM: Reduce stack usage in kvm_arch_vcpu_ioctl() Avi Kivity
2008-09-23 13:46 ` [PATCH 40/40] KVM: Reduce stack usage in kvm_pv_mmu_op() Avi Kivity
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1222177614-26669-38-git-send-email-avi@redhat.com \
--to=avi@redhat.com \
--cc=dave@linux.vnet.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox