Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/kernel/dma/debug.c
49062 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* Copyright (C) 2008 Advanced Micro Devices, Inc.
4
*
5
* Author: Joerg Roedel <[email protected]>
6
*/
7
8
#define pr_fmt(fmt) "DMA-API: " fmt
9
10
#include <linux/sched/task_stack.h>
11
#include <linux/scatterlist.h>
12
#include <linux/dma-map-ops.h>
13
#include <linux/sched/task.h>
14
#include <linux/stacktrace.h>
15
#include <linux/spinlock.h>
16
#include <linux/vmalloc.h>
17
#include <linux/debugfs.h>
18
#include <linux/uaccess.h>
19
#include <linux/export.h>
20
#include <linux/device.h>
21
#include <linux/types.h>
22
#include <linux/sched.h>
23
#include <linux/ctype.h>
24
#include <linux/list.h>
25
#include <linux/slab.h>
26
#include <linux/swiotlb.h>
27
#include <asm/sections.h>
28
#include "debug.h"
29
30
#define HASH_SIZE 16384ULL
31
#define HASH_FN_SHIFT 13
32
#define HASH_FN_MASK (HASH_SIZE - 1)
33
34
#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
35
/* If the pool runs out, add this many new entries at once */
36
#define DMA_DEBUG_DYNAMIC_ENTRIES (PAGE_SIZE / sizeof(struct dma_debug_entry))
37
38
enum {
39
dma_debug_single,
40
dma_debug_sg,
41
dma_debug_coherent,
42
dma_debug_noncoherent,
43
dma_debug_phy,
44
};
45
46
enum map_err_types {
47
MAP_ERR_CHECK_NOT_APPLICABLE,
48
MAP_ERR_NOT_CHECKED,
49
MAP_ERR_CHECKED,
50
};
51
52
#define DMA_DEBUG_STACKTRACE_ENTRIES 5
53
54
/**
55
* struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
56
* @list: node on pre-allocated free_entries list
57
* @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
58
* @dev_addr: dma address
59
* @size: length of the mapping
60
* @type: single, page, sg, coherent
61
* @direction: enum dma_data_direction
62
* @sg_call_ents: 'nents' from dma_map_sg
63
* @sg_mapped_ents: 'mapped_ents' from dma_map_sg
64
* @paddr: physical start address of the mapping
65
* @map_err_type: track whether dma_mapping_error() was checked
66
* @stack_len: number of backtrace entries in @stack_entries
67
* @stack_entries: stack of backtrace history
68
*/
69
struct dma_debug_entry {
70
struct list_head list;
71
struct device *dev;
72
u64 dev_addr;
73
u64 size;
74
int type;
75
int direction;
76
int sg_call_ents;
77
int sg_mapped_ents;
78
phys_addr_t paddr;
79
enum map_err_types map_err_type;
80
#ifdef CONFIG_STACKTRACE
81
unsigned int stack_len;
82
unsigned long stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
83
#endif
84
} ____cacheline_aligned_in_smp;
85
86
typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
87
88
struct hash_bucket {
89
struct list_head list;
90
spinlock_t lock;
91
};
92
93
/* Hash list to save the allocated dma addresses */
94
static struct hash_bucket dma_entry_hash[HASH_SIZE];
95
/* List of pre-allocated dma_debug_entry's */
96
static LIST_HEAD(free_entries);
97
/* Lock for the list above */
98
static DEFINE_SPINLOCK(free_entries_lock);
99
100
/* Global disable flag - will be set in case of an error */
101
static bool global_disable __read_mostly;
102
103
/* Early initialization disable flag, set at the end of dma_debug_init */
104
static bool dma_debug_initialized __read_mostly;
105
106
static inline bool dma_debug_disabled(void)
107
{
108
return global_disable || !dma_debug_initialized;
109
}
110
111
/* Global error count */
112
static u32 error_count;
113
114
/* Global error show enable*/
115
static u32 show_all_errors __read_mostly;
116
/* Number of errors to show */
117
static u32 show_num_errors = 1;
118
119
static u32 num_free_entries;
120
static u32 min_free_entries;
121
static u32 nr_total_entries;
122
123
/* number of preallocated entries requested by kernel cmdline */
124
static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
125
126
/* per-driver filter related state */
127
128
#define NAME_MAX_LEN 64
129
130
static char current_driver_name[NAME_MAX_LEN] __read_mostly;
131
static struct device_driver *current_driver __read_mostly;
132
133
static DEFINE_RWLOCK(driver_name_lock);
134
135
static const char *const maperr2str[] = {
136
[MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
137
[MAP_ERR_NOT_CHECKED] = "dma map error not checked",
138
[MAP_ERR_CHECKED] = "dma map error checked",
139
};
140
141
static const char *type2name[] = {
142
[dma_debug_single] = "single",
143
[dma_debug_sg] = "scatter-gather",
144
[dma_debug_coherent] = "coherent",
145
[dma_debug_noncoherent] = "noncoherent",
146
[dma_debug_phy] = "phy",
147
};
148
149
static const char *dir2name[] = {
150
[DMA_BIDIRECTIONAL] = "DMA_BIDIRECTIONAL",
151
[DMA_TO_DEVICE] = "DMA_TO_DEVICE",
152
[DMA_FROM_DEVICE] = "DMA_FROM_DEVICE",
153
[DMA_NONE] = "DMA_NONE",
154
};
155
156
/*
157
* The access to some variables in this macro is racy. We can't use atomic_t
158
* here because all these variables are exported to debugfs. Some of them even
159
* writeable. This is also the reason why a lock won't help much. But anyway,
160
* the races are no big deal. Here is why:
161
*
162
* error_count: the addition is racy, but the worst thing that can happen is
163
* that we don't count some errors
164
* show_num_errors: the subtraction is racy. Also no big deal because in
165
* worst case this will result in one warning more in the
166
* system log than the user configured. This variable is
167
* writeable via debugfs.
168
*/
169
static inline void dump_entry_trace(struct dma_debug_entry *entry)
170
{
171
#ifdef CONFIG_STACKTRACE
172
if (entry) {
173
pr_warn("Mapped at:\n");
174
stack_trace_print(entry->stack_entries, entry->stack_len, 0);
175
}
176
#endif
177
}
178
179
static bool driver_filter(struct device *dev)
180
{
181
struct device_driver *drv;
182
unsigned long flags;
183
bool ret;
184
185
/* driver filter off */
186
if (likely(!current_driver_name[0]))
187
return true;
188
189
/* driver filter on and initialized */
190
if (current_driver && dev && dev->driver == current_driver)
191
return true;
192
193
/* driver filter on, but we can't filter on a NULL device... */
194
if (!dev)
195
return false;
196
197
if (current_driver || !current_driver_name[0])
198
return false;
199
200
/* driver filter on but not yet initialized */
201
drv = dev->driver;
202
if (!drv)
203
return false;
204
205
/* lock to protect against change of current_driver_name */
206
read_lock_irqsave(&driver_name_lock, flags);
207
208
ret = false;
209
if (drv->name &&
210
strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
211
current_driver = drv;
212
ret = true;
213
}
214
215
read_unlock_irqrestore(&driver_name_lock, flags);
216
217
return ret;
218
}
219
220
#define err_printk(dev, entry, format, arg...) do { \
221
error_count += 1; \
222
if (driver_filter(dev) && \
223
(show_all_errors || show_num_errors > 0)) { \
224
WARN(1, pr_fmt("%s %s: ") format, \
225
dev ? dev_driver_string(dev) : "NULL", \
226
dev ? dev_name(dev) : "NULL", ## arg); \
227
dump_entry_trace(entry); \
228
} \
229
if (!show_all_errors && show_num_errors > 0) \
230
show_num_errors -= 1; \
231
} while (0);
232
233
/*
234
* Hash related functions
235
*
236
* Every DMA-API request is saved into a struct dma_debug_entry. To
237
* have quick access to these structs they are stored into a hash.
238
*/
239
static int hash_fn(struct dma_debug_entry *entry)
240
{
241
/*
242
* Hash function is based on the dma address.
243
* We use bits 20-27 here as the index into the hash
244
*/
245
return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
246
}
247
248
/*
249
* Request exclusive access to a hash bucket for a given dma_debug_entry.
250
*/
251
static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
252
unsigned long *flags)
253
__acquires(&dma_entry_hash[idx].lock)
254
{
255
int idx = hash_fn(entry);
256
unsigned long __flags;
257
258
spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
259
*flags = __flags;
260
return &dma_entry_hash[idx];
261
}
262
263
/*
264
* Give up exclusive access to the hash bucket
265
*/
266
static void put_hash_bucket(struct hash_bucket *bucket,
267
unsigned long flags)
268
__releases(&bucket->lock)
269
{
270
spin_unlock_irqrestore(&bucket->lock, flags);
271
}
272
273
static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
274
{
275
return ((a->dev_addr == b->dev_addr) &&
276
(a->dev == b->dev)) ? true : false;
277
}
278
279
static bool containing_match(struct dma_debug_entry *a,
280
struct dma_debug_entry *b)
281
{
282
if (a->dev != b->dev)
283
return false;
284
285
if ((b->dev_addr <= a->dev_addr) &&
286
((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
287
return true;
288
289
return false;
290
}
291
292
/*
293
* Search a given entry in the hash bucket list
294
*/
295
static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
296
struct dma_debug_entry *ref,
297
match_fn match)
298
{
299
struct dma_debug_entry *entry, *ret = NULL;
300
int matches = 0, match_lvl, last_lvl = -1;
301
302
list_for_each_entry(entry, &bucket->list, list) {
303
if (!match(ref, entry))
304
continue;
305
306
/*
307
* Some drivers map the same physical address multiple
308
* times. Without a hardware IOMMU this results in the
309
* same device addresses being put into the dma-debug
310
* hash multiple times too. This can result in false
311
* positives being reported. Therefore we implement a
312
* best-fit algorithm here which returns the entry from
313
* the hash which fits best to the reference value
314
* instead of the first-fit.
315
*/
316
matches += 1;
317
match_lvl = 0;
318
entry->size == ref->size ? ++match_lvl : 0;
319
entry->type == ref->type ? ++match_lvl : 0;
320
entry->direction == ref->direction ? ++match_lvl : 0;
321
entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
322
323
if (match_lvl == 4) {
324
/* perfect-fit - return the result */
325
return entry;
326
} else if (match_lvl > last_lvl) {
327
/*
328
* We found an entry that fits better then the
329
* previous one or it is the 1st match.
330
*/
331
last_lvl = match_lvl;
332
ret = entry;
333
}
334
}
335
336
/*
337
* If we have multiple matches but no perfect-fit, just return
338
* NULL.
339
*/
340
ret = (matches == 1) ? ret : NULL;
341
342
return ret;
343
}
344
345
static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
346
struct dma_debug_entry *ref)
347
{
348
return __hash_bucket_find(bucket, ref, exact_match);
349
}
350
351
static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
352
struct dma_debug_entry *ref,
353
unsigned long *flags)
354
{
355
356
struct dma_debug_entry *entry, index = *ref;
357
int limit = min(HASH_SIZE, (index.dev_addr >> HASH_FN_SHIFT) + 1);
358
359
for (int i = 0; i < limit; i++) {
360
entry = __hash_bucket_find(*bucket, ref, containing_match);
361
362
if (entry)
363
return entry;
364
365
/*
366
* Nothing found, go back a hash bucket
367
*/
368
put_hash_bucket(*bucket, *flags);
369
index.dev_addr -= (1 << HASH_FN_SHIFT);
370
*bucket = get_hash_bucket(&index, flags);
371
}
372
373
return NULL;
374
}
375
376
/*
377
* Add an entry to a hash bucket
378
*/
379
static void hash_bucket_add(struct hash_bucket *bucket,
380
struct dma_debug_entry *entry)
381
{
382
list_add_tail(&entry->list, &bucket->list);
383
}
384
385
/*
386
* Remove entry from a hash bucket list
387
*/
388
static void hash_bucket_del(struct dma_debug_entry *entry)
389
{
390
list_del(&entry->list);
391
}
392
393
/*
394
* For each mapping (initial cacheline in the case of
395
* dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
396
* scatterlist, or the cacheline specified in dma_map_single) insert
397
* into this tree using the cacheline as the key. At
398
* dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
399
* the entry already exists at insertion time add a tag as a reference
400
* count for the overlapping mappings. For now, the overlap tracking
401
* just ensures that 'unmaps' balance 'maps' before marking the
402
* cacheline idle, but we should also be flagging overlaps as an API
403
* violation.
404
*
405
* Memory usage is mostly constrained by the maximum number of available
406
* dma-debug entries in that we need a free dma_debug_entry before
407
* inserting into the tree. In the case of dma_map_page and
408
* dma_alloc_coherent there is only one dma_debug_entry and one
409
* dma_active_cacheline entry to track per event. dma_map_sg(), on the
410
* other hand, consumes a single dma_debug_entry, but inserts 'nents'
411
* entries into the tree.
412
*
413
* Use __GFP_NOWARN because the printk from an OOM, to netconsole, could end
414
* up right back in the DMA debugging code, leading to a deadlock.
415
*/
416
static RADIX_TREE(dma_active_cacheline, GFP_ATOMIC | __GFP_NOWARN);
417
static DEFINE_SPINLOCK(radix_lock);
418
#define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
419
#define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
420
#define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
421
422
static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
423
{
424
return ((entry->paddr >> PAGE_SHIFT) << CACHELINE_PER_PAGE_SHIFT) +
425
(offset_in_page(entry->paddr) >> L1_CACHE_SHIFT);
426
}
427
428
static int active_cacheline_read_overlap(phys_addr_t cln)
429
{
430
int overlap = 0, i;
431
432
for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
433
if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
434
overlap |= 1 << i;
435
return overlap;
436
}
437
438
static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
439
{
440
int i;
441
442
if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
443
return overlap;
444
445
for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
446
if (overlap & 1 << i)
447
radix_tree_tag_set(&dma_active_cacheline, cln, i);
448
else
449
radix_tree_tag_clear(&dma_active_cacheline, cln, i);
450
451
return overlap;
452
}
453
454
static void active_cacheline_inc_overlap(phys_addr_t cln)
455
{
456
int overlap = active_cacheline_read_overlap(cln);
457
458
overlap = active_cacheline_set_overlap(cln, ++overlap);
459
460
/* If we overflowed the overlap counter then we're potentially
461
* leaking dma-mappings.
462
*/
463
WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
464
pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
465
ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
466
}
467
468
static int active_cacheline_dec_overlap(phys_addr_t cln)
469
{
470
int overlap = active_cacheline_read_overlap(cln);
471
472
return active_cacheline_set_overlap(cln, --overlap);
473
}
474
475
static int active_cacheline_insert(struct dma_debug_entry *entry)
476
{
477
phys_addr_t cln = to_cacheline_number(entry);
478
unsigned long flags;
479
int rc;
480
481
/* If the device is not writing memory then we don't have any
482
* concerns about the cpu consuming stale data. This mitigates
483
* legitimate usages of overlapping mappings.
484
*/
485
if (entry->direction == DMA_TO_DEVICE)
486
return 0;
487
488
spin_lock_irqsave(&radix_lock, flags);
489
rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
490
if (rc == -EEXIST)
491
active_cacheline_inc_overlap(cln);
492
spin_unlock_irqrestore(&radix_lock, flags);
493
494
return rc;
495
}
496
497
static void active_cacheline_remove(struct dma_debug_entry *entry)
498
{
499
phys_addr_t cln = to_cacheline_number(entry);
500
unsigned long flags;
501
502
/* ...mirror the insert case */
503
if (entry->direction == DMA_TO_DEVICE)
504
return;
505
506
spin_lock_irqsave(&radix_lock, flags);
507
/* since we are counting overlaps the final put of the
508
* cacheline will occur when the overlap count is 0.
509
* active_cacheline_dec_overlap() returns -1 in that case
510
*/
511
if (active_cacheline_dec_overlap(cln) < 0)
512
radix_tree_delete(&dma_active_cacheline, cln);
513
spin_unlock_irqrestore(&radix_lock, flags);
514
}
515
516
/*
517
* Dump mappings entries on kernel space for debugging purposes
518
*/
519
void debug_dma_dump_mappings(struct device *dev)
520
{
521
int idx;
522
phys_addr_t cln;
523
524
for (idx = 0; idx < HASH_SIZE; idx++) {
525
struct hash_bucket *bucket = &dma_entry_hash[idx];
526
struct dma_debug_entry *entry;
527
unsigned long flags;
528
529
spin_lock_irqsave(&bucket->lock, flags);
530
list_for_each_entry(entry, &bucket->list, list) {
531
if (!dev || dev == entry->dev) {
532
cln = to_cacheline_number(entry);
533
dev_info(entry->dev,
534
"%s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
535
type2name[entry->type], idx,
536
&entry->paddr, entry->dev_addr,
537
entry->size, &cln,
538
dir2name[entry->direction],
539
maperr2str[entry->map_err_type]);
540
}
541
}
542
spin_unlock_irqrestore(&bucket->lock, flags);
543
544
cond_resched();
545
}
546
}
547
548
/*
549
* Dump mappings entries on user space via debugfs
550
*/
551
static int dump_show(struct seq_file *seq, void *v)
552
{
553
int idx;
554
phys_addr_t cln;
555
556
for (idx = 0; idx < HASH_SIZE; idx++) {
557
struct hash_bucket *bucket = &dma_entry_hash[idx];
558
struct dma_debug_entry *entry;
559
unsigned long flags;
560
561
spin_lock_irqsave(&bucket->lock, flags);
562
list_for_each_entry(entry, &bucket->list, list) {
563
cln = to_cacheline_number(entry);
564
seq_printf(seq,
565
"%s %s %s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
566
dev_driver_string(entry->dev),
567
dev_name(entry->dev),
568
type2name[entry->type], idx,
569
&entry->paddr, entry->dev_addr,
570
entry->size, &cln,
571
dir2name[entry->direction],
572
maperr2str[entry->map_err_type]);
573
}
574
spin_unlock_irqrestore(&bucket->lock, flags);
575
}
576
return 0;
577
}
578
DEFINE_SHOW_ATTRIBUTE(dump);
579
580
/*
581
* Wrapper function for adding an entry to the hash.
582
* This function takes care of locking itself.
583
*/
584
static void add_dma_entry(struct dma_debug_entry *entry, unsigned long attrs)
585
{
586
struct hash_bucket *bucket;
587
unsigned long flags;
588
int rc;
589
590
bucket = get_hash_bucket(entry, &flags);
591
hash_bucket_add(bucket, entry);
592
put_hash_bucket(bucket, flags);
593
594
rc = active_cacheline_insert(entry);
595
if (rc == -ENOMEM) {
596
pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n");
597
global_disable = true;
598
} else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
599
!(IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) &&
600
is_swiotlb_active(entry->dev))) {
601
err_printk(entry->dev, entry,
602
"cacheline tracking EEXIST, overlapping mappings aren't supported\n");
603
}
604
}
605
606
static int dma_debug_create_entries(gfp_t gfp)
607
{
608
struct dma_debug_entry *entry;
609
int i;
610
611
entry = (void *)get_zeroed_page(gfp);
612
if (!entry)
613
return -ENOMEM;
614
615
for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
616
list_add_tail(&entry[i].list, &free_entries);
617
618
num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
619
nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
620
621
return 0;
622
}
623
624
static struct dma_debug_entry *__dma_entry_alloc(void)
625
{
626
struct dma_debug_entry *entry;
627
628
entry = list_entry(free_entries.next, struct dma_debug_entry, list);
629
list_del(&entry->list);
630
memset(entry, 0, sizeof(*entry));
631
632
num_free_entries -= 1;
633
if (num_free_entries < min_free_entries)
634
min_free_entries = num_free_entries;
635
636
return entry;
637
}
638
639
/*
640
* This should be called outside of free_entries_lock scope to avoid potential
641
* deadlocks with serial consoles that use DMA.
642
*/
643
static void __dma_entry_alloc_check_leak(u32 nr_entries)
644
{
645
u32 tmp = nr_entries % nr_prealloc_entries;
646
647
/* Shout each time we tick over some multiple of the initial pool */
648
if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
649
pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
650
nr_entries,
651
(nr_entries / nr_prealloc_entries));
652
}
653
}
654
655
/* struct dma_entry allocator
656
*
657
* The next two functions implement the allocator for
658
* struct dma_debug_entries.
659
*/
660
static struct dma_debug_entry *dma_entry_alloc(void)
661
{
662
bool alloc_check_leak = false;
663
struct dma_debug_entry *entry;
664
unsigned long flags;
665
u32 nr_entries;
666
667
spin_lock_irqsave(&free_entries_lock, flags);
668
if (num_free_entries == 0) {
669
if (dma_debug_create_entries(GFP_ATOMIC)) {
670
global_disable = true;
671
spin_unlock_irqrestore(&free_entries_lock, flags);
672
pr_err("debugging out of memory - disabling\n");
673
return NULL;
674
}
675
alloc_check_leak = true;
676
nr_entries = nr_total_entries;
677
}
678
679
entry = __dma_entry_alloc();
680
681
spin_unlock_irqrestore(&free_entries_lock, flags);
682
683
if (alloc_check_leak)
684
__dma_entry_alloc_check_leak(nr_entries);
685
686
#ifdef CONFIG_STACKTRACE
687
entry->stack_len = stack_trace_save(entry->stack_entries,
688
ARRAY_SIZE(entry->stack_entries),
689
1);
690
#endif
691
return entry;
692
}
693
694
static void dma_entry_free(struct dma_debug_entry *entry)
695
{
696
unsigned long flags;
697
698
active_cacheline_remove(entry);
699
700
/*
701
* add to beginning of the list - this way the entries are
702
* more likely cache hot when they are reallocated.
703
*/
704
spin_lock_irqsave(&free_entries_lock, flags);
705
list_add(&entry->list, &free_entries);
706
num_free_entries += 1;
707
spin_unlock_irqrestore(&free_entries_lock, flags);
708
}
709
710
/*
711
* DMA-API debugging init code
712
*
713
* The init code does two things:
714
* 1. Initialize core data structures
715
* 2. Preallocate a given number of dma_debug_entry structs
716
*/
717
718
static ssize_t filter_read(struct file *file, char __user *user_buf,
719
size_t count, loff_t *ppos)
720
{
721
char buf[NAME_MAX_LEN + 1];
722
unsigned long flags;
723
int len;
724
725
if (!current_driver_name[0])
726
return 0;
727
728
/*
729
* We can't copy to userspace directly because current_driver_name can
730
* only be read under the driver_name_lock with irqs disabled. So
731
* create a temporary copy first.
732
*/
733
read_lock_irqsave(&driver_name_lock, flags);
734
len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
735
read_unlock_irqrestore(&driver_name_lock, flags);
736
737
return simple_read_from_buffer(user_buf, count, ppos, buf, len);
738
}
739
740
static ssize_t filter_write(struct file *file, const char __user *userbuf,
741
size_t count, loff_t *ppos)
742
{
743
char buf[NAME_MAX_LEN];
744
unsigned long flags;
745
size_t len;
746
int i;
747
748
/*
749
* We can't copy from userspace directly. Access to
750
* current_driver_name is protected with a write_lock with irqs
751
* disabled. Since copy_from_user can fault and may sleep we
752
* need to copy to temporary buffer first
753
*/
754
len = min(count, (size_t)(NAME_MAX_LEN - 1));
755
if (copy_from_user(buf, userbuf, len))
756
return -EFAULT;
757
758
buf[len] = 0;
759
760
write_lock_irqsave(&driver_name_lock, flags);
761
762
/*
763
* Now handle the string we got from userspace very carefully.
764
* The rules are:
765
* - only use the first token we got
766
* - token delimiter is everything looking like a space
767
* character (' ', '\n', '\t' ...)
768
*
769
*/
770
if (!isalnum(buf[0])) {
771
/*
772
* If the first character userspace gave us is not
773
* alphanumerical then assume the filter should be
774
* switched off.
775
*/
776
if (current_driver_name[0])
777
pr_info("switching off dma-debug driver filter\n");
778
current_driver_name[0] = 0;
779
current_driver = NULL;
780
goto out_unlock;
781
}
782
783
/*
784
* Now parse out the first token and use it as the name for the
785
* driver to filter for.
786
*/
787
for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
788
current_driver_name[i] = buf[i];
789
if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
790
break;
791
}
792
current_driver_name[i] = 0;
793
current_driver = NULL;
794
795
pr_info("enable driver filter for driver [%s]\n",
796
current_driver_name);
797
798
out_unlock:
799
write_unlock_irqrestore(&driver_name_lock, flags);
800
801
return count;
802
}
803
804
static const struct file_operations filter_fops = {
805
.read = filter_read,
806
.write = filter_write,
807
.llseek = default_llseek,
808
};
809
810
static int __init dma_debug_fs_init(void)
811
{
812
struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
813
814
debugfs_create_bool("disabled", 0444, dentry, &global_disable);
815
debugfs_create_u32("error_count", 0444, dentry, &error_count);
816
debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
817
debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
818
debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
819
debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
820
debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
821
debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
822
debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
823
824
return 0;
825
}
826
core_initcall_sync(dma_debug_fs_init);
827
828
static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
829
{
830
struct dma_debug_entry *entry;
831
unsigned long flags;
832
int count = 0, i;
833
834
for (i = 0; i < HASH_SIZE; ++i) {
835
spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
836
list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
837
if (entry->dev == dev) {
838
count += 1;
839
*out_entry = entry;
840
}
841
}
842
spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
843
}
844
845
return count;
846
}
847
848
static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
849
{
850
struct device *dev = data;
851
struct dma_debug_entry *entry;
852
int count;
853
854
if (dma_debug_disabled())
855
return 0;
856
857
switch (action) {
858
case BUS_NOTIFY_UNBOUND_DRIVER:
859
count = device_dma_allocations(dev, &entry);
860
if (count == 0)
861
break;
862
err_printk(dev, entry, "device driver has pending "
863
"DMA allocations while released from device "
864
"[count=%d]\n"
865
"One of leaked entries details: "
866
"[device address=0x%016llx] [size=%llu bytes] "
867
"[mapped with %s] [mapped as %s]\n",
868
count, entry->dev_addr, entry->size,
869
dir2name[entry->direction], type2name[entry->type]);
870
break;
871
default:
872
break;
873
}
874
875
return 0;
876
}
877
878
void dma_debug_add_bus(const struct bus_type *bus)
879
{
880
struct notifier_block *nb;
881
882
if (dma_debug_disabled())
883
return;
884
885
nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
886
if (nb == NULL) {
887
pr_err("dma_debug_add_bus: out of memory\n");
888
return;
889
}
890
891
nb->notifier_call = dma_debug_device_change;
892
893
bus_register_notifier(bus, nb);
894
}
895
896
static int dma_debug_init(void)
897
{
898
int i, nr_pages;
899
900
/* Do not use dma_debug_initialized here, since we really want to be
901
* called to set dma_debug_initialized
902
*/
903
if (global_disable)
904
return 0;
905
906
for (i = 0; i < HASH_SIZE; ++i) {
907
INIT_LIST_HEAD(&dma_entry_hash[i].list);
908
spin_lock_init(&dma_entry_hash[i].lock);
909
}
910
911
nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
912
for (i = 0; i < nr_pages; ++i)
913
dma_debug_create_entries(GFP_KERNEL);
914
if (num_free_entries >= nr_prealloc_entries) {
915
pr_info("preallocated %d debug entries\n", nr_total_entries);
916
} else if (num_free_entries > 0) {
917
pr_warn("%d debug entries requested but only %d allocated\n",
918
nr_prealloc_entries, nr_total_entries);
919
} else {
920
pr_err("debugging out of memory error - disabled\n");
921
global_disable = true;
922
923
return 0;
924
}
925
min_free_entries = num_free_entries;
926
927
dma_debug_initialized = true;
928
929
pr_info("debugging enabled by kernel config\n");
930
return 0;
931
}
932
core_initcall(dma_debug_init);
933
934
static __init int dma_debug_cmdline(char *str)
935
{
936
if (!str)
937
return -EINVAL;
938
939
if (strncmp(str, "off", 3) == 0) {
940
pr_info("debugging disabled on kernel command line\n");
941
global_disable = true;
942
}
943
944
return 1;
945
}
946
947
static __init int dma_debug_entries_cmdline(char *str)
948
{
949
if (!str)
950
return -EINVAL;
951
if (!get_option(&str, &nr_prealloc_entries))
952
nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
953
return 1;
954
}
955
956
__setup("dma_debug=", dma_debug_cmdline);
957
__setup("dma_debug_entries=", dma_debug_entries_cmdline);
958
959
static void check_unmap(struct dma_debug_entry *ref)
960
{
961
struct dma_debug_entry *entry;
962
struct hash_bucket *bucket;
963
unsigned long flags;
964
965
bucket = get_hash_bucket(ref, &flags);
966
entry = bucket_find_exact(bucket, ref);
967
968
if (!entry) {
969
/* must drop lock before calling dma_mapping_error */
970
put_hash_bucket(bucket, flags);
971
972
if (dma_mapping_error(ref->dev, ref->dev_addr)) {
973
err_printk(ref->dev, NULL,
974
"device driver tries to free an "
975
"invalid DMA memory address\n");
976
} else {
977
err_printk(ref->dev, NULL,
978
"device driver tries to free DMA "
979
"memory it has not allocated [device "
980
"address=0x%016llx] [size=%llu bytes]\n",
981
ref->dev_addr, ref->size);
982
}
983
return;
984
}
985
986
if (ref->size != entry->size) {
987
err_printk(ref->dev, entry, "device driver frees "
988
"DMA memory with different size "
989
"[device address=0x%016llx] [map size=%llu bytes] "
990
"[unmap size=%llu bytes]\n",
991
ref->dev_addr, entry->size, ref->size);
992
}
993
994
if (ref->type != entry->type) {
995
err_printk(ref->dev, entry, "device driver frees "
996
"DMA memory with wrong function "
997
"[device address=0x%016llx] [size=%llu bytes] "
998
"[mapped as %s] [unmapped as %s]\n",
999
ref->dev_addr, ref->size,
1000
type2name[entry->type], type2name[ref->type]);
1001
} else if ((entry->type == dma_debug_coherent ||
1002
entry->type == dma_debug_noncoherent) &&
1003
ref->paddr != entry->paddr) {
1004
err_printk(ref->dev, entry, "device driver frees "
1005
"DMA memory with different CPU address "
1006
"[device address=0x%016llx] [size=%llu bytes] "
1007
"[cpu alloc address=0x%pa] "
1008
"[cpu free address=0x%pa]",
1009
ref->dev_addr, ref->size,
1010
&entry->paddr,
1011
&ref->paddr);
1012
}
1013
1014
if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1015
ref->sg_call_ents != entry->sg_call_ents) {
1016
err_printk(ref->dev, entry, "device driver frees "
1017
"DMA sg list with different entry count "
1018
"[map count=%d] [unmap count=%d]\n",
1019
entry->sg_call_ents, ref->sg_call_ents);
1020
}
1021
1022
/*
1023
* This may be no bug in reality - but most implementations of the
1024
* DMA API don't handle this properly, so check for it here
1025
*/
1026
if (ref->direction != entry->direction) {
1027
err_printk(ref->dev, entry, "device driver frees "
1028
"DMA memory with different direction "
1029
"[device address=0x%016llx] [size=%llu bytes] "
1030
"[mapped with %s] [unmapped with %s]\n",
1031
ref->dev_addr, ref->size,
1032
dir2name[entry->direction],
1033
dir2name[ref->direction]);
1034
}
1035
1036
/*
1037
* Drivers should use dma_mapping_error() to check the returned
1038
* addresses of dma_map_single() and dma_map_page().
1039
* If not, print this warning message. See Documentation/core-api/dma-api.rst.
1040
*/
1041
if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1042
err_printk(ref->dev, entry,
1043
"device driver failed to check map error"
1044
"[device address=0x%016llx] [size=%llu bytes] "
1045
"[mapped as %s]",
1046
ref->dev_addr, ref->size,
1047
type2name[entry->type]);
1048
}
1049
1050
hash_bucket_del(entry);
1051
put_hash_bucket(bucket, flags);
1052
1053
/*
1054
* Free the entry outside of bucket_lock to avoid ABBA deadlocks
1055
* between that and radix_lock.
1056
*/
1057
dma_entry_free(entry);
1058
}
1059
1060
static void check_for_stack(struct device *dev, phys_addr_t phys)
1061
{
1062
void *addr;
1063
struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1064
1065
if (!stack_vm_area) {
1066
/* Stack is direct-mapped. */
1067
if (PhysHighMem(phys))
1068
return;
1069
addr = phys_to_virt(phys);
1070
if (object_is_on_stack(addr))
1071
err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
1072
} else {
1073
/* Stack is vmalloced. */
1074
int i;
1075
1076
for (i = 0; i < stack_vm_area->nr_pages; i++) {
1077
if (__phys_to_pfn(phys) !=
1078
page_to_pfn(stack_vm_area->pages[i]))
1079
continue;
1080
1081
addr = (u8 *)current->stack + i * PAGE_SIZE +
1082
(phys % PAGE_SIZE);
1083
err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
1084
break;
1085
}
1086
}
1087
}
1088
1089
static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1090
{
1091
if (memory_intersects(_stext, _etext, addr, len) ||
1092
memory_intersects(__start_rodata, __end_rodata, addr, len))
1093
err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1094
}
1095
1096
static void check_sync(struct device *dev,
1097
struct dma_debug_entry *ref,
1098
bool to_cpu)
1099
{
1100
struct dma_debug_entry *entry;
1101
struct hash_bucket *bucket;
1102
unsigned long flags;
1103
1104
bucket = get_hash_bucket(ref, &flags);
1105
1106
entry = bucket_find_contain(&bucket, ref, &flags);
1107
1108
if (!entry) {
1109
err_printk(dev, NULL, "device driver tries "
1110
"to sync DMA memory it has not allocated "
1111
"[device address=0x%016llx] [size=%llu bytes]\n",
1112
(unsigned long long)ref->dev_addr, ref->size);
1113
goto out;
1114
}
1115
1116
if (ref->size > entry->size) {
1117
err_printk(dev, entry, "device driver syncs"
1118
" DMA memory outside allocated range "
1119
"[device address=0x%016llx] "
1120
"[allocation size=%llu bytes] "
1121
"[sync offset+size=%llu]\n",
1122
entry->dev_addr, entry->size,
1123
ref->size);
1124
}
1125
1126
if (entry->direction == DMA_BIDIRECTIONAL)
1127
goto out;
1128
1129
if (ref->direction != entry->direction) {
1130
err_printk(dev, entry, "device driver syncs "
1131
"DMA memory with different direction "
1132
"[device address=0x%016llx] [size=%llu bytes] "
1133
"[mapped with %s] [synced with %s]\n",
1134
(unsigned long long)ref->dev_addr, entry->size,
1135
dir2name[entry->direction],
1136
dir2name[ref->direction]);
1137
}
1138
1139
if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1140
!(ref->direction == DMA_TO_DEVICE))
1141
err_printk(dev, entry, "device driver syncs "
1142
"device read-only DMA memory for cpu "
1143
"[device address=0x%016llx] [size=%llu bytes] "
1144
"[mapped with %s] [synced with %s]\n",
1145
(unsigned long long)ref->dev_addr, entry->size,
1146
dir2name[entry->direction],
1147
dir2name[ref->direction]);
1148
1149
if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1150
!(ref->direction == DMA_FROM_DEVICE))
1151
err_printk(dev, entry, "device driver syncs "
1152
"device write-only DMA memory to device "
1153
"[device address=0x%016llx] [size=%llu bytes] "
1154
"[mapped with %s] [synced with %s]\n",
1155
(unsigned long long)ref->dev_addr, entry->size,
1156
dir2name[entry->direction],
1157
dir2name[ref->direction]);
1158
1159
if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1160
ref->sg_call_ents != entry->sg_call_ents) {
1161
err_printk(ref->dev, entry, "device driver syncs "
1162
"DMA sg list with different entry count "
1163
"[map count=%d] [sync count=%d]\n",
1164
entry->sg_call_ents, ref->sg_call_ents);
1165
}
1166
1167
out:
1168
put_hash_bucket(bucket, flags);
1169
}
1170
1171
static void check_sg_segment(struct device *dev, struct scatterlist *sg)
1172
{
1173
unsigned int max_seg = dma_get_max_seg_size(dev);
1174
u64 start, end, boundary = dma_get_seg_boundary(dev);
1175
1176
/*
1177
* Either the driver forgot to set dma_parms appropriately, or
1178
* whoever generated the list forgot to check them.
1179
*/
1180
if (sg->length > max_seg)
1181
err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1182
sg->length, max_seg);
1183
/*
1184
* In some cases this could potentially be the DMA API
1185
* implementation's fault, but it would usually imply that
1186
* the scatterlist was built inappropriately to begin with.
1187
*/
1188
start = sg_dma_address(sg);
1189
end = start + sg_dma_len(sg) - 1;
1190
if ((start ^ end) & ~boundary)
1191
err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1192
start, end, boundary);
1193
}
1194
1195
void debug_dma_map_single(struct device *dev, const void *addr,
1196
unsigned long len)
1197
{
1198
if (unlikely(dma_debug_disabled()))
1199
return;
1200
1201
if (!virt_addr_valid(addr))
1202
err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1203
addr, len);
1204
1205
if (is_vmalloc_addr(addr))
1206
err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1207
addr, len);
1208
}
1209
EXPORT_SYMBOL(debug_dma_map_single);
1210
1211
void debug_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
1212
int direction, dma_addr_t dma_addr, unsigned long attrs)
1213
{
1214
struct dma_debug_entry *entry;
1215
1216
if (unlikely(dma_debug_disabled()))
1217
return;
1218
1219
if (dma_mapping_error(dev, dma_addr))
1220
return;
1221
1222
entry = dma_entry_alloc();
1223
if (!entry)
1224
return;
1225
1226
entry->dev = dev;
1227
entry->type = dma_debug_phy;
1228
entry->paddr = phys;
1229
entry->dev_addr = dma_addr;
1230
entry->size = size;
1231
entry->direction = direction;
1232
entry->map_err_type = MAP_ERR_NOT_CHECKED;
1233
1234
if (!(attrs & DMA_ATTR_MMIO)) {
1235
check_for_stack(dev, phys);
1236
1237
if (!PhysHighMem(phys))
1238
check_for_illegal_area(dev, phys_to_virt(phys), size);
1239
}
1240
1241
add_dma_entry(entry, attrs);
1242
}
1243
1244
void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1245
{
1246
struct dma_debug_entry ref;
1247
struct dma_debug_entry *entry;
1248
struct hash_bucket *bucket;
1249
unsigned long flags;
1250
1251
if (unlikely(dma_debug_disabled()))
1252
return;
1253
1254
ref.dev = dev;
1255
ref.dev_addr = dma_addr;
1256
bucket = get_hash_bucket(&ref, &flags);
1257
1258
list_for_each_entry(entry, &bucket->list, list) {
1259
if (!exact_match(&ref, entry))
1260
continue;
1261
1262
/*
1263
* The same physical address can be mapped multiple
1264
* times. Without a hardware IOMMU this results in the
1265
* same device addresses being put into the dma-debug
1266
* hash multiple times too. This can result in false
1267
* positives being reported. Therefore we implement a
1268
* best-fit algorithm here which updates the first entry
1269
* from the hash which fits the reference value and is
1270
* not currently listed as being checked.
1271
*/
1272
if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1273
entry->map_err_type = MAP_ERR_CHECKED;
1274
break;
1275
}
1276
}
1277
1278
put_hash_bucket(bucket, flags);
1279
}
1280
EXPORT_SYMBOL(debug_dma_mapping_error);
1281
1282
void debug_dma_unmap_phys(struct device *dev, dma_addr_t dma_addr,
1283
size_t size, int direction)
1284
{
1285
struct dma_debug_entry ref = {
1286
.type = dma_debug_phy,
1287
.dev = dev,
1288
.dev_addr = dma_addr,
1289
.size = size,
1290
.direction = direction,
1291
};
1292
1293
if (unlikely(dma_debug_disabled()))
1294
return;
1295
check_unmap(&ref);
1296
}
1297
1298
void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1299
int nents, int mapped_ents, int direction,
1300
unsigned long attrs)
1301
{
1302
struct dma_debug_entry *entry;
1303
struct scatterlist *s;
1304
int i;
1305
1306
if (unlikely(dma_debug_disabled()))
1307
return;
1308
1309
for_each_sg(sg, s, nents, i) {
1310
check_for_stack(dev, sg_phys(s));
1311
if (!PageHighMem(sg_page(s)))
1312
check_for_illegal_area(dev, sg_virt(s), s->length);
1313
}
1314
1315
for_each_sg(sg, s, mapped_ents, i) {
1316
entry = dma_entry_alloc();
1317
if (!entry)
1318
return;
1319
1320
entry->type = dma_debug_sg;
1321
entry->dev = dev;
1322
entry->paddr = sg_phys(s);
1323
entry->size = sg_dma_len(s);
1324
entry->dev_addr = sg_dma_address(s);
1325
entry->direction = direction;
1326
entry->sg_call_ents = nents;
1327
entry->sg_mapped_ents = mapped_ents;
1328
1329
check_sg_segment(dev, s);
1330
1331
add_dma_entry(entry, attrs);
1332
}
1333
}
1334
1335
static int get_nr_mapped_entries(struct device *dev,
1336
struct dma_debug_entry *ref)
1337
{
1338
struct dma_debug_entry *entry;
1339
struct hash_bucket *bucket;
1340
unsigned long flags;
1341
int mapped_ents;
1342
1343
bucket = get_hash_bucket(ref, &flags);
1344
entry = bucket_find_exact(bucket, ref);
1345
mapped_ents = 0;
1346
1347
if (entry)
1348
mapped_ents = entry->sg_mapped_ents;
1349
put_hash_bucket(bucket, flags);
1350
1351
return mapped_ents;
1352
}
1353
1354
void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1355
int nelems, int dir)
1356
{
1357
struct scatterlist *s;
1358
int mapped_ents = 0, i;
1359
1360
if (unlikely(dma_debug_disabled()))
1361
return;
1362
1363
for_each_sg(sglist, s, nelems, i) {
1364
1365
struct dma_debug_entry ref = {
1366
.type = dma_debug_sg,
1367
.dev = dev,
1368
.paddr = sg_phys(s),
1369
.dev_addr = sg_dma_address(s),
1370
.size = sg_dma_len(s),
1371
.direction = dir,
1372
.sg_call_ents = nelems,
1373
};
1374
1375
if (mapped_ents && i >= mapped_ents)
1376
break;
1377
1378
if (!i)
1379
mapped_ents = get_nr_mapped_entries(dev, &ref);
1380
1381
check_unmap(&ref);
1382
}
1383
}
1384
1385
static phys_addr_t virt_to_paddr(void *virt)
1386
{
1387
struct page *page;
1388
1389
if (is_vmalloc_addr(virt))
1390
page = vmalloc_to_page(virt);
1391
else
1392
page = virt_to_page(virt);
1393
1394
return page_to_phys(page) + offset_in_page(virt);
1395
}
1396
1397
void debug_dma_alloc_coherent(struct device *dev, size_t size,
1398
dma_addr_t dma_addr, void *virt,
1399
unsigned long attrs)
1400
{
1401
struct dma_debug_entry *entry;
1402
1403
if (unlikely(dma_debug_disabled()))
1404
return;
1405
1406
if (unlikely(virt == NULL))
1407
return;
1408
1409
/* handle vmalloc and linear addresses */
1410
if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1411
return;
1412
1413
entry = dma_entry_alloc();
1414
if (!entry)
1415
return;
1416
1417
entry->type = dma_debug_coherent;
1418
entry->dev = dev;
1419
entry->paddr = virt_to_paddr(virt);
1420
entry->size = size;
1421
entry->dev_addr = dma_addr;
1422
entry->direction = DMA_BIDIRECTIONAL;
1423
1424
add_dma_entry(entry, attrs);
1425
}
1426
1427
void debug_dma_free_coherent(struct device *dev, size_t size,
1428
void *virt, dma_addr_t dma_addr)
1429
{
1430
struct dma_debug_entry ref = {
1431
.type = dma_debug_coherent,
1432
.dev = dev,
1433
.dev_addr = dma_addr,
1434
.size = size,
1435
.direction = DMA_BIDIRECTIONAL,
1436
};
1437
1438
/* handle vmalloc and linear addresses */
1439
if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1440
return;
1441
1442
ref.paddr = virt_to_paddr(virt);
1443
1444
if (unlikely(dma_debug_disabled()))
1445
return;
1446
1447
check_unmap(&ref);
1448
}
1449
1450
void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1451
size_t size, int direction)
1452
{
1453
struct dma_debug_entry ref;
1454
1455
if (unlikely(dma_debug_disabled()))
1456
return;
1457
1458
ref.type = dma_debug_single;
1459
ref.dev = dev;
1460
ref.dev_addr = dma_handle;
1461
ref.size = size;
1462
ref.direction = direction;
1463
ref.sg_call_ents = 0;
1464
1465
check_sync(dev, &ref, true);
1466
}
1467
1468
void debug_dma_sync_single_for_device(struct device *dev,
1469
dma_addr_t dma_handle, size_t size,
1470
int direction)
1471
{
1472
struct dma_debug_entry ref;
1473
1474
if (unlikely(dma_debug_disabled()))
1475
return;
1476
1477
ref.type = dma_debug_single;
1478
ref.dev = dev;
1479
ref.dev_addr = dma_handle;
1480
ref.size = size;
1481
ref.direction = direction;
1482
ref.sg_call_ents = 0;
1483
1484
check_sync(dev, &ref, false);
1485
}
1486
1487
void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1488
int nelems, int direction)
1489
{
1490
struct scatterlist *s;
1491
int mapped_ents = 0, i;
1492
1493
if (unlikely(dma_debug_disabled()))
1494
return;
1495
1496
for_each_sg(sg, s, nelems, i) {
1497
1498
struct dma_debug_entry ref = {
1499
.type = dma_debug_sg,
1500
.dev = dev,
1501
.paddr = sg_phys(s),
1502
.dev_addr = sg_dma_address(s),
1503
.size = sg_dma_len(s),
1504
.direction = direction,
1505
.sg_call_ents = nelems,
1506
};
1507
1508
if (!i)
1509
mapped_ents = get_nr_mapped_entries(dev, &ref);
1510
1511
if (i >= mapped_ents)
1512
break;
1513
1514
check_sync(dev, &ref, true);
1515
}
1516
}
1517
1518
void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1519
int nelems, int direction)
1520
{
1521
struct scatterlist *s;
1522
int mapped_ents = 0, i;
1523
1524
if (unlikely(dma_debug_disabled()))
1525
return;
1526
1527
for_each_sg(sg, s, nelems, i) {
1528
1529
struct dma_debug_entry ref = {
1530
.type = dma_debug_sg,
1531
.dev = dev,
1532
.paddr = sg_phys(sg),
1533
.dev_addr = sg_dma_address(s),
1534
.size = sg_dma_len(s),
1535
.direction = direction,
1536
.sg_call_ents = nelems,
1537
};
1538
if (!i)
1539
mapped_ents = get_nr_mapped_entries(dev, &ref);
1540
1541
if (i >= mapped_ents)
1542
break;
1543
1544
check_sync(dev, &ref, false);
1545
}
1546
}
1547
1548
void debug_dma_alloc_pages(struct device *dev, struct page *page,
1549
size_t size, int direction,
1550
dma_addr_t dma_addr,
1551
unsigned long attrs)
1552
{
1553
struct dma_debug_entry *entry;
1554
1555
if (unlikely(dma_debug_disabled()))
1556
return;
1557
1558
entry = dma_entry_alloc();
1559
if (!entry)
1560
return;
1561
1562
entry->type = dma_debug_noncoherent;
1563
entry->dev = dev;
1564
entry->paddr = page_to_phys(page);
1565
entry->size = size;
1566
entry->dev_addr = dma_addr;
1567
entry->direction = direction;
1568
1569
add_dma_entry(entry, attrs);
1570
}
1571
1572
void debug_dma_free_pages(struct device *dev, struct page *page,
1573
size_t size, int direction,
1574
dma_addr_t dma_addr)
1575
{
1576
struct dma_debug_entry ref = {
1577
.type = dma_debug_noncoherent,
1578
.dev = dev,
1579
.paddr = page_to_phys(page),
1580
.dev_addr = dma_addr,
1581
.size = size,
1582
.direction = direction,
1583
};
1584
1585
if (unlikely(dma_debug_disabled()))
1586
return;
1587
1588
check_unmap(&ref);
1589
}
1590
1591
static int __init dma_debug_driver_setup(char *str)
1592
{
1593
int i;
1594
1595
for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1596
current_driver_name[i] = *str;
1597
if (*str == 0)
1598
break;
1599
}
1600
1601
if (current_driver_name[0])
1602
pr_info("enable driver filter for driver [%s]\n",
1603
current_driver_name);
1604
1605
1606
return 1;
1607
}
1608
__setup("dma_debug_driver=", dma_debug_driver_setup);
1609
1610