* [Qemu-devel] [PATCH v2 0/3] vfio: fixes for better support for 128 bit memory section sizes
@ 2013-08-22 8:20 Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64() Alexey Kardashevskiy
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 8:20 UTC (permalink / raw)
To: qemu-devel; +Cc: Alexey Kardashevskiy, Paolo Bonzini, Alex Williamson
I made a couple of small patches while debugging VFIO on SPAPR
which uses IOMMU MemoryRegion 2^64 bytes long.
Changes:
v2:
* added int128_exts64() function as a separate patch and used in
"vfio: Fix 128 bit handling".
Alexey Kardashevskiy (3):
int128: add int128_exts64()
vfio: Fix debug output for int128 values
vfio: Fix 128 bit handling
hw/misc/vfio.c | 19 +++++++++++++------
include/qemu/int128.h | 5 +++++
2 files changed, 18 insertions(+), 6 deletions(-)
--
1.8.4.rc4
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 8:20 [Qemu-devel] [PATCH v2 0/3] vfio: fixes for better support for 128 bit memory section sizes Alexey Kardashevskiy
@ 2013-08-22 8:20 ` Alexey Kardashevskiy
2013-08-22 9:09 ` Paolo Bonzini
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 2/3] vfio: Fix debug output for int128 values Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling Alexey Kardashevskiy
2 siblings, 1 reply; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 8:20 UTC (permalink / raw)
To: qemu-devel; +Cc: Alexey Kardashevskiy, Paolo Bonzini, Alex Williamson
This adds macro to extend signed 64bit value to signed 128bit value.
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
include/qemu/int128.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/include/qemu/int128.h b/include/qemu/int128.h
index 9ed47aa..987a1a9 100644
--- a/include/qemu/int128.h
+++ b/include/qemu/int128.h
@@ -38,6 +38,11 @@ static inline Int128 int128_2_64(void)
return (Int128) { 0, 1 };
}
+static inline Int128 int128_exts64(int64_t a)
+{
+ return (Int128) { .lo = a, .hi = (a >> 63) ? -1 : 0 };
+}
+
static inline Int128 int128_and(Int128 a, Int128 b)
{
return (Int128) { a.lo & b.lo, a.hi & b.hi };
--
1.8.4.rc4
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v2 2/3] vfio: Fix debug output for int128 values
2013-08-22 8:20 [Qemu-devel] [PATCH v2 0/3] vfio: fixes for better support for 128 bit memory section sizes Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64() Alexey Kardashevskiy
@ 2013-08-22 8:20 ` Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling Alexey Kardashevskiy
2 siblings, 0 replies; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 8:20 UTC (permalink / raw)
To: qemu-devel; +Cc: Alexey Kardashevskiy, Paolo Bonzini, Alex Williamson
Memory regions can easily be 2^64 byte long and therefore overflow
for just a bit but that is enough for int128_get64() to assert.
This takes care of debug printing of huge section sizes.
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
hw/misc/vfio.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
index 017e693..dfe3a80 100644
--- a/hw/misc/vfio.c
+++ b/hw/misc/vfio.c
@@ -1928,7 +1928,8 @@ static void vfio_listener_region_add(MemoryListener *listener,
if (vfio_listener_skipped_section(section)) {
DPRINTF("SKIPPING region_add %"HWADDR_PRIx" - %"PRIx64"\n",
section->offset_within_address_space,
- section->offset_within_address_space + section->size - 1);
+ section->offset_within_address_space +
+ int128_get64(int128_sub(section->size, int128_one())));
return;
}
@@ -1973,7 +1974,8 @@ static void vfio_listener_region_del(MemoryListener *listener,
if (vfio_listener_skipped_section(section)) {
DPRINTF("SKIPPING region_del %"HWADDR_PRIx" - %"PRIx64"\n",
section->offset_within_address_space,
- section->offset_within_address_space + section->size - 1);
+ section->offset_within_address_space +
+ int128_get64(int128_sub(section->size, int128_one())));
return;
}
--
1.8.4.rc4
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling
2013-08-22 8:20 [Qemu-devel] [PATCH v2 0/3] vfio: fixes for better support for 128 bit memory section sizes Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64() Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 2/3] vfio: Fix debug output for int128 values Alexey Kardashevskiy
@ 2013-08-22 8:20 ` Alexey Kardashevskiy
2013-08-22 9:11 ` Paolo Bonzini
2 siblings, 1 reply; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 8:20 UTC (permalink / raw)
To: qemu-devel; +Cc: Alexey Kardashevskiy, Paolo Bonzini, Alex Williamson
Upcoming VFIO on SPAPR PPC64 support will initialize the IOMMU
memory region with UINT64_MAX (2^64 bytes) size so int128_get64()
will assert.
The patch takes care of this check. The existing type1 IOMMU code
is not expected to map all 64 bits of RAM so the patch does not
touch that part.
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
Changes:
v2:
* used new function int128_exts64()
---
hw/misc/vfio.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
index dfe3a80..3878fc7 100644
--- a/hw/misc/vfio.c
+++ b/hw/misc/vfio.c
@@ -1920,6 +1920,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
VFIOContainer *container = container_of(listener, VFIOContainer,
iommu_data.listener);
hwaddr iova, end;
+ Int128 llend;
void *vaddr;
int ret;
@@ -1940,13 +1941,17 @@ static void vfio_listener_region_add(MemoryListener *listener,
}
iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
- end = (section->offset_within_address_space + int128_get64(section->size)) &
- TARGET_PAGE_MASK;
+ llend = int128_make64(section->offset_within_address_space);
+ llend = int128_add(llend, section->size);
+ llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
- if (iova >= end) {
+ if (int128_ge(int128_make64(iova), llend)) {
return;
}
+ end = (section->offset_within_address_space + int128_get64(section->size)) &
+ TARGET_PAGE_MASK;
+
vaddr = memory_region_get_ram_ptr(section->mr) +
section->offset_within_region +
(iova - section->offset_within_address_space);
--
1.8.4.rc4
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64() Alexey Kardashevskiy
@ 2013-08-22 9:09 ` Paolo Bonzini
2013-08-22 9:47 ` Peter Maydell
0 siblings, 1 reply; 13+ messages in thread
From: Paolo Bonzini @ 2013-08-22 9:09 UTC (permalink / raw)
To: Alexey Kardashevskiy; +Cc: Alex Williamson, qemu-devel
Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
> This adds macro to extend signed 64bit value to signed 128bit value.
>
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
> include/qemu/int128.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/include/qemu/int128.h b/include/qemu/int128.h
> index 9ed47aa..987a1a9 100644
> --- a/include/qemu/int128.h
> +++ b/include/qemu/int128.h
> @@ -38,6 +38,11 @@ static inline Int128 int128_2_64(void)
> return (Int128) { 0, 1 };
> }
>
> +static inline Int128 int128_exts64(int64_t a)
> +{
> + return (Int128) { .lo = a, .hi = (a >> 63) ? -1 : 0 };
> +}
The "? -1 : 0" is not necessary, but the compiler will remove it at -O1
or more (interestingly, or -O0 it will remove the shift and leave the
conditional!).
> static inline Int128 int128_and(Int128 a, Int128 b)
> {
> return (Int128) { a.lo & b.lo, a.hi & b.hi };
>
Acked-by: Paolo Bonzini <pbonzini@redhat.com>
Paolo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling Alexey Kardashevskiy
@ 2013-08-22 9:11 ` Paolo Bonzini
2013-08-22 10:41 ` Alexey Kardashevskiy
0 siblings, 1 reply; 13+ messages in thread
From: Paolo Bonzini @ 2013-08-22 9:11 UTC (permalink / raw)
To: Alexey Kardashevskiy; +Cc: Alex Williamson, qemu-devel
Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
> iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
> - end = (section->offset_within_address_space + int128_get64(section->size)) &
> - TARGET_PAGE_MASK;
> + llend = int128_make64(section->offset_within_address_space);
> + llend = int128_add(llend, section->size);
> + llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
>
> - if (iova >= end) {
> + if (int128_ge(int128_make64(iova), llend)) {
> return;
> }
>
> + end = (section->offset_within_address_space + int128_get64(section->size)) &
> + TARGET_PAGE_MASK;
> +
This can still fail for section->size = 2^64. Do your IOMMU patches
take care of it?
Paolo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 9:09 ` Paolo Bonzini
@ 2013-08-22 9:47 ` Peter Maydell
2013-08-22 9:48 ` Paolo Bonzini
0 siblings, 1 reply; 13+ messages in thread
From: Peter Maydell @ 2013-08-22 9:47 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Alexey Kardashevskiy, Alex Williamson, QEMU Developers
On 22 August 2013 10:09, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
>> +static inline Int128 int128_exts64(int64_t a)
>> +{
>> + return (Int128) { .lo = a, .hi = (a >> 63) ? -1 : 0 };
>> +}
>
> The "? -1 : 0" is not necessary, but the compiler will remove it at -O1
> or more (interestingly, or -O0 it will remove the shift and leave the
> conditional!).
We can avoid relying on implementation defined
behaviour here by using
.hi = (a < 0) ? -1 : 0;
(I know we allow ourselves to assume right-shift of signed
ints is arithmetic shift, but I think it's nicer to avoid it unless
it really makes the code better.)
-- PMM
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 9:47 ` Peter Maydell
@ 2013-08-22 9:48 ` Paolo Bonzini
2013-08-22 10:43 ` Peter Maydell
2013-08-22 10:50 ` Alexey Kardashevskiy
0 siblings, 2 replies; 13+ messages in thread
From: Paolo Bonzini @ 2013-08-22 9:48 UTC (permalink / raw)
To: Peter Maydell; +Cc: Alexey Kardashevskiy, Alex Williamson, QEMU Developers
Il 22/08/2013 11:47, Peter Maydell ha scritto:
> On 22 August 2013 10:09, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
>>> +static inline Int128 int128_exts64(int64_t a)
>>> +{
>>> + return (Int128) { .lo = a, .hi = (a >> 63) ? -1 : 0 };
>>> +}
>>
>> The "? -1 : 0" is not necessary, but the compiler will remove it at -O1
>> or more (interestingly, or -O0 it will remove the shift and leave the
>> conditional!).
>
> We can avoid relying on implementation defined
> behaviour here by using
> .hi = (a < 0) ? -1 : 0;
>
> (I know we allow ourselves to assume right-shift of signed
> ints is arithmetic shift, but I think it's nicer to avoid it unless
> it really makes the code better.)
This is what Alexey proposed. I suggested (a >> 63) without the ?: but
he misunderstood my (probably not clear enough) suggestion.
Paolo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling
2013-08-22 9:11 ` Paolo Bonzini
@ 2013-08-22 10:41 ` Alexey Kardashevskiy
2013-08-22 11:13 ` Paolo Bonzini
0 siblings, 1 reply; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 10:41 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Alex Williamson, qemu-devel
On 08/22/2013 07:11 PM, Paolo Bonzini wrote:
> Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
>> iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
>> - end = (section->offset_within_address_space + int128_get64(section->size)) &
>> - TARGET_PAGE_MASK;
>> + llend = int128_make64(section->offset_within_address_space);
>> + llend = int128_add(llend, section->size);
>> + llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
>>
>> - if (iova >= end) {
>> + if (int128_ge(int128_make64(iova), llend)) {
>> return;
>> }
>>
>> + end = (section->offset_within_address_space + int128_get64(section->size)) &
>> + TARGET_PAGE_MASK;
>> +
>
> This can still fail for section->size = 2^64. Do your IOMMU patches
> take care of it?
Nope. That part works for IOMMU mapped to RAM which is smaller than 2^64
bytes and therefore I do not see why we would need 2^64 bits sizes there.
Either way, I cannot test it quick (yes, I know, I should have some x86
VFIO setup by hand as everyone has a lot of x86, etc...) so I decided to
leave to the moment when x86 folks hit the problem :)
--
Alexey
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 9:48 ` Paolo Bonzini
@ 2013-08-22 10:43 ` Peter Maydell
2013-08-22 10:50 ` Alexey Kardashevskiy
1 sibling, 0 replies; 13+ messages in thread
From: Peter Maydell @ 2013-08-22 10:43 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Alexey Kardashevskiy, Alex Williamson, QEMU Developers
On 22 August 2013 10:48, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 22/08/2013 11:47, Peter Maydell ha scritto:
>> We can avoid relying on implementation defined
>> behaviour here by using
>> .hi = (a < 0) ? -1 : 0;
>>
>> (I know we allow ourselves to assume right-shift of signed
>> ints is arithmetic shift, but I think it's nicer to avoid it unless
>> it really makes the code better.)
>
> This is what Alexey proposed. I suggested (a >> 63) without the ?: but
> he misunderstood my (probably not clear enough) suggestion.
Yes, I found that email thread after sending this. I think
the (a < 0) variant is better than using a shift (with or without
the ?: operator).
-- PMM
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 9:48 ` Paolo Bonzini
2013-08-22 10:43 ` Peter Maydell
@ 2013-08-22 10:50 ` Alexey Kardashevskiy
2013-08-22 11:14 ` Paolo Bonzini
1 sibling, 1 reply; 13+ messages in thread
From: Alexey Kardashevskiy @ 2013-08-22 10:50 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Peter Maydell, Alex Williamson, QEMU Developers
On 08/22/2013 07:48 PM, Paolo Bonzini wrote:
> Il 22/08/2013 11:47, Peter Maydell ha scritto:
>> On 22 August 2013 10:09, Paolo Bonzini <pbonzini@redhat.com> wrote:
>>> Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
>>>> +static inline Int128 int128_exts64(int64_t a)
>>>> +{
>>>> + return (Int128) { .lo = a, .hi = (a >> 63) ? -1 : 0 };
>>>> +}
>>>
>>> The "? -1 : 0" is not necessary, but the compiler will remove it at -O1
>>> or more (interestingly, or -O0 it will remove the shift and leave the
>>> conditional!).
>>
>> We can avoid relying on implementation defined
>> behaviour here by using
>> .hi = (a < 0) ? -1 : 0;
>>
>> (I know we allow ourselves to assume right-shift of signed
>> ints is arithmetic shift, but I think it's nicer to avoid it unless
>> it really makes the code better.)
>
> This is what Alexey proposed. I suggested (a >> 63) without the ?: but
> he misunderstood my (probably not clear enough) suggestion.
Yes, I misunderstood. It was not obvious to me that (signed long
long)-1>>63 will be still -1. I really (really) envy people who can easily
read stuff like but I cannot :(
1) return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
2) return (Int128) { .lo = a, .hi = (a < 0) };
3) return (Int128) { .lo = a, .hi = a >> 63 };
So with which one should I repost the patch?
--
Alexey
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling
2013-08-22 10:41 ` Alexey Kardashevskiy
@ 2013-08-22 11:13 ` Paolo Bonzini
0 siblings, 0 replies; 13+ messages in thread
From: Paolo Bonzini @ 2013-08-22 11:13 UTC (permalink / raw)
To: Alexey Kardashevskiy; +Cc: Alex Williamson, qemu-devel
Il 22/08/2013 12:41, Alexey Kardashevskiy ha scritto:
> On 08/22/2013 07:11 PM, Paolo Bonzini wrote:
>> Il 22/08/2013 10:20, Alexey Kardashevskiy ha scritto:
>>> iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
>>> - end = (section->offset_within_address_space + int128_get64(section->size)) &
>>> - TARGET_PAGE_MASK;
>>> + llend = int128_make64(section->offset_within_address_space);
>>> + llend = int128_add(llend, section->size);
>>> + llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
>>>
>>> - if (iova >= end) {
>>> + if (int128_ge(int128_make64(iova), llend)) {
>>> return;
>>> }
>>>
>>> + end = (section->offset_within_address_space + int128_get64(section->size)) &
>>> + TARGET_PAGE_MASK;
>>> +
>>
>> This can still fail for section->size = 2^64. Do your IOMMU patches
>> take care of it?
>
> Nope. That part works for IOMMU mapped to RAM which is smaller than 2^64
> bytes and therefore I do not see why we would need 2^64 bits sizes there.
Understood. So the IOMMU patches take care of it because this code is
only used for non-IOMMU regions. Thanks,
Paolo
> Either way, I cannot test it quick (yes, I know, I should have some x86
> VFIO setup by hand as everyone has a lot of x86, etc...) so I decided to
> leave to the moment when x86 folks hit the problem :)
>
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64()
2013-08-22 10:50 ` Alexey Kardashevskiy
@ 2013-08-22 11:14 ` Paolo Bonzini
0 siblings, 0 replies; 13+ messages in thread
From: Paolo Bonzini @ 2013-08-22 11:14 UTC (permalink / raw)
To: Alexey Kardashevskiy; +Cc: Peter Maydell, Alex Williamson, QEMU Developers
Il 22/08/2013 12:50, Alexey Kardashevskiy ha scritto:
> 1) return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
> 2) return (Int128) { .lo = a, .hi = (a < 0) };
> 3) return (Int128) { .lo = a, .hi = a >> 63 };
>
> So with which one should I repost the patch?
The second would be "-(a < 0)" actually. Peter wants (1) I think, so go
for it.
Paolo
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2013-08-22 11:15 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-22 8:20 [Qemu-devel] [PATCH v2 0/3] vfio: fixes for better support for 128 bit memory section sizes Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 1/3] int128: add int128_exts64() Alexey Kardashevskiy
2013-08-22 9:09 ` Paolo Bonzini
2013-08-22 9:47 ` Peter Maydell
2013-08-22 9:48 ` Paolo Bonzini
2013-08-22 10:43 ` Peter Maydell
2013-08-22 10:50 ` Alexey Kardashevskiy
2013-08-22 11:14 ` Paolo Bonzini
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 2/3] vfio: Fix debug output for int128 values Alexey Kardashevskiy
2013-08-22 8:20 ` [Qemu-devel] [PATCH v2 3/3] vfio: Fix 128 bit handling Alexey Kardashevskiy
2013-08-22 9:11 ` Paolo Bonzini
2013-08-22 10:41 ` Alexey Kardashevskiy
2013-08-22 11:13 ` Paolo Bonzini
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).