[PATCH 08/24] bsd-user: Add host_to_target_semarray for semaphore operations

Warner Losh posted 24 patches 3 days, 17 hours ago
Maintainers: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Riku Voipio <riku.voipio@iki.fi>, Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
There is a newer version of this series
[PATCH 08/24] bsd-user: Add host_to_target_semarray for semaphore operations
Posted by Warner Losh 3 days, 17 hours ago
From: Stacey Son <sson@FreeBSD.org>

Add host_to_target_semarray() to convert host semaphore array to target
format for semctl(2) GETALL operations.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/bsd-misc.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
index 07d8bf1304..7bdf65450a 100644
--- a/bsd-user/bsd-misc.c
+++ b/bsd-user/bsd-misc.c
@@ -79,3 +79,35 @@ abi_long target_to_host_semarray(int semid, unsigned short **host_array,
 
     return 0;
 }
+
+abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
+        unsigned short **host_array)
+{
+    abi_long ret;
+    int nsems, i;
+    unsigned short *array;
+    union semun semun;
+    struct semid_ds semid_ds;
+
+    semun.buf = &semid_ds;
+
+    ret = semctl(semid, 0, IPC_STAT, semun);
+    if (ret == -1) {
+        free(*host_array);
+        return get_errno(ret);
+    }
+
+    nsems = semid_ds.sem_nsems;
+    array = (unsigned short *)lock_user(VERIFY_WRITE, target_addr,
+        nsems * sizeof(unsigned short), 0);
+    if (array == NULL) {
+        free(*host_array);
+        return -TARGET_EFAULT;
+    }
+    for (i = 0; i < nsems; i++) {
+        array[i] = (*host_array)[i];
+    }
+    free(*host_array);
+    unlock_user(array, target_addr, 1);
+    return 0;
+}

-- 
2.52.0
Re: [PATCH 08/24] bsd-user: Add host_to_target_semarray for semaphore operations
Posted by Richard Henderson 3 days, 8 hours ago
On 2/6/26 03:26, Warner Losh wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Add host_to_target_semarray() to convert host semaphore array to target
> format for semctl(2) GETALL operations.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/bsd-misc.c | 32 ++++++++++++++++++++++++++++++++
>   1 file changed, 32 insertions(+)
> 
> diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> index 07d8bf1304..7bdf65450a 100644
> --- a/bsd-user/bsd-misc.c
> +++ b/bsd-user/bsd-misc.c
> @@ -79,3 +79,35 @@ abi_long target_to_host_semarray(int semid, unsigned short **host_array,
>   
>       return 0;
>   }
> +
> +abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
> +        unsigned short **host_array)
> +{
> +    abi_long ret;
> +    int nsems, i;
> +    unsigned short *array;
> +    union semun semun;
> +    struct semid_ds semid_ds;
> +
> +    semun.buf = &semid_ds;
> +
> +    ret = semctl(semid, 0, IPC_STAT, semun);
> +    if (ret == -1) {
> +        free(*host_array);
> +        return get_errno(ret);
> +    }
> +
> +    nsems = semid_ds.sem_nsems;
> +    array = (unsigned short *)lock_user(VERIFY_WRITE, target_addr,
> +        nsems * sizeof(unsigned short), 0);
> +    if (array == NULL) {
> +        free(*host_array);
> +        return -TARGET_EFAULT;
> +    }
> +    for (i = 0; i < nsems; i++) {
> +        array[i] = (*host_array)[i];
> +    }
> +    free(*host_array);
> +    unlock_user(array, target_addr, 1);
> +    return 0;
> +}
> 

You can simplify this a bit:

...	unsigned short **host_arrayp)
{
     g_autofree unsigned short *host_array = *host_arrayp;

which now takes care of releasing the memory across all return paths.
You also don't need double-indirection in the loop.

You do need __put_user (or tswap16) in the loop for endianness.


r~
Re: [PATCH 08/24] bsd-user: Add host_to_target_semarray for semaphore operations
Posted by Warner Losh 3 days, 7 hours ago
On Thu, Feb 5, 2026 at 6:47 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/6/26 03:26, Warner Losh wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Add host_to_target_semarray() to convert host semaphore array to target
> > format for semctl(2) GETALL operations.
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >   bsd-user/bsd-misc.c | 32 ++++++++++++++++++++++++++++++++
> >   1 file changed, 32 insertions(+)
> >
> > diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> > index 07d8bf1304..7bdf65450a 100644
> > --- a/bsd-user/bsd-misc.c
> > +++ b/bsd-user/bsd-misc.c
> > @@ -79,3 +79,35 @@ abi_long target_to_host_semarray(int semid, unsigned
> short **host_array,
> >
> >       return 0;
> >   }
> > +
> > +abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
> > +        unsigned short **host_array)
> > +{
> > +    abi_long ret;
> > +    int nsems, i;
> > +    unsigned short *array;
> > +    union semun semun;
> > +    struct semid_ds semid_ds;
> > +
> > +    semun.buf = &semid_ds;
> > +
> > +    ret = semctl(semid, 0, IPC_STAT, semun);
> > +    if (ret == -1) {
> > +        free(*host_array);
> > +        return get_errno(ret);
> > +    }
> > +
> > +    nsems = semid_ds.sem_nsems;
> > +    array = (unsigned short *)lock_user(VERIFY_WRITE, target_addr,
> > +        nsems * sizeof(unsigned short), 0);
> > +    if (array == NULL) {
> > +        free(*host_array);
> > +        return -TARGET_EFAULT;
> > +    }
> > +    for (i = 0; i < nsems; i++) {
> > +        array[i] = (*host_array)[i];
> > +    }
> > +    free(*host_array);
> > +    unlock_user(array, target_addr, 1);
> > +    return 0;
> > +}
> >
>
> You can simplify this a bit:
>
> ...     unsigned short **host_arrayp)
> {
>      g_autofree unsigned short *host_array = *host_arrayp;
>
> which now takes care of releasing the memory across all return paths.
> You also don't need double-indirection in the loop.
>
> You do need __put_user (or tswap16) in the loop for endianness.
>

Right you are. Thanks! Will fix in v2

Warner


>
> r~
>
>