From nobody Sun May 19 15:05:09 2024 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 9001F156962 for ; Thu, 25 Apr 2024 23:48:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714088905; cv=none; b=gwxzjWY0EAuDV3HsArkfIhwl2nsIj/JrTpavhBZE5VOSLHNe5a431LNlj+ZlxOFfF0N6zLzS2a84Cgxj/WxpySQ0ZZyk2IV5bWVA9tI9JmkLsPf4Ub9lvnZwPiFO5bOeC1Fy6bPRCD+7gis/fFqNYmM4ISW+U2BLboGoG266TR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714088905; c=relaxed/simple; bh=lcrnHRD9ixghBZdzmzfMAtQdZt4DQgGAZC7L6LSo2/U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tKNbGJUEMJhwfcMTKwIAISrxfrCMsBKB6CbBOjwZldhFaJFKkOFGQHF6mivB62PY9VcaGJOEPFwPWnuri0cKt9igIixXRH6OJ5a0p+FhDmJo9aHOjZTS71P87G5W/vn1kT1bkq7EkON8Ra4nNeW0PdFzNZGqHHxOFV7Sr65oYg8= 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=N3uIFkfQ; arc=none smtp.client-ip=209.85.214.173 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="N3uIFkfQ" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1e2b1cd446fso12750605ad.3 for ; Thu, 25 Apr 2024 16:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714088902; x=1714693702; 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=iqvGcDnU4Q7GXsG0msQsBmmm+QdgUC8OStzEmaKGVBg=; b=N3uIFkfQ2XkXKfMCjWPo1XZSrU7OB6shyHckelPf9VXqedt+GT1ceNdaZk1yMfVtbG ceR8y81wxFSd/AksN22INaK9/cw4+B7Z0GfUfYBuQgyVvBKgx74kasSUcfAWGqc6jLWt FKaI7eQ0X8YNcEr90euTu3Kps4OYEs51DFtG4yuAjpcp6dC1fh7XngeD9yszkS9+SLcC BNKzv2ks9Mhi4Dokl0L4/NwQXREecrEbLalmpcUcQ8kF4oh8HL03aQ/Q+La1horkJxvL Trla6mJGmj3goP9DF+N/SCWq/yD3QCOOYbm4Huq2Hv3haeunoFH0Gr0/EHBmvgFS6E1I XdjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714088902; x=1714693702; 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=iqvGcDnU4Q7GXsG0msQsBmmm+QdgUC8OStzEmaKGVBg=; b=Z58KRUOsFiHYXl5GrEqLhkDljHaIBGD5aFYCM+Tn9HctutpGIBaXbNmy/lDgqYzipH 1aKRTzLUJTBHyM/jwT6BV+yu8mbvFCyYx8kvZz1PJqSBxYTc1UyaIlHK/kQte6yRH7GP gD22+uvZ9wHnmpvmR8xFDGe5w9lgxlCMbe8YjE5CTa4+G98d4LC/8eII8cbdu7BRgbmf pfaS5oIt3Uvip7AtX8Sb3MYKXWqzDxl8LoPBfuEEo4bnep0aALsjAyVWHMCmU3Wm+44j 8u1wM8L0vkw93bsbPhhpYchVa/supdx76cddS8XzII5YUC7RGdp5LaE9wDE74yMB5KHg Lz+w== X-Gm-Message-State: AOJu0Yw0NMxYzS0GDC7diM59VUlHq9f6aUNfPVFo2qFPafnkqTKYzuXg 9Q5TOIgyGgGCUAxsqJqUEnzUudeA+Y2hA4YXyb6gtsj2HwTadF2MY+W9jlDgc0F8kz/ITR67dNC 5 X-Google-Smtp-Source: AGHT+IFvadHJo6CHVYlerS5YjMlarvs6SjBXIDYE60SuTW+TYKkQrVr89MpU3rNa3hW0vdQ+MjDUOQ== X-Received: by 2002:a17:903:2b0f:b0:1eb:ed2:fe89 with SMTP id mc15-20020a1709032b0f00b001eb0ed2fe89mr12781plb.10.1714088901920; Thu, 25 Apr 2024 16:48:21 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i17-20020a170902c95100b001e0b5eeee41sm14349281pla.38.2024.04.25.16.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 16:48:21 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Samuel Holland , Albert Ou , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [PATCH v2 kvm-riscv/for-next 1/2] drivers/perf: riscv: Remove the warning from stop function Date: Thu, 25 Apr 2024 20:16:36 -0700 Message-Id: <20240426031637.4135544-2-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240426031637.4135544-1-atishp@rivosinc.com> References: <20240426031637.4135544-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: 22f5dac41004 ("drivers/perf: riscv: Implement SBI PMU snapshot funct= ion") Reviewed-by: Samuel Holland Signed-off-by: Atish Patra --- 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 15:05:09 2024 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 26FF2156C6A for ; Thu, 25 Apr 2024 23:48:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714088906; cv=none; b=mi25k9UuEa0u/QiUHDT2boQptLyCkNVxEb3qycmxWEz5p4b+Ael6nD0CHHo9LeHQIqQe/VtHjkEukMjmVjPzwDQJJtNgdQKE0Bk/lScMTnIhriFlUyag/ZfGG98q5pZkQSswwwTrYlu/vKSnqeEEIlzXt5jwzGr4To1eXX77hwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714088906; c=relaxed/simple; bh=6tcJOvrFV1iMaEVoAqZMacKXa4AyJaINchYn8HitefA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DX5cbNaXAlfMmU87vRZQR7tEP/b//IE85NFO+9MsTZXqdnuWOd5JbimtjgAToukzqIie78SsOkC2cbe3m+Cgshr7F13ci0aNfN5La6SGCMwkYBonoQ3D2YS97nYMDt4weCsP2ppIDjG5hX7Fiqb+OJoQKMHMtRpp75t16q7kBV0= 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=EU9i+AOL; arc=none smtp.client-ip=209.85.214.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="EU9i+AOL" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1eab699fcddso11166435ad.0 for ; Thu, 25 Apr 2024 16:48:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714088903; x=1714693703; 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=KZ7DvnuhtpSAdZVzlWSG9HvYdyHUjrzZHHo/ISn+uFM=; b=EU9i+AOLNslermU3q/qwldC0XhQEH2zkmqLeDal9esAqTJR64uQVgUT3cMy5sg/u4Q xhbBedqJomQYGacBk9/zrH+fEBFs+6YUiUbYB5UQHIBTCDf8qVwMWF12k0ZRZZtU04aD IW9SnpKKhrZ37QIQHppVI85RnB8VlLVjw6F5sO7cqPahrADTjQXjjEF13nUvHgxr0qb9 lca1z7035I8wfP7V/VgSSnlRBcJuftGag8P5SeMNU3Przz+xH0Y6BruIDYtNmmbTGeos AVjX7K24OYwO9eT36COZFoa/BZItDY+Tbfp8W1qmEAJk8foip15ar/ZbaIn0SFCCvGJb UKhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714088903; x=1714693703; 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=KZ7DvnuhtpSAdZVzlWSG9HvYdyHUjrzZHHo/ISn+uFM=; b=Woz0elrHmHOUdGue7eGoEtI/wn/FFigSqYhHfThUjOs/NKJlZfVr2gzIIRZZKyw2ms 1Z0dIk9YPx3l3RMtfQW16LKgIzaL1DFx8cvhraMNsvc527Ot42mFLcmilSkCF/t7k/9m 4juDV1u2dROTmSOXcSKS07azfcEFUWhpEs2KPQ/rphCM2R+Q9vLpVIjGNEvk9e8wXDkm WwNwUG0nx3VZF0JWTizI0+OvuB5KnLeo98qKZsCUOVKO4itr6GPZufzSpd1M1KecbQ6T 4nrkR70yL4nBQEo5XqzDzb82rox/kJolbwNAdDri5NtZ7aHApfEUM8hFljSIWgV8+fjF f2jg== X-Gm-Message-State: AOJu0YzMHfAYPYefHL/MLQCXk90yjqCCptEwJCboapFGDEZAswVHBFxy cm743ibAK0ixagqAsxy283gzVgrQMQm21EWCZdMeVr+fzZFkNsyBj0vvZxo/3Y6nlWOshcLzjaz d X-Google-Smtp-Source: AGHT+IGpkxpVvMWJQWbQvANCX7WQJuW5fyjePUy9yCGZseGfULFbWEJYzubnJIqYoSm63FBKJq95AA== X-Received: by 2002:a17:903:1246:b0:1e4:31e9:83ba with SMTP id u6-20020a170903124600b001e431e983bamr1293736plh.1.1714088903558; Thu, 25 Apr 2024 16:48:23 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i17-20020a170902c95100b001e0b5eeee41sm14349281pla.38.2024.04.25.16.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 16:48:23 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Samuel Holland , Albert Ou , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [PATCH v2 kvm-riscv/for-next 2/2] drivers/perf: riscv: Fix RV32 snapshot overflow use case Date: Thu, 25 Apr 2024 20:16:37 -0700 Message-Id: <20240426031637.4135544-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240426031637.4135544-1-atishp@rivosinc.com> References: <20240426031637.4135544-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: 22f5dac41004 ("drivers/perf: riscv: Implement SBI PMU snapshot funct= ion") Reviewed-by: Samuel Holland Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_sbi.c | 45 +++++++++++++++++++--------------- include/linux/perf/riscv_pmu.h | 2 ++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 2694110f1cff..5d699b06dcb6 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,36 @@ 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]; + 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(i) & sdata->ctr_overflow_mask) - temp_ctr_overflow_mask |=3D BIT(i + i * BITS_PER_LONG); + temp_ctr_overflow_mask |=3D sdata->ctr_overflow_mask << (i * BITS_PER_L= ONG); } } =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 +851,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 +864,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 +872,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 +903,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