Skip to content

Commit c84dc6e

Browse files
rientjesChristoph Hellwig
authored andcommitted
dma-pool: add additional coherent pools to map to gfp mask
The single atomic pool is allocated from the lowest zone possible since it is guaranteed to be applicable for any DMA allocation. Devices may allocate through the DMA API but not have a strict reliance on GFP_DMA memory. Since the atomic pool will be used for all non-blockable allocations, returning all memory from ZONE_DMA may unnecessarily deplete the zone. Provision for multiple atomic pools that will map to the optimal gfp mask of the device. When allocating non-blockable memory, determine the optimal gfp mask of the device and use the appropriate atomic pool. The coherent DMA mask will remain the same between allocation and free and, thus, memory will be freed to the same atomic pool it was allocated from. __dma_atomic_pool_init() will be changed to return struct gen_pool * later once dynamic expansion is added. Signed-off-by: David Rientjes <rientjes@google.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent e860c29 commit c84dc6e

5 files changed

Lines changed: 91 additions & 54 deletions

File tree

‎drivers/iommu/dma-iommu.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
952952

953953
/* Non-coherent atomic allocation? Easy */
954954
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
955-
dma_free_from_pool(cpu_addr, alloc_size))
955+
dma_free_from_pool(dev, cpu_addr, alloc_size))
956956
return;
957957

958958
if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
@@ -1035,7 +1035,8 @@ static void *iommu_dma_alloc(struct device *dev, size_t size,
10351035

10361036
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
10371037
!gfpflags_allow_blocking(gfp) && !coherent)
1038-
cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp);
1038+
cpu_addr = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page,
1039+
gfp);
10391040
else
10401041
cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
10411042
if (!cpu_addr)

‎include/linux/dma-direct.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
6767
}
6868

6969
u64 dma_direct_get_required_mask(struct device *dev);
70+
gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
71+
u64 *phys_mask);
7072
void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
7173
gfp_t gfp, unsigned long attrs);
7274
void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,

‎include/linux/dma-mapping.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,9 @@ void *dma_common_pages_remap(struct page **pages, size_t size,
630630
pgprot_t prot, const void *caller);
631631
void dma_common_free_remap(void *cpu_addr, size_t size);
632632

633-
bool dma_in_atomic_pool(void *start, size_t size);
634-
void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags);
635-
bool dma_free_from_pool(void *start, size_t size);
633+
void *dma_alloc_from_pool(struct device *dev, size_t size,
634+
struct page **ret_page, gfp_t flags);
635+
bool dma_free_from_pool(struct device *dev, void *start, size_t size);
636636

637637
int
638638
dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr,

‎kernel/dma/direct.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ u64 dma_direct_get_required_mask(struct device *dev)
4545
return (1ULL << (fls64(max_dma) - 1)) * 2 - 1;
4646
}
4747

48-
static gfp_t __dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
49-
u64 *phys_limit)
48+
gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
49+
u64 *phys_limit)
5050
{
5151
u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit);
5252

@@ -89,8 +89,8 @@ struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
8989

9090
/* we always manually zero the memory once we are done: */
9191
gfp &= ~__GFP_ZERO;
92-
gfp |= __dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
93-
&phys_limit);
92+
gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
93+
&phys_limit);
9494
page = dma_alloc_contiguous(dev, alloc_size, gfp);
9595
if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
9696
dma_free_contiguous(dev, page, alloc_size);
@@ -128,7 +128,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
128128
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
129129
dma_alloc_need_uncached(dev, attrs) &&
130130
!gfpflags_allow_blocking(gfp)) {
131-
ret = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp);
131+
ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp);
132132
if (!ret)
133133
return NULL;
134134
goto done;
@@ -212,7 +212,7 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
212212
}
213213

214214
if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
215-
dma_free_from_pool(cpu_addr, PAGE_ALIGN(size)))
215+
dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
216216
return;
217217

218218
if (force_dma_unencrypted(dev))

‎kernel/dma/pool.c‎

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <linux/genalloc.h>
1111
#include <linux/slab.h>
1212

13-
static struct gen_pool *atomic_pool __ro_after_init;
13+
static struct gen_pool *atomic_pool_dma __ro_after_init;
14+
static struct gen_pool *atomic_pool_dma32 __ro_after_init;
15+
static struct gen_pool *atomic_pool_kernel __ro_after_init;
1416

1517
#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
1618
static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
@@ -22,89 +24,119 @@ static int __init early_coherent_pool(char *p)
2224
}
2325
early_param("coherent_pool", early_coherent_pool);
2426

