Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1036072ybv; Fri, 7 Feb 2020 13:09:39 -0800 (PST) X-Google-Smtp-Source: APXvYqzQR1tQl845QPLdUaSqzNh7eCHQL1g6HNEoeqgfq/E81w7RXbvDGaLZkrQa0KdkaasWs0gh X-Received: by 2002:a9d:6386:: with SMTP id w6mr966836otk.115.1581109778228; Fri, 07 Feb 2020 13:09:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581109778; cv=none; d=google.com; s=arc-20160816; b=CY02qFde7wvtQSGZbSOKDsPilKQx+ST0IwMQ3Obzs9jDH0i8Ce1v8YtciEnKosEIqX GHv36tXuhwcWRnvbcCuACvQbFZxz+lfdxVLgCjP89N7o6GMMWY8LxKM+6+9DDLrgj7jc YNFkfPZB2O7IJ74v78quuqmCJOWiNru3clmiGZU+n+OTy5oftbDmdzUKzkKRrw6iFp+X L6BRIA6WidbHsfhPdbCCk9Xt7FvkUNP3SyhXib2NSLl5cl+JOfa4cZqsQnWJPc7Rm9xh Vd5lj2jDHaODE/GTXelGtPCy63ZS8l10363lTd65gXQozIl7Y3arabASXR475d43yGzN B+UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=onxmmjqXELNJOnSDJZ2qgajL7wmylkhLVi0Ggn784AA=; b=M6US/PgotMIm/pYVKzxr4J4viRVH5Dy6/PSNDfwH2GW4A0XQSkBFCGDgkJ/kKtAo86 laAJwTY83cv8jnCxPOUjUIFX7t5vZxfm9ZEZfe4Ke1QmQcrTD7XUUpxWeLbWMQWZ2w+d jcY/sGIuddD5tOPHfzqcDbwdo1fKkx25SiBJmvhlsR+/oGNeYKzsqfJe3tpQ++Xb5eSf E5ncikQzf3H7z0eUVBpq8pSAZhouSGxO3yO5BPyYaqjylcTCLNMk/gtkejgKGm4kEtPS r1IOo3EW51dBM+7s647YiLAUTHQnu0SzgiIqRBPO5MbZ6tYV23ABCFZxT2kmvhptzw6k iAlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=dk5vzb2t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2si4129548ois.221.2020.02.07.13.09.25; Fri, 07 Feb 2020 13:09:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=dk5vzb2t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727113AbgBGVIe (ORCPT + 99 others); Fri, 7 Feb 2020 16:08:34 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:44592 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726947AbgBGVIe (ORCPT ); Fri, 7 Feb 2020 16:08:34 -0500 Received: by mail-qk1-f194.google.com with SMTP id v195so473871qkb.11 for ; Fri, 07 Feb 2020 13:08:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=onxmmjqXELNJOnSDJZ2qgajL7wmylkhLVi0Ggn784AA=; b=dk5vzb2tCbBXS4d5M4it8BrIKygD26w5xPGFMn5hH7EB5FDmp0KmRWi/KQkqFkG1m8 CZbK1VtVLge1Eb4gNl4uRSdTopDs5ifncqrRoaKJt0xiy4KsrUNbkeoHFnpDu82NGpoM XgxVX/bjgquMFQ7AL/PU5uShMvk5OCyQSKtUkAks5S2bpFBayhohiAjbSrs0bfH02cql 1XoSJkbIUFB0Kr13E5+kCY/PyGf5zGsZNuwNfBWNLXgLPruhljRLmWxCkW2WzgOfO3Au cCNrrWIRqHsKpBql3MPaR/UFV+dcy3bnv/2M3EhyPrCMvK48LPH69/Y2TcNSDDw1XeRt sj7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=onxmmjqXELNJOnSDJZ2qgajL7wmylkhLVi0Ggn784AA=; b=EoWUGyVXpP2mpmE29xUV3YSQrVb7JAEEuvmfFcwG5rgVUNPcHnFrUsiKAW12uO/ABV XgxEgcfLoAwgA1rNFqsiq2TjIMtdH+Faf4iUlOfyKR6eojyUFhLo5F7Z98fzh3n6huhr yva2l3lol/2YZCGV3dB2RtatAXrk5ZNHPfCx6lvmzcI4/tJzZkbNljKxe7DIMugjoxoj qtG7ATIO3u/BuSt2LBk8a0W8JZvc/jU6Fm/lSPGwBVH35moRqjdXeR2wMy/xOoAYjk0f yhm402BvY0r4GT0oPeus8z1csmtKF6HtDqGWkW4Ue/JO4LaZo8CM/SH1a8cO8OGPg+Sn yvPw== X-Gm-Message-State: APjAAAUs4i+AtA2JDuawWqhEBAEW7dN2tPfFj8H0cO8ApfT7dtZUuHaF cNARpK98YP8IgijPTJA2VSIrkw== X-Received: by 2002:a37:e10f:: with SMTP id c15mr806650qkm.331.1581109713217; Fri, 07 Feb 2020 13:08:33 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-68-57-212.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.57.212]) by smtp.gmail.com with ESMTPSA id n7sm189917qkk.41.2020.02.07.13.08.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 07 Feb 2020 13:08:32 -0800 (PST) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1j0ArY-00086p-0F; Fri, 07 Feb 2020 17:08:32 -0400 Date: Fri, 7 Feb 2020 17:08:31 -0400 From: Jason Gunthorpe To: Joao Martins Cc: linux-nvdimm@lists.01.org, Dan Williams , Vishal Verma , Dave Jiang , Ira Weiny , Alex Williamson , Cornelia Huck , kvm@vger.kernel.org, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , x86@kernel.org, Liran Alon , Nikita Leshenko , Barret Rhoden , Boris Ostrovsky , Matthew Wilcox , Konrad Rzeszutek Wilk Subject: Re: [PATCH RFC 09/10] vfio/type1: Use follow_pfn for VM_FPNMAP VMAs Message-ID: <20200207210831.GA31015@ziepe.ca> References: <20200110190313.17144-1-joao.m.martins@oracle.com> <20200110190313.17144-10-joao.m.martins@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200110190313.17144-10-joao.m.martins@oracle.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 10, 2020 at 07:03:12PM +0000, Joao Martins wrote: > From: Nikita Leshenko > > Unconditionally interpreting vm_pgoff as a PFN is incorrect. > > VMAs created by /dev/mem do this, but in general VM_PFNMAP just means > that the VMA doesn't have an associated struct page and is being managed > directly by something other than the core mmu. > > Use follow_pfn like KVM does to find the PFN. > > Signed-off-by: Nikita Leshenko > drivers/vfio/vfio_iommu_type1.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index 2ada8e6cdb88..1e43581f95ea 100644 > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -362,9 +362,9 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, > vma = find_vma_intersection(mm, vaddr, vaddr + 1); > > if (vma && vma->vm_flags & VM_PFNMAP) { > - *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; > - if (is_invalid_reserved_pfn(*pfn)) > - ret = 0; > + ret = follow_pfn(vma, vaddr, pfn); > + if (!ret && !is_invalid_reserved_pfn(*pfn)) > + ret = -EOPNOTSUPP; > } FWIW this existing code is a huge hack and a security problem. I'm not sure how you could be successfully using this path on actual memory without hitting bad bugs? Fudamentally VFIO can't retain a reference to a page from within a VMA without some kind of recount/locking/etc to allow the thing that put the page there to know it is still being used (ie programmed in a IOMMU) by VFIO. Otherwise it creates use-after-free style security problems on the page. This code needs to be deleted, not extended :( Jason