From nobody Tue Feb 10 02:49:43 2026 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31945243870 for ; Thu, 16 Jan 2025 23:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737069039; cv=none; b=Y/JSYNIVaQMYA8Ay3d/BggjgTLMPfWup2ZJOYx257HODfsskbCqKYq94FeHNM3e4Bpwg9hBpJIRp1U997ktN3PR5u09mYu4/cfCV89wTjGgaJdjZKnC09YfLLGuZlccar14XyzbKCr7MtViRHf/Npves5d0sUskXMqiu5AS+G+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737069039; c=relaxed/simple; bh=en3osisonvml/lgF+LtGxrhewyYz8KPgAh9/6KZlsO8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gth4Z/QWqy+nJKgPpesUNRVo7sVaLTGj8xM6dq3jubsXbLEpfethNrQGqZ+buuST1XQFHAymPNAegEFPD1KicjcmF9gUiZiwQjxZuIDXbrnzIPPFqopKriOwmNybAdDwOxnesUdJSGn9kCd+MQTThEbrxWmXtu3sy5RuSg6xh54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=VUoAzm2Z; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="VUoAzm2Z" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-38a8b35e168so1049906f8f.1 for ; Thu, 16 Jan 2025 15:10:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737069035; x=1737673835; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JEkq9sMQTY0nBcBSdfXe/oLw7i/Fvb8myFij4FR9Lmw=; b=VUoAzm2ZPy38FIWPeaUDpKC+1ZHxetZDnaegrk+X2uBBcrlykEurb9fZrtRAaH1/uW k93TnBFLucuoEBw/syfclvCGGWozMokAiJJ8DMWjZ43N34W+IG92whowbTpZaJwh7NYo Zy07trwIABOj0n5GnbjxyhqRzdnxt6timXFPX2Fs0tfplJCZ+qadOyva0/s9qwfSGT3R kOuz94LfqasSP4prLl57kmU9IyOYv0Hbs9X6HqihP8Xgt1oPi4FrI3ZJ3wNnqbLsLJYZ m5bXhcLDXpVuB8E4IEP8lptYZ4iTcThBHDnppUGxlRNw7a1EhQEDRDaTJINPgcABHALM tFUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737069035; x=1737673835; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JEkq9sMQTY0nBcBSdfXe/oLw7i/Fvb8myFij4FR9Lmw=; b=DS4OkoAlyho6Fp7lj3voL9E90/lrEHsSNhfP0whTtkBzAsDpRsA6jlYhOAmO2q87my jDdiHrf9CnpU5n9UwlPzFFogQpITW468fH+iGpIPLFrHkdslpPr6/ywMxbr47I6Z6Ps5 DKBTmnEJCLZkYEp+G+fpCeS2BtZLigEJ/DODxZXoe6Ok6g58rdOzwrwwbv5dwgUZTjae 70mzRgpgZ/Cy/8vvyQAdBxLWY7xoarwp4wrrnXawG5Dzlc8bP5dvqyPmR4yURKDUDhuc jMYJotCJ2X6teOAt4rK6PrKlyqLTdCnP0vL3vWl42W2h8LQ9B13YGnUjawkSkOtj6Pg4 xhNA== X-Gm-Message-State: AOJu0YwP2cB6p9YWM1KTnnbIrl9MIASbCBh+N36lAB6GJcL7tSuV8VFH 63RloqoxgbKldTW9X933s/NVWNk2H2JdOwNBNoX1GOpL+pZIcDf/R1ALWcKZAKMzhaMGGzoVUQ6 E X-Gm-Gg: ASbGncuYUQaPLpPihOYC15hfnvW677GhHd1kdLnRfYVeeIe838Y9Rsi5jPVtkiXIEBZ ozLcmDJCoB4OwRqY1np7R70CyprUUP88U4K3cyRsJ/TOjYQf8GOCgE4rY7OYtgqZWULNZJsIA2d 22039RMXl6oHXM56AcAizMqNdUVH4KBiWbZCrQC5N1pEe4Ra+45aK298lTd1odzQB8ESgWcFOJk GXYyStBWvyGGLHchMcdKqeqJuY7yXh+w9zlxaiwwYeg9a7dM9jmSmr8MmgjjHoMKni2R6m96OxA NhmrVR05u347sQPy X-Google-Smtp-Source: AGHT+IGuu6zWay2mqSvZNm4qasvF1tOh4RKXx4XbpxtExBqMV1QOAm0F3fvLQN9z0J/WkxXd0ISjQg== X-Received: by 2002:a05:6000:1548:b0:385:e328:8908 with SMTP id ffacd0b85a97d-38bf5b0b67bmr204983f8f.29.1737069035166; Thu, 16 Jan 2025 15:10:35 -0800 (PST) Received: from rkanwal-XPS-15-9520.uk.rivosinc.com ([2a02:c7c:75ac:6300:b3f2:3a24:1767:7db0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38bf322b337sm974991f8f.59.2025.01.16.15.10.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jan 2025 15:10:34 -0800 (PST) From: Rajnesh Kanwal To: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Cc: linux-perf-users@vger.kernel.org, adrian.hunter@intel.com, alexander.shishkin@linux.intel.com, ajones@ventanamicro.com, anup@brainfault.org, acme@kernel.org, atishp@rivosinc.com, beeman@rivosinc.com, brauner@kernel.org, conor@kernel.org, heiko@sntech.de, irogers@google.com, mingo@redhat.com, james.clark@arm.com, renyu.zj@linux.alibaba.com, jolsa@kernel.org, jisheng.teoh@starfivetech.com, palmer@dabbelt.com, will@kernel.org, kaiwenxue1@gmail.com, vincent.chen@sifive.com, Rajnesh Kanwal Subject: [PATCH v2 7/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver Date: Thu, 16 Jan 2025 23:09:55 +0000 Message-Id: <20250116230955.867152-8-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250116230955.867152-1-rkanwal@rivosinc.com> References: <20250116230955.867152-1-rkanwal@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This integrates recently added CTR ext support in riscv_pmu_dev driver to enable branch stack sampling using PMU events. This mainly adds CTR enable/disable callbacks in rvpmu_ctr_stop() and rvpmu_ctr_start() function to start/stop branch recording along with the event. PMU overflow handler rvpmu_ovf_handler() is also updated to sample CTR entries in case of the overflow for the particular event programmed to records branches. The recorded entries are fed to core perf for further processing. Signed-off-by: Rajnesh Kanwal --- drivers/perf/riscv_pmu_common.c | 3 +- drivers/perf/riscv_pmu_dev.c | 67 ++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/drivers/perf/riscv_pmu_common.c b/drivers/perf/riscv_pmu_commo= n.c index c4c4b5d6bed0..23077a6c4931 100644 --- a/drivers/perf/riscv_pmu_common.c +++ b/drivers/perf/riscv_pmu_common.c @@ -327,8 +327,7 @@ static int riscv_pmu_event_init(struct perf_event *even= t) u64 event_config =3D 0; uint64_t cmask; =20 - /* driver does not support branch stack sampling */ - if (has_branch_stack(event)) + if (needs_branch_stack(event) && !riscv_pmu_ctr_supported(rvpmu)) return -EOPNOTSUPP; =20 hwc->flags =3D 0; diff --git a/drivers/perf/riscv_pmu_dev.c b/drivers/perf/riscv_pmu_dev.c index b9b257607b76..10697deb1d26 100644 --- a/drivers/perf/riscv_pmu_dev.c +++ b/drivers/perf/riscv_pmu_dev.c @@ -1030,7 +1030,7 @@ static void rvpmu_sbi_ctr_stop(struct perf_event *eve= nt, unsigned long flag) static void pmu_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in) { - /* Call CTR specific Sched hook. */ + riscv_pmu_ctr_sched_task(pmu_ctx, sched_in); } =20 static int rvpmu_sbi_find_num_ctrs(void) @@ -1379,6 +1379,13 @@ static irqreturn_t rvpmu_ovf_handler(int irq, void *= dev) hw_evt->state |=3D PERF_HES_UPTODATE; perf_sample_data_init(&data, 0, hw_evt->last_period); if (riscv_pmu_event_set_period(event)) { + if (needs_branch_stack(event)) { + riscv_pmu_ctr_consume(cpu_hw_evt, event); + perf_sample_save_brstack( + &data, event, + &cpu_hw_evt->branches->branch_stack, NULL); + } + /* * Unlike other ISAs, RISC-V don't have to disable interrupts * to avoid throttling here. As per the specification, the @@ -1577,10 +1584,14 @@ static int rvpmu_deleg_ctr_get_idx(struct perf_even= t *event) =20 static void rvpmu_ctr_add(struct perf_event *event, int flags) { + if (needs_branch_stack(event)) + riscv_pmu_ctr_add(event); } =20 static void rvpmu_ctr_del(struct perf_event *event, int flags) { + if (needs_branch_stack(event)) + riscv_pmu_ctr_del(event); } =20 static void rvpmu_ctr_start(struct perf_event *event, u64 ival) @@ -1595,6 +1606,9 @@ static void rvpmu_ctr_start(struct perf_event *event,= u64 ival) if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) && (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT)) rvpmu_set_scounteren((void *)event); + + if (needs_branch_stack(event)) + riscv_pmu_ctr_enable(event); } =20 static void rvpmu_ctr_stop(struct perf_event *event, unsigned long flag) @@ -1617,6 +1631,9 @@ static void rvpmu_ctr_stop(struct perf_event *event, = unsigned long flag) } else { rvpmu_sbi_ctr_stop(event, flag); } + + if (needs_branch_stack(event) && flag !=3D RISCV_PMU_STOP_FLAG_RESET) + riscv_pmu_ctr_disable(event); } =20 static int rvpmu_find_ctrs(void) @@ -1652,6 +1669,9 @@ static int rvpmu_event_map(struct perf_event *event, = u64 *econfig) { u64 config1; =20 + if (needs_branch_stack(event) && !riscv_pmu_ctr_valid(event)) + return -EOPNOTSUPP; + config1 =3D event->attr.config1; if (riscv_pmu_cdeleg_available() && !pmu_sbi_is_fw_event(event) && !(config1 & RISCV_PMU_CONFIG1_GUEST_EVENTS)) { /* GUEST events rely o= n SBI encoding */ @@ -1701,6 +1721,8 @@ static int rvpmu_starting_cpu(unsigned int cpu, struc= t hlist_node *node) enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE); } =20 + riscv_pmu_ctr_starting_cpu(); + if (sbi_pmu_snapshot_available()) return pmu_sbi_snapshot_setup(pmu, cpu); =20 @@ -1715,6 +1737,7 @@ static int rvpmu_dying_cpu(unsigned int cpu, struct h= list_node *node) =20 /* Disable all counters access for user mode now */ csr_write(CSR_SCOUNTEREN, 0x0); + riscv_pmu_ctr_dying_cpu(); =20 if (sbi_pmu_snapshot_available()) return pmu_sbi_snapshot_disable(); @@ -1838,6 +1861,29 @@ static void riscv_pmu_destroy(struct riscv_pmu *pmu) cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node); } =20 +static int branch_records_alloc(struct riscv_pmu *pmu) +{ + struct branch_records __percpu *tmp_alloc_ptr; + struct branch_records *records; + struct cpu_hw_events *events; + int cpu; + + if (!riscv_pmu_ctr_supported(pmu)) + return 0; + + tmp_alloc_ptr =3D alloc_percpu_gfp(struct branch_records, GFP_KERNEL); + if (!tmp_alloc_ptr) + return -ENOMEM; + + for_each_possible_cpu(cpu) { + events =3D per_cpu_ptr(pmu->hw_events, cpu); + records =3D per_cpu_ptr(tmp_alloc_ptr, cpu); + events->branches =3D records; + } + + return 0; +} + static void rvpmu_event_init(struct perf_event *event) { /* @@ -1850,6 +1896,9 @@ static void rvpmu_event_init(struct perf_event *event) event->hw.flags |=3D PERF_EVENT_FLAG_USER_ACCESS; else event->hw.flags |=3D PERF_EVENT_FLAG_LEGACY; + + if (branch_sample_call_stack(event)) + event->attach_state |=3D PERF_ATTACH_TASK_DATA; } =20 static void rvpmu_event_mapped(struct perf_event *event, struct mm_struct = *mm) @@ -1997,6 +2046,15 @@ static int rvpmu_device_probe(struct platform_device= *pdev) pmu->pmu.attr_groups =3D riscv_cdeleg_pmu_attr_groups; else pmu->pmu.attr_groups =3D riscv_sbi_pmu_attr_groups; + + ret =3D riscv_pmu_ctr_init(pmu); + if (ret) + goto out_free; + + ret =3D branch_records_alloc(pmu); + if (ret) + goto out_ctr_finish; + pmu->cmask =3D cmask; pmu->ctr_add =3D rvpmu_ctr_add; pmu->ctr_del =3D rvpmu_ctr_del; @@ -2013,6 +2071,10 @@ static int rvpmu_device_probe(struct platform_device= *pdev) pmu->csr_index =3D rvpmu_csr_index; pmu->sched_task =3D pmu_sched_task; =20 + ret =3D cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node= ); + if (ret) + goto out_ctr_finish; + ret =3D riscv_pm_pmu_register(pmu); if (ret) goto out_unregister; @@ -2062,6 +2124,9 @@ static int rvpmu_device_probe(struct platform_device = *pdev) out_unregister: riscv_pmu_destroy(pmu); =20 +out_ctr_finish: + riscv_pmu_ctr_finish(pmu); + out_free: kfree(pmu); return ret; --=20 2.34.1