25-
static gfp_t dma_atomic_pool_gfp(void)
27+
static int __init __dma_atomic_pool_init(struct gen_pool **pool,
28+
size_t pool_size, gfp_t gfp)
2629
{
27-
if (IS_ENABLED(CONFIG_ZONE_DMA))
28-
return GFP_DMA;
29-
if (IS_ENABLED(CONFIG_ZONE_DMA32))
30-
return GFP_DMA32;
31-
return GFP_KERNEL;
32-
}
33-
34-
static int __init dma_atomic_pool_init(void)
35-
{
36-
unsigned int pool_size_order = get_order(atomic_pool_size);
37-
unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
30+
const unsigned int order = get_order(pool_size);
31+
const unsigned long nr_pages = pool_size >> PAGE_SHIFT;
3832
struct page *page;
3933
void *addr;
4034
int ret;
4135

4236
if (dev_get_cma_area(NULL))
43-
page = dma_alloc_from_contiguous(NULL, nr_pages,
44-
pool_size_order, false);
37+
page = dma_alloc_from_contiguous(NULL, nr_pages, order, false);
4538
else
46-
page = alloc_pages(dma_atomic_pool_gfp(), pool_size_order);
39+
page = alloc_pages(gfp, order);
4740
if (!page)
4841
goto out;
4942

50-
arch_dma_prep_coherent(page, atomic_pool_size);
43+
arch_dma_prep_coherent(page, pool_size);
5144

52-
atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
53-
if (!atomic_pool)
45+
*pool = gen_pool_create(PAGE_SHIFT, -1);
46+
if (!*pool)
5447
goto free_page;
5548

56-
addr = dma_common_contiguous_remap(page, atomic_pool_size,
49+
addr = dma_common_contiguous_remap(page, pool_size,
5750
pgprot_dmacoherent(PAGE_KERNEL),
5851
__builtin_return_address(0));
5952
if (!addr)
6053
goto destroy_genpool;
6154

62-
ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
63-
page_to_phys(page), atomic_pool_size, -1);
55+
ret = gen_pool_add_virt(*pool, (unsigned long)addr, page_to_phys(page),
56+
pool_size, -1);
6457
if (ret)
6558
goto remove_mapping;
66-
gen_pool_set_algo(atomic_pool, gen_pool_first_fit_order_align, NULL);
59+
gen_pool_set_algo(*pool, gen_pool_first_fit_order_align, NULL);
6760

68-
pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
69-
atomic_pool_size / 1024);
61+
pr_info("DMA: preallocated %zu KiB %pGg pool for atomic allocations\n",
62+
pool_size >> 10, &gfp);
7063
return 0;
7164

7265
remove_mapping:
73-
dma_common_free_remap(addr, atomic_pool_size);
66+
dma_common_free_remap(addr, pool_size);
7467
destroy_genpool:
75-
gen_pool_destroy(atomic_pool);
76-
atomic_pool = NULL;
68+
gen_pool_destroy(*pool);
69+
*pool = NULL;
7770
free_page:
7871
if (!dma_release_from_contiguous(NULL, page, nr_pages))
79-
__free_pages(page, pool_size_order);
72+
__free_pages(page, order);
8073
out:
81-
pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
82-
atomic_pool_size / 1024);
74+
pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n",
75+
pool_size >> 10, &gfp);
8376
return -ENOMEM;
8477
}
78+
79+
static int __init dma_atomic_pool_init(void)
80+
{
81+
int ret = 0;
82+
int err;
83+
84+
ret = __dma_atomic_pool_init(&atomic_pool_kernel, atomic_pool_size,
85+
GFP_KERNEL);
86+
if (IS_ENABLED(CONFIG_ZONE_DMA)) {
87+
err = __dma_atomic_pool_init(&atomic_pool_dma,
88+
atomic_pool_size, GFP_DMA);
89+
if (!ret && err)
90+
ret = err;
91+
}
92+
if (IS_ENABLED(CONFIG_ZONE_DMA32)) {
93+
err = __dma_atomic_pool_init(&atomic_pool_dma32,
94+
atomic_pool_size, GFP_DMA32);
95+
if (!ret && err)
96+
ret = err;
97+
}
98+
return ret;
99+
}
85100
postcore_initcall(dma_atomic_pool_init);
86101

87-
bool dma_in_atomic_pool(void *start, size_t size)
102+
static inline struct gen_pool *dev_to_pool(struct device *dev)
88103
{
89-
if (unlikely(!atomic_pool))
90-
return false;
104+
u64 phys_mask;
105+
gfp_t gfp;
106+
107+
gfp = dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
108+
&phys_mask);
109+
if (IS_ENABLED(CONFIG_ZONE_DMA) && gfp == GFP_DMA)
110+
return atomic_pool_dma;
111+
if (IS_ENABLED(CONFIG_ZONE_DMA32) && gfp == GFP_DMA32)
112+
return atomic_pool_dma32;
113+
return atomic_pool_kernel;
114+
}
91115

92-
return gen_pool_has_addr(atomic_pool, (unsigned long)start, size);
116+
static bool dma_in_atomic_pool(struct device *dev, void *start, size_t size)
117+
{
118+
struct gen_pool *pool = dev_to_pool(dev);
119+
120+
if (unlikely(!pool))
121+
return false;
122+
return gen_pool_has_addr(pool, (unsigned long)start, size);
93123
}
94124

95-
void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
125+
void *dma_alloc_from_pool(struct device *dev, size_t size,
126+
struct page **ret_page, gfp_t flags)
96127
{
128+
struct gen_pool *pool = dev_to_pool(dev);
97129
unsigned long val;
98130
void *ptr = NULL;
99131

100-
if (!atomic_pool) {
101-
WARN(1, "coherent pool not initialised!\n");
132+
if (!pool) {
133+
WARN(1, "%pGg atomic pool not initialised!\n", &flags);
102134
return NULL;
103135
}
104136

105-
val = gen_pool_alloc(atomic_pool, size);
137+
val = gen_pool_alloc(pool, size);
106138
if (val) {
107-
phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
139+
phys_addr_t phys = gen_pool_virt_to_phys(pool, val);
108140

109141
*ret_page = pfn_to_page(__phys_to_pfn(phys));
110142
ptr = (void *)val;
@@ -114,10 +146,12 @@ void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
114146
return ptr;
115147
}
116148

117-
bool dma_free_from_pool(void *start, size_t size)
149+
bool dma_free_from_pool(struct device *dev, void *start, size_t size)
118150
{
119-
if (!dma_in_atomic_pool(start, size))
151+
struct gen_pool *pool = dev_to_pool(dev);
152+
153+
if (!dma_in_atomic_pool(dev, start, size))
120154
return false;
121-
gen_pool_free(atomic_pool, (unsigned long)start, size);
155+
gen_pool_free(pool, (unsigned long)start, size);
122156
return true;
123157
}

0 commit comments

Comments
 (0)