Skip to content

Commit

Permalink
futex2: Implement vectorized wait
Browse files Browse the repository at this point in the history
Add support to wait on multiple futexes. This is the interface
implemented by this syscall:

futex_waitv(struct futex_waitv *waiters, unsigned int nr_futexes,
	    unsigned int flags, struct timespec *timo)

struct futex_waitv {
	void *uaddr;
	unsigned int val;
	unsigned int flags;
};

Given an array of struct futex_waitv, wait on each uaddr. The thread
wakes if a futex_wake() is performed at any uaddr. The syscall returns
immediately if any waiter has *uaddr != val. *timo is an optional
timeout value for the operation. The flags argument of the syscall
should be used solely for specifying the timeout as realtime, if needed.
Flags for shared futexes, sizes, etc. should be used on the individual
flags of each waiter.

Returns the array index of one of the awakened futexes. There’s no given
information of how many were awakened, or any particular attribute of it
(if it’s the first awakened, if it is of the smaller index...).

Signed-off-by: André Almeida <andrealmeid@collabora.com>

Rebased-by: Joshua Ashton <joshua@froggi.es>
  • Loading branch information
andrealmeid authored and xanmod committed Jun 29, 2021
1 parent b5384dd commit 59c86f9
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 3 deletions.
1 change: 1 addition & 0 deletions arch/arm/tools/syscall.tbl
Expand Up @@ -462,3 +462,4 @@
446 common landlock_restrict_self sys_landlock_restrict_self
447 common futex_wait sys_futex_wait
448 common futex_wake sys_futex_wake
449 common futex_waitv sys_futex_waitv
2 changes: 1 addition & 1 deletion arch/arm64/include/asm/unistd.h
Expand Up @@ -38,7 +38,7 @@
#define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)
#define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800)

#define __NR_compat_syscalls 449
#define __NR_compat_syscalls 450
#endif

#define __ARCH_WANT_SYS_CLONE
Expand Down
1 change: 1 addition & 0 deletions arch/x86/entry/syscalls/syscall_32.tbl
Expand Up @@ -453,3 +453,4 @@
446 i386 landlock_restrict_self sys_landlock_restrict_self
447 i386 futex_wait sys_futex_wait
448 i386 futex_wake sys_futex_wake
449 i386 futex_waitv sys_futex_waitv compat_sys_futex_waitv
1 change: 1 addition & 0 deletions arch/x86/entry/syscalls/syscall_64.tbl
Expand Up @@ -370,6 +370,7 @@
446 common landlock_restrict_self sys_landlock_restrict_self
447 common futex_wait sys_futex_wait
448 common futex_wake sys_futex_wake
449 common futex_waitv sys_futex_waitv

#
# Due to a historical design error, certain syscalls are numbered differently
Expand Down
11 changes: 11 additions & 0 deletions include/linux/compat.h
Expand Up @@ -368,6 +368,12 @@ struct compat_robust_list_head {
compat_uptr_t list_op_pending;
};

struct compat_futex_waitv {
compat_uptr_t uaddr;
compat_uint_t val;
compat_uint_t flags;
};

#ifdef CONFIG_COMPAT_OLD_SIGACTION
struct compat_old_sigaction {
compat_uptr_t sa_handler;
Expand Down Expand Up @@ -692,6 +698,11 @@ asmlinkage long
compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
compat_size_t __user *len_ptr);

/* kernel/futex2.c */
asmlinkage long compat_sys_futex_waitv(struct compat_futex_waitv *waiters,
compat_uint_t nr_futexes, compat_uint_t flags,
struct __kernel_timespec __user *timo);

/* kernel/itimer.c */
asmlinkage long compat_sys_getitimer(int which,
struct old_itimerval32 __user *it);
Expand Down
4 changes: 4 additions & 0 deletions include/linux/syscalls.h
Expand Up @@ -71,6 +71,7 @@ struct open_how;
struct mount_attr;
struct landlock_ruleset_attr;
enum landlock_rule_type;
struct futex_waitv;

#include <linux/types.h>
#include <linux/aio_abi.h>
Expand Down Expand Up @@ -629,6 +630,9 @@ asmlinkage long sys_futex_wait(void __user *uaddr, unsigned int val,
struct __kernel_timespec __user *timo);
asmlinkage long sys_futex_wake(void __user *uaddr, unsigned int nr_wake,
unsigned int flags);
asmlinkage long sys_futex_waitv(struct futex_waitv __user *waiters,
unsigned int nr_futexes, unsigned int flags,
struct __kernel_timespec __user *timo);

