From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC2C3C433E0 for ; Fri, 5 Feb 2021 10:33:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7C97464F3F for ; Fri, 5 Feb 2021 10:33:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7C97464F3F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DBE6F6B0073; Fri, 5 Feb 2021 05:33:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D472B6B0074; Fri, 5 Feb 2021 05:33:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9C456B0075; Fri, 5 Feb 2021 05:33:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id 9A78D6B0073 for ; Fri, 5 Feb 2021 05:33:06 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5E074181AEF1E for ; Fri, 5 Feb 2021 10:33:06 +0000 (UTC) X-FDA: 77783851572.08.help21_350de71275e4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 401231819E766 for ; Fri, 5 Feb 2021 10:33:06 +0000 (UTC) X-HE-Tag: help21_350de71275e4 X-Filterd-Recvd-Size: 4694 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf30.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 10:33:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612521185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3gSmdzdXiBJ/yrnbO3CPPt8A/90suw/nQvW79Jp5bxM=; b=KIrKBHEK16ApacpRfSugW7aJYZwj36vocpkE1/MZ5TCMxlvm3EUg8OiinEsrmZOvHfEiHO ZO2SLoxNbDRGyYnueHtxUI2nOAm9H4Q4MNj3jqQxGPJCVADF2AgmrXxqrTrXQzo28EQIX3 ZazM4Og59HRyIwIRNrcaEjeDKZKdHwk= 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-151-h5qXyvvFPNayKE6P0wNAyQ-1; Fri, 05 Feb 2021 05:33:03 -0500 X-MC-Unique: h5qXyvvFPNayKE6P0wNAyQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 061E9814318; Fri, 5 Feb 2021 10:33:02 +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 7C4F019D9F; Fri, 5 Feb 2021 10:33:01 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jgg@ziepe.ca, linux-mm@kvack.org, Andrew Morton , dan.j.williams@intel.com Subject: [PATCH 2/2] KVM: do not assume PTE is writable after follow_pfn Date: Fri, 5 Feb 2021 05:32:59 -0500 Message-Id: <20210205103259.42866-3-pbonzini@redhat.com> In-Reply-To: <20210205103259.42866-1-pbonzini@redhat.com> References: <20210205103259.42866-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In order to convert an HVA to a PFN, KVM usually tries to use the get_user_pages family of functions. This however is not possible for VM_IO vmas; in that case, KVM instead uses follow_pfn. In doing this however KVM loses the information on whether the PFN is writable. That is usually not a problem because the main use of VM_IO vmas with KVM is for BARs in PCI device assignment, however it is a bug. To fix it, use follow_pte and check pte_write while under the protection of the PTE lock. The information can be used to fail hva_to_pfn_remapped or passed back to the caller via *writable. Usage of follow_pfn was introduced in commit add6a0cd1c5b ("KVM: MMU: try= to fix up page faults before giving up", 2016-07-05); however, even older versio= n have the same issue, all the way back to commit 2e2e3738af33 ("KVM: Handle vma regions with no backing page", 2008-07-20), as they also did not check whether the PFN was writable. Fixes: 2e2e3738af33 ("KVM: Handle vma regions with no backing page") Reported-by: David Stevens Cc: 3pvd@google.com Cc: Jann Horn Cc: Jason Gunthorpe Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index f34a85b93c2d..ee4ac2618ec5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1907,9 +1907,11 @@ static int hva_to_pfn_remapped(struct vm_area_stru= ct *vma, kvm_pfn_t *p_pfn) { unsigned long pfn; + pte_t *ptep; + spinlock_t *ptl; int r; =20 - r =3D follow_pfn(vma, addr, &pfn); + r =3D follow_pte(vma->vm_mm, addr, &ptep, &ptl); if (r) { /* * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does @@ -1924,14 +1926,19 @@ static int hva_to_pfn_remapped(struct vm_area_str= uct *vma, if (r) return r; =20 - r =3D follow_pfn(vma, addr, &pfn); + r =3D follow_pte(vma->vm_mm, addr, &ptep, &ptl); if (r) return r; + } =20 + if (write_fault && !pte_write(*ptep)) { + pfn =3D KVM_PFN_ERR_RO_FAULT; + goto out; } =20 if (writable) - *writable =3D true; + *writable =3D pte_write(*ptep); + pfn =3D pte_pfn(*ptep); =20 /* * Get a reference here because callers of *hva_to_pfn* and @@ -1946,6 +1953,8 @@ static int hva_to_pfn_remapped(struct vm_area_struc= t *vma, */=20 kvm_get_pfn(pfn); =20 +out: + pte_unmap_unlock(ptep, ptl); *p_pfn =3D pfn; return 0; } --=20 2.26.2