public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vfio/type1: check pfn valid before converting to struct page
@ 2023-05-16  9:30 Yan Zhao
  2023-05-16 11:50 ` Jason Gunthorpe
  2023-05-17 15:19 ` Sean Christopherson
  0 siblings, 2 replies; 6+ messages in thread
From: Yan Zhao @ 2023-05-16  9:30 UTC (permalink / raw)
  To: kvm, linux-kernel; +Cc: alex.williamson, kevin.tian, jgg, Yan Zhao

vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
e.g. for MMIO PFNs.

It's necessary to check if it's a valid pfn before calling pfn_to_page().

Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
---
 drivers/vfio/vfio_iommu_type1.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 493c31de0edb..0620dbe5cca0 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -860,6 +860,11 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
 		if (ret)
 			goto pin_unwind;
 
+		if (!pfn_valid(phys_pfn)) {
+			ret = -EINVAL;
+			goto pin_unwind;
+		}
+
 		ret = vfio_add_to_pfn_list(dma, iova, phys_pfn);
 		if (ret) {
 			if (put_pfn(phys_pfn, dma->prot) && do_accounting)

base-commit: b3c98052d46948a8d65d2778c7f306ff38366aac
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio/type1: check pfn valid before converting to struct page
  2023-05-16  9:30 [PATCH] vfio/type1: check pfn valid before converting to struct page Yan Zhao
@ 2023-05-16 11:50 ` Jason Gunthorpe
  2023-05-17 15:19 ` Sean Christopherson
  1 sibling, 0 replies; 6+ messages in thread
From: Jason Gunthorpe @ 2023-05-16 11:50 UTC (permalink / raw)
  To: Yan Zhao; +Cc: kvm, linux-kernel, alex.williamson, kevin.tian

On Tue, May 16, 2023 at 05:30:07PM +0800, Yan Zhao wrote:
> vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
> e.g. for MMIO PFNs.
> 
> It's necessary to check if it's a valid pfn before calling pfn_to_page().
> 
> Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> ---
>  drivers/vfio/vfio_iommu_type1.c | 5 +++++
>  1 file changed, 5 insertions(+)

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio/type1: check pfn valid before converting to struct page
  2023-05-16  9:30 [PATCH] vfio/type1: check pfn valid before converting to struct page Yan Zhao
  2023-05-16 11:50 ` Jason Gunthorpe
@ 2023-05-17 15:19 ` Sean Christopherson
  2023-05-18  0:52   ` Yan Zhao
  1 sibling, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2023-05-17 15:19 UTC (permalink / raw)
  To: Yan Zhao; +Cc: kvm, linux-kernel, alex.williamson, kevin.tian, jgg

On Tue, May 16, 2023, Yan Zhao wrote:
> vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
> e.g. for MMIO PFNs.
> 
> It's necessary to check if it's a valid pfn before calling pfn_to_page().
> 
> Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")

Might be worth adding a blurb to call out that this is _not_ ABI breakage.  Prior
to the buggy commit, KVMGT manually checked that the pfn pinned by vfio_pin_pages()
was pfn_valid(), and s390's driver(s) either blindly expected struct page memory,
e.g. did

        ret = page_array_pin(&pa, vdev);
        if (ret < 0) {
                page_array_unpin_free(&pa, vdev);
                return ret;
        }

        l = n;
        for (i = 0; i < pa.pa_nr; i++) {
                struct page *page = pfn_to_page(pa.pa_pfn[i]);
                void *from = kmap_local_page(page);

or in the case of its crypto driver, apparently was all kinds of confused about
virtual vs. physical, i.e. likely couldn't have worked with anything but "normal"
memory anyways.

AFAICT, those are the only in-tree users of vfio_pin_pages().

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio/type1: check pfn valid before converting to struct page
  2023-05-17 15:19 ` Sean Christopherson
@ 2023-05-18  0:52   ` Yan Zhao
  2023-05-18 18:07     ` Sean Christopherson
  0 siblings, 1 reply; 6+ messages in thread
From: Yan Zhao @ 2023-05-18  0:52 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: kvm, linux-kernel, alex.williamson, kevin.tian, jgg

On Wed, May 17, 2023 at 08:19:04AM -0700, Sean Christopherson wrote:
> On Tue, May 16, 2023, Yan Zhao wrote:
> > vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
> > e.g. for MMIO PFNs.
> > 
> > It's necessary to check if it's a valid pfn before calling pfn_to_page().
> > 
> > Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> 
> Might be worth adding a blurb to call out that this is _not_ ABI breakage.  Prior
Do you mean "_not_ ABI breakage" with
34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
or with this fix commit?

> to the buggy commit, KVMGT manually checked that the pfn pinned by vfio_pin_pages()
> was pfn_valid(), and s390's driver(s) either blindly expected struct page memory,
> e.g. did
> 
>         ret = page_array_pin(&pa, vdev);
>         if (ret < 0) {
>                 page_array_unpin_free(&pa, vdev);
>                 return ret;
>         }
> 
>         l = n;
>         for (i = 0; i < pa.pa_nr; i++) {
>                 struct page *page = pfn_to_page(pa.pa_pfn[i]);
>                 void *from = kmap_local_page(page);
> 
> or in the case of its crypto driver, apparently was all kinds of confused about
> virtual vs. physical, i.e. likely couldn't have worked with anything but "normal"
> memory anyways.
> 
> AFAICT, those are the only in-tree users of vfio_pin_pages().
Though there are no in-tree users currently expect vfio_pin_pages() to cover
MMIO ranges, with the
commit 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()"),
the IOVA ranges supported by vfio_pin_pages() is still reduced.

Previously, it's the callers(vendor drivers)' option to decide if they want to
support MMIO GFNs or not, while now only if there are other interfaces provided
by VFIO, the vendor drivers could not allow pinning of MMIO GFNs, i.e. no guest
P2P.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio/type1: check pfn valid before converting to struct page
  2023-05-18  0:52   ` Yan Zhao
@ 2023-05-18 18:07     ` Sean Christopherson
  2023-05-19  1:25       ` Yan Zhao
  0 siblings, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2023-05-18 18:07 UTC (permalink / raw)
  To: Yan Zhao; +Cc: kvm, linux-kernel, alex.williamson, kevin.tian, jgg

On Thu, May 18, 2023, Yan Zhao wrote:
> On Wed, May 17, 2023 at 08:19:04AM -0700, Sean Christopherson wrote:
> > On Tue, May 16, 2023, Yan Zhao wrote:
> > > vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
> > > e.g. for MMIO PFNs.
> > > 
> > > It's necessary to check if it's a valid pfn before calling pfn_to_page().
> > > 
> > > Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> > 
> > Might be worth adding a blurb to call out that this is _not_ ABI breakage.  Prior
> Do you mean "_not_ ABI breakage" with
> 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> or with this fix commit?

Mostly the former.  I brought it up because _if_ there was breakage in that commit,
then this fix would be "wrong" in the sense that it wouldn't undo any breakage, and
would likely make it harder to restore the previous behavior.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio/type1: check pfn valid before converting to struct page
  2023-05-18 18:07     ` Sean Christopherson
@ 2023-05-19  1:25       ` Yan Zhao
  0 siblings, 0 replies; 6+ messages in thread
From: Yan Zhao @ 2023-05-19  1:25 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: kvm, linux-kernel, alex.williamson, kevin.tian, jgg

On Thu, May 18, 2023 at 11:07:09AM -0700, Sean Christopherson wrote:
> On Thu, May 18, 2023, Yan Zhao wrote:
> > On Wed, May 17, 2023 at 08:19:04AM -0700, Sean Christopherson wrote:
> > > On Tue, May 16, 2023, Yan Zhao wrote:
> > > > vfio_pin_page_external() can return a phys_pfn for vma with VM_PFNMAP,
> > > > e.g. for MMIO PFNs.
> > > > 
> > > > It's necessary to check if it's a valid pfn before calling pfn_to_page().
> > > > 
> > > > Fixes: 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> > > 
> > > Might be worth adding a blurb to call out that this is _not_ ABI breakage.  Prior
> > Do you mean "_not_ ABI breakage" with
> > 34a255e67615 ("vfio: Replace phys_pfn with pages for vfio_pin_pages()")
> > or with this fix commit?
> 
> Mostly the former.  I brought it up because _if_ there was breakage in that commit,
> then this fix would be "wrong" in the sense that it wouldn't undo any breakage, and
> would likely make it harder to restore the previous behavior.

Ok. Let me post v2 to describe the problem and background clearly.

Thanks!

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-05-19  1:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-16  9:30 [PATCH] vfio/type1: check pfn valid before converting to struct page Yan Zhao
2023-05-16 11:50 ` Jason Gunthorpe
2023-05-17 15:19 ` Sean Christopherson
2023-05-18  0:52   ` Yan Zhao
2023-05-18 18:07     ` Sean Christopherson
2023-05-19  1:25       ` Yan Zhao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox