Re: [PATCH v4 5/5] iommu/dma: Force swiotlb_max_mapping_size on an untrusted device

From: Nicolin Chen
Date: Fri Feb 23 2024 - 14:58:40 EST


On Wed, Feb 21, 2024 at 11:39:29PM +0000, Michael Kelley wrote:
> From: Will Deacon <will@xxxxxxxxxx> Sent: Wednesday, February 21, 2024 3:35 AM
> > +static size_t iommu_dma_max_mapping_size(struct device *dev)
> > +{
> > + if (is_swiotlb_active(dev) && dev_is_untrusted(dev))
> > + return swiotlb_max_mapping_size(dev);
> > + return SIZE_MAX;
> > +}
> > +
>
> In this [1] email, Nicolin had a version of this function that incorporated
> the IOMMU granule. For granules bigger than 4K, I think that's needed
> so that when IOMMU code sets the swiotlb alloc_align_mask to the
> IOMMU granule - 1, the larger offset plus the size won't exceed the
> max number of slots. swiotlb_max_mapping_size() by itself may
> return a value that's too big when alloc_align_mask is used.
>
> Michael
>
> [1] https://lore.kernel.org/linux-iommu/SN6PR02MB415727E61B5295C259CCB268D4512@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/#m14dd359c5a4dd13e6cb0c35cf94f8d746257ae48

Yea, I just confirmed that with 64KB PAGE_SIZE the alloc_size
can be over swiotlb_max_mapping_size, i.e. 256KB > 252KB. Yet,
the max size corresponding to the max number of slots should
be 256KB. So, I feel that this is marginally safe?

With that being said, there seems to be a 4KB size waste, due
to aligning with the iommu_domain granule, in this particular
alloc_size=256KB case?

On the other hand, if swiotlb_max_mapping_size was subtracted
by 64KB (granule=64KB), i.e. alloc_size=192KB, which seems to
generate more swiotlb fragments?

Thanks
Nicolin