Skip to content

Commit 54adadf

Browse files
rientjesChristoph Hellwig
authored andcommitted
dma-pool: dynamically expanding atomic pools
When an atomic pool becomes fully depleted because it is now relied upon for all non-blocking allocations through the DMA API, allow background expansion of each pool by a kworker. When an atomic pool has less than the default size of memory left, kick off a kworker to dynamically expand the pool in the background. The pool is doubled in size, up to MAX_ORDER-1. If memory cannot be allocated at the requested order, smaller allocation(s) are attempted. This allows the default size to be kept quite low when one or more of the atomic pools is not used. Allocations for lowmem should also use GFP_KERNEL for the benefits of reclaim, so use GFP_KERNEL | GFP_DMA and GFP_KERNEL | GFP_DMA32 for lowmem allocations. This also allows __dma_atomic_pool_init() to return a pointer to the pool to make initialization cleaner. Also switch over some node ids to the more appropriate NUMA_NO_NODE. Signed-off-by: David Rientjes <rientjes@google.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent c84dc6e commit 54adadf

1 file changed

Lines changed: 84 additions & 38 deletions

File tree

‎kernel/dma/pool.c‎

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
#include <linux/init.h>
1010
#include <linux/genalloc.h>
1111
#include <linux/slab.h>
12+
#include <linux/workqueue.h>
1213

1314
static struct gen_pool *atomic_pool_dma __ro_after_init;
1415
static struct gen_pool *atomic_pool_dma32 __ro_after_init;
1516
static struct gen_pool *atomic_pool_kernel __ro_after_init;
1617

1718
#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
18-
static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
19+
static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
20+
21+
/* Dynamic background expansion when the atomic pool is near capacity */
22+
static struct work_struct atomic_pool_work;
1923

2024
static int __init early_coherent_pool(char *p)
2125
{
@@ -24,76 +28,116 @@ static int __init early_coherent_pool(char *p)
2428
}
2529
early_param("coherent_pool", early_coherent_pool);
2630

27-
static int __init __dma_atomic_pool_init(struct gen_pool **pool,
28-
size_t pool_size, gfp_t gfp)
31+
static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,
32+
gfp_t gfp)
2933
{
30-
const unsigned int order = get_order(pool_size);
31-
const unsigned long nr_pages = pool_size >> PAGE_SHIFT;
34+
unsigned int order;
3235
struct page *page;
3336
void *addr;
34-
int ret;
37+
int ret = -ENOMEM;
38+
39+
/* Cannot allocate larger than MAX_ORDER-1 */
40+
order = min(get_order(pool_size), MAX_ORDER-1);
41+
42+
do {
43+
pool_size = 1 << (PAGE_SHIFT + order);
3544

36-
if (dev_get_cma_area(NULL))
37-
page = dma_alloc_from_contiguous(NULL, nr_pages, order, false);
38-
else
39-
page = alloc_pages(gfp, order);
45+
if (dev_get_cma_area(NULL))
46+
page = dma_alloc_from_contiguous(NULL, 1 << order,
47+
order, false);
48+
else
49+
page = alloc_pages(gfp, order);
50+
} while (!page && order-- > 0);
4051
if (!page)
4152
goto out;
4253

4354
arch_dma_prep_coherent(page, pool_size);
4455

45-
*pool = gen_pool_create(PAGE_SHIFT, -1);
46-
if (!*pool)
47-
goto free_page;
48-
4956
addr = dma_common_contiguous_remap(page, pool_size,
5057
pgprot_dmacoherent(PAGE_KERNEL),
5158
__builtin_return_address(0));
5259
if (!addr)
53-
goto destroy_genpool;
60+
goto free_page;
5461

55-
ret = gen_pool_add_virt(*pool, (unsigned long)addr, page_to_phys(page),
56-
pool_size, -1);
62+
ret = gen_pool_add_virt(pool, (unsigned long)addr, page_to_phys(page),
63+
pool_size, NUMA_NO_NODE);
5764
if (ret)
5865
goto remove_mapping;
59-
gen_pool_set_algo(*pool, gen_pool_first_fit_order_align, NULL);
6066

61-
pr_info("DMA: preallocated %zu KiB %pGg pool for atomic allocations\n",
62-
pool_size >> 10, &gfp);
6367
return 0;
6468

6569
remove_mapping:
6670
dma_common_free_remap(addr, pool_size);
67-
destroy_genpool:
68-
gen_pool_destroy(*pool);
69-
*pool = NULL;
7071
free_page:
71-
if (!dma_release_from_contiguous(NULL, page, nr_pages))
72+
if (!dma_release_from_contiguous(NULL, page, 1 << order))
7273
__free_pages(page, order);
7374
out:
74-
pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n",
75-
pool_size >> 10, &gfp);
76-
return -ENOMEM;
75+
return ret;
76+
}
77+
78+
static void atomic_pool_resize(struct gen_pool *pool, gfp_t gfp)
79+
{
80+
if (pool && gen_pool_avail(pool) < atomic_pool_size)
81+
atomic_pool_expand(pool, gen_pool_size(pool), gfp);
82+
}
83+
84+
static void atomic_pool_work_fn(struct work_struct *work)
85+
{
86+
if (IS_ENABLED(CONFIG_ZONE_DMA))
87+
atomic_pool_resize(atomic_pool_dma,
88+
GFP_KERNEL | GFP_DMA);
89+
if (IS_ENABLED(CONFIG_ZONE_DMA32))
90+
atomic_pool_resize(atomic_pool_dma32,
91+
GFP_KERNEL | GFP_DMA32);
92+
atomic_pool_resize(atomic_pool_kernel, GFP_KERNEL);
93+
}
94+
95+
static __init struct gen_pool *__dma_atomic_pool_init(size_t pool_size,
96+
gfp_t gfp)
97+
{
98+
struct gen_pool *pool;
99+
int ret;
100+
101+
pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE);
102+
if (!pool)
103+
return NULL;
104+
105+
gen_pool_set_algo(pool, gen_pool_first_fit_order_align, NULL);
106+
107+
ret = atomic_pool_expand(pool, pool_size, gfp);
108+
if (ret) {
109+
gen_pool_destroy(pool);
110+
pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n",
111+
pool_size >> 10, &gfp);
112+
return NULL;
113+
}
114+
115+
pr_info("DMA: preallocated %zu KiB %pGg pool for atomic allocations\n",
116+
gen_pool_size(pool) >> 10, &gfp);
117+
return pool;
77118
}
78119

79120
static int __init dma_atomic_pool_init(void)
80121
{
81122
int ret = 0;
82-
int err;
83123

84-
ret = __dma_atomic_pool_init(&atomic_pool_kernel, atomic_pool_size,
85-
GFP_KERNEL);
124+
INIT_WORK(&atomic_pool_work, atomic_pool_work_fn);
125+
126+
atomic_pool_kernel = __dma_atomic_pool_init(atomic_pool_size,
127+
GFP_KERNEL);
128+
if (!atomic_pool_kernel)
129+
ret = -ENOMEM;
86130
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;
131+
atomic_pool_dma = __dma_atomic_pool_init(atomic_pool_size,
132+
GFP_KERNEL | GFP_DMA);
133+
if (!atomic_pool_dma)
134+
ret = -ENOMEM;
91135
}
92136
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;
137+
atomic_pool_dma32 = __dma_atomic_pool_init(atomic_pool_size,
138+
GFP_KERNEL | GFP_DMA32);
139+
if (!atomic_pool_dma32)
140+
ret = -ENOMEM;
97141
}
98142
return ret;
99143
}
@@ -142,6 +186,8 @@ void *dma_alloc_from_pool(struct device *dev, size_t size,
142186
ptr = (void *)val;
143187
memset(ptr, 0, size);
144188
}
189+
if (gen_pool_avail(pool) < atomic_pool_size)
190+
schedule_work(&atomic_pool_work);
145191

146192
return ptr;
147193
}

0 commit comments

Comments
 (0)