From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 664091870; Fri, 19 Jan 2024 04:07:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705637257; cv=none; b=YdB9b3yZYgzaksNytYxWU7eQUuaDm3Sw09MVTy6XYgyC0d+i10UNSknFQQKVbVk28zsB27pazJTrvPVCHVlPLfj8Rwic+cq4s7JEEGo2sAgHCak9/gvWQhohwNRUBX08lutRmFX/uA+U0NmHuIDEizVp49F1ssuUaWCHElZz9Hs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705637257; c=relaxed/simple; bh=EMVJzS2xUAq0Tked9+lQE9avM8FS5zcClqksRKRtOb8=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=N6T4x1KiWpXLPo0Rjl38ASuUpkby3JQfJvhwYG+S9G3chKHN/25rYrWd0jdJ8XhQuHclyAFhhcDuCvyrXlcm6d6YmZKnWoOElKnjw7Kp/spExp63b2GlWRmvNd73YRYbE+ncwujKzYg7wWy6U23c4t+aZL8qj9uf53m4O+AIAHk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4TGQzz43gzzGpq4; Fri, 19 Jan 2024 12:07:11 +0800 (CST) Received: from kwepemm600003.china.huawei.com (unknown [7.193.23.202]) by mail.maildlp.com (Postfix) with ESMTPS id 2E37218005D; Fri, 19 Jan 2024 12:07:32 +0800 (CST) Received: from ubuntu2204.huawei.com (10.67.174.22) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Fri, 19 Jan 2024 12:07:31 +0800 From: Yang Jihong To: , , , , , , , , , , CC: Subject: [PATCH 0/3] perf record: Fix segfault with '--timestamp-filename' option and pipe mode Date: Fri, 19 Jan 2024 04:03:01 +0000 Message-ID: <20240119040304.3708522-1-yangjihong1@huawei.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600003.china.huawei.com (7.193.23.202) When perf record uses '--timestamp-filename' option and pipe mode, will occur segfault: # perf record --timestamp-filename -o- true 1>/dev/null [ perf record: Woken up 1 times to write data ] [ perf record: Dump -.2024011813361681 ] perf: Segmentation fault Segmentation fault (core dumped) Debug the core file by using the gdb: # gdb perf core.3706841 [New LWP 3706841] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `perf record --timestamp-filename -o- true'. Program terminated with signal SIGSEGV, Segmentation fault. #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 145 struct perf_stat_evsel *ps = evsel->stats; (gdb) bt #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 #1 evlist__free_stats (evlist=evlist@entry=0x555bf0dad1b0) at util/stat.c:215 #2 0x0000555befd425e8 in evlist__delete (evlist=0x555bf0dad1b0) at util/evlist.c:175 #3 0x0000555befd42785 in evlist__delete (evlist=) at util/evlist.c:181 #4 0x0000555befc7f547 in cmd_record (argc=, argv=) at builtin-record.c:4252 #5 0x0000555befd27700 in run_builtin (p=p@entry=0x555bf05acf88 , argc=argc@entry=4, argv=argv@entry=0x7ffc2696a920) at perf.c:349 #6 0x0000555befc68751 in handle_internal_command (argv=0x7ffc2696a920, argc=4) at perf.c:402 #7 run_argv (argv=, argcp=) at perf.c:446 #8 main (argc=4, argv=0x7ffc2696a920) at perf.c:562 It is found that the memory of the evsel is modified. The reason is that perf_data__switch() not initialized 'new_filename', as a result, it uses the on-stack variable. It happens that the evsel address is stored here. 'new_filename' is freed later when uses '--timestamp-filename' option. Therefore, segfault occurs in the evsel_delete(). 1. patch1 fixes this problem. 2. patch2 to optimize the process, check conflict between '--timestamp-filename' option and the pipe mode before recording to avoid switch perf data. 3. patch3 fixes the code style problem by the way. Yang Jihong (3): perf record: Fix possible incorrect free in record__switch_output() perf record: Check conflict between '--timestamp-filename' option and pipe mode before recording perf data: Minor code style alignment cleanup tools/perf/builtin-record.c | 14 ++++++++++---- tools/perf/util/data.c | 10 ++++------ tools/perf/util/data.h | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-) -- 2.34.1