/* kernel/hrtimer.c */
asmlinkage long sys_nanosleep(struct __kernel_timespec __user *rqtp,
Expand Down
5 changes: 4 additions & 1 deletion include/uapi/asm-generic/unistd.h
Expand Up @@ -878,8 +878,11 @@ __SYSCALL(__NR_futex_wait, sys_futex_wait)
#define __NR_futex_wake 444
__SYSCALL(__NR_futex_wake, sys_futex_wake)

#define __NR_futex_waitv 445
__SC_COMP(__NR_futex_waitv, sys_futex_waitv, compat_sys_futex_waitv)

#undef __NR_syscalls
#define __NR_syscalls 449
#define __NR_syscalls 450

/*
* 32 bit systems traditionally used different
Expand Down
14 changes: 14 additions & 0 deletions include/uapi/linux/futex.h
Expand Up @@ -48,6 +48,20 @@

#define FUTEX_SHARED_FLAG 8

#define FUTEX_WAITV_MAX 128

/**
* struct futex_waitv - A waiter for vectorized wait
* @uaddr: User address to wait on
* @val: Expected value at uaddr
* @flags: Flags for this waiter
*/
struct futex_waitv {
void __user *uaddr;
unsigned int val;
unsigned int flags;
};

/*
* Support for robust futexes: the kernel cleans up held futexes at
* thread exit time.
Expand Down
177 changes: 177 additions & 0 deletions kernel/futex2.c
Expand Up @@ -83,6 +83,12 @@ struct futex_bucket {
/* Mask for futex2 flag operations */
#define FUTEX2_MASK (FUTEX_SIZE_MASK | FUTEX_CLOCK_REALTIME | FUTEX_SHARED_FLAG)

/* Mask for sys_futex_waitv flag */
#define FUTEXV_MASK (FUTEX_CLOCK_REALTIME)

/* Mask for each futex in futex_waitv list */
#define FUTEXV_WAITER_MASK (FUTEX_SIZE_MASK | FUTEX_SHARED_FLAG)

#define is_object_shared ((futexv->objects[i].flags & FUTEX_SHARED_FLAG) ? true : false)

#define FUT_OFF_INODE 1 /* We set bit 0 if key has a reference on inode */
Expand Down Expand Up @@ -704,6 +710,177 @@ SYSCALL_DEFINE4(futex_wait, void __user *, uaddr, unsigned int, val,
return futex_set_timer_and_wait(futexv, 1, timo, flags);
}

#ifdef CONFIG_COMPAT
/**
* compat_futex_parse_waitv - Parse a waitv array from userspace
* @futexv: Kernel side list of waiters to be filled
* @uwaitv: Userspace list to be parsed
* @nr_futexes: Length of futexv
*
* Return: Error code on failure, pointer to a prepared futexv otherwise
*/
static int compat_futex_parse_waitv(struct futex_waiter_head *futexv,
struct compat_futex_waitv __user *uwaitv,
unsigned int nr_futexes)
{
struct futex_bucket *bucket;
struct compat_futex_waitv waitv;
unsigned int i;

for (i = 0; i < nr_futexes; i++) {
if (copy_from_user(&waitv, &uwaitv[i], sizeof(waitv)))
return -EFAULT;

if ((waitv.flags & ~FUTEXV_WAITER_MASK) ||
(waitv.flags & FUTEX_SIZE_MASK) != FUTEX_32)
return -EINVAL;

futexv->objects[i].key.pointer = 0;
futexv->objects[i].flags = waitv.flags;
futexv->objects[i].uaddr = compat_ptr(waitv.uaddr);
futexv->objects[i].val = waitv.val;
futexv->objects[i].index = i;

bucket = futex_get_bucket(compat_ptr(waitv.uaddr),
&futexv->objects[i].key,
is_object_shared);

if (IS_ERR(bucket))
return PTR_ERR(bucket);

futexv->objects[i].bucket = bucket;

INIT_LIST_HEAD(&futexv->objects[i].list);
}

return 0;
}

COMPAT_SYSCALL_DEFINE4(futex_waitv, struct compat_futex_waitv __user *, waiters,
unsigned int, nr_futexes, unsigned int, flags,
struct __kernel_timespec __user *, timo)
{
struct futex_waiter_head *futexv;
int ret;

if (flags & ~FUTEXV_MASK)
return -EINVAL;

if (!nr_futexes || nr_futexes > FUTEX_WAITV_MAX || !waiters)
return -EINVAL;

futexv = kmalloc((sizeof(struct futex_waiter) * nr_futexes) +
sizeof(*futexv), GFP_KERNEL);
if (!futexv)
return -ENOMEM;

futexv->hint = false;
futexv->task = current;

ret = compat_futex_parse_waitv(futexv, waiters, nr_futexes);

if (!ret)
ret = futex_set_timer_and_wait(futexv, nr_futexes, timo, flags);

kfree(futexv);

return ret;
}
#endif

/**
* futex_parse_waitv - Parse a waitv array from userspace
* @futexv: Kernel side list of waiters to be filled
* @uwaitv: Userspace list to be parsed
* @nr_futexes: Length of futexv
*
* Return: Error code on failure, pointer to a prepared futexv otherwise
*/
static int futex_parse_waitv(struct futex_waiter_head *futexv,
struct futex_waitv __user *uwaitv,
unsigned int nr_futexes)
{
struct futex_bucket *bucket;
struct futex_waitv waitv;
unsigned int i;

for (i = 0; i < nr_futexes; i++) {
if (copy_from_user(&waitv, &uwaitv[i], sizeof(waitv)))
return -EFAULT;

if ((waitv.flags & ~FUTEXV_WAITER_MASK) ||
(waitv.flags & FUTEX_SIZE_MASK) != FUTEX_32)
return -EINVAL;

futexv->objects[i].key.pointer = 0;
futexv->objects[i].flags = waitv.flags;
futexv->objects[i].uaddr = waitv.uaddr;
futexv->objects[i].val = waitv.val;
futexv->objects[i].index = i;

bucket = futex_get_bucket(waitv.uaddr, &futexv->objects[i].key,
is_object_shared);

if (IS_ERR(bucket))
return PTR_ERR(bucket);

futexv->objects[i].bucket = bucket;

INIT_LIST_HEAD(&futexv->objects[i].list);
}

return 0;
}

/**
* sys_futex_waitv - Wait on a list of futexes
* @waiters: List of futexes to wait on
* @nr_futexes: Length of futexv
* @flags: Flag for timeout (monotonic/realtime)
* @timo: Optional absolute timeout.
*
* Given an array of `struct futex_waitv`, wait on each uaddr. The thread wakes
* if a futex_wake() is performed at any uaddr. The syscall returns immediately
* if any waiter has *uaddr != val. *timo is an optional timeout value for the
* operation. Each waiter has individual flags. The `flags` argument for the
* syscall should be used solely for specifying the timeout as realtime, if
* needed. Flags for shared futexes, sizes, etc. should be used on the
* individual flags of each waiter.
*
* Returns the array index of one of the awaken futexes. There's no given
* information of how many were awakened, or any particular attribute of it (if
* it's the first awakened, if it is of the smaller index...).
*/
SYSCALL_DEFINE4(futex_waitv, struct futex_waitv __user *, waiters,
unsigned int, nr_futexes, unsigned int, flags,
struct __kernel_timespec __user *, timo)
{
struct futex_waiter_head *futexv;
int ret;

if (flags & ~FUTEXV_MASK)
return -EINVAL;

if (!nr_futexes || nr_futexes > FUTEX_WAITV_MAX || !waiters)
return -EINVAL;

futexv = kmalloc((sizeof(struct futex_waiter) * nr_futexes) +
sizeof(*futexv), GFP_KERNEL);
if (!futexv)
return -ENOMEM;

futexv->hint = false;
futexv->task = current;

ret = futex_parse_waitv(futexv, waiters, nr_futexes);
if (!ret)
ret = futex_set_timer_and_wait(futexv, nr_futexes, timo, flags);

kfree(futexv);

return ret;
}

/**
* futex_get_parent - For a given futex in a futexv list, get a pointer to the futexv
* @waiter: Address of futex in the list
Expand Down
1 change: 1 addition & 0 deletions kernel/sys_ni.c
Expand Up @@ -154,6 +154,7 @@ COND_SYSCALL_COMPAT(get_robust_list);
/* kernel/futex2.c */
COND_SYSCALL(futex_wait);
COND_SYSCALL(futex_wake);
COND_SYSCALL(futex_waitv);

/* kernel/hrtimer.c */

Expand Down
5 changes: 4 additions & 1 deletion tools/include/uapi/asm-generic/unistd.h
Expand Up @@ -878,8 +878,11 @@ __SYSCALL(__NR_futex_wait, sys_futex_wait)
#define __NR_futex_wake 444
__SYSCALL(__NR_futex_wake, sys_futex_wake)

#define __NR_futex_waitv 445
__SC_COMP(__NR_futex_waitv, sys_futex_waitv, compat_sys_futex_waitv)

#undef __NR_syscalls
#define __NR_syscalls 449
#define __NR_syscalls 450

/*
* 32 bit systems traditionally used different
Expand Down
1 change: 1 addition & 0 deletions tools/perf/arch/x86/entry/syscalls/syscall_64.tbl
Expand Up @@ -370,6 +370,7 @@
446 common landlock_restrict_self sys_landlock_restrict_self
447 common futex_wait sys_futex_wait
448 common futex_wake sys_futex_wake
449 common futex_waitv sys_futex_waitv

#
# Due to a historical design error, certain syscalls are numbered differently
Expand Down

0 comments on commit 59c86f9

Please sign in to comment.