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

perf thread: Add reference count checking

Modify struct declaration and accessor functions for the reference
count checkers additional layer of indirection. Make sure pid_cmp in
builtin-sched.c uses the underlying/original struct in pointer
arithmetic, and not the temporary get/put indirection.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Brian Robbins <brianrob@linux.microsoft.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: Fangrui Song <maskray@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Ye Xingchen <ye.xingchen@zte.com.cn>
Cc: Yuan Can <yuancan@huawei.com>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230608232823.4027869-8-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
f6005caf 0dd5041c

+63 -53
+2 -2
tools/perf/builtin-sched.c
··· 1385 1385 { 1386 1386 pid_t l_tid, r_tid; 1387 1387 1388 - if (l->thread == r->thread) 1388 + if (RC_CHK_ACCESS(l->thread) == RC_CHK_ACCESS(r->thread)) 1389 1389 return 0; 1390 1390 l_tid = thread__tid(l->thread); 1391 1391 r_tid = thread__tid(r->thread); ··· 1393 1393 return -1; 1394 1394 if (l_tid > r_tid) 1395 1395 return 1; 1396 - return (int)(l->thread - r->thread); 1396 + return (int)(RC_CHK_ACCESS(l->thread) - RC_CHK_ACCESS(r->thread)); 1397 1397 } 1398 1398 1399 1399 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
+1 -1
tools/perf/tests/hists_link.c
··· 148 148 struct thread *t, struct map *m, struct symbol *s) 149 149 { 150 150 while (nr_samples--) { 151 - if (samples->thread == t && 151 + if (RC_CHK_ACCESS(samples->thread) == RC_CHK_ACCESS(t) && 152 152 RC_CHK_ACCESS(samples->map) == RC_CHK_ACCESS(m) && 153 153 samples->sym == s) 154 154 return 1;
+4 -1
tools/perf/ui/hist.c
··· 11 11 #include "../util/sort.h" 12 12 #include "../util/evsel.h" 13 13 #include "../util/evlist.h" 14 + #include "../util/thread.h" 14 15 #include "../util/util.h" 15 16 16 17 /* hist period print (hpp) functions */ ··· 275 274 if (ret) 276 275 return ret; 277 276 278 - if (a->thread != b->thread || !hist_entry__has_callchains(a) || !symbol_conf.use_callchain) 277 + if ((a->thread == NULL ? NULL : RC_CHK_ACCESS(a->thread)) != 278 + (b->thread == NULL ? NULL : RC_CHK_ACCESS(b->thread)) || 279 + !hist_entry__has_callchains(a) || !symbol_conf.use_callchain) 279 280 return 0; 280 281 281 282 ret = b->callchain->max_depth - a->callchain->max_depth;
+1 -1
tools/perf/util/hist.c
··· 2124 2124 struct hist_entry *he) 2125 2125 { 2126 2126 if (hists->thread_filter != NULL && 2127 - he->thread != hists->thread_filter) { 2127 + RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(hists->thread_filter)) { 2128 2128 he->filtered |= (1 << HIST_FILTER__THREAD); 2129 2129 return true; 2130 2130 }
+1 -1
tools/perf/util/machine.c
··· 2055 2055 if (!nd) 2056 2056 nd = thread_rb_node__find(th, &threads->entries.rb_root); 2057 2057 2058 - if (threads->last_match == th) 2058 + if (threads->last_match && RC_CHK_ACCESS(threads->last_match) == RC_CHK_ACCESS(th)) 2059 2059 threads__set_last_match(threads, NULL); 2060 2060 2061 2061 if (lock)
+1 -1
tools/perf/util/sort.c
··· 128 128 if (type != HIST_FILTER__THREAD) 129 129 return -1; 130 130 131 - return th && he->thread != th; 131 + return th && RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(th); 132 132 } 133 133 134 134 struct sort_entry sort_thread = {
+13 -7
tools/perf/util/thread.c
··· 41 41 { 42 42 char *comm_str; 43 43 struct comm *comm; 44 - struct thread *thread = zalloc(sizeof(*thread)); 44 + RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread)); 45 + struct thread *thread; 45 46 46 - if (thread != NULL) { 47 + if (ADD_RC_CHK(thread, _thread) != NULL) { 47 48 thread__set_pid(thread, pid); 48 49 thread__set_tid(thread, tid); 49 50 thread__set_ppid(thread, -1); ··· 69 68 list_add(&comm->list, thread__comm_list(thread)); 70 69 refcount_set(thread__refcnt(thread), 1); 71 70 /* Thread holds first ref to nsdata. */ 72 - thread->nsinfo = nsinfo__new(pid); 71 + RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid); 73 72 srccode_state_init(thread__srccode_state(thread)); 74 73 } 75 74 ··· 106 105 } 107 106 up_write(thread__comm_lock(thread)); 108 107 109 - nsinfo__zput(thread->nsinfo); 108 + nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo); 110 109 srccode_state_free(thread__srccode_state(thread)); 111 110 112 111 exit_rwsem(thread__namespaces_lock(thread)); 113 112 exit_rwsem(thread__comm_lock(thread)); 114 113 thread__free_stitch_list(thread); 115 - free(thread); 114 + RC_CHK_FREE(thread); 116 115 } 117 116 118 117 struct thread *thread__get(struct thread *thread) 119 118 { 120 - if (thread) 119 + struct thread *result; 120 + 121 + if (RC_CHK_GET(result, thread)) 121 122 refcount_inc(thread__refcnt(thread)); 122 - return thread; 123 + 124 + return result; 123 125 } 124 126 125 127 void thread__put(struct thread *thread) 126 128 { 127 129 if (thread && refcount_dec_and_test(thread__refcnt(thread))) 128 130 thread__delete(thread); 131 + else 132 + RC_CHK_PUT(thread); 129 133 } 130 134 131 135 static struct namespaces *__thread__namespaces(struct thread *thread)
+40 -39
tools/perf/util/thread.h
··· 15 15 #include "rwsem.h" 16 16 #include "event.h" 17 17 #include "callchain.h" 18 + #include <internal/rc_check.h> 18 19 19 20 struct addr_location; 20 21 struct map; ··· 35 34 struct thread *thread; 36 35 }; 37 36 38 - struct thread { 37 + DECLARE_RC_STRUCT(thread) { 39 38 struct maps *maps; 40 39 pid_t pid_; /* Not all tools update this */ 41 40 pid_t tid; ··· 124 123 125 124 static inline struct maps *thread__maps(struct thread *thread) 126 125 { 127 - return thread->maps; 126 + return RC_CHK_ACCESS(thread)->maps; 128 127 } 129 128 130 129 static inline void thread__set_maps(struct thread *thread, struct maps *maps) 131 130 { 132 - thread->maps = maps; 131 + RC_CHK_ACCESS(thread)->maps = maps; 133 132 } 134 133 135 134 static inline pid_t thread__pid(const struct thread *thread) 136 135 { 137 - return thread->pid_; 136 + return RC_CHK_ACCESS(thread)->pid_; 138 137 } 139 138 140 139 static inline void thread__set_pid(struct thread *thread, pid_t pid_) 141 140 { 142 - thread->pid_ = pid_; 141 + RC_CHK_ACCESS(thread)->pid_ = pid_; 143 142 } 144 143 145 144 static inline pid_t thread__tid(const struct thread *thread) 146 145 { 147 - return thread->tid; 146 + return RC_CHK_ACCESS(thread)->tid; 148 147 } 149 148 150 149 static inline void thread__set_tid(struct thread *thread, pid_t tid) 151 150 { 152 - thread->tid = tid; 151 + RC_CHK_ACCESS(thread)->tid = tid; 153 152 } 154 153 155 154 static inline pid_t thread__ppid(const struct thread *thread) 156 155 { 157 - return thread->ppid; 156 + return RC_CHK_ACCESS(thread)->ppid; 158 157 } 159 158 160 159 static inline void thread__set_ppid(struct thread *thread, pid_t ppid) 161 160 { 162 - thread->ppid = ppid; 161 + RC_CHK_ACCESS(thread)->ppid = ppid; 163 162 } 164 163 165 164 static inline int thread__cpu(const struct thread *thread) 166 165 { 167 - return thread->cpu; 166 + return RC_CHK_ACCESS(thread)->cpu; 168 167 } 169 168 170 169 static inline void thread__set_cpu(struct thread *thread, int cpu) 171 170 { 172 - thread->cpu = cpu; 171 + RC_CHK_ACCESS(thread)->cpu = cpu; 173 172 } 174 173 175 174 static inline int thread__guest_cpu(const struct thread *thread) 176 175 { 177 - return thread->guest_cpu; 176 + return RC_CHK_ACCESS(thread)->guest_cpu; 178 177 } 179 178 180 179 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu) 181 180 { 182 - thread->guest_cpu = guest_cpu; 181 + RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu; 183 182 } 184 183 185 184 static inline refcount_t *thread__refcnt(struct thread *thread) 186 185 { 187 - return &thread->refcnt; 186 + return &RC_CHK_ACCESS(thread)->refcnt; 188 187 } 189 188 190 189 static inline bool thread__comm_set(const struct thread *thread) 191 190 { 192 - return thread->comm_set; 191 + return RC_CHK_ACCESS(thread)->comm_set; 193 192 } 194 193 195 194 static inline void thread__set_comm_set(struct thread *thread, bool set) 196 195 { 197 - thread->comm_set = set; 196 + RC_CHK_ACCESS(thread)->comm_set = set; 198 197 } 199 198 200 199 static inline int thread__var_comm_len(const struct thread *thread) 201 200 { 202 - return thread->comm_len; 201 + return RC_CHK_ACCESS(thread)->comm_len; 203 202 } 204 203 205 204 static inline void thread__set_comm_len(struct thread *thread, int len) 206 205 { 207 - thread->comm_len = len; 206 + RC_CHK_ACCESS(thread)->comm_len = len; 208 207 } 209 208 210 209 static inline struct list_head *thread__namespaces_list(struct thread *thread) 211 210 { 212 - return &thread->namespaces_list; 211 + return &RC_CHK_ACCESS(thread)->namespaces_list; 213 212 } 214 213 215 214 static inline int thread__namespaces_list_empty(const struct thread *thread) 216 215 { 217 - return list_empty(&thread->namespaces_list); 216 + return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); 218 217 } 219 218 220 219 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread) 221 220 { 222 - return &thread->namespaces_lock; 221 + return &RC_CHK_ACCESS(thread)->namespaces_lock; 223 222 } 224 223 225 224 static inline struct list_head *thread__comm_list(struct thread *thread) 226 225 { 227 - return &thread->comm_list; 226 + return &RC_CHK_ACCESS(thread)->comm_list; 228 227 } 229 228 230 229 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) 231 230 { 232 - return &thread->comm_lock; 231 + return &RC_CHK_ACCESS(thread)->comm_lock; 233 232 } 234 233 235 234 static inline u64 thread__db_id(const struct thread *thread) 236 235 { 237 - return thread->db_id; 236 + return RC_CHK_ACCESS(thread)->db_id; 238 237 } 239 238 240 239 static inline void thread__set_db_id(struct thread *thread, u64 db_id) 241 240 { 242 - thread->db_id = db_id; 241 + RC_CHK_ACCESS(thread)->db_id = db_id; 243 242 } 244 243 245 244 static inline void *thread__priv(struct thread *thread) 246 245 { 247 - return thread->priv; 246 + return RC_CHK_ACCESS(thread)->priv; 248 247 } 249 248 250 249 static inline void thread__set_priv(struct thread *thread, void *p) 251 250 { 252 - thread->priv = p; 251 + RC_CHK_ACCESS(thread)->priv = p; 253 252 } 254 253 255 254 static inline struct thread_stack *thread__ts(struct thread *thread) 256 255 { 257 - return thread->ts; 256 + return RC_CHK_ACCESS(thread)->ts; 258 257 } 259 258 260 259 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts) 261 260 { 262 - thread->ts = ts; 261 + RC_CHK_ACCESS(thread)->ts = ts; 263 262 } 264 263 265 264 static inline struct nsinfo *thread__nsinfo(struct thread *thread) 266 265 { 267 - return thread->nsinfo; 266 + return RC_CHK_ACCESS(thread)->nsinfo; 268 267 } 269 268 270 269 static inline struct srccode_state *thread__srccode_state(struct thread *thread) 271 270 { 272 - return &thread->srccode_state; 271 + return &RC_CHK_ACCESS(thread)->srccode_state; 273 272 } 274 273 275 274 static inline bool thread__filter(const struct thread *thread) 276 275 { 277 - return thread->filter; 276 + return RC_CHK_ACCESS(thread)->filter; 278 277 } 279 278 280 279 static inline void thread__set_filter(struct thread *thread, bool filter) 281 280 { 282 - thread->filter = filter; 281 + RC_CHK_ACCESS(thread)->filter = filter; 283 282 } 284 283 285 284 static inline int thread__filter_entry_depth(const struct thread *thread) 286 285 { 287 - return thread->filter_entry_depth; 286 + return RC_CHK_ACCESS(thread)->filter_entry_depth; 288 287 } 289 288 290 289 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth) 291 290 { 292 - thread->filter_entry_depth = depth; 291 + RC_CHK_ACCESS(thread)->filter_entry_depth = depth; 293 292 } 294 293 295 294 static inline bool thread__lbr_stitch_enable(const struct thread *thread) 296 295 { 297 - return thread->lbr_stitch_enable; 296 + return RC_CHK_ACCESS(thread)->lbr_stitch_enable; 298 297 } 299 298 300 299 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en) 301 300 { 302 - thread->lbr_stitch_enable = en; 301 + RC_CHK_ACCESS(thread)->lbr_stitch_enable = en; 303 302 } 304 303 305 304 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread) 306 305 { 307 - return thread->lbr_stitch; 306 + return RC_CHK_ACCESS(thread)->lbr_stitch; 308 307 } 309 308 310 309 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs) 311 310 { 312 - thread->lbr_stitch = lbrs; 311 + RC_CHK_ACCESS(thread)->lbr_stitch = lbrs; 313 312 } 314 313 315 314 static inline bool thread__is_filtered(struct thread *thread)