Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 2 | #ifndef _ASM_GENERIC_FUTEX_H |
| 3 | #define _ASM_GENERIC_FUTEX_H |
| 4 | |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 5 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 7 | #include <asm/errno.h> |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 8 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 9 | #ifndef CONFIG_SMP |
| 10 | /* |
| 11 | * The following implementation only for uniprocessor machines. |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 12 | * It relies on preempt_disable() ensuring mutual exclusion. |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 13 | * |
| 14 | */ |
| 15 | |
| 16 | /** |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 17 | * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 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 |
Will Deacon | 4275035 | 2019-04-10 11:51:54 +0100 | [diff] [blame] | 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 |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 29 | */ |
| 30 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 31 | 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] | 32 | { |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 33 | int oldval, ret; |
| 34 | u32 tmp; |
| 35 | |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 36 | preempt_disable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 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 | |
| 68 | out_pagefault_enable: |
David Hildenbrand | f3dae07 | 2015-05-11 17:52:13 +0200 | [diff] [blame] | 69 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 70 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 71 | if (ret == 0) |
| 72 | *oval = oldval; |
| 73 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 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 |
Will Deacon | 4275035 | 2019-04-10 11:51:54 +0100 | [diff] [blame] | 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) |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 91 | */ |
| 92 | static inline int |
| 93 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 94 | u32 oldval, u32 newval) |
| 95 | { |
| 96 | u32 val; |
| 97 | |
David Hildenbrand | d9b9ff8 | 2015-05-11 17:52:14 +0200 | [diff] [blame] | 98 | preempt_disable(); |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 99 | if (unlikely(get_user(val, uaddr) != 0)) { |
| 100 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 101 | return -EFAULT; |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 102 | } |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 103 | |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 104 | if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) { |
| 105 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 106 | return -EFAULT; |
Romain Perier | fba7cd6 | 2016-04-14 15:36:03 +0200 | [diff] [blame] | 107 | } |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 108 | |
| 109 | *uval = val; |
David Hildenbrand | d9b9ff8 | 2015-05-11 17:52:14 +0200 | [diff] [blame] | 110 | preempt_enable(); |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #else |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 116 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 117 | 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] | 118 | { |
Vasily Averin | f9adc23 | 2019-07-16 09:22:03 +0300 | [diff] [blame] | 119 | return -ENOSYS; |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 122 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 123 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 124 | u32 oldval, u32 newval) |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 125 | { |
| 126 | return -ENOSYS; |
| 127 | } |
| 128 | |
Ley Foon Tan | 00f634b | 2014-11-06 15:19:34 +0800 | [diff] [blame] | 129 | #endif /* CONFIG_SMP */ |
Jeff Dike | f8aaeace | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 130 | #endif |