Received: by 2002:a05:7412:e79e:b0:f3:1519:9f41 with SMTP id o30csp117985rdd; Wed, 22 Nov 2023 10:52:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJ3IxL8M+49ztXw0OKBOG1S5NimJf2TSCWQZOhKQMwBkF0DuJsOoQ40En/4WLeWfiin+0I X-Received: by 2002:a05:6830:1106:b0:6b8:7880:de9 with SMTP id w6-20020a056830110600b006b878800de9mr3650237otq.19.1700679135115; Wed, 22 Nov 2023 10:52:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700679135; cv=none; d=google.com; s=arc-20160816; b=LXvocbEeGO5HMShZElhCuHGpP5frPFCGyfPBBS1sg0eJobSFVi3dp+0LACWlvpIpvx Kk3aPP1llJB8aXNbIUR7E1bikm+E1WK7Kj2KdPkFoJOgY1/yQbkTpvtz9Oy1yyKOcfGs bmH2LYXPzZVdDpXY04R9ah2qCFIkTYR4WTy4X1cT2wPN9E3yJNtxUrZ/MqMPwM9hUPuV E4K3lNchLWm3K7sNkXI7/nyr7CmLBGadNOorn/PGMucm7eRWweeYY3gbVFD+QzDAjN3h ti0gBB7+nm0TE5Sj7tqJPBL8nhseb3oPQCQ/xXPkbes2WokRpuTAng79aTjOkwJK7SP5 4BBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=/O6YV3kiLuD8Z6x7aOIhMrFlk5OJzozFlBrzBd86TN0=; fh=JY/d/GcSfgBtIwQQNwbIRX3y1rOCYJK4pcKMBkeJSdk=; b=DhB7anjZ+RTOFo2aXRp18py6zNVDqqofPlQWbH36wJfo8ob18DSA2y86BcrRJA/QWy IbWv3m7Yoibdpr+hBy8DSdHjFzaya5Sso9IVT1vD53KP5Jj5zY12m5obiJhBkxq/pRAZ eEiHCXU/lAUF4diLp6VH7IIS4p3PdRNUcVk37XrtZz8PnfC05egkJbJcHXJ9ESh9H0FQ l0cd3x7fLRZWk3OKZ4xBcxovaVtEmOS7+LQSbFL2oFBcU8fdAOwRE8+ANC2QNRvyKOU7 x/zfJchsSWmpRHzrLMDloh3bMkYDAbNQW0sHsrJFX8F8o92H1zfmjNq74b1bMP5bpQFw bLuQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id t11-20020a63460b000000b005c16f26b1b4si31793pga.440.2023.11.22.10.52.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 10:52:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id DB5C181C00DB; Wed, 22 Nov 2023 10:52:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbjKVSvr convert rfc822-to-8bit (ORCPT + 99 others); Wed, 22 Nov 2023 13:51:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231470AbjKVSvp (ORCPT ); Wed, 22 Nov 2023 13:51:45 -0500 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EC5A1BD; Wed, 22 Nov 2023 10:51:41 -0800 (PST) Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2839b418f7fso101042a91.2; Wed, 22 Nov 2023 10:51:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700679101; x=1701283901; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cZiYC/0re0S8gsuuO/+DoD+3NQYK4EAqlRLV/1GjVXs=; b=w7yASbpYw9fHlVhhsr7Un9QEAAMuBkeIPrXknemFXdTxDcHWs4v06b6SCQMbXay7o4 4KdCVSI91TEOng/VhL1zgpvH/fRRxQhDv0+U9XlHL2KB6xujS7Zo+kINnWKYmKQxr5bG yOtwFTAbMhmMiuFkvhItwnnyBQadMnW6v+NHO/60hUYlJzaMGt37vwm491yeGr2OLxYV 7i1+SRrF4Xj0VIym7nxNLOYxBesWLRXadlD8moJ4KGLYuumrB1IGC0z8UrKJwiAiBOsf YnnvA88tjSSbj4v8wcgHS1eEmffGOROZBh6V9kCgYjuS0BfUZN5D+GO893kPcRMr1R4q JjSA== X-Gm-Message-State: AOJu0YzMXq72pSJOgwvtCTN62waQ2dJ8zg/lM8BOxzYhf8fSL+zIQSwO Or8EhQ6ciCrd0FfrYMVDczup7R39XcLqn4Csaw0= X-Received: by 2002:a17:90b:1648:b0:27d:1f5c:22cb with SMTP id il8-20020a17090b164800b0027d1f5c22cbmr3209201pjb.20.1700679100643; Wed, 22 Nov 2023 10:51:40 -0800 (PST) MIME-Version: 1.0 References: <20231122155548.2449-1-p4ranlee@gmail.com> In-Reply-To: <20231122155548.2449-1-p4ranlee@gmail.com> From: Namhyung Kim Date: Wed, 22 Nov 2023 10:51:28 -0800 Message-ID: Subject: Re: [PATCH] perf callchain: Support riscv cross-platform To: Paran Lee Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Sean Christopherson , Li Dong , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, shjy180909@gmail.com, austindh.kim@gmail.com, honggyu.kp@gmail.com, tegongkang@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 22 Nov 2023 10:52:12 -0800 (PST) Hi Paran, On Wed, Nov 22, 2023 at 7:56 AM Paran Lee wrote: > > Support riscv cross platform callchain unwind. > Tested on RISCV 64 Starfive VisionFive2 Board > ------------ > $ uname -ra > Linux starfive 5.15.0-starfive # > 1 SMP Mon Dec 19 07:56:37 EST 2022 riscv64 GNU/Linux > paran@starfive:~/linux-next$ cat /etc/os-release > PRETTY_NAME="Debian GNU/Linux bookworm/sid" > NAME="Debian GNU/Linux" > ID=debian > HOME_URL="https://www.debian.org/" > SUPPORT_URL="https://www.debian.org/support" > BUG_REPORT_URL="https://bugs.debian.org/" > ------------ > paran@starfive:~/linux-next/tools/perf$ > make NO_AUXTRACE=1 NO_LIBTRACEEVENT=1 -j4 > Auto-detecting system features: > ... dwarf: [ on ] > ... dwarf_getlocations: [ on ] > ... glibc: [ on ] > ... libbfd: [ on ] > ... libbfd-buildid: [ on ] > ... libcap: [ on ] > ... libelf: [ on ] > ... libnuma: [ on ] > ... numa_num_possible_cpus: [ on ] > ... libperl: [ on ] > ... libpython: [ on ] > ... libcrypto: [ on ] > ... libunwind: [ on ] > ... libdw-dwarf-unwind: [ on ] > ... zlib: [ on ] > ... lzma: [ on ] > ... get_cpuid: [ OFF ] > ... bpf: [ on ] > ... libaio: [ on ] > ... libzstd: [ on ] > ... > CC arch/riscv/util/dwarf-regs.o > CC arch/riscv/util/unwind-libunwind.o > ... > LINK perf > ------------ > Signed-off-by: Paran Lee > --- > tools/build/Makefile.feature | 2 + > tools/build/feature/Makefile | 8 ++++ > tools/perf/Makefile.config | 18 +++++++++ > tools/perf/arch/riscv/util/Build | 1 + > tools/perf/arch/riscv/util/unwind-libunwind.c | 17 ++++++++ > tools/perf/check-headers.sh | 3 ++ > tools/perf/util/libunwind/riscv.c | 40 +++++++++++++++++++ > tools/perf/util/unwind-libunwind.c | 5 +++ > 8 files changed, 94 insertions(+) > create mode 100644 tools/perf/arch/riscv/util/unwind-libunwind.c > create mode 100644 tools/perf/util/libunwind/riscv.c > > diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature > index 934e2777a2db..e4140dee1d70 100644 > --- a/tools/build/Makefile.feature > +++ b/tools/build/Makefile.feature > @@ -93,9 +93,11 @@ FEATURE_TESTS_EXTRA := \ > libunwind-x86_64 \ > libunwind-arm \ > libunwind-aarch64 \ > + libunwind-riscv \ > libunwind-debug-frame \ > libunwind-debug-frame-arm \ > libunwind-debug-frame-aarch64 \ > + libunwind-debug-frame-riscv \ > cxx \ > llvm \ > llvm-version \ > diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile > index dad79ede4e0a..e3218f98f824 100644 > --- a/tools/build/feature/Makefile > +++ b/tools/build/feature/Makefile > @@ -45,8 +45,10 @@ FILES= \ > test-libunwind-x86_64.bin \ > test-libunwind-arm.bin \ > test-libunwind-aarch64.bin \ > + test-libunwind-riscv.bin \ > test-libunwind-debug-frame-arm.bin \ > test-libunwind-debug-frame-aarch64.bin \ > + test-libunwind-debug-frame-riscv.bin \ Whitespace damaged? Also I think you need to add the source files for the feature checks. Thanks, Namhyung > test-pthread-attr-setaffinity-np.bin \ > test-pthread-barrier.bin \ > test-stackprotector-all.bin \ > @@ -189,12 +191,18 @@ $(OUTPUT)test-libunwind-arm.bin: > $(OUTPUT)test-libunwind-aarch64.bin: > $(BUILD) -lelf -lunwind-aarch64 > > +$(OUTPUT)test-libunwind-riscv.bin: > + $(BUILD) -lelf -lunwind-riscv > + > $(OUTPUT)test-libunwind-debug-frame-arm.bin: > $(BUILD) -lelf -lunwind-arm > > $(OUTPUT)test-libunwind-debug-frame-aarch64.bin: > $(BUILD) -lelf -lunwind-aarch64 > > +$(OUTPUT)test-libunwind-debug-frame-riscv.bin: > + $(BUILD) -lelf -lunwind-riscv > + > $(OUTPUT)test-libaudit.bin: > $(BUILD) -laudit > > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config > index b3e6ed10f40c..b72c1b886b28 100644 > --- a/tools/perf/Makefile.config > +++ b/tools/perf/Makefile.config > @@ -88,6 +88,8 @@ endif > > ifeq ($(SRCARCH),riscv) > NO_PERF_REGS := 0 > + CFLAGS += -I$(OUTPUT)arch/riscv/include/generated > + LIBUNWIND_LIBS = -lunwind -lunwind-riscv > endif > > ifeq ($(SRCARCH),csky) > @@ -147,6 +149,7 @@ FEATURE_CHECK_LDFLAGS-libunwind-debug-frame = $(LIBUNWIND_LDFLAGS) $(LIBUNWIND_L > > FEATURE_CHECK_LDFLAGS-libunwind-arm += -lunwind -lunwind-arm > FEATURE_CHECK_LDFLAGS-libunwind-aarch64 += -lunwind -lunwind-aarch64 > +FEATURE_CHECK_LDFLAGS-libunwind-riscv += -lunwind -lunwind-riscv > FEATURE_CHECK_LDFLAGS-libunwind-x86 += -lunwind -llzma -lunwind-x86 > FEATURE_CHECK_LDFLAGS-libunwind-x86_64 += -lunwind -llzma -lunwind-x86_64 > > @@ -651,6 +654,21 @@ ifndef NO_LIBUNWIND > endif > endif > > + $(call feature_check,libunwind-riscv) > + ifeq ($(feature-libunwind-riscv), 1) > + $(call detected,CONFIG_LIBUNWIND_RISCV) > + CFLAGS += -DHAVE_LIBUNWIND_RISCV_SUPPORT > + LDFLAGS += -lunwind-riscv > + EXTLIBS_LIBUNWIND += -lunwind-riscv > + have_libunwind = 1 > + $(call feature_check,libunwind-debug-frame-riscv) > + ifneq ($(feature-libunwind-debug-frame-riscv), 1) > + msg := $(warning No debug_frame support found in libunwind-riscv); > + CFLAGS += -DNO_LIBUNWIND_DEBUG_FRAME_riscv > + endif > + endif > + > + > ifneq ($(feature-libunwind), 1) > msg := $(warning No libunwind found. Please install libunwind-dev[el] >= 1.1 and/or set LIBUNWIND_DIR); > NO_LOCAL_LIBUNWIND := 1 > diff --git a/tools/perf/arch/riscv/util/Build b/tools/perf/arch/riscv/util/Build > index 603dbb5ae4dc..0f75e25ae638 100644 > --- a/tools/perf/arch/riscv/util/Build > +++ b/tools/perf/arch/riscv/util/Build > @@ -2,4 +2,5 @@ perf-y += perf_regs.o > perf-y += header.o > > perf-$(CONFIG_DWARF) += dwarf-regs.o > +perf-$(CONFIG_LOCAL_LIBUNWIND) += unwind-libunwind.o > perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o > diff --git a/tools/perf/arch/riscv/util/unwind-libunwind.c b/tools/perf/arch/riscv/util/unwind-libunwind.c > new file mode 100644 > index 000000000000..e9d6c9c06f84 > --- /dev/null > +++ b/tools/perf/arch/riscv/util/unwind-libunwind.c > @@ -0,0 +1,17 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > + > +#ifndef REMOTE_UNWIND_LIBUNWIND > +#include > +#include "perf_regs.h" > +#include "../../../util/unwind.h" > +#endif > +#include "../../../util/debug.h" > + > +int LIBUNWIND__ARCH_REG_ID(int regnum) > +{ > + if (regnum < 0 || regnum >= PERF_REG_RISCV_MAX) > + return -EINVAL; > + > + return regnum; > +} > diff --git a/tools/perf/check-headers.sh b/tools/perf/check-headers.sh > index 66ba33dbcef2..3a774c9544c4 100755 > --- a/tools/perf/check-headers.sh > +++ b/tools/perf/check-headers.sh > @@ -48,6 +48,7 @@ FILES=( > "arch/loongarch/include/uapi/asm/perf_regs.h" > "arch/mips/include/uapi/asm/perf_regs.h" > "arch/powerpc/include/uapi/asm/perf_regs.h" > + "arch/riscv/include/uapi/asm/perf_regs.h" > "arch/s390/include/uapi/asm/perf_regs.h" > "arch/x86/include/uapi/asm/perf_regs.h" > "arch/x86/include/uapi/asm/kvm.h" > @@ -67,6 +68,8 @@ FILES=( > "arch/mips/include/uapi/asm/errno.h" > "arch/parisc/include/uapi/asm/errno.h" > "arch/powerpc/include/uapi/asm/errno.h" > + "arch/riscv/include/uapi/asm/kvm.h" > + "arch/riscv/include/uapi/asm/unistd.h" > "arch/sparc/include/uapi/asm/errno.h" > "arch/x86/include/uapi/asm/errno.h" > "include/asm-generic/bitops/arch_hweight.h" > diff --git a/tools/perf/util/libunwind/riscv.c b/tools/perf/util/libunwind/riscv.c > new file mode 100644 > index 000000000000..c340c5609161 > --- /dev/null > +++ b/tools/perf/util/libunwind/riscv.c > @@ -0,0 +1,40 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * This file setups defines to compile arch specific binary from the > + * generic one. > + * > + * The function 'LIBUNWIND__ARCH_REG_ID' name is set according to arch > + * name and the definition of this function is included directly from > + * 'arch/riscv/util/unwind-libunwind.c', to make sure that this function > + * is defined no matter what arch the host is. > + * > + * Finally, the arch specific unwind methods are exported which will > + * be assigned to each riscv thread. > + */ > + > +#define REMOTE_UNWIND_LIBUNWIND > + > +/* Define arch specific functions & regs for libunwind, should be > + * defined before including "unwind.h" > + */ > +#define LIBUNWIND__ARCH_REG_ID(regnum) libunwind__riscv_reg_id(regnum) > + > +#include "unwind.h" > +#include "libunwind-riscv.h" > +#define perf_event_riscv_regs perf_event_riscv64_regs > +#include <../../../arch/riscv/include/uapi/asm/perf_regs.h> > +#undef perf_event_riscv_regs > +#include "../../arch/riscv/util/unwind-libunwind.c" > + > +/* NO_LIBUNWIND_DEBUG_FRAME is a feature flag for local libunwind, > + * assign NO_LIBUNWIND_DEBUG_FRAME_RISCV64 to it for compiling riscv > + * unwind methods. > + */ > +#undef NO_LIBUNWIND_DEBUG_FRAME > +#ifdef NO_LIBUNWIND_DEBUG_FRAME_RISCV > +#define NO_LIBUNWIND_DEBUG_FRAME > +#endif > +#include "util/unwind-libunwind-local.c" > + > +struct unwind_libunwind_ops * > +riscv_unwind_libunwind_ops = &_unwind_libunwind_ops; > diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c > index 76cd63de80a8..52190f1eaf0d 100644 > --- a/tools/perf/util/unwind-libunwind.c > +++ b/tools/perf/util/unwind-libunwind.c > @@ -11,6 +11,7 @@ > struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops; > struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops; > struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops; > +struct unwind_libunwind_ops __weak *riscv_unwind_libunwind_ops; > > static void unwind__register_ops(struct maps *maps, struct unwind_libunwind_ops *ops) > { > @@ -54,6 +55,10 @@ int unwind__prepare_access(struct maps *maps, struct map *map, bool *initialized > if (dso_type == DSO__TYPE_64BIT) > ops = arm64_unwind_libunwind_ops; > } > + } else if (!strcmp(arch, "riscv64") || !strcmp(arch, "riscv")) { > + if (dso_type == DSO__TYPE_64BIT) > + ops = riscv_unwind_libunwind_ops; > + } > > if (!ops) { > pr_warning_once("unwind: target platform=%s is not supported\n", arch); > -- > 2.25.1 >