Skip to content

Commit 76a1994

Browse files
rientjesChristoph Hellwig
authored and
Christoph Hellwig
committed
dma-direct: atomic allocations must come from atomic coherent pools
When a device requires unencrypted memory and the context does not allow blocking, memory must be returned from the atomic coherent pools. This avoids the remap when CONFIG_DMA_DIRECT_REMAP is not enabled and the config only requires CONFIG_DMA_COHERENT_POOL. This will be used for CONFIG_AMD_MEM_ENCRYPT in a subsequent patch. Keep all memory in these pools unencrypted. When set_memory_decrypted() fails, this prohibits the memory from being added. If adding memory to the genpool fails, and set_memory_encrypted() subsequently fails, there is no alternative other than leaking the memory. Signed-off-by: David Rientjes <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 54adadf commit 76a1994

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

kernel/dma/direct.c

+39-7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,39 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
7676
min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit);
7777
}
7878

79+
/*
80+
* Decrypting memory is allowed to block, so if this device requires
81+
* unencrypted memory it must come from atomic pools.
82+
*/
83+
static inline bool dma_should_alloc_from_pool(struct device *dev, gfp_t gfp,
84+
unsigned long attrs)
85+
{
86+
if (!IS_ENABLED(CONFIG_DMA_COHERENT_POOL))
87+
return false;
88+
if (gfpflags_allow_blocking(gfp))
89+
return false;
90+
if (force_dma_unencrypted(dev))
91+
return true;
92+
if (!IS_ENABLED(CONFIG_DMA_DIRECT_REMAP))
93+
return false;
94+
if (dma_alloc_need_uncached(dev, attrs))
95+
return true;
96+
return false;
97+
}
98+
99+
static inline bool dma_should_free_from_pool(struct device *dev,
100+
unsigned long attrs)
101+
{
102+
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL))
103+
return true;
104+
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
105+
!force_dma_unencrypted(dev))
106+
return false;
107+
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP))
108+
return true;
109+
return false;
110+
}
111+
79112
struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
80113
gfp_t gfp, unsigned long attrs)
81114
{
@@ -125,9 +158,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
125158
struct page *page;
126159
void *ret;
127160

128-
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
129-
dma_alloc_need_uncached(dev, attrs) &&
130-
!gfpflags_allow_blocking(gfp)) {
161+
if (dma_should_alloc_from_pool(dev, gfp, attrs)) {
131162
ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp);
132163
if (!ret)
133164
return NULL;
@@ -204,17 +235,18 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
204235
{
205236
unsigned int page_order = get_order(size);
206237

238+
/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
239+
if (dma_should_free_from_pool(dev, attrs) &&
240+
dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
241+
return;
242+
207243
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
208244
!force_dma_unencrypted(dev)) {
209245
/* cpu_addr is a struct page cookie, not a kernel address */
210246
dma_free_contiguous(dev, cpu_addr, size);
211247
return;
212248
}
213249

214-
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
215-
dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
216-
return;
217-
218250
if (force_dma_unencrypted(dev))
219251
set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);
220252

kernel/dma/pool.c

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/dma-contiguous.h>
99
#include <linux/init.h>
1010
#include <linux/genalloc.h>
11+
#include <linux/set_memory.h>
1112
#include <linux/slab.h>
1213
#include <linux/workqueue.h>
1314

@@ -53,22 +54,42 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,
5354

5455
arch_dma_prep_coherent(page, pool_size);
5556

57+
#ifdef CONFIG_DMA_DIRECT_REMAP
5658
addr = dma_common_contiguous_remap(page, pool_size,
5759
pgprot_dmacoherent(PAGE_KERNEL),
5860
__builtin_return_address(0));
5961
if (!addr)
6062
goto free_page;
61-
63+
#else
64+
addr = page_to_virt(page);
65+
#endif
66+
/*
67+
* Memory in the atomic DMA pools must be unencrypted, the pools do not
68+
* shrink so no re-encryption occurs in dma_direct_free_pages().
69+
*/
70+
ret = set_memory_decrypted((unsigned long)page_to_virt(page),
71+
1 << order);
72+
if (ret)
73+
goto remove_mapping;
6274
ret = gen_pool_add_virt(pool, (unsigned long)addr, page_to_phys(page),
6375
pool_size, NUMA_NO_NODE);
6476
if (ret)
65-
goto remove_mapping;
77+
goto encrypt_mapping;
6678

6779
return 0;
6880

81+
encrypt_mapping:
82+
ret = set_memory_encrypted((unsigned long)page_to_virt(page),
83+
1 << order);
84+
if (WARN_ON_ONCE(ret)) {
85+
/* Decrypt succeeded but encrypt failed, purposely leak */
86+
goto out;
87+
}
6988
remove_mapping:
89+
#ifdef CONFIG_DMA_DIRECT_REMAP
7090
dma_common_free_remap(addr, pool_size);
71-
free_page:
91+
#endif
92+
free_page: __maybe_unused
7293
if (!dma_release_from_contiguous(NULL, page, 1 << order))
7394
__free_pages(page, order);
7495
out:

0 commit comments

Comments
 (0)