Received: by 2002:ab2:7104:0:b0:1f7:f6c3:9cb1 with SMTP id z4csp40291lql; Tue, 7 May 2024 09:13:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXDydhkfaDtAiFZuxVSzeWtlDcpqnGSlCw7zRZ3kuPxYIz+/zrIMG/J6ip68ADDVFIEOx8F1IUJ1DUxooMRtekCtWMQXffXgTtnT7IkEg== X-Google-Smtp-Source: AGHT+IGd7pQB5mcCm6BBOXR3bUj5/Cd7YO4OCKC65g58lOWYCfSjNvTeBr0EtPxbbdUOLHh1Juc+ X-Received: by 2002:a50:ab59:0:b0:568:9d96:b2d1 with SMTP id 4fb4d7f45d1cf-5731da6a739mr117906a12.32.1715098386753; Tue, 07 May 2024 09:13:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715098386; cv=pass; d=google.com; s=arc-20160816; b=Y/OBAGdjiUyMBP0MX/ZfvIrzXC/q9TefAbwldMf4XtPK16FP/9w2khCvstkmNzj22/ z1yWKl9E8neEAXHw4i/xj5PzSJzK4zsWYevD8CO/ENHHssXKDzz0DIZn67+OoXIm7lqJ 24zTAaz2u3fVHTHBzEOEFiSqREZpkHtlcnE2yX0BW5OkrgQg/RGWIuXQDXyPs8nMmaXr BH5C9FeKxnJWnVP5oIne2BTUtNasm6eTJpQQ4pyuaf9qEFfFNCbw7OfF435ggOU4YitN 65dKaCHg6E/SNmt61R+0pDyym2/x/fmpdFK/fDOCjirPcs8GsU2Q6su2vwm2DT1JQn4f REkw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5xfI8GlIpdk/dq7ZsckyDRl9W0stP1kv+OVmnAnTfxI=; fh=CEZhR5zNH59e4Q0mXgvxsRYN65+hEJnOXUDy6wIYCyo=; b=Q66EBoj7eeA+A1vsz6jiQqojJvMIwTpc+Zm6ng7KEciZPfXnZCB2RZLDnQqSsFdHHG s54YA2i+10z5N4N870OJf6HLctsOQjg8+rSiG2BNB5X6uOLUVrq7+RPkijbVjTsKDvvS rHZJgBeQwz4K9AzgjME8ISyHRjnZkzHjA07NYZ3eOKXHkXshApzJFuuALxfjwNTqydmz y+wes4L/IJzJkKYxFOgfom9O+JS0IDx6miNyz5bpn/ivJZ0Zl7fKMEXVVRqinbIWRj7c jIaMZ4mnI2StL6XvLVfYci8g4b1sdwKLx62evaXD6miPxpEEkhf6PYEGGNNq7NY7x6sn X0Cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZedpxqK9; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171758-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 b24-20020aa7df98000000b005726b7f5f8bsi6392670edy.557.2024.05.07.09.13.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 09:13:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171758-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=@redhat.com header.s=mimecast20190719 header.b=ZedpxqK9; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171758-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 9B8AC1F2B95D for ; Tue, 7 May 2024 16:05:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9BD19180A67; Tue, 7 May 2024 15:58:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZedpxqK9" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6010616C84A for ; Tue, 7 May 2024 15:58:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715097510; cv=none; b=DOoT+8VC2nNkl1f7oGwE5d0Sx1j2d0Vtw9vo8D8+4D39ln5EXs2Y3XBvWGJMIMillC6QvSkOeYrI+Vhq5Nd/sJbVv132T09ASoxidG2Rs+9C52VyxwsJN9E9cxUKqx5oS4fPqQgQQqMtIa/AZanwfcIZnOW/FjnVhs5bATe+T6I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715097510; c=relaxed/simple; bh=GnaXsr+zbd/U7GYWO/XXTnyktbvceHVhOXczQVr4XWs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Oin06P6R09yx9h6HvNxgD6Pbkn57g3jAQ4N8UZ5zqs0J2QH3BrgRDX2cfNDPYYV878kABUkLgl9IsidPbVE/PKWBMt5PFCoKdKoEYpgvMrWGHYRAcAZ6o1faRzsNeTJFCmhbkgPVcvjLaI6hlAPf86xgPPDQNGPPSTOlxfLpDZQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZedpxqK9; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715097504; 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=5xfI8GlIpdk/dq7ZsckyDRl9W0stP1kv+OVmnAnTfxI=; b=ZedpxqK9XNFMYAsemBoTBH2RizBIffP99E4PjrKvoAYQJsGtmFMaFBKwuKQlr9kh4xipOk OKp8PKj/Rxh4hnNquE1AIds4f3SEHSQdo5xCvyr549YTHCbhqde+dWbia5K/s2esYZIeJX 0++Z10IjhazLwk+BNcqIzH1FspDxYD4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-530--TK-8O3SNhOueOYN4IhQyA-1; Tue, 07 May 2024 11:58:20 -0400 X-MC-Unique: -TK-8O3SNhOueOYN4IhQyA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D716D802BFB; Tue, 7 May 2024 15:58:19 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9AF89492CAA; Tue, 7 May 2024 15:58:19 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Yu Zhang , Chao Peng , Fuad Tabba , Michael Roth , Isaku Yamahata , Kai Huang Subject: [PATCH 10/17] KVM: x86/mmu: Move private vs. shared check above slot validity checks Date: Tue, 7 May 2024 11:58:10 -0400 Message-ID: <20240507155817.3951344-11-pbonzini@redhat.com> In-Reply-To: <20240507155817.3951344-1-pbonzini@redhat.com> References: <20240507155817.3951344-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 From: Sean Christopherson Prioritize private vs. shared gfn attribute checks above slot validity checks to ensure a consistent userspace ABI. E.g. as is, KVM will exit to userspace if there is no memslot, but emulate accesses to the APIC access page even if the attributes mismatch. Fixes: 8dd2eee9d526 ("KVM: x86/mmu: Handle page fault for private memory") Cc: Yu Zhang Cc: Chao Peng Cc: Fuad Tabba Cc: Michael Roth Cc: Isaku Yamahata Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Message-ID: <20240228024147.41573-10-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0d884d0b0f35..ba50b93e93ed 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4317,11 +4317,6 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return RET_PF_EMULATE; } - if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { - kvm_mmu_prepare_memory_fault_exit(vcpu, fault); - return -EFAULT; - } - if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault); @@ -4359,9 +4354,24 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, { int ret; + /* + * Note that the mmu_invalidate_seq also serves to detect a concurrent + * change in attributes. is_page_fault_stale() will detect an + * invalidation relate to fault->fn and resume the guest without + * installing a mapping in the page tables. + */ fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq; smp_rmb(); + /* + * Now that we have a snapshot of mmu_invalidate_seq we can check for a + * private vs. shared mismatch. + */ + if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; + } + /* * Check for a relevant mmu_notifier invalidation event before getting * the pfn from the primary MMU, and before acquiring mmu_lock. -- 2.43.0