Skip to content

Commit b7176c2

Browse files
Barry SongChristoph Hellwig
authored andcommitted
dma-contiguous: provide the ability to reserve per-numa CMA
Right now, drivers like ARM SMMU are using dma_alloc_coherent() to get coherent DMA buffers to save their command queues and page tables. As there is only one default CMA in the whole system, SMMUs on nodes other than node0 will get remote memory. This leads to significant latency. This patch provides per-numa CMA so that drivers like SMMU can get local memory. Tests show localizing CMA can decrease dma_unmap latency much. For instance, before this patch, SMMU on node2 has to wait for more than 560ns for the completion of CMD_SYNC in an empty command queue; with this patch, it needs 240ns only. A positive side effect of this patch would be improving performance even further for those users who are worried about performance more than DMA security and use iommu.passthrough=1 to skip IOMMU. With local CMA, all drivers can get local coherent DMA buffers. Also, this patch changes the default CONFIG_CMA_AREAS to 19 in NUMA. As 1+CONFIG_CMA_AREAS should be quite enough for most servers on the market even they enable both hugetlb_cma and pernuma_cma. 2 numa nodes: 2(hugetlb) + 2(pernuma) + 1(default global cma) = 5 4 numa nodes: 4(hugetlb) + 4(pernuma) + 1(default global cma) = 9 8 numa nodes: 8(hugetlb) + 8(pernuma) + 1(default global cma) = 17 Signed-off-by: Barry Song <song.bao.hua@hisilicon.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent f75aef3 commit b7176c2

5 files changed

Lines changed: 120 additions & 11 deletions

File tree

‎Documentation/admin-guide/kernel-parameters.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,17 @@
599599
altogether. For more information, see
600600
include/linux/dma-contiguous.h
601601

602+
cma_pernuma=nn[MG]
603+
[ARM64,KNL]
604+
Sets the size of kernel per-numa memory area for
605+
contiguous memory allocations. A value of 0 disables
606+
per-numa CMA altogether. And If this option is not
607+
specificed, the default value is 0.
608+
With per-numa CMA enabled, DMA users on node nid will
609+
first try to allocate buffer from the pernuma area
610+
which is located in node nid, if the allocation fails,
611+
they will fallback to the global default memory area.
612+
602613
cmo_free_hint= [PPC] Format: { yes | no }
603614
Specify whether pages are marked as being inactive
604615
when they are freed. This is used in CMO environments

‎include/linux/dma-contiguous.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ static inline void dma_free_contiguous(struct device *dev, struct page *page,
171171

172172
#endif
173173

174+
#ifdef CONFIG_DMA_PERNUMA_CMA
175+
void dma_pernuma_cma_reserve(void);
176+
#else
177+
static inline void dma_pernuma_cma_reserve(void) { }
178+
#endif
179+
174180
#endif
175181

176182
#endif

‎kernel/dma/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ config DMA_CMA
118118
If unsure, say "n".
119119

120120
if DMA_CMA
121+
122+
config DMA_PERNUMA_CMA
123+
bool "Enable separate DMA Contiguous Memory Area for each NUMA Node"
124+
default NUMA && ARM64
125+
help
126+
Enable this option to get pernuma CMA areas so that devices like
127+
ARM64 SMMU can get local memory by DMA coherent APIs.
128+
129+
You can set the size of pernuma CMA by specifying "cma_pernuma=size"
130+
on the kernel's command line.
131+
121132
comment "Default contiguous memory area size:"
122133

123134
config CMA_SIZE_MBYTES

‎kernel/dma/contiguous.c‎

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ static int __init early_cma(char *p)
6969
}
7070
early_param("cma", early_cma);
7171

72+
#ifdef CONFIG_DMA_PERNUMA_CMA
73+
74+
static struct cma *dma_contiguous_pernuma_area[MAX_NUMNODES];
75+
static phys_addr_t pernuma_size_bytes __initdata;
76+
77+
static int __init early_cma_pernuma(char *p)
78+
{
79+
pernuma_size_bytes = memparse(p, &p);
80+
return 0;
81+
}
82+
early_param("cma_pernuma", early_cma_pernuma);
83+
#endif
84+
7285
#ifdef CONFIG_CMA_SIZE_PERCENTAGE
7386

7487
static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
@@ -96,6 +109,34 @@ static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
96109

97110
#endif
98111

