On Thu, Feb 5, 2026 at 6:47 PM Richard Henderson <
[email protected]> wrote:

> On 2/6/26 03:26, Warner Losh wrote:
> > From: Stacey Son <[email protected]>
> >
> > Add host_to_target_semarray() to convert host semaphore array to target
> > format for semctl(2) GETALL operations.
> >
> > Signed-off-by: Stacey Son <[email protected]>
> > Signed-off-by: Warner Losh <[email protected]>
> > ---
> >   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~
>
>

Reply via email to