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