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
38 while [ "$#" -gt 0 ]; do
39 gen_param_check "$meta" "$1"
44 #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
45 gen_proto_order_variant()
47 local meta="$1"; shift
49 local name="$1"; shift
51 local order="$1"; shift
52 local atomic="$1"; shift
55 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
57 local ret="$(gen_ret_type "${meta}" "${int}")"
58 local params="$(gen_params "${int}" "${atomic}" "$@")"
59 local checks="$(gen_params_checks "${meta}" "$@")"
60 local args="$(gen_args "$@")"
61 local retstmt="$(gen_ret_stmt "${meta}")"
64 static __always_inline ${ret}
65 ${atomicname}(${params})
68 ${retstmt}arch_${atomicname}(${args});
77 local xchg="$1"; shift
78 local mult="$1"; shift
80 if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
83 #define ${xchg}(ptr, oldp, ...) \\
85 typeof(ptr) __ai_ptr = (ptr); \\
86 typeof(oldp) __ai_oldp = (oldp); \\
87 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
88 instrument_atomic_write(__ai_oldp, ${mult}sizeof(*__ai_oldp)); \\
89 arch_${xchg}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
96 #define ${xchg}(ptr, ...) \\
98 typeof(ptr) __ai_ptr = (ptr); \\
99 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
100 arch_${xchg}(__ai_ptr, __VA_ARGS__); \\
108 // SPDX-License-Identifier: GPL-2.0
111 // DO NOT MODIFY THIS FILE DIRECTLY
114 * This file provides wrappers with KASAN instrumentation for atomic operations.
115 * To use this functionality an arch's atomic.h file needs to define all
116 * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include
117 * this file at the end. This file provides atomic_read() that forwards to
118 * arch_atomic_read() for actual atomic operation.
119 * Note: if an arch atomic operation is implemented by means of other atomic
120 * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use
121 * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid
122 * double instrumentation.
124 #ifndef _LINUX_ATOMIC_INSTRUMENTED_H
125 #define _LINUX_ATOMIC_INSTRUMENTED_H
127 #include <linux/build_bug.h>
128 #include <linux/compiler.h>
129 #include <linux/instrumented.h>
133 grep '^[a-z]' "$1" | while read name meta args; do
134 gen_proto "${meta}" "${name}" "atomic" "int" ${args}
137 grep '^[a-z]' "$1" | while read name meta args; do
138 gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
141 grep '^[a-z]' "$1" | while read name meta args; do
142 gen_proto "${meta}" "${name}" "atomic_long" "long" ${args}
146 for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg"; do
147 for order in "" "_acquire" "_release" "_relaxed"; do
148 gen_xchg "${xchg}${order}" ""
153 for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
154 gen_xchg "${xchg}" ""
158 gen_xchg "cmpxchg_double" "2 * "
162 gen_xchg "cmpxchg_double_local" "2 * "
166 #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */