[PATCH v3 07/33] linux-user/arm: Remove qemu_host_page_size from init_guest_commpage

Richard Henderson posted 33 patches 10 months, 4 weeks ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, Riku Voipio <riku.voipio@iki.fi>, Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Laurent Vivier <laurent@vivier.eu>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, David Hildenbrand <david@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, Nicholas Piggin <npiggin@gmail.com>, Daniel Henrique Barboza <danielhb413@gmail.com>, "Cédric Le Goater" <clg@kaod.org>, "Alex Bennée" <alex.bennee@linaro.org>, Yoshinori Sato <ysato@users.sourceforge.jp>
There is a newer version of this series
[PATCH v3 07/33] linux-user/arm: Remove qemu_host_page_size from init_guest_commpage
Posted by Richard Henderson 10 months, 4 weeks ago
Use qemu_real_host_page_size.
If the commpage is not within reserved_va, use MAP_FIXED_NOREPLACE.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/elfload.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 4fcc490ce6..2e2b1b0784 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -459,6 +459,7 @@ enum {
 static bool init_guest_commpage(void)
 {
     ARMCPU *cpu = ARM_CPU(thread_cpu);
+    int host_page_size = qemu_real_host_page_size();
     abi_ptr commpage;
     void *want;
     void *addr;
@@ -471,10 +472,12 @@ static bool init_guest_commpage(void)
         return true;
     }
 
-    commpage = HI_COMMPAGE & -qemu_host_page_size;
+    commpage = HI_COMMPAGE & -host_page_size;
     want = g2h_untagged(commpage);
-    addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
-                MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
+    addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE,
+                MAP_ANONYMOUS | MAP_PRIVATE |
+                (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE),
+                -1, 0);
 
     if (addr == MAP_FAILED) {
         perror("Allocating guest commpage");
@@ -487,12 +490,12 @@ static bool init_guest_commpage(void)
     /* Set kernel helper versions; rest of page is 0.  */
     __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
 
-    if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
+    if (mprotect(addr, host_page_size, PROT_READ)) {
         perror("Protecting guest commpage");
         exit(EXIT_FAILURE);
     }
 
-    page_set_flags(commpage, commpage | ~qemu_host_page_mask,
+    page_set_flags(commpage, commpage | (host_page_size - 1),
                    PAGE_READ | PAGE_EXEC | PAGE_VALID);
     return true;
 }
-- 
2.34.1
Re: [PATCH v3 07/33] linux-user/arm: Remove qemu_host_page_size from init_guest_commpage
Posted by Pierrick Bouvier 10 months, 3 weeks ago
On 1/2/24 05:57, Richard Henderson wrote:
> Use qemu_real_host_page_size.
> If the commpage is not within reserved_va, use MAP_FIXED_NOREPLACE.
> 
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   linux-user/elfload.c | 13 ++++++++-----
>   1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
> index 4fcc490ce6..2e2b1b0784 100644
> --- a/linux-user/elfload.c
> +++ b/linux-user/elfload.c
> @@ -459,6 +459,7 @@ enum {
>   static bool init_guest_commpage(void)
>   {
>       ARMCPU *cpu = ARM_CPU(thread_cpu);
> +    int host_page_size = qemu_real_host_page_size();
>       abi_ptr commpage;
>       void *want;
>       void *addr;
> @@ -471,10 +472,12 @@ static bool init_guest_commpage(void)
>           return true;
>       }
>   
> -    commpage = HI_COMMPAGE & -qemu_host_page_size;
> +    commpage = HI_COMMPAGE & -host_page_size;
>       want = g2h_untagged(commpage);
> -    addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
> -                MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
> +    addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE,
> +                MAP_ANONYMOUS | MAP_PRIVATE |
> +                (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE),
> +                -1, 0);
>   
>       if (addr == MAP_FAILED) {
>           perror("Allocating guest commpage");
> @@ -487,12 +490,12 @@ static bool init_guest_commpage(void)
>       /* Set kernel helper versions; rest of page is 0.  */
>       __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
>   
> -    if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
> +    if (mprotect(addr, host_page_size, PROT_READ)) {
>           perror("Protecting guest commpage");
>           exit(EXIT_FAILURE);
>       }
>   
> -    page_set_flags(commpage, commpage | ~qemu_host_page_mask,
> +    page_set_flags(commpage, commpage | (host_page_size - 1),
>                      PAGE_READ | PAGE_EXEC | PAGE_VALID);
>       return true;
>   }

To confirm if I understand correctly, when using a reserved va, the 
contiguous address space is reserved using mmap, thus 
MAP_FIXED_NOREPLACE would fail when hitting it?

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Re: [PATCH v3 07/33] linux-user/arm: Remove qemu_host_page_size from init_guest_commpage
Posted by Richard Henderson 10 months, 2 weeks ago
On 1/8/24 20:38, Pierrick Bouvier wrote:
> On 1/2/24 05:57, Richard Henderson wrote:
>> Use qemu_real_host_page_size.
>> If the commpage is not within reserved_va, use MAP_FIXED_NOREPLACE.
>>
>> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
>> ---
>>   linux-user/elfload.c | 13 ++++++++-----
>>   1 file changed, 8 insertions(+), 5 deletions(-)
>>
>> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
>> index 4fcc490ce6..2e2b1b0784 100644
>> --- a/linux-user/elfload.c
>> +++ b/linux-user/elfload.c
>> @@ -459,6 +459,7 @@ enum {
>>   static bool init_guest_commpage(void)
>>   {
>>       ARMCPU *cpu = ARM_CPU(thread_cpu);
>> +    int host_page_size = qemu_real_host_page_size();
>>       abi_ptr commpage;
>>       void *want;
>>       void *addr;
>> @@ -471,10 +472,12 @@ static bool init_guest_commpage(void)
>>           return true;
>>       }
>> -    commpage = HI_COMMPAGE & -qemu_host_page_size;
>> +    commpage = HI_COMMPAGE & -host_page_size;
>>       want = g2h_untagged(commpage);
>> -    addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
>> -                MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
>> +    addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE,
>> +                MAP_ANONYMOUS | MAP_PRIVATE |
>> +                (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE),
>> +                -1, 0);
>>       if (addr == MAP_FAILED) {
>>           perror("Allocating guest commpage");
>> @@ -487,12 +490,12 @@ static bool init_guest_commpage(void)
>>       /* Set kernel helper versions; rest of page is 0.  */
>>       __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu));
>> -    if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
>> +    if (mprotect(addr, host_page_size, PROT_READ)) {
>>           perror("Protecting guest commpage");
>>           exit(EXIT_FAILURE);
>>       }
>> -    page_set_flags(commpage, commpage | ~qemu_host_page_mask,
>> +    page_set_flags(commpage, commpage | (host_page_size - 1),
>>                      PAGE_READ | PAGE_EXEC | PAGE_VALID);
>>       return true;
>>   }
> 
> To confirm if I understand correctly, when using a reserved va, the contiguous address 
> space is reserved using mmap, thus MAP_FIXED_NOREPLACE would fail when hitting it?

Correct.

r~