• Petr Tesarik's avatar
    swiotlb: extend buffer pre-padding to alloc_align_mask if necessary · af133562
    Petr Tesarik authored
    Allow a buffer pre-padding of up to alloc_align_mask, even if it requires
    allocating additional IO TLB slots.
    
    If the allocation alignment is bigger than IO_TLB_SIZE and min_align_mask
    covers any non-zero bits in the original address between IO_TLB_SIZE and
    alloc_align_mask, these bits are not preserved in the swiotlb buffer
    address.
    
    To fix this case, increase the allocation size and use a larger offset
    within the allocated buffer. As a result, extra padding slots may be
    allocated before the mapping start address.
    
    Leave orig_addr in these padding slots initialized to INVALID_PHYS_ADDR.
    These slots do not correspond to any CPU buffer, so attempts to sync the
    data should be ignored.
    
    The padding slots should be automatically released when the buffer is
    unmapped. However, swiotlb_tbl_unmap_single() takes only the address of the
    DMA buffer slot, not the first padding slot. Save the number of padding
    slots in struct io_tlb_slot and use it to adjust the slot index in
    swiotlb_release_slots(), so all allocated slots are properly freed.
    
    Fixes: 2fd4fa5d3fb5 ("swiotlb: Fix alignment checks when both allocation and DMA masks are present")
    Link: https://lore.kernel.org/linux-iommu/20240311210507.217daf8b@meshulam.tesarici.cz/
    
    Signed-off-by: default avatarPetr Tesarik <petr.tesarik1@huawei-partners.com>
    Reviewed-by: default avatarMichael Kelley <mhklinux@outlook.com>
    Tested-by: default avatarMichael Kelley <mhklinux@outlook.com>
    Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
    af133562
swiotlb.c 50.9 KB