From nobody Tue Dec 16 11:05:29 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 2A8611F4706 for ; Sat, 15 Mar 2025 09:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029968; cv=none; b=npnhtfhTM+sk8MU5RMJhY0NbXyf2XYTXkA/F5sNB1L/8yMjPFFqj7y0G+N9DHV7ftpEOx0OLN81rKp/mtP7FBrwQnHXQZdNWw5QxfBvMdf6YVuB/STRx90iB8lnzfG/DueP3Sd0p/rcujpd0BnaO6m/XQ9NwUfvHGqdCV5VqpEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029968; c=relaxed/simple; bh=pLEJhlfw3fbIq3nvqJiuw0vOJP9zyUhLd4sW/C6OrrQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mzbMJNk62XB0RPauiB78Qlffp/KY0TLqsJ6BdrcVE1g/BUEVIxUEh4k76M/S8Nnpzv7p03kHfdDoSUTq+CIct+xu3RGRoZmxS+JVc55G71DXeQbKUbZOjGbwTmmwES0CkdnDodwF7QZkMcGK3VT+rssRCzCNFILNsuP73QOHyAI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=ZBuVIxAe; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="ZBuVIxAe" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-22359001f1aso73397925ad.3 for ; Sat, 15 Mar 2025 02:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1742029966; x=1742634766; 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=fVQeHb+cCKo9FI3oCd5KZj2+ztmvI+IS1H6dV0Bffso=; b=ZBuVIxAeujgieAmNiJU7uEPhv8quEDbmWM1ekcacXm7H1Qcle5/WiokQ6PITIP/KpC 2LxvcpvIIHXdfPAqQApMnlxAQo2kXbgFIs+/+cMr7Crrd2Zopr3SvD5HvR0ykmYPoKAt GJrDAkxvDTKd3o6vfCfaMBDI2rA6iD6jcMu2Ug8nsmiTiUyAOdji1+mKUBeq+Agvg04i ZH/UaqwqzVG6eGPba3Vm0AR9gM69uhHz9W4kU1PiqU8b/ct2CzfUK5NGWWpQs1Ud70G/ HDu3if3ctlJO+sFibaY4tBXgHA9t8ziNP3gZR5ewPgNQPGttIu1Uf01VRSbMZJsEb3h0 8s5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742029966; x=1742634766; 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=fVQeHb+cCKo9FI3oCd5KZj2+ztmvI+IS1H6dV0Bffso=; b=Fy1kEm+t+IhvOU/293uHD2QoXqKvqcWF8uDVg5VTjv+eNSdY1eJL518UnskY/LsjfA K4CLN3P8KfvdHEpfZ/61CTDgI0hk4ltcbkmr/ZJeYaujhKB3k8g8rz0Q3IJB3xKJWaC0 pqHIxj0sZyOs4sdnC+fYPfTfONKyOQrRkj3c4i1XKnLluNfxttulDrtFvrZQr+e53rM5 5L2mqArzKAELES5vrxR0v3j3E5pWQysS18/F1POALrPZy7mfHboz7IeZK38mTXy34OF5 3IGexXgHk/sf91brilo5dOz/Ss7NFUSqTkyE2doa+YIZ23S8HGJ1Uf1n4IUMMr7R1f0u awBg== X-Forwarded-Encrypted: i=1; AJvYcCXD5H2HINiTFeFbLpmYw3tCHaAYoO0KYbvkg+bAplUBdUz9/jdInnSMK+K4gEltC7sdSkJf3y+jawT8msk=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5QZ3MH7ASUO4dxd168JmKZRPe1pJuiYLjUlwFzIsX0mXxxXWF JBWmaclqfBLTeE3XU+QYwitFEHGt32FYJjMIDbB+RAkTKOTA8nMV2wJPTQ4KhBSJK1ohvBzIQ8x dvLc= X-Gm-Gg: ASbGncvjx4GWF1zQnsLIii3mh2GviylQhrPetXdTrPnwwcHQb1M3zA0Awq9TEy/gNoQ zPAYdmq/77FLLq2Nlj2tv0ByjNhU92qUgZy2ZCdKtRozRGaazCDEkVO0yxtsNHN7fDp1xKeS/5r 0F1P91IafcU1erjZKB4mK0vv/W1JI86PX59krMdghW2ZkETK75mrPqyO7J6cJLfx7XWoKOzVAYI N4lHL+dO3l/7ifA1OTLbwV/dvWdizJ4USrqD3jhkbBC5o7voTk6s36osfIAA7e33jfXLEwkigA9 msuUg9D7xnck9CMlxRleTmRIV1N6QYlJA3hJB9Ee39mOT6cA X-Google-Smtp-Source: AGHT+IGQ8Gz6JOShy4dRcyVD/SiQ18YggMiPkdBJ/XbTFh26anmZdWe+vf8X98ZKiGLO7S3+Q3G5dQ== X-Received: by 2002:a17:902:e750:b0:223:90ec:80f0 with SMTP id d9443c01a7336-225e0a6b3f8mr76818965ad.22.1742029966450; Sat, 15 Mar 2025 02:12:46 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6bd4a8fsm40925235ad.234.2025.03.15.02.12.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 15 Mar 2025 02:12:46 -0700 (PDT) From: Akihiko Odaki Date: Sat, 15 Mar 2025 18:12:10 +0900 Subject: [PATCH v5 1/5] KVM: arm64: PMU: Set raw values from user to PM{C,I}NTEN{SET,CLR}, PMOVS{SET,CLR} Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250315-pmc-v5-1-ecee87dab216@daynix.com> References: <20250315-pmc-v5-0-ecee87dab216@daynix.com> In-Reply-To: <20250315-pmc-v5-0-ecee87dab216@daynix.com> To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, devel@daynix.com, Akihiko Odaki , stable@vger.kernel.org X-Mailer: b4 0.15-dev-edae6 Commit a45f41d754e0 ("KVM: arm64: Add {get,set}_user for PM{C,I}NTEN{SET,CLR}, PMOVS{SET,CLR}") changed KVM_SET_ONE_REG to update the mentioned registers in a way matching with the behavior of guest register writes. This is a breaking change of a UAPI though the new semantics looks cleaner and VMMs are not prepared for this. Firecracker, QEMU, and crosvm perform migration by listing registers with KVM_GET_REG_LIST, getting their values with KVM_GET_ONE_REG and setting them with KVM_SET_ONE_REG. This algorithm assumes KVM_SET_ONE_REG restores the values retrieved with KVM_GET_ONE_REG without any alteration. However, bit operations added by the earlier commit do not preserve the values retried with KVM_GET_ONE_REG and potentially break migration. Remove the bit operations that alter the values retrieved with KVM_GET_ONE_REG. Cc: stable@vger.kernel.org Fixes: a45f41d754e0 ("KVM: arm64: Add {get,set}_user for PM{C,I}NTEN{SET,CL= R}, PMOVS{SET,CLR}") Signed-off-by: Akihiko Odaki Acked-by: Marc Zyngier Reviewed-by: Marc Zyngier --- arch/arm64/kvm/sys_regs.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 82430c1e1dd0..ffee72fd1273 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1051,26 +1051,9 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu= , struct sys_reg_params *p, =20 static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, = u64 val) { - bool set; - - val &=3D kvm_pmu_accessible_counter_mask(vcpu); - - switch (r->reg) { - case PMOVSSET_EL0: - /* CRm[1] being set indicates a SET register, and CLR otherwise */ - set =3D r->CRm & 2; - break; - default: - /* Op2[0] being set indicates a SET register, and CLR otherwise */ - set =3D r->Op2 & 1; - break; - } - - if (set) - __vcpu_sys_reg(vcpu, r->reg) |=3D val; - else - __vcpu_sys_reg(vcpu, r->reg) &=3D ~val; + u64 mask =3D kvm_pmu_accessible_counter_mask(vcpu); =20 + __vcpu_sys_reg(vcpu, r->reg) =3D val & mask; return 0; } =20 --=20 2.48.1 From nobody Tue Dec 16 11:05:29 2025 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 1E33A1F4169 for ; Sat, 15 Mar 2025 09:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029973; cv=none; b=R6ikb0A+QoTq5+/Kly6VJ2eWgldkaG4AFgL+BhBJ9GPJeks0JC9U6vWe8AlKReStZMF6CC20DLP3edyIlvg57RVqOaHPeeb24vprn4y2ycRvKSznu6GiYalgpE3mELnliiJXeu5YXj8Wy4NIoBjq7U9pR//tum7JMhXjN8DsRi8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029973; c=relaxed/simple; bh=1avxgZwVJG4KZf5fCS1jidC7wJmMcnTsHuVGLTbJhjo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TeRZ2BK8n9rVKiYsgZUqWjg0KXdv92tZQBXyV5mOUr9IZZcooA9sPCNUm7sIp69Qcc2dnHcdYX/XqqhvHl80EHLsD86AHJ5SZ9V+Or+4JyeuUT0zrNd84fn9KtvpsnEG5O8UvLas7Kc1Q75xseiuCmYwTIpXUC5LCXlQ58L5v50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=JPRKBDCn; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="JPRKBDCn" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22349bb8605so61713895ad.0 for ; Sat, 15 Mar 2025 02:12:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1742029971; x=1742634771; 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=RBhoY3JzR5UUf6f22tr97FXC7T2gtMfmjNwq/zidCrE=; b=JPRKBDCnS10QqsfGCuEECpBpS42wqUy2m2RapvWSUJHg+ia6cGD4VCwCdMUm6DzB87 lEQ0SnMHf992JmKeZDh+wNnE0yoLol4MivYs5Y2LnfDtkaudOOLuXvUwGKic724+SX6v VCjR1CZuGSmDAX6KPOfvcmdr0aP7RCqJY5N6pUzuQz2605/IcWfyOVqjaV0bXOft9Vu0 KfL+Mg3cVxuCUWJV4u4uKi+d7FDdailrMD1GZvVJorRq8ZxzFSMXJ0b2OKXDT5oSvtTw P6nKz3qnMeHHy5+/vzQVycnnUoATjQzcRslOFKn2ZFQmIuDOnHGRlLjgrSrzzu0K9MAv 327A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742029971; x=1742634771; 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=RBhoY3JzR5UUf6f22tr97FXC7T2gtMfmjNwq/zidCrE=; b=POV2oOuuAQoGAD6SIIF0R6q/PZi7LARs5JL2oO3BrooBbcJsfI9noZ22LayQoxAZnJ szbo+LryPXV3YGEp1IFmevVarAEPYNuPap6xqSAxZmZyTUv2OmdRDXYfUGY7t8uMyglf sI2XJVsVinrjTyry1AuiWzLgglr3rmWqz6TTi6+KhuO0jOuroGJpBESTEQqHQhkN6KY4 h+I3pBombMxlYt/o+WbEYOXY+cdcz4uO2MeNCKvu4SPrp9pKQIpLoqslmclr6BpWj1Tv l1rDRf30K785/IHOThiOlKoHUBDvMjqwrogFbdltNpZG2Aro+DzQGUA7THp1KcgV+gID 3pwQ== X-Forwarded-Encrypted: i=1; AJvYcCX9bc6SE3wLgnTZduJ3ZNbzVScQvv76tUgE0QHwjcI2+oTVtxO2iqKrx12IdnFfIhQvJwieFlStGVvhL5g=@vger.kernel.org X-Gm-Message-State: AOJu0YxUK3AfJKbZq1t3IsDKcR0j7YH3KmES6Lh6Nn+0IcMw0lbIlDCG fqa0uxsWfEzV5C3iTtwW96a/JyZoeklCyyIpVCMiK0kSlr/KBc3AFvOjZbfZT+w= X-Gm-Gg: ASbGncuC2Up6OjTF8C+NXe2aDSrY1UxrJjA/QXv6PDspd53LE4Kg8I+nlyOqedZA1++ I8OBhr6lqAtV0BmsAgF8d03wmYaHL9/4ZvKK1pX2Ni6O/Aw+/1akGHaDTk4AOtpyZp5/4fGeo7H y2Atn45JRlHjECPlUotCYB0BXL6wLLGOIIxu5QuxNb64WWw/ZQMRWXsBvobswdEBaCp4zf5xi2D q5mqW/OYcJYTBQ3Up54hDfGhCQplElt3aT6wZZ+KLNLqIJqyNEEEaHl/nhjG3WAfiSITjdemkGP /sFmbu+T9dZqqjZ8NHBWP0/hbgDUu8T2e4Em3DWFuNblvsOW X-Google-Smtp-Source: AGHT+IHzLP/Cp0+ZoY1hmXekIVA9+RY6XNkX4cssJ6gV1Uehk1XprCWAd4bfwyV6HDnSKCO9/1qjaA== X-Received: by 2002:a17:903:2b0c:b0:223:faf5:c82 with SMTP id d9443c01a7336-225e0a28898mr57188645ad.8.1742029971350; Sat, 15 Mar 2025 02:12:51 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-30153b994b3sm2327772a91.30.2025.03.15.02.12.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 15 Mar 2025 02:12:51 -0700 (PDT) From: Akihiko Odaki Date: Sat, 15 Mar 2025 18:12:11 +0900 Subject: [PATCH v5 2/5] KVM: arm64: PMU: Assume PMU presence in pmu-emul.c Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250315-pmc-v5-2-ecee87dab216@daynix.com> References: <20250315-pmc-v5-0-ecee87dab216@daynix.com> In-Reply-To: <20250315-pmc-v5-0-ecee87dab216@daynix.com> To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Many functions in pmu-emul.c checks kvm_vcpu_has_pmu(vcpu). A favorable interpretation is defensive programming, but it also has downsides: - It is confusing as it implies these functions are called without PMU although most of them are called only when a PMU is present. - It makes semantics of functions fuzzy. For example, calling kvm_pmu_disable_counter_mask() without PMU may result in no-op as there are no enabled counters, but it's unclear what kvm_pmu_get_counter_value() returns when there is no PMU. - It allows callers without checking kvm_vcpu_has_pmu(vcpu), but it is often wrong to call these functions without PMU. - It is error-prone to duplicate kvm_vcpu_has_pmu(vcpu) checks into multiple functions. Many functions are called for system registers, and the system register infrastructure already employs less error-prone, comprehensive checks. Check kvm_vcpu_has_pmu(vcpu) in callers of these functions instead, and remove the obsolete checks from pmu-emul.c. The only exceptions are the functions that implement ioctls as they have definitive semantics even when the PMU is not present. Signed-off-by: Akihiko Odaki Reviewed-by: Marc Zyngier --- arch/arm64/kvm/arm.c | 17 +++++++++++------ arch/arm64/kvm/emulate-nested.c | 6 ++++-- arch/arm64/kvm/pmu-emul.c | 26 +------------------------- arch/arm64/kvm/sys_regs.c | 6 ++++-- 4 files changed, 20 insertions(+), 35 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 0160b4924351..caa1357fa367 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -835,9 +835,11 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) if (ret) return ret; =20 - ret =3D kvm_arm_pmu_v3_enable(vcpu); - if (ret) - return ret; + if (kvm_vcpu_has_pmu(vcpu)) { + ret =3D kvm_arm_pmu_v3_enable(vcpu); + if (ret) + return ret; + } =20 if (is_protected_kvm_enabled()) { ret =3D pkvm_create_hyp_vm(kvm); @@ -1148,7 +1150,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) */ preempt_disable(); =20 - kvm_pmu_flush_hwstate(vcpu); + if (kvm_vcpu_has_pmu(vcpu)) + kvm_pmu_flush_hwstate(vcpu); =20 local_irq_disable(); =20 @@ -1167,7 +1170,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) if (ret <=3D 0 || kvm_vcpu_exit_request(vcpu, &ret)) { vcpu->mode =3D OUTSIDE_GUEST_MODE; isb(); /* Ensure work in x_flush_hwstate is committed */ - kvm_pmu_sync_hwstate(vcpu); + if (kvm_vcpu_has_pmu(vcpu)) + kvm_pmu_sync_hwstate(vcpu); if (unlikely(!irqchip_in_kernel(vcpu->kvm))) kvm_timer_sync_user(vcpu); kvm_vgic_sync_hwstate(vcpu); @@ -1197,7 +1201,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) * that the vgic can properly sample the updated state of the * interrupt line. */ - kvm_pmu_sync_hwstate(vcpu); + if (kvm_vcpu_has_pmu(vcpu)) + kvm_pmu_sync_hwstate(vcpu); =20 /* * Sync the vgic state before syncing the timer state because diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-neste= d.c index 607d37bab70b..9293fb078fc6 100644 --- a/arch/arm64/kvm/emulate-nested.c +++ b/arch/arm64/kvm/emulate-nested.c @@ -2516,7 +2516,8 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu) kvm_arch_vcpu_load(vcpu, smp_processor_id()); preempt_enable(); =20 - kvm_pmu_nested_transition(vcpu); + if (kvm_vcpu_has_pmu(vcpu)) + kvm_pmu_nested_transition(vcpu); } =20 static void kvm_inject_el2_exception(struct kvm_vcpu *vcpu, u64 esr_el2, @@ -2599,7 +2600,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u= 64 esr_el2, kvm_arch_vcpu_load(vcpu, smp_processor_id()); preempt_enable(); =20 - kvm_pmu_nested_transition(vcpu); + if (kvm_vcpu_has_pmu(vcpu)) + kvm_pmu_nested_transition(vcpu); =20 return 1; } diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 6c5950b9ceac..98fdc65f5b24 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -150,9 +150,6 @@ static u64 kvm_pmu_get_pmc_value(struct kvm_pmc *pmc) */ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) { - if (!kvm_vcpu_has_pmu(vcpu)) - return 0; - return kvm_pmu_get_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx)); } =20 @@ -191,9 +188,6 @@ static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, = u64 val, bool force) */ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 = val) { - if (!kvm_vcpu_has_pmu(vcpu)) - return; - kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false); } =20 @@ -350,7 +344,7 @@ void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vc= pu, u64 val) { int i; =20 - if (!kvm_vcpu_has_pmu(vcpu) || !val) + if (!val) return; =20 for (i =3D 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) { @@ -401,9 +395,6 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu) struct kvm_pmu *pmu =3D &vcpu->arch.pmu; bool overflow; =20 - if (!kvm_vcpu_has_pmu(vcpu)) - return; - overflow =3D kvm_pmu_overflow_status(vcpu); if (pmu->irq_level =3D=3D overflow) return; @@ -599,9 +590,6 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) { int i; =20 - if (!kvm_vcpu_has_pmu(vcpu)) - return; - /* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */ if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5)) val &=3D ~ARMV8_PMU_PMCR_LP; @@ -766,9 +754,6 @@ void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vc= pu, u64 data, struct kvm_pmc *pmc =3D kvm_vcpu_idx_to_pmc(vcpu, select_idx); u64 reg; =20 - if (!kvm_vcpu_has_pmu(vcpu)) - return; - reg =3D counter_index_to_evtreg(pmc->idx); __vcpu_sys_reg(vcpu, reg) =3D data & kvm_pmu_evtyper_mask(vcpu->kvm); =20 @@ -848,9 +833,6 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmce= id1) u64 val, mask =3D 0; int base, i, nr_events; =20 - if (!kvm_vcpu_has_pmu(vcpu)) - return 0; - if (!pmceid1) { val =3D read_sysreg(pmceid0_el0); /* always support CHAIN */ @@ -900,9 +882,6 @@ void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) =20 int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu) { - if (!kvm_vcpu_has_pmu(vcpu)) - return 0; - if (!vcpu->arch.pmu.created) return -EINVAL; =20 @@ -1231,9 +1210,6 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu) unsigned long mask; int i; =20 - if (!kvm_vcpu_has_pmu(vcpu)) - return; - mask =3D __vcpu_sys_reg(vcpu, PMCNTENSET_EL0); for_each_set_bit(i, &mask, 32) { struct kvm_pmc *pmc =3D kvm_vcpu_idx_to_pmc(vcpu, i); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index ffee72fd1273..e8e9c781a929 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1853,12 +1853,14 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcp= u, static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) { - u8 perfmon =3D pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit()); + u8 perfmon; u64 val =3D read_sanitised_ftr_reg(SYS_ID_DFR0_EL1); =20 val &=3D ~ID_DFR0_EL1_PerfMon_MASK; - if (kvm_vcpu_has_pmu(vcpu)) + if (kvm_vcpu_has_pmu(vcpu)) { + perfmon =3D pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit()); val |=3D SYS_FIELD_PREP(ID_DFR0_EL1, PerfMon, perfmon); + } =20 val =3D ID_REG_LIMIT_FIELD_ENUM(val, ID_DFR0_EL1, CopDbg, Debugv8p8); =20 --=20 2.48.1 From nobody Tue Dec 16 11:05:29 2025 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 91A141F63D5 for ; Sat, 15 Mar 2025 09:12:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029978; cv=none; b=IPkqTYKWDhLZZKinCZmjOjcNKhuAVLzJAUTao3uudphTPR/Hr/mKXgkeh26xGlVKDF5UsfrhSwxvcYjS9ULnu2fU1uCN6zloNURx8ZtKlcSrlK2ypW99ZhCsEjY1GwUab/fYGFSJ5z+la+V4DM3jXRt1pz70nlw+3BY0THrmYzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029978; c=relaxed/simple; bh=55NhlBArV0bxMhOMhIHB8X/BAl2ygANfTR+Wz8xFj8s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jAoG2Taf3nzIjr8HdbY295WLR6EPSsvWNLRANkNwJF130APr6m6qtF4lYyUMe27lEEca0SnRYIQXopfM0tBpDp6F9B0MaBTKwSsh+2lWCwJ8PmSA8Wl5y0b7ZYJHMz3ELeKP6VDxLWjVWw9Ho08qSVOkqjpFXNGWJFuAVciklzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=fSIz2ZfJ; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="fSIz2ZfJ" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-22398e09e39so61425225ad.3 for ; Sat, 15 Mar 2025 02:12:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1742029976; x=1742634776; 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=z45wX6sTn0j+WP194BbrDZyhFp7C/BntrOrXCNO4tBM=; b=fSIz2ZfJIpSydL/GmnIwvx/Et0Ty8syFi8rx6sQqDJ4pGDy55RRDVFTENPapmOMijT efcH9dYewzpueVMcTBcdrYilAxdD5rAv4v/5axm6K3ga/19fn5WoZZdl0b2Dab5Plfk/ AKAg6y/kZr0cPdRM7BPp/L7oNtoqKqc+RIYMxogLVbeK2nziVfDFU62lT97L5V/lL38E NUPoyZb3IqXaF2nMNOdgH7zyNuaSFdMq+MCxNWpYTHHC79wMDPdJnOZZvFaWUWjoaQHJ YASsgxcwWiAox2Fp8jANFe+IunOxuoDV9VjyJs/zba4nIzNT5h/pV2IatF5MBJgHtMB7 uX6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742029976; x=1742634776; 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=z45wX6sTn0j+WP194BbrDZyhFp7C/BntrOrXCNO4tBM=; b=vFBWAANg5HOmitkivTN3Su4a0AmYY32f/+Nbc6sl2+5lu+/54B4CQxDu4ZScon5cJ3 3c5xVysj+7TfiOfWohon4MGx6H2YcS+5VTacV60GfeUy8odZzUo1/dG6r/qcaz2BOdvh EOofH/RtJm/ZnjVUjW52tnWM/gPwRInAApM1aDVGeQOy9pKQMwe0Cj0Xz9x6xIBLz6Ru 53tdsqG//HOt2mw4Jr+AQAhOKZ7h0Y9tdwru1vgyXjhXp3n5dNMFtFzjTUJ2wkBbepoG WacXk0qnpjR8H+UokzucilYDclmm+vOzuRPFsYrGZvfPQNOaPyVPRUEv8AZEIIxJDuTZ C2/A== X-Forwarded-Encrypted: i=1; AJvYcCXZTAJOqQnmrXtm/VEf0pJ5EKRPA8YPUTvce4XhC+7cYPX5NQYESuMvuZbKUIt7847NJG8+X8E09tP5Nr0=@vger.kernel.org X-Gm-Message-State: AOJu0YwlO7MqwBZtp+rYHO7HOVD8GU8r3hRunPA7XGEIHKITQfZsnEY7 K1vkFvNhXmNXCkdp46rFEtLnp73fWe/ceaUEdcZqClLQ43+HhkwFf3z9jGNJJjk= X-Gm-Gg: ASbGncuPnwewy3ylXeuwkI0YdliWIeEzs4TPuPE2XTDbk2vyrTg00IPeJbGOTv2rk0O 7Cek7Vhnxocrp8QiGLIt1bVNI8IgEGCIezJ6XOdjxhiy41gNsgkFrcpZTCde0953miWL7A2F9Kc 2eEe2PV1lGUrEF4dta8eXzGA8/CSAiVxv4UJxAmH4WB4F9qmpCypD3JxZ43CQmDWE7OxtVZ4Qlg 6Ig2kpgB5LK1elkQFa8+n39j6DZYm77045ZzyunRcOj2cUsiyH04yane0WmQIHkK4eDe7/lCt3z YqKyTkJr7WEvKKFjQ9kGmy5LWI6evqz4CEt06eyCtAanDyGM X-Google-Smtp-Source: AGHT+IFM01ujgQGmxWWrZqVb8b6dOpUickjIAuSWkmx+lJTnPzesouNeLYIaUrB/yeU1KLg5VJSBcQ== X-Received: by 2002:a05:6a20:2d08:b0:1f3:1d13:96b3 with SMTP id adf61e73a8af0-1f5c1132fcfmr7792054637.5.1742029976132; Sat, 15 Mar 2025 02:12:56 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-af56ea7c7ffsm3280291a12.55.2025.03.15.02.12.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 15 Mar 2025 02:12:55 -0700 (PDT) From: Akihiko Odaki Date: Sat, 15 Mar 2025 18:12:12 +0900 Subject: [PATCH v5 3/5] KVM: arm64: PMU: Fix SET_ONE_REG for vPMC regs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250315-pmc-v5-3-ecee87dab216@daynix.com> References: <20250315-pmc-v5-0-ecee87dab216@daynix.com> In-Reply-To: <20250315-pmc-v5-0-ecee87dab216@daynix.com> To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Reload the perf event when setting the vPMU counter (vPMC) registers (PMCCNTR_EL0 and PMEVCNTR_EL0). This is a change corresponding to commit 9228b26194d1 ("KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current value") but for SET_ONE_REG. Values of vPMC registers are saved in sysreg files on certain occasions. These saved values don't represent the current values of the vPMC registers if the perf events for the vPMCs count events after the save. The current values of those registers are the sum of the sysreg file value and the current perf event counter value. But, when userspace writes those registers (using KVM_SET_ONE_REG), KVM only updates the sysreg file value and leaves the current perf event counter value as is. It is also important to keep the correct state even if userspace writes them after first run, specifically when debugging Windows on QEMU with GDB; QEMU tries to write back all visible registers when resuming the VM execution with GDB, corrupting the PMU state. Windows always uses the PMU so this can cause adverse effects on that particular OS. Fix this by releasing the current perf event and trigger recreating one with KVM_REQ_RELOAD_PMU. Fixes: 051ff581ce70 ("arm64: KVM: Add access handler for event counter regi= ster") Signed-off-by: Akihiko Odaki Reviewed-by: Marc Zyngier --- arch/arm64/kvm/pmu-emul.c | 13 +++++++++++++ arch/arm64/kvm/sys_regs.c | 20 +++++++++++++++++++- include/kvm/arm_pmu.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 98fdc65f5b24..593216bc14f0 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -191,6 +191,19 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, = u64 select_idx, u64 val) kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false); } =20 +/** + * kvm_pmu_set_counter_value_user - set PMU counter value from user + * @vcpu: The vcpu pointer + * @select_idx: The counter index + * @val: The counter value + */ +void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx,= u64 val) +{ + kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, select_idx)); + __vcpu_sys_reg(vcpu, counter_index_to_reg(select_idx)) =3D val; + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); +} + /** * kvm_pmu_release_perf_event - remove the perf event * @pmc: The PMU counter pointer diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index e8e9c781a929..4d1ef47d0049 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -960,6 +960,22 @@ static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const= struct sys_reg_desc *r, return 0; } =20 +static int set_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc= *r, + u64 val) +{ + u64 idx; + + if (r->CRn =3D=3D 9 && r->CRm =3D=3D 13 && r->Op2 =3D=3D 0) + /* PMCCNTR_EL0 */ + idx =3D ARMV8_PMU_CYCLE_IDX; + else + /* PMEVCNTRn_EL0 */ + idx =3D ((r->CRm & 3) << 3) | (r->Op2 & 7); + + kvm_pmu_set_counter_value_user(vcpu, idx, val); + return 0; +} + static bool access_pmu_evcntr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) @@ -1238,6 +1254,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const stru= ct sys_reg_desc *r, #define PMU_PMEVCNTR_EL0(n) \ { PMU_SYS_REG(PMEVCNTRn_EL0(n)), \ .reset =3D reset_pmevcntr, .get_user =3D get_pmu_evcntr, \ + .set_user =3D set_pmu_evcntr, \ .access =3D access_pmu_evcntr, .reg =3D (PMEVCNTR0_EL0 + n), } =20 /* Macro to expand the PMEVTYPERn_EL0 register */ @@ -2835,7 +2852,8 @@ static const struct sys_reg_desc sys_reg_descs[] =3D { .access =3D access_pmceid, .reset =3D NULL }, { PMU_SYS_REG(PMCCNTR_EL0), .access =3D access_pmu_evcntr, .reset =3D reset_unknown, - .reg =3D PMCCNTR_EL0, .get_user =3D get_pmu_evcntr}, + .reg =3D PMCCNTR_EL0, .get_user =3D get_pmu_evcntr, + .set_user =3D set_pmu_evcntr }, { PMU_SYS_REG(PMXEVTYPER_EL0), .access =3D access_pmu_evtyper, .reset =3D NULL }, { PMU_SYS_REG(PMXEVCNTR_EL0), diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 147bd3ee4f7b..b6d0a682505d 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -47,8 +47,10 @@ static __always_inline bool kvm_arm_support_pmu_v3(void) #define kvm_arm_pmu_irq_initialized(v) ((v)->arch.pmu.irq_num >=3D VGIC_NR= _SGIS) u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx); void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 = val); +void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx,= u64 val); u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu); u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu); +u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu); u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1); void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu); void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu); --=20 2.48.1 From nobody Tue Dec 16 11:05:29 2025 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (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 085901F76A8 for ; Sat, 15 Mar 2025 09:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029982; cv=none; b=PBvvaS5rghCUV1Ifu0iM/2I8469MS/hZCvH1cyuD5BM8dCdagtpv4PgyjjerzuHm7WifhUaIhQIf+JkHnv/U1qkq55+qV5K3/6CHVfbR7RyrCxkwDqXTSGWdgrj9ShL67H0Toe3+nvpnjBcRIiBDtDAP49aFhvruOJInj31xs1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029982; c=relaxed/simple; bh=UIs9U4gwmL5870YLJcYNAyG53WiYxibycuWPw35RO2U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=j/QfOMxqHnor6xyE6MiWnEMCuk2H5CLfztbqbF19oS6Nqjie1K9C10N14GHVvEtGKrkau6oZZlZCMxXdBUtWQtNm/ZchMVG9KUWrSYAASTmYtz2C9qi3vZdOCtbYNIS9QsSlNWYq+DV7r3HLOwqgLAs10zNIpzIWifJ/S030h5k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=ge13XMhN; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="ge13XMhN" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2fecba90cc3so1157976a91.2 for ; Sat, 15 Mar 2025 02:13:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1742029980; x=1742634780; 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=/FbB5lDaUNvpC7XuDXTEGvGqSXp+ZEz8J1AwcsET8uY=; b=ge13XMhNQoOiq2Z7rlFyMUPb3SZbgnIkK5FzDvCdBmX+tJ79Cuv0P9/KJo3i3eBjPV jHWXO92YExV6iq8PIOF/C01b7ByLP5WKKAwDiJ0jWbox+sBDNVKHsjQQg1irrcaARX3t iDckjVqZktiQx9lMTjCnbv8bIHcaK096oPkEFZzo/Vq5w6MSU8BmB9iGYQaqnPxz5Vjn GDfCJfYIbw5Z5j+d3eEUUItkuUeaBk2dLdTwqmdU4Tfb67+9DqQkHPkedG72RuhD13tL zzjGKSK/Oy5yCq9Yt1HUn9+FLfRt2lrdE6cGMKokw0FltxgV9tuhJQ1Fo9JXszKG4u0k 6meA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742029980; x=1742634780; 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=/FbB5lDaUNvpC7XuDXTEGvGqSXp+ZEz8J1AwcsET8uY=; b=wdOzsNuyGRhgdg1XZjsVeqfyN9Oy9Ah/NOyEw6wECbDNDZ1ucdFCa/UpMzBIFPyODO aylluuRCc/t7UcbQ+UjMQZZdBanCbFLnKMnbLhbcuSLQgObTWvcibMjW2JbdZ1wb0JgR /G+/mAWwNY/DalFCrkvKnW/foZ8zvfQyIEmJp7LFiywXCzmQIizxe4ZyqB7K+pT/y15I bOJA7dYUP2cngmC04LFQ+kJ193tHHU2vYdF3zNuKYjkn2LWeZUVwt6cuKJYrQC1InTD5 PaJGaPQmADTV/31Z2HvnUmJJyvONfpLw2DPgu/U7+jjhk+5EEjd/8D96nLrS0LPz53eZ /VNw== X-Forwarded-Encrypted: i=1; AJvYcCWo41Fu0Rj9YwauQgu3BLrR6DLjuJwNXbk/EhAFOOPOX/xVQy2hz46+a13Dh0CEL+6afObr4J8Q8mYDUcw=@vger.kernel.org X-Gm-Message-State: AOJu0YyFumAl+58iL6Q6RrNsU7fK0TYTFvtRb0nx7OuQ6PurSFA2wGXQ oFRAodDG1hRWsaCe8jLGCxs3b2h+EOGdMU02ap4tw/cajXG8ToEZ8+Ti597lU/6od1tp+Fddje1 XCvY= X-Gm-Gg: ASbGncu6xmsqmAu9TQuKp9ceEyp6NhwvRPh9+5K4IWCL3OiTTWVG57WnB5HHxIMMk20 PktxcuYuTYa04yXOR/Pqg7IDw3X088V+7/WKGDjOzbMY59hSu44I2jCocLH1prWtqe0RMknDLoL 4e9ChSR7t9n3JUZo2xUmyU3sxsvMEWnFRQ0OVRPoUu17KXyu7Kn7Zzum1lKKkzqKhXMhkoN/nYx 94pX2sraUUqEdOqSvcIELF6oWr8aqbGMhk5/qLdDuGBpMjRj6hrsFIn1aH8uQ4H0fKKN6wZLegp JlkqJrExqHd6Co3IzrdbXjtavCIm6A+cdXxRPFZ2n52cWgsroiFFa8i3SKU= X-Google-Smtp-Source: AGHT+IHaYE3C8lcppzvNgVq++/gQW7jqz6vA6hT6Z68AyNtCpi+E5uRU4W2Fjw9r55KOhScD/9aGrg== X-Received: by 2002:a17:90b:4b83:b0:2ff:6f88:b04a with SMTP id 98e67ed59e1d1-30151ca6de6mr7547868a91.15.1742029980303; Sat, 15 Mar 2025 02:13:00 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3015364ec2bsm2630846a91.46.2025.03.15.02.12.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 15 Mar 2025 02:12:59 -0700 (PDT) From: Akihiko Odaki Date: Sat, 15 Mar 2025 18:12:13 +0900 Subject: [PATCH v5 4/5] KVM: arm64: PMU: Reload when user modifies registers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250315-pmc-v5-4-ecee87dab216@daynix.com> References: <20250315-pmc-v5-0-ecee87dab216@daynix.com> In-Reply-To: <20250315-pmc-v5-0-ecee87dab216@daynix.com> To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Commit d0c94c49792c ("KVM: arm64: Restore PMU configuration on first run") added the code to reload the PMU configuration on first run. It is also important to keep the correct state even if system registers are modified after first run, specifically when debugging Windows on QEMU with GDB; QEMU tries to write back all visible registers when resuming the VM execution with GDB, corrupting the PMU state. Windows always uses the PMU so this can cause adverse effects on that particular OS. The usual register writes and reset are already handled independently, but register writes from userspace are not covered. Trigger the code to reload the PMU configuration for them instead so that PMU configuration changes made by users will be applied also after the first run. Signed-off-by: Akihiko Odaki Reviewed-by: Marc Zyngier --- arch/arm64/kvm/pmu-emul.c | 3 --- arch/arm64/kvm/sys_regs.c | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 593216bc14f0..8e10124a7420 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -917,9 +917,6 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu) return -EINVAL; } =20 - /* One-off reload of the PMU on first run */ - kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); - return 0; } =20 diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 4d1ef47d0049..727579acc7f6 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1070,6 +1070,8 @@ static int set_pmreg(struct kvm_vcpu *vcpu, const str= uct sys_reg_desc *r, u64 va u64 mask =3D kvm_pmu_accessible_counter_mask(vcpu); =20 __vcpu_sys_reg(vcpu, r->reg) =3D val & mask; + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); + return 0; } =20 @@ -1228,6 +1230,8 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const stru= ct sys_reg_desc *r, val |=3D ARMV8_PMU_PMCR_LC; =20 __vcpu_sys_reg(vcpu, r->reg) =3D val; + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); + return 0; } =20 --=20 2.48.1 From nobody Tue Dec 16 11:05:29 2025 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 23F291F8697 for ; Sat, 15 Mar 2025 09:13:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029986; cv=none; b=eZDtnptQio30pxwQuva++g597v87whph989QH+vscV2+xAyf8tjk/8Hfan3PzPqSxFo1+twVWqXOgdng6WhiD0DSG3wFzDqFfhlAgNKNxUH25puzFYnRMEEE47xWq43iRi9iBeeGJKiIiuP5mvh3Ht5XHkyG4YIiIRDZ8W5OAQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742029986; c=relaxed/simple; bh=cpCjePvPaysnhttxyP2q0T1A8fwc/2SmBzZwgVCNmyw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Lnc2hrjKrj1EwabZt4CjsEpK4xx6HuwqswIbbEkBTe3qlPWWQelZiNhH+Hp63Be1T1eYBfACZpq2AGxEFR/DbW8ME+rSsoGoX5UEW3bxQos7iXr7KkBNy0Iy91WgsCOCty0grgVqXTY1xmpQ2B+L94+7i7iuc/PBilHvuwBJLys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=xbqEMg4R; arc=none smtp.client-ip=209.85.216.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="xbqEMg4R" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2ff615a114bso639248a91.0 for ; Sat, 15 Mar 2025 02:13:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1742029984; x=1742634784; 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=b+bMpkAVOynda5P7ydoOSXioxAp6dNXrbOmJ322faHM=; b=xbqEMg4RNZLbQ/ffdW5prQiAnFGEIC5Uq2A81cy16kH40LM1lVqJ1BEXw58Q0lwfFN 5IZ0dz9Z7uLuPaHiLfq/lfBvjvCujCdbrcnGrpUZfwSj+qvTEb+0nTbHYV74U7D9zDcE OOs1ME98suwMyWh2uBU5pIypUJc12Y5Yt2x+QDqXwXM231vzbNMLTGYYrmGHAm47rBhB hgAHQ3oWVppkU6y8VMi0MuypqUjOa0AVSaVdJMzglGhfoqIyRH41NJEjzy73lobKu+Sg 3c/CUN8Y+gLpk2qPWxUeDl5RMA4XlTxjZDTIdEuh1qchLPkpmogZJxpjdE0Cb/0LMxME NaUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742029984; x=1742634784; 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=b+bMpkAVOynda5P7ydoOSXioxAp6dNXrbOmJ322faHM=; b=Y8lbvmgHN6F3SzAnfiUKoUGsNPnPDKh1qSEmkf/PluMLD7pJcFsu6abwCcr5pF44g9 Tj3LOGLuUc9Bh6/KcnW4XD/8PEyRALFN8yx2u/8nZwWqHDCL8nj1Ptt1nHf+QfjwXVcI o/odTZuAB9xtvu/P7IU5SPUxYZS71TPNuc60lN2YPE/pvrbCE+LsjxN4L2Qahe4Ax2dg hpb7M8qfHm05qBKDMEAluI4dIzGvuJuCmrb0sWZGiBV829T2Votdgi8WMgqTVIJz8PPH 3M7MjabFw5Jvti4G1mcFDZRUKf5vP64fgrGJflnui1H8gC9x4vsTRbVYrmtqFYm0mIoX ih2w== X-Forwarded-Encrypted: i=1; AJvYcCV3yLwwbuTY9HI5wpRrEoeMrK3NwjB93PVnI9Dzw3bPDda3A9uVcBEIoMd1W18LpNY0zRO/csEarwn6Htw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1AI15VwauC4oigCxzRXFiEr6F+vppGaTBqOyM2kP5ouiyING1 Ii9rhWUIIUqrHZ8N7Rp9n9SKQHtc78WDhJ/SQGSf8aKATHTPLwrVSs1jYnMk8HM= X-Gm-Gg: ASbGncuTe2yVpGcTRhgZ9BsU2b8lZ6+HgN1PHRUXbIMEefTrXyTb8ZhNMAQRdpZR53I HpqxhxcFun1NmgKWENHnpCiwGAirk0ISt8hRTd8r2eClddesDuokiAlP2JLOmEOufePqPficeuA r6stdhpGcmJbOr7FZdau4BaaL+DucZ8TZsXgFbI11n8aA2hRicgpYJcPI6uX1nkD5a6xX3C6/Cy ezj9EcWiPfgbkvlqjJ5TrF13myUSmyj25CnDGcGq9psqI3jUUU630h3PQSZPx2U4oPqKtJyJ2k+ ESi++CuwkngS0EVaY/nI5JNuDk39UL4An71SGlJM/hUYFgTroyacjQBnAvI= X-Google-Smtp-Source: AGHT+IF0dYo0boBR+9bl0vawDxMBgd8T0cqY/hF/tRnsNt/62E4WUY/Xos2ArXYf2sRSNXond0EV3Q== X-Received: by 2002:a05:6a21:69b:b0:1e1:9e9f:ae4 with SMTP id adf61e73a8af0-1f5c2952640mr6319263637.13.1742029984462; Sat, 15 Mar 2025 02:13:04 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-7371155aa8bsm4082744b3a.70.2025.03.15.02.13.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 15 Mar 2025 02:13:04 -0700 (PDT) From: Akihiko Odaki Date: Sat, 15 Mar 2025 18:12:14 +0900 Subject: [PATCH v5 5/5] KVM: arm64: PMU: Reload when resetting Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250315-pmc-v5-5-ecee87dab216@daynix.com> References: <20250315-pmc-v5-0-ecee87dab216@daynix.com> In-Reply-To: <20250315-pmc-v5-0-ecee87dab216@daynix.com> To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Replace kvm_pmu_vcpu_reset() with the generic PMU reloading mechanism to ensure the consistency with system registers and to reduce code size. Signed-off-by: Akihiko Odaki Reviewed-by: Marc Zyngier --- arch/arm64/kvm/pmu-emul.c | 14 -------------- arch/arm64/kvm/reset.c | 3 --- arch/arm64/kvm/sys_regs.c | 3 +++ include/kvm/arm_pmu.h | 2 -- 4 files changed, 3 insertions(+), 19 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 8e10124a7420..aae5713d8993 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -254,20 +254,6 @@ void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) pmu->pmc[i].idx =3D i; } =20 -/** - * kvm_pmu_vcpu_reset - reset pmu state for cpu - * @vcpu: The vcpu pointer - * - */ -void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) -{ - unsigned long mask =3D kvm_pmu_implemented_counter_mask(vcpu); - int i; - - for_each_set_bit(i, &mask, 32) - kvm_pmu_stop_counter(kvm_vcpu_idx_to_pmc(vcpu, i)); -} - /** * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu * @vcpu: The vcpu pointer diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 803e11b0dc8f..f82fcc614e13 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -196,9 +196,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu) vcpu->arch.reset_state.reset =3D false; spin_unlock(&vcpu->arch.mp_state_lock); =20 - /* Reset PMU outside of the non-preemptible section */ - kvm_pmu_vcpu_reset(vcpu); - preempt_disable(); loaded =3D (vcpu->cpu !=3D -1); if (loaded) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 727579acc7f6..14f66c7a4545 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -4480,6 +4480,9 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu) } =20 set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags); + + if (kvm_vcpu_has_pmu(vcpu)) + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); } =20 /** diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index b6d0a682505d..d6ad13925978 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -53,7 +53,6 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu= ); u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu); u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1); void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu); -void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu); void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu); void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu); @@ -126,7 +125,6 @@ static inline u64 kvm_pmu_accessible_counter_mask(struc= t kvm_vcpu *vcpu) return 0; } static inline void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) {} -static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {} static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {} static inline void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u= 64 val) {} static inline void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) {} --=20 2.48.1