From patchwork Wed Jun 26 07:23:01 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: 13712355 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 31371159598 for ; Wed, 26 Jun 2024 07:24:18 +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=1719386660; cv=none; b=SkMkp4MsXI68GHFt3wG/oqCoaO7Roa6gdgbD3xyrrMZLXSB1Faq0SBojLcw/jdRxdOzqT6W4HpA73EyEk+w1cjBhzILQueHHaUdzxqD6DB7CCT3Gw+XCjWSwtKH6zQIhQ/rlvnEokcuck5A2wPGhi0M6OW/NBk/pyTA2ZUZMVng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719386660; c=relaxed/simple; bh=06RBFHMu9Nmr2Q1NPTzjbWGxYqYy2/kVWYTcvUcMgCg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WupoBHdp4qNUA+hqEp3U6NXS76LN4AOOZebcUYkVs0Isv4dGI1G2gkIvf8FQZwmfy1jmifunFi90/5YF3oSkYgjxl7CXZobuI0wUS1vJ+g3SBFfOduFwgxXTTvwdpBXjGOHn/qE9xJ4xRxxaUI9DVbbKcyPxwWD+JToqZ70sK58= 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=ydsAod9K; 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="ydsAod9K" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-7066a4a611dso2803729b3a.3 for ; Wed, 26 Jun 2024 00:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719386657; x=1719991457; 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=8anUdf9RYagj+8dVpBIC/eX2xYMTPOXrNSg/TEplCt8=; b=ydsAod9K6yXeRpcMm8tDmaqjjNyUKIdzyPtNMlOEduO6KV+O77ZIJvbPFTym+ao7n2 ldrGpMu6MpOLla/t1FYY+iPf44ZN3G+Nh6MoNOmVLKJAhDcK4gO8VQR3eNt2yjlpui7k OA7p6CJ1TWmz3TrL/uKEiN2eB9BpzCL0lK6dKrX1CpGnjhONmaQBNAjWGmTqpl8qrI2Z ZwXQ2XOrQnlImygpB3UMOfUordVXLosu7h77IyeLiXPjWhJVjI+QXijgI24MidyjEDXz aUyNROmUaPistqK54MKpxsIxPhosOmRC03wiNjcRIMDcxPZQJMNueauvBmTzYsBbzZA3 QLCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719386657; x=1719991457; 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=8anUdf9RYagj+8dVpBIC/eX2xYMTPOXrNSg/TEplCt8=; b=Bs5uBIjpskP06FwOltUnnmwGrlheN3iasQrtB/AGMPApr0Q8AeppASQWu2sHblD9h8 7rRnT2iVnfwOwcU27RFQAz4y+5T84mUe1wf6jf17jU3LUMDGht31IKuIZIAUCjvvMKG1 K8Th/YOGkBFR7sUhEGLQY5CEAs7MRBuc6Z5qgwK2jU1WQXrVBgmzcaUC8xTwAiRMVr+H af9loLPlWEW5JuP4cVtKOQR5dlfXX82eAS36M1O9nfVUZq4nPfM4Z1VaO9hBTSQIm5la 2NxkatGGCT5FJdRYwiDlza4KBLCWY9i1LSXTMFUwobCm9BD8oMYDKdC9lMuQGWUD71h6 qG8w== X-Forwarded-Encrypted: i=1; AJvYcCUcP4oCbMnWSVMEF0mEtz5fqfGSRjYSuKIVIeamglbtNSy4sWnacbH+1KAc8ulNGmnNry+VEL5sRAXLg3NkY9s4GQxL X-Gm-Message-State: AOJu0YxlNm+aXNK7yCqCRhJmVN7XhT+xRBrsUyM09wi72dwUIf66+Zvh s/u8Wz7yVPOuIvFLkAxBeMeoApsGt+zdsBYBnjf743/Ps50xVou2UJqrJLiA+aM= X-Google-Smtp-Source: AGHT+IG/ltx0PscjisU5QEngpWeqUQrsUK17QM5DEDcSKzn36CUqRKBVIIjNHW6RZ5CQaoW0qeXI4Q== X-Received: by 2002:a05:6a00:bf0:b0:705:9aac:ffb8 with SMTP id d2e1a72fcca58-70674582b1bmr8915411b3a.9.1719386657496; Wed, 26 Jun 2024 00:24:17 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706984a721csm2692218b3a.37.2024.06.26.00.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 00:24:17 -0700 (PDT) From: Atish Patra Date: Wed, 26 Jun 2024 00:23:01 -0700 Subject: [PATCH v3 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: <20240626-misc_perf_fixes-v3-1-de3f8ed88dab@rivosinc.com> References: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@rivosinc.com> In-Reply-To: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@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. 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 Wed Jun 26 07:23:02 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: 13712356 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 0EF7715B10D for ; Wed, 26 Jun 2024 07:24:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719386661; cv=none; b=gkkKlFW962TAP6LX2Ph3kqdJRfWR9iCeV0JhqlQedIx4XT3lpVGmz5syIio8S+WBBNdSVfebNWrjW/YVZR/Ba+3b5LpZA4u13hhqbqucKdQd8sBVRx0SFZgZZ8voHVUZuO6hgJMIUZA6F46tETzknKIwlqqs5gJMJO1CBI3Agy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719386661; c=relaxed/simple; bh=Q6fB+0U1vwNDij8/HH+ONSB+ggEQtVYZcG1us9ReQnY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Nk8M9aShmtcFnAyR4Dm72atlNQE/mcKE1zs487bgG3OwIFlm1F0iIRGTgCREthvKtrd48z4WGllrO/e8LGUN5kxVhWUWIJ2fhZQcKjkA6q3IiZFP4otCdhNJhHsxukG/wVtEZuzcUtxw/tWj1r0nXbxVB0PGJJ2D3oezlGf9luM= 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=g1wmV/pH; arc=none smtp.client-ip=209.85.210.178 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="g1wmV/pH" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70677422857so2269410b3a.2 for ; Wed, 26 Jun 2024 00:24:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719386659; x=1719991459; 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=njNBT7xpsrpGcVD9LgzuKuD8VWJ85LTW2hNYGRcpIO8=; b=g1wmV/pH4O5LWKvS6hBr7YOkbXJGRiIh5pz2efN5qPfQrlwRjDVvMP+9B0yqgx/YPW IiLvqbO305UCLNExCAZj++IAc1fM2foAN+5+54abI4JFu6u3xhK8Ts/A0EibS1naTKLG NdSHrHnnRyp3JSIt/guO37fYnS2NwPmU88b7QZCV2Lp5tQ28ChbFLRZjPw/NaAXy9o+Y xqPyg8U7aBySoOdn/9EC6p1r0KZjmWkb12h4ngpPLE9nBYRTLhH8tUssM+bFT6uC0Ocn XTMdHETYhK4Uk5DHc0dhsQSYPZofnlxISQNBOesdc4PC55UsCpSv3wOy5VDpSBQhO5/e KP8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719386659; x=1719991459; 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=njNBT7xpsrpGcVD9LgzuKuD8VWJ85LTW2hNYGRcpIO8=; b=LOlYjm1txmlGOM4kqtVsWRXJaizADhfGDLukmLQvhxa6W0+WOZzznEwvdr53msqILf Zp6i8foMN8CmEgNIzeNDo0gCxzft1iVees2Y5Ye1i4rqlOA8Q2n2H/pjrxyyOESWRoQV cRLhPsPZu3kqVDfksId76mFCUXic4hNnYRHOAiYm2Ezw/NgaKN/OwIMR10xjOdILH0GZ kEBozqnV/3ioHWkUCMK4VALJ03TLokt367kRRthSpTqLSDLRGXz39MDmcx2yqzREnTzQ MfLLD2uyJOGv5iZ7JWcpbUoPrr/osrD1vjnRDN/lPPo2xKiszRojH59dAtSKxluJVj5J hYIw== X-Forwarded-Encrypted: i=1; AJvYcCVV48ZvYZn8TJUIthLurz/jfAWp6kuUk9nLTJZ3xsUKnebYk9sssDZ1eG2lXZNZgxDjv16V00I3Cd0w6Sb1tJM8UW+c X-Gm-Message-State: AOJu0YxuzyQ9D2a7b9Mrqq9V7VaBIbntpze0buUSyZAyMyGkajydi6zW wwxJYiYA5GO691ZcxUrRYqADycg8MgxYuu5z6+PfzdUPkhtzqD9+IMticI5kVak= X-Google-Smtp-Source: AGHT+IFjsQF5e7oc0NKST2xoCih6SJziqYjY4xf7uMtfC2cP8xPNNQr3F5oh1Cg01FunjCN8iViJ7Q== X-Received: by 2002:aa7:8ecb:0:b0:706:57ce:f042 with SMTP id d2e1a72fcca58-7067455bfd2mr9223061b3a.7.1719386659342; Wed, 26 Jun 2024 00:24:19 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706984a721csm2692218b3a.37.2024.06.26.00.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 00:24:18 -0700 (PDT) From: Atish Patra Date: Wed, 26 Jun 2024 00:23:02 -0700 Subject: [PATCH v3 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: <20240626-misc_perf_fixes-v3-2-de3f8ed88dab@rivosinc.com> References: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@rivosinc.com> In-Reply-To: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@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: Atish Patra Signed-off-by: Samuel Holland --- 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 Wed Jun 26 07:23:03 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: 13712357 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (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 CE8A115F3E6 for ; Wed, 26 Jun 2024 07:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719386663; cv=none; b=HFDNEq6WzcMDyoGTqr8+YZSt9MIqHM6P4MeDlUdqMXpfEomsgN32b4LBGQ+auTx+88v9w+UXlh4kwvWzvwJTmiaxGIJ70hniNkrGyan0+qVE56dyrU6LeameH4j22vnYU3R24/HzrW+/6nQgCn40L0ALw93TEGGZ6SWRx1tVj0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719386663; c=relaxed/simple; bh=VUWBEtm/D4zb4F+2qjBSmtWv1T8UgXgKWmKRuYOYU7Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GUaoaSIsWkM5KWlUwGFzGbqV0vKsK1IKwl6KUxGDVLcIqtnNnNZGzTThHZLdPnSEcdR+8otcbG+Cv1B/PxJECgC7RuCoMdjjfGjVcxvrTE46OfOz/z+o7PoiX4QOE8h4lBChA+k8Yr/TudcL9qEBTwzj3/qTt8j2MG6lwZdQxtw= 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=P2pl5PsI; arc=none smtp.client-ip=209.85.210.169 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="P2pl5PsI" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7066463c841so3028967b3a.1 for ; Wed, 26 Jun 2024 00:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719386661; x=1719991461; 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=Hrss5oGak6lOGeNrtNh2TIJslk06xEuOEGM+GgwCDDo=; b=P2pl5PsIIqZmUyC7OQUu6TBp3grS1OlsczFX8BSvHcLnUU20z3e0imjpscfwEhZ6J1 KlJLiZngaVBgAiCYgEgwrUbhPrWjbMV+nxDJpHVdQuHHMuICnEveXUndJvuWqPtkIekm rRcES9K11Gu+T+9hsBR+cKONj/vVgYrmIo+zyYp0dPHRDzdPlwF0je7oMNZT8UrGbXFD f9DrP3rBfPkbZJYS9YSRteJLPzW8a/93l5bia8qXDOgZEF3gxvtgpjKaIy6T+iMXkgzK QSiDn/GBkOUwYq+z1UaSaBf7Tx9I0z6QYoyZqinymmv6IBfdSG/un45zpFUW3xKS5Hw1 wopg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719386661; x=1719991461; 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=Hrss5oGak6lOGeNrtNh2TIJslk06xEuOEGM+GgwCDDo=; b=rV9vddnr+K19WK35nLmFPSfB6QxVwHjAGj5l3aAcSqNAPGlhr+kSD5xTa2R3ATHfW/ VjqO4MmA73QJby0SxihHGG9FbLnp2GoR0Uj1lc5H8JNV5oruWuOeFYwsM9T6f6EiBewm M73gKjcCuW+xfEbs37vGwLEPkxR+3lm+Nh+QkIPVbNjjM1ijDkPG6OXoZpD7RPD0LbzA FEAvP/J9UUu3zP4sC8fnXp35A79MKZ0ekMb56onk3rM7IKixLdXCg1m9BzdICltrmjbv ggDwD5gar5Z6ViC3K58yG77FJX9ZHULw0M3WviXszchlpxV/3kt9vZesQjCO8p4YXiKP 5SNA== X-Forwarded-Encrypted: i=1; AJvYcCWBR1Us9mbiOqjjyYLDL98SVt64Wip1hu7URJE7UXQ8hduSUu1NPFr/+Rn02kjNvIUNsIq8S2fRasqZqgf8J5qEFUSc X-Gm-Message-State: AOJu0YyRzDZeCly0M6+5kydQ7bqppCUaJyurlVfgWI7WGWpnXpf1AEKo XI4mQ4BINCeGkgCaS7tEyg0Tp+lf2orws5mVLSpETjLjKpZuydmtKyxswXqw5sM= X-Google-Smtp-Source: AGHT+IFvQFItmxduKUYuJVMymGEvvdaLRWz3IbarvmEbIrChrbcL8pGYz4tAs8BSP+va+caASzcEaw== X-Received: by 2002:a62:e817:0:b0:705:ddb0:5260 with SMTP id d2e1a72fcca58-706743ad900mr9780867b3a.0.1719386660966; Wed, 26 Jun 2024 00:24:20 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706984a721csm2692218b3a.37.2024.06.26.00.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 00:24:20 -0700 (PDT) From: Atish Patra Date: Wed, 26 Jun 2024 00:23:03 -0700 Subject: [PATCH v3 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: <20240626-misc_perf_fixes-v3-3-de3f8ed88dab@rivosinc.com> References: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@rivosinc.com> In-Reply-To: <20240626-misc_perf_fixes-v3-0-de3f8ed88dab@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 --- 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: