From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99B77405C28 for ; Thu, 4 Jun 2026 14:27:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780583230; cv=none; b=kE0CCuV/SWmTHYJn1NvAfAyHxpywc34ezNbkcZTYxlUwy4hrro3q9mpgAOojSLb5fAdUCq9LXdHx4yWpfFs2gB5+gJhG7toHowvn0c+0htoUasUElsFwGlyS7TdjQrqYWVLUrxJzaai5qmYLoseK8VBKNOdm1PidFWufoVF06HA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780583230; c=relaxed/simple; bh=R5hMbnNS12FEIZNlNB/bpEoNfDKSllPhXOnCYKccGQM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BLSkK+QHXBXwtIxNcPj48GVjgr3GY7yOzAMgF3DF4qWMqWZtQ+6O+wVy/Rqy4aygGucYYiZkZFFNXf52DEcNiKh3Evmyq0SVkjYQ27pn+qrCvH+EFK16V0H6TwrlXYYrz/CR46KmiiOfDMG233FdSsHGcAgEHcJhHVONRG+MqNs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca; spf=pass smtp.mailfrom=ziepe.ca; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b=MwQnXCOP; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="MwQnXCOP" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-9158629a220so90026785a.1 for ; Thu, 04 Jun 2026 07:27:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1780583228; x=1781188028; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=rHKibgV+RCLusPgB3KbLwXXB/tqYgf/ZSAW8CwOeGIc=; b=MwQnXCOP+LvWok0Hr0PwjGnqYw1RtFxMu1DnDWOitZjjZ3ZBZO35+8hK2SaH+uAN0l H/J5FqHZwzpgauCu8aQlj5Egy3aSVT92+WLAdFxcWsuGyt9PyJt55KFID60sklr+PYTU 6nveCpXV6KIbRtznYf2KijiN0seBclxi7oybxFalklmt6XLtZnAjEJV/rtB7eujd4msw iklgZVxbIwWw5B6/BwiEy8d5OHCcL3xdryqIaUWM6Yvy1Km2ZDLVwa9cp9aEuSi4bKTH gKuGKOcL9NSmFD1KPM/S9Oh7W97fvXAWSZtjtPCcrtZ+bxz2EXcVknPiCzydkz8mQ5Z5 UjKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780583228; x=1781188028; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rHKibgV+RCLusPgB3KbLwXXB/tqYgf/ZSAW8CwOeGIc=; b=oZzlS80c9tTy+WdMOqQVRT9nrb7ET2C+NzdQyw8+SV9RfZd1HD6/d4VcFmpOsP7Hty IGJPJlTsIHYGKi6B0loa7EOA09nprEkK/H9HIAGpmsH6vEJO4mK058GHHR7a9GNPLTDb C6aIcuHC+ij7INVwePvEMGoZtbbSG7AFAOmKJK0LKegDRwiwkvzoVFQOd/9OQz5p9+VS o59VwwIshxR1VOXWhAJ2x8VXGdZVpvmcZ1hDaSEIVc9jxmEzPQcBsbgeF8I8sh/lQw1p 23+RjlneUVeiNan92mYtsnHC1FfXWdkKEH3RLjWN7LVg/ka7V5+ugW6r7sYDnnil8mc8 WGBA== X-Forwarded-Encrypted: i=1; AFNElJ95fcL7DuvQBj1sRPjrGxLE4N7nuVIBp1jiE3P5S9vyfzaEE/ZkHMMQeoL7CWNCH7gNCMUwX+xVirwJ4vE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+NJORN+fWJtLpnBkokRxv77GI5AYG/sji0paPq5oiKZsFAyBc vjFawxB0v2aZ+ahuJX4LeZiuqQtCXhBK1K2KyF0WrwRl3o4EUYlnw9qkWHx/Zf68qc0= X-Gm-Gg: Acq92OHwOpTSNaJc/BLyxhrjka/G+ikPFLmZSPgrRrqkbQvQr276IAQP8FZogccdoBP EjzPdIX98rylBpq6lYszosRhKHE9lKDsSYHEi3qtI92/1fiYEyAuDXwchheH5I9s5Wv/9jASrUI 4BdyY69TW7wjHeTv8+lWG5lZqIR75iQiGJmM9Bwl1K3w1wAJcC6GcYO0MAQaOMhjlrR8WFlyJ7z G3CKGrUjewfrMy1L27lEla76BnnZEbNdnOokpAveD6lJFHQUJWDJpkbc9ont/NneXxbA7U1+RQ3 gRg238nHj8bW+2JDCHOwXQDk6otENLCrDA7MftjtGbHfKcu7DPHxJE3cyzzVl3J0f6bAVX0NhlA IBHDb2K1MNmWq5Z/mXSJuagcDgAn0SkN8CCVmfmPPZ+IRisWBD0u9BSMd9TwB4Hm2y8Rn4tCMrz 1JRy7wFSK1bbDp6l/9hiiAa1OcxeCOlh2DnEVZgsdQ2ZFhcxnygRo9HY0FxvABxDy3+z5Ty6g7e IQHJ6jqjku8vUwZO+tYZZe5n8g= X-Received: by 2002:a05:620a:6cc2:b0:90f:b39e:ec8 with SMTP id af79cd13be357-9158a6998c8mr1313811585a.17.1780583227568; Thu, 04 Jun 2026 07:27:07 -0700 (PDT) Received: from ziepe.ca (crbknf0213w-47-54-130-67.pppoe-dynamic.high-speed.nl.bellaliant.net. [47.54.130.67]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9158a3e0d55sm600410185a.43.2026.06.04.07.27.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jun 2026 07:27:07 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.97) (envelope-from ) id 1wV92I-00000008IGV-29F2; Thu, 04 Jun 2026 11:27:06 -0300 Date: Thu, 4 Jun 2026 11:27:06 -0300 From: Jason Gunthorpe To: Guanghui Feng Cc: adrian.larumbe@collabora.com, airlied@gmail.com, alex@shazbot.org, alikernel-developer@linux.alibaba.com, baolu.lu@linux.intel.com, boris.brezillon@collabora.com, dri-devel@lists.freedesktop.org, dwmw2@infradead.org, iommu@lists.linux.dev, joro@8bytes.org, kevin.tian@intel.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, liviu.dudau@arm.com, maarten.lankhorst@linux.intel.com, mripard@kernel.org, oliver.yang@linux.alibaba.com, robh@kernel.org, robin.murphy@arm.com, shiyu.zsq@linux.alibaba.com, steven.price@arm.com, suravee.suthikulpanit@amd.com, tzimmermann@suse.de, wei.guo.simon@linux.alibaba.com, will@kernel.org, xlpang@linux.alibaba.com Subject: Re: [PATCH v3 23/32] vfio: use iova_to_phys_length for efficient unmap Message-ID: <20260604142706.GZ2487554@ziepe.ca> References: <20260602104637.1219810-1-guanghuifeng@linux.alibaba.com> <20260603151804.1963871-1-guanghuifeng@linux.alibaba.com> <20260603151804.1963871-24-guanghuifeng@linux.alibaba.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; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260603151804.1963871-24-guanghuifeng@linux.alibaba.com> On Wed, Jun 03, 2026 at 11:17:55PM +0800, Guanghui Feng wrote: > Use iommu_iova_to_phys_length() to get PTE page size, allowing > traversal by actual mapping granularity instead of PAGE_SIZE steps. > > Signed-off-by: Guanghui Feng > Acked-by: Shiqiang Zhang > Acked-by: Simon Guo > --- > drivers/vfio/vfio_iommu_type1.c | 27 ++++++++++++++++++++++----- > 1 file changed, 22 insertions(+), 5 deletions(-) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index c8151ba54de3..115d88d7003e 100644 > --- a/drivers/vfio/vfio_iommu_type1.c > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -1177,25 +1177,42 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma, > > iommu_iotlb_gather_init(&iotlb_gather); > while (pos < dma->size) { > - size_t unmapped, len; > + size_t unmapped, len, pgsize; > phys_addr_t phys, next; > dma_addr_t iova = dma->iova + pos; > > - phys = iommu_iova_to_phys(domain->domain, iova); > - if (WARN_ON(!phys)) { > + /* Single page table walk returns both phys and PTE size */ > + phys = iommu_iova_to_phys_length(domain->domain, iova, > + &pgsize); > + if (WARN_ON(phys == PHYS_ADDR_MAX)) { > pos += PAGE_SIZE; > continue; > } > + if (WARN_ON(!pgsize || pgsize < PAGE_SIZE)) > + pgsize = PAGE_SIZE; > > /* > * To optimize for fewer iommu_unmap() calls, each of which > * may require hardware cache flushing, try to find the > * largest contiguous physical memory chunk to unmap. > + * > + * mapped_length already accounts for contiguous entries > + * from iova, then try to join following physically > + * contiguous PTEs. > */ > - for (len = PAGE_SIZE; pos + len < dma->size; len += PAGE_SIZE) { > - next = iommu_iova_to_phys(domain->domain, iova + len); > + len = min_t(size_t, pgsize, dma->size - pos); > + for (; pos + len < dma->size; ) { > + size_t next_pgsize; > + > + next = iommu_iova_to_phys_length(domain->domain, > + iova + len, > + &next_pgsize); vfio should not be calling it twice, the core code needs to give the best length as efficiently as it can. not open coding this in callers. I think I've said this three times now Jason