• Michael Kelley's avatar
    swiotlb: remove alloc_size argument to swiotlb_tbl_map_single() · 327e2c97
    Michael Kelley authored
    Currently swiotlb_tbl_map_single() takes alloc_align_mask and
    alloc_size arguments to specify an swiotlb allocation that is larger
    than mapping_size.  This larger allocation is used solely by
    iommu_dma_map_single() to handle untrusted devices that should not have
    DMA visibility to memory pages that are partially used for unrelated
    kernel data.
    
    Having two arguments to specify the allocation is redundant. While
    alloc_align_mask naturally specifies the alignment of the starting
    address of the allocation, it can also implicitly specify the size
    by rounding up the mapping_size to that alignment.
    
    Additionally, the current approach has an edge case bug.
    iommu_dma_map_page() already does the rounding up to compute the
    alloc_size argument. But swiotlb_tbl_map_single() then calculates the
    alignment offset based on the DMA min_align_mask, and adds that offset to
    alloc_size. If the offset is non-zero, the addition may result in a value
    that is larger than the max the swiotlb can allocate.  If the rounding up
    is done _after_ the alignment offset is added to the mapping_size (and
    the original mapping_size conforms to the value returned by
    swiotlb_max_mapping_size), then the max that the swiotlb can allocate
    will not be exceeded.
    
    In view of these issues, simplify the swiotlb_tbl_map_single() interface
    by removing the alloc_size argument. Most call sites pass the same value
    for mapping_size and alloc_size, and they pass alloc_align_mask as zero.
    Just remove the redundant argument from these callers, as they will see
    no functional change. For iommu_dma_map_page() also remove the alloc_size
    argument, and have swiotlb_tbl_map_single() compute the alloc_size by
    rounding up mapping_size after adding the offset based on min_align_mask.
    This has the side effect of fixing the edge case bug but with no other
    functional change.
    
    Also add a sanity test on the alloc_align_mask. While IOMMU code
    currently ensures the granule is not larger than PAGE_SIZE, if that
    guarantee were to be removed in the future, the downstream effect on the
    swiotlb might go unnoticed until strange allocation failures occurred.
    
    Tested on an ARM64 system with 16K page size and some kernel test-only
    hackery to allow modifying the DMA min_align_mask and the granule size
    that becomes the alloc_align_mask. Tested these combinations with a
    variety of original memory addresses and sizes, including those that
    reproduce the edge case bug:
    
     * 4K granule and 0 min_align_mask
     * 4K granule and 0xFFF min_align_mask (4K - 1)
     * 16K granule and 0xFFF min_align_mask
     * 64K granule and 0xFFF min_align_mask
     * 64K granule and 0x3FFF min_align_mask (16K - 1)
    
    With the changes, all combinations pass.
    Signed-off-by: default avatarMichael Kelley <mhklinux@outlook.com>
    Reviewed-by: default avatarPetr Tesarik <petr@tesarici.cz>
    Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
    327e2c97
swiotlb-xen.c 11.5 KB