2 # SPDX-License-Identifier: GPL-2.0
4 ATOMICDIR=$(dirname $0)
6 . ${ATOMICDIR}/atomic-tbl.sh
8 #gen_param_check(meta, arg)
11 local meta="$1"; shift
13 local type="${arg%%:*}"
14 local name="$(gen_param_name "${arg}")"
21 if [ ${type#c} != ${type} ]; then
22 # We don't write to constant parameters.
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.
30 printf "\tinstrument_atomic_${rw}(${name}, sizeof(*${name}));\n"
33 #gen_params_checks(meta, arg...)
36 local meta="$1"; shift
37 local order="$1"; shift
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"
46 while [ "$#" -gt 0 ]; do
47 gen_param_check "$meta" "$1"
52 #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
53 gen_proto_order_variant()
55 local meta="$1"; shift
57 local name="$1"; shift
59 local order="$1"; shift
60 local atomic="$1"; shift
63 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
65 local ret="$(gen_ret_type "${meta}" "${int}")"
66 local params="$(gen_params "${int}" "${atomic}" "$@")"
67 local checks="$(gen_params_checks "${meta}" "${order}" "$@")"
68 local args="$(gen_args "$@")"
69 local retstmt="$(gen_ret_stmt "${meta}")"
72 static __always_inline ${ret}
73 ${atomicname}(${params})
76 ${retstmt}arch_${atomicname}(${args});
85 local xchg="$1"; shift
86 local order="$1"; shift
87 local mult="$1"; shift
90 if [ "${xchg%_local}" = "${xchg}" ]; then
92 _release) kcsan_barrier="kcsan_release()" ;;
93 "") kcsan_barrier="kcsan_mb()" ;;
97 if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
100 #define ${xchg}${order}(ptr, oldp, ...) \\
102 typeof(ptr) __ai_ptr = (ptr); \\
103 typeof(oldp) __ai_oldp = (oldp); \\
105 [ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
107 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
108 instrument_atomic_write(__ai_oldp, ${mult}sizeof(*__ai_oldp)); \\
109 arch_${xchg}${order}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
116 #define ${xchg}${order}(ptr, ...) \\
118 typeof(ptr) __ai_ptr = (ptr); \\
120 [ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
122 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
123 arch_${xchg}${order}(__ai_ptr, __VA_ARGS__); \\
131 // SPDX-License-Identifier: GPL-2.0
134 // DO NOT MODIFY THIS FILE DIRECTLY
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.
147 #ifndef _LINUX_ATOMIC_INSTRUMENTED_H
148 #define _LINUX_ATOMIC_INSTRUMENTED_H
150 #include <linux/build_bug.h>
151 #include <linux/compiler.h>
152 #include <linux/instrumented.h>
156 grep '^[a-z]' "$1" | while read name meta args; do
157 gen_proto "${meta}" "${name}" "atomic" "int" ${args}
160 grep '^[a-z]' "$1" | while read name meta args; do
161 gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
164 grep '^[a-z]' "$1" | while read name meta args; do
165 gen_proto "${meta}" "${name}" "atomic_long" "long" ${args}
169 for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg" "try_cmpxchg64"; do
170 for order in "" "_acquire" "_release" "_relaxed"; do
171 gen_xchg "${xchg}" "${order}" ""
176 for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
177 gen_xchg "${xchg}" "" ""
181 gen_xchg "cmpxchg_double" "" "2 * "
185 gen_xchg "cmpxchg_double_local" "" "2 * "
189 #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */