Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp28158pxb; Wed, 30 Mar 2022 21:59:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAJvLEdwlu/XwvPHNA72eB+mVFPQwzyJph9HU7pjO7PqHhUFGKgpWDsKkVDov3X4/tE46J X-Received: by 2002:a63:bf0e:0:b0:386:361f:e97a with SMTP id v14-20020a63bf0e000000b00386361fe97amr9183882pgf.552.1648702785380; Wed, 30 Mar 2022 21:59:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648702785; cv=none; d=google.com; s=arc-20160816; b=t1h3TzIs+23lOZ8J+BFsNkczWCdIUxd3cA0qQQDglSi46LsRNj2/2UO6iOuSEnr2zx 1/YDauN2TB2KNXM8ioEjKnTZnhBxjIiiCLwVpz7j0odQGH4ZzriFQUEyU249YELgD+XG cpiA7odbZjv1/H3Kma6xd/JGg/eWca/yoSEeStGk8L5XSTP9pmjhwrehERbAqWpif6gv cdGAet+bCGQ56fA/3axmguAybEFKpv54GYnFXPntXHquIra0epDiXDqQuxXMTFa1beJ9 HlT8/fb0Boug8GsdE3pONLM41sa/LCH4pRQC7qqeTUZzmwZMzB0nHHs1w20X0QTK8nKV +EzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=nrxYfNyK7S9x3Z4TVz6BHoXQgDDsXzELpg8P7G1ivg8=; b=IZjADA9N40is19UAMA5VX7XDylijqxBect9NahSW7cXoyfoztdffKun7qMsOR7Mcdg TMvJ0V+ejWWPSdb2F1PyeU4PM+xvGtpRGRx5WwaOFAi6NwOSjz6mR4j2XXvlvy25pgyU 1Zx5eBsMEH6cvgFri8FAbSgvUcwUBHo9rdrTEKXNSFVW0CtbZ/45ZHUJVRoWU0BOsoZR vOv3iJincj2Fl40tQ/+Mv2p8SJLqCRrZSimnwS35dflukS1fIv8OZ6eCHYSt0SRl+bP7 AcrFlZWn90o+yMFAFoxu9bZHWudGIkGcXui1pld6zzlV+t+klUhmD9mO4IvWLdO7VYoW hGJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kRrEQEOf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id i1-20020a654841000000b00398ab3fdcafsi4308726pgs.655.2022.03.30.21.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 21:59:45 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kRrEQEOf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 235A5247838; Wed, 30 Mar 2022 20:36:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351808AbiC3XW1 (ORCPT + 99 others); Wed, 30 Mar 2022 19:22:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351794AbiC3XW1 (ORCPT ); Wed, 30 Mar 2022 19:22:27 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F96A326E5 for ; Wed, 30 Mar 2022 16:20:41 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id b130so17205159pga.13 for ; Wed, 30 Mar 2022 16:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=nrxYfNyK7S9x3Z4TVz6BHoXQgDDsXzELpg8P7G1ivg8=; b=kRrEQEOfK4vi4t86gOlwSDhe0QxixvlP3Y+iGpGQ+YI02dzbgJjUnSBPKJmZoJKUit 0pvaqdznhG3rOnDKxa4dCb9nvBeym00O+b6Bf9gGvITnHIPrGDtsyydg+2/ouQtfLPFY KF/Qvp5wr0kSin82mbEatQ5upUewJoA1VSEsPJClJBZ1u8HU0HtS8QYx5iWtjnjegPRb GRLDPQw1jw8cy+4/24EodHLv4lU0iayGdt94948UMQeHb9de2W+p4AGYkXFqkvCfRJOY KEsphq+fuLy/sBb6HCM4uQg6tO4z/E0oUSZvvGbcrPBdrdqkGfhYFu6+sjmgCSwY8UXJ KKxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=nrxYfNyK7S9x3Z4TVz6BHoXQgDDsXzELpg8P7G1ivg8=; b=J0rP0j9+oA1Y+iPWOEoTgcU4xw3MJ9kccyHfaGSZ8fySwiPLmKG8n/u6TjFA+rdcCS P85iWPX2J3H2PQ+ySs3RNDBrdxXPTkEwsH4QBriMrM8e0IgmVC2+cwamIdUsS32JBwTl CJWA9lYEFxW7mc5KoXdrbC7ov+H01FlqZvYcbCgv8QDhBMfnvfwXAShNjCnyNKIoUKOv RXFPum6AlBWqtGFfOapfC1UNvuCLGFuDvFfGh5pmfKVXJw2As3+aZXg/14l8VE9a5u/R otlyhOSDgt8FmEMKULuYz246TrEHJ2FSspKAYWOMRW41IbpBqOmp6/7E2q9U6omyxXBb r0RQ== X-Gm-Message-State: AOAM530Q78hmCYmQDLaj/PQ4v/Ej6R3BfkzrFKlfiN2gBjmCRWguC5nT wM9IPl8UwO5BAC3+2kdMX6wslQ== X-Received: by 2002:a05:6a00:194c:b0:4fb:32b9:dfb6 with SMTP id s12-20020a056a00194c00b004fb32b9dfb6mr1983349pfk.17.1648682440631; Wed, 30 Mar 2022 16:20:40 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm7584150pjd.15.2022.03.30.16.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 16:20:39 -0700 (PDT) Date: Wed, 30 Mar 2022 23:20:36 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Tom Lendacky , Brijesh Singh , Jon Grimm , David Kaplan , Boris Ostrovsky , Liam Merwick , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/5] KVM: nSVM: Don't forget about L1-injected events Message-ID: References: <8f9ae64a-dc64-6f46-8cd4-ffd2648a9372@maciej.szmigiero.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8f9ae64a-dc64-6f46-8cd4-ffd2648a9372@maciej.szmigiero.name> X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 31, 2022, Maciej S. Szmigiero wrote: > On 30.03.2022 23:59, Sean Christopherson wrote: > > On Thu, Mar 10, 2022, Maciej S. Szmigiero wrote: > > > @@ -3627,6 +3632,14 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu) > > > if (!(exitintinfo & SVM_EXITINTINFO_VALID)) > > > return; > > > + /* L1 -> L2 event re-injection needs a different handling */ > > > + if (is_guest_mode(vcpu) && > > > + exit_during_event_injection(svm, svm->nested.ctl.event_inj, > > > + svm->nested.ctl.event_inj_err)) { > > > + nested_svm_maybe_reinject(vcpu); > > > > Why is this manually re-injecting? More specifically, why does the below (out of > > sight in the diff) code that re-queues the exception/interrupt not work? The > > re-queued event should be picked up by nested_save_pending_event_to_vmcb12() and > > propagatred to vmcb12. > > A L1 -> L2 injected event should either be re-injected until successfully > injected into L2 or propagated to VMCB12 if there is a nested VMEXIT > during its delivery. > > svm_complete_interrupts() does not do such re-injection in some cases > (soft interrupts, soft exceptions, #VC) - it is trying to resort to > emulation instead, which is incorrect in this case. > > I think it's better to split out this L1 -> L2 nested case to a > separate function in nested.c rather than to fill > svm_complete_interrupts() in already very large svm.c with "if" blocks > here and there. Ah, I see it now. WTF. Ugh, commit 66fd3f7f901f ("KVM: Do not re-execute INTn instruction.") fixed VMX, but left SVM broken. Re-executing the INTn is wrong, the instruction has already completed decode and execution. E.g. if there's there's a code breakpoint on the INTn, rewinding will cause a spurious #DB. KVM's INT3 shenanigans are bonkers, but I guess there's no better option given that the APM says "Software interrupts cannot be properly injected if the processor does not support the NextRIP field.". What a mess. Anyways, for the common nrips=true case, I strongly prefer that we properly fix the non-nested case and re-inject software interrupts, which should in turn naturally fix this nested case. And for nrips=false, my vote is to either punt and document it as a "KVM erratum", or straight up make nested require nrips. Note, that also requires updating svm_queue_exception(), which assumes it will only be handed hardware exceptions, i.e. hardcodes type EXEPT. That's blatantly wrong, e.g. if userspace injects a software exception via KVM_SET_VCPU_EVENTS.