Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[linux-block.git] / include / asm-generic / futex.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
f8aaeace
JD
2#ifndef _ASM_GENERIC_FUTEX_H
3#define _ASM_GENERIC_FUTEX_H
4
f8aaeace 5#include <linux/futex.h>
730f412c 6#include <linux/uaccess.h>
f8aaeace 7#include <asm/errno.h>
f8aaeace 8
00f634bc
LFT
9#ifndef CONFIG_SMP
10/*
11 * The following implementation only for uniprocessor machines.
f3dae07e 12 * It relies on preempt_disable() ensuring mutual exclusion.
00f634bc
LFT
13 *
14 */
15
16/**
30d6e0a4 17 * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
00f634bc
LFT
18 * argument and comparison of the previous
19 * futex value with another constant.
20 *
21 * @encoded_op: encoded operation to execute
22 * @uaddr: pointer to user space address
23 *
24 * Return:
25 * 0 - On success
42750351
WD
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
00f634bc
LFT
29 */
30static inline int
30d6e0a4 31arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
00f634bc 32{
00f634bc
LFT
33 int oldval, ret;
34 u32 tmp;
35
f3dae07e 36 preempt_disable();
00f634bc
LFT
37
38 ret = -EFAULT;
39 if (unlikely(get_user(oldval, uaddr) != 0))
40 goto out_pagefault_enable;
41
42 ret = 0;
43 tmp = oldval;
44
45 switch (op) {
46 case FUTEX_OP_SET:
47 tmp = oparg;
48 break;
49 case FUTEX_OP_ADD:
50 tmp += oparg;
51 break;
52 case FUTEX_OP_OR:
53 tmp |= oparg;
54 break;
55 case FUTEX_OP_ANDN:
56 tmp &= ~oparg;
57 break;
58 case FUTEX_OP_XOR:
59 tmp ^= oparg;
60 break;
61 default:
62 ret = -ENOSYS;
63 }
64
65 if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
66 ret = -EFAULT;
67
68out_pagefault_enable:
f3dae07e 69 preempt_enable();
00f634bc 70
30d6e0a4
JS
71 if (ret == 0)
72 *oval = oldval;
73
00f634bc
LFT
74 return ret;
75}
76
77/**
78 * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
79 * uaddr with newval if the current value is
80 * oldval.
81 * @uval: pointer to store content of @uaddr
82 * @uaddr: pointer to user space address
83 * @oldval: old value
84 * @newval: new value to store to @uaddr
85 *
86 * Return:
87 * 0 - On success
42750351
WD
88 * -EFAULT - User access resulted in a page fault
89 * -EAGAIN - Atomic operation was unable to complete due to contention
90 * -ENOSYS - Function not implemented (only if !HAVE_FUTEX_CMPXCHG)
00f634bc
LFT
91 */
92static inline int
93futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
94 u32 oldval, u32 newval)
95{
96 u32 val;
97
d9b9ff8c 98 preempt_disable();
fba7cd68
RP
99 if (unlikely(get_user(val, uaddr) != 0)) {
100 preempt_enable();
00f634bc 101 return -EFAULT;
fba7cd68 102 }
00f634bc 103
fba7cd68
RP
104 if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
105 preempt_enable();
00f634bc 106 return -EFAULT;
fba7cd68 107 }
00f634bc
LFT
108
109 *uval = val;
d9b9ff8c 110 preempt_enable();
00f634bc
LFT
111
112 return 0;
113}
114
115#else
f8aaeace 116static inline int
30d6e0a4 117arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
f8aaeace 118{
f9adc23e 119 return -ENOSYS;
f8aaeace
JD
120}
121
e9056f13 122static inline int
8d7718aa
ML
123futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
124 u32 oldval, u32 newval)
e9056f13
IM
125{
126 return -ENOSYS;
127}
128
00f634bc 129#endif /* CONFIG_SMP */
f8aaeace 130#endif