gen-atomic-instrumented.sh 4.3 KB

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