From nobody Sun May 19 13:34:35 2024 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 44E6814B06B for ; Thu, 25 Apr 2024 19:39:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714073999; cv=none; b=Na0YMntQDjC5p2F4CKmauSVsXt+PDy6FbO9AyOTMFmI/fjWSyHUMpEKxz27AwE1HzgkiCkVx6mws0O8Py3Q2/0EYqCNUE5Zx0ZaqGDqOVp4aUv/WiCglaJ6d6YIJ0M3zrGyldF3UPLEZAr2iEMUnn5CJD5VMKSbre24RRdK1uWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714073999; c=relaxed/simple; bh=syWSVYZk+UhguXN13SKZzHMRwdHzGaDls4FDqD0fJbg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LwaV8tC3zw7ykzUTpJqI8g04Ngy1+XKgufRurzeWKhdXVTPwy2Gz/j71PMO3P7Jmi0cE5YzndNj6Q3w11SZX6QDHdDm8hZfxFnVJwmHaC85YoHwWxPLqKNJBINtgv7pg5bRBB08YBxwJo1JBy2F3zt+kiIBBpxXyoSBSKZ3GxCs= 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=jTHaAyMn; arc=none smtp.client-ip=209.85.210.180 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="jTHaAyMn" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6ed112c64beso1401378b3a.1 for ; Thu, 25 Apr 2024 12:39:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714073997; x=1714678797; 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=jwQycTSydr72l3rI7Ia2qnujI4hDZcXzi79vpal+Gok=; b=jTHaAyMn8khsO93P7lYb4cvVv9zo0XPxBoYAu0nyVKp+yEWaV3MMkD3X5WV4MzR7No umFytCNt37dzXluEub5yuBD6PJxOVCQRccwjaHDfHbgNUx5DEm43XEFLWpcPo3Ifn68x LvclCc4CatQ+Awi9gq6qYm/hS0qMkvUPUCf98fSUKe0J1t6ss+7VvFCd7i36QpYgP3oP 8rGyxBL6AfWV6kv+1S3k6pc8O3Yut4uO4SbM99AeU9Knfdz9hNCA1Ex17HQet+ThIpmD Oy8AHqQW4SmvvkT0XLsOKEqqm8+8RqzuJgmb+YD8dKH1UqTV+lLzQvYHynLdAgVdeR5M bSVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714073997; x=1714678797; 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=jwQycTSydr72l3rI7Ia2qnujI4hDZcXzi79vpal+Gok=; b=kGrTX+TEfo3PFfkrYPWzLF45+r8y5i4o/km859gGwYnOWo1XMjnL1F7359vrIBKm4K E58b1ULfVM69NP9KRG8ajAvcWgGTLAo2FMQweZQIwRrAaIMOG4HJndU1uqGw0Lg3LYYw WcUO9YzrhZ970gcPosy/2bS1l+EMG5i5wofp3DvkFNx6uMZSm23laHOg+35PZdP8XPxd uUtwlSlDqD1CMeclhloKAyY5DaKqLxkp+zlLwvyVGrVL3IR7X9XcWwRcJtOvESiIpIqK dd9WvuVAy1AwvTMKlPjMN0qHIcl8G/n9YKyk+1835wnBFWU3q6xnKY6K5HyXT9F5LK+e keaQ== X-Gm-Message-State: AOJu0YwD7bJnUttI2K+cI2VJfYTfZmkUnpSgUE//QV9IlzX+wnMDT+5m TaOW5yJ4detGPoFRzqJ8JBikxRuTJuXH6+pKKODw3/ll8uPvqEtEDc/hqrWMnjsgkJSIdf/OrI/ t X-Google-Smtp-Source: AGHT+IEthZSW1CX7a3PvofFEwe48ZsCzJG8DzRSx1GcFLQyxF4hEvHFisHhdU+teFKmXklSnLYrqaQ== X-Received: by 2002:a05:6a00:1704:b0:6eb:3d37:ce7a with SMTP id h4-20020a056a00170400b006eb3d37ce7amr979454pfc.21.1714073996665; Thu, 25 Apr 2024 12:39:56 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 12:39:56 -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 1/2] drivers/perf: riscv: Remove the warning from stop function Date: Thu, 25 Apr 2024 16:29:31 -0700 Message-Id: <20240425232933.4111680-2-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> 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" The warning message was initially added just to indicate that counter stop function is being called while the event is already stopped. However, we update the state to stopped case now in an overflow handler after stopping the counter. If there is another child overflow handler is registered (e.g kvm) it may call stop again which will trigger the warning. Fixes : commit 22f5dac41004d ("drivers/perf: riscv: Implement SBI PMU snaps= hot function") Signed-off-by: Atish Patra Reviewed-by: Samuel Holland --- drivers/perf/riscv_pmu.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c index 36d348753d05..78c490e0505a 100644 --- a/drivers/perf/riscv_pmu.c +++ b/drivers/perf/riscv_pmu.c @@ -191,8 +191,6 @@ void riscv_pmu_stop(struct perf_event *event, int flags) struct hw_perf_event *hwc =3D &event->hw; struct riscv_pmu *rvpmu =3D to_riscv_pmu(event->pmu); =20 - WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); - if (!(hwc->state & PERF_HES_STOPPED)) { if (rvpmu->ctr_stop) { rvpmu->ctr_stop(event, 0); --=20 2.34.1 From nobody Sun May 19 13:34:35 2024 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 8B64E15251C for ; Thu, 25 Apr 2024 19:39:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714074001; cv=none; b=o15MP6yiI2qZDb4TppW37yJp9HAMO/vVCjkVF36f+/Smuv9YyFlOGidvDCN+daRlPjNDgio9KQbGmdPDqHej91Bpo4oeZ2AcnZj5gh7GhB75LNLMJeoVHH/gQgosKG6H2UYzlLdAFFPtW0skEBooNfZmUfMpvul7N9dq8UIENS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714074001; c=relaxed/simple; bh=eonC85eoFtBJtpDL59x2N+cpTWUJEFnuEMfzaBkKfbA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VhibZs+qYo7CJiYLkhkPQP2/hmr2vz9zlZrOE43vl7PQz2sVdx5X1itKHZwU1oX1NKvggJUnfJEJpsWYB63LxV0pCSa/Gx1lTLL7oec8Qo7GRBMrOGV3P3DvSltZ1JIbSKfbTBIvAWipTgQDo67ho4B2aVsQemLdrEBEOPq2bYg= 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=upL2gC1L; arc=none smtp.client-ip=209.85.210.175 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="upL2gC1L" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6ecec796323so1500971b3a.3 for ; Thu, 25 Apr 2024 12:39:59 -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=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=FuEzACDgvxR/jlPdG6IpUjcsXiIpoPyyUaqA1tLQdL8=; b=upL2gC1L8CFSqUuAPyKO3LEYl+hZzyh/EtbUL57j/HmavW4/e3cAvek43PkBql8iHa XdJw3djlCml+IQdFbF/tAbHy+vy0UOlsye6N2tcbbm+oIaOArEX5OFD8TKBshJY76WSc xjptYUd6tgTFxM0GyomxKYXz1ma/2g3lZB1+Pc7OG4a1y6uAj8P6gDvTFvJJibk6tHuU utagmLn40wGJHYRVI02Sb9nv9wsA8Caua8kf7phCollEcZvFKE36t1Ro6LVxV6nRi/Bx 06kbZv4JjveSU5lFSD1My0n6hcZq06n/kKsz11IlioFwhwOmIi5ePXrATt4DSNZpkDq8 Skvg== 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=ItV7ibO+Tt83LOB78Go0gJIqvTqDMZF2C9R92x4wwv9Ivtv7s740Jtr6JmXplNE0NN QYEEnOGuEUWq0LeVaHACGkJBpJNsu9PukLecJTQtbCL3GzWQJ5TlfwcF28GygDFE77WC ipwZIvrFT10CS0AFG65ey4c8UQ3EcXj/f1HLYb2qE8JPnDMXlCkhzcXPHtnjdetTCLzH eCjIpxIlTTHhD+8dXMO+s0CxWDF7HJG6GmnaGii2hmYmMiOtmVDg9bAJtI/jwzzD5Lzt 2Sw6uiAptrcPE9y+O8GjK6ACvReFhqsSU5PEqBXClen+nhSmOY6nzA4+nukh+6gmUov3 sYNA== X-Gm-Message-State: AOJu0Yx9DIHQhSwGRi7FM6rgWVXx/JRC1rdJIdlzkIX1IMbIIxL16rFB wV3CTr4gc1d7xXpjjvyKcp3d8BJCBxe40BhcCp4cg8gnjaeovMNZKvV6sUHDou97IZ2kljn8dPf 0 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> 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" 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 snaps= hot 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); } =20 + memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNT= ERS); cpu_hw_evt->snapshot_set_done =3D true; =20 return 0; @@ -605,7 +606,7 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event) union sbi_pmu_ctr_info info =3D pmu_ctr_list[idx]; =20 /* Read the value from the shared memory directly only if counter is stop= ped */ - if (sbi_pmu_snapshot_available() & (hwc->state & PERF_HES_STOPPED)) { + if (sbi_pmu_snapshot_available() && (hwc->state & PERF_HES_STOPPED)) { val =3D 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 =3D this_cpu_ptr(pmu->hw_events); struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_addr; unsigned long flag =3D 0; - int i; + int i, idx; struct sbiret ret; - unsigned long temp_ctr_values[64] =3D {0}; - unsigned long ctr_val, temp_ctr_overflow_mask =3D 0; + u64 temp_ctr_overflow_mask =3D 0; =20 if (sbi_pmu_snapshot_available()) flag =3D SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; =20 + /* Reset the shadow copy to avoid save/restore any value from previous ov= erflow */ + memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNT= ERS); + for (i =3D 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) { /* No need to check the error here as we can't do anything about the err= or */ ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, i * BITS_PER_LO= NG, 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] =3D sdata->ctr_values[i]; - /* Save the overflow mask to avoid clobbering */ - if (BIT(i) & sdata->ctr_overflow_mask) - temp_ctr_overflow_mask |=3D 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] =3D + sdata->ctr_values[idx]; + /* Save the overflow mask to avoid clobbering */ + if (BIT(idx) & sdata->ctr_overflow_mask) + temp_ctr_overflow_mask |=3D BIT(idx + i * BITS_PER_LONG); + } } } =20 - /* 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 =3D 0; i < 64; i++) { - ctr_val =3D temp_ctr_values[i]; - if (ctr_val) - sdata->ctr_values[i] =3D ctr_val; - if (temp_ctr_overflow_mask) - sdata->ctr_overflow_mask =3D temp_ctr_overflow_mask; - } + for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) + sdata->ctr_values[idx] =3D cpu_hw_evt->snapshot_cval_shcopy[idx]; + if (temp_ctr_overflow_mask) + sdata->ctr_overflow_mask =3D temp_ctr_overflow_mask; } } =20 @@ -850,7 +853,7 @@ static inline void pmu_sbi_start_ovf_ctrs_sbi(struct cp= u_hw_events *cpu_hw_evt, static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *c= pu_hw_evt, u64 ctr_ovf_mask) { - int idx =3D 0; + int i, idx =3D 0; struct perf_event *event; unsigned long flag =3D SBI_PMU_START_FLAG_INIT_SNAPSHOT; u64 max_period, init_val =3D 0; @@ -863,7 +866,7 @@ static inline void pmu_sbi_start_ovf_ctrs_snapshot(stru= ct cpu_hw_events *cpu_hw_ hwc =3D &event->hw; max_period =3D riscv_pmu_ctr_get_width_mask(event); init_val =3D local64_read(&hwc->prev_count) & max_period; - sdata->ctr_values[idx] =3D init_val; + cpu_hw_evt->snapshot_cval_shcopy[idx] =3D 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(st= ruct cpu_hw_events *cpu_hw_ */ } =20 - for (idx =3D 0; idx < BITS_TO_LONGS(RISCV_MAX_COUNTERS); idx++) { + for (i =3D 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] =3D + 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); } } =20 @@ -898,7 +905,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *d= ev) int lidx, hidx, fidx; struct riscv_pmu *pmu; struct perf_event *event; - unsigned long overflow; + u64 overflow; u64 overflowed_ctrs =3D 0; struct cpu_hw_events *cpu_hw_evt =3D dev; u64 start_clock =3D 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 multipl= e SBI calls */ + u64 snapshot_cval_shcopy[RISCV_MAX_COUNTERS]; }; =20 struct riscv_pmu { --=20 2.34.1