Skip to content

Commit bf69bad

Browse files
andrealmeidPeter Zijlstra
authored andcommitted
futex: Implement sys_futex_waitv()
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 *timeout, clockid_t clockid) struct futex_waitv { __u64 val; __u64 uaddr; __u32 flags; __u32 __reserved; }; 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. *timeout is an optional absolute timeout value for the operation. This syscall supports only 64bit sized timeout structs. The flags argument of the syscall should be empty, but it can be used for future extensions. Flags for shared futexes, sizes, etc. should be used on the individual flags of each waiter. __reserved is used for explicit padding and should be 0, but it might be used for future extensions. If the userspace uses 32-bit pointers, it should make sure to explicitly cast it when assigning to waitv::uaddr. Returns the array index of one of the woken futexes. There’s no given information of how many were woken, or any particular attribute of it (if it’s the first woken, if it is of the smaller index...). Signed-off-by: André Almeida <andrealmeid@collabora.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20210923171111.300673-17-andrealmeid@collabora.com
1 parent bff7c57 commit bf69bad

8 files changed

Lines changed: 371 additions & 1 deletion

File tree

‎MAINTAINERS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7718,6 +7718,7 @@ M: Ingo Molnar <mingo@redhat.com>
77187718
R: Peter Zijlstra <peterz@infradead.org>
77197719
R: Darren Hart <dvhart@infradead.org>
77207720
R: Davidlohr Bueso <dave@stgolabs.net>
7721+
R: André Almeida <andrealmeid@collabora.com>
77217722
L: linux-kernel@vger.kernel.org
77227723
S: Maintained
77237724
T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git locking/core

‎include/linux/syscalls.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct mq_attr;
5858
struct compat_stat;
5959
struct old_timeval32;
6060
struct robust_list_head;
61+
struct futex_waitv;
6162
struct getcpu_cache;
6263
struct old_linux_dirent;
6364
struct perf_event_attr;
@@ -623,6 +624,10 @@ asmlinkage long sys_get_robust_list(int pid,
623624
asmlinkage long sys_set_robust_list(struct robust_list_head __user *head,
624625
size_t len);
625626

627+
asmlinkage long sys_futex_waitv(struct futex_waitv *waiters,
628+
unsigned int nr_futexes, unsigned int flags,
629+
struct __kernel_timespec __user *timeout, clockid_t clockid);
630+
626631
/* kernel/hrtimer.c */
627632
asmlinkage long sys_nanosleep(struct __kernel_timespec __user *rqtp,
628633
struct __kernel_timespec __user *rmtp);

‎include/uapi/asm-generic/unistd.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,11 @@ __SYSCALL(__NR_memfd_secret, sys_memfd_secret)
880880
#define __NR_process_mrelease 448
881881
__SYSCALL(__NR_process_mrelease, sys_process_mrelease)
882882

883+
#define __NR_futex_waitv 449
884+
__SYSCALL(__NR_futex_waitv, sys_futex_waitv)
885+
883886
#undef __NR_syscalls
884-
#define __NR_syscalls 449
887+
#define __NR_syscalls 450
885888

886889
/*
887890
* 32 bit systems traditionally used different

‎include/uapi/linux/futex.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@
4343
#define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \
4444
FUTEX_PRIVATE_FLAG)
4545

46+
/*
47+
* Flags to specify the bit length of the futex word for futex2 syscalls.
48+
* Currently, only 32 is supported.
49+
*/
50+
#define FUTEX_32 2
51+
52+
/*
53+
* Max numbers of elements in a futex_waitv array
54+
*/
55+
#define FUTEX_WAITV_MAX 128
56+
57+
/**
58+
* struct futex_waitv - A waiter for vectorized wait
59+
* @val: Expected value at uaddr
60+
* @uaddr: User address to wait on
61+
* @flags: Flags for this waiter
62+
* @__reserved: Reserved member to preserve data alignment. Should be 0.
63+
*/
64+
struct futex_waitv {
65+
__u64 val;
66+
__u64 uaddr;
67+
__u32 flags;
68+
__u32 __reserved;
69+
};
70+
4671
/*
4772
* Support for robust futexes: the kernel cleans up held futexes at
4873
* thread exit time.

‎kernel/futex/futex.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,21 @@ extern int futex_requeue(u32 __user *uaddr1, unsigned int flags,
268268
extern int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
269269
ktime_t *abs_time, u32 bitset);
270270

271+
/**
272+
* struct futex_vector - Auxiliary struct for futex_waitv()
273+
* @w: Userspace provided data
274+
* @q: Kernel side data
275+
*
276+
* Struct used to build an array with all data need for futex_waitv()
277+
*/
278+
struct futex_vector {
279+
struct futex_waitv w;
280+
struct futex_q q;
281+
};
282+
283+
extern int futex_wait_multiple(struct futex_vector *vs, unsigned int count,
284+
struct hrtimer_sleeper *to);
285+
271286
extern int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset);
272287

273288
extern int futex_wake_op(u32 __user *uaddr1, unsigned int flags,

‎kernel/futex/syscalls.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,125 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
199199
return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
200200
}
201201

