Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1547406rwl; Wed, 5 Apr 2023 19:58:11 -0700 (PDT) X-Google-Smtp-Source: AKy350YYbcf5yDn1STrHuv0UFhRJmvlcD22HcSOUFaQZJ/l5KhYARq5u/DwlPDk0RIwhr5F/JhSE X-Received: by 2002:a17:907:75e3:b0:935:3028:ff58 with SMTP id jz3-20020a17090775e300b009353028ff58mr4839325ejc.55.1680749891529; Wed, 05 Apr 2023 19:58:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680749891; cv=none; d=google.com; s=arc-20160816; b=v259IhoM5yoam+GXDE10utr9CDDEo8lYSQKG3+ic+n5JB0aEjQGPilPkAbZkfK/scO TPFvQg/gRn/kNDpfw/76bgg9IX9THmaINp1vddz7fDmJWg989STFTbsv27mlqnvy7xxx VJ3PD3dKilBYEV3AlActoMjovbR7/cO9Ce1zyvI4yiEfnodif5ppy99H8PUV0O7drA4z 5dR1T1c64CRhPrvMxcW/gcU0XiK32vMQbSOFfxbf8oNVDoRzGO8E31eYdnXGjzrzm5aa fTHNfjCJuKDweu+R3O8SAO24NeOzrbX1OwrZeHMyj0/ysoK5QNrWRiwVdHVATx13VBIN d1ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=8RXhPByWOeEjpvXrvc5PgaChwa2jGYzZDn+RT1v7Afo=; b=TVvA3H19bUUH6ZHjZ0wBV6QS29sw7KG23kY8WQJK9VoCbVJA46zd93a47bftwxkwxq VSNMSpaKA/jp5ItaaczEisoC2rSmmYWT1fS5G22ckQHhbLtfkyS6U8REouuxofUZGG4Q 6LbZihNP75aoI1rLaEJW4qc5h0QPK6YoEsXsWvTBRHd37xDyehnepl4hoR54GQ3u1gwf 6sXIBPTvuBUPN+kz45Lwco+ZTaxRg2tNfmVzJHAqw45bQ7BCm8G0Kz+emAL8b/KJB/Bs xLAgJBGj1LV+T4kJiNld5Q9Hv6fT6smZTNkGPP3Y1SlfAM0opQwxozaAK6KVhGtye6bj mT2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d13-20020a170906040d00b0092f97d1a8efsi260036eja.504.2023.04.05.19.57.45; Wed, 05 Apr 2023 19:58:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234192AbjDFCy0 (ORCPT + 99 others); Wed, 5 Apr 2023 22:54:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235107AbjDFCxq (ORCPT ); Wed, 5 Apr 2023 22:53:46 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 884026A45; Wed, 5 Apr 2023 19:53:43 -0700 (PDT) Received: from kwepemi500007.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4PsQx871ppzKwlc; Thu, 6 Apr 2023 10:51:08 +0800 (CST) Received: from huawei.com (10.174.179.93) by kwepemi500007.china.huawei.com (7.221.188.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 6 Apr 2023 10:53:31 +0800 From: Hangliang Lai To: CC: , , , , , , , , , , , , , , Subject: [PATCH v2] perf top: expand the range of multithreaded phase Date: Thu, 6 Apr 2023 10:53:29 +0800 Message-ID: <20230406025329.1888-1-laihangliang1@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.179.93] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemi500007.china.huawei.com (7.221.188.207) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.3 required=5.0 tests=RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: Yunfeng Ye --- v1 -> v2 - Since perf top is always multi-threaded, remove top->nr_threads_synthesize judgment. tools/perf/builtin-top.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index d4b5b02bab73..a18db1ee87fa 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1242,8 +1242,7 @@ static int __cmd_top(struct perf_top *top) if (perf_session__register_idle_thread(top->session) < 0) return ret; - if (top->nr_threads_synthesize > 1) - perf_set_multithreaded(); + perf_set_multithreaded(); init_process_thread(top); @@ -1273,9 +1272,6 @@ static int __cmd_top(struct perf_top *top) top->evlist->core.threads, true, 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) { @@ -1352,6 +1348,9 @@ static int __cmd_top(struct perf_top *top) out_join_thread: cond_signal(&top->qe.cond); pthread_join(thread_process, NULL); + + perf_set_singlethreaded(); + return ret; } -- 2.33.0