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