tools/perf: Adopt kernel compiler-context-analysis.h annotations
Subsystems (2)
Ian Rogers
reviewerv12 cover tools/perf: Adopt kernel compiler-context-analysis.h annotations
The perf tool previously added Clang thread-safety annotations in tools/perf/util/mutex.h (GUARDED_BY, LOCKABLE, EXCLUSIVE_LOCK_FUNCTION, UNLOCK_FUNCTION, etc.). Since then, the Linux kernel has adopted a unified set of static context analysis macros and attributes in include/linux/compiler-context-analysis.h (context_lock_struct, __guarded_by, __acquires, __releases, __must_hold, etc.).
This series makes tools/include/linux/compiler-context-analysis.h functional by synchronizing it and the required compiler_types.h helpers with the kernel headers, converts tools/perf from its custom mutex.h macros to the kernel-style compiler-context-analysis.h annotations, and expands context analysis coverage across comm, dso/dsos, thread/threads, config, intel-tpebs, env, and maps. Along the way, separate preparatory patches fix an unlocked __dsos__add() call and TOCTOU race in __machine__addnew_vdso(), skip BPF and OOL DSOs in dsos__find_kernel_dso(), ensure DSOs created in auxtrace, machine, and symbol helpers belong to a dsos collection, avoid dropping _dso__data_open_lock in dso__list_del() under REFCNT_CHECKING, switch dso__data_open_lock and tpebs_mtx to DEFINE_MUTEX(), add a read-lock fast path to dsos__findnew_id(), fix an unlocked read of tpebs_reader_thread and double-close of tpebs_cmd.out in intel-tpebs, avoid a potential read-to-write deadlock in maps__load_first(), and fix map index lookup fallback and removal in maps.
This series was developed and reviewed with assistance from an AI coding agent (Gemini 3.1 Pro) and reviewed with Sashiko.
v12:
- Rebase onto perf-tools-next.
- Newline-terminate control commands in tpebs_send_record_cmd() so consecutive writes cannot coalesce in evlist__ctlfd_recv(), send SIGTERM in tpebs_stop() if ENABLE had not completed (tpebs_enabled, annotated __guarded_by(&tpebs_mtx) in patch 17) or sending STOP fails, and ignore late samples in process_sample_event() once tpebs_stopping is set.
v11:
- Rebase onto perf-tools-next.
- Add Fixes tags to patches 7, 15, and 18.
- Serialize tpebs_send_record_cmd() with teardown via tpebs_cmd_in_flight and tpebs_cmd_cond, check check_if_command_finished(&tpebs_cmd) under tpebs_mtx only when not stopping, guard evsel__tpebs_open() against a concurrent tpebs_stop() with a generation counter (tpebs_gen) and tpebs_stopping across closing the fds and finish_command(), and annotate tpebs_cmd, control_fd, ack_fd, tpebs_stopping, tpebs_cmd_in_flight, and tpebs_gen with __guarded_by(&tpebs_mtx) in patch 17.
v10:
- Split the maps__load_first() lock inversion fix (patch 18) from the maps index lookup and removal fixes (patch 19).
- Fall back to the linear pointer scan in maps__by_address_index() and maps__by_name_index() when bsearch() returns NULL, check map__start(prev) < map__start(new) in __maps__insert(), factor out __maps__remove_at() so maps__remove_maps() removes at index i directly without risking an infinite loop, guard against -1 index wraparound in maps__find_next_entry(), refresh sort invariants in maps__fixup_end() and machine__create_kernel_maps(), and add a unit test in tests/maps.c.
v9:
- Rebase onto perf-tools-next (resolving the conflict in tools/include/linux/compiler.h with ASSERT_STATIC_STORAGE() and converting the newly added dso__set_symsrc_filename() lock annotation in tools/perf/util/dso.h).
- Move __must_hold_shared(&dsos->lock) on __dsos__find() from patch 13 into patch 7 where __dsos__find() is introduced so every commit in the series builds cleanly with -Wthread-safety.
v8:
- Set DSO_BINARY_TYPE__BPF_IMAGE for non-OOL ksymbol DSOs in machine__process_ksymbol_register() and skip DSO_BINARY_TYPE__BPF_PROG_INFO, DSO_BINARY_TYPE__BPF_IMAGE, and DSO_BINARY_TYPE__OOL in dsos__find_kernel_dso_cb() so that dsos__find_kernel_dso() does not match ksymbol DSOs in machine->dsos (patch 8).
- Split the dsos__add() additions in auxtrace, machine, and symbol into a standalone patch (patch 9) before "perf dso: Pass dso_data to close_data_fd() and dso__list_del()" (patch 10).
v7:
- Add newly created DSOs in machine__process_ksymbol_register() and dso__split_kallsyms() to machine->dsos so assert(dso__dsos(dso)) in dso__list_add() always holds.
- Add __guarded_by(&config_mutex) to function-local static parser buffers in parse_value() and perf_parse_file().
- Remove from maps_by_name before calling map__put(maps_by_address[address_idx]) in __maps__remove() so callers such as maps__remove_maps() that pass maps_by_address[i] as map do not use a freed handle in maps__by_name_index() under REFCNT_CHECKING.
v6:
- Rebase onto perf-tools-next.
v5:
- Hold machine->dsos.lock in write mode across machine->vdso_info initialization and temporary file creation in machine__findnew_vdso() and __machine__findnew_compat(), and annotate __machine__addnew_vdso() with __must_hold(&machine->dsos.lock).
- Reset tpebs_cmd.pid = 0 and return early without setting
t->started = true if start_command() fails in
evsel__tpebs_start_perf_record().v4:
- Place the DSO loaded in addr_filter__resolve_syms() into a dsos collection so dso__dsos(dso) is populated.
- Join tpebs_reader_thread on error in evsel__tpebs_open(), document why tpebs_cmd.pid is cleared before pthread_join() in tpebs_stop(), and include <pthread.h> and <signal.h> in util/intel-tpebs.c.
- Include <string.h> in util/maps.c.
v3:
- Rebase onto perf-tools-next (converting the newly added dso__find_binary_type() lock annotations in util/dso.c).
- Separate bug fixes, DEFINE_MUTEX() cleanups, and improvements (vdso, dso_data, dso__data_open_lock, dsos__findnew_id, intel-tpebs, tpebs_mtx, maps) from the annotation patches.
- Move AI coding assistant attribution to the cover letter and drop per-commit Assisted-by tags per linux-next Documentation/process/coding-assistants.rst.
- Add Marco Elver's Reviewed-by to patch 1.
- Define __diag_clang_23() and Clang ASM_INPUT_G/ASM_INPUT_RM/ ASM_OUTPUT_RM overrides in tools/include/linux/compiler-gcc.h.
- Probe for -Wthread-safety-pointer/-Wthread-safety-beta in Makefile.config.
- Pass write_locked to __dsos__find().
- Pass struct dso_data * to close_data_fd() and dso__list_del() to remove the REFCNT_CHECKING dso pointer from struct dso_data and avoid dropping dso__data_open_lock in dso__list_del(), and include <assert.h> in util/dso.c.
- Re-validate DSO lookup under the write lock in dsos__findnew_id() via __dsos__findnew_id(), and handle NULL dso_id in dso__needs_improve_id(), __dso__improve_id(), and dso_id__cmp().
- Reset tpebs_cmd.out = -1 if start_command() fails and include
<stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> in
util/intel-tpebs.c. - Bounds-check the index in __maps__remove() (freeing maps_by_name if out of bounds).
v2:
- Use -c rather than -E when probing for -fexperimental-late-parse-attributes in Makefile.config to avoid -Werror,-Wunused-command-line-argument.
- Acquire machine->dsos.lock in write mode across duplicate checking,
dso__set_long_name(), and __dsos__add() in __machine__addnew_vdso().
- Avoid dropping dso__data_open_lock in dso__list_del() under REFCNT_CHECKING.
- Pass tpebs_cmd.out directly to __sample_reader() and set tpebs_cmd.out = -1 in evsel__tpebs_open() to avoid a double-close in tpebs_stop(), and clarify the config_set_mutex comment.
- Drop maps__lock before calling map__load() in maps__load_first() to avoid a read-to-write self-deadlock when loading kernel maps inserts additional trampoline maps.
Ian Rogers (20):
compiler-context-analysis: Avoid unused parameter warning in
_context_unsafe_alias()
tools headers compiler: Synchronize compiler_types.h with the kernel
sources
tools headers: Synchronize linux/compiler-context-analysis.h with the
kernel sources
perf mutex: Use kernel style compiler-context-analysis.h annotations perf build: Enable additional Clang thread-safety flags perf comm: Add __guarded_by annotations to struct comm_strs perf vdso: Fix unlocked __dsos__add() and race in
__machine__addnew_vdso()
perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso() perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos perf dso: Pass dso_data to close_data_fd() and dso__list_del() perf dso: Use DEFINE_MUTEX for dso__data_open_lock
perf dsos: Use read lock fast path in dsos__findnew_id() perf dso: Expand context analysis annotations
perf thread: Expand context analysis annotations to threads and
namespaces_lock
perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd
handling
perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx
perf config, intel-tpebs, env: Add __guarded_by and __must_hold
annotations
perf maps: Fix lock inversion in maps__load_first()
perf maps: Fix map index lookup fallback and removal perf maps: Add context analysis annotations
include/linux/compiler-context-analysis.h | 2 +- .../include/linux/compiler-context-analysis.h | 518 ++++++++++++++++-- tools/include/linux/compiler-gcc.h | 72 ++- tools/include/linux/compiler.h | 50 +- tools/include/linux/compiler_types.h | 206 ++++++- tools/perf/Makefile.config | 8 +- tools/perf/builtin-sched.c | 16 +- tools/perf/builtin-top.c | 4 +- tools/perf/check-headers.sh | 1 + tools/perf/tests/dso-data.c | 4 + tools/perf/tests/maps.c | 140 +++++ tools/perf/util/annotate.c | 4 +- tools/perf/util/annotate.h | 8 +- tools/perf/util/auxtrace.c | 16 +- tools/perf/util/bpf-event.h | 5 +- tools/perf/util/comm.c | 10 +- tools/perf/util/config.c | 47 +- tools/perf/util/dso.c | 140 +++-- tools/perf/util/dso.h | 17 +- tools/perf/util/dsos.c | 34 +- tools/perf/util/dsos.h | 12 +- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 +- tools/perf/util/intel-tpebs.c | 226 +++++--- tools/perf/util/libbfd.c | 4 +- tools/perf/util/machine.c | 34 +- tools/perf/util/maps.c | 216 +++++--- tools/perf/util/mutex.c | 4 +- tools/perf/util/mutex.h | 90 +-- tools/perf/util/rwsem.c | 8 +- tools/perf/util/rwsem.h | 10 +- tools/perf/util/symbol.c | 4 + tools/perf/util/thread.c | 10 +- tools/perf/util/thread.h | 30 +- tools/perf/util/threads.c | 4 + tools/perf/util/threads.h | 6 +- tools/perf/util/vdso.c | 53 +- 37 files changed, 1492 insertions(+), 541 deletions(-)
base-commit: cc905a18a647a8e3ad42cd1596baa0e08fba26eb
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerPATCH v12 1/20 compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias()
_context_unsafe_alias() does not use its parameter 'p', which triggers -Wunused-parameter when building code that enables -Wextra (such as tools/perf, tools/lib/perf, tools/lib/subcmd, and tools/lib/symbol). Explicitly cast 'p' to void to silence the warning, matching __assume_ctx_lock() and __assume_shared_ctx_lock().
--- include/linux/compiler-context-analysis.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler-context-analysis.h +1 -1
@@ -243,7 +243,7 @@* alias as a function argument). */ #define context_unsafe_alias (p) _context_unsafe_alias((void **)&(p)) -static inline void _context_unsafe_alias (void **p) { } +static inline void _context_unsafe_alias (void **p) { (void )p; }/** * token_context_lock() - declare an abstract global context lock instance
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 01/20] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias()
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines
_context_unsafe_alias()does not use its parameter 'p', which triggers -Wunused-parameter when building code that enables -Wextra (such astools/perf,tools/lib/perf,tools/lib/subcmd, andtools/lib/symbol). Explicitly cast 'p' to void to silence the warning, matching__assume_ctx_lock()and__assume_shared_ctx_lock().Reviewed-by Marco Elver <[email protected]>Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v12 2/20 tools headers compiler: Synchronize compiler_types.h with the kernel sources
In reply to Ian RogersSynchronize the non-__KERNEL__ sections of tools/include/linux/compiler_types.h and tools/include/linux/compiler-gcc.h with the kernel sources (including ___PASTE/__PASTE, BTF_TYPE_TAG, object-size helpers, asm_goto_output, ASM_INPUT_*, asm_inline, __typeof_unqual__/__unqual_scalar_typeof, __signed_scalar_typeof, __native_word, compiletime_assert, and the __diag pragma helpers required by compiler-context-analysis.h), and remove the duplicated definitions from tools/include/linux/compiler.h.
--- tools/include/linux/compiler-gcc.h | 72 +++++++++- tools/include/linux/compiler.h | 50 +------ tools/include/linux/compiler_types.h | 206 ++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 53 deletions(-)
tools/include/linux/compiler-gcc.h +69 -3
@@ -1,7 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_TYPES_H#error "Please do not include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." -#endif +#endif /* __LINUX_COMPILER_TYPES_H */ /* * Common definitions for all gcc versions go here.
@@ -33,10 +33,76 @@#define __packed __attribute__((packed))#endif #ifndef __noreturn -#define __noreturn __attribute__((noreturn)) +#define __noreturn __attribute__((__noreturn__))#endif #ifndef __aligned#define __aligned (x) __attribute__((aligned(x)))#endif #define __printf (a, b) __attribute__((format(printf, a, b)))#define __scanf (a, b) __attribute__((format(scanf, a, b))) + +/* +* Turn individual warnings and errors on and off locally, depending +* on version. +*/ +#define __diag_str1 (s) #s +#define __diag_str (s) __diag_str1(s) + +#ifdef __clang__ +#define __diag_clang (version, severity, s) \ + __diag_clang_ ## version(__diag_clang_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_clang_ignore ignored +#define __diag_clang_warn warning +#define __diag_clang_error error + +#define __diag (s) _Pragma(__diag_str(clang diagnostic s)) + +#if __clang_major__ >=23 +#define __diag_clang_23 (s) __diag(s) +#else +#define __diag_clang_23 (s) +#endif + +#define __diag_clang_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag_clang(all, ignore, option) + +/* +* clang has horrible behavior with "g" or "rm" constraints for asm +* inputs, turning them into something worse than "m". Avoid using +* constraints with multiple possible uses (but "ir" seems to be ok): +* +* https://github.com/llvm/llvm-project/issues/20571 +*/ +#define ASM_INPUT_G "ir" +#define ASM_INPUT_RM "r" +#define ASM_OUTPUT_RM "=r" + +#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ > 19 || (__clang_major__ == 19 && __clang_minor__ > 0)) +#else /* !__clang__ */ +#define __diag_GCC (version, severity, s) \ + __diag_GCC_ ## version(__diag_GCC_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_GCC_ignore ignored +#define __diag_GCC_warn warning +#define __diag_GCC_error error + +#define __diag (s) _Pragma(__diag_str(GCC diagnostic s)) + +#if GCC_VERSION >=80000 +#define __diag_GCC_8 (s) __diag(s) +#else +#define __diag_GCC_8 (s) +#endif + +#define __diag_GCC_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag(__diag_GCC_ignore option) + +#define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >= 14) +#endif /* __clang__ */
tools/include/linux/compiler.h +3 -47
80 lines not on this page · raw message
tools/include/linux/compiler_types.h +203 -3
236 lines not on this page · raw message
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 02/20] tools headers compiler: Synchronize compiler_types.h with the kernel sources
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Synchronize the non-__KERNEL__ sections of
tools/include/linux/compiler_types.handtools/include/linux/compiler-gcc.hwith the kernel sources (including ___PASTE/__PASTE,BTF_TYPE_TAG, object-size helpers,asm_goto_output, ASM_INPUT_*,asm_inline, __typeof_unqual__/__unqual_scalar_typeof,__signed_scalar_typeof,__native_word,compiletime_assert, and the__diagpragma helpers required bycompiler-context-analysis.h), and remove the duplicated definitions fromtools/include/linux/compiler.h.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Namhyung Kim
maintainerRe: [PATCH v12 02/20] tools headers compiler: Synchronize compiler_types.h with the kernel sources
In reply to Ian RogersQuoted 21 lines · Ian Rogers
Synchronize the non-__KERNEL__ sections of
tools/include/linux/compiler_types.handtools/include/linux/compiler-gcc.hwith the kernel sources (including ___PASTE/__PASTE,BTF_TYPE_TAG, object-size helpers,asm_goto_output, ASM_INPUT_*,asm_inline, __typeof_unqual__/__unqual_scalar_typeof,__signed_scalar_typeof,__native_word,compiletime_assert, and the__diagpragma helpers required bycompiler-context-analysis.h), and remove the duplicated definitions fromtools/include/linux/compiler.h.Signed-off-by Ian Rogers <[email protected]>--- tools/include/linux/compiler-gcc.h | 72 +++++++++- tools/include/linux/compiler.h | 50 +------ tools/include/linux/compiler_types.h | 206 ++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 53 deletions(-)tools/include/linux/compiler-gcc.h +0
-0
tools/include/linux/compiler-gcc.h line 2
Quoted 3 lines
tools/include/linux/compiler-gcc.h +1
-1@@ -1,7 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_TYPES_H
I'm not sure if it's better to keep the TOOLS part in the macro guard. It would make the sync-ing a bit harder. And I also see other files already don't have TOOLS so it seems ok to drop.
Thanks,
Namhyung
Quoted 420 lines
tools/include/linux/compiler-gcc.h +68
-2#error "Please do not include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." -#endif +#endif /* __LINUX_COMPILER_TYPES_H */ /* * Common definitions for all gcc versions go here. @@ -33,10 +33,76 @@#define __packed __attribute__((packed))#endif #ifndef __noreturn -#define __noreturn __attribute__((noreturn)) +#define __noreturn __attribute__((__noreturn__))#endif #ifndef __aligned#define __aligned (x) __attribute__((aligned(x)))#endif #define __printf (a, b) __attribute__((format(printf, a, b)))#define __scanf (a, b) __attribute__((format(scanf, a, b))) + +/* +* Turn individual warnings and errors on and off locally, depending +* on version. +*/ +#define __diag_str1 (s) #s +#define __diag_str (s) __diag_str1(s) + +#ifdef __clang__ +#define __diag_clang (version, severity, s) \ + __diag_clang_ ## version(__diag_clang_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_clang_ignore ignored +#define __diag_clang_warn warning +#define __diag_clang_error error + +#define __diag (s) _Pragma(__diag_str(clang diagnostic s)) + +#if __clang_major__ >=23 +#define __diag_clang_23 (s) __diag(s) +#else +#define __diag_clang_23 (s) +#endif + +#define __diag_clang_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag_clang(all, ignore, option) + +/* +* clang has horrible behavior with "g" or "rm" constraints for asm +* inputs, turning them into something worse than "m". Avoid using +* constraints with multiple possible uses (but "ir" seems to be ok): +* +* https://github.com/llvm/llvm-project/issues/20571 +*/ +#define ASM_INPUT_G "ir" +#define ASM_INPUT_RM "r" +#define ASM_OUTPUT_RM "=r" + +#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ > 19 || (__clang_major__ == 19 && __clang_minor__ > 0)) +#else /* !__clang__ */ +#define __diag_GCC (version, severity, s) \ + __diag_GCC_ ## version(__diag_GCC_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_GCC_ignore ignored +#define __diag_GCC_warn warning +#define __diag_GCC_error error + +#define __diag (s) _Pragma(__diag_str(GCC diagnostic s)) + +#if GCC_VERSION >=80000 +#define __diag_GCC_8 (s) __diag(s) +#else +#define __diag_GCC_8 (s) +#endif + +#define __diag_GCC_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag(__diag_GCC_ignore option) + +#define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >= 14) +#endif /* __clang__ */ tools/include/linux/compiler.h +3
-47@@ -1,41 +1,11 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ -#define _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_H +#define __LINUX_COMPILER_H#ifndef __ASSEMBLY__#include <linux/compiler_types.h> -#ifndef __compiletime_error -# define __compiletime_error (message) -#endif - -#ifdef __OPTIMIZE__ -# define __compiletime_assert (condition, msg, prefix, suffix) \ - do { \ - extern void prefix ## suffix(void) __compiletime_error(msg); \ - if (!(condition)) \ - prefix ## suffix(); \ - } while (0) -#else -# define __compiletime_assert (condition, msg, prefix, suffix) do { } while (0) -#endif - -#define _compiletime_assert (condition, msg, prefix, suffix) \ - __compiletime_assert(condition, msg, prefix, suffix) - -/** -* compiletime_assert - break build and emit msg if condition is false -* @condition: a compile-time constant condition to check -* @msg: a message to emit if condition is false -* -* In tradition of POSIX assert, this macro will break the build if the -* supplied condition is *false*, emitting the supplied error message if the -* compiler has support to do so. -*/ -#define compiletime_assert (condition, msg) \ - _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) -/* Optimization barrier */ /* The "volatile" is due to gcc bugs */ #define barrier () __asm__ __volatile__("": : :"memory")@@ -48,10 +18,6 @@#define __always_unused __attribute__((__unused__))#endif -#ifndef __noreturn -#define __noreturn __attribute__((__noreturn__)) -#endif -#ifndef unreachable#define unreachable () __builtin_unreachable()#endif @@ -68,11 +34,6 @@#define __naked __attribute__((__naked__))#endif -/* Are two types/vars the same type (ignoring qualifiers)? */ -#ifndef __same_type -# define __same_type (a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) -#endif -/* Enforce static storage duration. */ #define ASSERT_STATIC_STORAGE (name) \ static typeof(name) * const __always_unused \@@ -242,11 +203,6 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s __u.__val; \ }) - -/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ -#define ___PASTE (a, b) a##b -#define __PASTE (a, b) ___PASTE(a, b) -#ifndef OPTIMIZER_HIDE_VAR /* Make the optimizer believe the variable can be manipulated arbitrarily. */ #define OPTIMIZER_HIDE_VAR (var) \@@ -263,4 +219,4 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s#endif /* __ASSEMBLY__ */ -#endif /* _TOOLS_LINUX_COMPILER_H */ +#endif /* __LINUX_COMPILER_H */ tools/include/linux/compiler_types.h +203
-3@@ -2,8 +2,6 @@#ifndef __LINUX_COMPILER_TYPES_H#define __LINUX_COMPILER_TYPES_H -/* Builtins */ -/* * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21. * In the meantime, to support gcc < 10, we implement __has_builtin @@ -13,6 +11,23 @@#define __has_builtin (x) (0)#endif +/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ +#define ___PASTE (a, b) a##b +#define __PASTE (a, b) ___PASTE(a, b) + +#ifndef __ASSEMBLY__ + +/* +* Skipped when running bindgen due to a libclang issue; +* see https://github.com/rust-lang/rust-bindgen/issues/2244. +*/ +#if defined(CONFIG_DEBUG_INFO_BTF ) && defined(CONFIG_PAHOLE_HAS_BTF_TAG ) && \ +__has_attribute (btf_type_tag) && !defined(__BINDGEN__) +# define BTF_TYPE_TAG (value) __attribute__((btf_type_tag(#value))) +#else +# define BTF_TYPE_TAG (value)/* nothing */ +#endif +#include <linux/compiler-context-analysis.h> /* Compiler specific macros. */ @@ -20,14 +35,101 @@#include <linux/compiler-gcc.h> #endif +#ifndef __compiletime_error +# define __compiletime_error (message) +#endif + +#ifndef __noreturn +# define __noreturn __attribute__((__noreturn__)) +#endif + +#endif /* __ASSEMBLY__ */ + +/* +* When the size of an allocated object is needed, use the best available +* mechanism to find it. (For cases where sizeof() cannot be used.) +* +* Optional: only supported since gcc >= 12 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html +* clang: https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-object-size +*/ +#if __has_builtin (__builtin_dynamic_object_size) +#define __struct_size (p) __builtin_dynamic_object_size(p, 0) +#define __member_size (p) __builtin_dynamic_object_size(p, 1) +#else +#define __struct_size (p) __builtin_object_size(p, 0) +#define __member_size (p) __builtin_object_size(p, 1) +#endif + +/* +* Determine if an attribute has been applied to a variable. +* Using __annotated needs to check for __annotated being available, +* or negative tests may fail when annotation cannot be checked. For +* example, see the definition of __is_cstr(). +*/ +#if __has_builtin (__builtin_has_attribute) +#define __annotated (var, attr) __builtin_has_attribute(var, attr) +#endif + +/* +* Optional: only supported since gcc >= 15, clang >= 19 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fcounted_005fby_005fref +* clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-counted-by-ref +*/ +#if __has_builtin (__builtin_counted_by_ref) && \ + !defined(CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF ) +#define __flex_counter (FAM ) __builtin_counted_by_ref(FAM) +#else +#define __flex_counter (FAM ) ((void *)NULL) +#endif + +/* +* Some versions of gcc do not mark 'asm goto' volatile: +* +* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979 +* +* We do it here by hand, because it doesn't hurt. +*/ #ifndef asm_goto_output -#define asm_goto_output(x...) asm goto(x) +#define asm_goto_output(x...) asm volatile goto(x) +#endif + +/* +* Clang has trouble with constraints with multiple +* alternative behaviors ("g" , "rm" and "=rm"). +*/ +#ifndef ASM_INPUT_G +#define ASM_INPUT_G "g" +#define ASM_INPUT_RM "rm" +#define ASM_OUTPUT_RM "=rm" +#endif + +#ifdef CONFIG_CC_HAS_ASM_INLINE +#define asm_inline asm __inline +#else +#define asm_inline asm +#endif + +#ifndef __ASSEMBLY__ +/* +* Use __typeof_unqual__() when available. +*/ +#if (defined(CC_HAS_TYPEOF_UNQUAL ) &&CC_HAS_TYPEOF_UNQUAL ) || defined(__CHECKER__) +# define USE_TYPEOF_UNQUAL 1 +#endif + +/* Are two types/vars the same type (ignoring qualifiers)? */ +#ifndef __same_type +# define __same_type (a, b) __builtin_types_compatible_p(typeof(a), typeof(b))#endif /* * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving * non-scalar types unchanged. */ +#ifndef USE_TYPEOF_UNQUAL /* * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char' * is not type-compatible with 'signed char', and we define a separate case. @@ -45,5 +147,103 @@ __scalar_type_to_expr_cases(long), \ __scalar_type_to_expr_cases(long long), \ default: (x))) +#else +#define __unqual_scalar_typeof(x) __typeof_unqual__(x) +#endif + +#endif /* !__ASSEMBLY__ */ + +/* +* __signed_scalar_typeof(x) - Declare a signed scalar type, leaving +* non-scalar types unchanged. +*/ + +#define __scalar_type_to_signed_cases (type) \ + unsigned type: (signed type)0, \ + signed type: (signed type)0 + +#define __signed_scalar_typeof (x) typeof( \ + _Generic((x), \ + char: (signed char)0, \ + __scalar_type_to_signed_cases(char), \ + __scalar_type_to_signed_cases(short), \ + __scalar_type_to_signed_cases(int), \ + __scalar_type_to_signed_cases(long), \ + __scalar_type_to_signed_cases(long long), \ + default: (x))) + +/* Is this type a native word size -- useful for atomic operations */ +#define __native_word (t) \ + (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \ + sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) + +#ifdef __OPTIMIZE__ +/* +* #ifdef __OPTIMIZE__ is only a good approximation; for instance "make +* CFLAGS_foo.o=-Og" defines __OPTIMIZE__, does not elide the conditional code +* and can break compilation with wrong error message(s). Combine with +* -U__OPTIMIZE__ when needed. +*/ +# define __compiletime_assert(condition, msg, prefix, suffix) \ + do { \ + /* \ +* __noreturn is needed to give the compiler enough \ +* information to avoid certain possibly-uninitialized \ +* warnings (regardless of the build failing). \ +*/ \ + __noreturn extern void prefix ## suffix(void) \ + __compiletime_error(msg); \ + if (!(condition)) \ + prefix ## suffix(); \ + } while (0) +#else +# define __compiletime_assert (condition, msg, prefix, suffix) ((void)(condition)) +#endif + +#define _compiletime_assert (condition, msg, prefix, suffix) \ + __compiletime_assert(condition, msg, prefix, suffix) + +/** +* compiletime_assert - break build and emit msg if condition is false +* @condition: a compile-time constant condition to check +* @msg: a message to emit if condition is false +* +* In tradition of POSIX assert, this macro will break the build if the +* supplied condition is *false*, emitting the supplied error message if the +* compiler has support to do so. +*/ +#define compiletime_assert (condition, msg) \ + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) + +#define compiletime_assert_atomic_type (t) \ + compiletime_assert(__native_word(t), \ + "Need native word sized stores/loads for atomicity.") + +/* Helpers for emitting diagnostics in pragmas. */ +#ifndef __diag +#define __diag (string) +#endif + +#ifndef __diag_GCC +#define __diag_GCC (version, severity, string) +#endif + +#ifndef __diag_clang +#define __diag_clang (version, severity, string) +#endif + +#define __diag_push () __diag(push) +#define __diag_pop () __diag(pop) + +#define __diag_ignore (compiler, version, option, comment) \ + __diag_ ## compiler(version, ignore, option) +#define __diag_warn (compiler, version, option, comment) \ + __diag_ ## compiler(version, warn, option) +#define __diag_error (compiler, version, option, comment) \ + __diag_ ## compiler(version, error, option) + +#ifndef __diag_ignore_all +#define __diag_ignore_all (option, comment) +#endif #endif /* __LINUX_COMPILER_TYPES_H */ Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerRe: [PATCH v12 02/20] tools headers compiler: Synchronize compiler_types.h with the kernel sources
Quoted 18 lines · Namhyung Kim
Quoted 16 lines · Ian Rogers
Synchronize the non-__KERNEL__ sections of
tools/include/linux/compiler_types.handtools/include/linux/compiler-gcc.hwith the kernel sources (including ___PASTE/__PASTE,BTF_TYPE_TAG, object-size helpers,asm_goto_output, ASM_INPUT_*,asm_inline, __typeof_unqual__/__unqual_scalar_typeof,__signed_scalar_typeof,__native_word,compiletime_assert, and the__diagpragma helpers required bycompiler-context-analysis.h), and remove the duplicated definitions fromtools/include/linux/compiler.h.Signed-off-by Ian Rogers <[email protected]>--- tools/include/linux/compiler-gcc.h | 72 +++++++++- tools/include/linux/compiler.h | 50 +------ tools/include/linux/compiler_types.h | 206 ++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 53 deletions(-)
Quoted 12 lines
tools/include/linux/compiler-gcc.h line 2
Quoted 8 lines
tools/include/linux/compiler-gcc.h +1
-1@@ -1,7 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_TYPES_HI'm not sure if it's better to keep the TOOLS part in the macro guard. It would make the sync-ing a bit harder. And I also see other files already don't have TOOLS so it seems ok to drop.
Agreed tools/include/linux/compiler_types.h already used__LINUX_COMPILER_TYPES_H for its guard, and since it now includes <linux/compiler-gcc.h> directly (matching the kernel), compiler-gcc.h needs to check __LINUX_COMPILER_TYPES_H as well so direct includes of <linux/compiler_types.h> work. Kept as-is in v13.
Thanks,
Ian
Quoted 424 lines
Thanks,
NamhyungQuoted 420 lines
tools/include/linux/compiler-gcc.h +68
-2#error "Please do not include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." -#endif +#endif /* __LINUX_COMPILER_TYPES_H */ /* * Common definitions for all gcc versions go here. @@ -33,10 +33,76 @@#define __packed __attribute__((packed))#endif #ifndef __noreturn -#define __noreturn __attribute__((noreturn)) +#define __noreturn __attribute__((__noreturn__))#endif #ifndef __aligned#define __aligned (x) __attribute__((aligned(x)))#endif #define __printf (a, b) __attribute__((format(printf, a, b)))#define __scanf (a, b) __attribute__((format(scanf, a, b))) + +/* +* Turn individual warnings and errors on and off locally, depending +* on version. +*/ +#define __diag_str1 (s) #s +#define __diag_str (s) __diag_str1(s) + +#ifdef __clang__ +#define __diag_clang (version, severity, s) \ + __diag_clang_ ## version(__diag_clang_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_clang_ignore ignored +#define __diag_clang_warn warning +#define __diag_clang_error error + +#define __diag (s) _Pragma(__diag_str(clang diagnostic s)) + +#if __clang_major__ >=23 +#define __diag_clang_23 (s) __diag(s) +#else +#define __diag_clang_23 (s) +#endif + +#define __diag_clang_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag_clang(all, ignore, option) + +/* +* clang has horrible behavior with "g" or "rm" constraints for asm +* inputs, turning them into something worse than "m". Avoid using +* constraints with multiple possible uses (but "ir" seems to be ok): +* +* https://github.com/llvm/llvm-project/issues/20571 +*/ +#define ASM_INPUT_G "ir" +#define ASM_INPUT_RM "r" +#define ASM_OUTPUT_RM "=r" + +#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ > 19 || (__clang_major__ == 19 && __clang_minor__ > 0)) +#else /* !__clang__ */ +#define __diag_GCC (version, severity, s) \ + __diag_GCC_ ## version(__diag_GCC_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_GCC_ignore ignored +#define __diag_GCC_warn warning +#define __diag_GCC_error error + +#define __diag (s) _Pragma(__diag_str(GCC diagnostic s)) + +#if GCC_VERSION >=80000 +#define __diag_GCC_8 (s) __diag(s) +#else +#define __diag_GCC_8 (s) +#endif + +#define __diag_GCC_all (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag(__diag_GCC_ignore option) + +#define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >= 14) +#endif /* __clang__ */ tools/include/linux/compiler.h +3
-47@@ -1,41 +1,11 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ -#define _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_H +#define __LINUX_COMPILER_H#ifndef __ASSEMBLY__#include <linux/compiler_types.h> -#ifndef __compiletime_error -# define __compiletime_error (message) -#endif - -#ifdef __OPTIMIZE__ -# define __compiletime_assert (condition, msg, prefix, suffix) \ - do { \ - extern void prefix ## suffix(void) __compiletime_error(msg); \ - if (!(condition)) \ - prefix ## suffix(); \ - } while (0) -#else -# define __compiletime_assert (condition, msg, prefix, suffix) do { } while (0) -#endif - -#define _compiletime_assert (condition, msg, prefix, suffix) \ - __compiletime_assert(condition, msg, prefix, suffix) - -/** -* compiletime_assert - break build and emit msg if condition is false -* @condition: a compile-time constant condition to check -* @msg: a message to emit if condition is false -* -* In tradition of POSIX assert, this macro will break the build if the -* supplied condition is *false*, emitting the supplied error message if the -* compiler has support to do so. -*/ -#define compiletime_assert (condition, msg) \ - _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) -/* Optimization barrier */ /* The "volatile" is due to gcc bugs */ #define barrier () __asm__ __volatile__("": : :"memory")@@ -48,10 +18,6 @@#define __always_unused __attribute__((__unused__))#endif -#ifndef __noreturn -#define __noreturn __attribute__((__noreturn__)) -#endif -#ifndef unreachable#define unreachable () __builtin_unreachable()#endif @@ -68,11 +34,6 @@#define __naked __attribute__((__naked__))#endif -/* Are two types/vars the same type (ignoring qualifiers)? */ -#ifndef __same_type -# define __same_type (a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) -#endif -/* Enforce static storage duration. */ #define ASSERT_STATIC_STORAGE (name) \ static typeof(name) * const __always_unused \@@ -242,11 +203,6 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s __u.__val; \ }) - -/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ -#define ___PASTE (a, b) a##b -#define __PASTE (a, b) ___PASTE(a, b) -#ifndef OPTIMIZER_HIDE_VAR /* Make the optimizer believe the variable can be manipulated arbitrarily. */ #define OPTIMIZER_HIDE_VAR (var) \@@ -263,4 +219,4 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s#endif /* __ASSEMBLY__ */ -#endif /* _TOOLS_LINUX_COMPILER_H */ +#endif /* __LINUX_COMPILER_H */ tools/include/linux/compiler_types.h +203
-4@@ -2,8 +2,6 @@#ifndef __LINUX_COMPILER_TYPES_H#define __LINUX_COMPILER_TYPES_H -/* Builtins */ -/* * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21. * In the meantime, to support gcc < 10, we implement __has_builtin @@ -13,6 +11,23 @@#define __has_builtin (x) (0)#endif +/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ +#define ___PASTE (a, b) a##b +#define __PASTE (a, b) ___PASTE(a, b) + +#ifndef __ASSEMBLY__ + +/* +* Skipped when running bindgen due to a libclang issue; +* see https://github.com/rust-lang/rust-bindgen/issues/2244. +*/ +#if defined(CONFIG_DEBUG_INFO_BTF ) && defined(CONFIG_PAHOLE_HAS_BTF_TAG ) && \ +__has_attribute (btf_type_tag) && !defined(__BINDGEN__) +# define BTF_TYPE_TAG (value) __attribute__((btf_type_tag(#value))) +#else +# define BTF_TYPE_TAG (value)/* nothing */ +#endif +#include <linux/compiler-context-analysis.h> /* Compiler specific macros. */ @@ -20,14 +35,101 @@#include <linux/compiler-gcc.h> #endif +#ifndef __compiletime_error +# define __compiletime_error (message) +#endif + +#ifndef __noreturn +# define __noreturn __attribute__((__noreturn__)) +#endif + +#endif /* __ASSEMBLY__ */ + +/* +* When the size of an allocated object is needed, use the best available +* mechanism to find it. (For cases where sizeof() cannot be used.) +* +* Optional: only supported since gcc >= 12 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html +* clang: https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-object-size +*/ +#if __has_builtin (__builtin_dynamic_object_size) +#define __struct_size (p) __builtin_dynamic_object_size(p, 0) +#define __member_size (p) __builtin_dynamic_object_size(p, 1) +#else +#define __struct_size (p) __builtin_object_size(p, 0) +#define __member_size (p) __builtin_object_size(p, 1) +#endif + +/* +* Determine if an attribute has been applied to a variable. +* Using __annotated needs to check for __annotated being available, +* or negative tests may fail when annotation cannot be checked. For +* example, see the definition of __is_cstr(). +*/ +#if __has_builtin (__builtin_has_attribute) +#define __annotated (var, attr) __builtin_has_attribute(var, attr) +#endif + +/* +* Optional: only supported since gcc >= 15, clang >= 19 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fcounted_005fby_005fref +* clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-counted-by-ref +*/ +#if __has_builtin (__builtin_counted_by_ref) && \ + !defined(CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF ) +#define __flex_counter (FAM ) __builtin_counted_by_ref(FAM) +#else +#define __flex_counter (FAM ) ((void *)NULL) +#endif + +/* +* Some versions of gcc do not mark 'asm goto' volatile: +* +* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979 +* +* We do it here by hand, because it doesn't hurt. +*/ #ifndef asm_goto_output -#define asm_goto_output(x...) asm goto(x) +#define asm_goto_output(x...) asm volatile goto(x) +#endif + +/* +* Clang has trouble with constraints with multiple +* alternative behaviors ("g" , "rm" and "=rm"). +*/ +#ifndef ASM_INPUT_G +#define ASM_INPUT_G "g" +#define ASM_INPUT_RM "rm" +#define ASM_OUTPUT_RM "=rm" +#endif + +#ifdef CONFIG_CC_HAS_ASM_INLINE +#define asm_inline asm __inline +#else +#define asm_inline asm +#endif + +#ifndef __ASSEMBLY__ +/* +* Use __typeof_unqual__() when available. +*/ +#if (defined(CC_HAS_TYPEOF_UNQUAL ) &&CC_HAS_TYPEOF_UNQUAL ) || defined(__CHECKER__) +# define USE_TYPEOF_UNQUAL 1 +#endif + +/* Are two types/vars the same type (ignoring qualifiers)? */ +#ifndef __same_type +# define __same_type (a, b) __builtin_types_compatible_p(typeof(a), typeof(b))#endif /* * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving * non-scalar types unchanged. */ +#ifndef USE_TYPEOF_UNQUAL /* * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char' * is not type-compatible with 'signed char', and we define a separate case. @@ -45,5 +147,103 @@ __scalar_type_to_expr_cases(long), \ __scalar_type_to_expr_cases(long long), \ default: (x))) +#else +#define __unqual_scalar_typeof(x) __typeof_unqual__(x) +#endif + +#endif /* !__ASSEMBLY__ */ + +/* +* __signed_scalar_typeof(x) - Declare a signed scalar type, leaving +* non-scalar types unchanged. +*/ + +#define __scalar_type_to_signed_cases (type) \ + unsigned type: (signed type)0, \ + signed type: (signed type)0 + +#define __signed_scalar_typeof (x) typeof( \ + _Generic((x), \ + char: (signed char)0, \ + __scalar_type_to_signed_cases(char), \ + __scalar_type_to_signed_cases(short), \ + __scalar_type_to_signed_cases(int), \ + __scalar_type_to_signed_cases(long), \ + __scalar_type_to_signed_cases(long long), \ + default: (x))) + +/* Is this type a native word size -- useful for atomic operations */ +#define __native_word (t) \ + (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \ + sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) + +#ifdef __OPTIMIZE__ +/* +* #ifdef __OPTIMIZE__ is only a good approximation; for instance "make +* CFLAGS_foo.o=-Og" defines __OPTIMIZE__, does not elide the conditional code +* and can break compilation with wrong error message(s). Combine with +* -U__OPTIMIZE__ when needed. +*/ +# define __compiletime_assert (condition, msg, prefix, suffix) \ + do { \ +/* \ +* __noreturn is needed to give the compiler enough \ +* information to avoid certain possibly-uninitialized \ +* warnings (regardless of the build failing). \ +*/ \ + __noreturn extern void prefix ## suffix(void) \ + __compiletime_error(msg); \ + if (!(condition)) \ + prefix ## suffix(); \ + } while (0) +#else +# define __compiletime_assert (condition, msg, prefix, suffix) ((void)(condition)) +#endif + +#define _compiletime_assert (condition, msg, prefix, suffix) \ + __compiletime_assert(condition, msg, prefix, suffix) + +/** +* compiletime_assert - break build and emit msg if condition is false +* @condition: a compile-time constant condition to check +* @msg: a message to emit if condition is false +* +* In tradition of POSIX assert, this macro will break the build if the +* supplied condition is *false*, emitting the supplied error message if the +* compiler has support to do so. +*/ +#define compiletime_assert (condition, msg) \ + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) + +#define compiletime_assert_atomic_type (t) \ + compiletime_assert(__native_word(t), \ + "Need native word sized stores/loads for atomicity.") + +/* Helpers for emitting diagnostics in pragmas. */ +#ifndef __diag +#define __diag (string) +#endif + +#ifndef __diag_GCC +#define __diag_GCC (version, severity, string) +#endif + +#ifndef __diag_clang +#define __diag_clang (version, severity, string) +#endif + +#define __diag_push () __diag(push) +#define __diag_pop () __diag(pop) + +#define __diag_ignore (compiler, version, option, comment) \ + __diag_ ## compiler(version, ignore, option) +#define __diag_warn (compiler, version, option, comment) \ + __diag_ ## compiler(version, warn, option) +#define __diag_error (compiler, version, option, comment) \ + __diag_ ## compiler(version, error, option) + +#ifndef __diag_ignore_all +#define __diag_ignore_all (option, comment) +#endif #endif /* __LINUX_COMPILER_TYPES_H */ --2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerPATCH v12 3/20 tools headers: Synchronize linux/compiler-context-analysis.h with the kernel sources
In reply to Ian RogersReplace the no-op stub tools/include/linux/compiler-context-analysis.h with the full kernel implementation from include/linux/compiler-context-analysis.h and add it to tools/perf/check-headers.sh so future changes are tracked.
--- .../include/linux/compiler-context-analysis.h | 518 ++++++++++++++++-- tools/perf/check-headers.sh | 1 + 2 files changed, 480 insertions(+), 39 deletions(-)
tools/include/linux/compiler-context-analysis.h +479 -39
@@ -1,42 +1,482 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H -#define _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H -/* * Macros and attributes for compiler-based static context analysis. -* No-op stubs for tools. -*/ - -#define __guarded_by (...) -#define __pt_guarded_by (...) - -#define context_lock_struct (name, ...) struct __VA_ARGS__ name - -#define __no_context_analysis -#define __context_unsafe (comment) -#define context_unsafe (...) ({ __VA_ARGS__; }) -#define context_unsafe_alias (p) -#define disable_context_analysis () -#define enable_context_analysis () - -#define __must_hold (...) -#define __must_not_hold (...) -#define __acquires (...) -#define __cond_acquires (ret, x) -#define __releases (...) -#define __acquire (x) (void)0 -#define __release (x) (void)0 - -#define __must_hold_shared (...) -#define __acquires_shared (...) -#define __cond_acquires_shared (ret, x) -#define __releases_shared (...) -#define __acquire_shared (x) (void)0 -#define __release_shared (x) (void)0 - -#define __acquire_ret (call, expr) (call) -#define __acquire_shared_ret (call, expr) (call) -#define __acquires_ret -#define __acquires_shared_ret - -#endif /* _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H */ +*/ + +#ifndef _LINUX_COMPILER_CONTEXT_ANALYSIS_H +#define _LINUX_COMPILER_CONTEXT_ANALYSIS_H + +#if defined(WARN_CONTEXT_ANALYSIS ) && !defined(__CHECKER__) && !defined(__GENKSYMS__) + +/* +* These attributes define new context lock (Clang: capability) types. +* Internal only. +*/ +# define __ctx_lock_type (name) __attribute__((capability(#name))) +# define __reentrant_ctx_lock __attribute__((reentrant_capability)) +# define __acquires_ctx_lock (...) __attribute__((acquire_capability(__VA_ARGS__))) +# define __acquires_shared_ctx_lock (...) __attribute__((acquire_shared_capability(__VA_ARGS__))) +# define __try_acquires_ctx_lock (ret, var) __attribute__((try_acquire_capability(ret, var))) +# define __try_acquires_shared_ctx_lock (ret, var) __attribute__((try_acquire_shared_capability(ret, var))) +# define __releases_ctx_lock (...) __attribute__((release_capability(__VA_ARGS__))) +# define __releases_shared_ctx_lock (...) __attribute__((release_shared_capability(__VA_ARGS__))) +# define __returns_ctx_lock (var) __attribute__((lock_returned(var))) + +/* +* The below are used to annotate code being checked. Internal only. +*/ +# define __excludes_ctx_lock (...) __attribute__((locks_excluded(__VA_ARGS__))) +# define __requires_ctx_lock (...) __attribute__((requires_capability(__VA_ARGS__))) +# define __requires_shared_ctx_lock (...) __attribute__((requires_shared_capability(__VA_ARGS__))) + +/* +* The "assert_capability" attribute is a bit confusingly named. It does not +* generate a check. Instead, it tells the analysis to *assume* the capability +* is held. This is used for augmenting runtime assertions, that can then help +* with patterns beyond the compiler's static reasoning abilities. +*/ +# define __assumes_ctx_lock (...) __attribute__((assert_capability(__VA_ARGS__))) +# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__))) + +/** +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @...: context lock instance pointer(s) +* +* Declares that the struct member or global variable is only accessible within +* the context entered by the given context lock(s). Read operations on the data +* require shared access to at least one of the context locks, while write +* operations require exclusive access to all listed context locks. +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock; +* long counter __guarded_by(&lock); +* }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by(&lock1, &lock2); +* }; +*/ +# define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__))) + +/** +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @...: context lock instance pointer(s) +* +* Declares that the data pointed to by the struct member pointer or global +* pointer is only accessible within the context entered by the given context +* lock(s). Read operations on the data require shared access to at least one +* of the context locks, while write operations require exclusive access to all +* listed context locks. +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock; +* long *counter __pt_guarded_by(&lock); +* }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by(&lock1, &lock2); +* }; +*/ +# define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__))) + +/** +* context_lock_struct() - declare or define a context lock struct +* @name: struct name +* +* Helper to declare or define a struct type that is also a context lock. +* +* .. code-block:: c +* +* context_lock_struct(my_handle) { +* int foo; +* long bar; +* }; +* +* struct some_state { +* ... +* }; +* // ... declared elsewhere ... +* context_lock_struct(some_state); +* +* Note: The implementation defines several helper functions that can acquire +* and release the context lock. +*/ +# define context_lock_struct (name, ...) \ + struct __ctx_lock_type(name) __VA_ARGS__ name; \ + static __always_inline void __acquire_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __no_context_analysis __acquires_ctx_lock(var) { } \ + static __always_inline void __acquire_shared_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __no_context_analysis __acquires_shared_ctx_lock(var) { } \ + static __always_inline bool __try_acquire_ctx_lock(const struct name *var, bool ret) \ + __attribute__((overloadable)) __no_context_analysis __try_acquires_ctx_lock(1, var) \ + { return ret; } \ + static __always_inline bool __try_acquire_shared_ctx_lock(const struct name *var, bool ret) \ + __attribute__((overloadable)) __no_context_analysis __try_acquires_shared_ctx_lock(1, var) \ + { return ret; } \ + static __always_inline void __release_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __no_context_analysis __releases_ctx_lock(var) { } \ + static __always_inline void __release_shared_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __no_context_analysis __releases_shared_ctx_lock(var) { } \ + static __always_inline void __assume_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __assumes_ctx_lock(var) { } \ + static __always_inline void __assume_shared_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __assumes_shared_ctx_lock(var) { } \ + struct name + +/** +* disable_context_analysis() - disables context analysis +* +* Disables context analysis. Must be paired with a later +* enable_context_analysis(). +*/ +# define disable_context_analysis () \ + __diag_push(); \ + __diag_ignore_all("-Wunknown-warning-option", "") \ + __diag_ignore_all("-Wthread-safety", "") \ + __diag_ignore_all("-Wthread-safety-pointer", "") + +/** +* enable_context_analysis() - re-enables context analysis +* +* Re-enables context analysis. Must be paired with a prior +* disable_context_analysis(). +*/ +# define enable_context_analysis () __diag_pop() + +/** +* __no_context_analysis - function attribute, disables context analysis +* +* Function attribute denoting that context analysis is disabled for the +* whole function. Prefer use of `context_unsafe()` where possible. +*/ +# define __no_context_analysis __attribute__((no_thread_safety_analysis)) + +#else /* !WARN_CONTEXT_ANALYSIS */ + +# define __ctx_lock_type (name) +# define __reentrant_ctx_lock +# define __acquires_ctx_lock (...) +# define __acquires_shared_ctx_lock (...) +# define __try_acquires_ctx_lock (ret, var) +# define __try_acquires_shared_ctx_lock (ret, var) +# define __releases_ctx_lock (...) +# define __releases_shared_ctx_lock (...) +# define __assumes_ctx_lock (...) +# define __assumes_shared_ctx_lock (...) +# define __returns_ctx_lock (var) +# define __guarded_by (...) +# define __pt_guarded_by (...) +# define __excludes_ctx_lock (...) +# define __requires_ctx_lock (...) +# define __requires_shared_ctx_lock (...) +# define __acquire_ctx_lock (var) do { } while (0) +# define __acquire_shared_ctx_lock (var) do { } while (0) +# define __try_acquire_ctx_lock (var, ret) (ret) +# define __try_acquire_shared_ctx_lock (var, ret) (ret) +# define __release_ctx_lock (var) do { } while (0) +# define __release_shared_ctx_lock (var) do { } while (0) +# define __assume_ctx_lock (var) do { (void)(var); } while (0) +# define __assume_shared_ctx_lock (var) do { (void)(var); } while (0) +# define context_lock_struct (name, ...) struct __VA_ARGS__ name +# define disable_context_analysis () +# define enable_context_analysis () +# define __no_context_analysis + +#endif /* WARN_CONTEXT_ANALYSIS */ + +/** +* context_unsafe() - disable context checking for contained code +* +* Disables context checking for contained statements or expression. +* +* .. code-block:: c +* +* struct some_data { +* spinlock_t lock; +* int counter __guarded_by(&lock); +* }; +* +* int foo(struct some_data *d) +* { +* // ... +* // other code that is still checked ... +* // ... +* return context_unsafe(d->counter); +* } +*/ +#define context_unsafe (...) \ +({ \ + disable_context_analysis(); \ + __VA_ARGS__; \ + enable_context_analysis() \ +}) + +/** +* __context_unsafe() - function attribute, disable context checking +* @comment: comment explaining why opt-out is safe +* +* Function attribute denoting that context analysis is disabled for the +* whole function. Forces adding an inline comment as argument. +*/ +#define __context_unsafe (comment) __no_context_analysis + +/** +* context_unsafe_alias() - helper to insert a context lock "alias barrier" +* @p: pointer aliasing a context lock or object containing context locks +* +* No-op function that acts as a "context lock alias barrier", where the +* analysis rightfully detects that we're switching aliases, but the switch is +* considered safe but beyond the analysis reasoning abilities. +* +* This should be inserted before the first use of such an alias. +* +* Implementation Note: The compiler ignores aliases that may be reassigned but +* their value cannot be determined (e.g. when passing a non-const pointer to an +* alias as a function argument). +*/ +#define context_unsafe_alias (p) _context_unsafe_alias((void **)&(p)) +static inline void _context_unsafe_alias (void **p) { (void )p; } + +/** +* token_context_lock() - declare an abstract global context lock instance +* @name: token context lock name +* +* Helper that declares an abstract global context lock instance @name, but not +* backed by a real data structure (linker error if accidentally referenced). +* The type name is `__ctx_lock_@name`. +*/ +#define token_context_lock (name, ...) \ + context_lock_struct(__ctx_lock_##name, ##__VA_ARGS__) {}; \ + extern const struct __ctx_lock_##name *name + +/** +* token_context_lock_instance() - declare another instance of a global context lock +* @ctx: token context lock previously declared with token_context_lock() +* @name: name of additional global context lock instance +* +* Helper that declares an additional instance @name of the same token context +* lock class @ctx. This is helpful where multiple related token contexts are +* declared, to allow using the same underlying type (`__ctx_lock_@ctx`) as +* function arguments. +*/ +#define token_context_lock_instance (ctx, name) \ + extern const struct __ctx_lock_##ctx *name + +/* +* Common keywords for static context analysis. +*/ + +/** +* __must_hold() - function attribute, caller must hold exclusive context lock +* +* Function attribute declaring that the caller must hold the given context +* lock instance(s) exclusively. +*/ +#define __must_hold (...) __requires_ctx_lock(__VA_ARGS__) + +/** +* __must_not_hold() - function attribute, caller must not hold context lock +* +* Function attribute declaring that the caller must not hold the given context +* lock instance(s). +*/ +#define __must_not_hold (...) __excludes_ctx_lock(__VA_ARGS__) + +/** +* __acquires() - function attribute, function acquires context lock exclusively +* +* Function attribute declaring that the function acquires the given context +* lock instance(s) exclusively, but does not release them. +*/ +#define __acquires (...) __acquires_ctx_lock(__VA_ARGS__) + +/* +* Clang's analysis does not care precisely about the value, only that it is +* either zero or non-zero. So the __cond_acquires() interface might be +* misleading if we say that @ret is the value returned if acquired. Instead, +* provide symbolic variants which we translate. +*/ +#define __cond_acquires_impl_true (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) +#define __cond_acquires_impl_false (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) +#define __cond_acquires_impl_nonzero (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) +#define __cond_acquires_impl_0 (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) +#define __cond_acquires_impl_nonnull (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) +#define __cond_acquires_impl_NULL (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) + +/** +* __cond_acquires() - function attribute, function conditionally +* acquires a context lock exclusively +* @ret: abstract value returned by function if context lock acquired +* @x: context lock instance pointer +* +* Function attribute declaring that the function conditionally acquires the +* given context lock instance @x exclusively, but does not release it. The +* function return value @ret denotes when the context lock is acquired. +* +* @ret may be one of: true, false, nonzero, 0, nonnull, NULL. +*/ +#define __cond_acquires (ret, x) __cond_acquires_impl_##ret(x) + +/** +* __releases() - function attribute, function releases a context lock exclusively +* +* Function attribute declaring that the function releases the given context +* lock instance(s) exclusively. The associated context(s) must be active on +* entry. +*/ +#define __releases (...) __releases_ctx_lock(__VA_ARGS__) + +/* +* Clang's analysis does not care precisely about the value, only that it is +* either zero or non-zero. So the __cond_acquires() interface might be +* misleading if we say that @ret is the value returned if acquired. Instead, +* provide symbolic variants which we translate. +*/ +#define __cond_acquires_impl_not_true (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_false (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) +#define __cond_acquires_impl_not_nonzero (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_0 (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) +#define __cond_acquires_impl_not_nonnull (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_NULL (x, ...) __try_acquires##__VA_ARGS__##_ctx_lock(1, x) + +/** +* __cond_releases() - function attribute, function conditionally +* releases a context lock exclusively +* @ret: abstract value returned by function if context lock releases +* @x: context lock instance pointer +* +* Function attribute declaring that the function conditionally releases the +* given context lock instance @x exclusively. The associated context(s) must +* be active on entry. The function return value @ret denotes when the context +* lock is released. +* +* @ret may be one of: true, false, nonzero, 0, nonnull, NULL. +* +* NOTE: clang does not have a native attribute for this; instead implement +* it as an unconditional release and a conditional acquire for the +* inverted condition -- which is semantically equivalent. +*/ +#define __cond_releases (ret, x) __releases(x) __cond_acquires_impl_not_##ret(x) + +/** +* __acquire() - function to acquire context lock exclusively +* @x: context lock instance pointer +* +* No-op function that acquires the given context lock instance @x exclusively. +*/ +#define __acquire (x) __acquire_ctx_lock(x) + +/** +* __release() - function to release context lock exclusively +* @x: context lock instance pointer +* +* No-op function that releases the given context lock instance @x. +*/ +#define __release (x) __release_ctx_lock(x) + +/** +* __must_hold_shared() - function attribute, caller must hold shared context lock +* +* Function attribute declaring that the caller must hold the given context +* lock instance(s) with shared access. +*/ +#define __must_hold_shared (...) __requires_shared_ctx_lock(__VA_ARGS__) + +/** +* __acquires_shared() - function attribute, function acquires context lock shared +* +* Function attribute declaring that the function acquires the given +* context lock instance(s) with shared access, but does not release them. +*/ +#define __acquires_shared (...) __acquires_shared_ctx_lock(__VA_ARGS__) + +/** +* __cond_acquires_shared() - function attribute, function conditionally +* acquires a context lock shared +* @ret: abstract value returned by function if context lock acquired +* @x: context lock instance pointer +* +* Function attribute declaring that the function conditionally acquires the +* given context lock instance @x with shared access, but does not release it. +* The function return value @ret denotes when the context lock is acquired. +* +* @ret may be one of: true, false, nonzero, 0, nonnull, NULL. +*/ +#define __cond_acquires_shared (ret, x) __cond_acquires_impl_##ret(x, _shared) + +/** +* __releases_shared() - function attribute, function releases a +* context lock shared +* +* Function attribute declaring that the function releases the given context +* lock instance(s) with shared access. The associated context(s) must be +* active on entry. +*/ +#define __releases_shared (...) __releases_shared_ctx_lock(__VA_ARGS__) + +/** +* __acquire_shared() - function to acquire context lock shared +* @x: context lock instance pointer +* +* No-op function that acquires the given context lock instance @x with shared +* access. +*/ +#define __acquire_shared (x) __acquire_shared_ctx_lock(x) + +/** +* __release_shared() - function to release context lock shared +* @x: context lock instance pointer +* +* No-op function that releases the given context lock instance @x with shared +* access. +*/ +#define __release_shared (x) __release_shared_ctx_lock(x) + +/** +* __acquire_ret() - helper to acquire context lock of return value +* @call: call expression +* @ret_expr: acquire expression that uses __ret +*/ +#define __acquire_ret (call, ret_expr) \ + ({ \ + __auto_type __ret = call; \ + __acquire(ret_expr); \ + __ret; \ + }) + +/** +* __acquire_shared_ret() - helper to acquire context lock shared of return value +* @call: call expression +* @ret_expr: acquire shared expression that uses __ret +*/ +#define __acquire_shared_ret (call, ret_expr) \ + ({ \ + __auto_type __ret = call; \ + __acquire_shared(ret_expr); \ + __ret; \ + }) + +/* +* Attributes to mark functions returning acquired context locks. +* +* This is purely cosmetic to help readability, and should be used with the +* above macros as follows: +* +* struct foo { spinlock_t lock; ... }; +* ... +* #define myfunc(...) __acquire_ret(_myfunc(__VA_ARGS__), &__ret->lock) +* struct foo *_myfunc(int bar) __acquires_ret; +* ... +*/ +#define __acquires_ret __no_context_analysis +#define __acquires_shared_ret __no_context_analysis + +#endif /* _LINUX_COMPILER_CONTEXT_ANALYSIS_H */
tools/perf/check-headers.sh +1 -0
@@ -23,6 +23,7 @@ declare -a FILES=( "include/linux/bits.h" "include/vdso/bits.h" "include/linux/cfi_types.h" + "include/linux/compiler-context-analysis.h" "include/linux/const.h" "include/vdso/const.h" "include/vdso/unaligned.h"
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 03/20] tools headers: Synchronize linux/compiler-context-analysis.h with the kernel sources
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Replace the no-op stub
tools/include/linux/compiler-context-analysis.hwith the full kernel implementation frominclude/linux/compiler-context-analysis.hand add it totools/perf/check-headers.shso future changes are tracked.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v12 4/20 perf mutex: Use kernel style compiler-context-analysis.h annotations
In reply to Ian RogersEnable -DWARN_CONTEXT_ANALYSIS alongside -Wthread-safety for Clang builds in tools/perf/Makefile.config, replace the custom lock safety macros in tools/perf with the kernel's compiler-context-analysis.h annotations (context_lock_struct, __guarded_by, __acquires, __acquires_shared, __releases, __releases_shared, __cond_acquires, __must_hold, __must_hold_shared, __must_not_hold, __returns_ctx_lock, __no_context_analysis, and __context_unsafe), and remove the redundant macro definitions from tools/perf/util/mutex.h.
--- tools/perf/Makefile.config | 2 +- tools/perf/builtin-sched.c | 16 +++---- tools/perf/builtin-top.c | 4 +- tools/perf/util/annotate.c | 4 +- tools/perf/util/annotate.h | 8 ++-- tools/perf/util/comm.c | 4 +- tools/perf/util/dso.c | 30 ++++++------ tools/perf/util/dso.h | 11 +++-- tools/perf/util/dsos.c | 6 +-- tools/perf/util/intel-tpebs.c | 6 +-- tools/perf/util/libbfd.c | 4 +- tools/perf/util/mutex.c | 4 +- tools/perf/util/mutex.h | 90 +++-------------------------------- tools/perf/util/rwsem.c | 8 ++-- tools/perf/util/rwsem.h | 10 ++-- tools/perf/util/thread.c | 8 ++-- tools/perf/util/thread.h | 2 +- 17 files changed, 70 insertions(+), 147 deletions(-)
tools/perf/Makefile.config +1 -1
@@ -64,7 +64,7 @@ ifeq ($(CC_NO_CLANG), 0) endif # Enabled Wthread-safety analysis for clang builds. - CFLAGS += -Wthread-safety + CFLAGS += -Wthread-safety -DWARN_CONTEXT_ANALYSIS endif include $(srctree)/tools/scripts/Makefile.arch
tools/perf/builtin-sched.c +8 -8
@@ -828,8 +828,8 @@ static void *thread_func(void *ctx) }static void create_tasks (struct perf_sched *sched) - EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex) - EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex) + __acquires(&sched->start_work_mutex) + __acquires(&sched->work_done_wait_mutex) {struct task_desc *task;pthread_attr_t attr;
@@ -858,8 +858,8 @@ static void create_tasks(struct perf_sched *sched) }static void destroy_tasks (struct perf_sched *sched) - UNLOCK_FUNCTION(sched->start_work_mutex) - UNLOCK_FUNCTION(sched->work_done_wait_mutex) + __releases(&sched->start_work_mutex) + __releases(&sched->work_done_wait_mutex) {struct task_desc *task;unsigned long i;
@@ -878,8 +878,8 @@ static void destroy_tasks(struct perf_sched *sched) }static void wait_for_tasks (struct perf_sched *sched) - EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) - EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) + __must_hold(&sched->work_done_wait_mutex) + __must_hold(&sched->start_work_mutex) {u64 cpu_usage_0, cpu_usage_1;struct task_desc *task;
@@ -930,8 +930,8 @@ static void wait_for_tasks(struct perf_sched *sched) }static void run_one_test (struct perf_sched *sched) - EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) - EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) + __must_hold(&sched->work_done_wait_mutex) + __must_hold(&sched->start_work_mutex) {u64 T0 ,T1 , delta, avg_delta, fluct;
tools/perf/builtin-top.c +2 -2
@@ -201,7 +201,7 @@ static void perf_top__record_precise_ip(struct perf_top *top,struct hist_entry *he, struct perf_sample *sample, u64 ip) - EXCLUSIVE_LOCKS_REQUIRED(he->hists->lock) + __must_hold(&he->hists->lock) { struct annotation *notes;struct symbol *sym = he->ms.sym;
@@ -730,7 +730,7 @@ static void *display_thread(void *arg)static int hist_iter__top_callback (struct hist_entry_iter *iter,struct addr_location *al,bool single,void *arg) - EXCLUSIVE_LOCKS_REQUIRED(iter->he->hists->lock) + __must_hold(&iter->he->hists->lock) {struct perf_top *top = arg;
tools/perf/util/annotate.c +2 -2
@@ -655,7 +655,7 @@ static struct mutex *annotation__get_mutex(const struct annotation *notes) }void annotation__lock (struct annotation *notes) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {struct mutex *mutex =annotation__get_mutex (notes);
@@ -664,7 +664,7 @@ void annotation__lock(struct annotation *notes) }void annotation__unlock (struct annotation *notes) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {struct mutex *mutex =annotation__get_mutex (notes);
tools/perf/util/annotate.h +4 -4
@@ -369,7 +369,7 @@ struct annotated_branch {u64 *br_cntr; }; -struct LOCKABLE annotation { +context_lock_struct (annotation) {struct annotated_source *src;struct annotated_branch *branch; };
@@ -379,9 +379,9 @@ static inline void annotation__init(struct annotation *notes __maybe_unused) }void annotation__exit (struct annotation *notes); -void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes); -void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes); -bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes); +void annotation__lock(struct annotation *notes) __acquires(notes); +void annotation__unlock(struct annotation *notes) __releases(notes); +bool annotation__trylock(struct annotation *notes) __cond_acquires(true, notes);static inline int annotation__cycles_width (struct annotation *notes) {
tools/perf/util/comm.c +2 -2
@@ -24,7 +24,7 @@ static struct comm_strs {static void comm_strs__remove_if_last (struct comm_str *cs);static void comm_strs__init (void ) -NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_once. */ +__context_unsafe (/* Inherently single threaded due to pthread_once. */ ) {init_rwsem (&_comm_strs.lock); _comm_strs.capacity =16 ;
@@ -120,7 +120,7 @@ static void comm_strs__remove_if_last(struct comm_str *cs) }static struct comm_str *__comm_strs__find (struct comm_strs *comm_strs,const char *str) - SHARED_LOCKS_REQUIRED(comm_strs->lock) + __must_hold_shared(&comm_strs->lock) {struct comm_str **result;
tools/perf/util/dso.c +15 -15
@@ -525,14 +525,14 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m,*/ struct mutex _dso__data_open_lock;static LIST_HEAD (dso__data_open); -static long dso__data_open_cnt GUARDED_BY (_dso__data_open_lock ); +static long dso__data_open_cnt __guarded_by(&_dso__data_open_lock);static void dso__data_open_lock_init (void ) {mutex_init (&_dso__data_open_lock); } -static struct mutex *dso__data_open_lock (void )LOCK_RETURNED (_dso__data_open_lock) +static struct mutex *dso__data_open_lock(void) __returns_ctx_lock(&_dso__data_open_lock) {static pthread_once_t data_open_lock_once =PTHREAD_ONCE_INIT ;
@@ -541,7 +541,7 @@ static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_open_locreturn &_dso__data_open_lock; } -static void dso__list_add (struct dso *dso)EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lock) {list_add_tail (&dso__data (dso)->open_entry, &dso__data_open);#ifdef REFCNT_CHECKING
@@ -552,7 +552,7 @@ static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_o dso__data_open_cnt++; } -static void dso__list_del (struct dso *dso)EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lock) {list_del_init (&dso__data (dso)->open_entry);#ifdef REFCNT_CHECKING
@@ -567,7 +567,7 @@ static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_ostatic void close_first_dso (void ); -static int do_open (char *name)EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static int do_open(char *name) __must_hold(&_dso__data_open_lock) { do { int fd = open(name, O_RDONLY|O_CLOEXEC);
@@ -670,7 +670,7 @@ void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool deco }static int __open_dso (struct dso *dso,struct machine *machine) -EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) {int fd = -EINVAL ;char *name;
@@ -703,7 +703,7 @@ static void check_data_close(void);* list/count of open DSO objects. */ static int open_dso (struct dso *dso,struct machine *machine) -EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) {int fd;struct nscookie nsc;
@@ -729,7 +729,7 @@ static int open_dso(struct dso *dso, struct machine *machine)return fd; } -static void close_data_fd (struct dso *dso)EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void close_data_fd(struct dso *dso) __must_hold(&_dso__data_open_lock) {if (dso__data (dso)->fd >=0 ) {close (dso__data (dso)->fd);
@@ -746,12 +746,12 @@ static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_o* Close @dso's data file descriptor and updates * list/count of open DSO objects. */ -static void close_dso (struct dso *dso)EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) {close_data_fd (dso); } -static void close_first_dso (void )EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void close_first_dso(void) __must_hold(&_dso__data_open_lock) {struct dso_data *dso_data;struct dso *dso;
@@ -796,7 +796,7 @@ void reset_fd_limit(void) fd_limit =0 ; } -static bool may_cache_fd (void )EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static bool may_cache_fd(void) __must_hold(&_dso__data_open_lock) {if (!fd_limit) fd_limit =get_fd_limit ();
@@ -812,7 +812,7 @@ static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)* for opened dso file descriptors. The limit is half * of the RLIMIT_NOFILE files opened. */ -static void check_data_close (void )EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) +static void check_data_close(void) __must_hold(&_dso__data_open_lock) {bool cache_fd =may_cache_fd ();
@@ -834,7 +834,7 @@ void dso__data_close(struct dso *dso) }static int dso__find_binary_type_locked (struct dso *dso) -EXCLUSIVE_LOCKS_REQUIRED (dso__lock (dso)) +__must_hold (dso__lock (dso)) {enum dso_binary_type binary_type_data[] = {DSO_BINARY_TYPE__BUILD_ID_CACHE ,
@@ -926,7 +926,7 @@ static int dso__find_binary_type_locked(struct dso *dso)#define FIND_MAX_RETRY 3static int __dso__find_binary_type (struct dso *dso) -EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) {int ret;int retry =0 ;
@@ -966,7 +966,7 @@ enum dso_binary_type dso__find_binary_type(struct dso *dso) }static void try_to_open_dso (struct dso *dso,struct machine *machine) -EXCLUSIVE_LOCKS_REQUIRED (_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) {struct dso_data *dso_data =dso__data (dso);
tools/perf/util/dso.h +6 -5
@@ -629,6 +629,7 @@ static inline void dso__set_loaded(struct dso *dso) }static inline struct mutex *dso__lock (struct dso *dso) + __returns_ctx_lock(&RC_CHK_ACCESS(dso)->lock) {return &RC_CHK_ACCESS (dso)->lock; }
@@ -760,7 +761,7 @@ static inline const char *dso__symsrc_filename(const struct dso *dso)return RC_CHK_ACCESS (dso)->symsrc_filename; } -void dso__set_symsrc_filename(struct dso *dso, char *val) EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); +void dso__set_symsrc_filename(struct dso *dso, char *val) __must_hold(dso__lock(dso));static inline void dso__free_symsrc_filename (struct dso *dso) {
@@ -809,7 +810,7 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id);int dso__name_len (const struct dso *dso);struct dso *dso__get (struct dso *dso); -void dso__put(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); +void dso__put(struct dso *dso) __must_not_hold(&_dso__data_open_lock);static inline void __dso__zput (struct dso **dso) {
@@ -919,9 +920,9 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m,* TODO */ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) - EXCLUSIVE_TRYLOCK_FUNCTION(true, _dso__data_open_lock); -void dso__data_put_fd(struct dso *dso) UNLOCK_FUNCTION(_dso__data_open_lock); -void dso__data_close(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); + __cond_acquires(true, &_dso__data_open_lock); +void dso__data_put_fd(struct dso *dso) __releases(&_dso__data_open_lock); +void dso__data_close(struct dso *dso) __must_not_hold(&_dso__data_open_lock);int dso__data_file_size (struct dso *dso,struct machine *machine);off_t dso__data_size (struct dso *dso,struct machine *machine);
tools/perf/util/dsos.c +3 -3
@@ -157,7 +157,7 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, const char *name, const struct dso_id *id, bool write_locked) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) { struct dsos__key key = { .long_name = name,
@@ -266,7 +266,7 @@ static int dsos__find_id_cb(struct dso *dso, void *data)static struct dso *__dsos__find_id (struct dsos *dsos,const char *name,const struct dso_id *id,bool cmp_short,bool write_locked) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) {struct dso *res;
@@ -330,7 +330,7 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const }static struct dso *__dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) {struct dso *dso =__dsos__find_id (dsos, name, id, false,/*write_locked=*/ true);
tools/perf/util/intel-tpebs.c +3 -3
@@ -69,7 +69,7 @@ static struct mutex *tpebs_mtx_get(void) }static struct tpebs_retire_lat *tpebs_retire_lat__find (struct evsel *evsel) -EXCLUSIVE_LOCKS_REQUIRED (tpebs_mtx_get ()); +__must_hold (tpebs_mtx_get ());static int evsel__tpebs_start_perf_record (struct evsel *evsel) {
@@ -244,7 +244,7 @@ static void *__sample_reader(void *arg __maybe_unused)return NULL ; } -static int tpebs_send_record_cmd (const char *msg)EXCLUSIVE_LOCKS_REQUIRED (tpebs_mtx_get ()) +static int tpebs_send_record_cmd (const char *msg)__must_hold (tpebs_mtx_get ()) {struct pollfd pollfd = { .events =POLLIN , };int ret, len, retries =0 ;
@@ -321,7 +321,7 @@ static int tpebs_send_record_cmd(const char *msg) EXCLUSIVE_LOCKS_REQUIRED(tpebs/* * tpebs_stop - stop the sample data read thread and the perf record process. */ -static int tpebs_stop (void )EXCLUSIVE_LOCKS_REQUIRED (tpebs_mtx_get ()) +static int tpebs_stop (void )__must_hold (tpebs_mtx_get ()) {int ret =0 ;
tools/perf/util/libbfd.c +2 -2
@@ -51,13 +51,13 @@ struct a2l_data {asymbol **syms; }; -static bool perf_bfd_lock (void *bfd_mutex)NO_THREAD_SAFETY_ANALYSIS +static bool perf_bfd_lock (void *bfd_mutex) __no_context_analysis {mutex_lock (bfd_mutex);return true; } -static bool perf_bfd_unlock (void *bfd_mutex)NO_THREAD_SAFETY_ANALYSIS +static bool perf_bfd_unlock (void *bfd_mutex) __no_context_analysis {mutex_unlock (bfd_mutex);return true;
tools/perf/util/mutex.c +2 -2
@@ -125,14 +125,14 @@ void mutex_destroy(struct mutex *mtx) }void mutex_lock (struct mutex *mtx) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {CHECK_ERR (mutex_take_over (mtx,pthread_mutex_lock (&mtx->lock),/*give_up=*/ false)); }void mutex_unlock (struct mutex *mtx) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {CHECK_ERR (pthread_mutex_unlock (&mtx->lock)); }
tools/perf/util/mutex.h +6 -84
114 lines not on this page · raw message
tools/perf/util/rwsem.c +4 -4
@@ -27,7 +27,7 @@ int exit_rwsem(struct rw_semaphore *sem) }int down_read (struct rw_semaphore *sem) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {#if RWS_ERRORCHECK mutex_lock (&sem->mtx);
@@ -38,7 +38,7 @@ int down_read(struct rw_semaphore *sem) }int up_read (struct rw_semaphore *sem) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {#if RWS_ERRORCHECK mutex_unlock (&sem->mtx);
@@ -49,7 +49,7 @@ int up_read(struct rw_semaphore *sem) }int down_write (struct rw_semaphore *sem) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {#if RWS_ERRORCHECK mutex_lock (&sem->mtx);
@@ -60,7 +60,7 @@ int down_write(struct rw_semaphore *sem) }int up_write (struct rw_semaphore *sem) -NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis {#if RWS_ERRORCHECK mutex_unlock (&sem->mtx);
tools/perf/util/rwsem.h +5 -5
@@ -10,7 +10,7 @@*/ #define RWS_ERRORCHECK 0 -struct LOCKABLE rw_semaphore { +context_lock_struct (rw_semaphore) {#if RWS_ERRORCHECK struct mutex mtx;#else
@@ -21,10 +21,10 @@ struct LOCKABLE rw_semaphore {int init_rwsem (struct rw_semaphore *sem);int exit_rwsem (struct rw_semaphore *sem); -int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem); -int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); +int down_read(struct rw_semaphore *sem) __acquires_shared(sem); +int up_read(struct rw_semaphore *sem) __releases_shared(sem); -int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem); -int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); +int down_write(struct rw_semaphore *sem) __acquires(sem); +int up_write(struct rw_semaphore *sem) __releases(sem);#endif /* _PERF_RWSEM_H */
tools/perf/util/thread.c +4 -4
@@ -41,7 +41,7 @@ int thread__init_maps(struct thread *thread, struct machine *machine) }struct thread *thread__new (pid_t pid,pid_t tid) -NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */ +__context_unsafe (/* Allocation/creation is inherently single threaded. */ ) {RC_STRUCT (thread) *_thread =zalloc (sizeof (*_thread));struct thread *thread;
@@ -203,7 +203,7 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp, }static struct comm *__thread__comm (struct thread *thread) -SHARED_LOCKS_REQUIRED (thread__comm_lock (thread)) +__must_hold_shared (thread__comm_lock (thread)) {if (list_empty (thread__comm_list (thread)))return NULL ;
@@ -250,7 +250,7 @@ struct comm *thread__exec_comm(struct thread *thread)static int ____thread__set_comm (struct thread *thread,const char *str,u64 timestamp,bool exec) -EXCLUSIVE_LOCKS_REQUIRED (thread__comm_lock (thread)) +__must_hold (thread__comm_lock (thread)) {struct comm *new, *curr =__thread__comm (thread);
@@ -308,7 +308,7 @@ int thread__set_comm_from_proc(struct thread *thread) }static const char *__thread__comm_str (struct thread *thread) -SHARED_LOCKS_REQUIRED (thread__comm_lock (thread)) +__must_hold_shared (thread__comm_lock (thread)) {const struct comm *comm =__thread__comm (thread);
tools/perf/util/thread.h +1 -1
@@ -251,7 +251,7 @@ static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) }static inline struct list_head *thread__comm_list (struct thread *thread) -SHARED_LOCKS_REQUIRED (thread__comm_lock (thread)) +__must_hold_shared (thread__comm_lock (thread)) {return &RC_CHK_ACCESS (thread)->comm_list; }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 04/20] perf mutex: Use kernel style compiler-context-analysis.h annotations
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Enable -DWARN_CONTEXT_ANALYSIS alongside -Wthread-safety for Clang builds in
tools/perf/Makefile.config, replace the custom lock safety macros intools/perfwith the kernel'scompiler-context-analysis.hannotations (context_lock_struct,__guarded_by,__acquires,__acquires_shared,__releases,__releases_shared,__cond_acquires,__must_hold,__must_hold_shared,__must_not_hold,__returns_ctx_lock,__no_context_analysis, and__context_unsafe), and remove the redundant macro definitions fromtools/perf/util/mutex.h.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v12 5/20 perf build: Enable additional Clang thread-safety flags
In reply to Ian RogersAlign tools/perf's Clang thread-safety flags with the kernel's scripts/Makefile.context-analysis by adding -Wthread-safety-pointer, -Wthread-safety-beta, and (when supported by the compiler) -fexperimental-late-parse-attributes.
--- tools/perf/Makefile.config | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/Makefile.config +7 -1
@@ -64,7 +64,13 @@ ifeq ($(CC_NO_CLANG), 0) endif # Enabled Wthread-safety analysis for clang builds. - CFLAGS += -Wthread-safety -DWARN_CONTEXT_ANALYSIS + CFLAGS += -DWARN_CONTEXT_ANALYSIS -Wthread-safety + ifeq ($(shell $(CC) -Werror -Wthread-safety-pointer -Wthread-safety-beta -c -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo 1), 1) + CFLAGS += -Wthread-safety-pointer -Wthread-safety-beta + endif + ifeq ($(shell $(CC) -Werror -fexperimental-late-parse-attributes -c -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo 1), 1) + CFLAGS += -fexperimental-late-parse-attributes + endif endif include $(srctree)/tools/scripts/Makefile.arch
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 05/20] perf build: Enable additional Clang thread-safety flags
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Align tools/perf's Clang thread-safety flags with the kernel's
scripts/Makefile.context-analysisby adding -Wthread-safety-pointer,(quoted diff) +0
-2-Wthread-safety-beta, and (when supported by the compiler) -fexperimental-late-parse-attributes.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v12 6/20 perf comm: Add __guarded_by annotations to struct comm_strs
In reply to Ian RogersAnnotate strs, num_strs, and capacity in struct comm_strs with __guarded_by(&lock) and __pt_guarded_by(&lock) so Clang verifies all accesses hold comm_strs->lock.
--- tools/perf/util/comm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/comm.c +3 -3
@@ -16,9 +16,9 @@ DECLARE_RC_STRUCT(comm_str) {static struct comm_strs {struct rw_semaphore lock; -struct comm_str **strs; -int num_strs; -int capacity; + struct comm_str **strs __pt_guarded_by(&lock) __guarded_by(&lock); + int num_strs __guarded_by(&lock); + int capacity __guarded_by(&lock); } _comm_strs;static void comm_strs__remove_if_last (struct comm_str *cs);
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 06/20] perf comm: Add __guarded_by annotations to struct comm_strs
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Annotate strs,
num_strs, and capacity in structcomm_strswith__guarded_by(&lock)and__pt_guarded_by(&lock)so Clang verifies all accesses holdcomm_strs->lock.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH v12 7/20 perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso()
In reply to Ian Rogers__machine__addnew_vdso() called __dsos__add(&machine->dsos, dso) without holding machine->dsos.lock, and machine__findnew_vdso() and __machine__findnew_compat() initialized machine->vdso_info and created temporary VDSO files without synchronization. Acquire machine->dsos.lock in write mode across machine->vdso_info initialization, duplicate checking via __dsos__find(), temporary file creation, dso__set_long_name(), and __dsos__add().
--- tools/perf/util/dsos.c | 8 ++++++- tools/perf/util/dsos.h | 2 ++ tools/perf/util/vdso.c | 52 +++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 20 deletions(-)
tools/perf/util/dsos.c +7 -1
@@ -284,12 +284,18 @@ static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const streturn res; } +struct dso *__dsos__find (struct dsos *dsos,const char *name,bool cmp_short,bool write_locked) + __must_hold_shared(&dsos->lock) +{ +return __dsos__find_id (dsos, name, &dso_id_empty, cmp_short, write_locked); +} +struct dso *dsos__find (struct dsos *dsos,const char *name,bool cmp_short) {struct dso *res;down_read (&dsos->lock); - res =__dsos__find_id (dsos, name, &dso_id_empty, cmp_short,/*write_locked=*/ false); + res =__dsos__find (dsos, name, cmp_short,/*write_locked=*/ false);up_read (&dsos->lock);return res; }
tools/perf/util/dsos.h +2 -0
@@ -30,6 +30,8 @@ void dsos__exit(struct dsos *dsos);int __dsos__add (struct dsos *dsos,struct dso *dso);int dsos__add (struct dsos *dsos,struct dso *dso); +struct dso *__dsos__find (struct dsos *dsos,const char *name,bool cmp_short,bool write_locked) + __must_hold_shared(&dsos->lock);struct dso *dsos__find (struct dsos *dsos,const char *name,bool cmp_short);struct dso *dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id);
tools/perf/util/vdso.c +33 -19
89 lines not on this page · raw message
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 07/20] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso()
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
__machine__addnew_vdso()called__dsos__add(&machine->dsos, dso)without holdingmachine->dsos.lock, andmachine__findnew_vdso()and__machine__findnew_compat()initializedmachine->vdso_infoand created temporary VDSO files without synchronization. Acquiremachine->dsos.lockin write mode acrossmachine->vdso_infoinitialization, duplicate checking via__dsos__find(), temporary file creation,dso__set_long_name(), and__dsos__add().Fixes f649ed80f3ca ("perf dsos: Tidy reference counting and locking")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v12 8/20 perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso()
In reply to Ian RogersIn machine__process_ksymbol_register(), set the binary type of non-OOL ksymbol DSOs to DSO_BINARY_TYPE__BPF_IMAGE (which machine__process_bpf_event_load() upgrades to DSO_BINARY_TYPE__BPF_PROG_INFO on PERF_BPF_EVENT_PROG_LOAD), and update dsos__find_kernel_dso_cb() to skip DSO_BINARY_TYPE__BPF_PROG_INFO, DSO_BINARY_TYPE__BPF_IMAGE, and DSO_BINARY_TYPE__OOL so that dsos__find_kernel_dso() does not match ksymbol DSOs in machine->dsos.
--- tools/perf/util/dsos.c | 3 +++ tools/perf/util/machine.c | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-)
tools/perf/util/dsos.c +3 -0
@@ -475,6 +475,9 @@ static int dsos__find_kernel_dso_cb(struct dso *dso, void *data)* is_kernel_module() treats it as a kernel cpumode. */ if (!dso__kernel (dso) || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_PROG_INFO || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_IMAGE || +dso__binary_type (dso) ==DSO_BINARY_TYPE__OOL ||is_kernel_module (dso__long_name (dso),PERF_RECORD_MISC_CPUMODE_UNKNOWN ))return 0 ;
tools/perf/util/machine.c +4 -5
@@ -769,6 +769,10 @@ static int machine__process_ksymbol_register(struct machine *machine,dso__set_binary_type (dso,DSO_BINARY_TYPE__OOL );dso__data (dso)->file_size = event->ksymbol.len;dso__set_loaded (dso); + }else { +dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE ); +if (is_bpf_image (event->ksymbol.name)) +dso__set_long_name (dso,"" , false); }map__set_start (map, event->ksymbol.addr);
@@ -780,11 +784,6 @@ static int machine__process_ksymbol_register(struct machine *machine, }dso__set_loaded (dso); - -if (is_bpf_image (event->ksymbol.name)) { -dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE ); -dso__set_long_name (dso,"" , false); - } }else { dso =dso__get (map__dso (map)); }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 08/20] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso()
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
In
machine__process_ksymbol_register(), set the binary type of non-OOL ksymbol DSOs toDSO_BINARY_TYPE__BPF_IMAGE(whichmachine__process_bpf_event_load()upgrades toDSO_BINARY_TYPE__BPF_PROG_INFOonPERF_BPF_EVENT_PROG_LOAD), and updatedsos__find_kernel_dso_cb()to skipDSO_BINARY_TYPE__BPF_PROG_INFO,DSO_BINARY_TYPE__BPF_IMAGE, andDSO_BINARY_TYPE__OOLso thatdsos__find_kernel_dso()does not match ksymbol DSOs inmachine->dsos.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Namhyung Kim
maintainerRe: [PATCH v12 08/20] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso()
In reply to Ian RogersQuoted 33 lines · Ian Rogers
In
machine__process_ksymbol_register(), set the binary type of non-OOL ksymbol DSOs toDSO_BINARY_TYPE__BPF_IMAGE(whichmachine__process_bpf_event_load()upgrades toDSO_BINARY_TYPE__BPF_PROG_INFOonPERF_BPF_EVENT_PROG_LOAD), and updatedsos__find_kernel_dso_cb()to skipDSO_BINARY_TYPE__BPF_PROG_INFO,DSO_BINARY_TYPE__BPF_IMAGE, andDSO_BINARY_TYPE__OOLso thatdsos__find_kernel_dso()does not match ksymbol DSOs inmachine->dsos.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/dsos.c | 3 +++ tools/perf/util/machine.c | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-)tools/perf/util/dsos.c +3
-0@@ -475,6 +475,9 @@ static int dsos__find_kernel_dso_cb(struct dso *dso, void *data)* is_kernel_module() treats it as a kernel cpumode. */ if (!dso__kernel (dso) || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_PROG_INFO || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_IMAGE || +dso__binary_type (dso) ==DSO_BINARY_TYPE__OOL ||is_kernel_module (dso__long_name (dso),PERF_RECORD_MISC_CPUMODE_UNKNOWN ))return 0 ;tools/perf/util/machine.c +0
-0
tools/perf/util/machine.c static int machine__process_ksymbol_register(struct machine *machine, · line 773
Quoted 5 lines
tools/perf/util/machine.c +2
-0@@ -769,6 +769,10 @@ static int machine__process_ksymbol_register(struct machine *machine,dso__set_binary_type (dso,DSO_BINARY_TYPE__OOL );dso__data (dso)->file_size = event->ksymbol.len;dso__set_loaded (dso); + }else { +dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE );
Shouldn't this move into the if statement below? At least it should check if the ksym_type is BPF.
Thanks,
Namhyung
Quoted 20 lines
tools/perf/util/machine.c +2
-5+if (is_bpf_image (event->ksymbol.name)) +dso__set_long_name (dso,"" , false); }map__set_start (map, event->ksymbol.addr);@@ -780,11 +784,6 @@ static int machine__process_ksymbol_register(struct machine *machine, }dso__set_loaded (dso); - -if (is_bpf_image (event->ksymbol.name)) { -dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE ); -dso__set_long_name (dso,"" , false); - } }else { dso =dso__get (map__dso (map)); }Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerRe: [PATCH v12 08/20] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso()
Quoted 30 lines · Namhyung Kim
Quoted 28 lines · Ian Rogers
In
machine__process_ksymbol_register(), set the binary type of non-OOL ksymbol DSOs toDSO_BINARY_TYPE__BPF_IMAGE(whichmachine__process_bpf_event_load()upgrades toDSO_BINARY_TYPE__BPF_PROG_INFOonPERF_BPF_EVENT_PROG_LOAD), and updatedsos__find_kernel_dso_cb()to skipDSO_BINARY_TYPE__BPF_PROG_INFO,DSO_BINARY_TYPE__BPF_IMAGE, andDSO_BINARY_TYPE__OOLso thatdsos__find_kernel_dso()does not match ksymbol DSOs inmachine->dsos.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/dsos.c | 3 +++ tools/perf/util/machine.c | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-)tools/perf/util/dsos.c +3
-0@@ -475,6 +475,9 @@ static int dsos__find_kernel_dso_cb(struct dso *dso, void *data)* is_kernel_module() treats it as a kernel cpumode. */ if (!dso__kernel (dso) || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_PROG_INFO || +dso__binary_type (dso) ==DSO_BINARY_TYPE__BPF_IMAGE || +dso__binary_type (dso) ==DSO_BINARY_TYPE__OOL ||is_kernel_module (dso__long_name (dso),PERF_RECORD_MISC_CPUMODE_UNKNOWN ))return 0 ;
Quoted 13 lines
tools/perf/util/machine.c static int machine__process_ksymbol_register(struct machine *machine, · line 773
Quoted 10 lines
tools/perf/util/machine.c +2
-0@@ -769,6 +769,10 @@ static int machine__process_ksymbol_register(struct machine *machine,dso__set_binary_type (dso,DSO_BINARY_TYPE__OOL );dso__data (dso)->file_size = event->ksymbol.len;dso__set_loaded (dso); + }else { +dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE );Shouldn't this move into the if statement below? At least it should check if the
ksym_typeis BPF.
Good catch. Setting DSO_BINARY_TYPE__BPF_IMAGE unconditionally was done so dsos__find_kernel_dso_cb() would also skip bpf_prog_* ksymbol DSOs before PERF_RECORD_BPF_EVENT sets DSO_BINARY_TYPE__BPF_PROG_INFO (or when --no-bpf-event is used), but that mislabels BPF programs as BPF_IMAGE.
In v13, I've moved dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE) into an else if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_BPF && is_bpf_image(event->ksymbol.name)) block, and factored out dso__is_bpf_prog() from __map__is_bpf_prog() (which falls back to checking the "bpf_prog_" prefix) for dsos__find_kernel_dso_cb() to use.
Thanks,
Ian
Quoted 24 lines
Thanks,
NamhyungQuoted 20 lines
tools/perf/util/machine.c +2
-6+if (is_bpf_image (event->ksymbol.name)) +dso__set_long_name (dso,"" , false); }map__set_start (map, event->ksymbol.addr);@@ -780,11 +784,6 @@ static int machine__process_ksymbol_register(struct machine *machine, }dso__set_loaded (dso); - -if (is_bpf_image (event->ksymbol.name)) { -dso__set_binary_type (dso,DSO_BINARY_TYPE__BPF_IMAGE ); -dso__set_long_name (dso,"" , false); - } }else { dso =dso__get (map__dso (map)); } --2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerPATCH v12 9/20 perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos
In reply to Ian RogersPlace the DSOs created in addr_filter__resolve_syms(), machine__process_ksymbol_register(), and maps__split_kallsyms() into a dsos collection so that all DSOs that may have data fds opened or be referenced by maps belong to a dsos collection.
--- tools/perf/util/auxtrace.c | 16 +++++++++++++--- tools/perf/util/machine.c | 4 ++++ tools/perf/util/symbol.c | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-)
tools/perf/util/auxtrace.c +13 -3
@@ -29,6 +29,7 @@#include "config.h" #include "evlist.h" #include "dso.h" +#include "dsos.h" #include "map.h" #include "pmu.h" #include "evsel.h"
@@ -2684,7 +2685,7 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)return 0 ; } -static struct dso *load_dso (const char *name) +static struct dso *load_dso (const char *name,struct dsos *dsos) {struct map *map;struct dso *dso;
@@ -2693,6 +2694,11 @@ static struct dso *load_dso(const char *name)if (!map)return NULL ; +if (dsos__add (dsos,map__dso (map))) { +map__put (map); +return NULL ; + } +if (map__load (map) <0 )pr_err ("File '%s' not found or has no symbols.\n" , name);
@@ -2794,6 +2800,7 @@ static int addr_filter__resolve_syms(struct addr_filter *filt) {u64 start, size;struct dso *dso; +struct dsos dsos;int err =0 ;if (!filt->sym_from && !filt->sym_to)
@@ -2802,9 +2809,11 @@ static int addr_filter__resolve_syms(struct addr_filter *filt)if (!filt->filename)return addr_filter__resolve_kernel_syms (filt); - dso =load_dso (filt->filename); +dsos__init (&dsos); + dso =load_dso (filt->filename, &dsos);if (!dso) {pr_err ("Failed to load symbols from: %s\n" , filt->filename); +dsos__exit (&dsos);return -EINVAL ; }
@@ -2831,13 +2840,14 @@ static int addr_filter__resolve_syms(struct addr_filter *filt) err =check_end_after_start (filt, start, size);if (err) -return err; + goto put_dso; filt->size = start + size - filt->addr; } put_dso:dso__put (dso); +dsos__exit (&dsos);return err; }
tools/perf/util/machine.c +4 -0
@@ -775,6 +775,10 @@ static int machine__process_ksymbol_register(struct machine *machine,dso__set_long_name (dso,"" , false); } + err =dsos__add (&machine->dsos, dso); +if (err) + goto out; +map__set_start (map, event->ksymbol.addr);map__set_end (map,map__start (map) + event->ksymbol.len); err =maps__fixup_overlap_and_insert (machine__kernel_maps (machine), map);
tools/perf/util/symbol.c +4 -0
@@ -1090,6 +1090,10 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,dso__set_symtab_type (ndso,DSO_BINARY_TYPE__KALLSYMS ); }dso__set_loaded (ndso); +if (dsos__add (&machine->dsos, ndso)) { +dso__put (ndso); +return -1 ; + } curr_map =map__new2 (pos->start, ndso);if (curr_map ==NULL ) {
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 09/20] perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Place the DSOs created in
addr_filter__resolve_syms(),machine__process_ksymbol_register(), andmaps__split_kallsyms()into a dsos collection so that all DSOs that may have data fds opened or be referenced by maps belong to a dsos collection.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH v12 10/20 perf dso: Pass dso_data to close_data_fd() and dso__list_del()
In reply to Ian Rogersclose_data_fd() and dso__list_del() only operate on struct dso_data fields, while close_first_dso() retrieves a struct dso_data * from dso__data_open. Pass struct dso_data * directly to close_data_fd() and dso__list_del() so close_first_dso() does not need to recover a struct dso * from struct dso_data *.
This removes the REFCNT_CHECKING dso pointer from struct dso_data and avoids dropping _dso__data_open_lock in dso__list_del() to call dso__put(), preventing lock-order inversions and races during EMFILE eviction.
--- tools/perf/util/dso.c | 44 ++++++++++++------------------------------- tools/perf/util/dso.h | 3 --- 2 files changed, 12 insertions(+), 35 deletions(-)
tools/perf/util/dso.c +12 -32
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include "dso.h" +#include <assert.h> #include <errno.h> #include <stdlib.h>
@@ -544,22 +545,14 @@ static struct mutex *dso__data_open_lock(void) __returns_ctx_lock(&_dso__data_op static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lock) {list_add_tail (&dso__data (dso)->open_entry, &dso__data_open); -#ifdef REFCNT_CHECKING -dso__data (dso)->dso =dso__get (dso); -#endif -/* Assume the dso is part of dsos, hence the optional reference count above. */ +/* Assume the dso is part of dsos, holding it live while on dso__data_open. */ assert (dso__dsos (dso)); dso__data_open_cnt++; } -static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lock) +static void dso__list_del(struct dso_data *dso_data) __must_hold(&_dso__data_open_lock) { -list_del_init (&dso__data (dso)->open_entry); -#ifdef REFCNT_CHECKING -mutex_unlock (dso__data_open_lock ()); -dso__put (dso__data (dso)->dso); -mutex_lock (dso__data_open_lock ()); -#endif +list_del_init (&dso_data->open_entry);WARN_ONCE (dso__data_open_cnt <=0 ,"DSO data fd counter out of bounds." ); dso__data_open_cnt--;
@@ -729,13 +722,13 @@ static int open_dso(struct dso *dso, struct machine *machine)return fd; } -static void close_data_fd(struct dso *dso) __must_hold(&_dso__data_open_lock) +static void close_data_fd(struct dso_data *dso_data) __must_hold(&_dso__data_open_lock) { -if (dso__data (dso)->fd >=0 ) { -close (dso__data (dso)->fd); -dso__data (dso)->fd =-1 ; -dso__data (dso)->file_size =0 ; -dso__list_del (dso); +if (dso_data->fd >=0 ) { +close (dso_data->fd); + dso_data->fd =-1 ; + dso_data->file_size =0 ; +dso__list_del (dso_data); } }
@@ -748,21 +741,15 @@ static void close_data_fd(struct dso *dso) __must_hold(&_dso__data_open_lock)*/ static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) { -close_data_fd (dso); +close_data_fd (dso__data (dso)); } static void close_first_dso(void) __must_hold(&_dso__data_open_lock) {struct dso_data *dso_data; -struct dso *dso; dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry); -#ifdef REFCNT_CHECKING - dso = dso_data->dso; -#else - dso = container_of(dso_data, struct dso, data); -#endif -close_dso (dso); +close_data_fd (dso_data); }static rlim_t get_fd_limit (void )
@@ -1858,9 +1845,6 @@ struct dso *dso__new_id(const char *name, const struct dso_id *id) data->fd =-1 ; data->status =DSO_DATA_STATUS_UNKNOWN ;INIT_LIST_HEAD (&data->open_entry); -#ifdef REFCNT_CHECKING - data->dso =NULL ;/* Set when on the open_entry list. */ -#endif }return res; }
@@ -1918,10 +1902,6 @@ struct dso *dso__get(struct dso *dso)void dso__put (struct dso *dso) { -#ifdef REFCNT_CHECKING -if (dso &&dso__data (dso) &&refcount_read (&RC_CHK_ACCESS (dso)->refcnt) ==2 ) -dso__data_close (dso); -#endif if (dso &&refcount_dec_and_test (&RC_CHK_ACCESS (dso)->refcnt))dso__delete (dso);else
tools/perf/util/dso.h +0 -3
@@ -261,9 +261,6 @@ struct dso_cache {struct dso_data {struct rb_root cache;struct list_head open_entry; -#ifdef REFCNT_CHECKING -struct dso *dso; -#endif int fd;int status;u32 status_seen;
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 10/20] perf dso: Pass dso_data to close_data_fd() and dso__list_del()
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
close_data_fd()anddso__list_del()only operate on structdso_datafields, whileclose_first_dso()retrieves a structdso_data* fromdso__data_open. Pass structdso_data* directly toclose_data_fd()anddso__list_del()soclose_first_dso()does not need to recover a struct dso * from structdso_data*.
Quoted 6 lines
This removes the
REFCNT_CHECKINGdso pointer from structdso_dataand avoids dropping_dso__data_open_lockindso__list_del()to calldso__put(), preventing lock-order inversions and races during EMFILE eviction.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v12 11/20 perf dso: Use DEFINE_MUTEX for dso__data_open_lock
In reply to Ian RogersReplace _dso__data_open_lock and its pthread_once() initialization wrapper dso__data_open_lock() with DEFINE_MUTEX(dso__data_open_lock).
--- tools/perf/util/dso.c | 68 +++++++++++++++++-------------------------- tools/perf/util/dso.h | 10 +++---- 2 files changed, 32 insertions(+), 46 deletions(-)
tools/perf/util/dso.c +27 -41
@@ -524,25 +524,11 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m,/* * Global list of open DSOs and the counter. */ -struct mutex _dso__data_open_lock; +DEFINE_MUTEX (dso__data_open_lock);static LIST_HEAD (dso__data_open); -static long dso__data_open_cnt __guarded_by(&_dso__data_open_lock); +static long dso__data_open_cnt __guarded_by(&dso__data_open_lock); -static void dso__data_open_lock_init (void ) -{ -mutex_init (&_dso__data_open_lock); -} - -static struct mutex *dso__data_open_lock(void) __returns_ctx_lock(&_dso__data_open_lock) -{ -static pthread_once_t data_open_lock_once =PTHREAD_ONCE_INIT ; - -pthread_once (&data_open_lock_once, dso__data_open_lock_init); - -return &_dso__data_open_lock; -} - -static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lock) +static void dso__list_add(struct dso *dso) __must_hold(&dso__data_open_lock) {list_add_tail (&dso__data (dso)->open_entry, &dso__data_open);/* Assume the dso is part of dsos, holding it live while on dso__data_open. */
@@ -550,7 +536,7 @@ static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lock) dso__data_open_cnt++; } -static void dso__list_del(struct dso_data *dso_data) __must_hold(&_dso__data_open_lock) +static void dso__list_del(struct dso_data *dso_data) __must_hold(&dso__data_open_lock) {list_del_init (&dso_data->open_entry); WARN_ONCE(dso__data_open_cnt <= 0,
@@ -560,7 +546,7 @@ static void dso__list_del(struct dso_data *dso_data) __must_hold(&_dso__data_opestatic void close_first_dso (void ); -static int do_open(char *name) __must_hold(&_dso__data_open_lock) +static int do_open(char *name) __must_hold(&dso__data_open_lock) { do { int fd = open(name, O_RDONLY|O_CLOEXEC);
@@ -663,7 +649,7 @@ void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool deco }static int __open_dso (struct dso *dso,struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) {int fd = -EINVAL ;char *name;
@@ -696,7 +682,7 @@ static void check_data_close(void);* list/count of open DSO objects. */ static int open_dso (struct dso *dso,struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) {int fd;struct nscookie nsc;
@@ -722,7 +708,7 @@ static int open_dso(struct dso *dso, struct machine *machine)return fd; } -static void close_data_fd(struct dso_data *dso_data) __must_hold(&_dso__data_open_lock) +static void close_data_fd(struct dso_data *dso_data) __must_hold(&dso__data_open_lock) {if (dso_data->fd >=0 ) {close (dso_data->fd);
@@ -739,12 +725,12 @@ static void close_data_fd(struct dso_data *dso_data) __must_hold(&_dso__data_ope* Close @dso's data file descriptor and updates * list/count of open DSO objects. */ -static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) +static void close_dso(struct dso *dso) __must_hold(&dso__data_open_lock) {close_data_fd (dso__data (dso)); } -static void close_first_dso(void) __must_hold(&_dso__data_open_lock) +static void close_first_dso(void) __must_hold(&dso__data_open_lock) {struct dso_data *dso_data;
@@ -783,7 +769,7 @@ void reset_fd_limit(void) fd_limit =0 ; } -static bool may_cache_fd(void) __must_hold(&_dso__data_open_lock) +static bool may_cache_fd(void) __must_hold(&dso__data_open_lock) {if (!fd_limit) fd_limit =get_fd_limit ();
@@ -799,7 +785,7 @@ static bool may_cache_fd(void) __must_hold(&_dso__data_open_lock)* for opened dso file descriptors. The limit is half * of the RLIMIT_NOFILE files opened. */ -static void check_data_close(void) __must_hold(&_dso__data_open_lock) +static void check_data_close(void) __must_hold(&dso__data_open_lock) {bool cache_fd =may_cache_fd ();
@@ -815,9 +801,9 @@ static void check_data_close(void) __must_hold(&_dso__data_open_lock)*/ void dso__data_close (struct dso *dso) { -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock);close_dso (dso); -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock); }static int dso__find_binary_type_locked (struct dso *dso)
@@ -913,7 +899,7 @@ static int dso__find_binary_type_locked(struct dso *dso)#define FIND_MAX_RETRY 3static int __dso__find_binary_type (struct dso *dso) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) {int ret;int retry =0 ;
@@ -945,15 +931,15 @@ enum dso_binary_type dso__find_binary_type(struct dso *dso) {int ret; -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock); ret =__dso__find_binary_type (dso); -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock);return ret; }static void try_to_open_dso (struct dso *dso,struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) {struct dso_data *dso_data =dso__data (dso);
@@ -984,7 +970,7 @@ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd)if (dso__data (dso)->status ==DSO_DATA_STATUS_ERROR )return false; -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock);try_to_open_dso (dso, machine);
@@ -992,13 +978,13 @@ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd)if (*fd >=0 )return true; -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock);return false; }void dso__data_put_fd (struct dso *dso__maybe_unused ) { -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock); }bool dso__data_status_seen (struct dso *dso,enum dso_data_status_seen by)
@@ -1169,7 +1155,7 @@ static ssize_t file_read(struct dso *dso, struct machine *machine, {ssize_t ret; -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock);/* * dso__data(dso)->fd might be closed if other thread opened another
@@ -1185,7 +1171,7 @@ static ssize_t file_read(struct dso *dso, struct machine *machine, ret =pread (dso__data (dso)->fd, data,DSO__DATA_CACHE_SIZE , offset); out: -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock);return ret; }
@@ -1292,7 +1278,7 @@ static int file_size(struct dso *dso, struct machine *machine)int ret =0 ;struct stat st; -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock);/* * dso__data(dso)->fd might be closed if other thread opened another
@@ -1315,7 +1301,7 @@ static int file_size(struct dso *dso, struct machine *machine)dso__data (dso)->file_size = st.st_size; out: -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock);return ret; }
@@ -1520,7 +1506,7 @@ uint16_t dso__e_machine_endian(struct dso *dso, struct machine *machine, uint32_return EM_NONE ; } -mutex_lock (dso__data_open_lock ()); +mutex_lock (&dso__data_open_lock);/* * dso__data(dso)->fd might be closed if other thread opened another
@@ -1533,7 +1519,7 @@ uint16_t dso__e_machine_endian(struct dso *dso, struct machine *machine, uint32_else if (e_flags) *e_flags =0 ; -mutex_unlock (dso__data_open_lock ()); +mutex_unlock (&dso__data_open_lock);return e_machine; }
tools/perf/util/dso.h +5 -5
@@ -354,7 +354,7 @@ DECLARE_RC_STRUCT(dso) {char name[]; }; -extern struct mutex _dso__data_open_lock; +extern struct mutex dso__data_open_lock;extern const struct dso_id dso_id_empty;int dso_id__cmp (const struct dso_id *a,const struct dso_id *b);
@@ -807,7 +807,7 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id);int dso__name_len (const struct dso *dso);struct dso *dso__get (struct dso *dso); -void dso__put(struct dso *dso) __must_not_hold(&_dso__data_open_lock); +void dso__put(struct dso *dso) __must_not_hold(&dso__data_open_lock);static inline void __dso__zput (struct dso **dso) {
@@ -917,9 +917,9 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m,* TODO */ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) - __cond_acquires(true, &_dso__data_open_lock); -void dso__data_put_fd(struct dso *dso) __releases(&_dso__data_open_lock); -void dso__data_close(struct dso *dso) __must_not_hold(&_dso__data_open_lock); + __cond_acquires(true, &dso__data_open_lock); +void dso__data_put_fd(struct dso *dso) __releases(&dso__data_open_lock); +void dso__data_close(struct dso *dso) __must_not_hold(&dso__data_open_lock);int dso__data_file_size (struct dso *dso,struct machine *machine);off_t dso__data_size (struct dso *dso,struct machine *machine);
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 11/20] perf dso: Use DEFINE_MUTEX for dso__data_open_lock
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines
Replace
_dso__data_open_lockand itspthread_once()initialization wrapperdso__data_open_lock()withDEFINE_MUTEX(dso__data_open_lock).Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v12 12/20 perf dsos: Use read lock fast path in dsos__findnew_id()
In reply to Ian RogersIn dsos__findnew_id(), try finding the DSO under the read lock first and only acquire the write lock when a new DSO must be inserted or an existing DSO needs its ID updated (checked via dso__needs_improve_id()).
--- tools/perf/util/dso.c | 16 +++++++++++++++- tools/perf/util/dso.h | 1 + tools/perf/util/dsos.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +15 -1
@@ -1685,6 +1685,18 @@ const struct dso_id dso_id_empty = { } }; +bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id) +{ +const struct dso_id *dso_id =dso__id_const (dso); + +if (!id) +return false; + +return (id->mmap2_valid && !dso_id->mmap2_valid) || + (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) || + (build_id__is_defined (&id->build_id) && !build_id__is_defined (&dso_id->build_id)); +} +void __dso__improve_id (struct dso *dso,const struct dso_id *id) {struct dsos *dsos =dso__dsos (dso);
@@ -1692,6 +1704,8 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)bool changed = false;/* dsos write lock held by caller. */ +if (!id) +return ;if (id->mmap2_valid && !dso_id->mmap2_valid) { dso_id->maj = id->maj;
@@ -1715,7 +1729,7 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)int dso_id__cmp (const struct dso_id *a,const struct dso_id *b) { -if (a == &dso_id_empty || b == &dso_id_empty) { +if (!a || !b || a == &dso_id_empty || b == &dso_id_empty) {/* There is no valid data to compare so the comparison always returns identical. */ return 0 ; }
tools/perf/util/dso.h +1 -0
@@ -802,6 +802,7 @@ void dso__delete(struct dso *dso);int dso__cmp_id (struct dso *a,struct dso *b);void dso__set_short_name (struct dso *dso,const char *name,bool name_allocated);void dso__set_long_name (struct dso *dso,const char *name,bool name_allocated); +bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id);void __dso__improve_id (struct dso *dso,const struct dso_id *id);int dso__name_len (const struct dso *dso);
tools/perf/util/dsos.c +10 -0
@@ -349,6 +349,16 @@ static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, conststruct dso *dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) {struct dso *dso; + +down_read (&dsos->lock); + dso =__dsos__find_id (dsos, name, id, false,/*write_locked=*/ false); +if (dso && !dso__needs_improve_id (dso, id)) { +up_read (&dsos->lock); +return dso; + } +up_read (&dsos->lock); +dso__put (dso); +down_write (&dsos->lock); dso =__dsos__findnew_id (dsos, name, id);up_write (&dsos->lock);
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 12/20] perf dsos: Use read lock fast path in dsos__findnew_id()
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
In
dsos__findnew_id(), try finding the DSO under the read lock first and only acquire the write lock when a new DSO must be inserted or an existing DSO needs its ID updated (checked viadso__needs_improve_id()).Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Namhyung Kim
maintainerRe: [PATCH v12 12/20] perf dsos: Use read lock fast path in dsos__findnew_id()
In reply to Ian RogersQuoted 19 lines · Ian Rogers
In
dsos__findnew_id(), try finding the DSO under the read lock first and only acquire the write lock when a new DSO must be inserted or an existing DSO needs its ID updated (checked viadso__needs_improve_id()).Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/dso.c | 16 +++++++++++++++- tools/perf/util/dso.h | 1 + tools/perf/util/dsos.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-)tools/perf/util/dso.c +0
-0@@ -1685,6 +1685,18 @@ const struct dso_id dso_id_empty = { } };
tools/perf/util/dso.c const struct dso_id dso_id_empty = { · line 1697
Quoted 10 lines
tools/perf/util/dso.c +10
-0+bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id) +{ +const struct dso_id *dso_id =dso__id_const (dso); + +if (!id) +return false; + +return (id->mmap2_valid && !dso_id->mmap2_valid) || + (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) || + (build_id__is_defined (&id->build_id) && !build_id__is_defined (&dso_id->build_id));
Don't we prefer build-ID over inode? I'm curious if it needs to check existing build-ID is defined first and return true. Any chance it'd overwrite a build-ID with an inode?
Thanks,
Namhyung
Quoted 59 lines
tools/perf/util/dso.c +5
-1+} +void __dso__improve_id (struct dso *dso,const struct dso_id *id) {struct dsos *dsos =dso__dsos (dso);@@ -1692,6 +1704,8 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)bool changed = false;/* dsos write lock held by caller. */ +if (!id) +return ;if (id->mmap2_valid && !dso_id->mmap2_valid) { dso_id->maj = id->maj;@@ -1715,7 +1729,7 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)int dso_id__cmp (const struct dso_id *a,const struct dso_id *b) { -if (a == &dso_id_empty || b == &dso_id_empty) { +if (!a || !b || a == &dso_id_empty || b == &dso_id_empty) {/* There is no valid data to compare so the comparison always returns identical. */ return 0 ; }tools/perf/util/dso.h +1
-0@@ -802,6 +802,7 @@ void dso__delete(struct dso *dso);int dso__cmp_id (struct dso *a,struct dso *b);void dso__set_short_name (struct dso *dso,const char *name,bool name_allocated);void dso__set_long_name (struct dso *dso,const char *name,bool name_allocated); +bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id);void __dso__improve_id (struct dso *dso,const struct dso_id *id);int dso__name_len (const struct dso *dso);tools/perf/util/dsos.c +10
-0@@ -349,6 +349,16 @@ static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, conststruct dso *dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) {struct dso *dso; + +down_read (&dsos->lock); + dso =__dsos__find_id (dsos, name, id, false,/*write_locked=*/ false); +if (dso && !dso__needs_improve_id (dso, id)) { +up_read (&dsos->lock); +return dso; + } +up_read (&dsos->lock); +dso__put (dso); +down_write (&dsos->lock); dso =__dsos__findnew_id (dsos, name, id);up_write (&dsos->lock);Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerRe: [PATCH v12 12/20] perf dsos: Use read lock fast path in dsos__findnew_id()
Quoted 21 lines · Namhyung Kim
Quoted 19 lines · Ian Rogers
In
dsos__findnew_id(), try finding the DSO under the read lock first and only acquire the write lock when a new DSO must be inserted or an existing DSO needs its ID updated (checked viadso__needs_improve_id()).Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/dso.c | 16 +++++++++++++++- tools/perf/util/dso.h | 1 + tools/perf/util/dsos.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-)tools/perf/util/dso.c +0
-0@@ -1685,6 +1685,18 @@ const struct dso_id dso_id_empty = { } };
Quoted 14 lines
tools/perf/util/dso.c const struct dso_id dso_id_empty = { · line 1697
Quoted 10 lines
tools/perf/util/dso.c +10
-0+bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id) +{ +const struct dso_id *dso_id =dso__id_const (dso); + +if (!id) +return false; + +return (id->mmap2_valid && !dso_id->mmap2_valid) || + (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) || + (build_id__is_defined (&id->build_id) && !build_id__is_defined (&dso_id->build_id));Don't we prefer build-ID over inode? I'm curious if it needs to check existing build-ID is defined first and return true. Any chance it'd overwrite a build-ID with an inode?
struct dso_id keeps the maj/min/ino fields and build_id as separate members rather than a union (unlike perf_record_mmap2), so build_id wouldn't be overwritten in memory, though setting mmap2_valid did take precedence in __dso_id__cmp() and perf report --mmaps.
Agreed we should prioritize build-ID over inode. In v13, I've moved the build-ID checks ahead of the inode checks in __dso_id__cmp(), dso__needs_improve_id(), and __dso__improve_id(), returning early when the existing DSO already has a defined build-ID so inode metadata isn't added on top of it.
Thanks,
Ian
Quoted 63 lines
Thanks,
NamhyungQuoted 59 lines
tools/perf/util/dso.c +5
-1+} +void __dso__improve_id (struct dso *dso,const struct dso_id *id) {struct dsos *dsos =dso__dsos (dso);@@ -1692,6 +1704,8 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)bool changed = false;/* dsos write lock held by caller. */ +if (!id) +return ;if (id->mmap2_valid && !dso_id->mmap2_valid) { dso_id->maj = id->maj;@@ -1715,7 +1729,7 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id)int dso_id__cmp (const struct dso_id *a,const struct dso_id *b) { -if (a == &dso_id_empty || b == &dso_id_empty) { +if (!a || !b || a == &dso_id_empty || b == &dso_id_empty) {/* There is no valid data to compare so the comparison always returns identical. */ return 0 ; }tools/perf/util/dso.h +1
-0@@ -802,6 +802,7 @@ void dso__delete(struct dso *dso);int dso__cmp_id (struct dso *a,struct dso *b);void dso__set_short_name (struct dso *dso,const char *name,bool name_allocated);void dso__set_long_name (struct dso *dso,const char *name,bool name_allocated); +bool dso__needs_improve_id (const struct dso *dso,const struct dso_id *id);void __dso__improve_id (struct dso *dso,const struct dso_id *id);int dso__name_len (const struct dso *dso);tools/perf/util/dsos.c +10
-1@@ -349,6 +349,16 @@ static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, conststruct dso *dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) {struct dso *dso; + +down_read (&dsos->lock); + dso =__dsos__find_id (dsos, name, id, false,/*write_locked=*/ false); +if (dso && !dso__needs_improve_id (dso, id)) { +up_read (&dsos->lock); +return dso; + } +up_read (&dsos->lock); +dso__put (dso); +down_write (&dsos->lock); dso =__dsos__findnew_id (dsos, name, id);up_write (&dsos->lock); --2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerPATCH v12 13/20 perf dso: Expand context analysis annotations
In reply to Ian RogersAnnotate struct dsos fields with __guarded_by(&lock), annotate dso__data_open and fd_limit in util/dso.c with __guarded_by(&dso__data_open_lock), and tighten __dsos__findnew_id() and __dsos__addnew_id() from shared to exclusive lock requirements.
--- tools/perf/tests/dso-data.c | 4 ++++ tools/perf/util/dso.c | 14 +++++++++----- tools/perf/util/dsos.c | 9 +++++++-- tools/perf/util/dsos.h | 10 +++++----- tools/perf/util/vdso.c | 1 + 5 files changed, 26 insertions(+), 12 deletions(-)
tools/perf/tests/dso-data.c +4 -0
@@ -116,12 +116,14 @@ static int dso__data_fd(struct dso *dso, struct machine *machine)static void dsos__delete (struct dsos *dsos) { +down_write (&dsos->lock);for (unsigned int i =0 ; i < dsos->cnt; i++) {struct dso *dso = dsos->dsos[i];dso__data_close (dso);unlink (dso__name (dso)); } +up_write (&dsos->lock);dsos__exit (dsos); }
@@ -254,6 +256,7 @@ static int set_fd_limit(int n) }static int test__dso_data_cache (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) +__context_unsafe (/* single-threaded test */ ) {struct machine machine;long nr_end, nr =open_files_cnt ();
@@ -324,6 +327,7 @@ static long new_limit(int count) }static int test__dso_data_reopen (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) +__context_unsafe (/* single-threaded test */ ) {struct machine machine;long nr_end, nr =open_files_cnt (), lim =new_limit (3 );
tools/perf/util/dso.c +9 -5
@@ -525,7 +525,8 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m,* Global list of open DSOs and the counter. */ DEFINE_MUTEX (dso__data_open_lock); -static LIST_HEAD (dso__data_open); +static struct list_head dso__data_open __guarded_by(&dso__data_open_lock) = +LIST_HEAD_INIT (dso__data_open); static long dso__data_open_cnt __guarded_by(&dso__data_open_lock); static void dso__list_add(struct dso *dso) __must_hold(&dso__data_open_lock)
@@ -757,7 +758,7 @@ static rlim_t get_fd_limit(void)return limit; } -static rlim_t fd_limit; +static rlim_t fd_limit __guarded_by(&dso__data_open_lock);/* * Used only by tests/dso-data.c to reset the environment
@@ -766,7 +767,8 @@ static rlim_t fd_limit;*/ void reset_fd_limit (void ) { - fd_limit =0 ; +/* Only called by single-threaded test setup. */ +context_unsafe (fd_limit =0 ); } static bool may_cache_fd(void) __must_hold(&dso__data_open_lock)
@@ -1723,8 +1725,10 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id) dso_id->build_id = id->build_id; changed = true; } -if (changed && dsos) - dsos->sorted = false; +if (changed && dsos) { +/* dsos write lock held by caller. */ +context_unsafe (dsos->sorted = false); + } }int dso_id__cmp (const struct dso_id *a,const struct dso_id *b)
tools/perf/util/dsos.c +7 -2
@@ -12,6 +12,7 @@#include <unistd.h> void dsos__init (struct dsos *dsos) +__context_unsafe (/* initialization */ ) {init_rwsem (&dsos->lock);
@@ -50,6 +51,7 @@ void dsos__exit(struct dsos *dsos)static int __dsos__for_each_dso (struct dsos *dsos,int (*cb)(struct dso *dso,void *data),void *data) + __must_hold_shared(&dsos->lock) {for (unsigned int i =0 ; i < dsos->cnt; i++) {struct dso *dso = dsos->dsos[i];
@@ -182,7 +184,8 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, }qsort (dsos->dsos, dsos->cnt,sizeof (struct dso *), dsos__cmp_long_name_id_short_name); - dsos->sorted = true; +/* write_locked is true, so dsos->lock is held exclusively. */ +context_unsafe (dsos->sorted = true); } res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *),
@@ -194,6 +197,7 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, }int __dsos__add (struct dsos *dsos,struct dso *dso) + __must_hold(&dsos->lock) {if (!dso)return -EINVAL ;
@@ -320,6 +324,7 @@ static void dso__set_basename(struct dso *dso) }static struct dso *__dsos__addnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) + __must_hold(&dsos->lock) {struct dso *dso =dso__new_id (name, id);
@@ -336,7 +341,7 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const }static struct dso *__dsos__findnew_id (struct dsos *dsos,const char *name,const struct dso_id *id) - __must_hold_shared(&dsos->lock) + __must_hold(&dsos->lock) {struct dso *dso =__dsos__find_id (dsos, name, id, false,/*write_locked=*/ true);
tools/perf/util/dsos.h +5 -5
@@ -19,16 +19,16 @@ struct machine;*/ struct dsos {struct rw_semaphore lock; -struct dso **dsos; -unsigned int cnt; -unsigned int allocated; -bool sorted; + struct dso **dsos __pt_guarded_by(&lock) __guarded_by(&lock); + unsigned int cnt __guarded_by(&lock); + unsigned int allocated __guarded_by(&lock); + bool sorted __guarded_by(&lock); };void dsos__init (struct dsos *dsos);void dsos__exit (struct dsos *dsos); -int __dsos__add (struct dsos *dsos,struct dso *dso); +int __dsos__add(struct dsos *dsos, struct dso *dso) __must_hold(&dsos->lock);int dsos__add (struct dsos *dsos,struct dso *dso);struct dso *__dsos__find (struct dsos *dsos,const char *name,bool cmp_short,bool write_locked) __must_hold_shared(&dsos->lock);
tools/perf/util/vdso.c +1 -0
@@ -126,6 +126,7 @@ void machine__exit_vdso(struct machine *machine)static struct dso *__machine__addnew_vdso (struct machine *machine,const char *short_name,const char *long_name) + __must_hold(&machine->dsos.lock) {struct dso *dso;
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 13/20] perf dso: Expand context analysis annotations
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Annotate struct dsos fields with
__guarded_by(&lock), annotatedso__data_openandfd_limitinutil/dso.cwith__guarded_by(&dso__data_open_lock), and tighten__dsos__findnew_id()and__dsos__addnew_id()from shared to exclusive lock requirements.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Ian Rogers
reviewerPATCH v12 14/20 perf thread: Expand context analysis annotations to threads and namespaces_lock
In reply to Ian RogersAnnotate shard and last_match in struct threads_table_entry with __guarded_by(&lock) and add __must_hold_shared()/__must_hold() to the last_match helpers in util/threads.c.
In util/thread.h and util/thread.c, add __returns_ctx_lock() to thread__namespaces_lock() and thread__comm_lock(), annotate namespaces_list and comm_list with __guarded_by(), and add __must_hold_shared()/__must_hold() to the namespaces_list helpers.
--- tools/perf/util/thread.c | 2 ++ tools/perf/util/thread.h | 28 +++++++++++++++++----------- tools/perf/util/threads.c | 4 ++++ tools/perf/util/threads.h | 6 +++--- 4 files changed, 26 insertions(+), 14 deletions(-)
tools/perf/util/thread.c +2 -0
@@ -149,6 +149,7 @@ void thread__put(struct thread *thread) }static struct namespaces *__thread__namespaces (struct thread *thread) +__must_hold_shared (thread__namespaces_lock (thread)) {if (list_empty (thread__namespaces_list (thread)))return NULL ;
@@ -169,6 +170,7 @@ struct namespaces *thread__namespaces(struct thread *thread)static int __thread__set_namespaces (struct thread *thread,u64 timestamp,struct perf_record_namespaces *event) +__must_hold (thread__namespaces_lock (thread)) {struct namespaces *new, *curr =__thread__namespaces (thread);
tools/perf/util/thread.h +17 -11
@@ -48,10 +48,10 @@ DECLARE_RC_STRUCT(thread) {bool exited;bool comm_set;int comm_len; -struct list_head namespaces_list;struct rw_semaphore namespaces_lock; -struct list_head comm_list; + struct list_head namespaces_list __guarded_by(&namespaces_lock);struct rw_semaphore comm_lock; + struct list_head comm_list __guarded_by(&comm_lock);u64 db_id;void *priv;
@@ -230,30 +230,36 @@ static inline void thread__set_comm_len(struct thread *thread, int len)RC_CHK_ACCESS (thread)->comm_len = len; } -static inline struct list_head *thread__namespaces_list (struct thread *thread) +static inline struct rw_semaphore *thread__namespaces_lock (const struct thread *thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->namespaces_lock) { -return &RC_CHK_ACCESS (thread)->namespaces_list; +return (struct rw_semaphore *)&RC_CHK_ACCESS (thread)->namespaces_lock; } -static inline int thread__namespaces_list_empty (const struct thread *thread) +static inline struct list_head *thread__namespaces_list (struct thread *thread) +__must_hold_shared (thread__namespaces_lock (thread)) { -return list_empty (&RC_CHK_ACCESS (thread)->namespaces_list); +/* Readers hold the shared lock; writers hold the exclusive lock. */ +return context_unsafe (&RC_CHK_ACCESS (thread)->namespaces_list); } -static inline struct rw_semaphore *thread__namespaces_lock (struct thread *thread) +static inline int thread__namespaces_list_empty (const struct thread *thread) +__must_hold_shared (thread__namespaces_lock (thread)) { -return &RC_CHK_ACCESS (thread)->namespaces_lock; +return list_empty (&RC_CHK_ACCESS (thread)->namespaces_list); } -static inline struct rw_semaphore *thread__comm_lock (struct thread *thread) +static inline struct rw_semaphore *thread__comm_lock (const struct thread *thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->comm_lock) { -return &RC_CHK_ACCESS (thread)->comm_lock; +return (struct rw_semaphore *)&RC_CHK_ACCESS (thread)->comm_lock; }static inline struct list_head *thread__comm_list (struct thread *thread)__must_hold_shared (thread__comm_lock (thread)) { -return &RC_CHK_ACCESS (thread)->comm_list; +/* Readers hold the shared lock; writers hold the exclusive lock. */ +return context_unsafe (&RC_CHK_ACCESS (thread)->comm_list); }static inline u64 thread__db_id (const struct thread *thread)
tools/perf/util/threads.c +4 -0
@@ -21,6 +21,7 @@ static bool key_equal(long key1, long key2, void *ctx __maybe_unused) }void threads__init (struct threads *threads) +__context_unsafe (/* initialization */ ) {for (int i =0 ; i <THREADS__TABLE_SIZE ; i++) {struct threads_table_entry *table = &threads->table[i];
@@ -32,6 +33,7 @@ void threads__init(struct threads *threads) }void threads__exit (struct threads *threads) +__context_unsafe (/* teardown */ ) {threads__remove_all_threads (threads);for (int i =0 ; i <THREADS__TABLE_SIZE ; i++) {
@@ -63,6 +65,7 @@ size_t threads__nr(struct threads *threads)*/ static struct thread *__threads_table_entry__get_last_match (struct threads_table_entry *table,pid_t tid) + __must_hold_shared(&table->lock) {struct thread *th, *res =NULL ;
@@ -76,6 +79,7 @@ static struct thread *__threads_table_entry__get_last_match(struct threads_tablestatic void __threads_table_entry__set_last_match (struct threads_table_entry *table,struct thread *th) + __must_hold(&table->lock) {thread__put (table->last_match); table->last_match =thread__get (th);
tools/perf/util/threads.h +3 -3
@@ -11,10 +11,10 @@ struct thread;#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS)struct threads_table_entry { -/* Key is tid, value is struct thread. */ -struct hashmap shard;struct rw_semaphore lock; -struct thread *last_match; +/* Key is tid, value is struct thread. */ + struct hashmap shard __guarded_by(&lock); + struct thread *last_match __guarded_by(&lock); };struct threads {
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 14/20] perf thread: Expand context analysis annotations to threads and namespaces_lock
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Annotate shard and
last_matchin structthreads_table_entrywith__guarded_by(&lock)and add __must_hold_shared()/__must_hold() to thelast_matchhelpers inutil/threads.c.In
util/thread.handutil/thread.c, add__returns_ctx_lock()tothread__namespaces_lock()andthread__comm_lock(), annotatenamespaces_listandcomm_listwith__guarded_by(), and add __must_hold_shared()/__must_hold() to thenamespaces_listhelpers.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Ian Rogers
reviewerPATCH v12 15/20 perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
In reply to Ian RogersIn tpebs_stop(), read tpebs_reader_thread into a local variable before releasing tpebs_mtx to call pthread_join().
Transfer ownership of tpebs_cmd.out to __sample_reader() by passing it as the thread argument and setting tpebs_cmd.out = -1 in evsel__tpebs_open() so tpebs_stop() does not double-close the fd closed by perf_session__delete(). Also reset tpebs_cmd.pid = 0 and tpebs_cmd.out = -1 and return early if start_command() fails in evsel__tpebs_start_perf_record(), as start_command() closes the pipe internally and may leave cmd->pid = -1 on error.
Serialize tpebs_send_record_cmd() with teardown via tpebs_cmd_in_flight and tpebs_cmd_cond, newline-terminate control commands so consecutive writes cannot coalesce in evlist__ctlfd_recv(), check check_if_command_finished(&tpebs_cmd) under tpebs_mtx only when not stopping, ignore late samples in process_sample_event() once tpebs_stopping is set, and guard evsel__tpebs_open() against a concurrent tpebs_stop() with tpebs_gen, tpebs_enabled, and tpebs_stopping across closing the fds and finish_command().
Include <pthread.h>, <signal.h>, <stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> explicitly.
--- tools/perf/util/intel-tpebs.c | 141 ++++++++++++++++++++++++++++------ 1 file changed, 119 insertions(+), 22 deletions(-)
tools/perf/util/intel-tpebs.c +119 -22
@@ -27,6 +27,12 @@#include <errno.h> #include <poll.h> #include <math.h> +#include <pthread.h> +#include <signal.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> #define PERF_DATA "-"
@@ -37,7 +43,11 @@ static pthread_t tpebs_reader_thread;static struct child_process tpebs_cmd;static int control_fd[2 ], ack_fd[2 ];static struct mutex tpebs_mtx; +static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER };static bool tpebs_stopping; +static bool tpebs_enabled; +static bool tpebs_cmd_in_flight; +static uint64_t tpebs_gen;struct tpebs_retire_lat {struct list_head nd;
@@ -120,10 +130,15 @@ static int evsel__tpebs_start_perf_record(struct evsel *evsel) tpebs_cmd.out =-1 ; ret =start_command (&tpebs_cmd);zfree (&tpebs_cmd.argv); +if (ret) { + tpebs_cmd.pid =0 ; + tpebs_cmd.out =-1 ; +return ret; + }list_for_each_entry (t, &tpebs_results, nd) t->started = true; -return ret; +return 0 ; }static bool is_child_pid (pid_t parent,pid_t child)
@@ -195,8 +210,8 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,struct tpebs_retire_lat *t;mutex_lock (tpebs_mtx_get ()); -if (tpebs_cmd.pid ==0 ) { -/* Record has terminated. */ +if (tpebs_stopping || tpebs_cmd.pid ==0 ) { +/* Record is stopping or has terminated. */ mutex_unlock (tpebs_mtx_get ());return 0 ; }
@@ -220,13 +235,13 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,return 0 ; } -static void *__sample_reader (void *arg__maybe_unused ) +static void *__sample_reader (void *arg) {struct perf_session *session;struct perf_data data = { .mode =PERF_DATA_MODE_READ , .path =PERF_DATA , - .file.fd = tpebs_cmd.out, + .file.fd = (int )(intptr_t )arg, };struct perf_tool tool;
@@ -236,8 +251,10 @@ static void *__sample_reader(void *arg __maybe_unused) tool.attr = perf_event__process_attr; session =perf_session__new (&data, &tool); -if (IS_ERR (session)) +if (IS_ERR (session)) { +close (data.file.fd);return NULL ; + }perf_session__process_events (session);perf_session__delete (session);
@@ -247,35 +264,49 @@ static void *__sample_reader(void *arg __maybe_unused)static int tpebs_send_record_cmd (const char *msg)__must_hold (tpebs_mtx_get ()) {struct pollfd pollfd = { .events =POLLIN , }; -int ret, len, retries =0 ; +bool is_stop = !strcmp (msg,EVLIST_CTL_CMD_STOP_TAG ); +int ret, len, retries =0 , ctrl_fd, resp_fd; +char cmd_buf[EVLIST_CTL_CMD_MAX_LEN ];char ack_buf[8 ]; +while (tpebs_cmd_in_flight) { +if (!is_stop && tpebs_stopping) +return -EBUSY ; +cond_wait (&tpebs_cmd_cond,tpebs_mtx_get ()); + } +if (!is_stop && tpebs_stopping) +return -EBUSY ; +/* Check if the command exited before the send, done with the lock held. */ if (tpebs_cmd.pid ==0 )return 0 ; + ctrl_fd = control_fd[1 ]; + resp_fd = ack_fd[0 ]; + tpebs_cmd_in_flight = true; +/* * Let go of the lock while sending/receiving as blocking can starve the * sample reading thread. */ mutex_unlock (tpebs_mtx_get ()); -/* Send perf record command.*/ - len =strlen (msg); - ret =write (control_fd[1 ], msg, len); +/* Send perf record command. */ + len =scnprintf (cmd_buf,sizeof (cmd_buf),"%s\n" , msg); + ret =write (ctrl_fd, cmd_buf, len);if (ret != len) {pr_err ("perf record control write control message '%s' failed\n" , msg); ret = -EPIPE ; goto out; } -if (!strcmp (msg,EVLIST_CTL_CMD_STOP_TAG )) { +if (is_stop) { ret =0 ; goto out; }/* Wait for an ack. */ - pollfd.fd = ack_fd[0 ]; + pollfd.fd = resp_fd;/* * We need this poll to ensure the ack_fd PIPE will not hang
@@ -284,7 +315,17 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())*/ again:if (!poll (&pollfd,1 ,500 )) { -if (check_if_command_finished (&tpebs_cmd)) { +bool stopping, finished; + +mutex_lock (tpebs_mtx_get ()); + stopping = tpebs_stopping; + finished = !stopping &&check_if_command_finished (&tpebs_cmd); +mutex_unlock (tpebs_mtx_get ()); +if (stopping) { + ret = -EBUSY ; + goto out; + } +if (finished) { ret =0 ; goto out; }
@@ -297,7 +338,17 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get()) }if (!(pollfd.revents &POLLIN )) { -if (check_if_command_finished (&tpebs_cmd)) { +bool stopping, finished; + +mutex_lock (tpebs_mtx_get ()); + stopping = tpebs_stopping; + finished = !stopping &&check_if_command_finished (&tpebs_cmd); +mutex_unlock (tpebs_mtx_get ()); +if (stopping) { + ret = -EBUSY ; + goto out; + } +if (finished) { ret =0 ; goto out; }
@@ -307,7 +358,7 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get()) goto out; } - ret =read (ack_fd[0 ], ack_buf,sizeof (ack_buf)); + ret =read (resp_fd, ack_buf,sizeof (ack_buf));if (ret >0 ) ret =strcmp (ack_buf,EVLIST_CTL_CMD_ACK_TAG );else
@@ -315,6 +366,8 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get()) out:/* Re-take lock as expected by caller. */ mutex_lock (tpebs_mtx_get ()); + tpebs_cmd_in_flight = false; +cond_broadcast (&tpebs_cmd_cond);return ret; }
@@ -331,12 +384,23 @@ static int tpebs_stop(void) __must_hold(tpebs_mtx_get())/* Like tpebs_start, we should only run tpebs_end once. */ if (tpebs_cmd.pid !=0 ) {pid_t actual_pid = tpebs_cmd.pid; +bool enabled = tpebs_enabled; +pthread_t reader_thread; tpebs_stopping = true; -tpebs_send_record_cmd (EVLIST_CTL_CMD_STOP_TAG ); + tpebs_enabled = false; + tpebs_gen++; +cond_broadcast (&tpebs_cmd_cond); +if (tpebs_send_record_cmd (EVLIST_CTL_CMD_STOP_TAG ) || !enabled) +kill (actual_pid,SIGTERM ); +/* +* tpebs_results is empty when stopping; clear pid before +* releasing tpebs_mtx to join the reader thread. +*/ tpebs_cmd.pid =0 ; + reader_thread = tpebs_reader_thread;mutex_unlock (tpebs_mtx_get ()); -pthread_join (tpebs_reader_thread,NULL ); +pthread_join (reader_thread,NULL );mutex_lock (tpebs_mtx_get ());if (control_fd[0 ] >=0 ) {close (control_fd[0 ]);
@@ -515,6 +579,8 @@ int evsel__tpebs_open(struct evsel *evsel)int ret;bool tpebs_empty;bool started_process = false; +bool started_thread = false; +uint64_t open_gen =0 ;/* We should only run tpebs_start when tpebs_recording is enabled. */ if (!tpebs_recording)
@@ -550,6 +616,8 @@ int evsel__tpebs_open(struct evsel *evsel) tpebs_empty =list_empty (&tpebs_results);if (!tpebs_empty) { started_process = true; + tpebs_enabled = false; + open_gen = ++tpebs_gen;/*Create control and ack fd for --control*/ if (pipe (control_fd) <0 ) {pr_err ("tpebs: Failed to create control fifo" );
@@ -567,13 +635,22 @@ int evsel__tpebs_open(struct evsel *evsel) goto out;if (pthread_create (&tpebs_reader_thread,/*attr=*/ NULL , __sample_reader, -/*arg=*/ NULL )) { + (void *)(intptr_t )tpebs_cmd.out)) {kill (tpebs_cmd.pid,SIGTERM );pr_err ("Could not create thread to process sample data.\n" ); ret =-1 ; goto out; } +/* __sample_reader now owns and closes the output fd. */ + tpebs_cmd.out =-1 ; + started_thread = true; ret =tpebs_send_record_cmd (EVLIST_CTL_CMD_ENABLE_TAG ); +if (!ret) { +if (tpebs_gen != open_gen) + ret = -EBUSY ; +else + tpebs_enabled = true; + } } out:if (ret) {
@@ -584,11 +661,25 @@ int evsel__tpebs_open(struct evsel *evsel)tpebs_retire_lat__delete (t); } -if (started_process) { -if (tpebs_cmd.pid >0 ) { -kill (tpebs_cmd.pid,SIGTERM ); -finish_command (&tpebs_cmd); +if (started_process && tpebs_gen == open_gen) { +pid_t actual_pid = tpebs_cmd.pid; + + tpebs_stopping = true; + tpebs_enabled = false; + tpebs_gen++; +cond_broadcast (&tpebs_cmd_cond); +if (actual_pid >0 ) { +kill (actual_pid,SIGTERM ); +while (tpebs_cmd_in_flight) +cond_wait (&tpebs_cmd_cond,tpebs_mtx_get ()); tpebs_cmd.pid =0 ; +if (started_thread) { +pthread_t reader_thread = tpebs_reader_thread; + +mutex_unlock (tpebs_mtx_get ()); +pthread_join (reader_thread,NULL ); +mutex_lock (tpebs_mtx_get ()); + } }if (tpebs_cmd.out >=0 ) {close (tpebs_cmd.out);
@@ -610,6 +701,12 @@ int evsel__tpebs_open(struct evsel *evsel)close (ack_fd[1 ]); ack_fd[1 ] =-1 ; } +if (actual_pid >0 ) { + tpebs_cmd.pid = actual_pid; +finish_command (&tpebs_cmd); + tpebs_cmd.pid =0 ; + } + tpebs_stopping = false; } }mutex_unlock (tpebs_mtx_get ());
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 15/20] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines
In
tpebs_stop(), readtpebs_reader_threadinto a local variable before releasingtpebs_mtxto callpthread_join().Transfer ownership of tpebs_cmd.out to
__sample_reader()by passing it as the thread argument and setting tpebs_cmd.out = -1 inevsel__tpebs_open()sotpebs_stop()does not double-close the fd closed byperf_session__delete(). Also reset tpebs_cmd.pid = 0 and tpebs_cmd.out = -1 and return early ifstart_command()fails inevsel__tpebs_start_perf_record(), asstart_command()closes the pipe internally and may leavecmd->pid= -1 on error.Serialize
tpebs_send_record_cmd()with teardown viatpebs_cmd_in_flightandtpebs_cmd_cond, newline-terminate control commands so consecutive writes cannot coalesce inevlist__ctlfd_recv(), checkcheck_if_command_finished(&tpebs_cmd)undertpebs_mtxonly when not stopping, ignore late samples inprocess_sample_event()oncetpebs_stoppingis set, and guardevsel__tpebs_open()against a concurrenttpebs_stop()withtpebs_gen,tpebs_enabled, andtpebs_stoppingacross closing the fds andfinish_command().
Quoted 6 lines
Include <
pthread.h>, <signal.h>, <stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> explicitly.Fixes 8db5cabcf1b6 ("perf stat: Fork and launch 'perf record' when 'perf stat' needs to get retire latency value for a metric.")Fixes bded0398ac19 ("perf tpebs: Fix concurrent stop races and PID reuse hazards in tpebs_stop")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Namhyung Kim
maintainerRe: [PATCH v12 15/20] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
In reply to Ian RogersQuoted 20 lines · Ian Rogers
In
tpebs_stop(), readtpebs_reader_threadinto a local variable before releasingtpebs_mtxto callpthread_join().Transfer ownership of tpebs_cmd.out to
__sample_reader()by passing it as the thread argument and setting tpebs_cmd.out = -1 inevsel__tpebs_open()sotpebs_stop()does not double-close the fd closed byperf_session__delete(). Also reset tpebs_cmd.pid = 0 and tpebs_cmd.out = -1 and return early ifstart_command()fails inevsel__tpebs_start_perf_record(), asstart_command()closes the pipe internally and may leavecmd->pid= -1 on error.Serialize
tpebs_send_record_cmd()with teardown viatpebs_cmd_in_flightandtpebs_cmd_cond, newline-terminate control commands so consecutive writes cannot coalesce inevlist__ctlfd_recv(), checkcheck_if_command_finished(&tpebs_cmd)undertpebs_mtxonly when not stopping, ignore late samples inprocess_sample_event()oncetpebs_stoppingis set, and guardevsel__tpebs_open()against a concurrenttpebs_stop()withtpebs_gen,tpebs_enabled, andtpebs_stoppingacross closing the fds andfinish_command().
Quoted 2 lines
Include <
pthread.h>, <signal.h>, <stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> explicitly.
I feel it's too verbose and not clear what's problem. Please explain the race and how it fixes the issue concisely.
Thanks,
Namhyung
Ian Rogers
reviewerRe: [PATCH v12 15/20] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
Quoted 22 lines · Namhyung Kim
Quoted 20 lines · Ian Rogers
In
tpebs_stop(), readtpebs_reader_threadinto a local variable before releasingtpebs_mtxto callpthread_join().Transfer ownership of tpebs_cmd.out to
__sample_reader()by passing it as the thread argument and setting tpebs_cmd.out = -1 inevsel__tpebs_open()sotpebs_stop()does not double-close the fd closed byperf_session__delete(). Also reset tpebs_cmd.pid = 0 and tpebs_cmd.out = -1 and return early ifstart_command()fails inevsel__tpebs_start_perf_record(), asstart_command()closes the pipe internally and may leavecmd->pid= -1 on error.Serialize
tpebs_send_record_cmd()with teardown viatpebs_cmd_in_flightandtpebs_cmd_cond, newline-terminate control commands so consecutive writes cannot coalesce inevlist__ctlfd_recv(), checkcheck_if_command_finished(&tpebs_cmd)undertpebs_mtxonly when not stopping, ignore late samples inprocess_sample_event()oncetpebs_stoppingis set, and guardevsel__tpebs_open()against a concurrenttpebs_stop()withtpebs_gen,tpebs_enabled, andtpebs_stoppingacross closing the fds andfinish_command().
Quoted 5 lines
Quoted 2 lines
Include <
pthread.h>, <signal.h>, <stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> explicitly.I feel it's too verbose and not clear what's problem. Please explain the race and how it fixes the issue concisely.
Agreed, the commit message grew unwieldy across revisions. In v13, I've rewritten it into two short paragraphs summarizing the two bugs (the tpebs_cmd.out double-close/leak between __sample_reader() and tpebs_stop(), and the unlocked tpebs_reader_thread read / concurrent open-close teardown race across tpebs_send_record_cmd()'s unlocked window) and how they are fixed.
Thanks,
Ian
Quoted 2 lines
Thanks,
Namhyung
Ian Rogers
reviewerPATCH v12 16/20 perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx
In reply to Ian RogersReplace tpebs_mtx_get() and its pthread_once() initialization helper tpebs_init() with DEFINE_MUTEX(tpebs_mtx) and static initializers for control_fd and ack_fd.
--- tools/perf/util/intel-tpebs.c | 91 +++++++++++++++-------------------- 1 file changed, 38 insertions(+), 53 deletions(-)
tools/perf/util/intel-tpebs.c +38 -53
@@ -38,12 +38,12 @@bool tpebs_recording;enum tpebs_mode tpebs_mode; +static DEFINE_MUTEX (tpebs_mtx);static LIST_HEAD (tpebs_results);static pthread_t tpebs_reader_thread;static struct child_process tpebs_cmd; -static int control_fd[2 ], ack_fd[2 ]; -static struct mutex tpebs_mtx;static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; +static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 };static bool tpebs_stopping;static bool tpebs_enabled;static bool tpebs_cmd_in_flight;
@@ -63,23 +63,8 @@ struct tpebs_retire_lat {bool started; }; -static void tpebs_init (void ) -{ -mutex_init (&tpebs_mtx); - control_fd[0 ] = control_fd[1 ] =-1 ; - ack_fd[0 ] = ack_fd[1 ] =-1 ; -} - -static struct mutex *tpebs_mtx_get (void ) -{ -static pthread_once_t tpebs_once =PTHREAD_ONCE_INIT ; - -pthread_once (&tpebs_once, tpebs_init); -return &tpebs_mtx; -} -static struct tpebs_retire_lat *tpebs_retire_lat__find (struct evsel *evsel) -__must_hold (tpebs_mtx_get ()); + __must_hold(&tpebs_mtx);static int evsel__tpebs_start_perf_record (struct evsel *evsel) {
@@ -209,19 +194,19 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused, {struct tpebs_retire_lat *t; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx);if (tpebs_stopping || tpebs_cmd.pid ==0 ) {/* Record is stopping or has terminated. */ -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return 0 ; } t =tpebs_retire_lat__find (sample->evsel);if (!t) { -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return -EINVAL ; }if (should_ignore_sample (sample, t)) { -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return 0 ; }/*
@@ -231,7 +216,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,*/ t->last = sample->weight3;update_stats (&t->stats, sample->weight3); -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return 0 ; }
@@ -261,7 +246,7 @@ static void *__sample_reader(void *arg)return NULL ; } -static int tpebs_send_record_cmd (const char *msg)__must_hold (tpebs_mtx_get ()) +static int tpebs_send_record_cmd(const char *msg) __must_hold(&tpebs_mtx) {struct pollfd pollfd = { .events =POLLIN , };bool is_stop = !strcmp (msg,EVLIST_CTL_CMD_STOP_TAG );
@@ -272,7 +257,7 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())while (tpebs_cmd_in_flight) {if (!is_stop && tpebs_stopping)return -EBUSY ; -cond_wait (&tpebs_cmd_cond,tpebs_mtx_get ()); +cond_wait (&tpebs_cmd_cond, &tpebs_mtx); }if (!is_stop && tpebs_stopping)return -EBUSY ;
@@ -289,7 +274,7 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())* Let go of the lock while sending/receiving as blocking can starve the * sample reading thread. */ -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);/* Send perf record command. */ len =scnprintf (cmd_buf,sizeof (cmd_buf),"%s\n" , msg);
@@ -317,10 +302,10 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())if (!poll (&pollfd,1 ,500 )) {bool stopping, finished; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); stopping = tpebs_stopping; finished = !stopping &&check_if_command_finished (&tpebs_cmd); -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);if (stopping) { ret = -EBUSY ; goto out;
@@ -340,10 +325,10 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())if (!(pollfd.revents &POLLIN )) {bool stopping, finished; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); stopping = tpebs_stopping; finished = !stopping &&check_if_command_finished (&tpebs_cmd); -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);if (stopping) { ret = -EBUSY ; goto out;
@@ -365,7 +350,7 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())pr_err ("tpebs: perf record control ack failed\n" ); out:/* Re-take lock as expected by caller. */ -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); tpebs_cmd_in_flight = false;cond_broadcast (&tpebs_cmd_cond);return ret;
@@ -374,7 +359,7 @@ static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_get())/* * tpebs_stop - stop the sample data read thread and the perf record process. */ -static int tpebs_stop (void )__must_hold (tpebs_mtx_get ()) +static int tpebs_stop(void) __must_hold(&tpebs_mtx) {int ret =0 ;
@@ -399,9 +384,9 @@ static int tpebs_stop(void) __must_hold(tpebs_mtx_get())*/ tpebs_cmd.pid =0 ; reader_thread = tpebs_reader_thread; -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);pthread_join (reader_thread,NULL ); -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx);if (control_fd[0 ] >=0 ) {close (control_fd[0 ]); control_fd[0 ] =-1 ;
@@ -537,20 +522,20 @@ static int evsel__tpebs_prepare(struct evsel *evsel)struct evsel *pos;struct tpebs_retire_lat *tpebs_event; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); tpebs_event =tpebs_retire_lat__find (evsel);if (tpebs_event) {/* evsel, or an identically named one, was already prepared. */ -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return 0 ; } tpebs_event =tpebs_retire_lat__new (evsel);if (!tpebs_event) { -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return -ENOMEM ; }list_add_tail (&tpebs_event->nd, &tpebs_results); -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);/* * Eagerly prepare all other evsels on the list to try to ensure that by
@@ -586,9 +571,9 @@ int evsel__tpebs_open(struct evsel *evsel)if (!tpebs_recording)return 0 ; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx);if (tpebs_stopping) { -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return -EBUSY ; }/* Only start the events once. */
@@ -598,17 +583,17 @@ int evsel__tpebs_open(struct evsel *evsel) t =tpebs_retire_lat__find (evsel); valid = t && t->started; -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);/* May fail as the event wasn't started. */ return valid ?0 : -EBUSY ; } -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx); ret =evsel__tpebs_prepare (evsel);if (ret)return ret; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx);if (tpebs_stopping || tpebs_cmd.pid !=0 ) { ret = -EBUSY ; goto out;
@@ -671,14 +656,14 @@ int evsel__tpebs_open(struct evsel *evsel)if (actual_pid >0 ) {kill (actual_pid,SIGTERM );while (tpebs_cmd_in_flight) -cond_wait (&tpebs_cmd_cond,tpebs_mtx_get ()); +cond_wait (&tpebs_cmd_cond, &tpebs_mtx); tpebs_cmd.pid =0 ;if (started_thread) {pthread_t reader_thread = tpebs_reader_thread; -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);pthread_join (reader_thread,NULL ); -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); } }if (tpebs_cmd.out >=0 ) {
@@ -709,7 +694,7 @@ int evsel__tpebs_open(struct evsel *evsel) tpebs_stopping = false; } } -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);return ret; }
@@ -729,7 +714,7 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_idx, int thread) count =perf_counts (evsel->counts, cpu_map_idx, thread); -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); t =tpebs_retire_lat__find (evsel);/* * If reading the first tpebs result, send a ping to the record
@@ -738,10 +723,10 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_idx, int thread)*/ if (t && &t->nd == tpebs_results.next) { ret =tpebs_send_record_cmd (EVLIST_CTL_CMD_PING_TAG ); -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);if (ret)return ret; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); }if (t ==NULL || t->stats.n ==0 ) {/* No sample data, use default. */
@@ -780,7 +765,7 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_idx, int thread)break ; } } -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx);if (old_count) { count->val = old_count->val + val;
@@ -805,7 +790,7 @@ void evsel__tpebs_close(struct evsel *evsel) {struct tpebs_retire_lat *t; -mutex_lock (tpebs_mtx_get ()); +mutex_lock (&tpebs_mtx); t =tpebs_retire_lat__find (evsel);if (t) {list_del_init (&t->nd);
@@ -814,5 +799,5 @@ void evsel__tpebs_close(struct evsel *evsel)if (list_empty (&tpebs_results))tpebs_stop (); } -mutex_unlock (tpebs_mtx_get ()); +mutex_unlock (&tpebs_mtx); }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 16/20] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Replace
tpebs_mtx_get()and itspthread_once()initialization helpertpebs_init()withDEFINE_MUTEX(tpebs_mtx)and static initializers forcontrol_fdandack_fd.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Namhyung Kim
maintainerRe: [PATCH v12 16/20] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx
In reply to Ian RogersQuoted 15 lines · Ian Rogers
Replace
tpebs_mtx_get()and itspthread_once()initialization helpertpebs_init()withDEFINE_MUTEX(tpebs_mtx)and static initializers forcontrol_fdandack_fd.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/intel-tpebs.c | 91 +++++++++++++++-------------------- 1 file changed, 38 insertions(+), 53 deletions(-)tools/perf/util/intel-tpebs.c +0
-0@@ -38,12 +38,12 @@
tools/perf/util/intel-tpebs.c line 46
Quoted 10 lines
tools/perf/util/intel-tpebs.c +2
-2bool tpebs_recording;enum tpebs_mode tpebs_mode; +static DEFINE_MUTEX (tpebs_mtx);static LIST_HEAD (tpebs_results);static pthread_t tpebs_reader_thread;static struct child_process tpebs_cmd; -static int control_fd[2 ], ack_fd[2 ]; -static struct mutex tpebs_mtx;static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; +static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 };
Nit: I think it's better to split the line when they have initializers.
Thanks,
Namhyung
Quoted 3 lines
tools/perf/util/intel-tpebs.c +0
-0static bool tpebs_stopping;static bool tpebs_enabled;static bool tpebs_cmd_in_flight;
Ian Rogers
reviewerRe: [PATCH v12 16/20] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx
Quoted 17 lines · Namhyung Kim
Quoted 15 lines · Ian Rogers
Replace
tpebs_mtx_get()and itspthread_once()initialization helpertpebs_init()withDEFINE_MUTEX(tpebs_mtx)and static initializers forcontrol_fdandack_fd.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/intel-tpebs.c | 91 +++++++++++++++-------------------- 1 file changed, 38 insertions(+), 53 deletions(-)tools/perf/util/intel-tpebs.c +0
-0@@ -38,12 +38,12 @@
Quoted 12 lines
tools/perf/util/intel-tpebs.c line 46
Quoted 10 lines
tools/perf/util/intel-tpebs.c +2
-2bool tpebs_recording;enum tpebs_mode tpebs_mode; +static DEFINE_MUTEX (tpebs_mtx);static LIST_HEAD (tpebs_results);static pthread_t tpebs_reader_thread;static struct child_process tpebs_cmd; -static int control_fd[2 ], ack_fd[2 ]; -static struct mutex tpebs_mtx;static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; +static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 };Nit: I think it's better to split the line when they have initializers.
Done in v13 (along with folding in the __guarded_by(&tpebs_mtx) annotations from the next patch).
Thanks,
Ian
Quoted 7 lines
Thanks,
NamhyungQuoted 3 lines
tools/perf/util/intel-tpebs.c +0
-0static bool tpebs_stopping;static bool tpebs_enabled;static bool tpebs_cmd_in_flight;
Ian Rogers
reviewerPATCH v12 17/20 perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations
In reply to Ian RogersAdd __guarded_by() and __must_hold() annotations to static parser and config state in util/config.c, TPEBS state in util/intel-tpebs.c, and bpf_progs and cgroups trees in struct perf_env.
--- tools/perf/util/bpf-event.h | 5 ++-- tools/perf/util/config.c | 47 +++++++++++++++++++++-------------- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 ++++++++------ tools/perf/util/intel-tpebs.c | 18 ++++++++------ 5 files changed, 54 insertions(+), 36 deletions(-)
tools/perf/util/bpf-event.h +3 -2
@@ -6,11 +6,11 @@#include <linux/rbtree.h> #include <api/fd/array.h> #include <stdio.h> +#include "env.h" struct bpf_prog_info ;struct machine ;union perf_event ; -struct perf_env ;struct perf_sample ;struct perf_session ;struct record_opts ;
@@ -42,7 +42,8 @@ int machine__process_bpf(struct machine *machine, union perf_event *event,int evlist__add_bpf_sb_event (struct evlist *evlist,struct perf_env *env); void __bpf_event__print_bpf_prog_info(struct perf_bpil *info_linear, struct perf_env *env, -FILE *fp); + FILE *fp) + __must_hold_shared(&env->bpf_progs.lock);void bpf_metadata_free (struct bpf_metadata *metadata);#else static inline int machine__process_bpf(struct machine *machine __maybe_unused,
tools/perf/util/config.c +29 -18
116 lines not on this page · raw message
tools/perf/util/env.c +1 -0
@@ -302,6 +302,7 @@ void perf_env__exit(struct perf_env *env) }void perf_env__init (struct perf_env *env) +__context_unsafe (/* initialization */ ) {memset (env,0 ,sizeof (*env));#ifdef HAVE_LIBBPF_SUPPORT
tools/perf/util/env.h +11 -8
@@ -139,16 +139,16 @@ struct perf_env {*/ struct {struct rw_semaphore lock; -struct rb_root infos; -u32 infos_cnt; -struct rb_root btfs; -u32 btfs_cnt; + struct rb_root infos __guarded_by(&lock); + u32 infos_cnt __guarded_by(&lock); + struct rb_root btfs __guarded_by(&lock); + u32 btfs_cnt __guarded_by(&lock); } bpf_progs;#endif // HAVE_LIBBPF_SUPPORT /* same reason as above (for perf-top) */ struct {struct rw_semaphore lock; -struct rb_root tree; + struct rb_root tree __guarded_by(&lock); } cgroups;/* For fast cpu to numa node lookup via perf_env__numa_node */
@@ -223,7 +223,8 @@ int perf_env__nr_cpus_avail(struct perf_env *env);void perf_env__init (struct perf_env *env);#ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, -struct bpf_prog_info_node *info_node); + struct bpf_prog_info_node *info_node) + __must_hold(&env->bpf_progs.lock);bool perf_env__insert_bpf_prog_info (struct perf_env *env,struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
@@ -233,9 +234,11 @@ void perf_env__iterate_bpf_prog_info(struct perf_env *env, void *data), void *data);bool perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); -bool __perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); +bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) + __must_hold(&env->bpf_progs.lock);struct btf_node *perf_env__find_btf (struct perf_env *env,__u32 btf_id); -struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id); +struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id) + __must_hold_shared(&env->bpf_progs.lock);#endif // HAVE_LIBBPF_SUPPORT int perf_env__numa_node (struct perf_env *env,struct perf_cpu cpu);
tools/perf/util/intel-tpebs.c +10 -8
@@ -39,15 +39,16 @@bool tpebs_recording;enum tpebs_mode tpebs_mode;static DEFINE_MUTEX (tpebs_mtx); -static LIST_HEAD (tpebs_results); -static pthread_t tpebs_reader_thread; -static struct child_process tpebs_cmd; +static struct list_head tpebs_results __guarded_by(&tpebs_mtx) = LIST_HEAD_INIT(tpebs_results); +static pthread_t tpebs_reader_thread __guarded_by(&tpebs_mtx); +static struct child_process tpebs_cmd __guarded_by(&tpebs_mtx);static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; -static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 }; -static bool tpebs_stopping; -static bool tpebs_enabled; -static bool tpebs_cmd_in_flight; -static uint64_t tpebs_gen; +static int control_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static int ack_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static bool tpebs_stopping __guarded_by(&tpebs_mtx); +static bool tpebs_enabled __guarded_by(&tpebs_mtx); +static bool tpebs_cmd_in_flight __guarded_by(&tpebs_mtx); +static uint64_t tpebs_gen __guarded_by(&tpebs_mtx);struct tpebs_retire_lat {struct list_head nd;
@@ -67,6 +68,7 @@ static struct tpebs_retire_lat *tpebs_retire_lat__find(struct evsel *evsel)__must_hold (&tpebs_mtx);static int evsel__tpebs_start_perf_record (struct evsel *evsel) + __must_hold(&tpebs_mtx) {const char **record_argv;int tpebs_event_size =0 , i =0 , ret;
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 17/20] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Add
__guarded_by()and__must_hold()annotations to static parser and config state inutil/config.c, TPEBS state inutil/intel-tpebs.c, andbpf_progsand cgroups trees in structperf_env.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
Namhyung Kim
maintainerRe: [PATCH v12 17/20] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations
In reply to Ian RogersQuoted 3 lines · Ian Rogers
Add
__guarded_by()and__must_hold()annotations to static parser and config state inutil/config.c, TPEBS state inutil/intel-tpebs.c, andbpf_progsand cgroups trees in structperf_env.
Looks like an unrelated set of changes. Probably better to split. I think TPEBS changes can go to the previous commit.
Thanks,
Namhyung
Quoted 269 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/bpf-event.h | 5 ++-- tools/perf/util/config.c | 47 +++++++++++++++++++++-------------- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 ++++++++------ tools/perf/util/intel-tpebs.c | 18 ++++++++------ 5 files changed, 54 insertions(+), 36 deletions(-)tools/perf/util/bpf-event.h +3
-2@@ -6,11 +6,11 @@#include <linux/rbtree.h> #include <api/fd/array.h> #include <stdio.h> +#include "env.h" struct bpf_prog_info ;struct machine ;union perf_event ; -struct perf_env ;struct perf_sample ;struct perf_session ;struct record_opts ;@@ -42,7 +42,8 @@ int machine__process_bpf(struct machine *machine, union perf_event *event,int evlist__add_bpf_sb_event (struct evlist *evlist,struct perf_env *env); void __bpf_event__print_bpf_prog_info(struct perf_bpil *info_linear, struct perf_env *env, -FILE *fp); + FILE *fp) + __must_hold_shared(&env->bpf_progs.lock);void bpf_metadata_free (struct bpf_metadata *metadata);#else static inline int machine__process_bpf(struct machine *machine __maybe_unused,tools/perf/util/config.c +29
-18@@ -64,15 +64,29 @@ struct perf_stat_config stat_config = {char buildid_dir[MAXPATHLEN ];/* root dir for buildid, binary cache */ -static FILE *config_file; -static const char *config_file_name; -static int config_linenr; -static int config_file_eof; -static struct perf_config_set *config_set; +/* Parsing and rewriting share the static parser state. */ +static DEFINE_MUTEX (config_mutex); + +/* Serializes whole perf_config__set_variable() updates. */ +static DEFINE_MUTEX (config_update_mutex); + +/* +* Protects the global config_set. Separate from config_mutex because +* initializing config_set parses the config files, which acquires +* config_mutex. +*/ +static DEFINE_MUTEX (config_set_mutex); + +static FILE *config_file __guarded_by(&config_mutex); +static const char *config_file_name __guarded_by(&config_mutex); +static int config_linenr __guarded_by(&config_mutex); +static int config_file_eof __guarded_by(&config_mutex); +static struct perf_config_set *config_set __guarded_by(&config_set_mutex);const char *config_exclusive_filename;static int get_next_char (void ) + __must_hold(&config_mutex) {int c;FILE *f;@@ -99,8 +113,9 @@ static int get_next_char(void) }static char *parse_value (void ) + __must_hold(&config_mutex) { -static char value[1024 ]; + static char value[1024] __guarded_by(&config_mutex);int quote =0 , comment =0 , space =0 ;size_t len =0 ;@@ -170,6 +185,7 @@ static inline int iskeychar(int c) }static int get_value (config_fn_t fn,void *data,char *name,unsigned int len) + __must_hold(&config_mutex) {int c;char *value;@@ -201,6 +217,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) }static int get_extended_base_var (char *name,int baselen,int c) + __must_hold(&config_mutex) { do { if (c == '\n')@@ -237,6 +254,7 @@ static int get_extended_base_var(char *name, int baselen, int c) }static int get_base_var (char *name) + __must_hold(&config_mutex) {int baselen =0 ;@@ -257,10 +275,11 @@ static int get_base_var(char *name) }static int perf_parse_file (config_fn_t fn,void *data) + __must_hold(&config_mutex) {int comment =0 ;int baselen =0 ; -static char var[MAXNAME ]; + static char var[MAXNAME] __guarded_by(&config_mutex);/* U+FEFF Byte Order Mark in UTF8 */ static const unsigned char *utf8_bom = (unsigned char *)"\xef\xbb\xbf" ;@@ -549,12 +568,6 @@ int perf_default_config(const char *var, const char *value,return 0 ; } -/* Parsing and rewriting share the static parser state. */ -static DEFINE_MUTEX (config_mutex); - -/* Serializes whole perf_config__set_variable() updates. */ -static DEFINE_MUTEX (config_update_mutex); -static int perf_config_from_file (config_fn_t fn,const char *filename,void *data) {int ret;@@ -737,6 +750,7 @@ static int set_value(struct perf_config_item *item, const char *value)static int collect_config (const char *var,const char *value,void *perf_config_set) + __must_hold(&config_mutex) {int ret =-1 ;char *ptr, *key;@@ -853,11 +867,8 @@ struct perf_config_set *perf_config_set__load_file(const char *file)return set; } -/* Not config_mutex: building the set parses the config files, which takes it. */ -static DEFINE_MUTEX (config_set_mutex); - -/* Called with config_set_mutex held. */ static int perf_config__init (void ) + __must_hold(&config_set_mutex) {if (config_set ==NULL ) config_set =perf_config_set__new ();@@ -975,7 +986,7 @@ int perf_config__set_variable(const char *var, const char *value)/* Static: the parser publishes it as config_file_name. */ { -static char path[PATH_MAX ]; + static char path[PATH_MAX] __guarded_by(&config_update_mutex);char *user_config =mkpath (path,sizeof (path),"%s/.perfconfig" ,getenv ("HOME" )); config_filename = config_exclusive_filename ?: user_config;tools/perf/util/env.c +1
-0@@ -302,6 +302,7 @@ void perf_env__exit(struct perf_env *env) }void perf_env__init (struct perf_env *env) +__context_unsafe (/* initialization */ ) {memset (env,0 ,sizeof (*env));#ifdef HAVE_LIBBPF_SUPPORT tools/perf/util/env.h +11
-8@@ -139,16 +139,16 @@ struct perf_env {*/ struct {struct rw_semaphore lock; -struct rb_root infos; -u32 infos_cnt; -struct rb_root btfs; -u32 btfs_cnt; + struct rb_root infos __guarded_by(&lock); + u32 infos_cnt __guarded_by(&lock); + struct rb_root btfs __guarded_by(&lock); + u32 btfs_cnt __guarded_by(&lock); } bpf_progs;#endif // HAVE_LIBBPF_SUPPORT /* same reason as above (for perf-top) */ struct {struct rw_semaphore lock; -struct rb_root tree; + struct rb_root tree __guarded_by(&lock); } cgroups;/* For fast cpu to numa node lookup via perf_env__numa_node */ @@ -223,7 +223,8 @@ int perf_env__nr_cpus_avail(struct perf_env *env);void perf_env__init (struct perf_env *env);#ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, -struct bpf_prog_info_node *info_node); + struct bpf_prog_info_node *info_node) + __must_hold(&env->bpf_progs.lock);bool perf_env__insert_bpf_prog_info (struct perf_env *env,struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,@@ -233,9 +234,11 @@ void perf_env__iterate_bpf_prog_info(struct perf_env *env, void *data), void *data);bool perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); -bool __perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); +bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) + __must_hold(&env->bpf_progs.lock);struct btf_node *perf_env__find_btf (struct perf_env *env,__u32 btf_id); -struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id); +struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id) + __must_hold_shared(&env->bpf_progs.lock);#endif // HAVE_LIBBPF_SUPPORT int perf_env__numa_node (struct perf_env *env,struct perf_cpu cpu);tools/perf/util/intel-tpebs.c +10
-8@@ -39,15 +39,16 @@bool tpebs_recording;enum tpebs_mode tpebs_mode;static DEFINE_MUTEX (tpebs_mtx); -static LIST_HEAD (tpebs_results); -static pthread_t tpebs_reader_thread; -static struct child_process tpebs_cmd; +static struct list_head tpebs_results __guarded_by(&tpebs_mtx) = LIST_HEAD_INIT(tpebs_results); +static pthread_t tpebs_reader_thread __guarded_by(&tpebs_mtx); +static struct child_process tpebs_cmd __guarded_by(&tpebs_mtx);static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; -static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 }; -static bool tpebs_stopping; -static bool tpebs_enabled; -static bool tpebs_cmd_in_flight; -static uint64_t tpebs_gen; +static int control_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static int ack_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static bool tpebs_stopping __guarded_by(&tpebs_mtx); +static bool tpebs_enabled __guarded_by(&tpebs_mtx); +static bool tpebs_cmd_in_flight __guarded_by(&tpebs_mtx); +static uint64_t tpebs_gen __guarded_by(&tpebs_mtx);struct tpebs_retire_lat {struct list_head nd;@@ -67,6 +68,7 @@ static struct tpebs_retire_lat *tpebs_retire_lat__find(struct evsel *evsel)__must_hold (&tpebs_mtx);static int evsel__tpebs_start_perf_record (struct evsel *evsel) + __must_hold(&tpebs_mtx) {const char **record_argv;int tpebs_event_size =0 , i =0 , ret;Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerRe: [PATCH v12 17/20] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations
Quoted 8 lines · Namhyung Kim
Quoted 3 lines · Ian Rogers
Add
__guarded_by()and__must_hold()annotations to static parser and config state inutil/config.c, TPEBS state inutil/intel-tpebs.c, andbpf_progsand cgroups trees in structperf_env.Looks like an unrelated set of changes. Probably better to split. I think TPEBS changes can go to the previous commit.
Done in v13: I folded the intel-tpebs annotations into patch 16 and split the config and env annotations into standalone patches 17 and 18.
Thanks,
Ian
Quoted 272 lines
Thanks,
NamhyungQuoted 269 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/bpf-event.h | 5 ++-- tools/perf/util/config.c | 47 +++++++++++++++++++++-------------- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 ++++++++------ tools/perf/util/intel-tpebs.c | 18 ++++++++------ 5 files changed, 54 insertions(+), 36 deletions(-)tools/perf/util/bpf-event.h +3
-2@@ -6,11 +6,11 @@#include <linux/rbtree.h> #include <api/fd/array.h> #include <stdio.h> +#include "env.h" struct bpf_prog_info ;struct machine ;union perf_event ; -struct perf_env ;struct perf_sample ;struct perf_session ;struct record_opts ;@@ -42,7 +42,8 @@ int machine__process_bpf(struct machine *machine, union perf_event *event,int evlist__add_bpf_sb_event (struct evlist *evlist,struct perf_env *env); void __bpf_event__print_bpf_prog_info(struct perf_bpil *info_linear, struct perf_env *env, -FILE *fp); + FILE *fp) + __must_hold_shared(&env->bpf_progs.lock);void bpf_metadata_free (struct bpf_metadata *metadata);#else static inline int machine__process_bpf(struct machine *machine __maybe_unused,tools/perf/util/config.c +29
-18@@ -64,15 +64,29 @@ struct perf_stat_config stat_config = {char buildid_dir[MAXPATHLEN ];/* root dir for buildid, binary cache */ -static FILE *config_file; -static const char *config_file_name; -static int config_linenr; -static int config_file_eof; -static struct perf_config_set *config_set; +/* Parsing and rewriting share the static parser state. */ +static DEFINE_MUTEX (config_mutex); + +/* Serializes whole perf_config__set_variable() updates. */ +static DEFINE_MUTEX (config_update_mutex); + +/* +* Protects the global config_set. Separate from config_mutex because +* initializing config_set parses the config files, which acquires +* config_mutex. +*/ +static DEFINE_MUTEX (config_set_mutex); + +static FILE *config_file __guarded_by(&config_mutex); +static const char *config_file_name __guarded_by(&config_mutex); +static int config_linenr __guarded_by(&config_mutex); +static int config_file_eof __guarded_by(&config_mutex); +static struct perf_config_set *config_set __guarded_by(&config_set_mutex);const char *config_exclusive_filename;static int get_next_char (void ) + __must_hold(&config_mutex) {int c;FILE *f;@@ -99,8 +113,9 @@ static int get_next_char(void) }static char *parse_value (void ) + __must_hold(&config_mutex) { -static char value[1024 ]; + static char value[1024] __guarded_by(&config_mutex);int quote =0 , comment =0 , space =0 ;size_t len =0 ;@@ -170,6 +185,7 @@ static inline int iskeychar(int c) }static int get_value (config_fn_t fn,void *data,char *name,unsigned int len) + __must_hold(&config_mutex) {int c;char *value;@@ -201,6 +217,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) }static int get_extended_base_var (char *name,int baselen,int c) + __must_hold(&config_mutex) { do { if (c == '\n')@@ -237,6 +254,7 @@ static int get_extended_base_var(char *name, int baselen, int c) }static int get_base_var (char *name) + __must_hold(&config_mutex) {int baselen =0 ;@@ -257,10 +275,11 @@ static int get_base_var(char *name) }static int perf_parse_file (config_fn_t fn,void *data) + __must_hold(&config_mutex) {int comment =0 ;int baselen =0 ; -static char var[MAXNAME ]; + static char var[MAXNAME] __guarded_by(&config_mutex);/* U+FEFF Byte Order Mark in UTF8 */ static const unsigned char *utf8_bom = (unsigned char *)"\xef\xbb\xbf" ;@@ -549,12 +568,6 @@ int perf_default_config(const char *var, const char *value,return 0 ; } -/* Parsing and rewriting share the static parser state. */ -static DEFINE_MUTEX (config_mutex); - -/* Serializes whole perf_config__set_variable() updates. */ -static DEFINE_MUTEX (config_update_mutex); -static int perf_config_from_file (config_fn_t fn,const char *filename,void *data) {int ret;@@ -737,6 +750,7 @@ static int set_value(struct perf_config_item *item, const char *value)static int collect_config (const char *var,const char *value,void *perf_config_set) + __must_hold(&config_mutex) {int ret =-1 ;char *ptr, *key;@@ -853,11 +867,8 @@ struct perf_config_set *perf_config_set__load_file(const char *file)return set; } -/* Not config_mutex: building the set parses the config files, which takes it. */ -static DEFINE_MUTEX (config_set_mutex); - -/* Called with config_set_mutex held. */ static int perf_config__init (void ) + __must_hold(&config_set_mutex) {if (config_set ==NULL ) config_set =perf_config_set__new ();@@ -975,7 +986,7 @@ int perf_config__set_variable(const char *var, const char *value)/* Static: the parser publishes it as config_file_name. */ { -static char path[PATH_MAX ]; + static char path[PATH_MAX] __guarded_by(&config_update_mutex);char *user_config =mkpath (path,sizeof (path),"%s/.perfconfig" ,getenv ("HOME" )); config_filename = config_exclusive_filename ?: user_config;tools/perf/util/env.c +1
-0@@ -302,6 +302,7 @@ void perf_env__exit(struct perf_env *env) }void perf_env__init (struct perf_env *env) +__context_unsafe (/* initialization */ ) {memset (env,0 ,sizeof (*env));#ifdef HAVE_LIBBPF_SUPPORT tools/perf/util/env.h +11
-8@@ -139,16 +139,16 @@ struct perf_env {*/ struct {struct rw_semaphore lock; -struct rb_root infos; -u32 infos_cnt; -struct rb_root btfs; -u32 btfs_cnt; + struct rb_root infos __guarded_by(&lock); + u32 infos_cnt __guarded_by(&lock); + struct rb_root btfs __guarded_by(&lock); + u32 btfs_cnt __guarded_by(&lock); } bpf_progs;#endif // HAVE_LIBBPF_SUPPORT /* same reason as above (for perf-top) */ struct {struct rw_semaphore lock; -struct rb_root tree; + struct rb_root tree __guarded_by(&lock); } cgroups;/* For fast cpu to numa node lookup via perf_env__numa_node */ @@ -223,7 +223,8 @@ int perf_env__nr_cpus_avail(struct perf_env *env);void perf_env__init (struct perf_env *env);#ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, -struct bpf_prog_info_node *info_node); + struct bpf_prog_info_node *info_node) + __must_hold(&env->bpf_progs.lock);bool perf_env__insert_bpf_prog_info (struct perf_env *env,struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,@@ -233,9 +234,11 @@ void perf_env__iterate_bpf_prog_info(struct perf_env *env, void *data), void *data);bool perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); -bool __perf_env__insert_btf (struct perf_env *env,struct btf_node *btf_node); +bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) + __must_hold(&env->bpf_progs.lock);struct btf_node *perf_env__find_btf (struct perf_env *env,__u32 btf_id); -struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id); +struct btf_node *__perf_env__find_btf (struct perf_env *env,__u32 btf_id) + __must_hold_shared(&env->bpf_progs.lock);#endif // HAVE_LIBBPF_SUPPORT int perf_env__numa_node (struct perf_env *env,struct perf_cpu cpu);tools/perf/util/intel-tpebs.c +10
-9@@ -39,15 +39,16 @@bool tpebs_recording;enum tpebs_mode tpebs_mode;static DEFINE_MUTEX (tpebs_mtx); -static LIST_HEAD (tpebs_results); -static pthread_t tpebs_reader_thread; -static struct child_process tpebs_cmd; +static struct list_head tpebs_results __guarded_by(&tpebs_mtx) = LIST_HEAD_INIT(tpebs_results); +static pthread_t tpebs_reader_thread __guarded_by(&tpebs_mtx); +static struct child_process tpebs_cmd __guarded_by(&tpebs_mtx);static struct cond tpebs_cmd_cond = { .cond =PTHREAD_COND_INITIALIZER }; -static int control_fd[2 ] = {-1 ,-1 }, ack_fd[2 ] = {-1 ,-1 }; -static bool tpebs_stopping; -static bool tpebs_enabled; -static bool tpebs_cmd_in_flight; -static uint64_t tpebs_gen; +static int control_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static int ack_fd[2] __guarded_by(&tpebs_mtx) = { -1, -1 }; +static bool tpebs_stopping __guarded_by(&tpebs_mtx); +static bool tpebs_enabled __guarded_by(&tpebs_mtx); +static bool tpebs_cmd_in_flight __guarded_by(&tpebs_mtx); +static uint64_t tpebs_gen __guarded_by(&tpebs_mtx);struct tpebs_retire_lat {struct list_head nd;@@ -67,6 +68,7 @@ static struct tpebs_retire_lat *tpebs_retire_lat__find(struct evsel *evsel)__must_hold (&tpebs_mtx);static int evsel__tpebs_start_perf_record (struct evsel *evsel) + __must_hold(&tpebs_mtx) {const char **record_argv;int tpebs_event_size =0 , i =0 , ret; --2.56.0.542.g3ef89b7020-goog
Ian Rogers
reviewerPATCH v12 18/20 perf maps: Fix lock inversion in maps__load_first()
In reply to Ian RogersIn maps__load_first(), take a reference to the first map and drop maps__lock(maps) before calling map__load() to avoid a read-to-write self-deadlock when loading kernel maps inserts extra trampoline maps.
--- tools/perf/util/maps.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
tools/perf/util/maps.c +8 -3
@@ -1459,10 +1459,15 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map)void maps__load_first (struct maps *maps) { -down_read (maps__lock (maps)); +struct map *first =NULL ; +down_read (maps__lock (maps));if (maps__nr_maps (maps) >0 ) -map__load (maps__maps_by_address (maps)[0 ]); - + first =map__get (maps__maps_by_address (maps)[0 ]);up_read (maps__lock (maps)); + +if (first) { +map__load (first); +map__put (first); + } }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 18/20] perf maps: Fix lock inversion in maps__load_first()
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
In
maps__load_first(), take a reference to the first map and dropmaps__lock(maps)before callingmap__load()to avoid a read-to-write self-deadlock when loading kernel maps inserts extra trampoline maps.Fixes e77b0236cd0c ("perf maps: Add maps__load_first()")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Ian Rogers
reviewerPATCH v12 19/20 perf maps: Fix map index lookup fallback and removal
In reply to Ian Rogersmaps__by_address_index() and maps__by_name_index() currently only run their linear pointer scan when maps_by_address_sorted or maps_by_name_sorted is false, returning -1 (UINT_MAX) immediately if bsearch() returns NULL. When a map's start/end address or DSO short_name is updated in place without clearing the sorted flags (for example during module map setup), map__start_cmp() or map__strcmp() tie-breakers can become stale and cause bsearch() to miss an entry that is present in the array.
Fix the lookup and removal paths so stale sort keys or missing maps are handled safely:
- Fall back to the linear pointer scan in maps__by_address_index() and maps__by_name_index() when bsearch() returns NULL, and avoid calling bsearch() on empty or NULL arrays.
- Check map__start(prev) < map__start(new) in __maps__insert() before preserving maps_by_address_sorted so entries with end == 0 (from map__new2()) or equal start addresses do not leave an unsorted array marked sorted.
- Factor out __maps__remove_at() with bounds checks on address_idx and name_idx (freeing maps_by_name if out of bounds) and remove from maps_by_name before calling map__put() on the maps_by_address entry so REFCNT_CHECKING does not see a freed handle. Have maps__remove_maps() call __maps__remove_at(maps, i) directly so it removes at index i in O(1) without redundant lookup or risking an infinite loop if lookup failed.
- Guard against maps__by_address_index() returning -1 wrapping ++i to
0 in maps__find_next_entry().
- Clear maps_by_name_sorted and re-sort maps_by_address when maps__fixup_end() clamps equal-start maps, and use machine__update_kernel_mmap() instead of the unlocked machine__set_kernel_mmap() in machine__create_kernel_maps().
Add a unit test in tests/maps.c covering maps__remove(), maps__remove_maps(), maps__fixup_end(), and maps__find_next_entry().
--- tools/perf/tests/maps.c | 140 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/machine.c | 21 ++---- tools/perf/util/maps.c | 92 ++++++++++++++++--------- 3 files changed, 208 insertions(+), 45 deletions(-)
tools/perf/tests/maps.c +140 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <inttypes.h> +#include <string.h> #include <unistd.h> #include <linux/compiler.h> #include <linux/kernel.h>
@@ -253,9 +254,148 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unureturn TEST_OK ; } +static bool remove_not_keep_cb (struct map *map,void *data) +{ +const char *keep_name = data; + +return strcmp (dso__name (map__dso (map)), keep_name) !=0 ; +} + +static int check_remove_after_end_update (bool swap_order,bool use_by_name, +bool use_fixup_end,bool use_remove_maps) +{ +struct perf_env host_env; +struct machine *machine; +struct maps *maps; +struct map *map_a =NULL , *map_b =NULL , *map_keep =NULL , *missing =NULL ; +struct map *map_first, *map_second, *found; +struct map_def expected_after_first[] = { + {"map_same" ,1000 , use_fixup_end ?1000 :1100 }, + {"z_keep" ,1000 ,1300 }, + }; +struct map_def expected_after_remove_maps[] = { + {"z_keep" ,1000 ,1300 }, + }; +int ret =TEST_FAIL ; + +perf_env__init (&host_env); + machine =machine__new_live (&host_env,/*kernel_maps=*/ false,getpid ()); + maps =maps__new (machine); +if (!machine || !maps) + goto out_env; + + map_a =dso__new_map ("map_same" ); + map_b =dso__new_map ("map_same" ); + map_keep =dso__new_map ("z_keep" ); + missing =dso__new_map ("missing" ); +if (!map_a || !map_b || !map_keep || !missing) + goto out_maps; + + map_first = swap_order ? map_b : map_a; + map_second = swap_order ? map_a : map_b; + +map__set_start (map_first,1000 ); +map__set_end (map_first,1100 ); +map__set_start (map_second,1000 ); +map__set_end (map_second,1200 ); +map__set_start (map_keep,1000 ); +map__set_end (map_keep,1300 ); + +if (maps__insert (maps, map_first) || +maps__insert (maps, map_second) || +maps__insert (maps, map_keep)) + goto out_maps; + +/* Trigger sorting of maps_by_address, and optionally maps_by_name. */ + found =maps__find (maps,1000 ); +map__zput (found); +if (use_by_name) { + found =maps__find_by_name (maps,"z_keep" ); +map__zput (found); + } + +/* Missing map must not wrap around to index 0 in maps__find_next_entry(). */ + found =maps__find_next_entry (maps, missing); +if (found) { +map__zput (found); + goto out_maps; + } + +if (use_fixup_end) { +maps__fixup_end (maps); + }else { +/* +* Simulate in-place end adjustment (as in module map setup), +* which can invert the pointer tie-breaker in map__start_cmp(). +*/ +map__set_end (map_second,1100 ); + } + +if (use_remove_maps) { +map__zput (map_a); +map__zput (map_b); +map__zput (map_keep); +maps__remove_maps (maps, remove_not_keep_cb, (void *)"z_keep" ); +if (use_by_name) { + found =maps__find_by_name (maps,"z_keep" ); +if (found &&maps__nr_maps (maps) ==1 ) + ret =TEST_OK ; +map__zput (found); + }else { + ret =check_maps (expected_after_remove_maps, +ARRAY_SIZE (expected_after_remove_maps), maps); + } + }else { +maps__remove (maps, map_first); +map__zput (map_a); +map__zput (map_b); +map__zput (map_keep); +if (use_by_name) { + found =maps__find_by_name (maps,"z_keep" ); +if (found &&maps__nr_maps (maps) ==2 ) + ret =TEST_OK ; +map__zput (found); + }else { + ret =check_maps (expected_after_first, +ARRAY_SIZE (expected_after_first), maps); + } + } + +out_maps: +map__zput (map_a); +map__zput (map_b); +map__zput (map_keep); +map__zput (missing); +out_env: +maps__zput (maps); +machine__delete (machine); +perf_env__exit (&host_env); +return ret; +} + +static int test__maps__remove (struct test_suite *t__maybe_unused ,int subtest__maybe_unused ) +{ +for (int fixup =0 ; fixup <=1 ; fixup++) { +for (int by_name =0 ; by_name <=1 ; by_name++) { +for (int swap =0 ; swap <=1 ; swap++) { +TEST_ASSERT_VAL ("maps__remove failed" , +check_remove_after_end_update (swap, by_name, + fixup, false) == +TEST_OK ); +TEST_ASSERT_VAL ("maps__remove_maps failed" , +check_remove_after_end_update (swap, by_name, + fixup, true) == +TEST_OK ); + } + } + } +return TEST_OK ; +} +static struct test_case tests__maps[] = {TEST_CASE ("Test merge_in interface" , maps__merge_in),TEST_CASE ("Test fix up overlap interface" , maps__fixup_overlap_and_insert), +TEST_CASE ("Test remove interface" , maps__remove), { .name =NULL , } };
tools/perf/util/machine.c +7 -14
@@ -1551,19 +1551,6 @@ static int machine__create_modules(struct machine *machine)return 0 ; } -static void machine__set_kernel_mmap (struct machine *machine, -u64 start,u64 end) -{ -map__set_start (machine->vmlinux_map, start); -map__set_end (machine->vmlinux_map, end); -/* -* Be a bit paranoid here, some perf.data file came with -* a zero sized synthesized MMAP event for the kernel. -*/ -if (start ==0 && end ==0 ) -map__set_end (machine->vmlinux_map, ~0ULL ); -} -struct kernel_mmap_mutation_ctx {u64 start;u64 end;
@@ -1575,6 +1562,10 @@ static int kernel_mmap_mutate_cb(struct map *map, void *data)map__set_start (map, ctx->start);map__set_end (map, ctx->end); +/* +* Be a bit paranoid here, some perf.data file came with +* a zero sized synthesized MMAP event for the kernel. +*/ if (ctx->start ==0 && ctx->end ==0 )map__set_end (map, ~0ULL );return 0 ;
@@ -1639,8 +1630,10 @@ int machine__create_kernel_maps(struct machine *machine) machine__kernel_map(machine));if (next) { -machine__set_kernel_mmap (machine, start,map__start (next)); + ret =machine__update_kernel_mmap (machine, start,map__start (next));map__put (next); +if (ret <0 ) + goto out_put; } }
tools/perf/util/maps.c +61 -31
186 lines not on this page · raw message
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 19/20] perf maps: Fix map index lookup fallback and removal
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines
maps__by_address_index()andmaps__by_name_index()currently only run their linear pointer scan whenmaps_by_address_sortedormaps_by_name_sortedis false, returning -1 (UINT_MAX) immediately ifbsearch()returns NULL. When a map's start/end address or DSOshort_nameis updated in place without clearing the sorted flags (for example during module map setup),map__start_cmp()ormap__strcmp()tie-breakers can become stale and causebsearch()to miss an entry that is present in the array.Fix the lookup and removal paths so stale sort keys or missing maps are handled safely:
- Fall back to the linear pointer scan inmaps__by_address_index()andmaps__by_name_index()whenbsearch()returns NULL, and avoid callingbsearch()on empty or NULL arrays.
- Checkmap__start(prev)<map__start(new)in__maps__insert()before
[ ... ]
Add a unit test intests/maps.ccoveringmaps__remove(),maps__remove_maps(),maps__fixup_end(), andmaps__find_next_entry().
Quoted 2 lines
Fixes 659ad3492b91 ("perf maps: Switch from rbtree to lazily sorted array for addresses")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
Ian Rogers
reviewerPATCH v12 20/20 perf maps: Add context analysis annotations
In reply to Ian RogersAdd __returns_ctx_lock() to maps__lock(), annotate DECLARE_RC_STRUCT(maps) members protected by lock with __guarded_by(&lock), and annotate internal maps helpers with __must_hold() and __must_hold_shared().
--- tools/perf/util/maps.c | 119 ++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 42 deletions(-)
tools/perf/util/maps.c +77 -42
@@ -31,12 +31,12 @@ DECLARE_RC_STRUCT(maps) {* @maps_by_address: array of maps sorted by their starting address if * maps_by_address_sorted is true. */ -struct map **maps_by_address; + struct map **maps_by_address __guarded_by(&lock);/** * @maps_by_name: optional array of maps sorted by their dso name if * maps_by_name_sorted is true. */ -struct map **maps_by_name; + struct map **maps_by_name __guarded_by(&lock);struct machine *machine;#ifdef HAVE_LIBUNWIND_SUPPORT void *addr_space;
@@ -51,26 +51,33 @@ DECLARE_RC_STRUCT(maps) {* @nr_maps: number of maps_by_address, and possibly maps_by_name, * entries that contain maps. */ -unsigned int nr_maps; + unsigned int nr_maps __guarded_by(&lock);/** * @nr_maps_allocated: number of entries in maps_by_address and possibly * maps_by_name. */ -unsigned int nr_maps_allocated; + unsigned int nr_maps_allocated __guarded_by(&lock);/** * @last_search_by_name_idx: cache of last found by name entry's index * as frequent searches for the same dso name are common. */ unsigned int last_search_by_name_idx;/** @maps_by_address_sorted: is maps_by_address sorted. */ -bool maps_by_address_sorted; + bool maps_by_address_sorted __guarded_by(&lock);/** @maps_by_name_sorted: is maps_by_name sorted. */ -bool maps_by_name_sorted; + bool maps_by_name_sorted __guarded_by(&lock);/** @ends_broken: does the map contain a map where end values are unset/unsorted? */ -bool ends_broken; + bool ends_broken __guarded_by(&lock); }; +static struct rw_semaphore *maps__lock (const struct maps *maps) + __returns_ctx_lock(&RC_CHK_ACCESS(maps)->lock) +{ +return (struct rw_semaphore *)&RC_CHK_ACCESS (maps)->lock; +} +static void check_invariants (const struct maps *maps__maybe_unused ) +__must_hold_shared (maps__lock (maps)) {#ifndef NDEBUG assert (RC_CHK_ACCESS (maps)->nr_maps <=RC_CHK_ACCESS (maps)->nr_maps_allocated);
@@ -120,55 +127,65 @@ static void check_invariants(const struct maps *maps __maybe_unused) }static struct map **maps__maps_by_address (const struct maps *maps) +__must_hold_shared (maps__lock (maps)) {return RC_CHK_ACCESS (maps)->maps_by_address; }static void maps__set_maps_by_address (struct maps *maps,struct map **new) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->maps_by_address = new; }static void maps__set_nr_maps_allocated (struct maps *maps,unsigned int nr_maps_allocated) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->nr_maps_allocated = nr_maps_allocated; }static void maps__set_nr_maps (struct maps *maps,unsigned int nr_maps) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->nr_maps = nr_maps; }/* Not in the header, to aid reference counting. */ static struct map **maps__maps_by_name (const struct maps *maps) +__must_hold_shared (maps__lock (maps)) {return RC_CHK_ACCESS (maps)->maps_by_name; }static void maps__set_maps_by_name (struct maps *maps,struct map **new) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->maps_by_name = new; }static bool maps__maps_by_address_sorted (const struct maps *maps) +__must_hold_shared (maps__lock (maps)) {return RC_CHK_ACCESS (maps)->maps_by_address_sorted; }static void maps__set_maps_by_address_sorted (struct maps *maps,bool value) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->maps_by_address_sorted = value; }static bool maps__maps_by_name_sorted (const struct maps *maps) +__must_hold_shared (maps__lock (maps)) {return RC_CHK_ACCESS (maps)->maps_by_name_sorted; }static void maps__set_maps_by_name_sorted (struct maps *maps,bool value) +__must_hold (maps__lock (maps)) {RC_CHK_ACCESS (maps)->maps_by_name_sorted = value; }
@@ -178,11 +195,22 @@ struct machine *maps__machine(const struct maps *maps)return RC_CHK_ACCESS (maps)->machine; } -unsigned int maps__nr_maps (const struct maps *maps) +static unsigned int __maps__nr_maps (const struct maps *maps) +__must_hold_shared (maps__lock (maps)) {return RC_CHK_ACCESS (maps)->nr_maps; } +unsigned int maps__nr_maps (const struct maps *maps) +{ +unsigned int nr; + +down_read (maps__lock (maps)); + nr =__maps__nr_maps (maps); +up_read (maps__lock (maps)); +return nr; +} +refcount_t *maps__refcnt (struct maps *maps) {return &RC_CHK_ACCESS (maps)->refcnt;
@@ -221,12 +249,8 @@ void maps__set_libdw_addr_space_dwfl(struct maps *maps, void *dwfl) }#endif -static struct rw_semaphore *maps__lock (struct maps *maps) -{ -return &RC_CHK_ACCESS (maps)->lock; -} -static void maps__init (struct maps *maps,struct machine *machine) +__context_unsafe (/* initialization */ ) {init_rwsem (maps__lock (maps));RC_CHK_ACCESS (maps)->maps_by_address =NULL ;
@@ -248,11 +272,12 @@ static void maps__init(struct maps *maps, struct machine *machine) }static void maps__exit (struct maps *maps) +__context_unsafe (/* teardown */ ) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps); -for (unsigned int i =0 ; i <maps__nr_maps (maps); i++) { +for (unsigned int i =0 ; i <__maps__nr_maps (maps); i++) {map__zput (maps_by_address[i]);if (maps_by_name)map__zput (maps_by_name[i]);
@@ -301,6 +326,7 @@ void maps__put(struct maps *maps) }static void __maps__free_maps_by_name (struct maps *maps) +__must_hold (maps__lock (maps)) {if (!maps__maps_by_name (maps))return ;
@@ -308,7 +334,7 @@ static void __maps__free_maps_by_name(struct maps *maps)/* * Free everything to try to do it from the rbtree in the next search */ -for (unsigned int i =0 ; i <maps__nr_maps (maps); i++) +for (unsigned int i =0 ; i <__maps__nr_maps (maps); i++)map__put (maps__maps_by_name (maps)[i]);zfree (&RC_CHK_ACCESS (maps)->maps_by_name);
@@ -341,12 +367,13 @@ static int map__start_cmp(const void *a, const void *b) }static void __maps__sort_by_address (struct maps *maps) +__must_hold (maps__lock (maps)) {if (maps__maps_by_address_sorted (maps))return ;qsort (maps__maps_by_address (maps), -maps__nr_maps (maps), +__maps__nr_maps (maps),sizeof (struct map *), map__start_cmp);maps__set_maps_by_address_sorted (maps, true);
@@ -389,7 +416,7 @@ static int maps__sort_by_name(struct maps *maps) err = -ENOMEM ; else {struct map **maps_by_address =maps__maps_by_address (maps); -unsigned int n =maps__nr_maps (maps); +unsigned int n =__maps__nr_maps (maps);maps__set_maps_by_name (maps, maps_by_name);for (unsigned int i =0 ; i < n; i++)
@@ -398,7 +425,7 @@ static int maps__sort_by_name(struct maps *maps) }if (!err) {qsort (maps_by_name, -maps__nr_maps (maps), +__maps__nr_maps (maps),sizeof (struct map *), map__strcmp);maps__set_maps_by_name_sorted (maps, true);
@@ -410,9 +437,10 @@ static int maps__sort_by_name(struct maps *maps) }static unsigned int maps__by_address_index (const struct maps *maps,const struct map *map) +__must_hold_shared (maps__lock (maps)) {struct map **maps_by_address =maps__maps_by_address (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);if (nr_maps &&maps__maps_by_address_sorted (maps)) { struct map **mapp =
@@ -431,9 +459,10 @@ static unsigned int maps__by_address_index(const struct maps *maps, const struct }static unsigned int maps__by_name_index (const struct maps *maps,const struct map *map) +__must_hold_shared (maps__lock (maps)) {struct map **maps_by_name =maps__maps_by_name (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);if (!maps_by_name)return -1 ;
@@ -474,10 +503,11 @@ static void map__set_kmap_maps(struct map *map, struct maps *maps) }static int __maps__insert (struct maps *maps,struct map *new) +__must_hold (maps__lock (maps)) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);unsigned int nr_allocate =RC_CHK_ACCESS (maps)->nr_maps_allocated;if (nr_maps +1 > nr_allocate) {
@@ -545,10 +575,11 @@ int maps__insert(struct maps *maps, struct map *map) }static void __maps__remove_at (struct maps *maps,unsigned int address_idx) +__must_hold (maps__lock (maps)) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);struct map *map;if (address_idx >= nr_maps)
@@ -578,6 +609,7 @@ static void __maps__remove_at(struct maps *maps, unsigned int address_idx) }static void __maps__remove (struct maps *maps,struct map *map) +__must_hold (maps__lock (maps)) {__maps__remove_at (maps,maps__by_address_index (maps, map)); }
@@ -640,7 +672,7 @@ bool maps__empty(struct maps *maps)bool res;down_read (maps__lock (maps)); - res =maps__nr_maps (maps) ==0 ; + res =__maps__nr_maps (maps) ==0 ;up_read (maps__lock (maps));return res;
@@ -668,7 +700,7 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data)* the array - this may cause maps to be skipped or * repeated. */ -for (unsigned int i =0 ; i <maps__nr_maps (maps); i++) { +for (unsigned int i =0 ; i <__maps__nr_maps (maps); i++) {struct map **maps_by_address =maps__maps_by_address (maps);struct map *map = maps_by_address[i];
@@ -695,7 +727,7 @@ int maps__load_maps(struct maps *maps)return 0 ;down_read (maps__lock (maps)); - nr_maps =maps__nr_maps (maps); + nr_maps =__maps__nr_maps (maps);if (nr_maps ==0 ) {up_read (maps__lock (maps));return 0 ;
@@ -728,7 +760,7 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *datadown_write (maps__lock (maps)); maps_by_address =maps__maps_by_address (maps); -for (unsigned int i =0 ; i <maps__nr_maps (maps);) { +for (unsigned int i =0 ; i <__maps__nr_maps (maps);) {if (cb (maps_by_address[i], data)) {__maps__remove_at (maps, i); removed = true;
@@ -782,7 +814,7 @@ struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, st* This prevents deadlocking if iteration triggers further map insertions. */ down_read (maps__lock (maps)); - nr_maps =maps__nr_maps (maps); + nr_maps =__maps__nr_maps (maps); maps_copy =calloc (nr_maps,sizeof (*maps_copy));if (maps_copy) {for (unsigned int i =0 ; i < nr_maps; i++) {
@@ -867,9 +899,10 @@ size_t maps__fprintf(struct maps *maps, FILE *fp)* Same as find_vma() in kernel. */ static unsigned int first_ending_after (struct maps *maps,const struct map *map) +__must_hold_shared (maps__lock (maps)) {struct map **maps_by_address =maps__maps_by_address (maps); -int low =0 , high = (int )maps__nr_maps (maps) -1 , first = high +1 ; +int low =0 , high = (int )__maps__nr_maps (maps) -1 , first = high +1 ;assert (maps__maps_by_address_sorted (maps));if (low <= high &&map__end (maps_by_address[0 ]) >map__start (map))
@@ -894,10 +927,11 @@ static unsigned int first_ending_after(struct maps *maps, const struct map *map)static int __maps__insert_sorted (struct maps *maps,unsigned int first_after_index,struct map *new1,struct map *new2) +__must_hold (maps__lock (maps)) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);unsigned int nr_allocate =RC_CHK_ACCESS (maps)->nr_maps_allocated;unsigned int to_add = new2 ?2 :1 ;
@@ -954,6 +988,7 @@ static int __maps__insert_sorted(struct maps *maps, unsigned int first_after_ind* adjusted or removed so that new fits without overlapping any entries. */ static int __maps__fixup_overlap_and_insert (struct maps *maps,struct map *new) +__must_hold (maps__lock (maps)) {int err =0 ;unsigned int i, ni =INT_MAX ;// Some gcc complain, but depends on maps_by_name...
@@ -965,7 +1000,7 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)* Iterate through entries where the end of the existing entry is * greater-than the new map's start. */ -for (i =first_ending_after (maps, new); i <maps__nr_maps (maps); ) { +for (i =first_ending_after (maps, new); i <__maps__nr_maps (maps); ) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps);struct map *pos = maps_by_address[i];
@@ -1076,7 +1111,7 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)return err; }else {struct map *next =NULL ; -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);if (i +1 < nr_maps) next = maps_by_address[i +1 ];
@@ -1160,8 +1195,8 @@ int maps__copy_from(struct maps *dest, struct maps *parent) }#endif parent_maps_by_address =maps__maps_by_address (parent); - n =maps__nr_maps (parent); -if (maps__nr_maps (dest) ==0 ) { + n =__maps__nr_maps (parent); +if (__maps__nr_maps (dest) ==0 ) {/* No existing mappings so just copy from parent to avoid reallocs in insert. */ unsigned int nr_maps_allocated =RC_CHK_ACCESS (parent)->nr_maps_allocated; struct map **dest_maps_by_address =
@@ -1245,7 +1280,7 @@ struct map *maps__find(struct maps *maps, u64 ip)if (maps__maps_by_address_sorted (maps)) {struct map **mapp =NULL ;struct map **maps_by_address =maps__maps_by_address (maps); -unsigned int nr_maps =maps__nr_maps (maps); +unsigned int nr_maps =__maps__nr_maps (maps);if (maps_by_address && nr_maps) mapp = bsearch(&ip, maps_by_address, nr_maps, sizeof(*mapp),
@@ -1281,7 +1316,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name)/* First check last found entry. */ i =RC_CHK_ACCESS (maps)->last_search_by_name_idx; -if (i <maps__nr_maps (maps) &&maps__maps_by_name (maps)) { +if (i <__maps__nr_maps (maps) &&maps__maps_by_name (maps)) {struct dso *dso =map__dso (maps__maps_by_name (maps)[i]);if (dso &&strcmp (dso__short_name (dso), name) ==0 ) {
@@ -1293,7 +1328,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name)/* Second search sorted array. */ if (!done &&maps__maps_by_name_sorted (maps)) {struct map **mapp = -bsearch (name,maps__maps_by_name (maps),maps__nr_maps (maps), +bsearch (name,maps__maps_by_name (maps),__maps__nr_maps (maps),sizeof (*mapp), map__strcmp_name);if (mapp) {
@@ -1316,7 +1351,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name)down_read (maps__lock (maps)); maps_by_address =maps__maps_by_address (maps); - n =maps__nr_maps (maps); + n =__maps__nr_maps (maps);for (i =0 ; i < n; i++) {struct map *pos = maps_by_address[i];struct dso *dso =map__dso (pos);
@@ -1346,7 +1381,7 @@ struct map *maps__find_next_entry(struct maps *maps, struct map *map)down_read (maps__lock (maps)); } i =maps__by_address_index (maps, map); - nr_maps =maps__nr_maps (maps); + nr_maps =__maps__nr_maps (maps);if (i < nr_maps && ++i < nr_maps) result =map__get (maps__maps_by_address (maps)[i]);
@@ -1365,7 +1400,7 @@ void maps__fixup_end(struct maps *maps)__maps__sort_by_address (maps); maps_by_address =maps__maps_by_address (maps); - n =maps__nr_maps (maps); + n =__maps__nr_maps (maps);for (unsigned int i =1 ; i < n; i++) {struct map *prev = maps_by_address[i -1 ];struct map *curr = maps_by_address[i];
@@ -1423,7 +1458,7 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) first_after_ =first_ending_after (kmaps, new_map); kmaps_maps_by_address =maps__maps_by_address (kmaps); -if (first_after_ >=maps__nr_maps (kmaps) || +if (first_after_ >=__maps__nr_maps (kmaps) ||map__start (kmaps_maps_by_address[first_after_]) >=map__end (new_map)) {/* No overlap so regular insert suffices. */ up_read (maps__lock (kmaps));
@@ -1438,7 +1473,7 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) first_after_ =first_ending_after (kmaps, new_map); kmaps_maps_by_address =maps__maps_by_address (kmaps); - kmaps__nr_maps =maps__nr_maps (kmaps); + kmaps__nr_maps =__maps__nr_maps (kmaps);if (first_after_ >= kmaps__nr_maps ||map__start (kmaps_maps_by_address[first_after_]) >=map__end (new_map)) {
@@ -1492,7 +1527,7 @@ void maps__load_first(struct maps *maps)struct map *first =NULL ;down_read (maps__lock (maps)); -if (maps__nr_maps (maps) >0 ) +if (__maps__nr_maps (maps) >0 ) first =map__get (maps__maps_by_address (maps)[0 ]);up_read (maps__lock (maps));
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
[email protected]
AI reviewRe: [PATCH v12 20/20] perf maps: Add context analysis annotations
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Add
__returns_ctx_lock()tomaps__lock(), annotateDECLARE_RC_STRUCT(maps)members protected by lock with__guarded_by(&lock), and annotate internal maps helpers with__must_hold()and__must_hold_shared().Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20