Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp387766lqo; Thu, 16 May 2024 09:03:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVUdRLmkYWdo+N7HnALz22BS+zLTrWirFlolkb/oJZODCnjNpOVCAwW1Hw99kkRjCY8EAma0S/YM7XH5VCeKJEdpd5elwQBZUUSpmwUdw== X-Google-Smtp-Source: AGHT+IGQ/fDxitwXZ+2ZsXiwBI4LVIpZJCRL/PE9im/0m46bp049WTJXjiILuSQgVjWrk3jW2gjZ X-Received: by 2002:a17:907:9405:b0:a5a:542d:ae0a with SMTP id a640c23a62f3a-a5a542daf06mr1274848766b.63.1715875396410; Thu, 16 May 2024 09:03:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715875396; cv=pass; d=google.com; s=arc-20160816; b=0XRNIywnz2lYRRb2WzYzaNErOquiOA51Xy/ZQ3447wsTQSbOXv0r7jXQBdkHI2ILGp oBKgb6dON7QEh+d9t2Y/VgccyhDl2LQtfsSq/ibBAjquoKGnRCn2Bbosv2aXkmDDCcRw CB3V8o25tjufBQsS0ecN+3YN9YNpRCV66nD6IMQF+G+iS10RTqRkByPvg73Y291o4sPS fPv9DrP4VM0z8PlFS9tCpYjdz3UHVNZh1+bFp8V2SUctw8BLUn1ZOAwc7FGWtTnaIlhC NqKE/0qhSt0yZXFCy1VRDCD4mVIka9/6wFBkGYyUn/ET5JJztdctzfqdcYFQHR97tnXo Lf3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:in-reply-to:date:dkim-signature; bh=7HpIXuT6tOszn6Sdim+VjymIhcvyFpypZfRE8OZjGxU=; fh=V1MVPpLz4hd4+K4Z0gyinwi6wp1LG/CjbcE/9M//aVM=; b=j6Cvu/VtdZM+x6QGTGR2qsb61IrCHVc9AFAEPllCcAWdWS3iKBh7vv0xc5aDYkFGhV b8/8maQW5Pnq+14LkwwDZiBbJNe4n0SxIXAVXuOvh39/A+EmURCTxf/WUdgFIYkU/Uve GQVyyPCbW9qAzxugcgt/ifR3zwZSPyYg9ZKn3/KpTziLj5EaBR2apJDm3/7/19HN0dxQ X6r+zglZQn4MSnWkZULTHwwsAypQFi3X09BT+XFowPDME2ny8sKW+w4u/5Nk1RD8T5xu QmQGkIUCZFKNhaIUJi6zse5bLvcpLMSDZOMOg9vF0LL1e0pdeuCxH0dMW1DqQNMW5Luz tnPQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=siHbNN0H; arc=pass (i=1 spf=pass spfdomain=flex--coltonlewis.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181285-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181285-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17945fcesi887775166b.28.2024.05.16.09.03.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 09:03:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181285-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=siHbNN0H; arc=pass (i=1 spf=pass spfdomain=flex--coltonlewis.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181285-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181285-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id F001A1F21FE2 for ; Thu, 16 May 2024 16:03:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F78A14D719; Thu, 16 May 2024 16:03:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="siHbNN0H" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 8491A14D2AE for ; Thu, 16 May 2024 16:02:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715875381; cv=none; b=YwQI4pa2XkBXGRESB29xXRlyI21pIhIiIf4FeTxW3IZ4K+qSO5lZOAWbymWx9qMm2GG1CfMKfTXVaL6zP2MaCgBGVENDJbLT7PuYtdVm1uNpdjd9mtQiHTqZ4F8KTfIjCXIZyodb4B5/CnMnD4r2ocCSRh6wr5ag/4FJgEYVlfQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715875381; c=relaxed/simple; bh=Qvn+eDPV3KjfuRiCHUcxOODBkfjgRSK4kviR/a5h1b0=; h=Date:In-Reply-To:Mime-Version:Message-ID:Subject:From:To:Cc: Content-Type; b=Usfjt/2fEokzrp1KDD1LoEVJcoZluIKU4xZDP342OCquNFBBW+uvu1YLbqhlG/4xRytlQSi/Y2XAWCpm/nmZntyzxOwwtYoIkEAVlGa67Da9hJLWUkGsQZqxBeNmOnV4TYHybPbOhgVCl20JVWG+VZehgOpqdfJSDGEGT4/uL9k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=siHbNN0H; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6b26783b4so10835007276.0 for ; Thu, 16 May 2024 09:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715875378; x=1716480178; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date:from:to :cc:subject:date:message-id:reply-to; bh=7HpIXuT6tOszn6Sdim+VjymIhcvyFpypZfRE8OZjGxU=; b=siHbNN0Hkag8LErRHX1pAwbMgIKp+zJYlmoN6XdicTxJc/rj0PGpfAKKVkhiovR8Xm PLh6BFoecYu5ynnu7QPuZWBPyIOkPx5JpRvuBMhuzuEwbKoct+2pKt+/1pY8sMNGHLHA upeNUc4mX2sQJn7zCPsmXjz5dTP8k4W/Ar104YMIWFYpitOC8Jnftt//qQrStp8SRnPQ /wwjMG8IHEvXxN8ufI547BvO8fvgFxl2tQn1PIV+lrMSzA9gHDMslurPOG4wUEIGlWAo Sfms6ZGdWxt/MDvj+BygTX2GdTraQjrZzsGRNJ3fDWIMBzqCoCqe7v5fJuoWWjkkfcLR QVFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715875378; x=1716480178; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7HpIXuT6tOszn6Sdim+VjymIhcvyFpypZfRE8OZjGxU=; b=F/TUUqfMkZxBLFTGDhx7lTzpzjJAPZQJp1c11mBHKQP3Dbunn20iG8nrBZC8+zGeZs WeSx0CsoOnPl2PJKpZmMmPRHN2hQr2JDxRy7b1+mwk2nwoFJr2DDcjVrIq/JeVh79Rak uLVdFODO+A1wvzNgs7oJ3sUySK3fDRXnCSbtAcGZUMteXW6zNSAITMGy05vmc+IHJwkq QSrDmc3ffCuLhGR62iBAnp2o3RDMDOwETvy9ljFVO9Bg94FV6sGObjh4OI/9NGpH+INf glA/cxxkIl3Ot8sXBoONkm79fbftfZX9cmJ3a2a3vrrOIaq0ReB38TIqr3ilzuss9y5w 2TLA== X-Forwarded-Encrypted: i=1; AJvYcCU7p7eeNBnVldiHHcXF45erlEobB+56T5h+NJMHapwK8y4Yyt7spFx8LrngyHhZwAsllhgZ4HW42F1gt5KGOM6gUbYh21eZeBdESp35 X-Gm-Message-State: AOJu0YzocYpSM36TItprv+UwRtmWgwQa3/KOOVegbe7YWwiIBk0Z2Fxq LD1s7KILmCNgNFS4+nOEEkbDqhlwS1PUb7XiJpaQ8Gx4zQF735Cj0x4oFg6QEgUBn5V1KQIYZR9 PsHOuAiYNN+FZW7rjzte4Jw== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6902:1201:b0:dee:6f9d:b753 with SMTP id 3f1490d57ef6-dee6f9dbdeamr1196625276.6.1715875378456; Thu, 16 May 2024 09:02:58 -0700 (PDT) Date: Thu, 16 May 2024 16:02:57 +0000 In-Reply-To: <861q69oi9c.wl-maz@kernel.org> (message from Marc Zyngier on Fri, 10 May 2024 15:26:23 +0100) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Message-ID: Subject: Re: [PATCH v5] KVM: arm64: Add early_param to control WFx trapping From: Colton Lewis To: Marc Zyngier Cc: kvm@vger.kernel.org, corbet@lwn.net, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev Content-Type: text/plain; charset="UTF-8"; format=flowed; delsp=yes Hi Marc. Thanks for the review. Marc Zyngier writes: > On Tue, 30 Apr 2024 19:14:44 +0100, > Colton Lewis wrote: >> diff --git a/Documentation/admin-guide/kernel-parameters.txt >> b/Documentation/admin-guide/kernel-parameters.txt >> index 31b3a25680d0..a4d94d9abbe4 100644 >> --- a/Documentation/admin-guide/kernel-parameters.txt >> +++ b/Documentation/admin-guide/kernel-parameters.txt >> @@ -2653,6 +2653,22 @@ >> [KVM,ARM] Allow use of GICv4 for direct injection of >> LPIs. >> + kvm-arm.wfe_trap_policy= >> + [KVM,ARM] Control when to set WFE instruction trap for >> + KVM VMs. >> + >> + trap: set WFE instruction trap >> + >> + notrap: clear WFE instruction trap >> + >> + kvm-arm.wfi_trap_policy= >> + [KVM,ARM] Control when to set WFI instruction trap for >> + KVM VMs. >> + >> + trap: set WFI instruction trap >> + >> + notrap: clear WFI instruction trap >> + > Please make it clear that neither traps are guaranteed. The > architecture *allows* an implementation to trap when no events (resp. > interrupts) are pending, but nothing more. An implementation is > perfectly allowed to ignore these bits. Will do. I'll just add an additional sentence stating "Traps are allowed but not guaranteed by the CPU architecture" >> diff --git a/arch/arm64/include/asm/kvm_host.h >> b/arch/arm64/include/asm/kvm_host.h >> index 21c57b812569..315ee7bfc1cb 100644 >> --- a/arch/arm64/include/asm/kvm_host.h >> +++ b/arch/arm64/include/asm/kvm_host.h >> @@ -67,6 +67,13 @@ enum kvm_mode { >> KVM_MODE_NV, >> KVM_MODE_NONE, >> }; >> + >> +enum kvm_wfx_trap_policy { >> + KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */ >> + KVM_WFX_NOTRAP, >> + KVM_WFX_TRAP, >> +}; > Since this is only ever used in arm.c, it really doesn't need to be > exposed anywhere else. I can move it to there. >> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c >> index a25265aca432..5ec52333e042 100644 >> --- a/arch/arm64/kvm/arm.c >> +++ b/arch/arm64/kvm/arm.c >> @@ -46,6 +46,8 @@ >> #include >> static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; >> +static enum kvm_wfx_trap_policy kvm_wfi_trap_policy = >> KVM_WFX_NOTRAP_SINGLE_TASK; >> +static enum kvm_wfx_trap_policy kvm_wfe_trap_policy = >> KVM_WFX_NOTRAP_SINGLE_TASK; > It would be worth declaring those as __read_mostly. Will do. >> +static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu) >> +{ >> + if (likely(kvm_wfi_trap_policy == KVM_WFX_NOTRAP_SINGLE_TASK)) >> + return single_task_running() && >> + (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) || >> + vcpu->kvm->arch.vgic.nassgireq); > So you are evaluating a runtime condition (scheduler queue length, > number of LPIs)... Yes. Only in the case of default behavior when no option is given, which should be equivalent to what the code was doing before. >> + >> + return kvm_wfi_trap_policy == KVM_WFX_NOTRAP; >> +} >> + >> +static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) >> +{ >> + if (likely(kvm_wfe_trap_policy == KVM_WFX_NOTRAP_SINGLE_TASK)) >> + return single_task_running(); >> + >> + return kvm_wfe_trap_policy == KVM_WFX_NOTRAP; >> +} >> + >> +static inline void kvm_vcpu_reset_hcr(struct kvm_vcpu *vcpu) > Why the inline? Because I moved it from the kvm_emulate.h header with no modification. It doesn't have to be. >> +{ >> + vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; >> + if (has_vhe() || has_hvhe()) >> + vcpu->arch.hcr_el2 |= HCR_E2H; >> + if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) { >> + /* route synchronous external abort exceptions to EL2 */ >> + vcpu->arch.hcr_el2 |= HCR_TEA; >> + /* trap error record accesses */ >> + vcpu->arch.hcr_el2 |= HCR_TERR; >> + } >> + >> + if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) { >> + vcpu->arch.hcr_el2 |= HCR_FWB; >> + } else { >> + /* >> + * For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C >> + * get set in SCTLR_EL1 such that we can detect when the guest >> + * MMU gets turned on and do the necessary cache maintenance >> + * then. >> + */ >> + vcpu->arch.hcr_el2 |= HCR_TVM; >> + } >> + >> + if (cpus_have_final_cap(ARM64_HAS_EVT) && >> + !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE)) >> + vcpu->arch.hcr_el2 |= HCR_TID4; >> + else >> + vcpu->arch.hcr_el2 |= HCR_TID2; >> + >> + if (vcpu_el1_is_32bit(vcpu)) >> + vcpu->arch.hcr_el2 &= ~HCR_RW; >> + >> + if (kvm_has_mte(vcpu->kvm)) >> + vcpu->arch.hcr_el2 |= HCR_ATA; >> + >> + >> + if (kvm_vcpu_should_clear_twe(vcpu)) >> + vcpu->arch.hcr_el2 &= ~HCR_TWE; >> + else >> + vcpu->arch.hcr_el2 |= HCR_TWE; >> + >> + if (kvm_vcpu_should_clear_twi(vcpu)) >> + vcpu->arch.hcr_el2 &= ~HCR_TWI; >> + else >> + vcpu->arch.hcr_el2 |= HCR_TWI; > ... and from the above runtime conditions you make it a forever > decision, for a vcpu that still hasn't executed a single instruction. > What could possibly go wrong? Oh I see. kvm_arch_vcpu_ioctl_vcpu_init only executes once when the vcpu is created (makes sense given the name), thereby making the decision permanent for the life of the vcpu. I misunderstood that fact before. I will move the decision back to when the vcpu is loaded as it was in earlier versions of this series. >> +static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum >> kvm_wfx_trap_policy *p) >> +{ >> + if (!arg) >> + return -EINVAL; >> + >> + if (strcmp(arg, "trap") == 0) { >> + *p = KVM_WFX_TRAP; >> + return 0; >> + } >> + >> + if (strcmp(arg, "notrap") == 0) { >> + *p = KVM_WFX_NOTRAP; >> + return 0; >> + } >> + >> + if (strcmp(arg, "default") == 0) { >> + *p = KVM_WFX_NOTRAP_SINGLE_TASK; >> + return 0; >> + } > Where is this "default" coming from? It's not documented. It was explicitly documented on earlier patch versions, but then I was told we didn't want people to rely on the default behavior so we have flexibility to change it in the future. There isn't much use for it if it isn't documented, so I'll take it out.