Path: blob/master/scripts/atomic/gen-atomic-instrumented.sh
49472 views
#!/bin/sh1# SPDX-License-Identifier: GPL-2.023ATOMICDIR=$(dirname $0)45. ${ATOMICDIR}/atomic-tbl.sh67#gen_param_check(meta, arg)8gen_param_check()9{10local meta="$1"; shift11local arg="$1"; shift12local type="${arg%%:*}"13local name="$(gen_param_name "${arg}")"14local rw="atomic_write"1516case "${type#c}" in17i) return;;18esac1920if [ ${type#c} != ${type} ]; then21# We don't write to constant parameters.22rw="atomic_read"23elif [ "${type}" = "p" ] ; then24# The "old" argument in try_cmpxchg() gets accessed non-atomically25rw="read_write"26elif [ "${meta}" != "s" ]; then27# An atomic RMW: if this parameter is not a constant, and this atomic is28# not just a 's'tore, this parameter is both read from and written to.29rw="atomic_read_write"30fi3132printf "\tinstrument_${rw}(${name}, sizeof(*${name}));\n"33}3435#gen_params_checks(meta, arg...)36gen_params_checks()37{38local meta="$1"; shift39local order="$1"; shift4041if [ "${order}" = "_release" ]; then42printf "\tkcsan_release();\n"43elif [ -z "${order}" ] && ! meta_in "$meta" "slv"; then44# RMW with return value is fully ordered45printf "\tkcsan_mb();\n"46fi4748while [ "$#" -gt 0 ]; do49gen_param_check "$meta" "$1"50shift;51done52}5354#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)55gen_proto_order_variant()56{57local meta="$1"; shift58local pfx="$1"; shift59local name="$1"; shift60local sfx="$1"; shift61local order="$1"; shift62local atomic="$1"; shift63local int="$1"; shift6465local atomicname="${atomic}_${pfx}${name}${sfx}${order}"6667local ret="$(gen_ret_type "${meta}" "${int}")"68local params="$(gen_params "${int}" "${atomic}" "$@")"69local checks="$(gen_params_checks "${meta}" "${order}" "$@")"70local args="$(gen_args "$@")"71local retstmt="$(gen_ret_stmt "${meta}")"7273gen_kerneldoc "" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "${atomic}" "${int}" "$@"7475cat <<EOF76static __always_inline ${ret}77${atomicname}(${params})78{79${checks}80${retstmt}raw_${atomicname}(${args});81}82EOF8384printf "\n"85}8687gen_xchg()88{89local xchg="$1"; shift90local order="$1"; shift9192kcsan_barrier=""93if [ "${xchg%_local}" = "${xchg}" ]; then94case "$order" in95_release) kcsan_barrier="kcsan_release()" ;;96"") kcsan_barrier="kcsan_mb()" ;;97esac98fi99100if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then101102cat <<EOF103#define ${xchg}${order}(ptr, oldp, ...) \\104({ \\105typeof(ptr) __ai_ptr = (ptr); \\106typeof(oldp) __ai_oldp = (oldp); \\107EOF108[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"109cat <<EOF110instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\111instrument_read_write(__ai_oldp, sizeof(*__ai_oldp)); \\112raw_${xchg}${order}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\113})114EOF115116else117118cat <<EOF119#define ${xchg}${order}(ptr, ...) \\120({ \\121typeof(ptr) __ai_ptr = (ptr); \\122EOF123[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"124cat <<EOF125instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\126raw_${xchg}${order}(__ai_ptr, __VA_ARGS__); \\127})128EOF129130fi131}132133cat << EOF134// SPDX-License-Identifier: GPL-2.0135136// Generated by $0137// DO NOT MODIFY THIS FILE DIRECTLY138139/*140* This file provoides atomic operations with explicit instrumentation (e.g.141* KASAN, KCSAN), which should be used unless it is necessary to avoid142* instrumentation. Where it is necessary to aovid instrumenation, the143* raw_atomic*() operations should be used.144*/145#ifndef _LINUX_ATOMIC_INSTRUMENTED_H146#define _LINUX_ATOMIC_INSTRUMENTED_H147148#include <linux/build_bug.h>149#include <linux/compiler.h>150#include <linux/instrumented.h>151152EOF153154grep '^[a-z]' "$1" | while read name meta args; do155gen_proto "${meta}" "${name}" "atomic" "int" ${args}156done157158grep '^[a-z]' "$1" | while read name meta args; do159gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}160done161162grep '^[a-z]' "$1" | while read name meta args; do163gen_proto "${meta}" "${name}" "atomic_long" "long" ${args}164done165166167for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128" "try_cmpxchg" "try_cmpxchg64" "try_cmpxchg128"; do168for order in "" "_acquire" "_release" "_relaxed"; do169gen_xchg "${xchg}" "${order}"170printf "\n"171done172done173174for xchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local" "sync_cmpxchg" \175"try_cmpxchg_local" "try_cmpxchg64_local" "try_cmpxchg128_local" "sync_try_cmpxchg"; do176gen_xchg "${xchg}" ""177printf "\n"178done179180cat <<EOF181182#endif /* _LINUX_ATOMIC_INSTRUMENTED_H */183EOF184185186