Merge tag 'dma-mapping-7.4-2026-11-18' of git://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux

Pull dma-mapping fixes from Marek Szyprowski:
 "Various fixes for the issues identified by sashiko.dev review system
  in the dma-mapping helpers:

   - added serialization to reserved memory region initialization

   - fixed initialization of the shared memory for confidential
     computing memory pools

   - fixed error and exit code paths"

* tag 'dma-mapping-7.4-2026-11-18' of git://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux:
  dma-pool: decrypt pages before creating the shared remap
  dma: swiotlb: Properly tear down the default pool on exit
  dma: swiotlb: Round restricted DMA pool size down to IO_TLB_SEGSIZE
  dma-pool: release CMA pages on atomic_pool_expand() error path
  dma: swiotlb: Serialize restricted DMA pool initialization
  dma-coherent: serialize reserved memory pool initialization
This commit is contained in:
Linus Torvalds
2026-11-18 11:07:30 -08:00
3 changed files with 70 additions and 27 deletions
+10 -1
View File
@@ -7,6 +7,7 @@
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/dma-direct.h>
#include <linux/dma-map-ops.h>
@@ -335,10 +336,18 @@ static phys_addr_t dma_reserved_default_memory_base __initdata;
static phys_addr_t dma_reserved_default_memory_size __initdata;
#endif
static DEFINE_MUTEX(rmem_dma_mutex);
static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
{
struct dma_coherent_mem *mem = rmem->priv;
struct dma_coherent_mem *mem;
/*
* Multiple devices can share the same pool and may be probed
* concurrently, so serialize the initialization of rmem->priv.
*/
guard(mutex)(&rmem_dma_mutex);
mem = rmem->priv;
if (!mem) {
mem = dma_init_coherent_memory(rmem->base, rmem->base,
rmem->size, true);
+28 -23
View File
@@ -106,6 +106,22 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
if (!page)
goto out;
/*
* Memory in the atomic DMA pools must be unencrypted, the pools do not
* shrink so no re-encryption occurs in dma_direct_free().
*/
if (dma_pool->cc_shared) {
ret = set_memory_decrypted((unsigned long)page_to_virt(page),
1 << order);
/*
* On failure the encryption state of the pages is unknown,
* so they must not be returned to the page allocator or CMA.
* Leak them intentionally.
*/
if (ret)
goto out;
}
arch_dma_prep_coherent(page, pool_size);
#ifdef CONFIG_DMA_DIRECT_REMAP
@@ -116,43 +132,32 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
addr = dma_common_contiguous_remap(page, pool_size, prot,
__builtin_return_address(0));
if (!addr)
goto free_page;
if (!addr) {
ret = -ENOMEM;
goto encrypt_pages;
}
#else
addr = page_to_virt(page);
#endif
/*
* Memory in the atomic DMA pools must be unencrypted, the pools do not
* shrink so no re-encryption occurs in dma_direct_free().
*/
if (dma_pool->cc_shared) {
ret = set_memory_decrypted((unsigned long)page_to_virt(page),
1 << order);
if (ret) {
leak_pages = true;
goto remove_mapping;
}
}
ret = gen_pool_add_virt(dma_pool->pool, (unsigned long)addr,
page_to_phys(page), pool_size, NUMA_NO_NODE);
if (ret)
goto encrypt_mapping;
goto remove_mapping;
dma_atomic_pool_size_add(gfp, pool_size);
return 0;
encrypt_mapping:
remove_mapping:
#ifdef CONFIG_DMA_DIRECT_REMAP
dma_common_free_remap(addr, pool_size);
encrypt_pages:
#endif
if (dma_pool->cc_shared &&
set_memory_encrypted((unsigned long)page_to_virt(page), 1 << order))
leak_pages = true;
remove_mapping:
#ifdef CONFIG_DMA_DIRECT_REMAP
dma_common_free_remap(addr, pool_size);
free_page:
#endif
if (!leak_pages)
if (!leak_pages &&
!dma_release_from_contiguous(NULL, page, 1 << order))
__free_pages(page, order);
out:
return ret;
+32 -3
View File
@@ -35,6 +35,7 @@
#include <linux/init.h>
#include <linux/memblock.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/pfn.h>
#include <linux/rculist.h>
#include <linux/scatterlist.h>
@@ -664,6 +665,17 @@ void __init swiotlb_exit(void)
return;
pr_info("tearing down default memory pool\n");
#ifdef CONFIG_SWIOTLB_DYNAMIC
io_tlb_default_mem.can_grow = false;
cancel_work_sync(&io_tlb_default_mem.dyn_alloc);
spin_lock(&io_tlb_default_mem.lock);
list_del_rcu(&mem->node);
io_tlb_default_mem.nslabs -= mem->nslabs;
spin_unlock(&io_tlb_default_mem.lock);
synchronize_rcu();
#else
io_tlb_default_mem.nslabs = 0;
#endif
tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
tbl_size = PAGE_ALIGN(mem->end - mem->start);
slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
@@ -1986,10 +1998,12 @@ void swiotlb_free_from_pool(struct device *dev,
swiotlb_release_slots(dev, tlb_addr, pool);
}
static DEFINE_MUTEX(rmem_swiotlb_mutex);
static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
struct device *dev)
{
struct io_tlb_mem *mem = rmem->priv;
struct io_tlb_mem *mem;
unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
/* Set Per-device io tlb area to one */
@@ -2003,10 +2017,25 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
/*
* Since multiple devices can share the same pool, the private data,
* io_tlb_mem struct, will be initialized by the first device attached
* to it.
* to it. Serialize this, as devices may be probed concurrently.
*/
guard(mutex)(&rmem_swiotlb_mutex);
mem = rmem->priv;
if (!mem) {
struct io_tlb_pool *pool;
unsigned long aligned_nslabs = ALIGN_DOWN(nslabs, IO_TLB_SEGSIZE);
if (aligned_nslabs != nslabs) {
dev_warn(dev, "Restricted DMA pool size %zu is not a multiple of %lu, rounding down to %lu bytes\n",
(size_t)rmem->size,
(unsigned long)IO_TLB_SEGSIZE << IO_TLB_SHIFT,
aligned_nslabs << IO_TLB_SHIFT);
nslabs = aligned_nslabs;
}
if (!nslabs) {
dev_err(dev, "Restricted DMA pool is too small\n");
return -EINVAL;
}
mem = kzalloc_obj(*mem);
if (!mem)
@@ -2034,7 +2063,7 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
mem->cc_shared = true;
ret = set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
rmem->size >> PAGE_SHIFT);
(nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
if (ret) {
dev_err(dev, "Failed to decrypt restricted DMA pool\n");
kfree(pool->areas);