Skip to content

Commit 9d4f645

Browse files
author
Christoph Hellwig
committed
dma-debug: store a phys_addr_t in struct dma_debug_entry
dma-debug goes to great length to split incoming physical addresses into a PFN and offset to store them in struct dma_debug_entry, just to recombine those for all meaningful uses. Just store a phys_addr_t instead. Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 7543c3e commit 9d4f645

File tree

1 file changed

+28
-51
lines changed

1 file changed

+28
-51
lines changed

kernel/dma/debug.c

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ enum map_err_types {
5959
* @direction: enum dma_data_direction
6060
* @sg_call_ents: 'nents' from dma_map_sg
6161
* @sg_mapped_ents: 'mapped_ents' from dma_map_sg
62-
* @pfn: page frame of the start address
63-
* @offset: offset of mapping relative to pfn
62+
* @paddr: physical start address of the mapping
6463
* @map_err_type: track whether dma_mapping_error() was checked
6564
* @stack_len: number of backtrace entries in @stack_entries
6665
* @stack_entries: stack of backtrace history
@@ -74,8 +73,7 @@ struct dma_debug_entry {
7473
int direction;
7574
int sg_call_ents;
7675
int sg_mapped_ents;
77-
unsigned long pfn;
78-
size_t offset;
76+
phys_addr_t paddr;
7977
enum map_err_types map_err_type;
8078
#ifdef CONFIG_STACKTRACE
8179
unsigned int stack_len;
@@ -389,14 +387,6 @@ static void hash_bucket_del(struct dma_debug_entry *entry)
389387
list_del(&entry->list);
390388
}
391389

392-
static unsigned long long phys_addr(struct dma_debug_entry *entry)
393-
{
394-
if (entry->type == dma_debug_resource)
395-
return __pfn_to_phys(entry->pfn) + entry->offset;
396-
397-
return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
398-
}
399-
400390
/*
401391
* For each mapping (initial cacheline in the case of
402392
* dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
@@ -428,8 +418,8 @@ static DEFINE_SPINLOCK(radix_lock);
428418

429419
static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
430420
{
431-
return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
432-
(entry->offset >> L1_CACHE_SHIFT);
421+
return ((entry->paddr >> PAGE_SHIFT) << CACHELINE_PER_PAGE_SHIFT) +
422+
(offset_in_page(entry->paddr) >> L1_CACHE_SHIFT);
433423
}
434424

435425
static int active_cacheline_read_overlap(phys_addr_t cln)
@@ -538,11 +528,11 @@ void debug_dma_dump_mappings(struct device *dev)
538528
if (!dev || dev == entry->dev) {
539529
cln = to_cacheline_number(entry);
540530
dev_info(entry->dev,
541-
"%s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
531+
"%s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
542532
type2name[entry->type], idx,
543-
phys_addr(entry), entry->pfn,
544-
entry->dev_addr, entry->size,
545-
&cln, dir2name[entry->direction],
533+
&entry->paddr, entry->dev_addr,
534+
entry->size, &cln,
535+
dir2name[entry->direction],
546536
maperr2str[entry->map_err_type]);
547537
}
548538
}
@@ -569,13 +559,13 @@ static int dump_show(struct seq_file *seq, void *v)
569559
list_for_each_entry(entry, &bucket->list, list) {
570560
cln = to_cacheline_number(entry);
571561
seq_printf(seq,
572-
"%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
562+
"%s %s %s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
573563
dev_driver_string(entry->dev),
574564
dev_name(entry->dev),
575565
type2name[entry->type], idx,
576-
phys_addr(entry), entry->pfn,
577-
entry->dev_addr, entry->size,
578-
&cln, dir2name[entry->direction],
566+
&entry->paddr, entry->dev_addr,
567+
entry->size, &cln,
568+
dir2name[entry->direction],
579569
maperr2str[entry->map_err_type]);
580570
}
581571
spin_unlock_irqrestore(&bucket->lock, flags);
@@ -1003,16 +993,16 @@ static void check_unmap(struct dma_debug_entry *ref)
1003993
"[mapped as %s] [unmapped as %s]\n",
1004994
ref->dev_addr, ref->size,
1005995
type2name[entry->type], type2name[ref->type]);
1006-
} else if ((entry->type == dma_debug_coherent) &&
1007-
(phys_addr(ref) != phys_addr(entry))) {
996+
} else if (entry->type == dma_debug_coherent &&
997+
ref->paddr != entry->paddr) {
1008998
err_printk(ref->dev, entry, "device driver frees "
1009999
"DMA memory with different CPU address "
10101000
"[device address=0x%016llx] [size=%llu bytes] "
1011-
"[cpu alloc address=0x%016llx] "
1012-
"[cpu free address=0x%016llx]",
1001+
"[cpu alloc address=0x%pa] "
1002+
"[cpu free address=0x%pa]",
10131003
ref->dev_addr, ref->size,
1014-
phys_addr(entry),
1015-
phys_addr(ref));
1004+
&entry->paddr,
1005+
&ref->paddr);
10161006
}
10171007

10181008
if (ref->sg_call_ents && ref->type == dma_debug_sg &&
@@ -1231,8 +1221,7 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
12311221

12321222
entry->dev = dev;
12331223
entry->type = dma_debug_single;
1234-
entry->pfn = page_to_pfn(page);
1235-
entry->offset = offset;
1224+
entry->paddr = page_to_phys(page);
12361225
entry->dev_addr = dma_addr;
12371226
entry->size = size;
12381227
entry->direction = direction;
@@ -1327,8 +1316,7 @@ void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
13271316

13281317
entry->type = dma_debug_sg;
13291318
entry->dev = dev;
1330-
entry->pfn = page_to_pfn(sg_page(s));
1331-
entry->offset = s->offset;
1319+
entry->paddr = sg_phys(s);
13321320
entry->size = sg_dma_len(s);
13331321
entry->dev_addr = sg_dma_address(s);
13341322
entry->direction = direction;
@@ -1374,8 +1362,7 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
13741362
struct dma_debug_entry ref = {
13751363
.type = dma_debug_sg,
13761364
.dev = dev,
1377-
.pfn = page_to_pfn(sg_page(s)),
1378-
.offset = s->offset,
1365+
.paddr = sg_phys(s),
13791366
.dev_addr = sg_dma_address(s),
13801367
.size = sg_dma_len(s),
13811368
.direction = dir,
@@ -1414,16 +1401,12 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
14141401

14151402
entry->type = dma_debug_coherent;
14161403
entry->dev = dev;
1417-
entry->offset = offset_in_page(virt);
1404+
entry->paddr = page_to_phys((is_vmalloc_addr(virt) ?
1405+
vmalloc_to_page(virt) : virt_to_page(virt)));
14181406
entry->size = size;
14191407
entry->dev_addr = dma_addr;
14201408
entry->direction = DMA_BIDIRECTIONAL;
14211409

1422-
if (is_vmalloc_addr(virt))
1423-
entry->pfn = vmalloc_to_pfn(virt);
1424-
else
1425-
entry->pfn = page_to_pfn(virt_to_page(virt));
1426-
14271410
add_dma_entry(entry, attrs);
14281411
}
14291412

@@ -1433,7 +1416,6 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
14331416
struct dma_debug_entry ref = {
14341417
.type = dma_debug_coherent,
14351418
.dev = dev,
1436-
.offset = offset_in_page(virt),
14371419
.dev_addr = dma_addr,
14381420
.size = size,
14391421
.direction = DMA_BIDIRECTIONAL,
@@ -1443,10 +1425,8 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
14431425
if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
14441426
return;
14451427

1446-
if (is_vmalloc_addr(virt))
1447-
ref.pfn = vmalloc_to_pfn(virt);
1448-
else
1449-
ref.pfn = page_to_pfn(virt_to_page(virt));
1428+
ref.paddr = page_to_phys((is_vmalloc_addr(virt) ?
1429+
vmalloc_to_page(virt) : virt_to_page(virt)));
14501430

14511431
if (unlikely(dma_debug_disabled()))
14521432
return;
@@ -1469,8 +1449,7 @@ void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
14691449

14701450
entry->type = dma_debug_resource;
14711451
entry->dev = dev;
1472-
entry->pfn = PHYS_PFN(addr);
1473-
entry->offset = offset_in_page(addr);
1452+
entry->paddr = addr;
14741453
entry->size = size;
14751454
entry->dev_addr = dma_addr;
14761455
entry->direction = direction;
@@ -1547,8 +1526,7 @@ void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
15471526
struct dma_debug_entry ref = {
15481527
.type = dma_debug_sg,
15491528
.dev = dev,
1550-
.pfn = page_to_pfn(sg_page(s)),
1551-
.offset = s->offset,
1529+
.paddr = sg_phys(s),
15521530
.dev_addr = sg_dma_address(s),
15531531
.size = sg_dma_len(s),
15541532
.direction = direction,
@@ -1579,8 +1557,7 @@ void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
15791557
struct dma_debug_entry ref = {
15801558
.type = dma_debug_sg,
15811559
.dev = dev,
1582-
.pfn = page_to_pfn(sg_page(s)),
1583-
.offset = s->offset,
1560+
.paddr = sg_phys(sg),
15841561
.dev_addr = sg_dma_address(s),
15851562
.size = sg_dma_len(s),
15861563
.direction = direction,

0 commit comments

Comments
 (0)