Skip to content

Commit b28e631

Browse files
committed
Merge tag 'dma-mapping-6.4-2023-04-28' of git://git.infradead.org/users/hch/dma-mapping
Pull dma-mapping updates from Christoph Hellwig: - fix a PageHighMem check in dma-coherent initialization (Doug Berger) - clean up the coherency defaul initialiation (Jiaxun Yang) - add cacheline to user/kernel dma-debug space dump messages (Desnes Nunes, Geert Uytterhoeve) - swiotlb statistics improvements (Michael Kelley) - misc cleanups (Petr Tesarik) * tag 'dma-mapping-6.4-2023-04-28' of git://git.infradead.org/users/hch/dma-mapping: swiotlb: Omit total_used and used_hiwater if !CONFIG_DEBUG_FS swiotlb: track and report io_tlb_used high water marks in debugfs swiotlb: fix debugfs reporting of reserved memory pools swiotlb: relocate PageHighMem test away from rmem_swiotlb_setup of: address: always use dma_default_coherent for default coherency dma-mapping: provide CONFIG_ARCH_DMA_DEFAULT_COHERENT dma-mapping: provide a fallback dma_default_coherent dma-debug: Use %pa to format phys_addr_t dma-debug: add cacheline to user/kernel space dump messages dma-debug: small dma_debug_entry's comment and variable name updates dma-direct: cleanup parameters to dma_direct_optimal_gfp_mask
2 parents 7d8d201 + ec274af commit b28e631

11 files changed

Lines changed: 189 additions & 88 deletions

File tree

arch/powerpc/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ config PPC
124124
#
125125
select ARCH_32BIT_OFF_T if PPC32
126126
select ARCH_DISABLE_KASAN_INLINE if PPC_RADIX_MMU
127+
select ARCH_DMA_DEFAULT_COHERENT if !NOT_COHERENT_CACHE
127128
select ARCH_ENABLE_MEMORY_HOTPLUG
128129
select ARCH_ENABLE_MEMORY_HOTREMOVE
129130
select ARCH_HAS_COPY_MC if PPC64
@@ -287,7 +288,6 @@ config PPC
287288
select NEED_PER_CPU_PAGE_FIRST_CHUNK if PPC64
288289
select NEED_SG_DMA_LENGTH
289290
select OF
290-
select OF_DMA_DEFAULT_COHERENT if !NOT_COHERENT_CACHE
291291
select OF_EARLY_FLATTREE
292292
select OLD_SIGACTION if PPC32
293293
select OLD_SIGSUSPEND

arch/riscv/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ config 32BIT
1212

1313
config RISCV
1414
def_bool y
15+
select ARCH_DMA_DEFAULT_COHERENT
1516
select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION
1617
select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2
1718
select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
@@ -127,7 +128,6 @@ config RISCV
127128
select MODULES_USE_ELF_RELA if MODULES
128129
select MODULE_SECTIONS if MODULES
129130
select OF
130-
select OF_DMA_DEFAULT_COHERENT
131131
select OF_EARLY_FLATTREE
132132
select OF_IRQ
133133
select PCI_DOMAINS_GENERIC if PCI

drivers/of/Kconfig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,4 @@ config OF_OVERLAY
102102
config OF_NUMA
103103
bool
104104

105-
config OF_DMA_DEFAULT_COHERENT
106-
# arches should select this if DMA is coherent by default for OF devices
107-
bool
108-
109105
endif # OF

drivers/of/address.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
10371037
bool of_dma_is_coherent(struct device_node *np)
10381038
{
10391039
struct device_node *node;
1040-
bool is_coherent = IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT);
1040+
bool is_coherent = dma_default_coherent;
10411041

10421042
node = of_node_get(np);
10431043

