From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (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 C9B943AD52E for ; Fri, 24 Jul 2026 20:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784925156; cv=none; b=sDA1cWXQ//mcTYndEJD+IPGqJiw38kgDb9RGXRreCs9JAzNxgdYS6ON+ls9MwdEes15yWFn9gOtv9dOfM2Ej6txSbCVJJNMdx49schiSTSFeXy012C6OpE/+YLcrRCrBK4ANsE9p+tUULI35tw40vnZC5Q1pN7N1dIzXH648iqU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784925156; c=relaxed/simple; bh=wbvnFfXVNNyCetXL1G+HGd5vNzk6KZOEDyK1EU9PmL0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=owHU9KAqEusvFK0DjwQoVZH0FjXkMfS8P3+2Va8TBK/LEidil5Cy+SCZLZfr+jJbLQ/F2F8xKhFGqa8BqEBwfoOLPV3Bca2s/o2Kn9I9lDG6hwlhRmw5dO51xXg6xdEq8lvbermad3F3t02PBnDxKjuhK2lI0we1bRMJJ8G8D6A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Wcoa9gmc; arc=none smtp.client-ip=209.85.214.198 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Wcoa9gmc" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2cfbf3e4a63so20289195ad.0 for ; Fri, 24 Jul 2026 13:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784925154; x=1785529954; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=8vQ5j8kt71qvQYEPkJl0026Rq7b2mwI8dRhp5tUeKPk=; b=Wcoa9gmcUSG+7x8634quKwrSBSZilETuA9U6UBWd51DvZDYK7W/Uw+8q5V2NcGKE1N 8q2B0yb7Qh+RGGW73FuCeBa9ePI9YqrJkyH2wkGCpLHXDbwrlX5U1WL6/4SDklHzJ9su LBQR/XzFh+7DpD7aQTKTzrNcD27cHRslFTjoG4PtprwFY6exRBt2nCwV1BqRsIrTVuMW BOXg2N+14cA9wPnnsne5cGjWvNiPAMWzasfZu1neqhlpDpBGDnDXQHUoHBCoLhcUdvmT kL4Oz7UOAoOcOkuPR3XJHovVkhTcQj05jTcp5zr+IZGcQj5E/raDu8u4w+iLzvnxQ0JY N3Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784925154; x=1785529954; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8vQ5j8kt71qvQYEPkJl0026Rq7b2mwI8dRhp5tUeKPk=; b=ZhlEzkiJ+wRpvMQpdOdUXVO7KG4qbtauQVbKNYEXZW7scRAopUYBsW/j8DFOfH6aiJ yFZpHSKJXjwkZaNjQ7Xjc6OsgG/K2gll4gicwZ4UErwI9EcJHdEKaTjjfXeELrXlLPe8 l3YuUpuqQZQMR/tKn6u9ZsWimAcxhnYiXXFVcDn/A4XUuSE0vvqc2ybDh80ozpWmKs75 KN6n/5nD+bw0VIZ89uNGHUBkuJIWgjGoT+RSUaORmyv5Z/kKiCRQo263ZngjGgCE0TyX y+F7dQ59WkeqfmDSw1ttPBv8cH0/JZugxCtifQZXuYvqqnKRSJMnEsETABDeJCc1vCo5 IJ4g== X-Forwarded-Encrypted: i=1; AHgh+Rqfh0KXF2PS6Id4LDQYKrpyDSWzuDOJn/Tg7hKcwbVR205nAyeBzXLSwuvOdBMLj3Vs6F/LbngiXKh8UGE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz614cmAP8y2U17W+viy2DN6DSWDNzfEJihDK6ryqaxBxnI5VAc hxQ5ha3pSUqQBEmczb7Iv7P7CkripRaPSE2hKsyXq+i+z8pykAstVcD0Rz9P2VivTBJzWQ9v3yG aecWX0A== X-Received: from plbkt11.prod.google.com ([2002:a17:903:88b:b0:2ca:b8f5:2d26]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c94d:b0:2bf:dd0:c8b1 with SMTP id d9443c01a7336-2cfde56bf07mr370435ad.0.1784925154059; Fri, 24 Jul 2026 13:32:34 -0700 (PDT) Date: Fri, 24 Jul 2026 13:32:33 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260629183746.699840-1-yosry@kernel.org> <20260629183746.699840-11-yosry@kernel.org> Message-ID: Subject: Re: [PATCH v3 10/10] KVM: selftests: Trigger L2->L1 exits stress save+restore and #PF test From: Sean Christopherson To: Yosry Ahmed Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Fri, Jul 24, 2026, Yosry Ahmed wrote: > On Fri, Jul 24, 2026 at 11:25=E2=80=AFAM Sean Christopherson wrote: > > > > On Fri, Jul 24, 2026, Yosry Ahmed wrote: > > > On Fri, Jul 24, 2026 at 10:50=E2=80=AFAM Sean Christopherson wrote: > > > > > static bool parse_args_nested(int argc, char *argv[]) > > > > > { > > > > > bool nested =3D false; > > > > > @@ -192,10 +218,13 @@ int main(int argc, char *argv[]) > > > > > gva_t gva; > > > > > u64 pte; > > > > > > > > > > + TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXCEPTION_PAYLOAD)); > > > > > > > > But KVM_CAP_EXCEPTION_PAYLOAD _isn't_ required, it's an optional fe= ature. Actually, > > > > this is ridiculous. The test is injecting a #UD, it doesn't have a= payload. > > > > Bad AI, bad. > > > > > > KVM_CAP_EXCEPTION_PAYLOAD is required to inject a *pending* exception= , > > > which is needed as KVM won't check for interception on already > > > injected exceptions. I will add a comment. > > > > Drop the TEST_REQUIRE(), and instead only inject the pending #UD if the= CAP > > is supported. >=20 > I prefer to keep the TEST_REQUIRE() as I don't like the test coverage > silently changing based on the available caps tbh. That ship already sailed by agreeing to make nested support conditional :-) > Does it matter in practice? IIUC it's only possible for KVM_CAP_EXCEPTION= _PAYLOAD > to not exist if the test is run on an older kernel, right? Correct, but people do run upstream selftests on older kernels, myself incl= uded. We do effectively break/drop backwards compatibility if there's sufficient justification, e.g. KVM_SET_USER_MEMORY_REGION2 is required for tests that = create memslots so that selftests doesn't have to carry fallback code, but I don't= think this one-off test qualifies for exemption. > > > > > + > > > > > nested =3D parse_args_nested(argc, argv); > > > > > > > > > > vm =3D vm_create_with_one_vcpu(&vcpu, nested ? l1_guest_cod= e : guest_access_memory); > > > > > vm_install_exception_handler(vm, PF_VECTOR, guest_pf_handle= r); > > > > > + vm_enable_cap(vm, KVM_CAP_EXCEPTION_PAYLOAD, -2ul); > > > > > > > > -2ul? > > > > > > That was actually me, not the AI. Apparently the other selftests also > > > write -2ul for some reason. I was too lazy to go change them and/or > > > figure out why -2ul, it didn't make any sense to me. I chose the lazy > > > option and just used the same thing here. > > > > > > Someone named Sean Christopherson wrote the other two, maybe we can a= sk him? :P > > > > Nah, that guy's an asshole, not worth your time. > > > > > > > if (nested) { > > > > > TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cp= u_has(X86_FEATURE_VMX)); > > > > > @@ -270,8 +299,16 @@ int main(int argc, char *argv[]) > > > > > > > > > > state =3D vcpu_save_state(vcpu); > > > > > > > > > > + /* > > > > > + * If the vCPU is in guest mode, inject a #UD to tr= igger an > > > > > + * L2->L1 VM-Exit every other iteration. > > > > > + */ > > > > > + if (nested && vcpu_state_is_guest_mode(state) && co= unt % 2 =3D=3D 0) > > > > > > > > Checking "nested" here is unnecessary. > > > > > > It is, actually. Sashiko pointed out [1] that > > > vcpu_state_is_guest_mode() will read uninitialized memory otherwise. > > > > > > [1]https://sashiko.dev/#/patchset/20260518202514.2037078-1-yosry%40ke= rnel.org?part=3D8 > > > > Oof. That's subtle. At the risk of true evil, what if we did: >=20 > As I mentioned above, I prefer taking a dependency on > KVM_CAP_EXCEPTION_PAYLOAD rather than varying coverage. At least for > the nested case, I think I can move the TEST_REQUIRE() into the > 'nested' case, but then it's probably better to leave it as an arg to > allow for meaningfully running the test in L0. The problem is that TEST_REQUIRE() skips the whole test, regardless of when= it's called. I.e. if you go that route, passes for the non-nested case will sho= w up as skips. TEST_REQUIRE() really is intended to be used when something is a= hard requirement. > Actually, what if we just skip the nested case completely if > KVM_CAP_EXCEPTION_PAYLOAD isn't available? Can you use KVM_NMI instead of KVM_SET_VCPU_EVENTS to effect the nested #VM= EXIT? That ioctl is so ancient that I'm a-ok skipping any CAP check for it. > > int inject_ud; > > > > /* > > * Big comment here, explaining both the CAP dependency and the= need to > > * check for nested before querying guest state. > > */ > > inject_ud =3D nested && kvm_has_cap(KVM_CAP_EXCEPTION_PAYLOAD); >=20 > Either way yeah I think we should document why checking 'nested' is neede= d. >=20 > Or.. what if we just check state->nested.size inside > kvm_x86_state_is_guest_mode()? or explicitly clear state->nested in > vcpu_save_state() if nested_size =3D=3D 0? I like both of those ideas. If you do the former, definitely make kvm_x86_state_is_guest_mode() a "public" API.