112+
#ifdef CONFIG_DMA_PERNUMA_CMA
113+
void __init dma_pernuma_cma_reserve(void)
114+
{
115+
int nid;
116+
117+
if (!pernuma_size_bytes)
118+
return;
119+
120+
for_each_online_node(nid) {
121+
int ret;
122+
char name[20];
123+
struct cma **cma = &dma_contiguous_pernuma_area[nid];
124+
125+
snprintf(name, sizeof(name), "pernuma%d", nid);
126+
ret = cma_declare_contiguous_nid(0, pernuma_size_bytes, 0, 0,
127+
0, false, name, cma, nid);
128+
if (ret) {
129+
pr_warn("%s: reservation failed: err %d, node %d", __func__,
130+
ret, nid);
131+
continue;
132+
}
133+
134+
pr_debug("%s: reserved %llu MiB on node %d\n", __func__,
135+
(unsigned long long)pernuma_size_bytes / SZ_1M, nid);
136+
}
137+
}
138+
#endif
139+
99140
/**
100141
* dma_contiguous_reserve() - reserve area(s) for contiguous memory handling
101142
* @limit: End address of the reserved memory (optional, 0 for any).
@@ -228,23 +269,44 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
228269
* @size: Requested allocation size.
229270
* @gfp: Allocation flags.
230271
*
231-
* This function allocates contiguous memory buffer for specified device. It
232-
* tries to use device specific contiguous memory area if available, or the
233-
* default global one.
272+
* tries to use device specific contiguous memory area if available, or it
273+
* tries to use per-numa cma, if the allocation fails, it will fallback to
274+
* try default global one.
234275
*
235-
* Note that it byapss one-page size of allocations from the global area as
236-
* the addresses within one page are always contiguous, so there is no need
237-
* to waste CMA pages for that kind; it also helps reduce fragmentations.
276+
* Note that it bypass one-page size of allocations from the per-numa and
277+
* global area as the addresses within one page are always contiguous, so
278+
* there is no need to waste CMA pages for that kind; it also helps reduce
279+
* fragmentations.
238280
*/
239281
struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
240282
{
283+
#ifdef CONFIG_DMA_PERNUMA_CMA
284+
int nid = dev_to_node(dev);
285+
#endif
286+
241287
/* CMA can be used only in the context which permits sleeping */
242288
if (!gfpflags_allow_blocking(gfp))
243289
return NULL;
244290
if (dev->cma_area)
245291
return cma_alloc_aligned(dev->cma_area, size, gfp);
246-
if (size <= PAGE_SIZE || !dma_contiguous_default_area)
292+
if (size <= PAGE_SIZE)
293+
return NULL;
294+
295+
#ifdef CONFIG_DMA_PERNUMA_CMA
296+
if (nid != NUMA_NO_NODE && !(gfp & (GFP_DMA | GFP_DMA32))) {
297+
struct cma *cma = dma_contiguous_pernuma_area[nid];
298+
struct page *page;
299+
300+
if (cma) {
301+
page = cma_alloc_aligned(cma, size, gfp);
302+
if (page)
303+
return page;
304+
}
305+
}
306+
#endif
307+
if (!dma_contiguous_default_area)
247308
return NULL;
309+
248310
return cma_alloc_aligned(dma_contiguous_default_area, size, gfp);
249311
}
250312

@@ -261,9 +323,27 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
261323
*/
262324
void dma_free_contiguous(struct device *dev, struct page *page, size_t size)
263325
{
264-
if (!cma_release(dev_get_cma_area(dev), page,
265-
PAGE_ALIGN(size) >> PAGE_SHIFT))
266-
__free_pages(page, get_order(size));
326+
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
327+
328+
/* if dev has its own cma, free page from there */
329+
if (dev->cma_area) {
330+
if (cma_release(dev->cma_area, page, count))
331+
return;
332+
} else {
333+
/*
334+
* otherwise, page is from either per-numa cma or default cma
335+
*/
336+
#ifdef CONFIG_DMA_PERNUMA_CMA
337+
if (cma_release(dma_contiguous_pernuma_area[page_to_nid(page)],
338+
page, count))
339+
return;
340+
#endif
341+
if (cma_release(dma_contiguous_default_area, page, count))
342+
return;
343+
}
344+
345+
/* not in any cma, free from buddy */
346+
__free_pages(page, get_order(size));
267347
}
268348

269349
/*

‎mm/Kconfig‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,14 @@ config CMA_DEBUGFS
516516
config CMA_AREAS
517517
int "Maximum count of the CMA areas"
518518
depends on CMA
519+
default 19 if NUMA
519520
default 7
520521
help
521522
CMA allows to create CMA areas for particular purpose, mainly,
522523
used as device private area. This parameter sets the maximum
523524
number of CMA area in the system.
524525

525-
If unsure, leave the default value "7".
526+
If unsure, leave the default value "7" in UMA and "19" in NUMA.
526527

527528
config MEM_SOFT_DIRTY
528529
bool "Track memory changes"

0 commit comments

Comments
 (0)