202+
/* Mask of available flags for each futex in futex_waitv list */
203+
#define FUTEXV_WAITER_MASK (FUTEX_32 | FUTEX_PRIVATE_FLAG)
204+
205+
/**
206+
* futex_parse_waitv - Parse a waitv array from userspace
207+
* @futexv: Kernel side list of waiters to be filled
208+
* @uwaitv: Userspace list to be parsed
209+
* @nr_futexes: Length of futexv
210+
*
211+
* Return: Error code on failure, 0 on success
212+
*/
213+
static int futex_parse_waitv(struct futex_vector *futexv,
214+
struct futex_waitv __user *uwaitv,
215+
unsigned int nr_futexes)
216+
{
217+
struct futex_waitv aux;
218+
unsigned int i;
219+
220+
for (i = 0; i < nr_futexes; i++) {
221+
if (copy_from_user(&aux, &uwaitv[i], sizeof(aux)))
222+
return -EFAULT;
223+
224+
if ((aux.flags & ~FUTEXV_WAITER_MASK) || aux.__reserved)
225+
return -EINVAL;
226+
227+
if (!(aux.flags & FUTEX_32))
228+
return -EINVAL;
229+
230+
futexv[i].w.flags = aux.flags;
231+
futexv[i].w.val = aux.val;
232+
futexv[i].w.uaddr = aux.uaddr;
233+
futexv[i].q = futex_q_init;
234+
}
235+
236+
return 0;
237+
}
238+
239+
/**
240+
* sys_futex_waitv - Wait on a list of futexes
241+
* @waiters: List of futexes to wait on
242+
* @nr_futexes: Length of futexv
243+
* @flags: Flag for timeout (monotonic/realtime)
244+
* @timeout: Optional absolute timeout.
245+
* @clockid: Clock to be used for the timeout, realtime or monotonic.
246+
*
247+
* Given an array of `struct futex_waitv`, wait on each uaddr. The thread wakes
248+
* if a futex_wake() is performed at any uaddr. The syscall returns immediately
249+
* if any waiter has *uaddr != val. *timeout is an optional timeout value for
250+
* the operation. Each waiter has individual flags. The `flags` argument for
251+
* the syscall should be used solely for specifying the timeout as realtime, if
252+
* needed. Flags for private futexes, sizes, etc. should be used on the
253+
* individual flags of each waiter.
254+
*
255+
* Returns the array index of one of the woken futexes. No further information
256+
* is provided: any number of other futexes may also have been woken by the
257+
* same event, and if more than one futex was woken, the retrned index may
258+
* refer to any one of them. (It is not necessaryily the futex with the
259+
* smallest index, nor the one most recently woken, nor...)
260+
*/
261+
262+
SYSCALL_DEFINE5(futex_waitv, struct futex_waitv __user *, waiters,
263+
unsigned int, nr_futexes, unsigned int, flags,
264+
struct __kernel_timespec __user *, timeout, clockid_t, clockid)
265+
{
266+
struct hrtimer_sleeper to;
267+
struct futex_vector *futexv;
268+
struct timespec64 ts;
269+
ktime_t time;
270+
int ret;
271+
272+
/* This syscall supports no flags for now */
273+
if (flags)
274+
return -EINVAL;
275+
276+
if (!nr_futexes || nr_futexes > FUTEX_WAITV_MAX || !waiters)
277+
return -EINVAL;
278+
279+
if (timeout) {
280+
int flag_clkid = 0, flag_init = 0;
281+
282+
if (clockid == CLOCK_REALTIME) {
283+
flag_clkid = FLAGS_CLOCKRT;
284+
flag_init = FUTEX_CLOCK_REALTIME;
285+
}
286+
287+
if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
288+
return -EINVAL;
289+
290+
if (get_timespec64(&ts, timeout))
291+
return -EFAULT;
292+
293+
/*
294+
* Since there's no opcode for futex_waitv, use
295+
* FUTEX_WAIT_BITSET that uses absolute timeout as well
296+
*/
297+
ret = futex_init_timeout(FUTEX_WAIT_BITSET, flag_init, &ts, &time);
298+
if (ret)
299+
return ret;
300+
301+
futex_setup_timer(&time, &to, flag_clkid, 0);
302+
}
303+
304+
futexv = kcalloc(nr_futexes, sizeof(*futexv), GFP_KERNEL);
305+
if (!futexv)
306+
return -ENOMEM;
307+
308+
ret = futex_parse_waitv(futexv, waiters, nr_futexes);
309+
if (!ret)
310+
ret = futex_wait_multiple(futexv, nr_futexes, timeout ? &to : NULL);
311+
312+
if (timeout) {
313+
hrtimer_cancel(&to.timer);
314+
destroy_hrtimer_on_stack(&to.timer);
315+
}
316+
317+
kfree(futexv);
318+
return ret;
319+
}
320+
202321
#ifdef CONFIG_COMPAT
203322
COMPAT_SYSCALL_DEFINE2(set_robust_list,
204323
struct compat_robust_list_head __user *, head,

0 commit comments

Comments
 (0)