Linux 5.6.13
[linux/fpc-iii.git] / include / asm-generic / futex.h
blob02970b11f71f47b5e2ef93853d24587af530e702
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_FUTEX_H
3 #define _ASM_GENERIC_FUTEX_H
5 #include <linux/futex.h>
6 #include <linux/uaccess.h>
7 #include <asm/errno.h>
9 #ifndef CONFIG_SMP
11 * The following implementation only for uniprocessor machines.
12 * It relies on preempt_disable() ensuring mutual exclusion.
16 /**
17 * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
18 * argument and comparison of the previous
19 * futex value with another constant.
21 * @encoded_op: encoded operation to execute
22 * @uaddr: pointer to user space address
24 * Return:
25 * 0 - On success
26 * -EFAULT - User access resulted in a page fault
27 * -EAGAIN - Atomic operation was unable to complete due to contention
28 * -ENOSYS - Operation not supported
30 static inline int
31 arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
33 int oldval, ret;
34 u32 tmp;
36 preempt_disable();
37 pagefault_disable();
39 ret = -EFAULT;
40 if (unlikely(get_user(oldval, uaddr) != 0))
41 goto out_pagefault_enable;
43 ret = 0;
44 tmp = oldval;
46 switch (op) {
47 case FUTEX_OP_SET:
48 tmp = oparg;
49 break;
50 case FUTEX_OP_ADD:
51 tmp += oparg;
52 break;
53 case FUTEX_OP_OR:
54 tmp |= oparg;
55 break;
56 case FUTEX_OP_ANDN:
57 tmp &= ~oparg;
58 break;
59 case FUTEX_OP_XOR:
60 tmp ^= oparg;
61 break;
62 default:
63 ret = -ENOSYS;
66 if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
67 ret = -EFAULT;
69 out_pagefault_enable:
70 pagefault_enable();
71 preempt_enable();
73 if (ret == 0)
74 *oval = oldval;
76 return ret;
79 /**
80 * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
81 * uaddr with newval if the current value is
82 * oldval.
83 * @uval: pointer to store content of @uaddr
84 * @uaddr: pointer to user space address
85 * @oldval: old value
86 * @newval: new value to store to @uaddr
88 * Return:
89 * 0 - On success
90 * -EFAULT - User access resulted in a page fault
91 * -EAGAIN - Atomic operation was unable to complete due to contention
92 * -ENOSYS - Function not implemented (only if !HAVE_FUTEX_CMPXCHG)
94 static inline int
95 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
96 u32 oldval, u32 newval)
98 u32 val;
100 preempt_disable();
101 if (unlikely(get_user(val, uaddr) != 0)) {
102 preempt_enable();
103 return -EFAULT;
106 if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
107 preempt_enable();
108 return -EFAULT;
111 *uval = val;
112 preempt_enable();
114 return 0;
117 #else
118 static inline int
119 arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
121 return -ENOSYS;
124 static inline int
125 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
126 u32 oldval, u32 newval)
128 return -ENOSYS;
131 #endif /* CONFIG_SMP */
132 #endif