dma-direct: always leak memory that can't be re-encrypted

We must never let unencrypted memory go back into the general page pool.
So if we fail to set it back to encrypted when freeing DMA memory, leak
the memory instead and warn the user.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
This commit is contained in:
Christoph Hellwig 2021-11-09 15:41:01 +01:00
parent 5570449b68
commit a90cf30437

View File

@ -84,9 +84,14 @@ static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size)
static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
{
int ret;
if (!force_dma_unencrypted(dev))
return 0;
return set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size));
ret = set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size));
if (ret)
pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
return ret;
}
static void __dma_direct_free_pages(struct device *dev, struct page *page,
@ -261,7 +266,6 @@ done:
return ret;
out_encrypt_pages:
/* If memory cannot be re-encrypted, it must be leaked */
if (dma_set_encrypted(dev, page_address(page), size))
return NULL;
out_free_pages:
@ -307,7 +311,8 @@ void dma_direct_free(struct device *dev, size_t size,
} else {
if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED))
arch_dma_clear_uncached(cpu_addr, size);
dma_set_encrypted(dev, cpu_addr, 1 << page_order);
if (dma_set_encrypted(dev, cpu_addr, 1 << page_order))
return;
}
__dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);
@ -361,7 +366,8 @@ void dma_direct_free_pages(struct device *dev, size_t size,
dma_free_from_pool(dev, vaddr, size))
return;
dma_set_encrypted(dev, vaddr, 1 << page_order);
if (dma_set_encrypted(dev, vaddr, 1 << page_order))
return;
__dma_direct_free_pages(dev, page, size);
}