futex: More futex2 bits
Subsystems (20)
Peter Zijlstra
reviewercover futex: More futex2 bits
Hi,
New version of the futex2 patches. These are actually tested and appear to work as expected.
I'm hoping to get at least the first 3 patches merged such that Jens can base the io_uring futex patches on them.
Changes since v0:
- switched over to 'unsigned long' for values (Arnd)
- unshare vmalloc_huge() (Willy)
- added wait/requeue syscalls
- fixed NUMA to support sparse nodemask
- added FUTEX2_n vs FUTEX2_NUMA check to ensure the node_id fits in the futex
- added selftests
- fixed a ton of silly bugs
--- arch/alpha/kernel/syscalls/syscall.tbl | 3 + arch/arm/tools/syscall.tbl | 3 + arch/arm64/include/asm/unistd32.h | 6 + arch/ia64/kernel/syscalls/syscall.tbl | 3 + arch/m68k/kernel/syscalls/syscall.tbl | 3 + arch/microblaze/kernel/syscalls/syscall.tbl | 3 + arch/mips/kernel/syscalls/syscall_n32.tbl | 3 + arch/mips/kernel/syscalls/syscall_n64.tbl | 3 + arch/mips/kernel/syscalls/syscall_o32.tbl | 3 + arch/parisc/kernel/syscalls/syscall.tbl | 3 + arch/powerpc/kernel/syscalls/syscall.tbl | 3 + arch/s390/kernel/syscalls/syscall.tbl | 3 + arch/sh/kernel/syscalls/syscall.tbl | 3 + arch/sparc/kernel/syscalls/syscall.tbl | 3 + arch/x86/entry/syscalls/syscall_32.tbl | 3 + arch/x86/entry/syscalls/syscall_64.tbl | 3 + arch/xtensa/kernel/syscalls/syscall.tbl | 3 + include/linux/futex.h | 14 +- include/linux/syscalls.h | 10 + include/linux/vmalloc.h | 1 + include/uapi/asm-generic/unistd.h | 9 +- include/uapi/linux/futex.h | 17 +- kernel/futex/core.c | 144 +++++++++++--- kernel/futex/futex.h | 96 ++++++++- kernel/futex/pi.c | 12 +- kernel/futex/requeue.c | 18 +- kernel/futex/syscalls.c | 221 ++++++++++++++++----- kernel/futex/waitwake.c | 80 ++++---- kernel/sys_ni.c | 3 + mm/vmalloc.c | 7 + .../selftests/futex/functional/futex_requeue.c | 100 +++++++++- .../selftests/futex/functional/futex_wait.c | 56 +++++- .../futex/functional/futex_wait_timeout.c | 14 +- .../futex/functional/futex_wait_wouldblock.c | 28 ++- .../selftests/futex/functional/futex_waitv.c | 15 +- tools/testing/selftests/futex/functional/run.sh | 6 + tools/testing/selftests/futex/include/futex2test.h | 39 ++++ 37 files changed, 777 insertions(+), 167 deletions(-)
Peter Zijlstra
reviewerPATCH 1/14 futex: Clarify FUTEX2 flags
sys_futex_waitv() is part of the futex2 series (the first and only so far) of syscalls and has a flags field per futex (as opposed to flags being encoded in the futex op).
This new flags field has a new namespace, which unfortunately isn't super explicit. Notably it currently takes FUTEX_32 and FUTEX_PRIVATE_FLAG.
Introduce the FUTEX2 namespace to clarify this
--- include/uapi/linux/futex.h | 16 +++++++++++++--- kernel/futex/syscalls.c | 7 +++---- 2 files changed, 16 insertions(+), 7 deletions(-)
include/uapi/linux/futex.h +13 -3
@@ -44,10 +44,20 @@ FUTEX_PRIVATE_FLAG)/* -* Flags to specify the bit length of the futex word for futex2 syscalls. -* Currently, only 32 is supported. +* Flags for futex2 syscalls. */ -#define FUTEX_32 2 +/* 0x00 */ +/* 0x01 */ +#define FUTEX2_32 0x02 +/* 0x04 */ +/* 0x08 */ +/* 0x10 */ +/* 0x20 */ +/* 0x40 */ +#define FUTEX2_PRIVATE FUTEX_PRIVATE_FLAG + +/* do not use */ +#define FUTEX_32 FUTEX2_32/* historical accident :-( */ /* * Max numbers of elements in a futex_waitv array
kernel/futex/syscalls.c +3 -4
@@ -183,8 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -/* Mask of available flags for each futex in futex_waitv list */ -#define FUTEXV_WAITER_MASK (FUTEX_32 | FUTEX_PRIVATE_FLAG) +#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE)/** * futex_parse_waitv - Parse a waitv array from userspace
@@ -205,10 +204,10 @@ static int futex_parse_waitv(struct futeif (copy_from_user (&aux, &uwaitv[i],sizeof (aux)))return -EFAULT ; -if ((aux.flags & ~FUTEXV_WAITER_MASK ) || aux.__reserved) +if ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ; -if (!(aux.flags &FUTEX_32 )) +if (!(aux.flags &FUTEX2_32 ))return -EINVAL ; futexv[i].w.flags = aux.flags;
Peter Zijlstra
reviewerPATCH 5/14 futex: Add sys_futex_wake()
In reply to Peter ZijlstraTo complement sys_futex_waitv() add sys_futex_wake(). This syscall implements what was previously known as FUTEX_WAKE_BITSET except it uses 'unsigned long' for the bitmask and takes FUTEX2 flags.
The 'unsigned long' allows FUTEX2_64 on 64bit platforms.
--- arch/alpha/kernel/syscalls/syscall.tbl | 1 arch/arm/tools/syscall.tbl | 1 arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 arch/m68k/kernel/syscalls/syscall.tbl | 1 arch/microblaze/kernel/syscalls/syscall.tbl | 1 arch/mips/kernel/syscalls/syscall_n32.tbl | 1 arch/mips/kernel/syscalls/syscall_n64.tbl | 1 arch/mips/kernel/syscalls/syscall_o32.tbl | 1 arch/parisc/kernel/syscalls/syscall.tbl | 1 arch/powerpc/kernel/syscalls/syscall.tbl | 1 arch/s390/kernel/syscalls/syscall.tbl | 1 arch/sh/kernel/syscalls/syscall.tbl | 1 arch/sparc/kernel/syscalls/syscall.tbl | 1 arch/x86/entry/syscalls/syscall_32.tbl | 1 arch/x86/entry/syscalls/syscall_64.tbl | 1 arch/xtensa/kernel/syscalls/syscall.tbl | 1 include/linux/syscalls.h | 3 ++ include/uapi/asm-generic/unistd.h | 5 ++-- kernel/futex/syscalls.c | 30 ++++++++++++++++++++++++++++ kernel/sys_ni.c | 1 21 files changed, 55 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -491,3 +491,4 @@ 559 common futex_waitv sys_futex_waitv 560 common set_mempolicy_home_node sys_ni_syscall 561 common cachestat sys_cachestat +562 common futex_wake sys_futex_wake
arch/arm/tools/syscall.tbl +1 -0
@@ -465,3 +465,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/arm64/include/asm/unistd32.h +2 -0
@@ -909,6 +909,8 @@ __SYSCALL(__NR_futex_waitv, sys_futex_wa __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)#define __NR_cachestat 451 __SYSCALL(__NR_cachestat, sys_cachestat) +#define __NR_futex_wake 452 +__SYSCALL (__NR_futex_wake, sys_futex_wake)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
@@ -372,3 +372,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -451,3 +451,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -457,3 +457,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -390,3 +390,4 @@ 449 n32 futex_waitv sys_futex_waitv 450 n32 set_mempolicy_home_node sys_set_mempolicy_home_node 451 n32 cachestat sys_cachestat +452 n32 futex_wake sys_futex_wake
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -366,3 +366,4 @@ 449 n64 futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 n64 cachestat sys_cachestat +452 n64 futex_wake sys_futex_wake
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
@@ -439,3 +439,4 @@ 449 o32 futex_waitv sys_futex_waitv 450 o32 set_mempolicy_home_node sys_set_mempolicy_home_node 451 o32 cachestat sys_cachestat +452 o32 futex_wake sys_futex_wake
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -450,3 +450,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -538,3 +538,4 @@ 449 common futex_waitv sys_futex_waitv 450 nospu set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -454,3 +454,4 @@ 449 common futex_waitv sys_futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat sys_cachestat +452 common futex_wake sys_futex_wake sys_futex_wake
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -454,3 +454,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -497,3 +497,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -456,3 +456,4 @@ 449 i386 futex_waitv sys_futex_waitv 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node 451 i386 cachestat sys_cachestat +452 i386 futex_wake sys_futex_wake
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -373,6 +373,7 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake # # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -422,3 +422,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
include/linux/syscalls.h +3 -0
@@ -563,6 +563,9 @@ asmlinkage long sys_set_robust_list(struasmlinkage long sys_futex_waitv (struct futex_waitv *waiters,unsigned int nr_futexes,unsigned int flags,struct __kernel_timespec __user *timeout,clockid_t clockid); + +asmlinkage long sys_futex_wake (void __user *uaddr,unsigned long mask,int nr,unsigned int flags); +asmlinkage long sys_nanosleep (struct __kernel_timespec __user *rqtp,struct __kernel_timespec __user *rmtp); asmlinkage long sys_nanosleep_time32(struct old_timespec32 __user *rqtp,
include/uapi/asm-generic/unistd.h +3 -2
@@ -816,12 +816,13 @@ __SYSCALL(__NR_process_mrelease, sys_pro __SYSCALL(__NR_futex_waitv, sys_futex_waitv)#define __NR_set_mempolicy_home_node 450 __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node) -#define __NR_cachestat 451 __SYSCALL(__NR_cachestat, sys_cachestat) +#define __NR_futex_wake 452 +__SYSCALL(__NR_futex_wake, sys_futex_wake)#undef __NR_syscalls -#define __NR_syscalls 452 +#define __NR_syscalls 453/* * 32 bit systems traditionally used different
kernel/futex/syscalls.c +30 -0
@@ -306,6 +306,36 @@ SYSCALL_DEFINE5(futex_waitv, struct futereturn ret; } +/* +* sys_futex_wake - Wake a number of futexes +* @uaddr: Address of the futex(es) to wake +* @mask: bitmask +* @nr: Number of the futexes to wake +* @flags: FUTEX2 flags +* +* Identical to the traditional FUTEX_WAKE_BITSET op, except it is part of the +* futex2 family of calls. +*/ + +SYSCALL_DEFINE4 (futex_wake, + void __user *, uaddr, + unsigned long, mask, + int, nr, + unsigned int, flags) +{ +if (flags & ~FUTEX2_MASK ) +return -EINVAL ; + + flags =futex2_to_flags (flags); +if (!futex_flags_valid (flags)) +return -EINVAL ; + +if (!futex_validate_input (flags, mask)) +return -EINVAL ; + +return futex_wake (uaddr, flags, nr, mask); +} +#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE2(set_robust_list, struct compat_robust_list_head __user *, head,
kernel/sys_ni.c +1 -0
@@ -87,6 +87,7 @@ COND_SYSCALL_COMPAT(set_robust_list);COND_SYSCALL (get_robust_list);COND_SYSCALL_COMPAT (get_robust_list);COND_SYSCALL (futex_waitv); +COND_SYSCALL (futex_wake);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);COND_SYSCALL (init_module);
Peter Zijlstra
reviewerPATCH 4/14 futex: Validate futex value against futex size
In reply to Peter ZijlstraEnsure the futex value fits in the given futex size. Since this adds a constraint to an existing syscall, it might possibly change behaviour.
Currently the value would be truncated to a u32 and any high bits would get silently lost.
--- kernel/futex/futex.h | 8 ++++++++ kernel/futex/syscalls.c | 3 +++ 2 files changed, 11 insertions(+)
kernel/futex/futex.h +8 -0
@@ -86,6 +86,14 @@ static inline unsigned int futex_size(unreturn 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ } +static inline bool futex_validate_input (unsigned int flags,u64 val) +{ +int bits =8 *futex_size (flags); +if (bits <64 && (val >> bits)) +return false; +return true; +} +#ifdef CONFIG_FAIL_FUTEX extern bool should_fail_futex (bool fshared);#else
kernel/futex/syscalls.c +3 -0
@@ -209,6 +209,9 @@ static int futex_parse_waitv(struct futeif (!futex_flags_valid (flags))return -EINVAL ; +if (!futex_validate_input (flags, aux.val)) +return -EINVAL ; + futexv[i].w.flags = flags; futexv[i].w.val = aux.val; futexv[i].w.uaddr = aux.uaddr;
Peter Zijlstra
reviewerPATCH 7/14 futex: Propagate flags into get_futex_key()
In reply to Peter ZijlstraInstead of only passing FLAGS_SHARED as a boolean, pass down flags as a whole.
--- kernel/futex/core.c | 5 ++++- kernel/futex/futex.h | 2 +- kernel/futex/pi.c | 4 ++-- kernel/futex/requeue.c | 6 +++--- kernel/futex/waitwake.c | 14 +++++++------- 5 files changed, 17 insertions(+), 14 deletions(-)
kernel/futex/core.c +4 -1
@@ -217,7 +217,7 @@ static u64 get_inode_sequence_number(str* * lock_page() might sleep, the caller should not hold a spinlock. */ -int get_futex_key (u32 __user *uaddr,bool fshared,union futex_key *key, +int get_futex_key (u32 __user *uaddr,unsigned int flags,union futex_key *key,enum futex_access rw) {unsigned long address = (unsigned long )uaddr;
@@ -225,6 +225,9 @@ int get_futex_key(u32 __user *uaddr, boostruct page *page, *tail;struct address_space *mapping;int err, ro =0 ; +bool fshared; + + fshared = flags &FLAGS_SHARED ;/* * The futex address must be "naturally" aligned.
kernel/futex/futex.h +1 -1
@@ -174,7 +174,7 @@ enum futex_access {FUTEX_WRITE }; -extern int get_futex_key (u32 __user *uaddr,bool fshared,union futex_key *key, +extern int get_futex_key (u32 __user *uaddr,unsigned int flags,union futex_key *key,enum futex_access rw); extern struct hrtimer_sleeper *
kernel/futex/pi.c +2 -2
@@ -945,7 +945,7 @@ int futex_lock_pi(u32 __user *uaddr, uns to =futex_setup_timer (time, &timeout, flags,0 ); retry: - ret =get_futex_key (uaddr, flags &FLAGS_SHARED , &q.key,FUTEX_WRITE ); + ret =get_futex_key (uaddr, flags, &q.key,FUTEX_WRITE );if (unlikely (ret !=0 )) goto out;
@@ -1117,7 +1117,7 @@ int futex_unlock_pi(u32 __user *uaddr, uif ((uval &FUTEX_TID_MASK ) != vpid)return -EPERM ; - ret =get_futex_key (uaddr, flags &FLAGS_SHARED , &key,FUTEX_WRITE ); + ret =get_futex_key (uaddr, flags, &key,FUTEX_WRITE );if (ret)return ret;
kernel/futex/requeue.c +3 -3
@@ -424,10 +424,10 @@ int futex_requeue(u32 __user *uaddr1, un } retry: - ret =get_futex_key (uaddr1, flags &FLAGS_SHARED , &key1,FUTEX_READ ); + ret =get_futex_key (uaddr1, flags, &key1,FUTEX_READ );if (unlikely (ret !=0 ))return ret; - ret =get_futex_key (uaddr2, flags &FLAGS_SHARED , &key2, + ret =get_futex_key (uaddr2, flags, &key2, requeue_pi ?FUTEX_WRITE :FUTEX_READ );if (unlikely (ret !=0 ))return ret;
@@ -789,7 +789,7 @@ int futex_wait_requeue_pi(u32 __user *ua*/ rt_mutex_init_waiter (&rt_waiter); - ret =get_futex_key (uaddr2, flags &FLAGS_SHARED , &key2,FUTEX_WRITE ); + ret =get_futex_key (uaddr2, flags, &key2,FUTEX_WRITE );if (unlikely (ret !=0 )) goto out;
kernel/futex/waitwake.c +7 -7
@@ -145,13 +145,13 @@ int futex_wake(u32 __user *uaddr, unsignstruct futex_hash_bucket *hb;struct futex_q *this, *next;union futex_key key =FUTEX_KEY_INIT ; -int ret;DEFINE_WAKE_Q (wake_q); +int ret;if (!bitset)return -EINVAL ; - ret =get_futex_key (uaddr, flags &FLAGS_SHARED , &key,FUTEX_READ ); + ret =get_futex_key (uaddr, flags, &key,FUTEX_READ );if (unlikely (ret !=0 ))return ret;
@@ -245,10 +245,10 @@ int futex_wake_op(u32 __user *uaddr1, unDEFINE_WAKE_Q (wake_q); retry: - ret =get_futex_key (uaddr1, flags &FLAGS_SHARED , &key1,FUTEX_READ ); + ret =get_futex_key (uaddr1, flags, &key1,FUTEX_READ );if (unlikely (ret !=0 ))return ret; - ret =get_futex_key (uaddr2, flags &FLAGS_SHARED , &key2,FUTEX_WRITE ); + ret =get_futex_key (uaddr2, flags, &key2,FUTEX_WRITE );if (unlikely (ret !=0 ))return ret;
@@ -423,7 +423,7 @@ static int futex_wait_multiple_setup(strcontinue ; ret =get_futex_key (u64_to_user_ptr (vs[i].w.uaddr), - vs[i].w.flags &FLAGS_SHARED , + vs[i].w.flags, &vs[i].q.key,FUTEX_READ );if (unlikely (ret))
@@ -435,7 +435,7 @@ static int futex_wait_multiple_setup(strfor (i =0 ; i < count; i++) {u32 __user *uaddr = (u32 __user *)(unsigned long )vs[i].w.uaddr;struct futex_q *q = &vs[i].q; -u32 val = (u32 )vs[i].w.val; +u32 val = vs[i].w.val; hb =futex_q_lock (q); ret =futex_get_value_locked (&uval, uaddr);
@@ -599,7 +599,7 @@ int futex_wait_setup(u32 __user *uaddr,* while the syscall executes. */ retry: - ret =get_futex_key (uaddr, flags &FLAGS_SHARED , &q->key,FUTEX_READ ); + ret =get_futex_key (uaddr, flags, &q->key,FUTEX_READ );if (unlikely (ret !=0 ))return ret;
Peter Zijlstra
reviewerPATCH 10/14 mm: Add vmalloc_huge_node()
In reply to Peter ZijlstraTo enable node specific hash-tables.
--- include/linux/vmalloc.h | 1 + mm/vmalloc.c | 7 +++++++ 2 files changed, 8 insertions(+)
include/linux/vmalloc.h +1 -0
@@ -152,6 +152,7 @@ extern void *__vmalloc_node_range(unsignvoid *__vmalloc_node (unsigned long size,unsigned long align,gfp_t gfp_mask,int node,const void *caller)__alloc_size(1) ;void *vmalloc_huge (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ; +void *vmalloc_huge_node (unsigned long size,gfp_t gfp_mask,int node)__alloc_size(1) ;extern void *__vmalloc_array (size_t n,size_t size,gfp_t flags)__alloc_size(1, 2) ;extern void *vmalloc_array (size_t n,size_t size)__alloc_size(1, 2) ;
mm/vmalloc.c +7 -0
@@ -3416,6 +3416,13 @@ void *vmalloc(unsigned long size) }EXPORT_SYMBOL (vmalloc); +void *vmalloc_huge_node (unsigned long size,gfp_t gfp_mask,int node) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL ,VM_ALLOW_HUGE_VMAP , + node,__builtin_return_address (0 )); +} +/** * vmalloc_huge - allocate virtually contiguous memory, allow huge pages * @size: allocation size
Peter Zijlstra
reviewerPATCH 11/14 futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraExtend the futex2 interface to be numa aware.
When FUTEX2_NUMA is specified for a futex, the user value is extended to two words (of the same size). The first is the user value we all know, the second one will be the node to place this futex on.
struct futex_numa_32 {
u32 val;
u32 node;};
When node is set to ~0, WAIT will set it to the current node_id such that WAKE knows where to find it. If userspace corrupts the node value between WAIT and WAKE, the futex will not be found and no wakeup will happen.
When FUTEX2_NUMA is not set, the node is simply an extention of the hash, such that traditional futexes are still interleaved over the nodes.
This is done to avoid having to have a separate !numa hash-table.
--- include/linux/futex.h | 3 + kernel/futex/core.c | 128 +++++++++++++++++++++++++++++++++++++++--------- kernel/futex/futex.h | 26 +++++++-- kernel/futex/syscalls.c | 2 4 files changed, 127 insertions(+), 32 deletions(-)
include/linux/futex.h +3 -0
@@ -34,6 +34,7 @@ union futex_key {u64 i_seq;unsigned long pgoff;unsigned int offset; +/* unsigned int node; */ } shared; struct { union {
@@ -42,11 +43,13 @@ union futex_key { };unsigned long address;unsigned int offset; +/* unsigned int node; */ } private;struct {u64 ptr;unsigned long word;unsigned int offset; +unsigned int node;/* NOT hashed! */ } both; };
kernel/futex/core.c +104 -24
198 lines not on this page · raw message
kernel/futex/futex.h +19 -7
@@ -65,6 +65,12 @@ static inline unsigned int futex2_to_flareturn flags; } +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ +} +static inline bool futex_flags_valid (unsigned int flags) {/* Only 64bit futexes for 64bit code */
@@ -77,13 +83,19 @@ static inline bool futex_flags_valid(unsif ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 )return false; -return true; -} +/* +* Must be able to represent both NUMA_NO_NODE and every valid nodeid +* in a futex word. +*/ +if (flags &FLAGS_NUMA ) { +int bits =8 *futex_size (flags); +u64 max = ~0ULL ; + max >>=64 - bits; +if (nr_node_ids >= max) +return false; + } -static inline unsigned int futex_size (unsigned int flags) -{ -unsigned int size = flags &FLAGS_SIZE_MASK ; -return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ +return true; }static inline bool futex_validate_input (unsigned int flags,u64 val)
@@ -182,7 +194,7 @@ enum futex_access {FUTEX_WRITE }; -extern int get_futex_key (u32 __user *uaddr,unsigned int flags,union futex_key *key, +extern int get_futex_key (void __user *uaddr,unsigned int flags,union futex_key *key,enum futex_access rw); extern struct hrtimer_sleeper *
kernel/futex/syscalls.c +1 -1
@@ -179,7 +179,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_NUMA | FUTEX2_PRIVATE)/** * futex_parse_waitv - Parse a waitv array from userspace
Peter Zijlstra
reviewerPATCH 14/14 futex,selftests: Extend the futex selftests
In reply to Peter ZijlstraExtend the wait/requeue selftests to also cover the futex2 syscalls.
--- tools/testing/selftests/futex/functional/futex_requeue.c | 100 +++++++++- tools/testing/selftests/futex/functional/futex_wait.c | 56 ++++- tools/testing/selftests/futex/functional/futex_wait_timeout.c | 14 + tools/testing/selftests/futex/functional/futex_wait_wouldblock.c | 28 ++ tools/testing/selftests/futex/functional/futex_waitv.c | 15 - tools/testing/selftests/futex/functional/run.sh | 6 tools/testing/selftests/futex/include/futex2test.h | 39 +++ 7 files changed, 229 insertions(+), 29 deletions(-)
tools/testing/selftests/futex/functional/futex_requeue.c +90 -10
@@ -7,8 +7,10 @@#include <pthread.h> #include <limits.h> +#include <stdbool.h> #include "logging.h" #include "futextest.h" +#include "futex2test.h" #define TEST_NAME "futex-requeue"#define timeout_ns 30000000
@@ -16,24 +18,58 @@volatile futex_t *f1; +bool futex2 =0 ; +bool mixed =0 ; +void usage (char *prog) {printf ("Usage: %s\n" , prog);printf (" -c Use color\n" ); +printf (" -n Use futex2 interface\n" ); +printf (" -x Use mixed size futex\n" );printf (" -h Display this help message\n" );printf (" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n" ,VQUIET ,VCRITICAL ,VINFO ); } -void *waiterfn (void *arg) +static void *waiterfn (void *arg) { +unsigned int flags =0 ;struct timespec to; - to.tv_sec =0 ; - to.tv_nsec = timeout_ns; +if (futex2) { +unsigned long mask; + +if (clock_gettime (CLOCK_MONOTONIC , &to)) { +printf ("clock_gettime() failed errno %d" , errno); +return NULL ; + } + + to.tv_nsec += timeout_ns; +if (to.tv_nsec >=1000000000 ) { + to.tv_sec++; + to.tv_nsec -=1000000000 ; + } + +if (mixed) { + flags |=FUTEX2_16 ; + mask = (unsigned short )(~0U ); + }else { + flags |=FUTEX2_32 ; + mask = (unsigned int )(~0U ); + } + +if (futex2_wait (f1, *f1, mask, flags, + &to,CLOCK_MONOTONIC )) +printf ("waiter failed errno %d\n" , errno); + }else { + + to.tv_sec =0 ; + to.tv_nsec = timeout_ns; -if (futex_wait (f1, *f1, &to,0 )) -printf ("waiter failed errno %d\n" , errno); +if (futex_wait (f1, *f1, &to, flags)) +printf ("waiter failed errno %d\n" , errno); + }return NULL ; }
@@ -48,7 +84,7 @@ int main(int argc, char *argv[]) f1 = &_f1; -while ((c =getopt (argc, argv,"cht:v:" )) !=-1 ) { +while ((c =getopt (argc, argv,"xncht:v:" )) !=-1 ) {switch (c) {case 'c' :log_color (1 );
@@ -59,6 +95,12 @@ int main(int argc, char *argv[])case 'v' :log_verbosity (atoi (optarg));break ; +case 'x' : + mixed=1 ; +/* fallthrough */ +case 'n' : + futex2=1 ; +break ;default :usage (basename (argv[0 ]));exit (1 );
@@ -79,7 +121,22 @@ int main(int argc, char *argv[])usleep (WAKE_WAIT_US );info ("Requeuing 1 futex from f1 to f2\n" ); - res =futex_cmp_requeue (f1,0 , &f2,0 ,1 ,0 ); +if (futex2) { +struct futex_waitv futexes[2 ] = { + { + .val =0 , + .uaddr = (unsigned long )f1, + .flags = mixed ?FUTEX2_16 :FUTEX2_32 , + }, + { + .uaddr = (unsigned long )&f2, + .flags =FUTEX2_32 , + }, + }; + res =futex2_requeue (futexes,0 ,0 ,1 ); + }else { + res =futex_cmp_requeue (f1,0 , &f2,0 ,1 ,0 ); + }if (res !=1 ) { ksft_test_result_fail("futex_requeue simple returned: %d %s\n", res ? errno : res,
@@ -89,7 +146,11 @@ int main(int argc, char *argv[])info ("Waking 1 futex at f2\n" ); - res =futex_wake (&f2,1 ,0 ); +if (futex2) { + res =futex2_wake (&f2, ~0U ,1 ,FUTEX2_32 ); + }else { + res =futex_wake (&f2,1 ,0 ); + }if (res !=1 ) { ksft_test_result_fail("futex_requeue simple returned: %d %s\n", res ? errno : res,
@@ -112,7 +173,22 @@ int main(int argc, char *argv[])usleep (WAKE_WAIT_US );info ("Waking 3 futexes at f1 and requeuing 7 futexes from f1 to f2\n" ); - res =futex_cmp_requeue (f1,0 , &f2,3 ,7 ,0 ); +if (futex2) { +struct futex_waitv futexes[2 ] = { + { + .val =0 , + .uaddr = (unsigned long )f1, + .flags = mixed ?FUTEX2_16 :FUTEX2_32 , + }, + { + .uaddr = (unsigned long )&f2, + .flags =FUTEX2_32 , + }, + }; + res =futex2_requeue (futexes,0 ,3 ,7 ); + }else { + res =futex_cmp_requeue (f1,0 , &f2,3 ,7 ,0 ); + }if (res !=10 ) { ksft_test_result_fail("futex_requeue many returned: %d %s\n", res ? errno : res,
@@ -121,7 +197,11 @@ int main(int argc, char *argv[]) }info ("Waking INT_MAX futexes at f2\n" ); - res =futex_wake (&f2,INT_MAX ,0 ); +if (futex2) { + res =futex2_wake (&f2, ~0U ,INT_MAX ,FUTEX2_32 ); + }else { + res =futex_wake (&f2,INT_MAX ,0 ); + }if (res !=7 ) { ksft_test_result_fail("futex_requeue many returned: %d %s\n", res ? errno : res,
tools/testing/selftests/futex/functional/futex_wait.c +47 -9
116 lines not on this page · raw message
tools/testing/selftests/futex/functional/futex_wait_timeout.c +13 -1
@@ -125,7 +125,7 @@ int main(int argc, char *argv[]) }ksft_print_header (); -ksft_set_plan (9 ); +ksft_set_plan (11 );ksft_print_msg ("%s: Block on a futex and wait for timeout\n" ,basename (argv[0 ]));ksft_print_msg ("\tArguments: timeout=%ldns\n" , timeout_ns);
@@ -194,6 +194,18 @@ int main(int argc, char *argv[]) res =futex_waitv (&waitv,1 ,0 , &to,CLOCK_REALTIME );test_timeout (res, &ret,"futex_waitv realtime" ,ETIMEDOUT ); +/* futex2_wait with CLOCK_MONOTONIC */ +if (futex_get_abs_timeout (CLOCK_MONOTONIC , &to, timeout_ns)) +return RET_FAIL ; + res =futex2_wait (&f1, f1,1 ,FUTEX2_32 , &to,CLOCK_MONOTONIC ); +test_timeout (res, &ret,"futex2_wait monotonic" ,ETIMEDOUT ); + +/* futex2_wait with CLOCK_REALTIME */ +if (futex_get_abs_timeout (CLOCK_REALTIME , &to, timeout_ns)) +return RET_FAIL ; + res =futex2_wait (&f1, f1,1 ,FUTEX2_32 , &to,CLOCK_REALTIME ); +test_timeout (res, &ret,"futex2_wait realtime" ,ETIMEDOUT ); +ksft_print_cnts ();return ret; }
tools/testing/selftests/futex/functional/futex_wait_wouldblock.c +26 -2
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])struct futex_waitv waitv = { .uaddr = (uintptr_t )&f1, .val = f1+1 , - .flags =FUTEX_32 , + .flags =FUTEX2_32 |FUTEX2_PRIVATE , .__reserved =0 };
@@ -68,7 +68,7 @@ int main(int argc, char *argv[]) }ksft_print_header (); -ksft_set_plan (2 ); +ksft_set_plan (3 );ksft_print_msg ("%s: Test the unexpected futex value in FUTEX_WAIT\n" ,basename (argv[0 ]));
@@ -106,6 +106,30 @@ int main(int argc, char *argv[])ksft_test_result_pass ("futex_waitv\n" ); } +if (clock_gettime (CLOCK_MONOTONIC , &to)) { +error ("clock_gettime failed\n" , errno); +return errno; + } + + to.tv_nsec += timeout_ns; + +if (to.tv_nsec >=1000000000 ) { + to.tv_sec++; + to.tv_nsec -=1000000000 ; + } + +info ("Calling futex2_wait on f1: %u @ %p with val=%u\n" , f1, &f1, f1+1 ); + res =futex2_wait (&f1, f1+1 , ~0U ,FUTEX2_32 |FUTEX2_PRIVATE , + &to,CLOCK_MONOTONIC ); +if (!res || errno !=EWOULDBLOCK ) { +ksft_test_result_pass ("futex2_wait returned: %d %s\n" , + res ? errno : res, + res ?strerror (errno) :"" ); + ret =RET_FAIL ; + }else { +ksft_test_result_pass ("futex2_wait\n" ); + } +ksft_print_cnts ();return ret; }
tools/testing/selftests/futex/functional/futex_waitv.c +8 -7
@@ -88,7 +88,7 @@ int main(int argc, char *argv[])for (i =0 ; i <NR_FUTEXES ; i++) { waitv[i].uaddr = (uintptr_t )&futexes[i]; - waitv[i].flags =FUTEX_32 |FUTEX_PRIVATE_FLAG ; + waitv[i].flags =FUTEX2_32 |FUTEX2_PRIVATE ; waitv[i].val =0 ; waitv[i].__reserved =0 ; }
@@ -99,7 +99,8 @@ int main(int argc, char *argv[])usleep (WAKE_WAIT_US ); - res =futex_wake (u64_to_ptr (waitv[NR_FUTEXES -1 ].uaddr),1 ,FUTEX_PRIVATE_FLAG ); + res =futex2_wake (u64_to_ptr (waitv[NR_FUTEXES -1 ].uaddr), ~0U ,1 , +FUTEX2_PRIVATE |FUTEX2_32 );if (res !=1 ) { ksft_test_result_fail("futex_wake private returned: %d %s\n", res ? errno : res,
@@ -122,7 +123,7 @@ int main(int argc, char *argv[]) *shared_data =0 ; waitv[i].uaddr = (uintptr_t )shared_data; - waitv[i].flags =FUTEX_32 ; + waitv[i].flags =FUTEX2_32 ; waitv[i].val =0 ; waitv[i].__reserved =0 ; }
@@ -145,8 +146,8 @@ int main(int argc, char *argv[])for (i =0 ; i <NR_FUTEXES ; i++)shmdt (u64_to_ptr (waitv[i].uaddr)); -/* Testing a waiter without FUTEX_32 flag */ - waitv[0 ].flags =FUTEX_PRIVATE_FLAG ; +/* Testing a waiter without FUTEX2_32 flag */ + waitv[0 ].flags =FUTEX2_PRIVATE ;if (clock_gettime (CLOCK_MONOTONIC , &to))error ("gettime64 failed\n" , errno);
@@ -160,11 +161,11 @@ int main(int argc, char *argv[]) res ? strerror(errno) : ""); ret =RET_FAIL ; }else { -ksft_test_result_pass ("futex_waitv without FUTEX_32\n" ); +ksft_test_result_pass ("futex_waitv without FUTEX2_32\n" ); }/* Testing a waiter with an unaligned address */ - waitv[0 ].flags =FUTEX_PRIVATE_FLAG |FUTEX_32 ; + waitv[0 ].flags =FUTEX2_PRIVATE |FUTEX2_32 ; waitv[0 ].uaddr =1 ;if (clock_gettime (CLOCK_MONOTONIC , &to))
tools/testing/selftests/futex/functional/run.sh +6 -0
@@ -76,9 +76,15 @@ echo echo ./futex_wait $COLOR +echo +./futex_wait -n $COLOR echo ./futex_requeue $COLOR +echo +./futex_requeue -n $COLOR +echo +./futex_requeue -x $COLOR echo ./futex_waitv $COLOR
tools/testing/selftests/futex/include/futex2test.h +39 -0
@@ -8,6 +8,28 @@#define u64_to_ptr (x) ((void *)(uintptr_t)(x)) +#ifndef __NR_futex_wake +#define __NR_futex_wake 452 +#define __NR_futex_wait 453 +#define __NR_futex_requeue 454 +#endif + +#ifndef FUTEX2_8 +/* +* Flags for futex2 syscalls. +*/ +#define FUTEX2_8 0x00 +#define FUTEX2_16 0x01 +#define FUTEX2_32 0x02 +#define FUTEX2_64 0x03 +#define FUTEX2_NUMA 0x04 +/* 0x08 */ +/* 0x10 */ +/* 0x20 */ +/* 0x40 */ +#define FUTEX2_PRIVATE FUTEX_PRIVATE_FLAG +#endif +/** * futex_waitv - Wait at multiple futexes, wake on any * @waiters: Array of waiters
@@ -20,3 +42,20 @@ static inline int futex_waitv(volatile s {return syscall (__NR_futex_waitv, waiters, nr_waiters, flags, timo, clockid); } + +static inline int futex2_wake (volatile void *uaddr,unsigned long mask,int nr,unsigned int flags) +{ +return syscall (__NR_futex_wake, uaddr, mask, nr, flags); +} + +static inline int futex2_wait (volatile void *uaddr,unsigned long val,unsigned long mask, +unsigned int flags,struct timespec *timo,clockid_t clockid) +{ +return syscall (__NR_futex_wait, uaddr, val, mask, flags, timo, clockid); +} + +static inline int futex2_requeue (struct futex_waitv *futexes,unsigned int flags, +int nr_wake,int nr_requeue) +{ +return syscall (__NR_futex_requeue, futexes, flags, nr_wake, nr_requeue); +}
Peter Zijlstra
reviewerPATCH 6/14 futex: Add sys_futex_wait()
In reply to Peter ZijlstraTo complement sys_futex_waitv()/wake(), add sys_futex_wait(). This syscall implements what was previously known as FUTEX_WAIT_BITSET except it uses 'unsigned long' for the value and bitmask arguments, takes timespec and clockid_t arguments for the absolute timeout and uses FUTEX2 flags.
The 'unsigned long' allows FUTEX2_64 on 64bit platforms.
--- arch/alpha/kernel/syscalls/syscall.tbl | 1 arch/arm/tools/syscall.tbl | 1 arch/arm64/include/asm/unistd32.h | 2 arch/ia64/kernel/syscalls/syscall.tbl | 1 arch/m68k/kernel/syscalls/syscall.tbl | 1 arch/microblaze/kernel/syscalls/syscall.tbl | 1 arch/mips/kernel/syscalls/syscall_n32.tbl | 1 arch/mips/kernel/syscalls/syscall_n64.tbl | 1 arch/mips/kernel/syscalls/syscall_o32.tbl | 1 arch/parisc/kernel/syscalls/syscall.tbl | 1 arch/powerpc/kernel/syscalls/syscall.tbl | 1 arch/s390/kernel/syscalls/syscall.tbl | 1 arch/sh/kernel/syscalls/syscall.tbl | 1 arch/sparc/kernel/syscalls/syscall.tbl | 1 arch/x86/entry/syscalls/syscall_32.tbl | 1 arch/x86/entry/syscalls/syscall_64.tbl | 1 arch/xtensa/kernel/syscalls/syscall.tbl | 1 include/linux/syscalls.h | 4 include/uapi/asm-generic/unistd.h | 4 kernel/futex/futex.h | 3 kernel/futex/syscalls.c | 120 +++++++++++++++++++++------- kernel/futex/waitwake.c | 65 ++++++++------- kernel/sys_ni.c | 1 23 files changed, 156 insertions(+), 59 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -492,3 +492,4 @@ 560 common set_mempolicy_home_node sys_ni_syscall 561 common cachestat sys_cachestat 562 common futex_wake sys_futex_wake +563 common futex_wait sys_futex_wait
arch/arm/tools/syscall.tbl +1 -0
@@ -466,3 +466,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/arm64/include/asm/unistd32.h +2 -0
@@ -911,6 +911,8 @@ __SYSCALL(__NR_set_mempolicy_home_node, __SYSCALL(__NR_cachestat, sys_cachestat)#define __NR_futex_wake 452 __SYSCALL(__NR_futex_wake, sys_futex_wake) +#define __NR_futex_wait 453 +__SYSCALL (__NR_futex_wait, sys_futex_wait)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
@@ -373,3 +373,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -452,3 +452,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -458,3 +458,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -391,3 +391,4 @@ 450 n32 set_mempolicy_home_node sys_set_mempolicy_home_node 451 n32 cachestat sys_cachestat 452 n32 futex_wake sys_futex_wake +453 n32 futex_wait sys_futex_wait
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -367,3 +367,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 n64 cachestat sys_cachestat 452 n64 futex_wake sys_futex_wake +453 n64 futex_wait sys_futex_wait
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
@@ -440,3 +440,4 @@ 450 o32 set_mempolicy_home_node sys_set_mempolicy_home_node 451 o32 cachestat sys_cachestat 452 o32 futex_wake sys_futex_wake +453 o32 futex_wait sys_futex_wait
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -451,3 +451,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -539,3 +539,4 @@ 450 nospu set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -455,3 +455,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat sys_cachestat 452 common futex_wake sys_futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait sys_futex_wait
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -455,3 +455,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -498,3 +498,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -457,3 +457,4 @@ 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node 451 i386 cachestat sys_cachestat 452 i386 futex_wake sys_futex_wake +453 i386 futex_wait sys_futex_wait
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -374,6 +374,7 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait # # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -423,3 +423,4 @@ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake +453 common futex_wait sys_futex_wait
include/linux/syscalls.h +4 -0
@@ -566,6 +566,10 @@ asmlinkage long sys_futex_waitv(struct fasmlinkage long sys_futex_wake (void __user *uaddr,unsigned long mask,int nr,unsigned int flags); +asmlinkage long sys_futex_wait (void __user *uaddr,unsigned long val,unsigned long mask, +unsigned int flags,struct __kernel_timespec __user *timespec, +clockid_t clockid); +asmlinkage long sys_nanosleep (struct __kernel_timespec __user *rqtp,struct __kernel_timespec __user *rmtp); asmlinkage long sys_nanosleep_time32(struct old_timespec32 __user *rqtp,
include/uapi/asm-generic/unistd.h +3 -1
@@ -820,9 +820,11 @@ __SYSCALL(__NR_set_mempolicy_home_node, __SYSCALL(__NR_cachestat, sys_cachestat)#define __NR_futex_wake 452 __SYSCALL(__NR_futex_wake, sys_futex_wake) +#define __NR_futex_wait 453 +__SYSCALL(__NR_futex_wait, sys_futex_wait)#undef __NR_syscalls -#define __NR_syscalls 453 +#define __NR_syscalls 454/* * 32 bit systems traditionally used different
kernel/futex/futex.h +3 -0
@@ -330,6 +330,9 @@ extern int futex_requeue(u32 __user *uad u32 __user *uaddr2, int nr_wake, int nr_requeue, u32 *cmpval, int requeue_pi); +extern int __futex_wait (u32 __user *uaddr,unsigned int flags,u32 val, +struct hrtimer_sleeper *to,u32 bitset); +extern int futex_wait (u32 __user *uaddr,unsigned int flags,u32 val,ktime_t *abs_time,u32 bitset);
kernel/futex/syscalls.c +90 -30
150 lines not on this page · raw message
kernel/futex/waitwake.c +37 -28
99 lines not on this page · raw message
kernel/sys_ni.c +1 -0
@@ -88,6 +88,7 @@ COND_SYSCALL(get_robust_list);COND_SYSCALL_COMPAT (get_robust_list);COND_SYSCALL (futex_waitv);COND_SYSCALL (futex_wake); +COND_SYSCALL (futex_wait);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);COND_SYSCALL (init_module);
Peter Zijlstra
reviewerPATCH 8/14 futex: Add flags2 argument to futex_requeue()
In reply to Peter ZijlstraIn order to support mixed size requeue, add a second flags argument to the internal futex_requeue() function.
--- kernel/futex/futex.h | 5 +++-- kernel/futex/requeue.c | 12 +++++++----- kernel/futex/syscalls.c | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-)
kernel/futex/futex.h +3 -2
@@ -318,8 +318,9 @@ extern int futex_wait_requeue_pi(u32 __u val, ktime_t *abs_time, u32 bitset, u32 __user *uaddr2); -extern int futex_requeue (u32 __user *uaddr1,unsigned int flags, -u32 __user *uaddr2,int nr_wake,int nr_requeue, +extern int futex_requeue (u32 __user *uaddr1,unsigned int flags1, +u32 __user *uaddr2,unsigned int flags2, +int nr_wake,int nr_requeue,u32 *cmpval,int requeue_pi); extern int __futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
kernel/futex/requeue.c +7 -5
@@ -346,8 +346,9 @@ futex_proxy_trylock_atomic(u32 __user *p/** * futex_requeue() - Requeue waiters from uaddr1 to uaddr2 * @uaddr1: source futex user address -* @flags: futex flags (FLAGS_SHARED, etc.) +* @flags1: futex flags (FLAGS_SHARED, etc.) * @uaddr2: target futex user address +* @flags2: futex flags (FLAGS_SHARED, etc.) * @nr_wake: number of waiters to wake (must be 1 for requeue_pi) * @nr_requeue: number of waiters to requeue (0-INT_MAX) * @cmpval: @uaddr1 expected value (or %NULL)
@@ -361,7 +362,8 @@ futex_proxy_trylock_atomic(u32 __user *p* - >=0 - on success, the number of tasks requeued or woken; * - <0 - on error */ -int futex_requeue (u32 __user *uaddr1,unsigned int flags,u32 __user *uaddr2, +int futex_requeue (u32 __user *uaddr1,unsigned int flags1, +u32 __user *uaddr2,unsigned int flags2,int nr_wake,int nr_requeue,u32 *cmpval,int requeue_pi) {union futex_key key1 =FUTEX_KEY_INIT , key2 =FUTEX_KEY_INIT ;
@@ -424,10 +426,10 @@ int futex_requeue(u32 __user *uaddr1, un } retry: - ret =get_futex_key (uaddr1, flags, &key1,FUTEX_READ ); + ret =get_futex_key (uaddr1, flags1, &key1,FUTEX_READ );if (unlikely (ret !=0 ))return ret; - ret =get_futex_key (uaddr2, flags, &key2, + ret =get_futex_key (uaddr2, flags2, &key2, requeue_pi ?FUTEX_WRITE :FUTEX_READ );if (unlikely (ret !=0 ))return ret;
@@ -459,7 +461,7 @@ int futex_requeue(u32 __user *uaddr1, unif (ret)return ret; -if (!(flags &FLAGS_SHARED )) +if (!(flags1 &FLAGS_SHARED )) goto retry_private; goto retry;
kernel/futex/syscalls.c +3 -3
@@ -106,9 +106,9 @@ long do_futex(u32 __user *uaddr, int op,case FUTEX_WAKE_BITSET :return futex_wake (uaddr, flags, val, val3);case FUTEX_REQUEUE : -return futex_requeue (uaddr, flags, uaddr2, val, val2,NULL ,0 ); +return futex_requeue (uaddr, flags, uaddr2, flags, val, val2,NULL ,0 );case FUTEX_CMP_REQUEUE : -return futex_requeue (uaddr, flags, uaddr2, val, val2, &val3,0 ); +return futex_requeue (uaddr, flags, uaddr2, flags, val, val2, &val3,0 );case FUTEX_WAKE_OP :return futex_wake_op (uaddr, flags, uaddr2, val, val2, val3);case FUTEX_LOCK_PI :
@@ -125,7 +125,7 @@ long do_futex(u32 __user *uaddr, int op,return futex_wait_requeue_pi (uaddr, flags, val, timeout, val3, uaddr2);case FUTEX_CMP_REQUEUE_PI : -return futex_requeue (uaddr, flags, uaddr2, val, val2, &val3,1 ); +return futex_requeue (uaddr, flags, uaddr2, flags, val, val2, &val3,1 ); }return -ENOSYS ; }
Peter Zijlstra
reviewerPATCH 2/14 futex: Extend the FUTEX2 flags
In reply to Peter ZijlstraAdd the definition for the missing but always intended extra sizes, and add a NUMA flag for the planned numa extention.
--- include/uapi/linux/futex.h | 7 ++++--- kernel/futex/syscalls.c | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-)
include/uapi/linux/futex.h +4 -3
@@ -46,10 +46,11 @@/* * Flags for futex2 syscalls. */ -/* 0x00 */ -/* 0x01 */ +#define FUTEX2_8 0x00 +#define FUTEX2_16 0x01#define FUTEX2_32 0x02 -/* 0x04 */ +#define FUTEX2_64 0x03 +#define FUTEX2_NUMA 0x04/* 0x08 */ /* 0x10 */ /* 0x20 */
kernel/futex/syscalls.c +7 -2
@@ -183,7 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)/** * futex_parse_waitv - Parse a waitv array from userspace
@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ; -if (!(aux.flags &FUTEX2_32 )) +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((aux.flags &FUTEX2_64 ) ==FUTEX2_64 ) +return -EINVAL ; + } + +if ((aux.flags &FUTEX2_64 ) !=FUTEX2_32 )return -EINVAL ; futexv[i].w.flags = aux.flags;
Peter Zijlstra
reviewerPATCH 3/14 futex: Flag conversion
In reply to Peter ZijlstraFutex has 3 sets of flags:
- legacy futex op bits
- futex2 flags
- internal flags
Add a few helpers to convert from the API flags into the internal flags.
--- kernel/futex/futex.h | 64 +++++++++++++++++++++++++++++++++++++++++++++--- kernel/futex/syscalls.c | 24 ++++++------------ kernel/futex/waitwake.c | 4 +-- 3 files changed, 72 insertions(+), 20 deletions(-)
kernel/futex/futex.h +61 -3
@@ -5,6 +5,7 @@#include <linux/futex.h> #include <linux/rtmutex.h> #include <linux/sched/wake_q.h> +#include <linux/compat.h> #ifdef CONFIG_PREEMPT_RT #include <linux/rcuwait.h>
@@ -16,8 +17,15 @@* Futex flags used to encode options to functions and preserve them across * restarts. */ +#define FLAGS_SIZE_8 0x00 +#define FLAGS_SIZE_16 0x01 +#define FLAGS_SIZE_32 0x02 +#define FLAGS_SIZE_64 0x03 + +#define FLAGS_SIZE_MASK 0x03 +#ifdef CONFIG_MMU -# define FLAGS_SHARED 0x01 +# define FLAGS_SHARED 0x10#else /* * NOMMU does not have per process address space. Let the compiler optimize
@@ -25,8 +33,58 @@*/ # define FLAGS_SHARED 0x00#endif -#define FLAGS_CLOCKRT 0x02 -#define FLAGS_HAS_TIMEOUT 0x04 +#define FLAGS_CLOCKRT 0x20 +#define FLAGS_HAS_TIMEOUT 0x40 +#define FLAGS_NUMA 0x80 + +/* FUTEX_ to FLAGS_ */ +static inline unsigned int futex_to_flags (unsigned int op) +{ +unsigned int flags =FLAGS_SIZE_32 ; + +if (!(op &FUTEX_PRIVATE_FLAG )) + flags |=FLAGS_SHARED ; + +if (op &FUTEX_CLOCK_REALTIME ) + flags |=FLAGS_CLOCKRT ; + +return flags; +} + +/* FUTEX2_ to FLAGS_ */ +static inline unsigned int futex2_to_flags (unsigned int flags2) +{ +unsigned int flags = flags2 &FUTEX2_64 ; + +if (!(flags2 &FUTEX2_PRIVATE )) + flags |=FLAGS_SHARED ; + +if (flags2 &FUTEX2_NUMA ) + flags |=FLAGS_NUMA ; + +return flags; +} + +static inline bool futex_flags_valid (unsigned int flags) +{ +/* Only 64bit futexes for 64bit code */ +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((flags &FLAGS_SIZE_MASK ) ==FLAGS_SIZE_64 ) +return false; + } + +/* Only 32bit futexes are implemented -- for now */ +if ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 ) +return false; + +return true; +} + +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ +}#ifdef CONFIG_FAIL_FUTEX extern bool should_fail_futex (bool fshared);
kernel/futex/syscalls.c +9 -15
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0-or-later -#include <linux/compat.h> #include <linux/syscalls.h> #include <linux/time_namespace.h>
@@ -85,15 +84,12 @@ SYSCALL_DEFINE3(get_robust_list, int, pilong do_futex (u32 __user *uaddr,int op,u32 val,ktime_t *timeout,u32 __user *uaddr2,u32 val2,u32 val3) { +unsigned int flags =futex_to_flags (op);int cmd = op &FUTEX_CMD_MASK ; -unsigned int flags =0 ; -if (!(op &FUTEX_PRIVATE_FLAG )) - flags |=FLAGS_SHARED ; - -if (op &FUTEX_CLOCK_REALTIME ) { - flags |=FLAGS_CLOCKRT ; -if (cmd !=FUTEX_WAIT_BITSET && cmd !=FUTEX_WAIT_REQUEUE_PI && +if (flags &FLAGS_CLOCKRT ) { +if (cmd !=FUTEX_WAIT_BITSET && + cmd !=FUTEX_WAIT_REQUEUE_PI && cmd !=FUTEX_LOCK_PI2 )return -ENOSYS ; }
@@ -201,21 +197,19 @@ static int futex_parse_waitv(struct futeunsigned int i;for (i =0 ; i < nr_futexes; i++) { +unsigned int flags; +if (copy_from_user (&aux, &uwaitv[i],sizeof (aux)))return -EFAULT ;if ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ; -if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { -if ((aux.flags &FUTEX2_64 ) ==FUTEX2_64 ) -return -EINVAL ; - } - -if ((aux.flags &FUTEX2_64 ) !=FUTEX2_32 ) + flags =futex2_to_flags (aux.flags); +if (!futex_flags_valid (flags))return -EINVAL ; - futexv[i].w.flags = aux.flags; + futexv[i].w.flags = flags; futexv[i].w.val = aux.val; futexv[i].w.uaddr = aux.uaddr; futexv[i].q = futex_q_init;
kernel/futex/waitwake.c +2 -2
@@ -419,11 +419,11 @@ static int futex_wait_multiple_setup(str*/ retry:for (i =0 ; i < count; i++) { -if ((vs[i].w.flags &FUTEX_PRIVATE_FLAG ) && retry) +if (!(vs[i].w.flags &FLAGS_SHARED ) && retry)continue ; ret =get_futex_key (u64_to_user_ptr (vs[i].w.uaddr), - !(vs[i].w.flags &FUTEX_PRIVATE_FLAG ), + vs[i].w.flags &FLAGS_SHARED , &vs[i].q.key,FUTEX_READ );if (unlikely (ret))
Peter Zijlstra
reviewerPATCH 9/14 futex: Add sys_futex_requeue()
In reply to Peter ZijlstraFinish of the 'simple' futex2 syscall group by adding sys_futex_requeue(). Unlike sys_futex_{wait,wake}() it's arguments are too numerous to fit into a regular syscall. As such, use struct futex_waitv to pass the 'source' and 'destination' futexes to the syscall.
This syscall implements what was previously known as FUTEX_CMP_REQUEUE and uses {val, uaddr, flags} for source and {uaddr, flags} for destination.
This design explicitly allows requeueing between different types of futex by having a different flags word per uaddr.
--- arch/alpha/kernel/syscalls/syscall.tbl | 1 arch/arm/tools/syscall.tbl | 1 arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 arch/m68k/kernel/syscalls/syscall.tbl | 1 arch/microblaze/kernel/syscalls/syscall.tbl | 1 arch/mips/kernel/syscalls/syscall_n32.tbl | 1 arch/mips/kernel/syscalls/syscall_n64.tbl | 1 arch/mips/kernel/syscalls/syscall_o32.tbl | 1 arch/parisc/kernel/syscalls/syscall.tbl | 1 arch/powerpc/kernel/syscalls/syscall.tbl | 1 arch/s390/kernel/syscalls/syscall.tbl | 1 arch/sh/kernel/syscalls/syscall.tbl | 1 arch/sparc/kernel/syscalls/syscall.tbl | 1 arch/x86/entry/syscalls/syscall_32.tbl | 1 arch/x86/entry/syscalls/syscall_64.tbl | 1 arch/xtensa/kernel/syscalls/syscall.tbl | 1 include/linux/syscalls.h | 3 ++ include/uapi/asm-generic/unistd.h | 4 ++ kernel/futex/syscalls.c | 38 ++++++++++++++++++++++++++++ kernel/sys_ni.c | 1 21 files changed, 63 insertions(+), 1 deletion(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -493,3 +493,4 @@ 561 common cachestat sys_cachestat 562 common futex_wake sys_futex_wake 563 common futex_wait sys_futex_wait +564 common futex_requeue sys_futex_requeue
arch/arm/tools/syscall.tbl +1 -0
@@ -467,3 +467,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/arm64/include/asm/unistd32.h +2 -0
@@ -913,6 +913,8 @@ __SYSCALL(__NR_cachestat, sys_cachestat) __SYSCALL(__NR_futex_wake, sys_futex_wake)#define __NR_futex_wait 453 __SYSCALL(__NR_futex_wait, sys_futex_wait) +#define __NR_futex_requeue 454 +__SYSCALL (__NR_futex_requeue, sys_futex_requeue)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
@@ -374,3 +374,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -453,3 +453,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -459,3 +459,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -392,3 +392,4 @@ 451 n32 cachestat sys_cachestat 452 n32 futex_wake sys_futex_wake 453 n32 futex_wait sys_futex_wait +454 n32 futex_requeue sys_futex_requeue
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -368,3 +368,4 @@ 451 n64 cachestat sys_cachestat 452 n64 futex_wake sys_futex_wake 453 n64 futex_wait sys_futex_wait +454 n64 futex_requeue sys_futex_requeue
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
@@ -441,3 +441,4 @@ 451 o32 cachestat sys_cachestat 452 o32 futex_wake sys_futex_wake 453 o32 futex_wait sys_futex_wait +454 o32 futex_requeue sys_futex_requeue
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -452,3 +452,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -540,3 +540,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -456,3 +456,4 @@ 451 common cachestat sys_cachestat sys_cachestat 452 common futex_wake sys_futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue sys_futex_requeue
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -456,3 +456,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -499,3 +499,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -458,3 +458,4 @@ 451 i386 cachestat sys_cachestat 452 i386 futex_wake sys_futex_wake 453 i386 futex_wait sys_futex_wait +454 i386 futex_requeue sys_futex_requeue
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -375,6 +375,7 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue # # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -424,3 +424,4 @@ 451 common cachestat sys_cachestat 452 common futex_wake sys_futex_wake 453 common futex_wait sys_futex_wait +454 common futex_requeue sys_futex_requeue
include/linux/syscalls.h +3 -0
@@ -570,6 +570,9 @@ asmlinkage long sys_futex_wait(void __us unsigned int flags, struct __kernel_timespec __user *timespec, clockid_t clockid); +asmlinkage long sys_futex_requeue (struct futex_waitv __user *waiters, +unsigned int flags,int nr_wake,int nr_requeue); +asmlinkage long sys_nanosleep (struct __kernel_timespec __user *rqtp,struct __kernel_timespec __user *rmtp); asmlinkage long sys_nanosleep_time32(struct old_timespec32 __user *rqtp,
include/uapi/asm-generic/unistd.h +3 -1
@@ -822,9 +822,11 @@ __SYSCALL(__NR_cachestat, sys_cachestat) __SYSCALL(__NR_futex_wake, sys_futex_wake)#define __NR_futex_wait 453 __SYSCALL(__NR_futex_wait, sys_futex_wait) +#define __NR_futex_requeue 454 +__SYSCALL(__NR_futex_requeue, sys_futex_requeue)#undef __NR_syscalls -#define __NR_syscalls 454 +#define __NR_syscalls 455/* * 32 bit systems traditionally used different
kernel/futex/syscalls.c +38 -0
@@ -396,6 +396,44 @@ SYSCALL_DEFINE6(futex_wait,return ret; } +/* +* sys_futex_requeue - Requeue a waiter from one futex to another +* @waiters: array describing the source and destination futex +* @flags: unused +* @nr_wake: number of futexes to wake +* @nr_requeue: number of futexes to requeue +* +* Identical to the traditional FUTEX_CMP_REQUEUE op, except it is part of the +* futex2 family of calls. +*/ + +SYSCALL_DEFINE4 (futex_requeue, + struct futex_waitv __user *, waiters, + unsigned int, flags, + int, nr_wake, + int, nr_requeue) +{ +struct futex_vector futexes[2 ]; +u32 cmpval; +int ret; + +if (flags) +return -EINVAL ; + +if (!waiters) +return -EINVAL ; + + ret =futex_parse_waitv (futexes, waiters,2 ); +if (ret) +return ret; + + cmpval = futexes[0 ].w.val; + +return futex_requeue (u64_to_user_ptr (futexes[0 ].w.uaddr), futexes[0 ].w.flags, +u64_to_user_ptr (futexes[1 ].w.uaddr), futexes[1 ].w.flags, + nr_wake, nr_requeue, &cmpval,0 ); +} +#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE2(set_robust_list, struct compat_robust_list_head __user *, head,
kernel/sys_ni.c +1 -0
@@ -89,6 +89,7 @@ COND_SYSCALL_COMPAT(get_robust_list);COND_SYSCALL (futex_waitv);COND_SYSCALL (futex_wake);COND_SYSCALL (futex_wait); +COND_SYSCALL (futex_requeue);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);COND_SYSCALL (init_module);
Peter Zijlstra
reviewerPATCH 12/14 futex: Propagate flags into futex_get_value_locked()
In reply to Peter ZijlstraIn order to facilitate variable sized futexes propagate the flags into futex_get_value_locked().
--- kernel/futex/core.c | 4 ++-- kernel/futex/futex.h | 2 +- kernel/futex/pi.c | 8 ++++---- kernel/futex/requeue.c | 4 ++-- kernel/futex/waitwake.c | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-)
kernel/futex/core.c +2 -2
@@ -515,12 +515,12 @@ int futex_cmpxchg_value_locked(u32 *curvreturn ret; } -int futex_get_value_locked (u32 *dest,u32 __user *from) +int futex_get_value_locked (u32 *dest,u32 __user *from,unsigned int flags) {int ret;pagefault_disable (); - ret =__get_user (*dest, from); + ret =futex_get_value (dest, from, flags);pagefault_enable ();return ret ? -EFAULT :0 ;
kernel/futex/futex.h +1 -1
@@ -218,7 +218,7 @@ extern void futex_wake_mark(struct wake_extern int fault_in_user_writeable (u32 __user *uaddr);extern int futex_cmpxchg_value_locked (u32 *curval,u32 __user *uaddr,u32 uval,u32 newval); -extern int futex_get_value_locked (u32 *dest,u32 __user *from); +extern int futex_get_value_locked (u32 *dest,u32 __user *from,unsigned int flags);extern struct futex_q *futex_top_waiter (struct futex_hash_bucket *hb,union futex_key *key);extern void __futex_unqueue (struct futex_q *q);
kernel/futex/pi.c +4 -4
@@ -239,7 +239,7 @@ static int attach_to_pi_state(u32 __user* still is what we expect it to be, otherwise retry the entire * operation. */ -if (futex_get_value_locked (&uval2, uaddr)) +if (futex_get_value_locked (&uval2, uaddr,FLAGS_SIZE_32 )) goto out_efault;if (uval != uval2)
@@ -358,7 +358,7 @@ static int handle_exit_race(u32 __user ** The same logic applies to the case where the exiting task is * already gone. */ -if (futex_get_value_locked (&uval2, uaddr)) +if (futex_get_value_locked (&uval2, uaddr,FLAGS_SIZE_32 ))return -EFAULT ;/* If the user space value has changed, try again. */
@@ -526,7 +526,7 @@ int futex_lock_pi_atomic(u32 __user *uad* Read the user space value first so we can validate a few * things before proceeding further. */ -if (futex_get_value_locked (&uval, uaddr)) +if (futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 ))return -EFAULT ;if (unlikely (should_fail_futex (true)))
@@ -762,7 +762,7 @@ static int __fixup_pi_state_owner(u32 __if (!pi_state->owner) newtid |=FUTEX_OWNER_DIED ; - err =futex_get_value_locked (&uval, uaddr); + err =futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 );if (err) goto handle_err;
kernel/futex/requeue.c +2 -2
@@ -273,7 +273,7 @@ futex_proxy_trylock_atomic(u32 __user *pu32 curval;int ret; -if (futex_get_value_locked (&curval, pifutex)) +if (futex_get_value_locked (&curval, pifutex,FLAGS_SIZE_32 ))return -EFAULT ;if (unlikely (should_fail_futex (true)))
@@ -451,7 +451,7 @@ int futex_requeue(u32 __user *uaddr1, unif (likely (cmpval !=NULL )) {u32 curval; - ret =futex_get_value_locked (&curval, uaddr1); + ret =futex_get_value_locked (&curval, uaddr1,FLAGS_SIZE_32 );if (unlikely (ret)) {double_unlock_hb (hb1, hb2);
kernel/futex/waitwake.c +2 -2
@@ -438,7 +438,7 @@ static int futex_wait_multiple_setup(stru32 val = vs[i].w.val; hb =futex_q_lock (q); - ret =futex_get_value_locked (&uval, uaddr); + ret =futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 );if (!ret && uval == val) {/*
@@ -606,7 +606,7 @@ int futex_wait_setup(u32 __user *uaddr, retry_private: *hb =futex_q_lock (q); - ret =futex_get_value_locked (&uval, uaddr); + ret =futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 );if (ret) {futex_q_unlock (*hb);
Peter Zijlstra
reviewerPATCH 13/14 futex: Enable FUTEX2_{8,16}
In reply to Peter ZijlstraWhen futexes are no longer u32 aligned, the lower offset bits are no longer available to put type info in. However, since offset is the offset within a page, there are plenty bits available on the top end.
After that, pass flags into futex_get_value_locked() for WAIT and disallow FUTEX2_64 instead of mandating FUTEX2_32.
--- include/linux/futex.h | 11 ++++++----- kernel/futex/core.c | 9 +++++++++ kernel/futex/futex.h | 4 ++-- kernel/futex/waitwake.c | 5 +++-- 4 files changed, 20 insertions(+), 9 deletions(-)
include/linux/futex.h +6 -5
@@ -16,18 +16,19 @@ struct task_struct;* The key type depends on whether it's a shared or private mapping. * Don't rearrange members without looking at hash_futex(). * -* offset is aligned to a multiple of sizeof(u32) (== 4) by definition. -* We use the two low order bits of offset to tell what is the kind of key : +* offset is the position within a page and is in the range [0, PAGE_SIZE). +* The high bits of the offset indicate what kind of key this is: * 00 : Private process futex (PTHREAD_PROCESS_PRIVATE) * (no reference on an inode or mm) * 01 : Shared futex (PTHREAD_PROCESS_SHARED) * mapped on a file (reference on the underlying inode) * 10 : Shared futex (PTHREAD_PROCESS_SHARED) * (but private mapping on an mm, and reference taken on it) -*/ +*/ -#define FUT_OFF_INODE 1/* We set bit 0 if key has a reference on inode */ -#define FUT_OFF_MMSHARED 2/* We set bit 1 if key has a reference on mm */ +#define FUT_OFF_INODE (PAGE_SIZE << 0) +#define FUT_OFF_MMSHARED (PAGE_SIZE << 1) +#define FUT_OFF_SIZE (PAGE_SIZE << 2) union futex_key { struct {
kernel/futex/core.c +9 -0
@@ -308,6 +308,15 @@ int get_futex_key(void __user *uaddr, un }/* +* Encode the futex size in the offset. This makes cross-size +* wake-wait fail -- see futex_match(). +* +* NOTE that cross-size wake-wait is fundamentally broken wrt +* FLAGS_NUMA but could possibly work for !NUMA. +*/ + key->both.offset |=FUT_OFF_SIZE * (flags &FLAGS_SIZE_MASK ); + +/* * PROCESS_PRIVATE futexes are fast. * As the mm cannot disappear under us and the 'key' only needs * virtual address, we dont even have to find the underlying vma.
kernel/futex/futex.h +2 -2
@@ -79,8 +79,8 @@ static inline bool futex_flags_valid(unsreturn false; } -/* Only 32bit futexes are implemented -- for now */ -if ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 ) +/* 64bit futexes aren't implemented -- yet */ +if ((flags &FLAGS_SIZE_MASK ) ==FLAGS_SIZE_64 )return false;/*
kernel/futex/waitwake.c +3 -2
@@ -434,11 +434,12 @@ static int futex_wait_multiple_setup(strfor (i =0 ; i < count; i++) {u32 __user *uaddr = (u32 __user *)(unsigned long )vs[i].w.uaddr; +unsigned int flags = vs[i].w.flags;struct futex_q *q = &vs[i].q;u32 val = vs[i].w.val; hb =futex_q_lock (q); - ret =futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 ); + ret =futex_get_value_locked (&uval, uaddr, flags);if (!ret && uval == val) {/*
@@ -606,7 +607,7 @@ int futex_wait_setup(u32 __user *uaddr, retry_private: *hb =futex_q_lock (q); - ret =futex_get_value_locked (&uval, uaddr,FLAGS_SIZE_32 ); + ret =futex_get_value_locked (&uval, uaddr, flags);if (ret) {futex_q_unlock (*hb);
Peter Zijlstra
reviewerRe: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraQuoted 14 lines · Peter Zijlstra
kernel/futex/core.c +2
-1@@ -217,32 +259,55 @@ static u64 get_inode_sequence_number(str* * lock_page() might sleep, the caller should not hold a spinlock. */ -int get_futex_key (u32 __user *uaddr,unsigned int flags,union futex_key *key, +int get_futex_key (void __user *uaddr,unsigned int flags,union futex_key *key,enum futex_access rw) {unsigned long address = (unsigned long )uaddr;struct mm_struct *mm = current->mm;struct page *page, *tail;struct address_space *mapping; +int node, err, size, ro =0 ;bool fshared;
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 280
Quoted 10 lines
kernel/futex/core.c +2
-0fshared = flags &FLAGS_SHARED ; + size =futex_size (flags);/* * The futex address must be "naturally" aligned. */ key->both.offset = address %PAGE_SIZE ; +if (unlikely ((address % size) !=0 ))return -EINVAL ;
This enforces u32 alignment for:
struct futex_numa_32 {
u32 val;
u32 node;
};Or do we want to enfore u64 alignment for that?
Quoted 7 lines
kernel/futex/core.c +4
-0address -= key->both.offset; +if (flags &FLAGS_NUMA ) + size *=2 ; + +if (unlikely (!access_ok (uaddr, size)))return -EFAULT ;
Quoted 7 lines · Peter Zijlstra
Hi,
New version of the futex2 patches. These are actually tested and appear to work as expected.
I'm hoping to get at least the first 3 patches merged such that Jens can base the
io_uringfutex patches on them.
First 4 now, as we'll need the validate patch as well!
Signature · 1 line
Jens Axboe
Arnd Bergmann
maintainerRe: [PATCH v1 05/14] futex: Add sys_futex_wake()
In reply to Peter Zijlstraarch/arm/tools/syscall.tbl line 468
Quoted 7 lines · Peter Zijlstra
arch/arm/tools/syscall.tbl +1
-0@@ -465,3 +465,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wake
This clashes with __NR_fchmodat2 in linux-next, which also wants number 452.
arch/arm64/include/asm/unistd32.h __SYSCALL(__NR_futex_waitv, sys_futex_wa · line 916
Quoted 11 lines
arch/arm64/include/asm/unistd32.h +2
-0@@ -909,6 +909,8 @@ __SYSCALL(__NR_futex_waitv, sys_futex_wa __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)#define __NR_cachestat 451 __SYSCALL(__NR_cachestat, sys_cachestat) +#define __NR_futex_wake 452 +__SYSCALL (__NR_futex_wake, sys_futex_wake)/* * Please add new compat syscalls above this comment and update
Unfortunately, changing this file still requires updating __NR_compat_syscalls in arch/arm64/include/asm/unistd.h as well.
kernel/sys_ni.c COND_SYSCALL_COMPAT(set_robust_list); · line 93
Quoted 10 lines
kernel/sys_ni.c +1
-0@@ -87,6 +87,7 @@ COND_SYSCALL_COMPAT(set_robust_list);COND_SYSCALL (get_robust_list);COND_SYSCALL_COMPAT (get_robust_list);COND_SYSCALL (futex_waitv); +COND_SYSCALL (futex_wake);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);COND_SYSCALL (init_module);
This is fine for the moment, but I wonder if we should start making futex mandatory at some point. Right now, sparc32 with CONFIG_SMP cannot support futex because of the lack of atomics in early sparc processors, but sparc32 glibc actually requires futexes and consequently only works on uniprocessor machines, on sparc64 compat mode, or on Leon3 with out of tree patches.
Arnd
Arnd Bergmann
maintainerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Peter Zijlstrakernel/futex/syscalls.c static int futex_parse_waitv(struct fute · line 216
Quoted 13 lines · Peter Zijlstra
*
futex_parse_waitv- Parse a waitv array from userspacekernel/futex/syscalls.c +6
-1@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ; -if (!(aux.flags &FUTEX2_32 )) +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((aux.flags &FUTEX2_64 ) ==FUTEX2_64 ) +return -EINVAL ; + } + +if ((aux.flags &FUTEX2_64 ) !=FUTEX2_32 )return -EINVAL ;
This looks slightly confusing, how about defining another FUTEX2_SIZEMASK (or similar) macro to clarify that "aux.flags & FUTEX2_64" is a mask operation that can match the FUTEX2_{8,16,32,64} values?
Arnd
Arnd Bergmann
maintainerQuoted 9 lines · Peter Zijlstra
Hi,
New version of the futex2 patches. These are actually tested and appear to work as expected.
I'm hoping to get at least the first 3 patches merged such that Jens can base the
io_uringfutex patches on them.
Quoted 9 lines
Changes since v0:
- switched over to 'unsigned long' for values (Arnd)
- unsharevmalloc_huge()(Willy)
- added wait/requeue syscalls
- fixed NUMA to support sparse nodemask
- addedFUTEX2_nvsFUTEX2_NUMAcheck to ensure thenode_idfits in the futex
- added selftests
- fixed a ton of silly bugs
The changes look good to me, and the ABI should be fine without special compat handler now. I sent a couple of minor comments, but nothing important.
Arnd
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Arnd BergmannQuoted 6 lines · Arnd Bergmann
Quoted 5 lines · Peter Zijlstra
*
futex_parse_waitv- Parse a waitv array from userspacekernel/futex/syscalls.c +0
-0@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ;
Quoted 13 lines
kernel/futex/syscalls.c static int futex_parse_waitv(struct fute · line 216
Quoted 8 lines
kernel/futex/syscalls.c +6
-1-if (!(aux.flags &FUTEX2_32 )) +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((aux.flags &FUTEX2_64 ) ==FUTEX2_64 ) +return -EINVAL ; + } + +if ((aux.flags &FUTEX2_64 ) !=FUTEX2_32 )return -EINVAL ;This looks slightly confusing, how about defining another
FUTEX2_SIZEMASK(or similar) macro to clarify that "aux.flags &FUTEX2_64" is a mask operation that can match the FUTEX2_{8,16,32,64} values?
Yeah, I had that in an earlier version, but then reconsidered as I didn't want to clutter the uabi with that. But perhaps I over-throught this.
Peter Zijlstra
reviewerRe: [PATCH v1 05/14] futex: Add sys_futex_wake()
In reply to Arnd BergmannQuoted 10 lines · Arnd Bergmann
arch/arm/tools/syscall.tbl line 468
Quoted 7 lines · Peter Zijlstra
arch/arm/tools/syscall.tbl +1
-0@@ -465,3 +465,4 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common cachestat sys_cachestat +452 common futex_wake sys_futex_wakeThis clashes with
__NR_fchmodat2in linux-next, which also wants number 452.
Yeah, I fully expected some collisions :/ Unavoidable.
Quoted 9 lines
Quoted 9 lines
arch/arm64/include/asm/unistd32.h +2
-0@@ -909,6 +909,8 @@ __SYSCALL(__NR_futex_waitv, sys_futex_wa __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)#define __NR_cachestat 451 __SYSCALL(__NR_cachestat, sys_cachestat) +#define __NR_futex_wake 452 +__SYSCALL (__NR_futex_wake, sys_futex_wake)
Quoted 5 lines
arch/arm64/include/asm/unistd32.h __SYSCALL(__NR_futex_waitv, sys_futex_wa · line 916
Quoted 2 lines
arch/arm64/include/asm/unistd32.h +0
-0/* * Please add new compat syscalls above this comment and update Unfortunately, changing this file still requires updating
__NR_compat_syscallsinarch/arm64/include/asm/unistd.has well.
Bah I missed that. I'll go fix it up. Thanks!
Quoted 9 lines
Quoted 9 lines
kernel/sys_ni.c +1
-0@@ -87,6 +87,7 @@ COND_SYSCALL_COMPAT(set_robust_list);COND_SYSCALL (get_robust_list);COND_SYSCALL_COMPAT (get_robust_list);COND_SYSCALL (futex_waitv); +COND_SYSCALL (futex_wake);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);
Quoted 8 lines
kernel/sys_ni.c COND_SYSCALL_COMPAT(set_robust_list); · line 93
Quoted 1 line
kernel/sys_ni.c +0
-0COND_SYSCALL (init_module);This is fine for the moment, but I wonder if we should start making futex mandatory at some point. Right now, sparc32 with
CONFIG_SMPcannot support futex because of the lack of atomics in early sparc processors, but sparc32 glibc actually requires futexes and consequently only works on uniprocessor machines, on sparc64 compat mode, or on Leon3 with out of tree patches.
PARISC is another 'fun' case.
Arnd Bergmann
maintainerRe: [PATCH v1 05/14] futex: Add sys_futex_wake()
Quoted 20 lines · Peter Zijlstra
Quoted 10 lines · Arnd Bergmann
Quoted 9 lines · Peter Zijlstra
kernel/sys_ni.c +1
-0@@ -87,6 +87,7 @@ COND_SYSCALL_COMPAT(set_robust_list);COND_SYSCALL (get_robust_list);COND_SYSCALL_COMPAT (get_robust_list);COND_SYSCALL (futex_waitv); +COND_SYSCALL (futex_wake);COND_SYSCALL (kexec_load);COND_SYSCALL_COMPAT (kexec_load);Quoted 8 lines
kernel/sys_ni.c COND_SYSCALL_COMPAT(set_robust_list); · line 93
Quoted 1 line
kernel/sys_ni.c +0
-0COND_SYSCALL (init_module);This is fine for the moment, but I wonder if we should start making futex mandatory at some point. Right now, sparc32 with
CONFIG_SMPcannot support futex because of the lack of atomics in early sparc processors, but sparc32 glibc actually requires futexes and consequently only works on uniprocessor machines, on sparc64 compat mode, or on Leon3 with out of tree patches.
Quoted 1 line
PARISC is another 'fun' case.
I had to look up how that works, but as far as I can tell, the parisc code actually has a chance of working, as the userspace atomics go through the light-weight syscall that shares a hashed lock with the actual futex syscall. On sparc32 I think it's worse because userspace assumes that atomic instructions are supported while the kernel assumes they are not.
Arnd
Re: [PATCH v1 10/14] mm: Add vmalloc_huge_node()
In reply to Peter ZijlstraLooks good:
Re: [PATCH v1 05/14] futex: Add sys_futex_wake()
In reply to Peter ZijlstraQuoted 9 lines · Peter Zijlstra
To complement
sys_futex_waitv()addsys_futex_wake(). This syscall implements what was previously known asFUTEX_WAKE_BITSETexcept it uses 'unsigned long' for the bitmask and takes FUTEX2 flags.The 'unsigned long' allows
FUTEX2_64on 64bit platforms.Signed-off-by Peter Zijlstra (Intel) <[email protected]>arch/m68k/kernel/syscalls/syscall.tbl | 1
For adding the syscall:
For the actual number, you have to take the Big Syscall Lock ;-)
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v1 06/14] futex: Add sys_futex_wait()
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
To complement sys_futex_waitv()/wake(), add
sys_futex_wait(). This syscall implements what was previously known asFUTEX_WAIT_BITSETexcept it uses 'unsigned long' for the value and bitmask arguments, takes timespec andclockid_targuments for the absolute timeout and uses FUTEX2 flags.The 'unsigned long' allows
FUTEX2_64on 64bit platforms.Signed-off-by Peter Zijlstra (Intel) <[email protected]>arch/m68k/kernel/syscalls/syscall.tbl | 1
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v1 09/14] futex: Add sys_futex_requeue()
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Finish of the 'simple' futex2 syscall group by adding
sys_futex_requeue(). Unlike sys_futex_{wait,wake}() it's arguments are too numerous to fit into a regular syscall. As such, use structfutex_waitvto pass the 'source' and 'destination' futexes to the syscall.This syscall implements what was previously known as
FUTEX_CMP_REQUEUEand uses {val, uaddr, flags} for source and {uaddr, flags} for destination.
Quoted 6 lines
This design explicitly allows requeueing between different types of futex by having a different flags word per uaddr.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>arch/m68k/kernel/syscalls/syscall.tbl | 1
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v1 01/14] futex: Clarify FUTEX2 flags
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
sys_futex_waitv()is part of the futex2 series (the first and only so far) of syscalls and has a flags field per futex (as opposed to flags being encoded in the futex op).This new flags field has a new namespace, which unfortunately isn't super explicit. Notably it currently takes
FUTEX_32andFUTEX_PRIVATE_FLAG.Introduce the FUTEX2 namespace to clarify this
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Peter ZijlstraQuoted 21 lines · Peter Zijlstra
include/uapi/linux/futex.h +4
-1+#define FUTEX2_8 0x00 +#define FUTEX2_16 0x01#define FUTEX2_32 0x02 -/* 0x04 */ +#define FUTEX2_64 0x03 +#define FUTEX2_NUMA 0x04/* 0x08 */ /* 0x10 */ /* 0x20 */ kernel/futex/syscalls.c +1
-1@@ -183,7 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)/** * futex_parse_waitv - Parse a waitv array from userspace
kernel/futex/syscalls.c static int futex_parse_waitv(struct fute · line 208
Quoted 2 lines
kernel/futex/syscalls.c +0
-0@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ;
With the above aux.flags with FUTEX2_32 set will result in -EINVAL. I don't think that's intentional.
Thanks,
tglx
Re: [PATCH v1 03/14] futex: Flag conversion
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
kernel/futex/futex.h +10
-0+ +static inline bool futex_flags_valid (unsigned int flags) +{ +/* Only 64bit futexes for 64bit code */ +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((flags &FLAGS_SIZE_MASK ) ==FLAGS_SIZE_64 ) +return false; + } + +/* Only 32bit futexes are implemented -- for now */
kernel/futex/futex.h line 86
Quoted 10 lines
kernel/futex/futex.h +10
-0+if ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 ) +return false; + +return true; +} + +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */
Lacks a new line and the comment is both misplaced and kinda obvious, no?
Other than that, this looks abour right.
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 19 lines · Thomas Gleixner
Quoted 18 lines · Peter Zijlstra
include/uapi/linux/futex.h +4
-1+#define FUTEX2_8 0x00 +#define FUTEX2_16 0x01#define FUTEX2_32 0x02 -/* 0x04 */ +#define FUTEX2_64 0x03 +#define FUTEX2_NUMA 0x04/* 0x08 */ /* 0x10 */ /* 0x20 */ kernel/futex/syscalls.c +1
-1@@ -183,7 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)
Quoted 8 lines
kernel/futex/syscalls.c static int futex_parse_waitv(struct fute · line 208
Quoted 5 lines
kernel/futex/syscalls.c +0
-0/** * futex_parse_waitv - Parse a waitv array from userspace @@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ;With the above aux.flags with
FUTEX2_32set will result in-EINVAL. I don't think that's intentional.
FUTEX2_8 0 FUTEX2_16 1 FUTEX2_32 2 FUTEX2_64 3
Therefore FUTEX2_64, when used as a mask, includes then all.
Arnd suggested adding FUTEX2_SIZE_MASK or something. And I initially had something like that, but pulled it for not wanting to pullute the uabi.
Can easily add back.
Peter Zijlstra
reviewerRe: [PATCH v1 03/14] futex: Flag conversion
In reply to Thomas GleixnerQuoted 15 lines · Thomas Gleixner
Quoted 14 lines · Peter Zijlstra
kernel/futex/futex.h +14
-0+ +static inline bool futex_flags_valid (unsigned int flags) +{ +/* Only 64bit futexes for 64bit code */ +if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { +if ((flags &FLAGS_SIZE_MASK ) ==FLAGS_SIZE_64 ) +return false; + } + +/* Only 32bit futexes are implemented -- for now */ +if ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 ) +return false; + +return true;
Quoted 8 lines
kernel/futex/futex.h line 86
Quoted 6 lines
kernel/futex/futex.h +6
-0+} + +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ Lacks a new line and the comment is both misplaced and kinda obvious, no?
Yeah, it was a more complicated transform at some point, I'll fix.
Re: [PATCH v1 06/14] futex: Add sys_futex_wait()
In reply to Peter Zijlstrakernel/futex/waitwake.c int futex_wait(u32 __user *uaddr, unsign · line 684
Quoted 12 lines · Peter Zijlstra
kernel/futex/waitwake.c +12
-0+int futex_wait (u32 __user *uaddr,unsigned int flags,u32 val,ktime_t *abs_time,u32 bitset) +{ +struct hrtimer_sleeper timeout, *to; +struct restart_block *restart; +int ret; + + to =futex_setup_timer (abs_time, &timeout, flags, + current->timer_slack_ns); + + ret =__futex_wait (uaddr, flags, val, to, bitset); +if (!to) +return ret;
Can you please put an empty new line and a comment between the __futex_wait() and the if (!to) check? The original code was less obfuscated.
Other than that: Reviewed-by: Thomas Gleixner <[email protected]>
Re: [PATCH v1 07/14] futex: Propagate flags into get_futex_key()
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
Instead of only passing
FLAGS_SHAREDas a boolean, pass down flags as a whole.Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Re: [PATCH v1 08/14] futex: Add flags2 argument to futex_requeue()
In reply to Peter ZijlstraQuoted 2 lines · Peter Zijlstra
In order to support mixed size requeue, add a second flags argument to the internal
futex_requeue()function.
No user visible changes, right?
Re: [PATCH v1 04/14] futex: Validate futex value against futex size
In reply to Peter Zijlstrakernel/futex/futex.h static inline unsigned int futex_size(un · line 92
Quoted 4 lines · Peter Zijlstra
kernel/futex/futex.h +4
-0+static inline bool futex_validate_input (unsigned int flags,u64 val) +{ +int bits =8 *futex_size (flags); +if (bits <64 && (val >> bits))
New line between declaration and code.
kernel/futex/futex.h static inline unsigned int futex_size(un · line 95
Quoted 3 lines
kernel/futex/futex.h +3
-0+return false; +return true; +}
Other than that::
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 4 lines · Thomas Gleixner
Quoted 3 lines · Peter Zijlstra
kernel/futex/syscalls.c +1
-1-#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)
Quoted 8 lines
Review static int futex_parse_waitv(struct fute · line 208
Quoted 5 lines
/**
*futex_parse_waitv- Parse a waitv array from userspace(quoted diff) +0
-0@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct fute if ((aux.flags & ~FUTEX2_MASK) || aux.__reserved) return -EINVAL;With the above aux.flags with
FUTEX2_32set will result in-EINVAL. I don't think that's intentional.
Also if you allow 64bit wide futexes, how is that supposed to work with the existing code, which clearly expects a 32bit uval throughout the place?
Thanks,
tglx
Re: [PATCH v1 09/14] futex: Add sys_futex_requeue()
In reply to Peter ZijlstraQuoted 9 lines · Peter Zijlstra
kernel/futex/syscalls.c +9
-0+/* +* sys_futex_requeue - Requeue a waiter from one futex to another +* @waiters: array describing the source and destination futex +* @flags: unused +* @nr_wake: number of futexes to wake +* @nr_requeue: number of futexes to requeue +* +* Identical to the traditional FUTEX_CMP_REQUEUE op, except it is part of the +* futex2 family of calls.
kernel/futex/syscalls.c SYSCALL_DEFINE6(futex_wait, · line 417
Quoted 10 lines
kernel/futex/syscalls.c +10
-0+*/ + +SYSCALL_DEFINE4 (futex_requeue, + struct futex_waitv __user *, waiters, + unsigned int, flags, + int, nr_wake, + int, nr_requeue) +{ +struct futex_vector futexes[2 ]; +u32 cmpval;
So this is explictely u32. I'm completely confused vs. the 64 bit futex size variant enablement earlier in the series by now.
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 14 lines · Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Peter Zijlstra
kernel/futex/syscalls.c +1
-1-#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)Quoted 8 lines
Review static int futex_parse_waitv(struct fute · line 208
Quoted 5 lines
/**
*futex_parse_waitv- Parse a waitv array from userspace(quoted diff) +0
-0@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct fute if ((aux.flags & ~FUTEX2_MASK) || aux.__reserved) return -EINVAL;With the above aux.flags with
FUTEX2_32set will result in-EINVAL. I don't think that's intentional.
Quoted 3 lines
Also if you allow 64bit wide futexes, how is that supposed to work with the existing code, which clearly expects a 32bit uval throughout the place?
Not allowed yet, these patches only allow 8,16,32. I still need to audit the whole futex core and do 'u32 -> unsigned long' (and everything else that follows from that), and only when that's done can the futex2 syscalls allow FUTEX2_64 on 64bit archs.
So for now, these patches:
- add the FUTEX2_64 flag,
- add 'unsigned long' interface such that
64bit can potentiall use it,
- explicitly disallow having it set.
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
struct
futex_hash_bucket*futex_hash(unionfutex_key*key) {kernel/futex/core.c +4
-2- u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, +u32 hash =jhash2 ((u32 *)key, + offsetof(typeof(*key), both.offset) / sizeof(u32), key->both.offset); +int node = key->both.node; -return &futex_queues[hash & (futex_hashsize -1 )]; +if (node ==-1 ) {
kernel/futex/core.c late_initcall(fail_futex_debugfs); · line 155
Quoted 10 lines
kernel/futex/core.c +10
-0+/* +* In case of !FLAGS_NUMA, use some unused hash bits to pick a +* node -- this ensures regular futexes are interleaved across +* the nodes and avoids having to allocate multiple +* hash-tables. +* +* NOTE: this isn't perfectly uniform, but it is fast and +* handles sparse node masks. +*/ + node = (hash >> futex_hashshift) % nr_node_ids;
Is nr_node_ids guaranteed to be stable after init? It's marked __read_mostly, but not __ro_after_init.
Quoted 8 lines
kernel/futex/core.c +7
-0+if (!node_possible (node)) { + node =find_next_bit_wrap (node_possible_map.bits, + nr_node_ids, node); + } + } + +return &futex_queues[node][hash & (futex_hashsize -1 )]; }
kernel/futex/core.c late_initcall(fail_futex_debugfs);
Quoted 10 lines
kernel/futex/core.c +2
-1fshared = flags &FLAGS_SHARED ; + size =futex_size (flags);/* * The futex address must be "naturally" aligned. */ key->both.offset = address %PAGE_SIZE ; -if (unlikely ((address %sizeof (u32)) !=0 )) +if (unlikely ((address % size) !=0 ))return -EINVAL ;
Hmm. Shouldn't that have changed with the allowance of the 1 and 2 byte futexes?
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 292
Quoted 13 lines
kernel/futex/core.c +5
-1address -= key->both.offset; -if (unlikely (!access_ok (uaddr,sizeof (u32)))) +if (flags &FLAGS_NUMA ) + size *=2 ; + +if (unlikely (!access_ok (uaddr, size)))return -EFAULT ;if (unlikely (should_fail_futex (fshared)))return -EFAULT ; + key->both.node =-1 ;
Please put this into an else path.
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 294
Quoted 2 lines
kernel/futex/core.c +2
-0+if (flags &FLAGS_NUMA ) { +void __user *naddr = uaddr + size/2 ;
size / 2;
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 306
Quoted 12 lines
kernel/futex/core.c +12
-0+ +if (futex_get_value (&node, naddr, flags)) +return -EFAULT ; + +if (node ==-1 ) { + node =numa_node_id (); +if (futex_put_value (node, naddr, flags)) +return -EFAULT ; + } + +if (node >=MAX_NUMNODES || !node_possible (node)) +return -EINVAL ;
That's clearly an else path too. No point in checking whether numa_node_id() is valid.
Quoted 16 lines
kernel/futex/futex.h +8
-0+ key->both.node = node; + } +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ +} +static inline bool futex_flags_valid (unsigned int flags) {/* Only 64bit futexes for 64bit code */ @@ -77,13 +83,19 @@ static inline bool futex_flags_valid(unsif ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 )return false;
kernel/futex/futex.h static inline bool futex_flags_valid(uns · line 93
Quoted 10 lines
kernel/futex/futex.h +8
-2-return true; -} +/* +* Must be able to represent both NUMA_NO_NODE and every valid nodeid +* in a futex word. +*/ +if (flags &FLAGS_NUMA ) { +int bits =8 *futex_size (flags); +u64 max = ~0ULL ; + max >>=64 - bits;
Your newline key is broken, right?
kernel/futex/futex.h static inline bool futex_flags_valid(uns · line 96
Quoted 3 lines
kernel/futex/futex.h +3
-0+if (nr_node_ids >= max) +return false; + }
Thanks,
tglx
Peter Zijlstra
reviewerRe: [PATCH v1 09/14] futex: Add sys_futex_requeue()
In reply to Thomas GleixnerQuoted 15 lines · Thomas Gleixner
Quoted 14 lines · Peter Zijlstra
kernel/futex/syscalls.c +14
-0+/* +* sys_futex_requeue - Requeue a waiter from one futex to another +* @waiters: array describing the source and destination futex +* @flags: unused +* @nr_wake: number of futexes to wake +* @nr_requeue: number of futexes to requeue +* +* Identical to the traditional FUTEX_CMP_REQUEUE op, except it is part of the +* futex2 family of calls. +*/ + +SYSCALL_DEFINE4(futex_requeue, + struct futex_waitv __user *, waiters, + unsigned int, flags,
Quoted 8 lines
kernel/futex/syscalls.c SYSCALL_DEFINE6(futex_wait, · line 417
Quoted 5 lines
kernel/futex/syscalls.c +5
-0+ int, nr_wake, + int, nr_requeue) +{ +struct futex_vector futexes[2 ]; +u32 cmpval;So this is explictely u32. I'm completely confused vs. the 64 bit futex size variant enablement earlier in the series by now.
As per the previous email; these patches only enable the syscall part of 64bit futexes, they do not convert the core, and per futex_flags_valid() (patches 4 and 13), explicitly disallow having FUTEX2_64 set.
- /* Only 32bit futexes are implemented -- for now */ - if ((flags & FLAGS_SIZE_MASK) != FLAGS_SIZE_32) + /* 64bit futexes aren't implemented -- yet */ + if ((flags & FLAGS_SIZE_MASK) == FLAGS_SIZE_64) return false;
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 19 lines · Thomas Gleixner
Quoted 18 lines · Peter Zijlstra
include/uapi/linux/futex.h +4
-1+#define FUTEX2_8 0x00 +#define FUTEX2_16 0x01#define FUTEX2_32 0x02 -/* 0x04 */ +#define FUTEX2_64 0x03 +#define FUTEX2_NUMA 0x04/* 0x08 */ /* 0x10 */ /* 0x20 */ kernel/futex/syscalls.c +1
-1@@ -183,7 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)
Quoted 8 lines
kernel/futex/syscalls.c static int futex_parse_waitv(struct fute · line 208
Quoted 5 lines
kernel/futex/syscalls.c +0
-0/** * futex_parse_waitv - Parse a waitv array from userspace @@ -207,7 +207,12 @@ static int futex_parse_waitv(struct futeif ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved)return -EINVAL ;With the above aux.flags with
FUTEX2_32set will result in-EINVAL. I don't think that's intentional.
Aargh. This is really nasty to make FUTEX2_64 0x3 and abuse it to test the flags for validity. Intuitive and obvious is something else.
Peter Zijlstra
reviewerRe: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Thomas GleixnerQuoted 9 lines · Thomas Gleixner
Quoted 8 lines · Peter Zijlstra
struct
futex_hash_bucket*futex_hash(unionfutex_key*key) {kernel/futex/core.c +3
-1- u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, +u32 hash =jhash2 ((u32 *)key, + offsetof(typeof(*key), both.offset) / sizeof(u32), key->both.offset); +int node = key->both.node;
Quoted 15 lines
kernel/futex/core.c late_initcall(fail_futex_debugfs); · line 155
Quoted 12 lines
kernel/futex/core.c +11
-1-return &futex_queues[hash & (futex_hashsize -1 )]; +if (node ==-1 ) { +/* +* In case of !FLAGS_NUMA, use some unused hash bits to pick a +* node -- this ensures regular futexes are interleaved across +* the nodes and avoids having to allocate multiple +* hash-tables. +* +* NOTE: this isn't perfectly uniform, but it is fast and +* handles sparse node masks. +*/ + node = (hash >> futex_hashshift) % nr_node_ids;Is
nr_node_idsguaranteed to be stable after init? It's marked__read_mostly, but not__ro_after_init.
AFAICT it is only ever written to in setup_nr_node_ids() and that is all __init code. So I'm thinking this could/should indeed be __ro_after_init. Esp. so since it is an exported variable.
Mike?
Quoted 11 lines
Quoted 11 lines
kernel/futex/core.c +8
-0+if (!node_possible (node)) { + node =find_next_bit_wrap (node_possible_map.bits, + nr_node_ids, node); + } + } + +return &futex_queues[node][hash & (futex_hashsize -1 )]; } fshared = flags &FLAGS_SHARED ; + size =futex_size (flags);
Quoted 10 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 280
Quoted 7 lines
kernel/futex/core.c +1
-1/* * The futex address must be "naturally" aligned. */ key->both.offset = address %PAGE_SIZE ; -if (unlikely ((address %sizeof (u32)) !=0 )) +if (unlikely ((address % size) !=0 ))return -EINVAL ;Hmm. Shouldn't that have changed with the allowance of the 1 and 2 byte futexes?
That patches comes after this.. :-)
But I do have an open question here; do we want FUTEX2_NUMA futexes aligned at futex_size or double that? That is, what do we want the alignment of:
struct futex_numa_32 {
u32 val;
u32 node;
};to be? Having that u64 aligned will guarantee these two values end up in the same page, having them u32 aligned (as per this patch) allows for them to be split.
The current paths don't care, we don't hold locks, but perhaps it makes sense to be conservative.
Quoted 9 lines
Quoted 9 lines
kernel/futex/core.c +4
-1address -= key->both.offset; -if (unlikely (!access_ok (uaddr,sizeof (u32)))) +if (flags &FLAGS_NUMA ) + size *=2 ; + +if (unlikely (!access_ok (uaddr, size)))return -EFAULT ;
Quoted 6 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 292
Quoted 4 lines
kernel/futex/core.c +1
-0if (unlikely (should_fail_futex (fshared)))return -EFAULT ; + key->both.node =-1 ;Please put this into an else path.
Can do, but I figured the compiler could figure it out through dead store elimitation or somesuch pass.
Quoted 5 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 294
Quoted 2 lines
kernel/futex/core.c +2
-0+if (flags &FLAGS_NUMA ) { +void __user *naddr = uaddr + size/2 ;size / 2;
Quoted 15 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 306
Quoted 12 lines
kernel/futex/core.c +12
-0+ +if (futex_get_value (&node, naddr, flags)) +return -EFAULT ; + +if (node ==-1 ) { + node =numa_node_id (); +if (futex_put_value (node, naddr, flags)) +return -EFAULT ; + } + +if (node >=MAX_NUMNODES || !node_possible (node)) +return -EINVAL ;That's clearly an else path too. No point in checking whether
numa_node_id()is valid.
No, this also checks if the value we read from userspace is valid.
Only when the value we read from userspace is -1 do we set numa_node_id(), otherwise we take the value as read, which then must be a valid value.
Quoted 16 lines
Quoted 16 lines
kernel/futex/futex.h +8
-0+ key->both.node = node; + } +static inline unsigned int futex_size (unsigned int flags) +{ +unsigned int size = flags &FLAGS_SIZE_MASK ; +return 1 << size;/* {0,1,2,3} -> {1,2,4,8} */ +} +static inline bool futex_flags_valid (unsigned int flags) {/* Only 64bit futexes for 64bit code */ @@ -77,13 +83,19 @@ static inline bool futex_flags_valid(unsif ((flags &FLAGS_SIZE_MASK ) !=FLAGS_SIZE_32 )return false;
Quoted 11 lines
kernel/futex/futex.h static inline bool futex_flags_valid(uns · line 93
Quoted 10 lines
kernel/futex/futex.h +8
-2-return true; -} +/* +* Must be able to represent both NUMA_NO_NODE and every valid nodeid +* in a futex word. +*/ +if (flags &FLAGS_NUMA ) { +int bits =8 *futex_size (flags); +u64 max = ~0ULL ; + max >>=64 - bits;Your newline key is broken, right?
Yes :-)
Quoted 3 lines
Quoted 3 lines
kernel/futex/futex.h +3
-0+if (nr_node_ids >= max) +return false; + }
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 2 lines · Thomas Gleixner
Aargh. This is really nasty to make
FUTEX2_640x3and abuse it to test the flags for validity. Intuitive and obvious is something else.
Like so then?
include/uapi/linux/futex.h +2 -0
@@ -57,6 +57,8 @@/* 0x40 */ #define FUTEX2_PRIVATE FUTEX_PRIVATE_FLAG +#define FUTEX2_SIZE_MASK 0x03 +/* do not use */ #define FUTEX_32 FUTEX2_32/* historical accident :-( */
kernel/futex/syscalls.c +3 -3
@@ -183,7 +183,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uadreturn do_futex (uaddr, op, val, tp, uaddr2, (unsigned long )utime, val3); } -#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE)/** * futex_parse_waitv - Parse a waitv array from userspace
@@ -208,11 +208,11 @@ static int futex_parse_waitv(struct futereturn -EINVAL ;if (!IS_ENABLED (CONFIG_64BIT ) ||in_compat_syscall ()) { -if ((aux.flags &FUTEX2_64 ) ==FUTEX2_64 ) +if ((aux.flags &FUTEX2_SIZE_MASK ) ==FUTEX2_64 )return -EINVAL ; } -if ((aux.flags &FUTEX2_64 ) !=FUTEX2_32 ) +if ((aux.flags &FUTEX2_SIZE_MASK ) !=FUTEX2_32 )return -EINVAL ; futexv[i].w.flags = aux.flags;
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Peter ZijlstraQuoted 24 lines · Peter Zijlstra
Quoted 14 lines · Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Peter Zijlstra
kernel/futex/syscalls.c +1
-1-#define FUTEX2_MASK (FUTEX2_32 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE)Quoted 8 lines
Review static int futex_parse_waitv(struct fute · line 208
Quoted 5 lines
/**
*futex_parse_waitv- Parse a waitv array from userspace(quoted diff) +0
-0@@ -207,7 +207,12 @@ static int futex_parse_waitv(struct fute if ((aux.flags & ~FUTEX2_MASK) || aux.__reserved) return -EINVAL;With the above aux.flags with
FUTEX2_32set will result in-EINVAL. I don't think that's intentional.Quoted 3 lines
Also if you allow 64bit wide futexes, how is that supposed to work with the existing code, which clearly expects a 32bit uval throughout the place?
Not allowed yet, these patches only allow 8,16,32. I still need to audit the whole futex core and do 'u32 -> unsigned long' (and everything else that follows from that), and only when that's done can the futex2 syscalls allow
FUTEX2_64on 64bit archs.
Quoted 6 lines
So for now, these patches:
- add the
FUTEX2_64flag,
- add 'unsigned long' interface such that
64bit can potentiall use it,
- explicitly disallow having it set.
I figured that out very late. This flags having a size fields which claims to be flags had confused the hell out of me.
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Peter ZijlstraReview
Quoted 2 lines · Peter Zijlstra
(quoted diff) +1
-1-#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE)
Along with some comment which documents that the size "flags" constitute a number field and not flags in the sense of binary flags.
And please name these size constants so it really becomes obvious:
#define FUTEX2_SIZE_U32 2
Review static int futex_parse_waitv(struct fute · line 213
Quoted 10 lines
/**
*futex_parse_waitv- Parse a waitv array from userspace(quoted diff) +1
-1@@ -208,11 +208,11 @@ static int futex_parse_waitv(struct fute return -EINVAL; if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) { - if ((aux.flags & FUTEX2_64) == FUTEX2_64) + if ((aux.flags & FUTEX2_SIZE_MASK) == FUTEX2_64) return -EINVAL; }
That should be part of the actual 64bit futex enablement, no?
Review static int futex_parse_waitv(struct fute · line 215
Quoted 3 lines
(quoted diff) +1
-1- if ((aux.flags & FUTEX2_64) != FUTEX2_32) + if ((aux.flags & FUTEX2_SIZE_MASK) != FUTEX2_32) return -EINVAL;
In hindsight I think it was as mistake just to have this __u32 flags field in the new interface. Soemthing like the incomplete below might be retrofittable, no?
include/uapi/linux/futex.h +6 -1
@@ -74,7 +74,12 @@struct futex_waitv {__u64 val;__u64 uaddr; -__u32 flags; +union { +__u32 flags; + __u32 size : 2, + :5 , + private :1 ; + };__u32 __reserved; };
kernel/futex/syscalls.c +2 -2
@@ -204,10 +204,10 @@ static int futex_parse_waitv(struct futeif (copy_from_user (&aux, &uwaitv[i],sizeof (aux)))return -EFAULT ; -if ((aux.flags & ~FUTEX2_MASK ) || aux.__reserved) +if ((aux.flags & ~FUTEX2_VALID_FLAGBITS ) || aux.__reserved)return -EINVAL ; -if (!(aux.flags &FUTEX2_32 )) +if (aux.size !=FUTEX2_SIZE_U32 )return -EINVAL ; futexv[i].w.flags = aux.flags;
If this muck already confused me right now, then I don't want to experience the confusion factor 6 month down the road :)
Thanks,
tglx
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraQuoted 5 lines · Peter Zijlstra
Quoted 2 lines · Thomas Gleixner
Hmm. Shouldn't that have changed with the allowance of the 1 and 2 byte futexes?
That patches comes after this.. :-)
Futexes are really cursed :)
Quoted 4 lines
But I do have an open question here; do we want
FUTEX2_NUMAfutexes aligned atfutex_sizeor double that? That is, what do we want the alignment of:
Quoted 8 lines
struct futex_numa_32 { u32 val; u32 node; };to be? Having that u64 aligned will guarantee these two values end up in the same page, having them u32 aligned (as per this patch) allows for them to be split.
Same page and same cacheline.
Quoted 2 lines
The current paths don't care, we don't hold locks, but perhaps it makes sense to be conservative.
I think it makes sense.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
kernel/futex/core.c +4
-1address -= key->both.offset; -if (unlikely (!access_ok (uaddr,sizeof (u32)))) +if (flags &FLAGS_NUMA ) + size *=2 ; + +if (unlikely (!access_ok (uaddr, size)))return -EFAULT ;if (unlikely (should_fail_futex (fshared)))return -EFAULT ;
Quoted 6 lines
Quoted 3 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 292
Quoted 1 line
kernel/futex/core.c +1
-0+ key->both.node =-1 ;Please put this into an else path.
Can do, but I figured the compiler could figure it out through dead store elimitation or somesuch pass.
Sure, but taste disagrees and it simply makes the code more obvious.
Quoted 21 lines
Quoted 5 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 294
Quoted 2 lines
kernel/futex/core.c +2
-0+if (flags &FLAGS_NUMA ) { +void __user *naddr = uaddr + size/2 ;size / 2;
Quoted 15 lines
kernel/futex/core.c static u64 get_inode_sequence_number(str · line 306
Quoted 12 lines
kernel/futex/core.c +12
-0+ +if (futex_get_value (&node, naddr, flags)) +return -EFAULT ; + +if (node ==-1 ) { + node =numa_node_id (); +if (futex_put_value (node, naddr, flags)) +return -EFAULT ; + } + +if (node >=MAX_NUMNODES || !node_possible (node)) +return -EINVAL ;That's clearly an else path too. No point in checking whether
numa_node_id()is valid.
Quoted 5 lines
No, this also checks if the value we read from userspace is valid.
Only when the value we read from userspace is -1 do we set
numa_node_id(), otherwise we take the value as read, which then must be a valid value.
Right, but:
if (node == -1) {
node = numa_node_id();
if (futex_put_value(node, naddr, flags))
return -EFAULT;
} else if (node >= MAX_NUMNODES || !node_possible(node)) {
return -EINVAL;
}makes it clear that the path where @node read from user space is != -1 needs to be validated, while your version checks the result of
node = numa_node_id();
too, which does not make sense to me. Yes, it works, but ...
Thanks,
tglx
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
Review
Quoted 2 lines · Peter Zijlstra
(quoted diff) +1
-1-#define FUTEX2_MASK (FUTEX2_64 | FUTEX2_PRIVATE) +#define FUTEX2_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE)Along with some comment which documents that the size "flags" constitute a number field and not flags in the sense of binary flags.
And please name these size constants so it really becomes obvious:
#define
FUTEX2_SIZE_U322
So you want them named:
#define FUTEX2_SIZE_U8 0x00
#define FUTEX2_SIZE_U16 0x01
#define FUTEX2_SIZE_U32 0x02
#define FUTEX2_SIZE_U64 0x03
#define FUTEX2_SIZE_MASK 0x03
Sure, can do.
Quoted 5 lines
Quoted 5 lines
/**
*futex_parse_waitv- Parse a waitv array from userspace(quoted diff) +0
-0@@ -208,11 +208,11 @@ static int futex_parse_waitv(struct fute return -EINVAL;
Quoted 7 lines
Review static int futex_parse_waitv(struct fute · line 213
Quoted 5 lines
(quoted diff) +1
-1if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) { - if ((aux.flags & FUTEX2_64) == FUTEX2_64) + if ((aux.flags & FUTEX2_SIZE_MASK) == FUTEX2_64) return -EINVAL; }That should be part of the actual 64bit futex enablement, no?
The 'unsigned long' thing is part of the syscalls, which is why I had it now.
Quoted 14 lines
Review static int futex_parse_waitv(struct fute · line 215
Quoted 4 lines
(quoted diff) +1
-1- if ((aux.flags & FUTEX2_64) != FUTEX2_32) + if ((aux.flags & FUTEX2_SIZE_MASK) != FUTEX2_32) return -EINVAL;In hindsight I think it was as mistake just to have this
__u32flags field in the new interface. Soemthing like the incomplete below might be retrofittable, no?include/uapi/linux/futex.h +0
-0@@ -74,7 +74,12 @@struct futex_waitv {__u64 val;
include/uapi/linux/futex.h line 84
Quoted 10 lines
include/uapi/linux/futex.h +6
-1__u64 uaddr; -__u32 flags; +union { +__u32 flags; + __u32 size : 2, + :5 , + private :1 ; + };__u32 __reserved; };
Durr, I'm not sure I remember if that does the right thing across architectures -- might just work. But I'm fairly sure this isn't the only case of a field in a flags thing in our APIs. Although obviously I can't find another case in a hurry :/
Also, sys_futex_{wake,wait}() have this thing as a syscall argument, surely you don't want to put this union there as well?
I'd much prefer to just keep the 'unsigned int flags' thing and perhaps put a comment on-top of the '#define FUTEX2_*' thingies. Note that having it a field instead of a bunch of flags makes sense, since you can only have a single size, not a combination of sizes.
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Thomas Gleixner
include/uapi/linux/futex.h +5
-1@@ -74,7 +74,12 @@struct futex_waitv {__u64 val;__u64 uaddr; -__u32 flags; +union { +__u32 flags; + __u32 size : 2, + :5 , + private :1 ;
Quoted 8 lines
include/uapi/linux/futex.h line 84
Quoted 3 lines
include/uapi/linux/futex.h +1
-0+ };__u32 __reserved; };Durr, I'm not sure I remember if that does the right thing across architectures -- might just work. But I'm fairly sure this isn't the only case of a field in a flags thing in our APIs. Although obviously I can't find another case in a hurry :/
I know, but that doesn't make these things more readable and neither an argument against doing it for futex2 :)
Quoted 2 lines
Also, sys_futex_{wake,wait}() have this thing as a syscall argument, surely you don't want to put this union there as well?
Why not? The anon union does not break the ABI unless I'm missing something. Existing user space can still use 'flags' and people who care about readability can use the bitfield, no?
Its inside struct futex_waitv and not an explicit syscall argument, right?
Quoted 4 lines
I'd much prefer to just keep the 'unsigned int flags' thing and perhaps put a comment on-top of the '#define FUTEX2_*' thingies. Note that having it a field instead of a bunch of flags makes sense, since you can only have a single size, not a combination of sizes.
I'm aware of that by now :)
Still that explicit bitfield does neither need comments nor does it leave room for interpretation.
Thanks,
tglx
Peter Zijlstra
reviewerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
Quoted 8 lines · Thomas Gleixner
Quoted 2 lines
Also, sys_futex_{wake,wait}() have this thing as a syscall argument, surely you don't want to put this union there as well?
Why not? The anon union does not break the ABI unless I'm missing something. Existing user space can still use 'flags' and people who care about readability can use the bitfield, no?
Its inside struct
futex_waitvand not an explicit syscall argument, right?
Nope, see patches 5 and 6, they introduce:
sys_futex_wake(void __user *uaddr, unsigned long mask, int nr, unsigned int flags); sys_futex_wait(void __user *uaddr, unsigned long val,
unsigned long mask, unsigned int flags,
struct __kernel_timespec __user *timeout, clockid_t clockid);
Using a union, would turn that into:
sys_futex_wake(void __user *uaddr, unsigned long mask, int nr, union futex_flags flags); sys_futex_wait(void __user *uaddr, unsigned long val,
unsigned long mask, union futex_flags flags,
struct __kernel_timespec __user *timeout, clockid_t clockid);
Which then gets people to write garbage like:
futex_wake(add, 0xFFFF, 1, (union futex_flags){ .flags = FUTEX2_SIZE_U16 | FUTEX2_PRIVATE));
or
futex_wake(add, 0xFFFF, 1, (union futex_flags){ .size = FUTEX2_SIZE_U16, private = true, ));You really want that ?
Arnd Bergmann
maintainerRe: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Thomas GleixnerQuoted 25 lines · Thomas Gleixner
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Thomas Gleixner
include/uapi/linux/futex.h +5
-1@@ -74,7 +74,12 @@struct futex_waitv {__u64 val;__u64 uaddr; -__u32 flags; +union { +__u32 flags; + __u32 size : 2, + :5 , + private :1 ;Quoted 8 lines
include/uapi/linux/futex.h line 84
Quoted 3 lines
include/uapi/linux/futex.h +1
-0+ };__u32 __reserved; };Durr, I'm not sure I remember if that does the right thing across architectures -- might just work. But I'm fairly sure this isn't the only case of a field in a flags thing in our APIs. Although obviously I can't find another case in a hurry :/
I know, but that doesn't make these things more readable and neither an argument against doing it for futex2 :)
...
Quoted 3 lines
Still that explicit bitfield does neither need comments nor does it leave room for interpretation.
It may be clear to the compiler, but without comments or looking up psABI documentation I certainly wouldn't know immediately which bits of the flags word overlay the bitfields for a given combination of __BIG_ENDIAN/__LITTLE_ENDIAN and __BIG_ENDIAN_BITFIELD/__LITTLE_ENDIAN_BITFIELD or architectures with unusual struct alignment requirements (m68k or arm-oabi).
I'd prefer to completely avoid the bitfield here. Maybe having exclusive flags for each width would be less confusing, at the cost of needing two more flag bits and a slightly more complicated sanity check, or we could take an extra byte out of the __reserved field to store the length.
Arnd
Re: [PATCH v1 02/14] futex: Extend the FUTEX2 flags
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
On Tue, Aug 01, 2023 at 12:43:24AM +0200, Thomas Gleixner wrote:
Which then gets people to write garbage like:futex_wake(add, 0xFFFF, 1, (union futex_flags){ .flags = FUTEX2_SIZE_U16 | FUTEX2_PRIVATE)); or futex_wake(add, 0xFFFF, 1, (union futex_flags){ .size = FUTEX2_SIZE_U16, private = true, ));You really want that ?
Well, people write garbage no matter what. So just keep the flags and make the names explicit.
Note to myself: /me shouldn't look at futex patches when tired
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Quoted 2 lines
Is
nr_node_idsguaranteed to be stable after init? It's marked__read_mostly, but not__ro_after_init.AFAICT it is only ever written to in
setup_nr_node_ids()and that is all__initcode. So I'm thinking this could/should indeed be__ro_after_init. Esp. so since it is an exported variable.Mike?
Its stable and lots of other components depend on it like f.e. the size of cpumasks.
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Peter ZijlstraQuoted 1 line · Peter Zijlstra
Extend the futex2 interface to be numa aware.
Sorry to be chiming in that late but it seems that this is useful to mitigate NUMA issues also for our platform.
Quoted 3 lines
When
FUTEX2_NUMAis not set, the node is simply an extention of the hash, such that traditional futexes are still interleaved over the nodes.
Could we follow NUMA policies like with other metadata allocations during systen call processing? If there is no NUMA task policy then the futex should be placed on the local NUMA node.
That way the placement of the futex can be controlled by the tasks memory policy. We could skip the FUTEX2_NUMA option.
Quoted 17 lines
(quoted diff) +8
-2@@ -114,10 +137,29 @@ late_initcall(fail_futex_debugfs); */ struct futex_hash_bucket *futex_hash(union futex_key *key) { - u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, + u32 hash = jhash2((u32 *)key, + offsetof(typeof(*key), both.offset) / sizeof(u32), key->both.offset); + int node = key->both.node; - return &futex_queues[hash & (futex_hashsize - 1)]; + if (node == -1) { + /* + * In case of !FLAGS_NUMA, use some unused hash bits to pick a + * node -- this ensures regular futexes are interleaved across + * the nodes and avoids having to allocate multiple
Review late_initcall(fail_futex_debugfs); · line 160
Quoted 10 lines
(quoted diff) +10
-0+ * hash-tables. + * + * NOTE: this isn't perfectly uniform, but it is fast and + * handles sparse node masks. + */ + node = (hash >> futex_hashshift) % nr_node_ids; + if (!node_possible(node)) { + node = find_next_bit_wrap(node_possible_map.bits, + nr_node_ids, node); + }
Use memory allocation policies here instead?
Peter Zijlstra
reviewerRe: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
Quoted 6 lines · Christoph Lameter (Ampere)
Quoted 1 line · Peter Zijlstra
Extend the futex2 interface to be numa aware.
Sorry to be chiming in that late but it seems that this is useful to mitigate NUMA issues also for our platform.
I read this like: I tested it and it works for me. Is that a correct reading of your statement?
If so, I'll look at bumping this on the priority list and I'll look at the placement suggestion you had when I respin the patches.
Thanks!
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
Quoted 2 lines · Peter Zijlstra
I read this like: I tested it and it works for me. Is that a correct reading of your statement?
We tested it and it works as we expected.
Which in turn raises the issue if this could be done to other large system hashes as well.
Peter Zijlstra
reviewerRe: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
In reply to Christoph Lameter (Ampere)Quoted 6 lines · Christoph Lameter (Ampere)
Quoted 3 lines
When
FUTEX2_NUMAis not set, the node is simply an extention of the hash, such that traditional futexes are still interleaved over the nodes.Could we follow NUMA policies like with other metadata allocations during systen call processing?
I had a quick look at this, and since the mempolicy stuff is per vma, and we don't have the vma, this is going to be terribly expensive -- mmap_lock and all that.
Once lockless vma lookups land (soonish, perhaps), this could be reconsidered. But for now there just isn't a sane way to do this.
Using memory policies is probably okay -- but still risky, since you get the extra failure case where if you change the mempolicy between WAIT and WAKE things will not match and sadness happens, but that *SHOULD* hopefully not happen a lot. Mempolicies are typically fairly static.
Quoted 5 lines
If there is no NUMA task policy then the futex
should be placed on the local NUMA node.That way the placement of the futex can be controlled by the tasks memory policy. We could skip the
FUTEX2_NUMAoption.
That doesn't work. If we don't have storage for the node across WAIT/WAKE, then the node must be deterministic per futex_hash(). Otherwise wake has no chance of finding the entry.
Consider our random unbound task with no policies etc. (default state) doing FUTEX_WAIT and going to sleep while on node-0, it's sibling thread, that happens to run on node-1 issues FUTEX_WAKE.
If they disagree on determining 'node', then they will not find match and the wakeup doesn't happen and userspace gets really sad.
The current scheme where we determine node based on hash bits is fully deterministic and WAIT/WAKE will agree on which node-hash to use. The interleave is no worse than the global hash today -- OTOH it also isn't better.
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
Sorry saw this after the other email.
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines
Could we follow NUMA policies like with other metadata allocations during systen call processing?
I had a quick look at this, and since the mempolicy stuff is per vma, and we don't have the vma, this is going to be terribly expensive --
mmap_lockand all that.
There is a memory policy for the task as a whole that is used for slab allocations and allocations that are not vma bound in current->mempolicy. Use that.
Quoted 4 lines
Using memory policies is probably okay -- but still risky, since you get the extra failure case where if you change the mempolicy between WAIT and WAKE things will not match and sadness happens, but that *SHOULD* hopefully not happen a lot. Mempolicies are typically fairly static.
Right.
Quoted 6 lines
Quoted 2 lines
That way the placement of the futex can be controlled by the tasks memory policy. We could skip the
FUTEX2_NUMAoption.That doesn't work. If we don't have storage for the node across WAIT/WAKE, then the node must be deterministic per
futex_hash(). Otherwise wake has no chance of finding the entry.
You can get a node number following the current task mempolicy by calling mempolicy_slab_node() and keep using that node for the future.
It is also possible to check if the policy is interleave and then follow the distributed hash scheme.
Quoted 4 lines
The current scheme where we determine node based on hash bits is fully deterministic and WAIT/WAKE will agree on which node-hash to use. The interleave is no worse than the global hash today -- OTOH it also isn't better.
This is unexpected strange behavior for those familiar with NUMA. We have tools to set memory policies for tasks and those policies should be used throughout.
Peter Zijlstra
reviewerRe: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
Quoted 12 lines · Christoph Lameter (Ampere)
Sorry saw this after the other email.
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines
Could we follow NUMA policies like with other metadata allocations during systen call processing?
I had a quick look at this, and since the mempolicy stuff is per vma, and we don't have the vma, this is going to be terribly expensive --
mmap_lockand all that.
Quoted 6 lines
There is a memory policy for the task as a whole that is used for slab allocations and allocations that are not vma bound in
current->mempolicy. Use that.You can get a node number following the current task mempolicy by calling
mempolicy_slab_node()and keep using that node for the future.
I'll look into the per task thing, which I'm hoping means per-process. We need something that is mm wide consistent.
But since futexes play in the address space, I was really rather thinking we ought to use the vma policy.
Re: [PATCH v1 11/14] futex: Implement FUTEX2_NUMA
Quoted 2 lines · Peter Zijlstra
I'll look into the per task thing, which I'm hoping means per-process. We need something that is mm wide consistent.
Each thread can modify its policy and that is used f.e. to control memory allocations for syscalls. For example a thread wants to allocate kernel metadata on a specific node then the policy would be set to that node. Syscall is done and then the tasks resets the policy to the default.
mm wide memory policies are set at an VMA level and are associated with addresses.
Quoted 2 lines
But since futexes play in the address space, I was really rather thinking we ought to use the vma policy.
If they are associated with an address then you can use the address space policy.