Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/scripts/atomic/gen-atomic-instrumented.sh
49472 views
1
#!/bin/sh
2
# SPDX-License-Identifier: GPL-2.0
3
4
ATOMICDIR=$(dirname $0)
5
6
. ${ATOMICDIR}/atomic-tbl.sh
7
8
#gen_param_check(meta, arg)
9
gen_param_check()
10
{
11
local meta="$1"; shift
12
local arg="$1"; shift
13
local type="${arg%%:*}"
14
local name="$(gen_param_name "${arg}")"
15
local rw="atomic_write"
16
17
case "${type#c}" in
18
i) return;;
19
esac
20
21
if [ ${type#c} != ${type} ]; then
22
# We don't write to constant parameters.
23
rw="atomic_read"
24
elif [ "${type}" = "p" ] ; then
25
# The "old" argument in try_cmpxchg() gets accessed non-atomically
26
rw="read_write"
27
elif [ "${meta}" != "s" ]; then
28
# An atomic RMW: if this parameter is not a constant, and this atomic is
29
# not just a 's'tore, this parameter is both read from and written to.
30
rw="atomic_read_write"
31
fi
32
33
printf "\tinstrument_${rw}(${name}, sizeof(*${name}));\n"
34
}
35
36
#gen_params_checks(meta, arg...)
37
gen_params_checks()
38
{
39
local meta="$1"; shift
40
local order="$1"; shift
41
42
if [ "${order}" = "_release" ]; then
43
printf "\tkcsan_release();\n"
44
elif [ -z "${order}" ] && ! meta_in "$meta" "slv"; then
45
# RMW with return value is fully ordered
46
printf "\tkcsan_mb();\n"
47
fi
48
49
while [ "$#" -gt 0 ]; do
50
gen_param_check "$meta" "$1"
51
shift;
52
done
53
}
54
55
#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
56
gen_proto_order_variant()
57
{
58
local meta="$1"; shift
59
local pfx="$1"; shift
60
local name="$1"; shift
61
local sfx="$1"; shift
62
local order="$1"; shift
63
local atomic="$1"; shift
64
local int="$1"; shift
65
66
local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
67
68
local ret="$(gen_ret_type "${meta}" "${int}")"
69
local params="$(gen_params "${int}" "${atomic}" "$@")"
70
local checks="$(gen_params_checks "${meta}" "${order}" "$@")"
71
local args="$(gen_args "$@")"
72
local retstmt="$(gen_ret_stmt "${meta}")"
73
74
gen_kerneldoc "" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "${atomic}" "${int}" "$@"
75
76
cat <<EOF
77
static __always_inline ${ret}
78
${atomicname}(${params})
79
{
80
${checks}
81
${retstmt}raw_${atomicname}(${args});
82
}
83
EOF
84
85
printf "\n"
86
}
87
88
gen_xchg()
89
{
90
local xchg="$1"; shift
91
local order="$1"; shift
92
93
kcsan_barrier=""
94
if [ "${xchg%_local}" = "${xchg}" ]; then
95
case "$order" in
96
_release) kcsan_barrier="kcsan_release()" ;;
97
"") kcsan_barrier="kcsan_mb()" ;;
98
esac
99
fi
100
101
if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
102
103
cat <<EOF
104
#define ${xchg}${order}(ptr, oldp, ...) \\
105
({ \\
106
typeof(ptr) __ai_ptr = (ptr); \\
107
typeof(oldp) __ai_oldp = (oldp); \\
108
EOF
109
[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
110
cat <<EOF
111
instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\
112
instrument_read_write(__ai_oldp, sizeof(*__ai_oldp)); \\
113
raw_${xchg}${order}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
114
})
115
EOF
116
117
else
118
119
cat <<EOF
120
#define ${xchg}${order}(ptr, ...) \\
121
({ \\
122
typeof(ptr) __ai_ptr = (ptr); \\
123
EOF
124
[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
125
cat <<EOF
126
instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\
127
raw_${xchg}${order}(__ai_ptr, __VA_ARGS__); \\
128
})
129
EOF
130
131
fi
132
}
133
134
cat << EOF
135
// SPDX-License-Identifier: GPL-2.0
136
137
// Generated by $0
138
// DO NOT MODIFY THIS FILE DIRECTLY
139
140
/*
141
* This file provoides atomic operations with explicit instrumentation (e.g.
142
* KASAN, KCSAN), which should be used unless it is necessary to avoid
143
* instrumentation. Where it is necessary to aovid instrumenation, the
144
* raw_atomic*() operations should be used.
145
*/
146
#ifndef _LINUX_ATOMIC_INSTRUMENTED_H
147
#define _LINUX_ATOMIC_INSTRUMENTED_H
148
149
#include <linux/build_bug.h>
150
#include <linux/compiler.h>
151
#include <linux/instrumented.h>
152
153
EOF
154
155
grep '^[a-z]' "$1" | while read name meta args; do
156
gen_proto "${meta}" "${name}" "atomic" "int" ${args}
157
done
158
159
grep '^[a-z]' "$1" | while read name meta args; do
160
gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
161
done
162
163
grep '^[a-z]' "$1" | while read name meta args; do
164
gen_proto "${meta}" "${name}" "atomic_long" "long" ${args}
165
done
166
167
168
for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128" "try_cmpxchg" "try_cmpxchg64" "try_cmpxchg128"; do
169
for order in "" "_acquire" "_release" "_relaxed"; do
170
gen_xchg "${xchg}" "${order}"
171
printf "\n"
172
done
173
done
174
175
for xchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local" "sync_cmpxchg" \
176
"try_cmpxchg_local" "try_cmpxchg64_local" "try_cmpxchg128_local" "sync_try_cmpxchg"; do
177
gen_xchg "${xchg}" ""
178
printf "\n"
179
done
180
181
cat <<EOF
182
183
#endif /* _LINUX_ATOMIC_INSTRUMENTED_H */
184
EOF
185
186