Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 1 | #ifndef _ASM_GENERIC_FUTEX_H |
| 2 | #define _ASM_GENERIC_FUTEX_H |
| 3 | |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 4 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 5 | #include <linux/uaccess.h> |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 6 | #include <asm/errno.h> |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 7 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 8 | #ifndef CONFIG_SMP |
| 9 | /* |
| 10 | * The following implementation only for uniprocessor machines. |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 11 | * It relies on preempt_disable() ensuring mutual exclusion. |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 12 | * |
| 13 | */ |
| 14 | |
| 15 | /** |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 16 | * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 17 | * argument and comparison of the previous |
| 18 | * futex value with another constant. |
| 19 | * |
| 20 | * @encoded_op: encoded operation to execute |
| 21 | * @uaddr: pointer to user space address |
| 22 | * |
| 23 | * Return: |
| 24 | * 0 - On success |
| 25 | * <0 - On error |
| 26 | */ |
| 27 | static inline int |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 28 | arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr) |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 29 | { |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 30 | int oldval, ret; |
| 31 | u32 tmp; |
| 32 | |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 33 | preempt_disable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 34 | pagefault_disable(); |
| 35 | |
| 36 | ret = -EFAULT; |
| 37 | if (unlikely(get_user(oldval, uaddr) != 0)) |
| 38 | goto out_pagefault_enable; |
| 39 | |
| 40 | ret = 0; |
| 41 | tmp = oldval; |
| 42 | |
| 43 | switch (op) { |
| 44 | case FUTEX_OP_SET: |
| 45 | tmp = oparg; |
| 46 | break; |
| 47 | case FUTEX_OP_ADD: |
| 48 | tmp += oparg; |
| 49 | break; |
| 50 | case FUTEX_OP_OR: |
| 51 | tmp |= oparg; |
| 52 | break; |
| 53 | case FUTEX_OP_ANDN: |
| 54 | tmp &= ~oparg; |
| 55 | break; |
| 56 | case FUTEX_OP_XOR: |
| 57 | tmp ^= oparg; |
| 58 | break; |
| 59 | default: |
| 60 | ret = -ENOSYS; |
| 61 | } |
| 62 | |
| 63 | if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0)) |
| 64 | ret = -EFAULT; |
| 65 | |
| 66 | out_pagefault_enable: |
| 67 | pagefault_enable(); |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 68 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 69 | |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 70 | if (ret == 0) |
| 71 | *oval = oldval; |
| 72 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the |
| 78 | * uaddr with newval if the current value is |
| 79 | * oldval. |
| 80 | * @uval: pointer to store content of @uaddr |
| 81 | * @uaddr: pointer to user space address |
| 82 | * @oldval: old value |
| 83 | * @newval: new value to store to @uaddr |
| 84 | * |
| 85 | * Return: |
| 86 | * 0 - On success |
| 87 | * <0 - On error |
| 88 | */ |
| 89 | static inline int |
| 90 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 91 | u32 oldval, u32 newval) |
| 92 | { |
| 93 | u32 val; |
| 94 | |
David Hildenbrand | d9b9ff8 | 2015-05-11 17:52:14 +0200 | [diff] [blame] | 95 | preempt_disable(); |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 96 | if (unlikely(get_user(val, uaddr) != 0)) { |
| 97 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 98 | return -EFAULT; |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 99 | } |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 100 | |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 101 | if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) { |
| 102 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 103 | return -EFAULT; |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 104 | } |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 105 | |
| 106 | *uval = val; |
David Hildenbrand | d9b9ff8 | 2015-05-11 17:52:14 +0200 | [diff] [blame] | 107 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | #else |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 113 | static inline int |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 114 | arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr) |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 115 | { |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 116 | int oldval = 0, ret; |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 117 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 118 | pagefault_disable(); |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 119 | |
| 120 | switch (op) { |
| 121 | case FUTEX_OP_SET: |
| 122 | case FUTEX_OP_ADD: |
| 123 | case FUTEX_OP_OR: |
| 124 | case FUTEX_OP_ANDN: |
| 125 | case FUTEX_OP_XOR: |
| 126 | default: |
| 127 | ret = -ENOSYS; |
| 128 | } |
| 129 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 130 | pagefault_enable(); |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 131 | |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 132 | if (!ret) |
| 133 | *oval = oldval; |
| 134 | |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 138 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 139 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 140 | u32 oldval, u32 newval) |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 141 | { |
| 142 | return -ENOSYS; |
| 143 | } |
| 144 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 145 | #endif /* CONFIG_SMP */ |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 146 | #endif |