futex: Make syscall entry points less convoluted

The futex and the compat syscall entry points do pretty much the same
except for the timespec data types and the corresponding copy from
user function.

Split out the rest into inline functions and share the functionality.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210422194705.244476369@linutronix.de

+37 -26
+37 -26
kernel/futex.c
··· 3757 3757 return -ENOSYS; 3758 3758 } 3759 3759 3760 + static __always_inline bool futex_cmd_has_timeout(u32 cmd) 3761 + { 3762 + switch (cmd) { 3763 + case FUTEX_WAIT: 3764 + case FUTEX_LOCK_PI: 3765 + case FUTEX_WAIT_BITSET: 3766 + case FUTEX_WAIT_REQUEUE_PI: 3767 + return true; 3768 + } 3769 + return false; 3770 + } 3771 + 3772 + static __always_inline int 3773 + futex_init_timeout(u32 cmd, u32 op, struct timespec64 *ts, ktime_t *t) 3774 + { 3775 + if (!timespec64_valid(ts)) 3776 + return -EINVAL; 3777 + 3778 + *t = timespec64_to_ktime(*ts); 3779 + if (cmd == FUTEX_WAIT) 3780 + *t = ktime_add_safe(ktime_get(), *t); 3781 + else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME)) 3782 + *t = timens_ktime_to_host(CLOCK_MONOTONIC, *t); 3783 + return 0; 3784 + } 3760 3785 3761 3786 SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, 3762 3787 const struct __kernel_timespec __user *, utime, 3763 3788 u32 __user *, uaddr2, u32, val3) 3764 3789 { 3765 - struct timespec64 ts; 3790 + int ret, cmd = op & FUTEX_CMD_MASK; 3766 3791 ktime_t t, *tp = NULL; 3767 - int cmd = op & FUTEX_CMD_MASK; 3792 + struct timespec64 ts; 3768 3793 3769 - if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI || 3770 - cmd == FUTEX_WAIT_BITSET || 3771 - cmd == FUTEX_WAIT_REQUEUE_PI)) { 3794 + if (utime && futex_cmd_has_timeout(cmd)) { 3772 3795 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG)))) 3773 3796 return -EFAULT; 3774 3797 if (get_timespec64(&ts, utime)) 3775 3798 return -EFAULT; 3776 - if (!timespec64_valid(&ts)) 3777 - return -EINVAL; 3778 - 3779 - t = timespec64_to_ktime(ts); 3780 - if (cmd == FUTEX_WAIT) 3781 - t = ktime_add_safe(ktime_get(), t); 3782 - else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME)) 3783 - t = timens_ktime_to_host(CLOCK_MONOTONIC, t); 3799 + ret = futex_init_timeout(cmd, op, &ts, &t); 3800 + if (ret) 3801 + return ret; 3784 3802 tp = &t; 3785 3803 } 3786 3804 ··· 3968 3950 const struct old_timespec32 __user *, utime, u32 __user *, uaddr2, 3969 3951 u32, val3) 3970 3952 { 3971 - struct timespec64 ts; 3953 + int ret, cmd = op & FUTEX_CMD_MASK; 3972 3954 ktime_t t, *tp = NULL; 3973 - int cmd = op & FUTEX_CMD_MASK; 3955 + struct timespec64 ts; 3974 3956 3975 - if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI || 3976 - cmd == FUTEX_WAIT_BITSET || 3977 - cmd == FUTEX_WAIT_REQUEUE_PI)) { 3957 + if (utime && futex_cmd_has_timeout(cmd)) { 3978 3958 if (get_old_timespec32(&ts, utime)) 3979 3959 return -EFAULT; 3980 - if (!timespec64_valid(&ts)) 3981 - return -EINVAL; 3982 - 3983 - t = timespec64_to_ktime(ts); 3984 - if (cmd == FUTEX_WAIT) 3985 - t = ktime_add_safe(ktime_get(), t); 3986 - else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME)) 3987 - t = timens_ktime_to_host(CLOCK_MONOTONIC, t); 3960 + ret = futex_init_timeout(cmd, op, &ts, &t); 3961 + if (ret) 3962 + return ret; 3988 3963 tp = &t; 3989 3964 } 3990 3965