diff mbox series

[v3,2/3] bsd-user: Propagate alignment argument to mmap_find_vma()

Message ID 20250307130951.4816-3-philmd@linaro.org
State New
Headers show
Series user: Extract common MMAP API to 'user/mmap.h' | expand

Commit Message

Philippe Mathieu-Daudé March 7, 2025, 1:09 p.m. UTC
Propagate the alignment to mmap_find_vma(), effectively
embedding mmap_find_vma_aligned() within mmap_find_vma().

Since we ignore the alignment in do_bsd_shmat(), leave a
FIXME comment.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 bsd-user/bsd-mem.h |  2 +-
 bsd-user/qemu.h    |  2 +-
 bsd-user/mmap.c    | 10 ++--------
 3 files changed, 4 insertions(+), 10 deletions(-)

Comments

Richard Henderson March 7, 2025, 4:08 p.m. UTC | #1
On 3/7/25 05:09, Philippe Mathieu-Daudé wrote:
> Propagate the alignment to mmap_find_vma(), effectively
> embedding mmap_find_vma_aligned() within mmap_find_vma().
> 
> Since we ignore the alignment in do_bsd_shmat(), leave a
> FIXME comment.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   bsd-user/bsd-mem.h |  2 +-
>   bsd-user/qemu.h    |  2 +-
>   bsd-user/mmap.c    | 10 ++--------
>   3 files changed, 4 insertions(+), 10 deletions(-)
> 
> diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
> index f5ec0de24ca..87219da2919 100644
> --- a/bsd-user/bsd-mem.h
> +++ b/bsd-user/bsd-mem.h
> @@ -372,7 +372,7 @@ static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg)
>           } else {
>               abi_ulong mmap_start;
>   
> -            mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
> +            mmap_start = mmap_find_vma(0, shm_info.shm_segsz, 0 /* FIXME??? */);

It's not really ignoring the alignment, but not requiring alignment above page size.

Traditionally, the alignment for shmat should be SHMLBA.
But in current freebsd sources,

sys/sys/shm.h:#define SHMLBA      PAGE_SIZE /* Segment low boundary address multiple */

there are no crazy broken old architectures to worry about.


r~
Philippe Mathieu-Daudé March 7, 2025, 4:17 p.m. UTC | #2
On 7/3/25 17:08, Richard Henderson wrote:
> On 3/7/25 05:09, Philippe Mathieu-Daudé wrote:
>> Propagate the alignment to mmap_find_vma(), effectively
>> embedding mmap_find_vma_aligned() within mmap_find_vma().
>>
>> Since we ignore the alignment in do_bsd_shmat(), leave a
>> FIXME comment.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   bsd-user/bsd-mem.h |  2 +-
>>   bsd-user/qemu.h    |  2 +-
>>   bsd-user/mmap.c    | 10 ++--------
>>   3 files changed, 4 insertions(+), 10 deletions(-)
>>
>> diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
>> index f5ec0de24ca..87219da2919 100644
>> --- a/bsd-user/bsd-mem.h
>> +++ b/bsd-user/bsd-mem.h
>> @@ -372,7 +372,7 @@ static inline abi_long do_bsd_shmat(int shmid, 
>> abi_ulong shmaddr, int shmflg)
>>           } else {
>>               abi_ulong mmap_start;
>> -            mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
>> +            mmap_start = mmap_find_vma(0, shm_info.shm_segsz, 0 /* 
>> FIXME??? */);
> 
> It's not really ignoring the alignment, but not requiring alignment 
> above page size.
> 
> Traditionally, the alignment for shmat should be SHMLBA.
> But in current freebsd sources,
> 
> sys/sys/shm.h:#define SHMLBA      PAGE_SIZE /* Segment low boundary 
> address multiple */
> 
> there are no crazy broken old architectures to worry about.

OK, thank you for checking!
Warner Losh March 9, 2025, 11:20 p.m. UTC | #3
On Fri, Mar 7, 2025 at 6:10 AM Philippe Mathieu-Daudé <philmd@linaro.org>
wrote:

> Propagate the alignment to mmap_find_vma(), effectively
> embedding mmap_find_vma_aligned() within mmap_find_vma().
>
> Since we ignore the alignment in do_bsd_shmat(), leave a
> FIXME comment.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  bsd-user/bsd-mem.h |  2 +-
>  bsd-user/qemu.h    |  2 +-
>  bsd-user/mmap.c    | 10 ++--------
>  3 files changed, 4 insertions(+), 10 deletions(-)
>

Reviewed-by: Warner Losh <imp@bsdimp.com>
diff mbox series

Patch

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index f5ec0de24ca..87219da2919 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -372,7 +372,7 @@  static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg)
         } else {
             abi_ulong mmap_start;
 
-            mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
+            mmap_start = mmap_find_vma(0, shm_info.shm_segsz, 0 /* FIXME??? */);
 
             if (mmap_start == -1) {
                 return -TARGET_ENOMEM;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 4e97c796318..0b3bd65b180 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -242,7 +242,7 @@  abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
                        abi_ulong new_addr);
 int target_msync(abi_ulong start, abi_ulong len, int flags);
 extern abi_ulong mmap_next_start;
-abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
+abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong alignment);
 void mmap_reserve(abi_ulong start, abi_ulong size);
 void TSA_NO_TSA mmap_fork_start(void);
 void TSA_NO_TSA mmap_fork_end(int child);
diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 28d7e387a20..da22fcc7c41 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -275,8 +275,7 @@  static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
  * It must be called with mmap_lock() held.
  * Return -1 if error.
  */
-static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
-                                       abi_ulong alignment)
+abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong alignment)
 {
     void *ptr, *prev;
     abi_ulong addr;
@@ -395,11 +394,6 @@  static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
     }
 }
 
-abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
-{
-    return mmap_find_vma_aligned(start, size, 0);
-}
-
 /* NOTE: all the constants are the HOST ones */
 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
                      int flags, int fd, off_t offset)
@@ -496,7 +490,7 @@  abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
         if ((flags & MAP_ALIGNMENT_MASK) != 0) {
             alignment = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
         }
-        start = mmap_find_vma_aligned(real_start, host_len, alignment);
+        start = mmap_find_vma(real_start, host_len, alignment);
         if (start == (abi_ulong)-1) {
             errno = ENOMEM;
             goto fail;