Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2978475pxk; Mon, 21 Sep 2020 01:59:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTeN9oV+i1B1hSabj6/3FboQ83igRWS07BzaUd2abEjpDHqU9OtoxsN4cbdh2F9majRRZS X-Received: by 2002:a17:906:9591:: with SMTP id r17mr50470817ejx.424.1600678774817; Mon, 21 Sep 2020 01:59:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600678774; cv=none; d=google.com; s=arc-20160816; b=WoYrzjJfoY6+yOdAixl1Kah2bPRiOhvxSEqiUT0pb4Olmfxi281HoANbrdfKnrl1n1 8nVV4ly3BVi+pjZgvlP2LjPdyeOyTuOQ/HMdFoWCZvHkW3XDygBMdunstrbju2PLMbzY qjZnCWRWhMtq37RV5LhfNC/OTNRkm0dVg2ce7gAlCR/mStCFOExocdER3eVstZSq2/g7 80gxomb8eDGaAJ1nezGbx9faNsy8+U2j3y8k+NTdLIazlwBcusxIWF2668xMLzoqWfnR fhxfijzq+8rpEiq0Y2eN7H3Upt6u4RBjIfWVr8qozx0ih/+hK7xqRdf/RBck4iBPDCui qTLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=QtDGrtlEykQ6KPjGLzoVNEGTLi0Wn2lMDIF84aVaKUY=; b=g7EFibhXwvHjOUhtOpQ575NdzD5Mja6MMDWWmGPy3cwaqy40aXKPqc48HEVfj3Ntwy 8w7x6JPRT8Ts0EyRiBzlq8zoIcp6eki1uoh3neLOg7MXASekAeLCNUjAacvn+dRF6a/c Ou1Y29z5QLFDHPQVT+/iJp2LnclWwXYDCTDoz3BdmJ43E+fTYlVuUH8D7mhj+V0xzDLZ AOQm3Z8At6+UKf6Bcb9FC2VZwSGzHaTTaBE7Iu42ctylFKQG363ZRsXs9/KZ+G56HBnE K0auWzdOdWCsx7bL5JLGv1PWEd/tVNNRCKop3ixeHWw/wxDsKKvVKQTlPS1aAdugjolm A9dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dT1dlPvV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n24si7581856eji.99.2020.09.21.01.59.11; Mon, 21 Sep 2020 01:59:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dT1dlPvV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726435AbgIUI5j (ORCPT + 99 others); Mon, 21 Sep 2020 04:57:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:35525 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726353AbgIUI5j (ORCPT ); Mon, 21 Sep 2020 04:57:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600678658; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QtDGrtlEykQ6KPjGLzoVNEGTLi0Wn2lMDIF84aVaKUY=; b=dT1dlPvV4LS9q5nw9txN5soYZdMJsJ1kEsZIVkJbWBrPjct8Wp71Lt3PQqtMULbB31YGtp r/AIUztxFfXhcS771k5mlbs2c3Q2lxhj6i5kui1loYqano4rgh5g0GvqllTKtzBAxlWh/0 a/T789VEzz5/UYY/REj8t37zNRthdUE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-349-XYppMNu8OM6FHcbYzkTZzg-1; Mon, 21 Sep 2020 04:57:34 -0400 X-MC-Unique: XYppMNu8OM6FHcbYzkTZzg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 50567802B6B; Mon, 21 Sep 2020 08:57:32 +0000 (UTC) Received: from starship (unknown [10.35.206.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68D9B61177; Mon, 21 Sep 2020 08:57:28 +0000 (UTC) Message-ID: <2b6a4042a0a75cbc5e00b32752afa9965abd697d.camel@redhat.com> Subject: Re: [PATCH v4 2/2] KVM: nSVM: implement ondemand allocation of the nested state From: Maxim Levitsky To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Vitaly Kuznetsov , Ingo Molnar , Wanpeng Li , "H. Peter Anvin" , Borislav Petkov , Jim Mattson , Joerg Roedel , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , linux-kernel@vger.kernel.org, Thomas Gleixner Date: Mon, 21 Sep 2020 11:57:27 +0300 In-Reply-To: <5a3538861a65973f9ae6e2d0798ac17f52428ded.camel@redhat.com> References: <20200917101048.739691-1-mlevitsk@redhat.com> <20200917101048.739691-3-mlevitsk@redhat.com> <20200917162942.GE13522@sjchrist-ice> <20200920161602.GA17325@linux.intel.com> <5a3538861a65973f9ae6e2d0798ac17f52428ded.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.3 (3.36.3-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2020-09-21 at 10:53 +0300, Maxim Levitsky wrote: > On Sun, 2020-09-20 at 18:42 +0200, Paolo Bonzini wrote: > > On 20/09/20 18:16, Sean Christopherson wrote: > > > > Maxim, your previous version was adding some error handling to > > > > kvm_x86_ops.set_efer. I don't remember what was the issue; did you have > > > > any problems propagating all the errors up to KVM_SET_SREGS (easy), > > > > kvm_set_msr (harder) etc.? > > > I objected to letting .set_efer() return a fault. > > > > So did I, and that's why we get KVM_REQ_OUT_OF_MEMORY. But it was more > > of an "it's ugly and it ought not to fail" thing than something I could > > pinpoint. > > > > It looks like we agree, but still we have to choose the lesser evil? > > > > Paolo > > > > > A relatively minor issue is > > > the code in vmx_set_efer() that handles lack of EFER because technically KVM > > > can emulate EFER.SCE+SYSCALL without supporting EFER in hardware. Returning > > > success/'0' would avoid that particular issue. My primary concern is that I'd > > > prefer not to add another case where KVM can potentially ignore a fault > > > indicated by a helper, a la vmx_set_cr4(). > > The thing is that kvm_emulate_wrmsr injects #GP when kvm_set_msr returns any non zero value, > and returns 1 which means keep on going if I understand correctly (0 is userspace exit, > negative value would be a return to userspace with an error) > > So the question is if we have other wrmsr handlers which return negative value, and would > be affected by changing kvm_emulate_wrmsr to pass through the error value. > I am checking the code now. > > I do agree now that this is the *correct* solution to this problem. > > Best regards, > Maxim Levitsky So those are results of my analysis: WRMSR called functions that return negative value (I could have missed something, but I double checked the wrmsr code in both SVM and VMX, and in the common x86 code): vmx_set_vmx_msr - this is only called from userspace (msr_info->host_initiated == true), so this can be left as is xen_hvm_config - this code should probably return 1 in some cases, but in one case, it legit does memory allocation like I do, and failure should probably kill the guest as well (but I can keep it as is if we are afraid that new behavier will not be backward compatible) What do you think about this (only compile tested since I don't have any xen setups): diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 36e963dc1da61..66a57c5b14dfd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2695,24 +2695,19 @@ static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) u32 page_num = data & ~PAGE_MASK; u64 page_addr = data & PAGE_MASK; u8 *page; - int r; - r = -E2BIG; if (page_num >= blob_size) - goto out; - r = -ENOMEM; + return 1; + page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE); - if (IS_ERR(page)) { - r = PTR_ERR(page); - goto out; + if (IS_ERR(page)) + return PTR_ERR(page); + + if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) { + kfree(page); + return 1; } - if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) - goto out_free; - r = 0; -out_free: - kfree(page); -out: - return r; + return 0; } The msr write itself can be reached from the guest through two functions, from kvm_emulate_wrmsr which is called in wrmsr interception from both VMX and SVM, and from em_wrmsr which is called in unlikely case the emulator needs to emulate a wrmsr. Both should be changed to inject #GP only on positive return value and pass the error otherwise. Sounds reasonable? If you agree I'll post the patches implementing this. Best regards, Maxim Levitsky