[PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram

Alexander Graf posted 1 patch 2 years, 6 months ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20211025082558.96864-1-agraf@csgraf.de
Maintainers: Roman Bolshakov <r.bolshakov@yadro.com>, Cameron Esfahani <dirty@apple.com>
There is a newer version of this series
accel/hvf/hvf-accel-ops.c | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram
Posted by Alexander Graf 2 years, 6 months ago
HVF has generic memory listener code that adds all RAM regions as HVF RAM
regions. However, HVF can only handle page aligned, page granule regions.

So let's ignore regions that are not page aligned and sized. They will be
trapped as MMIO instead.

Signed-off-by: Alexander Graf <agraf@csgraf.de>
---
 accel/hvf/hvf-accel-ops.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c
index 6bf319d34c..090155853a 100644
--- a/accel/hvf/hvf-accel-ops.c
+++ b/accel/hvf/hvf-accel-ops.c
@@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
         }
     }
 
+    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
+        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
+        /* Not page aligned, so we can not map as RAM */
+        add = false;
+    }
+
     mem = hvf_find_overlap_slot(
             section->offset_within_address_space,
             int128_get64(section->size));
-- 
2.30.1 (Apple Git-130)


Re: [PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram
Posted by Paolo Bonzini 2 years, 6 months ago
On 25/10/21 10:25, Alexander Graf wrote:
> HVF has generic memory listener code that adds all RAM regions as HVF RAM
> regions. However, HVF can only handle page aligned, page granule regions.
> 
> So let's ignore regions that are not page aligned and sized. They will be
> trapped as MMIO instead.
> 
> Signed-off-by: Alexander Graf <agraf@csgraf.de>
> ---
>   accel/hvf/hvf-accel-ops.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c
> index 6bf319d34c..090155853a 100644
> --- a/accel/hvf/hvf-accel-ops.c
> +++ b/accel/hvf/hvf-accel-ops.c
> @@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
>           }
>       }
>   
> +    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
> +        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
> +        /* Not page aligned, so we can not map as RAM */
> +        add = false;
> +    }
> +
>       mem = hvf_find_overlap_slot(
>               section->offset_within_address_space,
>               int128_get64(section->size));
> 

Queued, thanks.

Paolo


Re: [PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram
Posted by Alexander Graf 2 years, 6 months ago
On 25.10.21 19:11, Paolo Bonzini wrote:
> On 25/10/21 10:25, Alexander Graf wrote:
>> HVF has generic memory listener code that adds all RAM regions as HVF 
>> RAM
>> regions. However, HVF can only handle page aligned, page granule 
>> regions.
>>
>> So let's ignore regions that are not page aligned and sized. They 
>> will be
>> trapped as MMIO instead.
>>
>> Signed-off-by: Alexander Graf <agraf@csgraf.de>
>> ---
>>   accel/hvf/hvf-accel-ops.c | 6 ++++++
>>   1 file changed, 6 insertions(+)
>>
>> diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c
>> index 6bf319d34c..090155853a 100644
>> --- a/accel/hvf/hvf-accel-ops.c
>> +++ b/accel/hvf/hvf-accel-ops.c
>> @@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection 
>> *section, bool add)
>>           }
>>       }
>>   +    if (int128_get64(section->size) & (qemu_real_host_page_size - 
>> 1) ||
>> +        section->offset_within_address_space & 
>> (qemu_real_host_page_size - 1)) {
>> +        /* Not page aligned, so we can not map as RAM */
>> +        add = false;
>> +    }
>> +
>>       mem = hvf_find_overlap_slot(
>>               section->offset_within_address_space,
>>               int128_get64(section->size));
>>
>
> Queued, thanks.


You probably want v2 instead :)

Alex



Re: [PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram
Posted by Paolo Bonzini 2 years, 6 months ago
On 25/10/21 21:10, Alexander Graf wrote:
>>>           }
>>>       }
>>>   +    if (int128_get64(section->size) & (qemu_real_host_page_size - 
>>> 1) ||
>>> +        section->offset_within_address_space & 
>>> (qemu_real_host_page_size - 1)) {
>>> +        /* Not page aligned, so we can not map as RAM */
>>> +        add = false;
>>> +    }
>>> +
>>>       mem = hvf_find_overlap_slot(
>>>               section->offset_within_address_space,
>>>               int128_get64(section->size));
>>>
>>
>> Queued, thanks.
> 
> 
> You probably want v2 instead :)

That's actually what I had applied. :)

Paolo


Re: [PATCH] hvf: Avoid mapping regions < PAGE_SIZE as ram
Posted by Philippe Mathieu-Daudé 2 years, 6 months ago
Hi Alex,

On 10/25/21 10:25, Alexander Graf wrote:
> HVF has generic memory listener code that adds all RAM regions as HVF RAM
> regions. However, HVF can only handle page aligned, page granule regions.
> 
> So let's ignore regions that are not page aligned and sized. They will be
> trapped as MMIO instead.
> 
> Signed-off-by: Alexander Graf <agraf@csgraf.de>
> ---
>  accel/hvf/hvf-accel-ops.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c
> index 6bf319d34c..090155853a 100644
> --- a/accel/hvf/hvf-accel-ops.c
> +++ b/accel/hvf/hvf-accel-ops.c
> @@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
>          }
>      }
>  
> +    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
> +        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {

Could we use QEMU_IS_ALIGNED() instead?

> +        /* Not page aligned, so we can not map as RAM */
> +        add = false;
> +    }
> +
>      mem = hvf_find_overlap_slot(
>              section->offset_within_address_space,
>              int128_get64(section->size));
>