* [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic @ 2014-11-07 21:18 Alexander Graf 2014-11-07 21:24 ` [Qemu-devel] [Qemu-ppc] " Alexander Graf 2014-11-10 12:31 ` [Qemu-devel] " Igor Mammedov 0 siblings, 2 replies; 9+ messages in thread From: Alexander Graf @ 2014-11-07 21:18 UTC (permalink / raw) To: qemu-ppc; +Cc: pbonzini, qemu-stable, stuart.yoder, kvm, qemu-devel Memory slots have to be page aligned to get entered into KVM. There is existing logic that tries to ensure that we pad memory slots that are not page aligned to the biggest region that would still fit in the alignment requirements. Unfortunately, that logic is broken. It tries to calculate the start offset based on the region size. Fix up the logic to do the thing it was intended to do and document it properly in the comment above it. With this patch applied, I can successfully run an e500 guest with more than 3GB RAM (at which point RAM starts overlapping subpage memory regions). Cc: qemu-stable@nongnu.org Signed-off-by: Alexander Graf <agraf@suse.de> --- kvm-all.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kvm-all.c b/kvm-all.c index 44a5e72..596e7ce 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -634,8 +634,10 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) unsigned delta; /* kvm works in page size chunks, but the function may be called - with sub-page size and unaligned start address. */ - delta = TARGET_PAGE_ALIGN(size) - size; + with sub-page size and unaligned start address. Pad the start + address to next and truncate size to previous page boundary. */ + delta = (TARGET_PAGE_SIZE - (start_addr & ~TARGET_PAGE_MASK)); + delta &= ~TARGET_PAGE_MASK; if (delta > size) { return; } -- 1.8.1.4 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [Qemu-ppc] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-07 21:18 [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic Alexander Graf @ 2014-11-07 21:24 ` Alexander Graf 2014-11-10 12:31 ` [Qemu-devel] " Igor Mammedov 1 sibling, 0 replies; 9+ messages in thread From: Alexander Graf @ 2014-11-07 21:24 UTC (permalink / raw) To: qemu-ppc; +Cc: qemu-devel, pbonzini, qemu-stable, kvm, stuart.yoder On 07.11.14 22:18, Alexander Graf wrote: > Memory slots have to be page aligned to get entered into KVM. There > is existing logic that tries to ensure that we pad memory slots that > are not page aligned to the biggest region that would still fit in the > alignment requirements. > > Unfortunately, that logic is broken. It tries to calculate the start > offset based on the region size. > > Fix up the logic to do the thing it was intended to do and document it > properly in the comment above it. > > With this patch applied, I can successfully run an e500 guest with more > than 3GB RAM (at which point RAM starts overlapping subpage memory regions). > > Cc: qemu-stable@nongnu.org > Signed-off-by: Alexander Graf <agraf@suse.de> If everyone agrees that this patch does indeed do what the code is intended to do (I think it's quite correct, to be 100% right it should use getpagesize() rather than TARGET_PAGE_SIZE), this should go into 2.2 still. Alex ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-07 21:18 [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic Alexander Graf 2014-11-07 21:24 ` [Qemu-devel] [Qemu-ppc] " Alexander Graf @ 2014-11-10 12:31 ` Igor Mammedov 2014-11-10 13:16 ` Alexander Graf 1 sibling, 1 reply; 9+ messages in thread From: Igor Mammedov @ 2014-11-10 12:31 UTC (permalink / raw) To: Alexander Graf Cc: kvm, qemu-devel, qemu-stable, stuart.yoder, qemu-ppc, pbonzini On Fri, 7 Nov 2014 22:18:45 +0100 Alexander Graf <agraf@suse.de> wrote: > Memory slots have to be page aligned to get entered into KVM. There > is existing logic that tries to ensure that we pad memory slots that > are not page aligned to the biggest region that would still fit in the > alignment requirements. > > Unfortunately, that logic is broken. It tries to calculate the start > offset based on the region size. > > Fix up the logic to do the thing it was intended to do and document it > properly in the comment above it. > > With this patch applied, I can successfully run an e500 guest with more > than 3GB RAM (at which point RAM starts overlapping subpage memory regions). > > Cc: qemu-stable@nongnu.org > Signed-off-by: Alexander Graf <agraf@suse.de> > --- > kvm-all.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/kvm-all.c b/kvm-all.c > index 44a5e72..596e7ce 100644 > --- a/kvm-all.c > +++ b/kvm-all.c > @@ -634,8 +634,10 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) > unsigned delta; > > /* kvm works in page size chunks, but the function may be called > - with sub-page size and unaligned start address. */ > - delta = TARGET_PAGE_ALIGN(size) - size; > + with sub-page size and unaligned start address. Pad the start > + address to next and truncate size to previous page boundary. */ I'm a bit confused how it works at all. Lets assume that there is no mapped pages that include start_addr, then if start_addr were padded to next page, kvm would map it from there but the rest of QEMU would still use unaligned start_addr for MemoryRegion that isn't even mapped. It would seem that instead of padding up to the next page, start_addr should be moved to the start of the page that includes it to make page with original start_addr available to guest. > + delta = (TARGET_PAGE_SIZE - (start_addr & ~TARGET_PAGE_MASK)); > + delta &= ~TARGET_PAGE_MASK; > if (delta > size) { > return; > } ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 12:31 ` [Qemu-devel] " Igor Mammedov @ 2014-11-10 13:16 ` Alexander Graf 2014-11-10 13:54 ` Paolo Bonzini ` (2 more replies) 0 siblings, 3 replies; 9+ messages in thread From: Alexander Graf @ 2014-11-10 13:16 UTC (permalink / raw) To: Igor Mammedov Cc: kvm, qemu-devel, qemu-stable, stuart.yoder, qemu-ppc, pbonzini On 10.11.14 13:31, Igor Mammedov wrote: > On Fri, 7 Nov 2014 22:18:45 +0100 > Alexander Graf <agraf@suse.de> wrote: > >> Memory slots have to be page aligned to get entered into KVM. There >> is existing logic that tries to ensure that we pad memory slots that >> are not page aligned to the biggest region that would still fit in the >> alignment requirements. >> >> Unfortunately, that logic is broken. It tries to calculate the start >> offset based on the region size. >> >> Fix up the logic to do the thing it was intended to do and document it >> properly in the comment above it. >> >> With this patch applied, I can successfully run an e500 guest with more >> than 3GB RAM (at which point RAM starts overlapping subpage memory regions). >> >> Cc: qemu-stable@nongnu.org >> Signed-off-by: Alexander Graf <agraf@suse.de> >> --- >> kvm-all.c | 6 ++++-- >> 1 file changed, 4 insertions(+), 2 deletions(-) >> >> diff --git a/kvm-all.c b/kvm-all.c >> index 44a5e72..596e7ce 100644 >> --- a/kvm-all.c >> +++ b/kvm-all.c >> @@ -634,8 +634,10 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) >> unsigned delta; >> >> /* kvm works in page size chunks, but the function may be called >> - with sub-page size and unaligned start address. */ >> - delta = TARGET_PAGE_ALIGN(size) - size; >> + with sub-page size and unaligned start address. Pad the start >> + address to next and truncate size to previous page boundary. */ > I'm a bit confused how it works at all. > Lets assume that there is no mapped pages that include start_addr, > then if start_addr were padded to next page, kvm would map it from there > but the rest of QEMU would still use unaligned start_addr for MemoryRegion > that isn't even mapped. Sorry, I don't understand this paragraph. Memory slots in general are accelerations for memory access - for MMIO (RAM is usually aligned), KVM can always exit to QEMU and just do a manual MMIO exit. > It would seem that instead of padding up to the next page, start_addr > should be moved to the start of the page that includes it to make page > with original start_addr available to guest. No, because in that case you would map something as RAM that really isn't RAM. Imagine you have the following memory layout: 0x1000 page size 1) 0x00000 - 0x10000 RAM 2) 0x10000 - 0x10100 MMIO 3) 0x10100 - 0x20000 RAM Then you want to map 1) as memory slot and 4) from 0x11000 onwards as memory slot. You can't map the page from 0x10000 - 0x11000 as memory slot, because part of it is MMIO. Alex ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 13:16 ` Alexander Graf @ 2014-11-10 13:54 ` Paolo Bonzini 2014-11-10 13:55 ` Peter Maydell 2014-11-10 13:55 ` Igor Mammedov 2 siblings, 0 replies; 9+ messages in thread From: Paolo Bonzini @ 2014-11-10 13:54 UTC (permalink / raw) To: Alexander Graf, Igor Mammedov Cc: qemu-stable, qemu-ppc, stuart.yoder, kvm, qemu-devel On 10/11/2014 14:16, Alexander Graf wrote: > No, because in that case you would map something as RAM that really > isn't RAM. > > Imagine you have the following memory layout: > > 0x1000 page size > > 1) 0x00000 - 0x10000 RAM > 2) 0x10000 - 0x10100 MMIO > 3) 0x10100 - 0x20000 RAM > > Then you want to map 1) as memory slot and 4) from 0x11000 onwards as > memory slot. > > You can't map the page from 0x10000 - 0x11000 as memory slot, because > part of it is MMIO. Right. The partial RAM page remains marked as MMIO as far as KVM is concerned, so accesses are slow and you cannot run code from it. However, it is fundamental that MMIO areas are not marked as RAM. Paolo ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 13:16 ` Alexander Graf 2014-11-10 13:54 ` Paolo Bonzini @ 2014-11-10 13:55 ` Peter Maydell 2014-11-10 14:48 ` Alexander Graf 2014-11-10 13:55 ` Igor Mammedov 2 siblings, 1 reply; 9+ messages in thread From: Peter Maydell @ 2014-11-10 13:55 UTC (permalink / raw) To: Alexander Graf Cc: kvm-devel, qemu-stable, Stuart Yoder, QEMU Developers, qemu-ppc@nongnu.org, Paolo Bonzini, Igor Mammedov On 10 November 2014 13:16, Alexander Graf <agraf@suse.de> wrote: > Sorry, I don't understand this paragraph. Memory slots in general are > accelerations for memory access - for MMIO (RAM is usually aligned), KVM > can always exit to QEMU and just do a manual MMIO exit. ...you're a bit stuck if you were hoping to execute code from that RAM, though, so they're not *purely* acceleration, right? -- PMM ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 13:55 ` Peter Maydell @ 2014-11-10 14:48 ` Alexander Graf 0 siblings, 0 replies; 9+ messages in thread From: Alexander Graf @ 2014-11-10 14:48 UTC (permalink / raw) To: Peter Maydell Cc: kvm-devel, qemu-stable, Stuart Yoder, QEMU Developers, qemu-ppc@nongnu.org, Paolo Bonzini, Igor Mammedov On 10.11.14 14:55, Peter Maydell wrote: > On 10 November 2014 13:16, Alexander Graf <agraf@suse.de> wrote: >> Sorry, I don't understand this paragraph. Memory slots in general are >> accelerations for memory access - for MMIO (RAM is usually aligned), KVM >> can always exit to QEMU and just do a manual MMIO exit. > > ...you're a bit stuck if you were hoping to execute code from > that RAM, though, so they're not *purely* acceleration, right? Yes and no. Technically, there's no reason KVM couldn't do an MMIO exit dance to fetch the next instruction. From user space this should be indistinguishable. Today, I don't think it's implemented though :). Alex ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 13:16 ` Alexander Graf 2014-11-10 13:54 ` Paolo Bonzini 2014-11-10 13:55 ` Peter Maydell @ 2014-11-10 13:55 ` Igor Mammedov 2014-11-10 14:47 ` Alexander Graf 2 siblings, 1 reply; 9+ messages in thread From: Igor Mammedov @ 2014-11-10 13:55 UTC (permalink / raw) To: Alexander Graf Cc: kvm, qemu-devel, qemu-stable, stuart.yoder, qemu-ppc, pbonzini On Mon, 10 Nov 2014 14:16:58 +0100 Alexander Graf <agraf@suse.de> wrote: > > > On 10.11.14 13:31, Igor Mammedov wrote: > > On Fri, 7 Nov 2014 22:18:45 +0100 > > Alexander Graf <agraf@suse.de> wrote: > > > >> Memory slots have to be page aligned to get entered into KVM. There > >> is existing logic that tries to ensure that we pad memory slots that > >> are not page aligned to the biggest region that would still fit in the > >> alignment requirements. > >> > >> Unfortunately, that logic is broken. It tries to calculate the start > >> offset based on the region size. > >> > >> Fix up the logic to do the thing it was intended to do and document it > >> properly in the comment above it. > >> > >> With this patch applied, I can successfully run an e500 guest with more > >> than 3GB RAM (at which point RAM starts overlapping subpage memory regions). > >> > >> Cc: qemu-stable@nongnu.org > >> Signed-off-by: Alexander Graf <agraf@suse.de> > >> --- > >> kvm-all.c | 6 ++++-- > >> 1 file changed, 4 insertions(+), 2 deletions(-) > >> > >> diff --git a/kvm-all.c b/kvm-all.c > >> index 44a5e72..596e7ce 100644 > >> --- a/kvm-all.c > >> +++ b/kvm-all.c > >> @@ -634,8 +634,10 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) > >> unsigned delta; > >> > >> /* kvm works in page size chunks, but the function may be called > >> - with sub-page size and unaligned start address. */ > >> - delta = TARGET_PAGE_ALIGN(size) - size; > >> + with sub-page size and unaligned start address. Pad the start > >> + address to next and truncate size to previous page boundary. */ > > I'm a bit confused how it works at all. > > Lets assume that there is no mapped pages that include start_addr, > > then if start_addr were padded to next page, kvm would map it from there > > but the rest of QEMU would still use unaligned start_addr for MemoryRegion > > that isn't even mapped. > > Sorry, I don't understand this paragraph. Memory slots in general are > accelerations for memory access - for MMIO (RAM is usually aligned), KVM > can always exit to QEMU and just do a manual MMIO exit. > > > It would seem that instead of padding up to the next page, start_addr > > should be moved to the start of the page that includes it to make page > > with original start_addr available to guest. > > No, because in that case you would map something as RAM that really > isn't RAM. > > Imagine you have the following memory layout: > > 0x1000 page size > > 1) 0x00000 - 0x10000 RAM > 2) 0x10000 - 0x10100 MMIO > 3) 0x10100 - 0x20000 RAM > > Then you want to map 1) as memory slot and 4) from 0x11000 onwards as > memory slot. so every access to RAM 0x10100-0x11000 which is not represented as memory slot would cause VMEXIT? > > You can't map the page from 0x10000 - 0x11000 as memory slot, because > part of it is MMIO. > > > Alex ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic 2014-11-10 13:55 ` Igor Mammedov @ 2014-11-10 14:47 ` Alexander Graf 0 siblings, 0 replies; 9+ messages in thread From: Alexander Graf @ 2014-11-10 14:47 UTC (permalink / raw) To: Igor Mammedov Cc: kvm, qemu-devel, qemu-stable, stuart.yoder, qemu-ppc, pbonzini On 10.11.14 14:55, Igor Mammedov wrote: > On Mon, 10 Nov 2014 14:16:58 +0100 > Alexander Graf <agraf@suse.de> wrote: > >> >> >> On 10.11.14 13:31, Igor Mammedov wrote: >>> On Fri, 7 Nov 2014 22:18:45 +0100 >>> Alexander Graf <agraf@suse.de> wrote: >>> >>>> Memory slots have to be page aligned to get entered into KVM. There >>>> is existing logic that tries to ensure that we pad memory slots that >>>> are not page aligned to the biggest region that would still fit in the >>>> alignment requirements. >>>> >>>> Unfortunately, that logic is broken. It tries to calculate the start >>>> offset based on the region size. >>>> >>>> Fix up the logic to do the thing it was intended to do and document it >>>> properly in the comment above it. >>>> >>>> With this patch applied, I can successfully run an e500 guest with more >>>> than 3GB RAM (at which point RAM starts overlapping subpage memory regions). >>>> >>>> Cc: qemu-stable@nongnu.org >>>> Signed-off-by: Alexander Graf <agraf@suse.de> >>>> --- >>>> kvm-all.c | 6 ++++-- >>>> 1 file changed, 4 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/kvm-all.c b/kvm-all.c >>>> index 44a5e72..596e7ce 100644 >>>> --- a/kvm-all.c >>>> +++ b/kvm-all.c >>>> @@ -634,8 +634,10 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) >>>> unsigned delta; >>>> >>>> /* kvm works in page size chunks, but the function may be called >>>> - with sub-page size and unaligned start address. */ >>>> - delta = TARGET_PAGE_ALIGN(size) - size; >>>> + with sub-page size and unaligned start address. Pad the start >>>> + address to next and truncate size to previous page boundary. */ >>> I'm a bit confused how it works at all. >>> Lets assume that there is no mapped pages that include start_addr, >>> then if start_addr were padded to next page, kvm would map it from there >>> but the rest of QEMU would still use unaligned start_addr for MemoryRegion >>> that isn't even mapped. >> >> Sorry, I don't understand this paragraph. Memory slots in general are >> accelerations for memory access - for MMIO (RAM is usually aligned), KVM >> can always exit to QEMU and just do a manual MMIO exit. >> >>> It would seem that instead of padding up to the next page, start_addr >>> should be moved to the start of the page that includes it to make page >>> with original start_addr available to guest. >> >> No, because in that case you would map something as RAM that really >> isn't RAM. >> >> Imagine you have the following memory layout: >> >> 0x1000 page size >> >> 1) 0x00000 - 0x10000 RAM >> 2) 0x10000 - 0x10100 MMIO >> 3) 0x10100 - 0x20000 RAM >> >> Then you want to map 1) as memory slot and 4) from 0x11000 onwards as >> memory slot. > so every access to RAM 0x10100-0x11000 which is not represented as memory > slot would cause VMEXIT? Yes, there's no other way. Otherwise we wouldn't be able to trap on the exits from 0x10000 - 0x10100. Hardware only gives us page granularity. Usually this isn't an issue because overlapping MMIO regions are pretty large chunks of power-of-2 size - if you see any overlapping at all. On e500 this bites us though, because we end up with small MSI-X windows inside our address space (which in turn might also be a bug, but that doesn't mean that the slot mapping logic should be left as broken as it is). Alex ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-11-10 14:49 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2014-11-07 21:18 [Qemu-devel] [PATCH] kvm: Fix memory slot page alignment logic Alexander Graf 2014-11-07 21:24 ` [Qemu-devel] [Qemu-ppc] " Alexander Graf 2014-11-10 12:31 ` [Qemu-devel] " Igor Mammedov 2014-11-10 13:16 ` Alexander Graf 2014-11-10 13:54 ` Paolo Bonzini 2014-11-10 13:55 ` Peter Maydell 2014-11-10 14:48 ` Alexander Graf 2014-11-10 13:55 ` Igor Mammedov 2014-11-10 14:47 ` Alexander Graf
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).