From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.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 D66E614AD2D for ; Tue, 7 Jan 2025 11:34:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736249655; cv=none; b=oYdLqYlMO5leO4s3I+e4n3VcckoutRLrraDEVhpKSUE0G8AexFzAwYK0LSH4EUE1W7YzvCELgfPR4e0CFJwlAB0CmrAVZvRhOEUid2NV6qqQp2wBLe+z/3eSQAkop0hs1z2nXzrFXI8FVI4TPah8AXgh+R91qRAVUi/khjiT32s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736249655; c=relaxed/simple; bh=BqBJvaOGlhTt0QNXY2X9hDEmBV0K3J79hXiflyajx+8=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Mrm+I4qiLKpUVHvXSGxSucoSk0J+wUsidKRGC0xVRFAQ4PSIkmTaahBim0CekFt6UDmKMIDMT2WJ+Wv0tSCWgMmrDnommOFE5QwR74bEV/myKM2lqBaixme8uqFj8DrEyL562UZnj/kWj+5ihQWS5jkzucozz7xgef+Y3M6iWrg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=kKPkXDEV; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="kKPkXDEV" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3862ca8e0bbso11695125f8f.0 for ; Tue, 07 Jan 2025 03:34:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736249651; x=1736854451; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=QjoW7nPeQ1gQB5Sat/OF0zaLoESe++zQXRiDsGpHPNI=; b=kKPkXDEVMj9AnXXQOL5NVfS1JaOZRtFxULYlZTMLwKv9XBHxCtUSem4DxHlylqqlDt V2H+/xONgeKMRROgJhnvhhTPfRndaMD0l0Z23X2Rt/DfAW4F0D5YSon3x1VZA24XGQ+L oGn7XFCM9Dt6FzwtgdZsGM4yh+agdh83Asy1vpGmRp3F6RGm3q2GUzcAl1jg912asuiQ obHfOFl7e3Oidy5DBLnWGxYhQjveUZEM2h1DovpXwhR8pTOsjM1KKcrfEt2mssa7OT1/ MzoS/L2SZwBI17UOygkkwjeqDm/nB4GIPYVgZxE2KrTvY621UYI3fO5zQQZN18F2h/q3 aXlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736249651; x=1736854451; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QjoW7nPeQ1gQB5Sat/OF0zaLoESe++zQXRiDsGpHPNI=; b=VxIVradCPqjjl1yUkPgfTcnIaEzrjtHX6YOJcbypFpxjALP4U0inPKLV/7wGORb7h9 IW64++RWXIp4vvcjj2yAFedyw+SStfsPGuUHbCWqbFUByCDe0iwNC9HZUMCqJmAD9+Bx 774HcFBuimu3RaEmEaeGLPb6rmQO/lvvW4duKWQNIHB34jOWQdgz3W9Ny2RAKW3VGJuO aN1MYaJeoBw1Is9quKvdCT/D5spZ9juVgIEj8NRJy5hn3AgKs9aa3NAZPJUyVOlAjOCW reIrV3nFHMOlHbXw5/wKYdNDz7Z03O3eiSJMB+xQ+7KhpW/8ZAslXuEEsIh5Rt1XnwQv NJJg== X-Forwarded-Encrypted: i=1; AJvYcCVAqoK5D0mT9xQqmYSvtejysU4Cl4kH9o+noa6V2l2F2mPtNosWOlz4+MC+6hpXPHY7ePboGijRdTTYucM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1TtXm2HSiyUADOXApl4KIOZlufeAKb3xxJVl38b1fjT/6ICKI np7TI4os41zznaa9mdAhay/beeV1VCFDmYtLe2rhUKMxKS2nav7DJPT14nZu54E= X-Gm-Gg: ASbGncux+srhlgzU1GEQNFZReVg5WMqO96U86W3t6MDj5Cf9eGaullI0VUsO5mB2Dfd 2OfXEG5Tjq3fL1yHuz7m/9GO3hO3F9teeIpPx4Qx3AqV7iyuT5OF43Y6aMuQfs9Yr/X1+jygdme g8UZCBDUFmcHGxyQOIjWQ5qdb1aCZgmurry/rQqdZiAhd7aM+wfVXX8jzvordBjV41kv6rMUcH4 AUmM82KtUanY/MKEqgDLUF5td1Jrfuf+nMNTuXgA5xTtA5dpV9gQ0Pl X-Google-Smtp-Source: AGHT+IHqyrD1eQ8+R2Eoh1fh2j5lDdKTDPWitWk/AFFrwGNk+j09vRhskZzie6luRndcH4/LPLs3Dg== X-Received: by 2002:a5d:64c8:0:b0:386:43c9:3ac2 with SMTP id ffacd0b85a97d-38a221ea74cmr52928364f8f.15.1736249651001; Tue, 07 Jan 2025 03:34:11 -0800 (PST) Received: from pop-os.. ([145.224.66.180]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c89e1acsm50299218f8f.68.2025.01.07.03.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 03:34:10 -0800 (PST) From: James Clark To: maz@kernel.org, kvmarm@lists.linux.dev, oliver.upton@linux.dev, suzuki.poulose@arm.com, coresight@lists.linaro.org Cc: James Clark , Joey Gouly , Zenghui Yu , Catalin Marinas , Will Deacon , Mike Leach , Alexander Shishkin , Mark Rutland , Anshuman Khandual , "Rob Herring (Arm)" , Mark Brown , Shiqi Liu , James Morse , Fuad Tabba , Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 00/10] kvm/coresight: Support exclude guest and exclude host Date: Tue, 7 Jan 2025 11:32:37 +0000 Message-Id: <20250107113252.260631-1-james.clark@linaro.org> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit FEAT_TRF is a Coresight feature that allows trace capture to be completely filtered at different exception levels, unlike the existing TRCVICTLR controls which may still emit target addresses of branches, even if the following trace is filtered. Without FEAT_TRF, it was possible to start a trace session on a host and also collect trace from the guest as TRCVICTLR was never programmed to exclude guests (and it could still emit target addresses even if it was). With FEAT_TRF, the current behavior of trace in guests exists depends on whether nVHE or VHE are being used. Both of the examples below are from the host's point of view, as Coresight isn't accessible from guests. This patchset is only relevant to when FEAT_TRF exists, otherwise there is no change. Current behavior: nVHE/pKVM: Because the host and the guest are both using TRFCR_EL1, trace will be generated in guests depending on the same filter rules the host is using. For example if the host is tracing userspace only, then guest userspace trace will also be collected. (This is further limited by whether TRBE is used because an issue with TRBE means that it's completely disabled in nVHE guests, but it's possible to have other tracing components.) VHE: With VHE, the host filters will be in TRFCR_EL2, but the filters in TRFCR_EL1 will be active when the guest is running. Because we don't write to TRFCR_EL1, guest trace will be completely disabled. New behavior: The guest filtering rules from the Perf session are now honored for both nVHE and VHE modes. This is done by either writing to TRFCR_EL12 at the start of the Perf session and doing nothing else further, or caching the guest value and writing it at guest switch for nVHE. In pKVM, trace is now be disabled for both protected and unprotected guests. There is also an optimization where the Coresight drivers pass their enabled state to KVM. This means in the common case KVM doesn't have to touch any sysregs when the feature isn't in use. Applies to 6.13-rc6 --- Changes since V9 [9]: * Rebase onto 6.13-rc6 and pickup commit 38131c02a53f ("KVM: arm64: Track presence of SPE/TRBE in kvm_host_data instead of vCPU") to get host_data_set_flag(). Also requires two intertwined parent commits. Changes since V8 [8]: * Rename guest_trfcr_el1 -> trfcr_while_in_guest * Rename GUEST_FILTER -> EL1_TRACING_CONFIGURED * Rename kvm_set_trfcr() -> kvm_tracing_set_el1_configuration() * #include ordering * Reorder Coresight driver to remove need for preempt_disable() to avoid the warning * Force EL1_TRACING_CONFIGURED on for pKVM which drops an additional special case but still disables trace * Change set/clear trfcr to a single function that disables swapping if it has the same value as the host * Make the drain condition a bit clearer with __trace_needs_drain() instead of host trfcr != 0 (Or checking individual E*TRE bits) * Drain is only really required on switch to guest so move it there * Only for pKVM, restore the original behavior for draining whenever trbe is enabled. This prevents hypothetical case where a host has the filters disabled but hasn't drained yet which we had by only looking at host trfcr != 0 Changes since V7 [6]: * Drop SPE changes * Change the interface to be based on intent, i.e kvm_enable_trbe() rather than passing the raw register value * Drop change to re-use vcpu_flags mechanism in favour of [7] * Simplify by using the same switch function to and from guest Changes since V6 [5]: * Implement a better "do nothing" case where both the SPE and Coresight drivers give the enabled state to KVM, allowing some register reads to be dropped. * Move the state and feature flags out of the vCPU into the per-CPU host_debug_state. * Simplify the switch logic by adding a new flag HOST_STATE_SWAP_TRFCR and only storing a single TRFCR value. * Rename vcpu flag macros to a more generic kvm_flag... Changes since V5 [4]: * Sort new sysreg entries by encoding * Add a comment about sorting arch/arm64/tools/sysreg * Warn on preemptible() before calling smp_processor_id() * Pickup tags * Change TRFCR_EL2 from SysregFields to Sysreg because it was only used once Changes since V4 [3]: * Remove all V3 changes that made it work in pKVM and just disable trace there instead * Restore PMU host/hyp state sharing back to how it was (kvm_pmu_update_vcpu_events()) * Simplify some of the duplication in the comments and function docs * Add a WARN_ON_ONCE() if kvm_etm_set_guest_trfcr() is called when the trace filtering feature doesn't exist. * Split sysreg change into a tools update followed by the new register addition Changes since V3: * Create a new shared area to store the host state instead of copying it before each VCPU run * Drop commit that moved SPE and trace registers from host_debug_state into the kvm sysregs array because the guest values were never used * Document kvm_etm_set_guest_trfcr() * Guard kvm_etm_set_guest_trfcr() with a feature check * Drop Mark B and Suzuki's review tags on the sysreg patch because it turned out that broke the Perf build and needed some unconventional changes to fix it (as in: to update the tools copy of the headers in the same commit as the kernel changes) Changes since V2: * Add a new iflag to signify presence of FEAT_TRF and keep the existing TRBE iflag. This fixes the issue where TRBLIMITR_EL1 was being accessed even if TRBE didn't exist * Reword a commit message Changes since V1: * Squashed all the arm64/tools/sysreg changes into the first commit * Add a new commit to move SPE and TRBE regs into the kvm sysreg array * Add a comment above the TRFCR global that it's per host CPU rather than vcpu Changes since nVHE RFC [1]: * Re-write just in terms of the register value to be written for the host and the guest. This removes some logic from the hyp code and a value of kvm_vcpu_arch:trfcr_el1 = 0 no longer means "don't restore". * Remove all the conditional compilation and new files. * Change the kvm_etm_update_vcpu_events macro to a function. * Re-use DEBUG_STATE_SAVE_TRFCR so iflags don't need to be expanded anymore. * Expand the cover letter. Changes since VHE v3 [2]: * Use the same interface as nVHE mode so TRFCR_EL12 is now written by kvm. [1]: https://lore.kernel.org/kvmarm/20230804101317.460697-1-james.clark@arm.com/ [2]: https://lore.kernel.org/kvmarm/20230905102117.2011094-1-james.clark@arm.com/ [3]: https://lore.kernel.org/linux-arm-kernel/20240104162714.1062610-1-james.clark@arm.com/ [4]: https://lore.kernel.org/all/20240220100924.2761706-1-james.clark@arm.com/ [5]: https://lore.kernel.org/linux-arm-kernel/20240226113044.228403-1-james.clark@arm.com/ [6]: https://lore.kernel.org/kvmarm/20241112103717.589952-1-james.clark@linaro.org/T/#t [7]: https://lore.kernel.org/kvmarm/20241115224924.2132364-4-oliver.upton@linux.dev/ [8]: https://lore.kernel.org/linux-arm-kernel/20241127100130.1162639-1-james.clark@linaro.org/ [9]: https://lore.kernel.org/linux-arm-kernel/20250106142446.628923-1-james.clark@linaro.org/ James Clark (7): arm64/sysreg: Add a comment that the sysreg file should be sorted tools: arm64: Update sysreg.h header files arm64/sysreg/tools: Move TRFCR definitions to sysreg coresight: trbe: Remove redundant disable call KVM: arm64: coresight: Give TRBE enabled state to KVM KVM: arm64: Support trace filtering for guests coresight: Pass guest TRFCR value to KVM Oliver Upton (3): KVM: arm64: Drop MDSCR_EL1_DEBUG_MASK KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts KVM: arm64: Track presence of SPE/TRBE in kvm_host_data instead of vCPU arch/arm64/include/asm/kvm_asm.h | 5 +- arch/arm64/include/asm/kvm_host.h | 37 +- arch/arm64/include/asm/sysreg.h | 12 - arch/arm64/kvm/arm.c | 5 +- arch/arm64/kvm/debug.c | 94 ++-- arch/arm64/kvm/hyp/nvhe/debug-sr.c | 72 +-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 - arch/arm64/kvm/hyp/vhe/debug-sr.c | 5 - arch/arm64/tools/sysreg | 38 ++ .../coresight/coresight-etm4x-core.c | 49 ++- drivers/hwtracing/coresight/coresight-etm4x.h | 2 +- drivers/hwtracing/coresight/coresight-priv.h | 3 + .../coresight/coresight-self-hosted-trace.h | 9 - drivers/hwtracing/coresight/coresight-trbe.c | 15 +- tools/arch/arm64/include/asm/sysreg.h | 410 +++++++++++++++++- tools/include/linux/kasan-tags.h | 15 + 16 files changed, 626 insertions(+), 151 deletions(-) create mode 100644 tools/include/linux/kasan-tags.h -- 2.34.1