From patchwork Fri Jun 28 07:51:41 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: 13715669 Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (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 46C83145340 for ; Fri, 28 Jun 2024 07:52:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719561127; cv=none; b=cCDg1dsrIWvObXS64Dq1sevfaT+2OG3J92LCpw97HW2wqsxX6SD0A1No1gNyJ4rGau4lgIyVLjH3tQ6MU+NrXyUU7NcWrDSFdVXJblhRCunBZaNyNIFXVnhSVpGNV94XJtS3MQD6aZjPc5DqxHfZ9BaI0/er4lSXPxJjiWXslwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719561127; c=relaxed/simple; bh=Ark6jD3S2s0EGCk/kplfkHpXwZMR4vMqPsKy7BXH++g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Te0roLUP9Q/8SqQm79b/PCb7NXxIIoFckNuHHiMfecYiy+1L5hdnUxT9222koFIRFLc08nFCKBOKLNUoBsh1rXbwEN8C5QC17oHEz1u/fxuqmolBP5eaVei/5lc4opc1p5PfDxkCVUHp7rm9WOilWJ+IukKc0YarJcMMsr06H+w= 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=K8MLtgJy; arc=none smtp.client-ip=209.85.219.177 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="K8MLtgJy" Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-e02b79c6f21so270560276.2 for ; Fri, 28 Jun 2024 00:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719561125; x=1720165925; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=eu+2wcx6ARI0Khk7sEf+LxSnsWz3kwiEgnTb6o13mh0=; b=K8MLtgJykJ6umJOFTmnh7i1rJ0JFUa2EJUnCBDKyJyVwabXGhCIUaHu7iofA4qsI2e te5ZDAz3DRBbn+Phaj7JETpQCxB7PkW4fx8SexCL4w138FEa43FrSA8Pc/bZh2bIRQm1 maxY0NkVU/w0q4RI125XHdn0viFOqJiw68wgjakzpz6/wzV20yhBTXWB3O4aHzpSK+TP ih3SxkzZznifi60f6IaWvfGPr5laqppuSGejZB2nlG7rEXlxec3N3M9p31bemXsAZ2Y2 DzuVQGrMG7UpHvYm5gQ/v8NOkBhpduTmYGq8j4mT/EMCtw/y0GRnVeWO3u6tKOlFTNFA 9MFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719561125; x=1720165925; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eu+2wcx6ARI0Khk7sEf+LxSnsWz3kwiEgnTb6o13mh0=; b=vdXZu4cF8xgTatol3iIbXhgH2OqcNYbwj8squ+BRtfNVjk5k+ie5jB0d07Xn9DVRRb Hs0XLilHHzUunEWW/Tux/BytfooRvYoZ6dKJSJul4u7onDuM+b+YRY4L5tt9bw0klLq4 z900Hwq/iIESAgwq29/60msIyABm9RPsYyOCy6PAA3aq4ll5JnliKdjjyqAEZj7gkj8L uZ26OKcR3jdWsVN3+AOBTUtv5j2FB0ahm8jXGYYJk4CDyMkDGzeS7v0YjcWCiFNeE63E 3gF+YqqZ1pDmOQEoNAVri4LohrO6650yxnNmifGNLnTBjSrVDXZNWVdaeB7k76XFvHso c28g== X-Forwarded-Encrypted: i=1; AJvYcCVMEIZ4zzNLrQOOOIjC7QjkxuM2zPdwWCypDXWO4oexKVDvAQh4OKMzbwKegwqPeje1dM49Xku2BsD4T3wYJ/eyiibS X-Gm-Message-State: AOJu0YyoBH13hzpQmMP0M3jCDqO6drLfsVBDuFiQUVDrlHxhjr3E3jIu CY/CJhngh+oYi7RdNcxZ3UIaKywJXdHfz+G7XXJKlqsySASnlgybz61uEMmEmU8= X-Google-Smtp-Source: AGHT+IEZUhwullYVKgF25a+0boKvH0ownItzxOgMvrt6D0RnS9rjqr16EhSB3iPBE5ZjBx8I+fBacw== X-Received: by 2002:a05:6902:1701:b0:e03:48ed:d275 with SMTP id 3f1490d57ef6-e0348edd2d9mr4844561276.61.1719561125224; Fri, 28 Jun 2024 00:52:05 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-72c69b53bf2sm685068a12.2.2024.06.28.00.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 00:52:04 -0700 (PDT) From: Atish Patra Date: Fri, 28 Jun 2024 00:51:41 -0700 Subject: [PATCH v4 1/3] drivers/perf: riscv: Do not update the event data if uptodate Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240628-misc_perf_fixes-v4-1-e01cfddcf035@rivosinc.com> References: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> In-Reply-To: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> To: linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org Cc: Atish Patra , Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Andrew Jones , Conor Dooley , Samuel Holland , Palmer Dabbelt , Alexandre Ghiti , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Atish Patra , garthlei@pku.edu.cn X-Mailer: b4 0.15-dev-13183 In case of an counter overflow, the event data may get corrupted if called from an external overflow handler. This happens because we can't update the counter without starting it when SBI PMU extension is in use. However, the prev_count has been already updated at the first pass while the counter value is still the old one. The solution is simple where we don't need to update it again if it is already updated which can be detected using hwc state. The event state in the overflow handler is updated in the following patch. Thus, this fix can't be backported to kernel version where overflow support was added. Fixes: a8625217a054 ("drivers/perf: riscv: Implement SBI PMU snapshot function") Reported-by: garthlei@pku.edu.cn Closes:https://lore.kernel.org/all/CC51D53B-846C-4D81-86FC-FBF969D0A0D6@pku.edu.cn/ Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c index 78c490e0505a..0a02e85a8951 100644 --- a/drivers/perf/riscv_pmu.c +++ b/drivers/perf/riscv_pmu.c @@ -167,7 +167,7 @@ u64 riscv_pmu_event_update(struct perf_event *event) unsigned long cmask; u64 oldval, delta; - if (!rvpmu->ctr_read) + if (!rvpmu->ctr_read || (hwc->state & PERF_HES_UPTODATE)) return 0; cmask = riscv_pmu_ctr_get_width_mask(event); From patchwork Fri Jun 28 07:51:42 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: 13715670 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (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 CACAE145334 for ; Fri, 28 Jun 2024 07:52:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719561129; cv=none; b=PlGmJin3rCbrVBkOvNNE8Y6VYtXSwo53sLZYESpExvvVzNmRFjqaoYDCy8QutRAdSaNTbUPZdbVZ4qhJBjZ07E7SoE5/BQxdIwQyvTdD7nHjU5+5+zo2augbgv/DDMMMV2bF5BKg+Wkqj9hAfniI0EuTeyPlerSoI+CY6xOxn8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719561129; c=relaxed/simple; bh=UYQnyeyH4ohjpyhxmQyUsjOd4jl+wfm38hnpNZUDtac=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=P1GmhPIpTTXikWO31pI072dmhpKOlq9FIWjHR1A5mtlYbsQNEERrnI12SgZqAzO4IyDm8/5qobgkhZvqifMY9BYaNO0aPmLIknMPPjpjdjjRC94p3/oZZQGL4OhKHRrmIsB43b+Ae+ONvU6k+CNwotJOt2tJdXi62QLI2BjaFRk= 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=loJxZlJM; arc=none smtp.client-ip=209.85.210.49 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="loJxZlJM" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-6f8d0a00a35so298770a34.2 for ; Fri, 28 Jun 2024 00:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719561127; x=1720165927; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=8QTaOQgSJlRgxGe2DAtbRN63Xb4cFygQnCpr3YrplOU=; b=loJxZlJMUieHyNxT02kKCmi4kUOIsRKls+hJFwcSG8KL3vL80Y0mmPNp116Wl8sZJa DgRclrjq+bmWkWmEqkfK0ddRCvXF7fNeOg0zne+4/Wi963cXE4z0tVBHfTXXuMiHEbIf uYpZChYR0WePqxrRl9IUZ9NrdVTCRDhEI1Uc3ziTGSoTUCyS4hiUIRGOzZzanN1xBy5A tmAYJl8xSEc4ezVqIkwFgfBW3+254TnvKCZCNc7CPHCYKtCmjJICu19YhH5oslXMGYt/ FZt7hveVKgGk8gZDcDuaKQ7IUVYHTvAxI9RCnSTJuLLv/OIm6GotT1y88TNWqFhwoj6k ZUDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719561127; x=1720165927; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8QTaOQgSJlRgxGe2DAtbRN63Xb4cFygQnCpr3YrplOU=; b=dlmHi6uwoFLCh61GbH15p4sS+aifsLBGxDbF5G9SrkkelFk1vpvCEGZS/309IWpRf3 tG/2EBXCiL0J9kHvyJ+5dOXQMzBj3oPVznJdNfn3RGSSHQyEq7mH/lwLOFm8sG3g2d5H jNiucnKofoJO/8dL3gQobqxWLvAszGMLudxfJn1uXIBY2A99B/HA7f2vos4uYdeUK3iI iIOmu308oqBD5jEU5Co0Sc1BdrROUvtQQRyjUScqVVHMZXIrCd5wdwr77Jn/hjzGarYy F5UxhDqo6/EcOIc+KhNuHYsiaM0jtYlbvksCs0bG8cfG4/ipWRYoR+W8tbvsA5j6EeZJ 5/5Q== X-Forwarded-Encrypted: i=1; AJvYcCUTFBmFHYspqhRBOWiG4OH4wjb0AhLpA3f7BQv/aQwohbSKRtZuZu6wLLn2G+OCJKuZYQGImAwPuaur3RJ9e+vHxGwP X-Gm-Message-State: AOJu0YyXSiYRXRtP3wYHa95U7L1eFHTpmEQw65LOtkxczEbm+5lYKk4M /ZhxfNkp631U9KNTZW7nWahtCBQPHoBqcYRijJIY5eveg6c1IggRy7F5DV6BNSo= X-Google-Smtp-Source: AGHT+IE5FpdJsgBgjR00qw7HsO4MmvDOUqzWFrZg+uaxB1OldOrplMNqpPJ7b0OBtuxblj+ltyW8xw== X-Received: by 2002:a9d:7495:0:b0:701:f1cd:350a with SMTP id 46e09a7af769-701f5874810mr3112027a34.11.1719561126989; Fri, 28 Jun 2024 00:52:06 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-72c69b53bf2sm685068a12.2.2024.06.28.00.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 00:52:06 -0700 (PDT) From: Atish Patra Date: Fri, 28 Jun 2024 00:51:42 -0700 Subject: [PATCH v4 2/3] drivers/perf: riscv: Reset the counter to hpmevent mapping while starting cpus Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240628-misc_perf_fixes-v4-2-e01cfddcf035@rivosinc.com> References: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> In-Reply-To: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> To: linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org Cc: Atish Patra , Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Andrew Jones , Conor Dooley , Samuel Holland , Palmer Dabbelt , Alexandre Ghiti , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-13183 From: Samuel Holland Currently, we stop all the counters while a new cpu is brought online. However, the hpmevent to counter mappings are not reset. The firmware may have some stale encoding in their mapping structure which may lead to undesirable results. We have not encountered such scenario though. Signed-off-by: Samuel Holland Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_sbi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index a2e4005e1fd0..94bc369a3454 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -762,7 +762,7 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu) * which may include counters that are not enabled yet. */ sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, - 0, pmu->cmask, 0, 0, 0, 0); + 0, pmu->cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); } static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) From patchwork Fri Jun 28 07:51:43 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: 13715671 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 5FABC145340 for ; Fri, 28 Jun 2024 07:52:09 +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=1719561130; cv=none; b=GuNSjVWkETKfAPGkhsXRUMZoZDOTYYzjamBw0MAcIn/ZDpbv+XCPR4wbxqujD/f1QCJxFYYhQGoitI8gOgdnetlLp+dCRvS1yC6QMYMeewqCDDdR1+FC1oCX/pZ7TkGsfaBCeHfNUPzS0avoXk6klcAOIBSUuYB+XYvSfctfGXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719561130; c=relaxed/simple; bh=2/Gu1GKW3gCm5IsVIukdOBuH7a+DWyYtIl6k1cTTIA4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Qq2DOSV/WRRZrJ19j2/agCd5mA4u/aljvwM4VbPjS2DyFbL5LPnRVxq6XuvkQKbRnftsyhrBf+u7r9nA6xlR2+eOWY2BZhAzeeI+OBl1rzKnYa6QurIcFHMO943V54+rQEbIaWPaah9KpB0+L5HI2KAqMqGheKAiHiXKpQ8c6U0= 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=e051xH6V; 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="e051xH6V" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-706b53ee183so1056403b3a.1 for ; Fri, 28 Jun 2024 00:52:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719561129; x=1720165929; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=6fc3IuO6YmBNA8s0mWpyu1I7/gJNRg54J9fB76y1B8s=; b=e051xH6VN1JL1JL6PZROjMdygC5Ly5ntYuR4M3vZngwQOt8JYK6LMey6u4nJ/hevZj /V6Gt7KPSXuT0PROvVYfHuQzikMMEzsZQD7FXpVwdMtuEVUzUKDKl+XEy+gWXxoRt7I6 o56TyOjoIMP6CNpVuIYtg/2NaQMxBS9NAnbDpx5qYTqpPvlwBU9Sy92MPE7BG+l1Bjdd /ThB5SB9rjCjAMtVdUkdKbexxAI2p251UB6R7cw4nzElkJkw7hS2MU1YvxRjhSt47Tyx o0labLteXJLzN5KqHcyKRRq5P6ksscueryPUmq7La+OHNbiRajrc64vLdPZRyUhat/xE Tsxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719561129; x=1720165929; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6fc3IuO6YmBNA8s0mWpyu1I7/gJNRg54J9fB76y1B8s=; b=N9bSg4sq7koWdu/qDkHsc08Z7BxfVasACzhODjzfGi5lweobqFegpd+XVkJrgYdGCO D9qy/dsfY0jLIlD1U3enBpNSy85kGMErr91vqeNlzLHothTqajOhZp0+oUsJhZCrle5L kxgv3zhQ3poquQR6AiXRBeBOVb623MEOU8/8JBKF9MBxv3Ks/86ovM09rOw/UhhKIJQn u7LwpNsVnLvG21QHw2cwzcKKPLUjpkVmhGDnW5THhpyCefuFE/4kQmFrytEPdOZ07Wrn 0P7N47ZODr4/Adyulv36/+txzzSohRUtFcn6Dck38/ofWzxV4NbyNGeOV3kPDDe8FFT1 JIiw== X-Forwarded-Encrypted: i=1; AJvYcCUfJmIuO0tPQ87gVy/NVUMjpoFS3WqJ6gMGEdFxUt2fB5IbkCFI+wgVVL2Wki/kqnEmj4BEeFQcUhjX+rABFZ/wyTYU X-Gm-Message-State: AOJu0YyRPtTFcrOWXsGFUHuKxIG0ZFzBi1+ByXAzSoP+4Sy86hFtH5wS /qjZnNgk0QG3OxlrRQwtu81mLJjMhV870HVbcPICQ25B5Lx0SB4bKn76VZfKcR8= X-Google-Smtp-Source: AGHT+IF3KuUY30k6Fxmt5zQarV5j2YsUdQtDIpW2glwEhLDfVpSu4XavuBx9WBfU01ZxprM4j4Fscw== X-Received: by 2002:a05:6a00:2e96:b0:708:41c4:8849 with SMTP id d2e1a72fcca58-7085223cda8mr1543391b3a.9.1719561128579; Fri, 28 Jun 2024 00:52:08 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-72c69b53bf2sm685068a12.2.2024.06.28.00.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 00:52:08 -0700 (PDT) From: Atish Patra Date: Fri, 28 Jun 2024 00:51:43 -0700 Subject: [PATCH v4 3/3] perf: RISC-V: Check standard event availability Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240628-misc_perf_fixes-v4-3-e01cfddcf035@rivosinc.com> References: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> In-Reply-To: <20240628-misc_perf_fixes-v4-0-e01cfddcf035@rivosinc.com> To: linux-riscv@lists.infradead.org, kvm-riscv@lists.infradead.org Cc: Atish Patra , Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Andrew Jones , Conor Dooley , Samuel Holland , Palmer Dabbelt , Alexandre Ghiti , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-13183 From: Samuel Holland The RISC-V SBI PMU specification defines several standard hardware and cache events. Currently, all of these events are exposed to userspace, even when not actually implemented. They appear in the `perf list` output, and commands like `perf stat` try to use them. This is more than just a cosmetic issue, because the PMU driver's .add function fails for these events, which causes pmu_groups_sched_in() to prematurely stop scheduling in other (possibly valid) hardware events. Add logic to check which events are supported by the hardware (i.e. can be mapped to some counter), so only usable events are reported to userspace. Since the kernel does not know the mapping between events and possible counters, this check must happen during boot, when no counters are in use. Make the check asynchronous to minimize impact on boot time. Fixes: e9991434596f ("RISC-V: Add perf platform driver based on SBI PMU extension") Signed-off-by: Samuel Holland Reviewed-by: Atish Patra Tested-by: Atish Patra Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_pmu.c | 2 +- drivers/perf/riscv_pmu_sbi.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 04db1f993c47..bcf41d6e0df0 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -327,7 +327,7 @@ static long kvm_pmu_create_perf_event(struct kvm_pmc *pmc, struct perf_event_att event = perf_event_create_kernel_counter(attr, -1, current, kvm_riscv_pmu_overflow, pmc); if (IS_ERR(event)) { - pr_err("kvm pmu event creation failed for eidx %lx: %ld\n", eidx, PTR_ERR(event)); + pr_debug("kvm pmu event creation failed for eidx %lx: %ld\n", eidx, PTR_ERR(event)); return PTR_ERR(event); } diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 94bc369a3454..4e842dcedfba 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -114,7 +115,7 @@ struct sbi_pmu_event_data { }; }; -static const struct sbi_pmu_event_data pmu_hw_event_map[] = { +static struct sbi_pmu_event_data pmu_hw_event_map[] = { [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = { SBI_PMU_HW_CPU_CYCLES, SBI_PMU_EVENT_TYPE_HW, 0}}, @@ -148,7 +149,7 @@ static const struct sbi_pmu_event_data pmu_hw_event_map[] = { }; #define C(x) PERF_COUNT_HW_CACHE_##x -static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX] +static struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX] [PERF_COUNT_HW_CACHE_OP_MAX] [PERF_COUNT_HW_CACHE_RESULT_MAX] = { [C(L1D)] = { @@ -293,6 +294,34 @@ static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_M }, }; +static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, + 0, cmask, 0, edata->event_idx, 0, 0); + if (!ret.error) { + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, + ret.value, 0x1, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); + } else if (ret.error == SBI_ERR_NOT_SUPPORTED) { + /* This event cannot be monitored by any counter */ + edata->event_idx = -EINVAL; + } +} + +static void pmu_sbi_check_std_events(struct work_struct *work) +{ + for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) + pmu_sbi_check_event(&pmu_hw_event_map[i]); + + for (int i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) + for (int j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) + for (int k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) + pmu_sbi_check_event(&pmu_cache_event_map[i][j][k]); +} + +static DECLARE_WORK(check_std_events_work, pmu_sbi_check_std_events); + static int pmu_sbi_ctr_get_width(int idx) { return pmu_ctr_list[idx].width; @@ -478,6 +507,12 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) u64 raw_config_val; int ret; + /* + * Ensure we are finished checking standard hardware events for + * validity before allowing userspace to configure any events. + */ + flush_work(&check_std_events_work); + switch (type) { case PERF_TYPE_HARDWARE: if (config >= PERF_COUNT_HW_MAX) @@ -1359,6 +1394,9 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) if (ret) goto out_unregister; + /* Asynchronously check which standard events are available */ + schedule_work(&check_std_events_work); + return 0; out_unregister: