Re: [PATCH 2/4] vfio/type1: batch vfio_find_vpfn() in function vfio_unpin_pages_remote()

From: Jason Gunthorpe
Date: Thu Jul 03 2025 - 08:28:48 EST


On Thu, Jul 03, 2025 at 12:18:22PM +0800, lizhe.67@xxxxxxxxxxxxx wrote:
> On Wed, 2 Jul 2025 15:27:59 -0300, jgg@xxxxxxxx wrote:
>
> > On Mon, Jun 30, 2025 at 03:25:16PM +0800, lizhe.67@xxxxxxxxxxxxx wrote:
> > > From: Li Zhe <lizhe.67@xxxxxxxxxxxxx>
> > >
> > > The function vpfn_pages() can help us determine the number of vpfn
> > > nodes on the vpfn rb tree within a specified range. This allows us
> > > to avoid searching for each vpfn individually in the function
> > > vfio_unpin_pages_remote(). This patch batches the vfio_find_vpfn()
> > > calls in function vfio_unpin_pages_remote().
> > >
> > > Signed-off-by: Li Zhe <lizhe.67@xxxxxxxxxxxxx>
> > > ---
> > > drivers/vfio/vfio_iommu_type1.c | 10 +++-------
> > > 1 file changed, 3 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> > > index a2d7abd4f2c2..330fff4fe96d 100644
> > > --- a/drivers/vfio/vfio_iommu_type1.c
> > > +++ b/drivers/vfio/vfio_iommu_type1.c
> > > @@ -804,16 +804,12 @@ static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
> > > unsigned long pfn, unsigned long npage,
> > > bool do_accounting)
> > > {
> > > - long unlocked = 0, locked = 0;
> > > + long unlocked = 0, locked = vpfn_pages(dma, iova, npage);
> > > long i;
> >
> > The logic in vpfn_pages?() doesn't seem quite right? Don't we want to
> > count the number of pages within the range that fall within the rb
> > tree?
> >
> > vpfn_pages() looks like it is only counting the number of RB tree
> > nodes within the range?
>
> As I understand it, a vfio_pfn corresponds to a single page, am I right?

It does look that way, it is not what I was expecting iommufd holds
ranges for this job..

So this is OK then

Jason