• Sean Christopherson's avatar
    swiotlb: rewrite comment explaining why the source is preserved on DMA_FROM_DEVICE · 1132a1dc
    Sean Christopherson authored
    Rewrite the comment explaining why swiotlb copies the original buffer to
    the TLB buffer before initiating DMA *from* the device, i.e. before the
    device DMAs into the TLB buffer.  The existing comment's argument that
    preserving the original data can prevent a kernel memory leak is bogus.
    
    If the driver that triggered the mapping _knows_ that the device will
    overwrite the entire mapping, or the driver will consume only the written
    parts, then copying from the original memory is completely pointless.
    
    If neither of the above holds true, then copying from the original adds
    value only if preserving the data is necessary for functional
    correctness, or the driver explicitly initialized the original memory.
    If the driver didn't initialize the memory, then copying the original
    buffer to the TLB buffer simply changes what kernel data is leaked to
    user space.
    
    Writing the entire TLB buffer _does_ prevent leaking stale TLB buffer
    data from a previous bounce, but that can be achieved by simply zeroing
    the TLB buffer when grabbing a slot.
    
    The real reason swiotlb ended up initializing the TLB buffer with the
    original buffer is that it's necessary to make swiotlb operate as
    transparently as possible, i.e. to behave as closely as possible to
    hardware, and to avoid corrupting the original buffer, e.g. if the driver
    knows the device will do partial writes and is relying on the unwritten
    data to be preserved.
    Reviewed-by: default avatarRobin Murphy <robin.murphy@arm.com>
    Link: https://lore.kernel.org/all/ZN5elYQ5szQndN8n@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
    Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
    1132a1dc
swiotlb.c 46.4 KB