Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4132132rwl; Mon, 10 Apr 2023 06:42:32 -0700 (PDT) X-Google-Smtp-Source: AKy350bTtDYzLjEuXvY9KVkpqNL5+e6nsohoW8ysUFnAEIcBVs/dcSmo9JfpJcrD1hQkmYraLMN6 X-Received: by 2002:a17:907:874f:b0:94a:532b:3f70 with SMTP id qo15-20020a170907874f00b0094a532b3f70mr4308970ejc.20.1681134152347; Mon, 10 Apr 2023 06:42:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681134152; cv=none; d=google.com; s=arc-20160816; b=BJPaGXXLiE4nT6NgHbx9++pPotY7a6/OSDTJXKrskKR+cy/LERzneA+xxiraCXFlsD 2biZJTrMfU5Nl/t67cv1Bjuaw/IcNWn+JWSEKpjZOfPyHpvWgkAh2fdROx0Ohxjlp4CJ RuF3bAJp+6S6fU4QZuUVXLiXY84c+JpFI6gDbleyvcrIPY8vUw0mmnTr/HTWTk8+Z6v4 J7GpGKH5s6WXbkXoX5grl4lX1Ap6ojYwnHedKN38USJVrnj7S8MQ0AXGAXWtKt7o0nzF u30acq8fJVAm9D4YLIyH1GYvicavo7iO/2NczeWzr7aJmdbuhcTigylrBWpjMumUFme2 B+sA== 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=ta5lDIYDsDKr4Qhl8LYr7OkrP9wmajFbu7jIxfqYf2U=; b=XN+biLg1/gVsc4e28y4n5jkpOkHEYcweqyetvvBETi+DF9k0mKn9osGi3u+mhHE9Ni EkPiFagHfpDKpJe8eXO9wogGyl/4AIudvx/wfhqxxlI/QfMwwxXs02HWHzXXAV1fHJwt lfWFi1w9h6rltKAWs6ZMbIsybLnvQN1YtKQ/8RMyBjyYBk7wXGe9oMTXpDWX3VPAhAPU RMFr23zTYOlh7eZiePk4axHWNlLMtJtERlP1Dba4xHjEONSrD5n8RckHkCoXwtY1syV0 qTdLcq9YooinCUR+xNg0WoeQMBHnD/JkIBlcOPkiZQXiUvRmiHsTmK7UuS+fIMb4NzWF uz/Q== 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 f23-20020a05640214d700b0050260ef706bsi6216742edx.133.2023.04.10.06.42.04; Mon, 10 Apr 2023 06:42:32 -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 S229699AbjDJNWt (ORCPT + 99 others); Mon, 10 Apr 2023 09:22:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229626AbjDJNWs (ORCPT ); Mon, 10 Apr 2023 09:22:48 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8051C5B86; Mon, 10 Apr 2023 06:22:46 -0700 (PDT) Received: from kwepemi500007.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Pw8gz1J8Zz17Rv0; Mon, 10 Apr 2023 21:19:11 +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; Mon, 10 Apr 2023 21:22:41 +0800 From: Hangliang Lai To: CC: , , , , , , , , , , , , , , Subject: [PATCH v3] perf top: expand the range of multithreaded phase Date: Mon, 10 Apr 2023 21:22:39 +0800 Message-ID: <20230410132239.2062-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: dggems705-chm.china.huawei.com (10.3.19.182) 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, RCVD_IN_MSPIKE_H2,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 --- v2 -> v3 - Sorry for my misunderstanding, patch v3 makes perf_set_multithreaded unconditional after synthesis and set_singlethread in the end. tools/perf/builtin-top.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index d4b5b02bab73..ae96ddaf85c4 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1273,8 +1273,7 @@ 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(); + perf_set_multithreaded(); if (perf_hpp_list.socket) { ret = perf_env__read_cpu_topology_map(&perf_env); @@ -1352,6 +1351,7 @@ 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