From: Anshuman Khandual <anshuman.khandual@arm.com>
To: Mark Rutland <mark.rutland@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, will@kernel.org,
catalin.marinas@arm.com, Mark Brown <broonie@kernel.org>,
James Clark <james.clark@arm.com>, Rob Herring <robh@kernel.org>,
Marc Zyngier <maz@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
linux-perf-users@vger.kernel.org,
Oliver Upton <oliver.upton@linux.dev>,
James Morse <james.morse@arm.com>,
kvmarm@lists.linux.dev
Subject: Re: [PATCH V16 2/8] KVM: arm64: Prevent guest accesses into BRBE system registers/instructions
Date: Fri, 1 Mar 2024 13:16:10 +0530 [thread overview]
Message-ID: <3c5238e5-bf2b-4e6c-a81a-e7d7bb0d9fb4@arm.com> (raw)
In-Reply-To: <ZeB9kHheAGJ__TQU@FVFF77S0Q05N>
On 2/29/24 18:20, Mark Rutland wrote:
> Hi Suzuki,
>
> On Thu, Feb 29, 2024 at 11:45:08AM +0000, Suzuki K Poulose wrote:
>> On 27/02/2024 11:13, Anshuman Khandual wrote:
>>> On 2/27/24 15:34, Mark Rutland wrote:
>>>> On Fri, Feb 23, 2024 at 12:58:48PM +0530, Anshuman Khandual wrote:
>>>>> On 2/21/24 19:31, Mark Rutland wrote:
>>>>>> On Thu, Jan 25, 2024 at 03:11:13PM +0530, Anshuman Khandual wrote:
>>>>>>> Currently BRBE feature is not supported in a guest environment. This hides
>>>>>>> BRBE feature availability via masking ID_AA64DFR0_EL1.BRBE field.
>>>>>>
>>>>>> Does that means that a guest can currently see BRBE advertised in the
>>>>>> ID_AA64DFR0_EL1.BRB field, or is that hidden by the regular cpufeature code
>>>>>> today?
>>>>>
>>>>> IIRC it is hidden, but will have to double check. When experimenting for BRBE
>>>>> guest support enablement earlier, following changes were need for the feature
>>>>> to be visible in ID_AA64DFR0_EL1.
>>>>>
>>>>> diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
>>>>> index 646591c67e7a..f258568535a8 100644
>>>>> --- a/arch/arm64/kernel/cpufeature.c
>>>>> +++ b/arch/arm64/kernel/cpufeature.c
>>>>> @@ -445,6 +445,7 @@ static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
>>>>> };
>>>>> static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
>>>>> + S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_BRBE_SHIFT, 4, ID_AA64DFR0_EL1_BRBE_IMP),
>>>>> S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_DoubleLock_SHIFT, 4, 0),
>>>>> ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_PMSVer_SHIFT, 4, 0),
>>>>> ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_CTX_CMPs_SHIFT, 4, 0),
>>>>>
>>>>> Should we add the following entry - explicitly hiding BRBE from the guest
>>>>> as a prerequisite patch ?
>>
>> This has nothing to do with the Guest visibility of the BRBE. This is
>> specifically for host "userspace" (via MRS emulation).
>>
>>>>>
>>>>> S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_BRBE_SHIFT, 4, ID_AA64DFR0_EL1_BRBE_NI)
>>>>
>>>> Is it visbile currently, or is it hidden currently?
>>>>
>>>> * If it is visible before this patch, that's a latent bug that we need to go
>>>> fix first, and that'll require more coordination.
>>>>
>>>> * If it is not visible before this patch, there's no problem in the code, but
>>>> the commit message needs to explicitly mention that's the case as the commit
>>>> message currently implies it is visible by only mentioning hiding it.
>>>>
>>>> ... so can you please double check as you suggested above? We should be able to
>>>> explain why it is or is not visible today.
>>>
>>> It is currently hidden i.e following code returns 1 in the host
>>> but returns 0 inside the guest.
>>>
>>> aa64dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1);
>>> brbe = cpuid_feature_extract_unsigned_field(aa64dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT);
>>>
>>> Hence - will update the commit message here as suggested.
>>
>> This is by virtue of the masking we do in the kvm/sysreg.c below.
>
> Yep, once this patch is applied.
>
> I think we might have some crossed wires here; I'm only really asking for the
> commit message (and title) to be updated and clarified.
Understood.
>
> Ignoring the patchlet above, and just considering the original patch:
>
> IIUC before the patch is applied, the ID_AA64DFR0_EL1.BRBE field is zero for
> the guest because we don't have an arm64_ftr_bits entry for the
> ID_AA64DFR0_EL1.BRBE field, and so init_cpu_ftr_reg() will leave that as zero
> in arm64_ftr_reg::sys_val, and hence when read_sanitised_id_aa64dfr0_el1()
> calls read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1), the BRBE field will be zero.
Makes sense, but should not arm64_ftr_reg::sys_val be explicitly set to '0' via
ID_AA64DFR0_EL1_BRBE_NI via adding a S_ARM64_FTR_BITS() into ftr_id_aa64dfr0[] ?
OR because it's going to be made visible via S_ARM64_FTR_BITS(FTR_VISIBLE
, ...., ID_AA64DFR0_EL1_BRBE_IMP) for enabling it in the guest, this might not be
necessary for now. Besides it is also being blocked explicitly now via this patch
in read_sanitised_id_aa64dfr0_el1().
>
> This series as-is doesn't add an arm64_ftr_bits entry for ID_AA64DFR0_EL1.BRBE,
> so it'd still be hidden from a guest regardless of whether we add explicit
> masking in read_sanitised_id_aa64dfr0_el1(). The reason to add that masking is
> to be explicit, so that if/when we add an arm64_ftr_bits entry for
> ID_AA64DFR0_EL1.BRBE, it isn't exposed to a guest unexpectedly.
>
> Similarly, IIUC the BRBE register accesses are *already* trapped, and
> emulate_sys_reg() will log a warning an inject an UNDEFINED exception into the
> guest if the guest tries to access the BRBE registers. Any well-behaved guest
> *shouldn't* do that, but a poorly-behaved guest could do that and (slowly) spam
> dmesg with messages about the unhandled sysreg traps. The reasons to handle
> thos regs is largely to suppress that warning, and to make it clear that we
> intend for those to be handled as undef.
Understood.
>
> So the commit title should be something like:
>
> KVM: arm64: explicitly handle BRBE register accesses as UNDEFINED
>
> ... and the message should mention the key points from the above.
>
> Suzuki, does that sound right to you?
>
> Anshuman, can you go re-write the commit message with that in mind?
Sure, will something like the following be okay ?
KVM: arm64: Explicitly handle BRBE register accesses as UNDEFINED
Although ID_AA64DFR0_EL1.BRBE field is zero for the guest because there is
no arm64_ftr_bits[] entry for the ID_AA64DFR0_EL1.BRBE field while getting
processed for read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1), this masks BRBE
feature here to be rather explicit. This will prevent unexpected exposure
of BRBE feature to guest when arm64_ftr_bits[] changes for ID_AA64DFR0_EL1.
This also makes all guest accesses into BRBE registers, and instructions
as undefined access explicitly.
next prev parent reply other threads:[~2024-03-01 7:46 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-25 9:41 [PATCH V16 0/8] arm64/perf: Enable branch stack sampling Anshuman Khandual
2024-01-25 9:41 ` [PATCH V16 1/8] arm64/sysreg: Add BRBE registers and fields Anshuman Khandual
2024-01-25 14:20 ` Mark Brown
2024-02-21 13:52 ` Mark Rutland
2024-02-21 13:59 ` Mark Brown
2024-02-21 14:05 ` Mark Rutland
2024-02-21 14:07 ` Mark Brown
2024-02-23 5:28 ` Anshuman Khandual
2024-02-23 13:31 ` Mark Brown
2024-02-23 6:36 ` Anshuman Khandual
2024-02-26 4:22 ` [PATCH] arm64/hw_breakpoint: Determine lengths from generic perf breakpoint macros Anshuman Khandual
2024-02-26 4:26 ` Anshuman Khandual
2024-02-26 4:24 ` [PATCH] arm64/sysreg: Add BRBE registers and fields Anshuman Khandual
2024-02-26 13:18 ` Mark Brown
2024-02-27 10:06 ` Mark Rutland
2024-01-25 9:41 ` [PATCH V16 2/8] KVM: arm64: Prevent guest accesses into BRBE system registers/instructions Anshuman Khandual
2024-01-29 12:15 ` Suzuki K Poulose
2024-01-30 3:40 ` Anshuman Khandual
2024-02-21 14:01 ` Mark Rutland
2024-02-23 7:28 ` Anshuman Khandual
2024-02-27 10:04 ` Mark Rutland
2024-02-27 11:13 ` Anshuman Khandual
2024-02-29 11:45 ` Suzuki K Poulose
2024-02-29 12:50 ` Mark Rutland
2024-02-29 15:43 ` Suzuki K Poulose
2024-03-01 7:46 ` Anshuman Khandual [this message]
2024-03-01 12:49 ` Mark Rutland
2024-01-25 9:41 ` [PATCH V16 3/8] drivers: perf: arm_pmuv3: Enable branch stack sampling framework Anshuman Khandual
2024-01-25 13:44 ` Suzuki K Poulose
2024-01-29 4:35 ` Anshuman Khandual
2024-02-21 17:25 ` Mark Rutland
2024-03-01 5:37 ` Anshuman Khandual
2024-03-01 13:52 ` Mark Rutland
2024-01-25 9:41 ` [PATCH V16 4/8] drivers: perf: arm_pmuv3: Enable branch stack sampling via FEAT_BRBE Anshuman Khandual
2024-02-21 18:23 ` Mark Rutland
2024-02-28 8:11 ` Anshuman Khandual
2024-02-28 11:52 ` Mark Rutland
2024-02-29 8:55 ` Anshuman Khandual
2024-01-25 9:41 ` [PATCH V16 5/8] KVM: arm64: nvhe: Disable branch generation in nVHE guests Anshuman Khandual
2024-01-29 12:20 ` Suzuki K Poulose
2024-01-30 3:41 ` Anshuman Khandual
2024-02-29 18:40 ` Marc Zyngier
2024-03-01 2:20 ` Anshuman Khandual
2024-01-25 9:41 ` [PATCH V16 6/8] perf: test: Speed up running brstack test on an Arm model Anshuman Khandual
2024-01-25 9:41 ` [PATCH V16 7/8] perf: test: Remove empty lines from branch filter test output Anshuman Khandual
2024-01-25 9:41 ` [PATCH V16 8/8] perf: test: Extend branch stack sampling test for Arm64 BRBE Anshuman Khandual
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3c5238e5-bf2b-4e6c-a81a-e7d7bb0d9fb4@arm.com \
--to=anshuman.khandual@arm.com \
--cc=acme@kernel.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=james.clark@arm.com \
--cc=james.morse@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=mingo@redhat.com \
--cc=oliver.upton@linux.dev \
--cc=peterz@infradead.org \
--cc=robh@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox