linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf top: expand the range of multithreaded phase
@ 2023-03-17 10:05 Hangliang Lai
  2023-04-01  0:11 ` Namhyung Kim
  0 siblings, 1 reply; 10+ messages in thread
From: Hangliang Lai @ 2023-03-17 10:05 UTC (permalink / raw)
  To: liuwenyu7, acme, adrian.hunter, alexander.shishkin, brauner,
	hewenliang4, irogers, jolsa, linfeilong, linux-kernel,
	linux-perf-users, mark.rutland, mingo, namhyung, yeyunfeng

In __cmd_top, perf_set_multithreaded is used to enable pthread_rwlock, thus
donw_read and down_write can work to handle concurrency problems. Then top 
use perf_set_singlethreaded and switch to single threaded phase, assuming 
that no thread concurrency will happen later. 

However, a UAF problem could occur in perf top in single threaded phase, 
The concurrent procedure is like this:

display_thread                              process_thread
--------------                              --------------

thread__comm_len
  -> thread__comm_str
    -> __thread__comm_str(thread)
                                            thread__delete
                                             -> comm__free
                                              -> comm_str__put
                                               -> zfree(&cs->str)
    -> thread->comm_len = strlen(comm);

Since in single thread phase, perf_singlethreaded is true, down_read and 
down_write can not work to avoid concurrency problems.

This patch put perf_set_singlethreaded to the function tail to expand the 
multithreaded phase range, make display_thread and process_thread run
safe. 

Signed-off-by: Hangliang Lai  <laihangliang1@huawei.com>
Reported-by: Wenyu Liu <liuwenyu7@huawei.com>
Reviewed-by: Yunfeng Ye <yeyunfeng@huawei.com>
---
 tools/perf/builtin-top.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 7c6413447..74239940b 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1280,9 +1280,6 @@ static int __cmd_top(struct perf_top *top)
 				    top->evlist->core.threads, false,
 				    top->nr_threads_synthesize);
 
-	if (top->nr_threads_synthesize > 1)
-		perf_set_singlethreaded();
-
 	if (perf_hpp_list.socket) {
 		ret = perf_env__read_cpu_topology_map(&perf_env);
 		if (ret < 0) {
@@ -1359,6 +1356,10 @@ out_join:
 out_join_thread:
 	pthread_cond_signal(&top->qe.cond);
 	pthread_join(thread_process, NULL);
+
+	if (top->nr_threads_synthesize > 1)
+		perf_set_singlethreaded();
+
 	return ret;
 }
 
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-04-12 13:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-17 10:05 [PATCH] perf top: expand the range of multithreaded phase Hangliang Lai
2023-04-01  0:11 ` Namhyung Kim
2023-04-06  2:53   ` [PATCH v2] " Hangliang Lai
2023-04-07 21:21     ` Namhyung Kim
2023-04-10  2:58       ` Hangliang Lai
2023-04-10  3:38         ` Wenyu Liu(D)
2023-04-10 13:22       ` [PATCH v3] " Hangliang Lai
2023-04-10 15:36         ` Namhyung Kim
2023-04-11  1:32           ` [PATCH v4] " Hangliang Lai
2023-04-12 13:39             ` Arnaldo Carvalho de Melo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).