Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4848932rwl; Mon, 10 Apr 2023 18:33:05 -0700 (PDT) X-Google-Smtp-Source: AKy350bNvyE7oXabKKLgUosVGBZxYrI6XIpiLi97NCqZoyTxRtdgvzMPO3nuG9a0bPHd660WBTfH X-Received: by 2002:a17:903:1252:b0:1a6:3d8e:face with SMTP id u18-20020a170903125200b001a63d8efacemr6386893plh.0.1681176785156; Mon, 10 Apr 2023 18:33:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681176785; cv=none; d=google.com; s=arc-20160816; b=giv4D33lnPN44Vfqro36gRKkKzAYNzaxEg+fsLfEHNzvzF6f/iuzc9SM21RSxYL5d3 kAbRb+g2J1rxBK6j0ldnT9iduv7Aau2ED9FNW5yZcQ0Iiwz63x5mXtUnb9bUziimTJRb TI8aOF05HUbt5hU16x+mxfXSjq0apSZTASiiaPN3cQVUlvOmV9AnDsnhQHM64K18OJpg Ru0tdZBE6TPGi/XUtrxFPHL+v/f5pexTvMDU/M4TuPEshU0JbYawxNNi+Od9Q1Itr/XV z2vypmrGMHJZ6DzYmos10vY6AjEiBHJNo3YhtKgYji3XEfelwg/iSBpgkN69N2Wg44ah ibrQ== 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=tI1VKMIGPW9B/SaHgJaTZkoII5tr3Fmj0sXZkfrunGU=; b=nwtBYQGXQMc7qph4l/NO/S9X5Cg2oB22sfbrbUk4o9ZAhnNMBWGGMVfW77YBrNRY5r 3kIXmv019WgEWjUwrz9u+wkvTdvCmnjlUXoCxdZ1DTxbY+EZsxjM7lUdfc3CVBu2Kj2x 4F9U+cRs66R9kG4xxM6jfJdqMXuUHw7UtZlFLiNuwBFZ1tgIOAr19eInVudvQ9iKrZ/u h2fMLtNYuFg8vAaVdGq/teR5CDHRVKUH60mILla+A81Qt7kwuQOiH+kTU/XkGXMJeyXc chev0aCJQSDTprXt1sD2vHfBZzEFtQGwNlL8XdtYnCUbJoyN6Ge1VXEXJ7gNxYxi8oYx NOIA== 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 r22-20020a170902be1600b001a63bb1497dsi4069779pls.376.2023.04.10.18.32.52; Mon, 10 Apr 2023 18:33:05 -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 S229938AbjDKBcc (ORCPT + 99 others); Mon, 10 Apr 2023 21:32:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229775AbjDKBca (ORCPT ); Mon, 10 Apr 2023 21:32:30 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A3BE10F7; Mon, 10 Apr 2023 18:32:29 -0700 (PDT) Received: from kwepemi500007.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4PwSxD3mCLzKrtw; Tue, 11 Apr 2023 09:31:44 +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; Tue, 11 Apr 2023 09:32:25 +0800 From: Hangliang Lai To: CC: , , , , , , , , , , , , , , Subject: [PATCH v4] perf top: expand the range of multithreaded phase Date: Tue, 11 Apr 2023 09:32:24 +0800 Message-ID: <20230411013224.2079-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, 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 Co-developed-by: Wenyu Liu Reviewed-by: Yunfeng Ye Acked-by: Namhyung Kim --- v3 -> v4 - Add Acked-by and Co-developed-by. 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