Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | |
| 4 | ATOMICDIR=$(dirname $0) |
| 5 | |
| 6 | . ${ATOMICDIR}/atomic-tbl.sh |
| 7 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 8 | #gen_param_check(meta, arg) |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 9 | gen_param_check() |
| 10 | { |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 11 | local meta="$1"; shift |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 12 | local arg="$1"; shift |
| 13 | local type="${arg%%:*}" |
| 14 | local name="$(gen_param_name "${arg}")" |
| 15 | local rw="write" |
| 16 | |
| 17 | case "${type#c}" in |
| 18 | i) return;; |
| 19 | esac |
| 20 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 21 | if [ ${type#c} != ${type} ]; then |
| 22 | # We don't write to constant parameters. |
| 23 | rw="read" |
| 24 | elif [ "${meta}" != "s" ]; then |
| 25 | # An atomic RMW: if this parameter is not a constant, and this atomic is |
| 26 | # not just a 's'tore, this parameter is both read from and written to. |
| 27 | rw="read_write" |
| 28 | fi |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 29 | |
Marco Elver | ed8af2e | 2020-01-21 17:05:09 +0100 | [diff] [blame] | 30 | printf "\tinstrument_atomic_${rw}(${name}, sizeof(*${name}));\n" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 33 | #gen_params_checks(meta, arg...) |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 34 | gen_params_checks() |
| 35 | { |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 36 | local meta="$1"; shift |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 37 | local order="$1"; shift |
| 38 | |
| 39 | if [ "${order}" = "_release" ]; then |
| 40 | printf "\tkcsan_release();\n" |
| 41 | elif [ -z "${order}" ] && ! meta_in "$meta" "slv"; then |
| 42 | # RMW with return value is fully ordered |
| 43 | printf "\tkcsan_mb();\n" |
| 44 | fi |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 45 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 46 | while [ "$#" -gt 0 ]; do |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 47 | gen_param_check "$meta" "$1" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 48 | shift; |
| 49 | done |
| 50 | } |
| 51 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 52 | #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...) |
| 53 | gen_proto_order_variant() |
| 54 | { |
| 55 | local meta="$1"; shift |
| 56 | local pfx="$1"; shift |
| 57 | local name="$1"; shift |
| 58 | local sfx="$1"; shift |
| 59 | local order="$1"; shift |
| 60 | local atomic="$1"; shift |
| 61 | local int="$1"; shift |
| 62 | |
| 63 | local atomicname="${atomic}_${pfx}${name}${sfx}${order}" |
| 64 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 65 | local ret="$(gen_ret_type "${meta}" "${int}")" |
| 66 | local params="$(gen_params "${int}" "${atomic}" "$@")" |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 67 | local checks="$(gen_params_checks "${meta}" "${order}" "$@")" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 68 | local args="$(gen_args "$@")" |
| 69 | local retstmt="$(gen_ret_stmt "${meta}")" |
| 70 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 71 | cat <<EOF |
Marco Elver | c020395 | 2019-11-26 15:04:04 +0100 | [diff] [blame] | 72 | static __always_inline ${ret} |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 73 | ${atomicname}(${params}) |
| 74 | { |
| 75 | ${checks} |
| 76 | ${retstmt}arch_${atomicname}(${args}); |
| 77 | } |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 78 | EOF |
| 79 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 80 | printf "\n" |
| 81 | } |
| 82 | |
| 83 | gen_xchg() |
| 84 | { |
| 85 | local xchg="$1"; shift |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 86 | local order="$1"; shift |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 87 | local mult="$1"; shift |
| 88 | |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 89 | kcsan_barrier="" |
| 90 | if [ "${xchg%_local}" = "${xchg}" ]; then |
| 91 | case "$order" in |
| 92 | _release) kcsan_barrier="kcsan_release()" ;; |
| 93 | "") kcsan_barrier="kcsan_mb()" ;; |
| 94 | esac |
| 95 | fi |
| 96 | |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 97 | if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then |
| 98 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 99 | cat <<EOF |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 100 | #define ${xchg}${order}(ptr, oldp, ...) \\ |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 101 | ({ \\ |
| 102 | typeof(ptr) __ai_ptr = (ptr); \\ |
| 103 | typeof(oldp) __ai_oldp = (oldp); \\ |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 104 | EOF |
| 105 | [ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n" |
| 106 | cat <<EOF |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 107 | instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\ |
| 108 | instrument_atomic_write(__ai_oldp, ${mult}sizeof(*__ai_oldp)); \\ |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 109 | arch_${xchg}${order}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\ |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 110 | }) |
| 111 | EOF |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 112 | |
| 113 | else |
| 114 | |
| 115 | cat <<EOF |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 116 | #define ${xchg}${order}(ptr, ...) \\ |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 117 | ({ \\ |
| 118 | typeof(ptr) __ai_ptr = (ptr); \\ |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 119 | EOF |
| 120 | [ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n" |
| 121 | cat <<EOF |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 122 | instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\ |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 123 | arch_${xchg}${order}(__ai_ptr, __VA_ARGS__); \\ |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 124 | }) |
| 125 | EOF |
| 126 | |
| 127 | fi |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 130 | cat << EOF |
| 131 | // SPDX-License-Identifier: GPL-2.0 |
| 132 | |
| 133 | // Generated by $0 |
| 134 | // DO NOT MODIFY THIS FILE DIRECTLY |
| 135 | |
| 136 | /* |
| 137 | * This file provides wrappers with KASAN instrumentation for atomic operations. |
| 138 | * To use this functionality an arch's atomic.h file needs to define all |
| 139 | * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include |
| 140 | * this file at the end. This file provides atomic_read() that forwards to |
| 141 | * arch_atomic_read() for actual atomic operation. |
| 142 | * Note: if an arch atomic operation is implemented by means of other atomic |
| 143 | * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use |
| 144 | * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid |
| 145 | * double instrumentation. |
| 146 | */ |
Mark Rutland | e3d18ce | 2021-07-13 11:52:51 +0100 | [diff] [blame] | 147 | #ifndef _LINUX_ATOMIC_INSTRUMENTED_H |
| 148 | #define _LINUX_ATOMIC_INSTRUMENTED_H |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 149 | |
| 150 | #include <linux/build_bug.h> |
Marco Elver | c020395 | 2019-11-26 15:04:04 +0100 | [diff] [blame] | 151 | #include <linux/compiler.h> |
Marco Elver | ed8af2e | 2020-01-21 17:05:09 +0100 | [diff] [blame] | 152 | #include <linux/instrumented.h> |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 153 | |
| 154 | EOF |
| 155 | |
| 156 | grep '^[a-z]' "$1" | while read name meta args; do |
| 157 | gen_proto "${meta}" "${name}" "atomic" "int" ${args} |
| 158 | done |
| 159 | |
| 160 | grep '^[a-z]' "$1" | while read name meta args; do |
| 161 | gen_proto "${meta}" "${name}" "atomic64" "s64" ${args} |
| 162 | done |
| 163 | |
Mark Rutland | 67d1b0d | 2021-07-13 11:52:52 +0100 | [diff] [blame] | 164 | grep '^[a-z]' "$1" | while read name meta args; do |
| 165 | gen_proto "${meta}" "${name}" "atomic_long" "long" ${args} |
| 166 | done |
| 167 | |
| 168 | |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 169 | for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg"; do |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 170 | for order in "" "_acquire" "_release" "_relaxed"; do |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 171 | gen_xchg "${xchg}" "${order}" "" |
Mark Rutland | bccf1ec | 2021-05-25 15:02:32 +0100 | [diff] [blame] | 172 | printf "\n" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 173 | done |
| 174 | done |
| 175 | |
| 176 | for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 177 | gen_xchg "${xchg}" "" "" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 178 | printf "\n" |
| 179 | done |
| 180 | |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 181 | gen_xchg "cmpxchg_double" "" "2 * " |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 182 | |
| 183 | printf "\n\n" |
| 184 | |
Marco Elver | e87c4f6 | 2021-11-30 12:44:24 +0100 | [diff] [blame] | 185 | gen_xchg "cmpxchg_double_local" "" "2 * " |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 186 | |
| 187 | cat <<EOF |
| 188 | |
Mark Rutland | e3d18ce | 2021-07-13 11:52:51 +0100 | [diff] [blame] | 189 | #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */ |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 190 | EOF |