include/linux/dma-map-ops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ static inline bool dev_is_dma_coherent(struct device *dev)
269269
return dev->dma_coherent;
270270
}
271271
#else
272+
#define dma_default_coherent true
273+
272274
static inline bool dev_is_dma_coherent(struct device *dev)
273275
{
274276
return true;

include/linux/swiotlb.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
8787
* @for_alloc: %true if the pool is used for memory allocation
8888
* @nareas: The area number in the pool.
8989
* @area_nslabs: The slot number in the area.
90+
* @total_used: The total number of slots in the pool that are currently used
91+
* across all areas. Used only for calculating used_hiwater in
92+
* debugfs.
93+
* @used_hiwater: The high water mark for total_used. Used only for reporting
94+
* in debugfs.
9095
*/
9196
struct io_tlb_mem {
9297
phys_addr_t start;
@@ -102,6 +107,10 @@ struct io_tlb_mem {
102107
unsigned int area_nslabs;
103108
struct io_tlb_area *areas;
104109
struct io_tlb_slot *slots;
110+
#ifdef CONFIG_DEBUG_FS
111+
atomic_long_t total_used;
112+
atomic_long_t used_hiwater;
113+
#endif
105114
};
106115
extern struct io_tlb_mem io_tlb_default_mem;
107116

kernel/dma/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ config ARCH_HAS_DMA_PREP_COHERENT
7676
config ARCH_HAS_FORCE_DMA_UNENCRYPTED
7777
bool
7878

79+
#
80+
# Select this option if the architecture assumes DMA devices are coherent
81+
# by default.
82+
#
83+
config ARCH_DMA_DEFAULT_COHERENT
84+
bool
85+
7986
config SWIOTLB
8087
bool
8188
select NEED_DMA_MAP_STATE

kernel/dma/debug.c

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum map_err_types {
5353
* struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
5454
* @list: node on pre-allocated free_entries list
5555
* @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
56+
* @dev_addr: dma address
5657
* @size: length of the mapping
5758
* @type: single, page, sg, coherent
5859
* @direction: enum dma_data_direction
@@ -395,37 +396,6 @@ static unsigned long long phys_addr(struct dma_debug_entry *entry)
395396
return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
396397
}
397398

398-
/*
399-
* Dump mapping entries for debugging purposes
400-
*/
401-
void debug_dma_dump_mappings(struct device *dev)
402-
{
403-
int idx;
404-
405-
for (idx = 0; idx < HASH_SIZE; idx++) {
406-
struct hash_bucket *bucket = &dma_entry_hash[idx];
407-
struct dma_debug_entry *entry;
408-
unsigned long flags;
409-
410-
spin_lock_irqsave(&bucket->lock, flags);
411-
412-
list_for_each_entry(entry, &bucket->list, list) {
413-
if (!dev || dev == entry->dev) {
414-
dev_info(entry->dev,
415-
"%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
416-
type2name[entry->type], idx,
417-
phys_addr(entry), entry->pfn,
418-
entry->dev_addr, entry->size,
419-
dir2name[entry->direction],
420-
maperr2str[entry->map_err_type]);
421-
}
422-
}
423-
424-
spin_unlock_irqrestore(&bucket->lock, flags);
425-
cond_resched();
426-
}
427-
}
428-
429399
/*
430400
* For each mapping (initial cacheline in the case of
431401
* dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
@@ -546,6 +516,70 @@ static void active_cacheline_remove(struct dma_debug_entry *entry)
546516
spin_unlock_irqrestore(&radix_lock, flags);
547517
}
548518

519+
/*
520+
* Dump mappings entries on kernel space for debugging purposes
521+
*/
522+
void debug_dma_dump_mappings(struct device *dev)
523+
{
524+
int idx;
525+
phys_addr_t cln;
526+
527+
for (idx = 0; idx < HASH_SIZE; idx++) {
528+
struct hash_bucket *bucket = &dma_entry_hash[idx];
529+
struct dma_debug_entry *entry;
530+
unsigned long flags;
531+
532+
spin_lock_irqsave(&bucket->lock, flags);
533+
list_for_each_entry(entry, &bucket->list, list) {
534+
if (!dev || dev == entry->dev) {
535+
cln = to_cacheline_number(entry);
536+
dev_info(entry->dev,
537+
"%s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
538+
type2name[entry->type], idx,
539+
phys_addr(entry), entry->pfn,
540+
entry->dev_addr, entry->size,
541+
&cln, dir2name[entry->direction],
542+
maperr2str[entry->map_err_type]);
543+
}
544+
}
545+
spin_unlock_irqrestore(&bucket->lock, flags);
546+
547+
cond_resched();
548+
}
549+
}
550+
551+
/*
552+
* Dump mappings entries on user space via debugfs
553+
*/
554+
static int dump_show(struct seq_file *seq, void *v)
555+
{
556+
int idx;
557+
phys_addr_t cln;
558+
559+
for (idx = 0; idx < HASH_SIZE; idx++) {
560+
struct hash_bucket *bucket = &dma_entry_hash[idx];
561+
struct dma_debug_entry *entry;
562+
unsigned long flags;
563+
564+
spin_lock_irqsave(&bucket->lock, flags);
565+
list_for_each_entry(entry, &bucket->list, list) {
566+
cln = to_cacheline_number(entry);
567+
seq_printf(seq,
568+
"%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
569+
dev_driver_string(entry->dev),
570+
dev_name(entry->dev),
571+
type2name[entry->type], idx,
572+
phys_addr(entry), entry->pfn,
573+
entry->dev_addr, entry->size,
574+
&cln, dir2name[entry->direction],
575+
maperr2str[entry->map_err_type]);
576+
}
577+
spin_unlock_irqrestore(&bucket->lock, flags);
578+
}
579+
return 0;
580+
}
581+
DEFINE_SHOW_ATTRIBUTE(dump);
582+
549583
/*
550584
* Wrapper function for adding an entry to the hash.
551585
* This function takes care of locking itself.
@@ -764,33 +798,6 @@ static const struct file_operations filter_fops = {
764798
.llseek = default_llseek,
765799
};
766800

767-
static int dump_show(struct seq_file *seq, void *v)
768-
{
769-
int idx;
770-
771-
for (idx = 0; idx < HASH_SIZE; idx++) {
772-
struct hash_bucket *bucket = &dma_entry_hash[idx];
773-
struct dma_debug_entry *entry;
774-
unsigned long flags;
775-
776-
spin_lock_irqsave(&bucket->lock, flags);
777-
list_for_each_entry(entry, &bucket->list, list) {
778-
seq_printf(seq,
779-
"%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx %s %s\n",
780-
dev_name(entry->dev),
781-
dev_driver_string(entry->dev),
782-
type2name[entry->type], idx,
783-
phys_addr(entry), entry->pfn,
784-
entry->dev_addr, entry->size,
785-
dir2name[entry->direction],
786-
maperr2str[entry->map_err_type]);
787-
}
788-
spin_unlock_irqrestore(&bucket->lock, flags);
789-
}
790-
return 0;
791-
}
792-
DEFINE_SHOW_ATTRIBUTE(dump);
793-
794801
static int __init dma_debug_fs_init(void)
795802
{
796803
struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
@@ -1262,13 +1269,13 @@ void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
12621269
}
12631270
EXPORT_SYMBOL(debug_dma_mapping_error);
12641271

1265-
void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1272+
void debug_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
12661273
size_t size, int direction)
12671274
{
12681275
struct dma_debug_entry ref = {
12691276
.type = dma_debug_single,
12701277
.dev = dev,
1271-
.dev_addr = addr,
1278+
.dev_addr = dma_addr,
12721279
.size = size,
12731280
.direction = direction,
12741281
};
@@ -1403,13 +1410,13 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
14031410
}
14041411

14051412
void debug_dma_free_coherent(struct device *dev, size_t size,
1406-
void *virt, dma_addr_t addr)
1413+
void *virt, dma_addr_t dma_addr)
14071414
{
14081415
struct dma_debug_entry ref = {
14091416
.type = dma_debug_coherent,
14101417
.dev = dev,
14111418
.offset = offset_in_page(virt),
1412-
.dev_addr = addr,
1419+
.dev_addr = dma_addr,
14131420
.size = size,
14141421
.direction = DMA_BIDIRECTIONAL,
14151422
};

kernel/dma/direct.c

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

47-
static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
48-
u64 *phys_limit)
47+
static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 *phys_limit)
4948
{
50-
u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit);
49+
u64 dma_limit = min_not_zero(
50+
dev->coherent_dma_mask,
51+
dev->bus_dma_limit);
5152

5253
/*
5354
* Optimistically try the zone that the physical address mask falls
@@ -126,8 +127,7 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
126127
if (is_swiotlb_for_alloc(dev))
127128
return dma_direct_alloc_swiotlb(dev, size);
128129

129-
gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
130-
&phys_limit);
130+
gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit);
131131
page = dma_alloc_contiguous(dev, size, gfp);
132132
if (page) {
133133
if (!dma_coherent_ok(dev, page_to_phys(page), size) ||
@@ -172,14 +172,13 @@ static void *dma_direct_alloc_from_pool(struct device *dev, size_t size,
172172
dma_addr_t *dma_handle, gfp_t gfp)
173173
{
174174
struct page *page;
175-
u64 phys_mask;
175+
u64 phys_limit;
176176
void *ret;
177177

178178
if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_DMA_COHERENT_POOL)))
179179
return NULL;
180180

181-
gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
182-
&phys_mask);
181+
gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit);
183182
page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok);
184183
if (!page)
185184
return NULL;

kernel/dma/mapping.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
#include "debug.h"
1818
#include "direct.h"
1919

20-
bool dma_default_coherent;
20+
#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
21+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
22+
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
23+
bool dma_default_coherent = IS_ENABLED(CONFIG_ARCH_DMA_DEFAULT_COHERENT);
24+
#endif
2125

2226
/*
2327
* Managed DMA API

0 commit comments

Comments
 (0)