Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf rwsem: Add clang's -Wthread-safety annotations

Add annotations used by clang's -Wthread-safety.

Fix dsos compilation errors caused by a lock of annotations.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Chaitanya S Prakash <chaitanyas.prakash@arm.com>
Cc: Fei Lang <langfei@huawei.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Link: https://lore.kernel.org/r/20250519224645.1810891-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
6fe06449 ab2c742d

+23 -5
+3
tools/perf/util/dsos.c
··· 157 157 const char *name, 158 158 const struct dso_id *id, 159 159 bool write_locked) 160 + SHARED_LOCKS_REQUIRED(dsos->lock) 160 161 { 161 162 struct dsos__key key = { 162 163 .long_name = name, ··· 263 262 264 263 static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const struct dso_id *id, 265 264 bool cmp_short, bool write_locked) 265 + SHARED_LOCKS_REQUIRED(dsos->lock) 266 266 { 267 267 struct dso *res; 268 268 ··· 340 338 } 341 339 342 340 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) 341 + SHARED_LOCKS_REQUIRED(dsos->lock) 343 342 { 344 343 struct dso *dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true); 345 344
+11
tools/perf/util/mutex.h
··· 43 43 #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__))) 44 44 45 45 /* 46 + * Documents functions that acquire a shared (reader) lock in the body of a 47 + * function, and do not release it. 48 + */ 49 + #define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__))) 50 + 51 + /* 46 52 * Documents functions that expect a lock to be held on entry to the function, 47 53 * and release it in the body of the function. 48 54 */ ··· 61 55 /* Documents a function that expects a mutex to be held prior to entry. */ 62 56 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__))) 63 57 58 + /* Documents a function that expects a shared (reader) lock to be held prior to entry. */ 59 + #define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__))) 60 + 64 61 /* Turns off thread safety checking within the body of a particular function. */ 65 62 #define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) 66 63 ··· 75 66 #define LOCKS_EXCLUDED(...) 76 67 #define LOCK_RETURNED(x) 77 68 #define EXCLUSIVE_LOCK_FUNCTION(...) 69 + #define SHARED_LOCK_FUNCTION(...) 78 70 #define UNLOCK_FUNCTION(...) 79 71 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) 80 72 #define EXCLUSIVE_LOCKS_REQUIRED(...) 73 + #define SHARED_LOCKS_REQUIRED(...) 81 74 #define NO_THREAD_SAFETY_ANALYSIS 82 75 83 76 #endif
+4
tools/perf/util/rwsem.c
··· 27 27 } 28 28 29 29 int down_read(struct rw_semaphore *sem) 30 + NO_THREAD_SAFETY_ANALYSIS 30 31 { 31 32 #if RWS_ERRORCHECK 32 33 mutex_lock(&sem->mtx); ··· 38 37 } 39 38 40 39 int up_read(struct rw_semaphore *sem) 40 + NO_THREAD_SAFETY_ANALYSIS 41 41 { 42 42 #if RWS_ERRORCHECK 43 43 mutex_unlock(&sem->mtx); ··· 49 47 } 50 48 51 49 int down_write(struct rw_semaphore *sem) 50 + NO_THREAD_SAFETY_ANALYSIS 52 51 { 53 52 #if RWS_ERRORCHECK 54 53 mutex_lock(&sem->mtx); ··· 60 57 } 61 58 62 59 int up_write(struct rw_semaphore *sem) 60 + NO_THREAD_SAFETY_ANALYSIS 63 61 { 64 62 #if RWS_ERRORCHECK 65 63 mutex_unlock(&sem->mtx);
+5 -5
tools/perf/util/rwsem.h
··· 10 10 */ 11 11 #define RWS_ERRORCHECK 0 12 12 13 - struct rw_semaphore { 13 + struct LOCKABLE rw_semaphore { 14 14 #if RWS_ERRORCHECK 15 15 struct mutex mtx; 16 16 #else ··· 21 21 int init_rwsem(struct rw_semaphore *sem); 22 22 int exit_rwsem(struct rw_semaphore *sem); 23 23 24 - int down_read(struct rw_semaphore *sem); 25 - int up_read(struct rw_semaphore *sem); 24 + int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem); 25 + int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); 26 26 27 - int down_write(struct rw_semaphore *sem); 28 - int up_write(struct rw_semaphore *sem); 27 + int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem); 28 + int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); 29 29 30 30 #endif /* _PERF_RWSEM_H */