mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-05-24 10:39:52 +00:00
futex: Flag conversion
Futex 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. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: André Almeida <andrealmeid@igalia.com> Link: https://lore.kernel.org/r/20230921105247.722140574@noisy.programming.kicks-ass.net
This commit is contained in:
parent
d6d08d2479
commit
5694289ce1
3 changed files with 71 additions and 20 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include <linux/futex.h>
|
#include <linux/futex.h>
|
||||||
#include <linux/rtmutex.h>
|
#include <linux/rtmutex.h>
|
||||||
#include <linux/sched/wake_q.h>
|
#include <linux/sched/wake_q.h>
|
||||||
|
#include <linux/compat.h>
|
||||||
|
|
||||||
#ifdef CONFIG_PREEMPT_RT
|
#ifdef CONFIG_PREEMPT_RT
|
||||||
#include <linux/rcuwait.h>
|
#include <linux/rcuwait.h>
|
||||||
|
@ -16,8 +17,15 @@
|
||||||
* Futex flags used to encode options to functions and preserve them across
|
* Futex flags used to encode options to functions and preserve them across
|
||||||
* restarts.
|
* 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
|
#ifdef CONFIG_MMU
|
||||||
# define FLAGS_SHARED 0x01
|
# define FLAGS_SHARED 0x10
|
||||||
#else
|
#else
|
||||||
/*
|
/*
|
||||||
* NOMMU does not have per process address space. Let the compiler optimize
|
* NOMMU does not have per process address space. Let the compiler optimize
|
||||||
|
@ -25,8 +33,57 @@
|
||||||
*/
|
*/
|
||||||
# define FLAGS_SHARED 0x00
|
# define FLAGS_SHARED 0x00
|
||||||
#endif
|
#endif
|
||||||
#define FLAGS_CLOCKRT 0x02
|
#define FLAGS_CLOCKRT 0x20
|
||||||
#define FLAGS_HAS_TIMEOUT 0x04
|
#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_SIZE_MASK;
|
||||||
|
|
||||||
|
if (!(flags2 & FUTEX2_PRIVATE))
|
||||||
|
flags |= FLAGS_SHARED;
|
||||||
|
|
||||||
|
if (flags2 & FUTEX2_NUMA)
|
||||||
|
flags |= FLAGS_NUMA;
|
||||||
|
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned int futex_size(unsigned int flags)
|
||||||
|
{
|
||||||
|
return 1 << (flags & FLAGS_SIZE_MASK);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_FAIL_FUTEX
|
#ifdef CONFIG_FAIL_FUTEX
|
||||||
extern bool should_fail_futex(bool fshared);
|
extern bool should_fail_futex(bool fshared);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <linux/compat.h>
|
|
||||||
#include <linux/syscalls.h>
|
#include <linux/syscalls.h>
|
||||||
#include <linux/time_namespace.h>
|
#include <linux/time_namespace.h>
|
||||||
|
|
||||||
|
@ -85,15 +84,12 @@ err_unlock:
|
||||||
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
|
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
|
||||||
u32 __user *uaddr2, u32 val2, u32 val3)
|
u32 __user *uaddr2, u32 val2, u32 val3)
|
||||||
{
|
{
|
||||||
|
unsigned int flags = futex_to_flags(op);
|
||||||
int cmd = op & FUTEX_CMD_MASK;
|
int cmd = op & FUTEX_CMD_MASK;
|
||||||
unsigned int flags = 0;
|
|
||||||
|
|
||||||
if (!(op & FUTEX_PRIVATE_FLAG))
|
if (flags & FLAGS_CLOCKRT) {
|
||||||
flags |= FLAGS_SHARED;
|
if (cmd != FUTEX_WAIT_BITSET &&
|
||||||
|
cmd != FUTEX_WAIT_REQUEUE_PI &&
|
||||||
if (op & FUTEX_CLOCK_REALTIME) {
|
|
||||||
flags |= FLAGS_CLOCKRT;
|
|
||||||
if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI &&
|
|
||||||
cmd != FUTEX_LOCK_PI2)
|
cmd != FUTEX_LOCK_PI2)
|
||||||
return -ENOSYS;
|
return -ENOSYS;
|
||||||
}
|
}
|
||||||
|
@ -201,21 +197,19 @@ static int futex_parse_waitv(struct futex_vector *futexv,
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < nr_futexes; i++) {
|
for (i = 0; i < nr_futexes; i++) {
|
||||||
|
unsigned int flags;
|
||||||
|
|
||||||
if (copy_from_user(&aux, &uwaitv[i], sizeof(aux)))
|
if (copy_from_user(&aux, &uwaitv[i], sizeof(aux)))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
if ((aux.flags & ~FUTEX2_VALID_MASK) || aux.__reserved)
|
if ((aux.flags & ~FUTEX2_VALID_MASK) || aux.__reserved)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) {
|
flags = futex2_to_flags(aux.flags);
|
||||||
if ((aux.flags & FUTEX2_SIZE_MASK) == FUTEX2_SIZE_U64)
|
if (!futex_flags_valid(flags))
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((aux.flags & FUTEX2_SIZE_MASK) != FUTEX2_SIZE_U32)
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
futexv[i].w.flags = aux.flags;
|
futexv[i].w.flags = flags;
|
||||||
futexv[i].w.val = aux.val;
|
futexv[i].w.val = aux.val;
|
||||||
futexv[i].w.uaddr = aux.uaddr;
|
futexv[i].w.uaddr = aux.uaddr;
|
||||||
futexv[i].q = futex_q_init;
|
futexv[i].q = futex_q_init;
|
||||||
|
|
|
@ -419,11 +419,11 @@ static int futex_wait_multiple_setup(struct futex_vector *vs, int count, int *wo
|
||||||
*/
|
*/
|
||||||
retry:
|
retry:
|
||||||
for (i = 0; i < count; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
if ((vs[i].w.flags & FUTEX_PRIVATE_FLAG) && retry)
|
if (!(vs[i].w.flags & FLAGS_SHARED) && retry)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ret = get_futex_key(u64_to_user_ptr(vs[i].w.uaddr),
|
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);
|
&vs[i].q.key, FUTEX_READ);
|
||||||
|
|
||||||
if (unlikely(ret))
|
if (unlikely(ret))
|
||||||
|
|
Loading…
Add table
Reference in a new issue