2 # SPDX-License-Identifier: GPL-2.0
4 ATOMICDIR=$(dirname $0)
6 . ${ATOMICDIR}/atomic-tbl.sh
12 local type="${arg%%:*}"
13 local name="$(gen_param_name "${arg}")"
20 # We don't write to constant parameters
21 [ ${type#c} != ${type} ] && rw="read"
23 printf "\tkasan_check_${rw}(${name}, sizeof(*${name}));\n"
26 #gen_param_check(arg...)
29 while [ "$#" -gt 0 ]; do
35 # gen_guard(meta, atomic, pfx, name, sfx, order)
38 local meta="$1"; shift
39 local atomic="$1"; shift
41 local name="$1"; shift
43 local order="$1"; shift
45 local atomicname="arch_${atomic}_${pfx}${name}${sfx}${order}"
47 local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
49 # We definitely need a preprocessor symbol for this atomic if it is an
50 # ordering variant, or if there's a generic fallback.
51 if [ ! -z "${order}" ] || [ ! -z "${template}" ]; then
52 printf "defined(${atomicname})"
56 # If this is a base variant, but a relaxed variant *may* exist, then we
57 # only have a preprocessor symbol if the relaxed variant isn't defined
58 if meta_has_relaxed "${meta}"; then
59 printf "!defined(${atomicname}_relaxed) || defined(${atomicname})"
63 #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
64 gen_proto_order_variant()
66 local meta="$1"; shift
68 local name="$1"; shift
70 local order="$1"; shift
71 local atomic="$1"; shift
74 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
76 local guard="$(gen_guard "${meta}" "${atomic}" "${pfx}" "${name}" "${sfx}" "${order}")"
78 local ret="$(gen_ret_type "${meta}" "${int}")"
79 local params="$(gen_params "${int}" "${atomic}" "$@")"
80 local checks="$(gen_params_checks "$@")"
81 local args="$(gen_args "$@")"
82 local retstmt="$(gen_ret_stmt "${meta}")"
84 [ ! -z "${guard}" ] && printf "#if ${guard}\n"
88 ${atomicname}(${params})
91 ${retstmt}arch_${atomicname}(${args});
93 #define ${atomicname} ${atomicname}
96 [ ! -z "${guard}" ] && printf "#endif\n"
103 local xchg="$1"; shift
104 local mult="$1"; shift
107 #define ${xchg}(ptr, ...) \\
109 typeof(ptr) __ai_ptr = (ptr); \\
110 kasan_check_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
111 arch_${xchg}(__ai_ptr, __VA_ARGS__); \\
118 local name="$1"; shift
119 local sfx="$1"; shift
120 local guard="defined(arch_${name}${sfx})"
122 [ -z "${sfx}" ] && guard="!defined(arch_${name}_relaxed) || defined(arch_${name})"
124 printf "#if ${guard}\n"
125 gen_xchg "${name}${sfx}" ""
130 // SPDX-License-Identifier: GPL-2.0
133 // DO NOT MODIFY THIS FILE DIRECTLY
136 * This file provides wrappers with KASAN instrumentation for atomic operations.
137 * To use this functionality an arch's atomic.h file needs to define all
138 * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include
139 * this file at the end. This file provides atomic_read() that forwards to
140 * arch_atomic_read() for actual atomic operation.
141 * Note: if an arch atomic operation is implemented by means of other atomic
142 * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use
143 * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid
144 * double instrumentation.
146 #ifndef _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
147 #define _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
149 #include <linux/build_bug.h>
150 #include <linux/kasan-checks.h>
154 grep '^[a-z]' "$1" | while read name meta args; do
155 gen_proto "${meta}" "${name}" "atomic" "int" ${args}
158 grep '^[a-z]' "$1" | while read name meta args; do
159 gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
162 for xchg in "xchg" "cmpxchg" "cmpxchg64"; do
163 for order in "" "_acquire" "_release" "_relaxed"; do
164 gen_optional_xchg "${xchg}" "${order}"
168 for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
169 gen_xchg "${xchg}" ""
173 gen_xchg "cmpxchg_double" "2 * "
177 gen_xchg "cmpxchg_double_local" "2 * "
181 #endif /* _ASM_GENERIC_ATOMIC_INSTRUMENTED_H */