From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 1E5BE198E90 for ; Mon, 23 Dec 2024 11:28:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734953292; cv=none; b=X0bOhg+QZZwNjc/FfiPzCre1qMRfbRt7PvRjiDKlokf6LY+d7mCWGmKUpVpovtG3ifvCdqLuor4yxzEAL2psCJo3QWPTYY8OJFk52+KN1jkaQrglwgmHa2r4s2jjAxYH2vlPQHUjdcKW3er74NIq/KMmsOI0dWcBR48p2rUARp0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734953292; c=relaxed/simple; bh=323Z4w2bQUcSRLtbflCw+USwNfTaxhPbVFQeDyKF7xQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Xd3c+xKImFFMu/dqyUGBLEz74LTY1TDZCXMwqoMWSs8slzASfqwe8rKR3iTH8LhZHaO1oF9sJI3xl5468rYsj4WF3qQHO2YLIDOsZCRZIJp2Y40mSKDFZCmEImJyd5+XZtSYnze6VBHTMVt7k9YH8KoVQdWJSXfO9Y5WZtiGWVk= 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=TOfRIyjo; arc=none smtp.client-ip=209.85.128.45 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="TOfRIyjo" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4363dc916ceso30914405e9.0 for ; Mon, 23 Dec 2024 03:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1734953288; x=1735558088; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Wgzz0gsE3gqrFwP8Idx/74NN9Vh0nBG1Pbn09jOALdI=; b=TOfRIyjo7xgiKs1AloC/qPDdcP98pCR+kej4DVds1HdbrORODcBiNoOfwJbpVn1hbp xioqY+HLvgQuZoCn1rSE77F3w3nM8bPicK/Gui4MwakjzAv86muuCCzRuO4rapAaM3rk 9cA0cBP3JEO0gGyhcqvkZEM6WNsAzyh1/V2epA8Qav4cJswRvaImKZs41B6N7cpQw6S0 N1Z5+pwAcF1zlWAMooG7GdAQlnDkxQKvizig48K8+owvLBpUKx14zQ3ikJdLtufBU1j/ hb+zi73wlLZBAhs6zjNp0J/EgNq7b/S7H3iIaTlsBg3nY7F38g0l8Xl5yUFSZGGV5ZFY 2htw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734953288; x=1735558088; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Wgzz0gsE3gqrFwP8Idx/74NN9Vh0nBG1Pbn09jOALdI=; b=XYJpCYHMA/3TKIxFIC9tuaTsUkEhlzolJO4/UYBz56cifXIDu2gzIpZtebSRCSRFoA MF0/z+7bUhdDwNdak7UHL9ArBMQUBeTksXTR5lDa91ZPWmhuH3N0mCQJvfWmsU7RLDON 6X6BAkdBvD8GpIzXDAYgIhzayqLC2m5+zSMn1IAX39X9E2W1a7BabrSiStW4rCONZ44n OalAofxec2irEK29ObOhmhQWIlpENOxcWe0weCgMjd7upvz70fZ1wZko9p6XlcS+0H+k ouLIPGB0BaqfTbmapYCXzB8/dhWQkVPC2RcSvsi2lHHSmRXQUN3luLnw9yj4OKYPSCGN EhsA== X-Forwarded-Encrypted: i=1; AJvYcCW0hYqQjs5cpxC4nvSPN9Qbyh5sFuGSh/Q/tbn+H7q+cWcEfizrmw1Nh9oP/3CINEgA4EWAaUtyj/W6EAg=@vger.kernel.org X-Gm-Message-State: AOJu0YyHFioiqjVXDTKVLlm1EFM5eN1YzwnrNGId6Y6VF/NpBzNEC4Rk +uYpqN4uHuNis6/omSi4xMXJw19MwWbiG7sJLTfEat6KZw1HRMSRP4RRHMQNg/U= X-Gm-Gg: ASbGnctz8bbLyqdf5Tgi1+GRU6jVJEZpf6bfo+5sIA6dER5CHCpX5sAumYkXzoeBmmc J8FGJdSnl+krx4oSYuOdjZATDcbXV4YaNMfRiAwJ+s5bfC56yTlFwBwPvjYi2rHXQSEKhnsHSLe nNydrOuvmwSS2YH1l5qpqV6M7Jff6VAp55o/1Nyje/ZmkLABYCR07gdGj+OouLbx/BuaSYx8h5Z oXVwrUWpCe6EBg84zZYasP0Wb6YC6V2n5h52HI1RfcQvO337CntGIjPcPf/WrJ49Q== X-Google-Smtp-Source: AGHT+IHm0ZFL9nIVAaexFN4PWOjn7+ySvP/pC4MHFJJmDVZySNdmlh63s633dEeCGONBXuT62LRO2Q== X-Received: by 2002:a05:600c:35c8:b0:434:e892:1033 with SMTP id 5b1f17b1804b1-4365c763d24mr135710245e9.2.1734953288409; Mon, 23 Dec 2024 03:28:08 -0800 (PST) Received: from [192.168.68.163] ([145.224.66.70]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43656b119ccsm167419945e9.24.2024.12.23.03.28.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 Dec 2024 03:28:08 -0800 (PST) Message-ID: <0cfda877-7c3d-4e59-a1b1-fe245f7298f0@linaro.org> Date: Mon, 23 Dec 2024 11:28:06 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 7/8] KVM: arm64: Support trace filtering for guests To: Marc Zyngier , Oliver Upton Cc: kvmarm@lists.linux.dev, suzuki.poulose@arm.com, coresight@lists.linaro.org, Joey Gouly , Zenghui Yu , Catalin Marinas , Will Deacon , Mike Leach , Alexander Shishkin , Mark Brown , Anshuman Khandual , James Morse , "Rob Herring (Arm)" , Shiqi Liu , Fuad Tabba , Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20241127100130.1162639-1-james.clark@linaro.org> <20241127100130.1162639-8-james.clark@linaro.org> <86r061p63b.wl-maz@kernel.org> Content-Language: en-US From: James Clark In-Reply-To: <86r061p63b.wl-maz@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 21/12/2024 12:34 pm, Marc Zyngier wrote: > On Wed, 27 Nov 2024 10:01:24 +0000, > James Clark wrote: >> >> For nVHE, switch the filter value in and out if the Coresight driver >> asks for it. This will support filters for guests when sinks other than >> TRBE are used. >> >> For VHE, just write the filter directly to TRFCR_EL1 where trace can be >> used even with TRBE sinks. >> >> Signed-off-by: James Clark >> --- >> arch/arm64/include/asm/kvm_host.h | 5 +++++ >> arch/arm64/kvm/debug.c | 28 ++++++++++++++++++++++++++++ >> arch/arm64/kvm/hyp/nvhe/debug-sr.c | 1 + >> 3 files changed, 34 insertions(+) >> >> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h >> index ba251caa593b..cce07887551b 100644 >> --- a/arch/arm64/include/asm/kvm_host.h >> +++ b/arch/arm64/include/asm/kvm_host.h >> @@ -613,6 +613,7 @@ struct kvm_host_data { >> #define KVM_HOST_DATA_FLAG_HAS_SPE 0 >> #define KVM_HOST_DATA_FLAG_HAS_TRF 1 >> #define KVM_HOST_DATA_FLAG_TRBE_ENABLED 2 >> +#define KVM_HOST_DATA_FLAG_GUEST_FILTER 3 > > Guest filter what? This is meaningless. > KVM_HOST_DATA_FLAG_SWAP_TRFCR maybe? >> unsigned long flags; >> >> struct kvm_cpu_context host_ctxt; >> @@ -1387,6 +1388,8 @@ void kvm_clr_pmu_events(u64 clr); >> bool kvm_set_pmuserenr(u64 val); >> void kvm_enable_trbe(void); >> void kvm_disable_trbe(void); >> +void kvm_set_trfcr(u64 guest_trfcr); >> +void kvm_clear_trfcr(void); >> #else >> static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {} >> static inline void kvm_clr_pmu_events(u64 clr) {} >> @@ -1396,6 +1399,8 @@ static inline bool kvm_set_pmuserenr(u64 val) >> } >> static inline void kvm_enable_trbe(void) {} >> static inline void kvm_disable_trbe(void) {} >> +static inline void kvm_set_trfcr(u64 guest_trfcr) {} >> +static inline void kvm_clear_trfcr(void) {} >> #endif >> >> void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu); >> diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c >> index 0c340ae7b5d1..9266f2776991 100644 >> --- a/arch/arm64/kvm/debug.c >> +++ b/arch/arm64/kvm/debug.c >> @@ -337,3 +337,31 @@ void kvm_disable_trbe(void) >> host_data_clear_flag(TRBE_ENABLED); >> } >> EXPORT_SYMBOL_GPL(kvm_disable_trbe); >> + >> +void kvm_set_trfcr(u64 guest_trfcr) > > Again. Is this the guest's view? or the host view while running the > guest? I asked the question on the previous patch, and you didn't > reply. > Ah sorry missed that one: > Guest value? Or host state while running the guest? If the former, > then this has nothing to do here. If the latter, this should be > spelled out (trfcr_in_guest?), and the comment amended. Yes, the latter, guest TRFCR reads are undef anyway. I can rename this and the host_data variable to be trfcr_in_guest. >> +{ >> + if (is_protected_kvm_enabled() || WARN_ON_ONCE(preemptible())) >> + return; >> + >> + if (has_vhe()) >> + write_sysreg_s(guest_trfcr, SYS_TRFCR_EL12); >> + else { >> + *host_data_ptr(guest_trfcr_el1) = guest_trfcr; >> + host_data_set_flag(GUEST_FILTER); >> + } > > Oh come on. This is basic coding style, see section 3 in > Documentation/process/coding-style.rst. > Oops, I'd have thought checkpatch could catch something like that. Will fix. >> +} >> +EXPORT_SYMBOL_GPL(kvm_set_trfcr); >> + >> +void kvm_clear_trfcr(void) >> +{ >> + if (is_protected_kvm_enabled() || WARN_ON_ONCE(preemptible())) >> + return; >> + >> + if (has_vhe()) >> + write_sysreg_s(0, SYS_TRFCR_EL12); >> + else { >> + *host_data_ptr(guest_trfcr_el1) = 0; >> + host_data_clear_flag(GUEST_FILTER); >> + } >> +} >> +EXPORT_SYMBOL_GPL(kvm_clear_trfcr); > > Why do we have two helpers? Clearly, calling kvm_set_trfcr() with > E{1,0}TRE=={0,0} should result in *disabling* things. Except it > doesn't, and you should fix it. Once that is fixed, it becomes obvious> that kvm_clear_trfcr() serves no purpose. > With only one kvm_set_trfcr() there's no way to distinguish swapping in a 0 value or stopping swapping altogether. I thought we wanted a single flag that gated the register accesses so the hyp mostly does nothing? With only kvm_set_trfcr() you first need to check FEAT_TRF then you need to compare the real register with trfcr_in_guest to know whether to swap or not every time. Actually I think some of the previous versions had something like this but it was a bit more complicated. Maybe set/clear_trfcr() aren't great names. Perhaps kvm_set_trfcr_in_guest() and kvm_disable_trfcr_in_guest()? With the second one hinting that it stops the swapping regardless of what the values are. I don't think calling kvm_set_trfcr() with E{1,0}TRE=={0,0} is actually broken in this version, it means that the Coresight driver wants that value to be installed for guests. So it should actually _enable_ swapping in the value of 0, not disable anything. > To sum it up, KVM's API should reflect the architecture instead of > making things up. > We had kvm_set_trfcr(u64 host_trfcr, u64 guest_trfcr) on the last version, which also serves the same purpose I mentioned above because you can check if they're the same or not and disable swapping. I don't know if that counts as reflecting the architecture better. But Oliver mentioned he preferred it more "intent" based which is why I added the clear_trfcr(). >> diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c >> index 9479bee41801..7edee7ace433 100644 >> --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c >> +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c >> @@ -67,6 +67,7 @@ static void __trace_do_switch(u64 *saved_trfcr, u64 new_trfcr) >> static bool __trace_needs_switch(void) >> { >> return host_data_test_flag(TRBE_ENABLED) || >> + host_data_test_flag(GUEST_FILTER) || >> (is_protected_kvm_enabled() && host_data_test_flag(HAS_TRF)); > > Wouldn't it make more sense to just force the "GUEST_FILTER" flag in > the pKVM case, and drop the 3rd term altogether? > > M. > Yep we can set GUEST_FILTER once at startup and it gets dropped along with HAS_TRF. That's a lot simpler. Thanks James