From patchwork Thu Apr 25 23:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 1927875 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=FqxFHTNz; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=DLb/zUHW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VQR5p1D0cz23tj for ; Fri, 26 Apr 2024 05:40:13 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JPjE2xK9cXxtrnivmit99quA7o6xhHgwywNWbZ2d1L8=; b=FqxFHTNz1SFDp2 qvhQnFdtbOrJUw6ddAaV2eQrehm0OpkPij3AWVMNrfQrfwoOCrMP/9+iTu1KzfATNiU5G7Mj/F53w Ne+4h9sD1yFU5MN5pC8zbkfd6NMmx5Nu6jKumz7RRHYuwCiKS+YJ4vAyl0bFS2W/XHS5VSG2ZpQqy CP/2UoeJJVrpEpQvNNoT2DEWnJX4gwZqyeDM39++ta3Fx+vY4sb4EiG+HfJYK8Ee6rWF/xU9w6gZA 7v/xzjhRRKluEfskKxO6zQb+MDNBrFPvHdKMfV3ov6itOG3uM5jbCAapgk8rOrjawvSYmrS4996fW m7wZXOUfJiuaZQG2mHQQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s04ww-00000009uzb-2Aj5; Thu, 25 Apr 2024 19:40:06 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s04wp-00000009uxI-2RP5 for kvm-riscv@lists.infradead.org; Thu, 25 Apr 2024 19:40:00 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6ed627829e6so1673998b3a.1 for ; Thu, 25 Apr 2024 12:39:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714073998; x=1714678798; darn=lists.infradead.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=FuEzACDgvxR/jlPdG6IpUjcsXiIpoPyyUaqA1tLQdL8=; b=DLb/zUHW7HiYaNpXTiLvLxlPTcR79IHszBI8FzZ30CRvh6HuCljtmNqRM7X3/HNLBT ttyN7jHVb5WY4+3Hglf/6NVUDhXXqZ/yRKiHq5gpWO6YEtpZUR6OuAYA1/ucBP8oOumO gJiIiP6+XWb1u9fJYWSltPeAUds/kU1bk75YtXEt6DAgXsGk7A/l1S1s8TnZ5VscROEp bxCSZOVzU4Qdnl/kkygK+2+CX28O3VlIVVh0Yyd75Cs24+c8cE73e2PW+jSIb0Rx53Sh tn5WBnmPnDgs9K98KJ8h0fDpWgXHJtCN3vGiHO6cPfmvqcdKNlsTwKPMdaA2AqaJ0zT2 DB9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714073998; x=1714678798; 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=FuEzACDgvxR/jlPdG6IpUjcsXiIpoPyyUaqA1tLQdL8=; b=XmvhE5MmMUsUfAobggGc473MkJ2LyuYaaFcev+FGSu81QyjxW7rAytmbvp+pNn1b9P LN7tuQ6urDLyiKzrR3OdiY66uF7tkpxy+iZCQm3h5kOmhzk9W6m98x/Qyllj8QLcnTer yF61dKxTDH8NY0MX7QV+lMdTX2rXkI2CCscgn9MELaYlvhEaKKT9QuSj0CceO/O+sdL/ 5wIwF4Tz1psxY6xUWQ+chGEMrnIMbma8KY67qz2A/RZC8KBKrJxSQ7HriBBxhG9Fx8fY drFWRUbyvbKPNACEbm9nC72h0gvTtrggm5wOkPyjS+vqikiFWMkB3U5kNifD9zTV3u8t Pl5Q== X-Forwarded-Encrypted: i=1; AJvYcCWIrFHLL5yTeFHhAVyvk/1WboXheI60iyiLDwMqgI2iQ7Vn7qA6U6Ig0Vl60r49n4lgH95My1NApsVAcjgKadhKR+GvavRCH462oPGatQ== X-Gm-Message-State: AOJu0Ywb4Lf3OtsFX/RqyAIx2lAoFMn538ve+fynY7JyF3FeP9jLfKXy T9L/ZlAsZapJ2L28GJ18KTcP4Ou8Ia0CAZgjLgCROGQhgFF0LgF/8VcPlQTruNv+PzL4alI3/qj C X-Google-Smtp-Source: AGHT+IHWwmGuvBVQMoylwvzXz3CnnNRk0Jt2cal/7gbWtOpzPMnzFmO8NffRKGYa6qFriuIqRhnFMA== X-Received: by 2002:a05:6a20:dc95:b0:1aa:9595:4261 with SMTP id ky21-20020a056a20dc9500b001aa95954261mr722289pzb.22.1714073998176; Thu, 25 Apr 2024 12:39:58 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id fk12-20020a056a003a8c00b006e6a684a6ddsm13525487pfb.220.2024.04.25.12.39.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 12:39:57 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org, Mark Rutland , Samuel Holland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [kvm-riscv/for-next 2/2] drivers/perf: riscv: Fix RV32 snapshot overflow use case Date: Thu, 25 Apr 2024 16:29:32 -0700 Message-Id: <20240425232933.4111680-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425232933.4111680-1-atishp@rivosinc.com> References: <20240425232933.4111680-1-atishp@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_123959_697070_ABEC25C7 X-CRM114-Status: GOOD ( 24.19 ) X-Spam-Score: 2.4 (++) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The shadow copy alogirthm is implemented incorrectly. This patch fixes the behavior by keeping a per cpu shadow copy of the counter values to avoid clobbering for the cases where system more than XLEN [...] Content analysis details: (2.4 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:436 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 2.4 DATE_IN_FUTURE_03_06 Date: is 3 to 6 hours after Received: date X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The shadow copy alogirthm is implemented incorrectly. This patch fixes the behavior by keeping a per cpu shadow copy of the counter values to avoid clobbering for the cases where system more than XLEN counters and the overflown counter index are beyond XLEN. This issue can only be observed only in RV32 if an SBI implementation assigns logical counters ids greater than XLEN or firmware counter overflow is supported in the future. Fixes : commit 22f5dac41004d ("drivers/perf: riscv: Implement SBI PMU snapshot function") Signed-off-by: Atish Patra Reviewed-by: Samuel Holland --- drivers/perf/riscv_pmu_sbi.c | 49 +++++++++++++++++++--------------- include/linux/perf/riscv_pmu.h | 2 ++ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 2694110f1cff..98aaeb13e9db 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -588,6 +588,7 @@ static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu) return sbi_err_map_linux_errno(ret.error); } + memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS); cpu_hw_evt->snapshot_set_done = true; return 0; @@ -605,7 +606,7 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event) union sbi_pmu_ctr_info info = pmu_ctr_list[idx]; /* Read the value from the shared memory directly only if counter is stopped */ - if (sbi_pmu_snapshot_available() & (hwc->state & PERF_HES_STOPPED)) { + if (sbi_pmu_snapshot_available() && (hwc->state & PERF_HES_STOPPED)) { val = sdata->ctr_values[idx]; return val; } @@ -769,36 +770,38 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events); struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr; unsigned long flag = 0; - int i; + int i, idx; struct sbiret ret; - unsigned long temp_ctr_values[64] = {0}; - unsigned long ctr_val, temp_ctr_overflow_mask = 0; + u64 temp_ctr_overflow_mask = 0; if (sbi_pmu_snapshot_available()) flag = SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; + /* Reset the shadow copy to avoid save/restore any value from previous overflow */ + memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS); + for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) { /* No need to check the error here as we can't do anything about the error */ ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, i * BITS_PER_LONG, cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0); if (!ret.error && sbi_pmu_snapshot_available()) { /* Save the counter values to avoid clobbering */ - temp_ctr_values[i * BITS_PER_LONG + i] = sdata->ctr_values[i]; - /* Save the overflow mask to avoid clobbering */ - if (BIT(i) & sdata->ctr_overflow_mask) - temp_ctr_overflow_mask |= BIT(i + i * BITS_PER_LONG); + for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG) { + cpu_hw_evt->snapshot_cval_shcopy[i * BITS_PER_LONG + idx] = + sdata->ctr_values[idx]; + /* Save the overflow mask to avoid clobbering */ + if (BIT(idx) & sdata->ctr_overflow_mask) + temp_ctr_overflow_mask |= BIT(idx + i * BITS_PER_LONG); + } } } - /* Restore the counter values to the shared memory */ + /* Restore the counter values to the shared memory for used hw counters */ if (sbi_pmu_snapshot_available()) { - for (i = 0; i < 64; i++) { - ctr_val = temp_ctr_values[i]; - if (ctr_val) - sdata->ctr_values[i] = ctr_val; - if (temp_ctr_overflow_mask) - sdata->ctr_overflow_mask = temp_ctr_overflow_mask; - } + for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) + sdata->ctr_values[idx] = cpu_hw_evt->snapshot_cval_shcopy[idx]; + if (temp_ctr_overflow_mask) + sdata->ctr_overflow_mask = temp_ctr_overflow_mask; } } @@ -850,7 +853,7 @@ static inline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt, static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_evt, u64 ctr_ovf_mask) { - int idx = 0; + int i, idx = 0; struct perf_event *event; unsigned long flag = SBI_PMU_START_FLAG_INIT_SNAPSHOT; u64 max_period, init_val = 0; @@ -863,7 +866,7 @@ static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_ hwc = &event->hw; max_period = riscv_pmu_ctr_get_width_mask(event); init_val = local64_read(&hwc->prev_count) & max_period; - sdata->ctr_values[idx] = init_val; + cpu_hw_evt->snapshot_cval_shcopy[idx] = init_val; } /* * We do not need to update the non-overflow counters the previous @@ -871,10 +874,14 @@ static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_ */ } - for (idx = 0; idx < BITS_TO_LONGS(RISCV_MAX_COUNTERS); idx++) { + for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) { + /* Restore the counter values to relative indices for used hw counters */ + for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG) + sdata->ctr_values[idx] = + cpu_hw_evt->snapshot_cval_shcopy[idx + i * BITS_PER_LONG]; /* Start all the counters in a single shot */ sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx * BITS_PER_LONG, - cpu_hw_evt->used_hw_ctrs[idx], flag, 0, 0, 0); + cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0); } } @@ -898,7 +905,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) int lidx, hidx, fidx; struct riscv_pmu *pmu; struct perf_event *event; - unsigned long overflow; + u64 overflow; u64 overflowed_ctrs = 0; struct cpu_hw_events *cpu_hw_evt = dev; u64 start_clock = sched_clock(); diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h index c3fa90970042..701974639ff2 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -45,6 +45,8 @@ struct cpu_hw_events { phys_addr_t snapshot_addr_phys; /* Boolean flag to indicate setup is already done */ bool snapshot_set_done; + /* A shadow copy of the counter values to avoid clobbering during multiple SBI calls */ + u64 snapshot_cval_shcopy[RISCV_MAX_COUNTERS]; }; struct riscv_pmu {