1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Author: Joerg Roedel <joerg.roedel@amd.com>
8 #define pr_fmt(fmt) "DMA-API: " fmt
10 #include <linux/sched/task_stack.h>
11 #include <linux/scatterlist.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/sched/task.h>
14 #include <linux/stacktrace.h>
15 #include <linux/dma-debug.h>
16 #include <linux/spinlock.h>
17 #include <linux/vmalloc.h>
18 #include <linux/debugfs.h>
19 #include <linux/uaccess.h>
20 #include <linux/export.h>
21 #include <linux/device.h>
22 #include <linux/types.h>
23 #include <linux/sched.h>
24 #include <linux/ctype.h>
25 #include <linux/list.h>
26 #include <linux/slab.h>
28 #include <asm/sections.h>
30 #define HASH_SIZE 16384ULL
31 #define HASH_FN_SHIFT 13
32 #define HASH_FN_MASK (HASH_SIZE - 1)
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))
46 MAP_ERR_CHECK_NOT_APPLICABLE
,
51 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
54 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
55 * @list: node on pre-allocated free_entries list
56 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
57 * @size: length of the mapping
58 * @type: single, page, sg, coherent
59 * @direction: enum dma_data_direction
60 * @sg_call_ents: 'nents' from dma_map_sg
61 * @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
64 * @map_err_type: track whether dma_mapping_error() was checked
65 * @stacktrace: support backtraces when a violation is detected
67 struct dma_debug_entry
{
68 struct list_head list
;
78 enum map_err_types map_err_type
;
79 #ifdef CONFIG_STACKTRACE
80 unsigned int stack_len
;
81 unsigned long stack_entries
[DMA_DEBUG_STACKTRACE_ENTRIES
];
83 } ____cacheline_aligned_in_smp
;
85 typedef bool (*match_fn
)(struct dma_debug_entry
*, struct dma_debug_entry
*);
88 struct list_head list
;
92 /* Hash list to save the allocated dma addresses */
93 static struct hash_bucket dma_entry_hash
[HASH_SIZE
];
94 /* List of pre-allocated dma_debug_entry's */
95 static LIST_HEAD(free_entries
);
96 /* Lock for the list above */
97 static DEFINE_SPINLOCK(free_entries_lock
);
99 /* Global disable flag - will be set in case of an error */
100 static bool global_disable __read_mostly
;
102 /* Early initialization disable flag, set at the end of dma_debug_init */
103 static bool dma_debug_initialized __read_mostly
;
105 static inline bool dma_debug_disabled(void)
107 return global_disable
|| !dma_debug_initialized
;
110 /* Global error count */
111 static u32 error_count
;
113 /* Global error show enable*/
114 static u32 show_all_errors __read_mostly
;
115 /* Number of errors to show */
116 static u32 show_num_errors
= 1;
118 static u32 num_free_entries
;
119 static u32 min_free_entries
;
120 static u32 nr_total_entries
;
122 /* number of preallocated entries requested by kernel cmdline */
123 static u32 nr_prealloc_entries
= PREALLOC_DMA_DEBUG_ENTRIES
;
125 /* per-driver filter related state */
127 #define NAME_MAX_LEN 64
129 static char current_driver_name
[NAME_MAX_LEN
] __read_mostly
;
130 static struct device_driver
*current_driver __read_mostly
;
132 static DEFINE_RWLOCK(driver_name_lock
);
134 static const char *const maperr2str
[] = {
135 [MAP_ERR_CHECK_NOT_APPLICABLE
] = "dma map error check not applicable",
136 [MAP_ERR_NOT_CHECKED
] = "dma map error not checked",
137 [MAP_ERR_CHECKED
] = "dma map error checked",
140 static const char *type2name
[] = {
141 [dma_debug_single
] = "single",
142 [dma_debug_sg
] = "scather-gather",
143 [dma_debug_coherent
] = "coherent",
144 [dma_debug_resource
] = "resource",
147 static const char *dir2name
[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
148 "DMA_FROM_DEVICE", "DMA_NONE" };
151 * The access to some variables in this macro is racy. We can't use atomic_t
152 * here because all these variables are exported to debugfs. Some of them even
153 * writeable. This is also the reason why a lock won't help much. But anyway,
154 * the races are no big deal. Here is why:
156 * error_count: the addition is racy, but the worst thing that can happen is
157 * that we don't count some errors
158 * show_num_errors: the subtraction is racy. Also no big deal because in
159 * worst case this will result in one warning more in the
160 * system log than the user configured. This variable is
161 * writeable via debugfs.
163 static inline void dump_entry_trace(struct dma_debug_entry
*entry
)
165 #ifdef CONFIG_STACKTRACE
167 pr_warn("Mapped at:\n");
168 stack_trace_print(entry
->stack_entries
, entry
->stack_len
, 0);
173 static bool driver_filter(struct device
*dev
)
175 struct device_driver
*drv
;
179 /* driver filter off */
180 if (likely(!current_driver_name
[0]))
183 /* driver filter on and initialized */
184 if (current_driver
&& dev
&& dev
->driver
== current_driver
)
187 /* driver filter on, but we can't filter on a NULL device... */
191 if (current_driver
|| !current_driver_name
[0])
194 /* driver filter on but not yet initialized */
199 /* lock to protect against change of current_driver_name */
200 read_lock_irqsave(&driver_name_lock
, flags
);
204 strncmp(current_driver_name
, drv
->name
, NAME_MAX_LEN
- 1) == 0) {
205 current_driver
= drv
;
209 read_unlock_irqrestore(&driver_name_lock
, flags
);
214 #define err_printk(dev, entry, format, arg...) do { \
216 if (driver_filter(dev) && \
217 (show_all_errors || show_num_errors > 0)) { \
218 WARN(1, pr_fmt("%s %s: ") format, \
219 dev ? dev_driver_string(dev) : "NULL", \
220 dev ? dev_name(dev) : "NULL", ## arg); \
221 dump_entry_trace(entry); \
223 if (!show_all_errors && show_num_errors > 0) \
224 show_num_errors -= 1; \
228 * Hash related functions
230 * Every DMA-API request is saved into a struct dma_debug_entry. To
231 * have quick access to these structs they are stored into a hash.
233 static int hash_fn(struct dma_debug_entry
*entry
)
236 * Hash function is based on the dma address.
237 * We use bits 20-27 here as the index into the hash
239 return (entry
->dev_addr
>> HASH_FN_SHIFT
) & HASH_FN_MASK
;
243 * Request exclusive access to a hash bucket for a given dma_debug_entry.
245 static struct hash_bucket
*get_hash_bucket(struct dma_debug_entry
*entry
,
246 unsigned long *flags
)
247 __acquires(&dma_entry_hash
[idx
].lock
)
249 int idx
= hash_fn(entry
);
250 unsigned long __flags
;
252 spin_lock_irqsave(&dma_entry_hash
[idx
].lock
, __flags
);
254 return &dma_entry_hash
[idx
];
258 * Give up exclusive access to the hash bucket
260 static void put_hash_bucket(struct hash_bucket
*bucket
,
262 __releases(&bucket
->lock
)
264 spin_unlock_irqrestore(&bucket
->lock
, flags
);
267 static bool exact_match(struct dma_debug_entry
*a
, struct dma_debug_entry
*b
)
269 return ((a
->dev_addr
== b
->dev_addr
) &&
270 (a
->dev
== b
->dev
)) ? true : false;
273 static bool containing_match(struct dma_debug_entry
*a
,
274 struct dma_debug_entry
*b
)
276 if (a
->dev
!= b
->dev
)
279 if ((b
->dev_addr
<= a
->dev_addr
) &&
280 ((b
->dev_addr
+ b
->size
) >= (a
->dev_addr
+ a
->size
)))
287 * Search a given entry in the hash bucket list
289 static struct dma_debug_entry
*__hash_bucket_find(struct hash_bucket
*bucket
,
290 struct dma_debug_entry
*ref
,
293 struct dma_debug_entry
*entry
, *ret
= NULL
;
294 int matches
= 0, match_lvl
, last_lvl
= -1;
296 list_for_each_entry(entry
, &bucket
->list
, list
) {
297 if (!match(ref
, entry
))
301 * Some drivers map the same physical address multiple
302 * times. Without a hardware IOMMU this results in the
303 * same device addresses being put into the dma-debug
304 * hash multiple times too. This can result in false
305 * positives being reported. Therefore we implement a
306 * best-fit algorithm here which returns the entry from
307 * the hash which fits best to the reference value
308 * instead of the first-fit.
312 entry
->size
== ref
->size
? ++match_lvl
: 0;
313 entry
->type
== ref
->type
? ++match_lvl
: 0;
314 entry
->direction
== ref
->direction
? ++match_lvl
: 0;
315 entry
->sg_call_ents
== ref
->sg_call_ents
? ++match_lvl
: 0;
317 if (match_lvl
== 4) {
318 /* perfect-fit - return the result */
320 } else if (match_lvl
> last_lvl
) {
322 * We found an entry that fits better then the
323 * previous one or it is the 1st match.
325 last_lvl
= match_lvl
;
331 * If we have multiple matches but no perfect-fit, just return
334 ret
= (matches
== 1) ? ret
: NULL
;
339 static struct dma_debug_entry
*bucket_find_exact(struct hash_bucket
*bucket
,
340 struct dma_debug_entry
*ref
)
342 return __hash_bucket_find(bucket
, ref
, exact_match
);
345 static struct dma_debug_entry
*bucket_find_contain(struct hash_bucket
**bucket
,
346 struct dma_debug_entry
*ref
,
347 unsigned long *flags
)
350 unsigned int max_range
= dma_get_max_seg_size(ref
->dev
);
351 struct dma_debug_entry
*entry
, index
= *ref
;
352 unsigned int range
= 0;
354 while (range
<= max_range
) {
355 entry
= __hash_bucket_find(*bucket
, ref
, containing_match
);
361 * Nothing found, go back a hash bucket
363 put_hash_bucket(*bucket
, *flags
);
364 range
+= (1 << HASH_FN_SHIFT
);
365 index
.dev_addr
-= (1 << HASH_FN_SHIFT
);
366 *bucket
= get_hash_bucket(&index
, flags
);
373 * Add an entry to a hash bucket
375 static void hash_bucket_add(struct hash_bucket
*bucket
,
376 struct dma_debug_entry
*entry
)
378 list_add_tail(&entry
->list
, &bucket
->list
);
382 * Remove entry from a hash bucket list
384 static void hash_bucket_del(struct dma_debug_entry
*entry
)
386 list_del(&entry
->list
);
389 static unsigned long long phys_addr(struct dma_debug_entry
*entry
)
391 if (entry
->type
== dma_debug_resource
)
392 return __pfn_to_phys(entry
->pfn
) + entry
->offset
;
394 return page_to_phys(pfn_to_page(entry
->pfn
)) + entry
->offset
;
398 * Dump mapping entries for debugging purposes
400 void debug_dma_dump_mappings(struct device
*dev
)
404 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
405 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
406 struct dma_debug_entry
*entry
;
409 spin_lock_irqsave(&bucket
->lock
, flags
);
411 list_for_each_entry(entry
, &bucket
->list
, list
) {
412 if (!dev
|| dev
== entry
->dev
) {
414 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
415 type2name
[entry
->type
], idx
,
416 phys_addr(entry
), entry
->pfn
,
417 entry
->dev_addr
, entry
->size
,
418 dir2name
[entry
->direction
],
419 maperr2str
[entry
->map_err_type
]);
423 spin_unlock_irqrestore(&bucket
->lock
, flags
);
429 * For each mapping (initial cacheline in the case of
430 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
431 * scatterlist, or the cacheline specified in dma_map_single) insert
432 * into this tree using the cacheline as the key. At
433 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
434 * the entry already exists at insertion time add a tag as a reference
435 * count for the overlapping mappings. For now, the overlap tracking
436 * just ensures that 'unmaps' balance 'maps' before marking the
437 * cacheline idle, but we should also be flagging overlaps as an API
440 * Memory usage is mostly constrained by the maximum number of available
441 * dma-debug entries in that we need a free dma_debug_entry before
442 * inserting into the tree. In the case of dma_map_page and
443 * dma_alloc_coherent there is only one dma_debug_entry and one
444 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
445 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
446 * entries into the tree.
448 * At any time debug_dma_assert_idle() can be called to trigger a
449 * warning if any cachelines in the given page are in the active set.
451 static RADIX_TREE(dma_active_cacheline
, GFP_NOWAIT
);
452 static DEFINE_SPINLOCK(radix_lock
);
453 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
454 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
455 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
457 static phys_addr_t
to_cacheline_number(struct dma_debug_entry
*entry
)
459 return (entry
->pfn
<< CACHELINE_PER_PAGE_SHIFT
) +
460 (entry
->offset
>> L1_CACHE_SHIFT
);
463 static int active_cacheline_read_overlap(phys_addr_t cln
)
467 for (i
= RADIX_TREE_MAX_TAGS
- 1; i
>= 0; i
--)
468 if (radix_tree_tag_get(&dma_active_cacheline
, cln
, i
))
473 static int active_cacheline_set_overlap(phys_addr_t cln
, int overlap
)
477 if (overlap
> ACTIVE_CACHELINE_MAX_OVERLAP
|| overlap
< 0)
480 for (i
= RADIX_TREE_MAX_TAGS
- 1; i
>= 0; i
--)
481 if (overlap
& 1 << i
)
482 radix_tree_tag_set(&dma_active_cacheline
, cln
, i
);
484 radix_tree_tag_clear(&dma_active_cacheline
, cln
, i
);
489 static void active_cacheline_inc_overlap(phys_addr_t cln
)
491 int overlap
= active_cacheline_read_overlap(cln
);
493 overlap
= active_cacheline_set_overlap(cln
, ++overlap
);
495 /* If we overflowed the overlap counter then we're potentially
496 * leaking dma-mappings. Otherwise, if maps and unmaps are
497 * balanced then this overflow may cause false negatives in
498 * debug_dma_assert_idle() as the cacheline may be marked idle
501 WARN_ONCE(overlap
> ACTIVE_CACHELINE_MAX_OVERLAP
,
502 pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
503 ACTIVE_CACHELINE_MAX_OVERLAP
, &cln
);
506 static int active_cacheline_dec_overlap(phys_addr_t cln
)
508 int overlap
= active_cacheline_read_overlap(cln
);
510 return active_cacheline_set_overlap(cln
, --overlap
);
513 static int active_cacheline_insert(struct dma_debug_entry
*entry
)
515 phys_addr_t cln
= to_cacheline_number(entry
);
519 /* If the device is not writing memory then we don't have any
520 * concerns about the cpu consuming stale data. This mitigates
521 * legitimate usages of overlapping mappings.
523 if (entry
->direction
== DMA_TO_DEVICE
)
526 spin_lock_irqsave(&radix_lock
, flags
);
527 rc
= radix_tree_insert(&dma_active_cacheline
, cln
, entry
);
529 active_cacheline_inc_overlap(cln
);
530 spin_unlock_irqrestore(&radix_lock
, flags
);
535 static void active_cacheline_remove(struct dma_debug_entry
*entry
)
537 phys_addr_t cln
= to_cacheline_number(entry
);
540 /* ...mirror the insert case */
541 if (entry
->direction
== DMA_TO_DEVICE
)
544 spin_lock_irqsave(&radix_lock
, flags
);
545 /* since we are counting overlaps the final put of the
546 * cacheline will occur when the overlap count is 0.
547 * active_cacheline_dec_overlap() returns -1 in that case
549 if (active_cacheline_dec_overlap(cln
) < 0)
550 radix_tree_delete(&dma_active_cacheline
, cln
);
551 spin_unlock_irqrestore(&radix_lock
, flags
);
555 * debug_dma_assert_idle() - assert that a page is not undergoing dma
556 * @page: page to lookup in the dma_active_cacheline tree
558 * Place a call to this routine in cases where the cpu touching the page
559 * before the dma completes (page is dma_unmapped) will lead to data
562 void debug_dma_assert_idle(struct page
*page
)
564 static struct dma_debug_entry
*ents
[CACHELINES_PER_PAGE
];
565 struct dma_debug_entry
*entry
= NULL
;
566 void **results
= (void **) &ents
;
567 unsigned int nents
, i
;
571 if (dma_debug_disabled())
577 cln
= (phys_addr_t
) page_to_pfn(page
) << CACHELINE_PER_PAGE_SHIFT
;
578 spin_lock_irqsave(&radix_lock
, flags
);
579 nents
= radix_tree_gang_lookup(&dma_active_cacheline
, results
, cln
,
580 CACHELINES_PER_PAGE
);
581 for (i
= 0; i
< nents
; i
++) {
582 phys_addr_t ent_cln
= to_cacheline_number(ents
[i
]);
584 if (ent_cln
== cln
) {
587 } else if (ent_cln
>= cln
+ CACHELINES_PER_PAGE
)
590 spin_unlock_irqrestore(&radix_lock
, flags
);
595 cln
= to_cacheline_number(entry
);
596 err_printk(entry
->dev
, entry
,
597 "cpu touching an active dma mapped cacheline [cln=%pa]\n",
602 * Wrapper function for adding an entry to the hash.
603 * This function takes care of locking itself.
605 static void add_dma_entry(struct dma_debug_entry
*entry
)
607 struct hash_bucket
*bucket
;
611 bucket
= get_hash_bucket(entry
, &flags
);
612 hash_bucket_add(bucket
, entry
);
613 put_hash_bucket(bucket
, flags
);
615 rc
= active_cacheline_insert(entry
);
617 pr_err("cacheline tracking ENOMEM, dma-debug disabled\n");
618 global_disable
= true;
621 /* TODO: report -EEXIST errors here as overlapping mappings are
622 * not supported by the DMA API
626 static int dma_debug_create_entries(gfp_t gfp
)
628 struct dma_debug_entry
*entry
;
631 entry
= (void *)get_zeroed_page(gfp
);
635 for (i
= 0; i
< DMA_DEBUG_DYNAMIC_ENTRIES
; i
++)
636 list_add_tail(&entry
[i
].list
, &free_entries
);
638 num_free_entries
+= DMA_DEBUG_DYNAMIC_ENTRIES
;
639 nr_total_entries
+= DMA_DEBUG_DYNAMIC_ENTRIES
;
644 static struct dma_debug_entry
*__dma_entry_alloc(void)
646 struct dma_debug_entry
*entry
;
648 entry
= list_entry(free_entries
.next
, struct dma_debug_entry
, list
);
649 list_del(&entry
->list
);
650 memset(entry
, 0, sizeof(*entry
));
652 num_free_entries
-= 1;
653 if (num_free_entries
< min_free_entries
)
654 min_free_entries
= num_free_entries
;
659 static void __dma_entry_alloc_check_leak(void)
661 u32 tmp
= nr_total_entries
% nr_prealloc_entries
;
663 /* Shout each time we tick over some multiple of the initial pool */
664 if (tmp
< DMA_DEBUG_DYNAMIC_ENTRIES
) {
665 pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
667 (nr_total_entries
/ nr_prealloc_entries
));
671 /* struct dma_entry allocator
673 * The next two functions implement the allocator for
674 * struct dma_debug_entries.
676 static struct dma_debug_entry
*dma_entry_alloc(void)
678 struct dma_debug_entry
*entry
;
681 spin_lock_irqsave(&free_entries_lock
, flags
);
682 if (num_free_entries
== 0) {
683 if (dma_debug_create_entries(GFP_ATOMIC
)) {
684 global_disable
= true;
685 spin_unlock_irqrestore(&free_entries_lock
, flags
);
686 pr_err("debugging out of memory - disabling\n");
689 __dma_entry_alloc_check_leak();
692 entry
= __dma_entry_alloc();
694 spin_unlock_irqrestore(&free_entries_lock
, flags
);
696 #ifdef CONFIG_STACKTRACE
697 entry
->stack_len
= stack_trace_save(entry
->stack_entries
,
698 ARRAY_SIZE(entry
->stack_entries
),
704 static void dma_entry_free(struct dma_debug_entry
*entry
)
708 active_cacheline_remove(entry
);
711 * add to beginning of the list - this way the entries are
712 * more likely cache hot when they are reallocated.
714 spin_lock_irqsave(&free_entries_lock
, flags
);
715 list_add(&entry
->list
, &free_entries
);
716 num_free_entries
+= 1;
717 spin_unlock_irqrestore(&free_entries_lock
, flags
);
721 * DMA-API debugging init code
723 * The init code does two things:
724 * 1. Initialize core data structures
725 * 2. Preallocate a given number of dma_debug_entry structs
728 static ssize_t
filter_read(struct file
*file
, char __user
*user_buf
,
729 size_t count
, loff_t
*ppos
)
731 char buf
[NAME_MAX_LEN
+ 1];
735 if (!current_driver_name
[0])
739 * We can't copy to userspace directly because current_driver_name can
740 * only be read under the driver_name_lock with irqs disabled. So
741 * create a temporary copy first.
743 read_lock_irqsave(&driver_name_lock
, flags
);
744 len
= scnprintf(buf
, NAME_MAX_LEN
+ 1, "%s\n", current_driver_name
);
745 read_unlock_irqrestore(&driver_name_lock
, flags
);
747 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
750 static ssize_t
filter_write(struct file
*file
, const char __user
*userbuf
,
751 size_t count
, loff_t
*ppos
)
753 char buf
[NAME_MAX_LEN
];
759 * We can't copy from userspace directly. Access to
760 * current_driver_name is protected with a write_lock with irqs
761 * disabled. Since copy_from_user can fault and may sleep we
762 * need to copy to temporary buffer first
764 len
= min(count
, (size_t)(NAME_MAX_LEN
- 1));
765 if (copy_from_user(buf
, userbuf
, len
))
770 write_lock_irqsave(&driver_name_lock
, flags
);
773 * Now handle the string we got from userspace very carefully.
775 * - only use the first token we got
776 * - token delimiter is everything looking like a space
777 * character (' ', '\n', '\t' ...)
780 if (!isalnum(buf
[0])) {
782 * If the first character userspace gave us is not
783 * alphanumerical then assume the filter should be
786 if (current_driver_name
[0])
787 pr_info("switching off dma-debug driver filter\n");
788 current_driver_name
[0] = 0;
789 current_driver
= NULL
;
794 * Now parse out the first token and use it as the name for the
795 * driver to filter for.
797 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
) {
798 current_driver_name
[i
] = buf
[i
];
799 if (isspace(buf
[i
]) || buf
[i
] == ' ' || buf
[i
] == 0)
802 current_driver_name
[i
] = 0;
803 current_driver
= NULL
;
805 pr_info("enable driver filter for driver [%s]\n",
806 current_driver_name
);
809 write_unlock_irqrestore(&driver_name_lock
, flags
);
814 static const struct file_operations filter_fops
= {
816 .write
= filter_write
,
817 .llseek
= default_llseek
,
820 static int dump_show(struct seq_file
*seq
, void *v
)
824 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
825 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
826 struct dma_debug_entry
*entry
;
829 spin_lock_irqsave(&bucket
->lock
, flags
);
830 list_for_each_entry(entry
, &bucket
->list
, list
) {
832 "%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx %s %s\n",
833 dev_name(entry
->dev
),
834 dev_driver_string(entry
->dev
),
835 type2name
[entry
->type
], idx
,
836 phys_addr(entry
), entry
->pfn
,
837 entry
->dev_addr
, entry
->size
,
838 dir2name
[entry
->direction
],
839 maperr2str
[entry
->map_err_type
]);
841 spin_unlock_irqrestore(&bucket
->lock
, flags
);
845 DEFINE_SHOW_ATTRIBUTE(dump
);
847 static void dma_debug_fs_init(void)
849 struct dentry
*dentry
= debugfs_create_dir("dma-api", NULL
);
851 debugfs_create_bool("disabled", 0444, dentry
, &global_disable
);
852 debugfs_create_u32("error_count", 0444, dentry
, &error_count
);
853 debugfs_create_u32("all_errors", 0644, dentry
, &show_all_errors
);
854 debugfs_create_u32("num_errors", 0644, dentry
, &show_num_errors
);
855 debugfs_create_u32("num_free_entries", 0444, dentry
, &num_free_entries
);
856 debugfs_create_u32("min_free_entries", 0444, dentry
, &min_free_entries
);
857 debugfs_create_u32("nr_total_entries", 0444, dentry
, &nr_total_entries
);
858 debugfs_create_file("driver_filter", 0644, dentry
, NULL
, &filter_fops
);
859 debugfs_create_file("dump", 0444, dentry
, NULL
, &dump_fops
);
862 static int device_dma_allocations(struct device
*dev
, struct dma_debug_entry
**out_entry
)
864 struct dma_debug_entry
*entry
;
868 for (i
= 0; i
< HASH_SIZE
; ++i
) {
869 spin_lock_irqsave(&dma_entry_hash
[i
].lock
, flags
);
870 list_for_each_entry(entry
, &dma_entry_hash
[i
].list
, list
) {
871 if (entry
->dev
== dev
) {
876 spin_unlock_irqrestore(&dma_entry_hash
[i
].lock
, flags
);
882 static int dma_debug_device_change(struct notifier_block
*nb
, unsigned long action
, void *data
)
884 struct device
*dev
= data
;
885 struct dma_debug_entry
*uninitialized_var(entry
);
888 if (dma_debug_disabled())
892 case BUS_NOTIFY_UNBOUND_DRIVER
:
893 count
= device_dma_allocations(dev
, &entry
);
896 err_printk(dev
, entry
, "device driver has pending "
897 "DMA allocations while released from device "
899 "One of leaked entries details: "
900 "[device address=0x%016llx] [size=%llu bytes] "
901 "[mapped with %s] [mapped as %s]\n",
902 count
, entry
->dev_addr
, entry
->size
,
903 dir2name
[entry
->direction
], type2name
[entry
->type
]);
912 void dma_debug_add_bus(struct bus_type
*bus
)
914 struct notifier_block
*nb
;
916 if (dma_debug_disabled())
919 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
921 pr_err("dma_debug_add_bus: out of memory\n");
925 nb
->notifier_call
= dma_debug_device_change
;
927 bus_register_notifier(bus
, nb
);
930 static int dma_debug_init(void)
934 /* Do not use dma_debug_initialized here, since we really want to be
935 * called to set dma_debug_initialized
940 for (i
= 0; i
< HASH_SIZE
; ++i
) {
941 INIT_LIST_HEAD(&dma_entry_hash
[i
].list
);
942 spin_lock_init(&dma_entry_hash
[i
].lock
);
947 nr_pages
= DIV_ROUND_UP(nr_prealloc_entries
, DMA_DEBUG_DYNAMIC_ENTRIES
);
948 for (i
= 0; i
< nr_pages
; ++i
)
949 dma_debug_create_entries(GFP_KERNEL
);
950 if (num_free_entries
>= nr_prealloc_entries
) {
951 pr_info("preallocated %d debug entries\n", nr_total_entries
);
952 } else if (num_free_entries
> 0) {
953 pr_warn("%d debug entries requested but only %d allocated\n",
954 nr_prealloc_entries
, nr_total_entries
);
956 pr_err("debugging out of memory error - disabled\n");
957 global_disable
= true;
961 min_free_entries
= num_free_entries
;
963 dma_debug_initialized
= true;
965 pr_info("debugging enabled by kernel config\n");
968 core_initcall(dma_debug_init
);
970 static __init
int dma_debug_cmdline(char *str
)
975 if (strncmp(str
, "off", 3) == 0) {
976 pr_info("debugging disabled on kernel command line\n");
977 global_disable
= true;
983 static __init
int dma_debug_entries_cmdline(char *str
)
987 if (!get_option(&str
, &nr_prealloc_entries
))
988 nr_prealloc_entries
= PREALLOC_DMA_DEBUG_ENTRIES
;
992 __setup("dma_debug=", dma_debug_cmdline
);
993 __setup("dma_debug_entries=", dma_debug_entries_cmdline
);
995 static void check_unmap(struct dma_debug_entry
*ref
)
997 struct dma_debug_entry
*entry
;
998 struct hash_bucket
*bucket
;
1001 bucket
= get_hash_bucket(ref
, &flags
);
1002 entry
= bucket_find_exact(bucket
, ref
);
1005 /* must drop lock before calling dma_mapping_error */
1006 put_hash_bucket(bucket
, flags
);
1008 if (dma_mapping_error(ref
->dev
, ref
->dev_addr
)) {
1009 err_printk(ref
->dev
, NULL
,
1010 "device driver tries to free an "
1011 "invalid DMA memory address\n");
1013 err_printk(ref
->dev
, NULL
,
1014 "device driver tries to free DMA "
1015 "memory it has not allocated [device "
1016 "address=0x%016llx] [size=%llu bytes]\n",
1017 ref
->dev_addr
, ref
->size
);
1022 if (ref
->size
!= entry
->size
) {
1023 err_printk(ref
->dev
, entry
, "device driver frees "
1024 "DMA memory with different size "
1025 "[device address=0x%016llx] [map size=%llu bytes] "
1026 "[unmap size=%llu bytes]\n",
1027 ref
->dev_addr
, entry
->size
, ref
->size
);
1030 if (ref
->type
!= entry
->type
) {
1031 err_printk(ref
->dev
, entry
, "device driver frees "
1032 "DMA memory with wrong function "
1033 "[device address=0x%016llx] [size=%llu bytes] "
1034 "[mapped as %s] [unmapped as %s]\n",
1035 ref
->dev_addr
, ref
->size
,
1036 type2name
[entry
->type
], type2name
[ref
->type
]);
1037 } else if ((entry
->type
== dma_debug_coherent
) &&
1038 (phys_addr(ref
) != phys_addr(entry
))) {
1039 err_printk(ref
->dev
, entry
, "device driver frees "
1040 "DMA memory with different CPU address "
1041 "[device address=0x%016llx] [size=%llu bytes] "
1042 "[cpu alloc address=0x%016llx] "
1043 "[cpu free address=0x%016llx]",
1044 ref
->dev_addr
, ref
->size
,
1049 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
1050 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
1051 err_printk(ref
->dev
, entry
, "device driver frees "
1052 "DMA sg list with different entry count "
1053 "[map count=%d] [unmap count=%d]\n",
1054 entry
->sg_call_ents
, ref
->sg_call_ents
);
1058 * This may be no bug in reality - but most implementations of the
1059 * DMA API don't handle this properly, so check for it here
1061 if (ref
->direction
!= entry
->direction
) {
1062 err_printk(ref
->dev
, entry
, "device driver frees "
1063 "DMA memory with different direction "
1064 "[device address=0x%016llx] [size=%llu bytes] "
1065 "[mapped with %s] [unmapped with %s]\n",
1066 ref
->dev_addr
, ref
->size
,
1067 dir2name
[entry
->direction
],
1068 dir2name
[ref
->direction
]);
1072 * Drivers should use dma_mapping_error() to check the returned
1073 * addresses of dma_map_single() and dma_map_page().
1074 * If not, print this warning message. See Documentation/DMA-API.txt.
1076 if (entry
->map_err_type
== MAP_ERR_NOT_CHECKED
) {
1077 err_printk(ref
->dev
, entry
,
1078 "device driver failed to check map error"
1079 "[device address=0x%016llx] [size=%llu bytes] "
1081 ref
->dev_addr
, ref
->size
,
1082 type2name
[entry
->type
]);
1085 hash_bucket_del(entry
);
1086 dma_entry_free(entry
);
1088 put_hash_bucket(bucket
, flags
);
1091 static void check_for_stack(struct device
*dev
,
1092 struct page
*page
, size_t offset
)
1095 struct vm_struct
*stack_vm_area
= task_stack_vm_area(current
);
1097 if (!stack_vm_area
) {
1098 /* Stack is direct-mapped. */
1099 if (PageHighMem(page
))
1101 addr
= page_address(page
) + offset
;
1102 if (object_is_on_stack(addr
))
1103 err_printk(dev
, NULL
, "device driver maps memory from stack [addr=%p]\n", addr
);
1105 /* Stack is vmalloced. */
1108 for (i
= 0; i
< stack_vm_area
->nr_pages
; i
++) {
1109 if (page
!= stack_vm_area
->pages
[i
])
1112 addr
= (u8
*)current
->stack
+ i
* PAGE_SIZE
+ offset
;
1113 err_printk(dev
, NULL
, "device driver maps memory from stack [probable addr=%p]\n", addr
);
1119 static inline bool overlap(void *addr
, unsigned long len
, void *start
, void *end
)
1121 unsigned long a1
= (unsigned long)addr
;
1122 unsigned long b1
= a1
+ len
;
1123 unsigned long a2
= (unsigned long)start
;
1124 unsigned long b2
= (unsigned long)end
;
1126 return !(b1
<= a2
|| a1
>= b2
);
1129 static void check_for_illegal_area(struct device
*dev
, void *addr
, unsigned long len
)
1131 if (overlap(addr
, len
, _stext
, _etext
) ||
1132 overlap(addr
, len
, __start_rodata
, __end_rodata
))
1133 err_printk(dev
, NULL
, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr
, len
);
1136 static void check_sync(struct device
*dev
,
1137 struct dma_debug_entry
*ref
,
1140 struct dma_debug_entry
*entry
;
1141 struct hash_bucket
*bucket
;
1142 unsigned long flags
;
1144 bucket
= get_hash_bucket(ref
, &flags
);
1146 entry
= bucket_find_contain(&bucket
, ref
, &flags
);
1149 err_printk(dev
, NULL
, "device driver tries "
1150 "to sync DMA memory it has not allocated "
1151 "[device address=0x%016llx] [size=%llu bytes]\n",
1152 (unsigned long long)ref
->dev_addr
, ref
->size
);
1156 if (ref
->size
> entry
->size
) {
1157 err_printk(dev
, entry
, "device driver syncs"
1158 " DMA memory outside allocated range "
1159 "[device address=0x%016llx] "
1160 "[allocation size=%llu bytes] "
1161 "[sync offset+size=%llu]\n",
1162 entry
->dev_addr
, entry
->size
,
1166 if (entry
->direction
== DMA_BIDIRECTIONAL
)
1169 if (ref
->direction
!= entry
->direction
) {
1170 err_printk(dev
, entry
, "device driver syncs "
1171 "DMA memory with different direction "
1172 "[device address=0x%016llx] [size=%llu bytes] "
1173 "[mapped with %s] [synced with %s]\n",
1174 (unsigned long long)ref
->dev_addr
, entry
->size
,
1175 dir2name
[entry
->direction
],
1176 dir2name
[ref
->direction
]);
1179 if (to_cpu
&& !(entry
->direction
== DMA_FROM_DEVICE
) &&
1180 !(ref
->direction
== DMA_TO_DEVICE
))
1181 err_printk(dev
, entry
, "device driver syncs "
1182 "device read-only DMA memory for cpu "
1183 "[device address=0x%016llx] [size=%llu bytes] "
1184 "[mapped with %s] [synced with %s]\n",
1185 (unsigned long long)ref
->dev_addr
, entry
->size
,
1186 dir2name
[entry
->direction
],
1187 dir2name
[ref
->direction
]);
1189 if (!to_cpu
&& !(entry
->direction
== DMA_TO_DEVICE
) &&
1190 !(ref
->direction
== DMA_FROM_DEVICE
))
1191 err_printk(dev
, entry
, "device driver syncs "
1192 "device write-only DMA memory to device "
1193 "[device address=0x%016llx] [size=%llu bytes] "
1194 "[mapped with %s] [synced with %s]\n",
1195 (unsigned long long)ref
->dev_addr
, entry
->size
,
1196 dir2name
[entry
->direction
],
1197 dir2name
[ref
->direction
]);
1199 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
1200 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
1201 err_printk(ref
->dev
, entry
, "device driver syncs "
1202 "DMA sg list with different entry count "
1203 "[map count=%d] [sync count=%d]\n",
1204 entry
->sg_call_ents
, ref
->sg_call_ents
);
1208 put_hash_bucket(bucket
, flags
);
1211 static void check_sg_segment(struct device
*dev
, struct scatterlist
*sg
)
1213 #ifdef CONFIG_DMA_API_DEBUG_SG
1214 unsigned int max_seg
= dma_get_max_seg_size(dev
);
1215 u64 start
, end
, boundary
= dma_get_seg_boundary(dev
);
1218 * Either the driver forgot to set dma_parms appropriately, or
1219 * whoever generated the list forgot to check them.
1221 if (sg
->length
> max_seg
)
1222 err_printk(dev
, NULL
, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1223 sg
->length
, max_seg
);
1225 * In some cases this could potentially be the DMA API
1226 * implementation's fault, but it would usually imply that
1227 * the scatterlist was built inappropriately to begin with.
1229 start
= sg_dma_address(sg
);
1230 end
= start
+ sg_dma_len(sg
) - 1;
1231 if ((start
^ end
) & ~boundary
)
1232 err_printk(dev
, NULL
, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1233 start
, end
, boundary
);
1237 void debug_dma_map_single(struct device
*dev
, const void *addr
,
1240 if (unlikely(dma_debug_disabled()))
1243 if (!virt_addr_valid(addr
))
1244 err_printk(dev
, NULL
, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1247 if (is_vmalloc_addr(addr
))
1248 err_printk(dev
, NULL
, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1251 EXPORT_SYMBOL(debug_dma_map_single
);
1253 void debug_dma_map_page(struct device
*dev
, struct page
*page
, size_t offset
,
1254 size_t size
, int direction
, dma_addr_t dma_addr
)
1256 struct dma_debug_entry
*entry
;
1258 if (unlikely(dma_debug_disabled()))
1261 if (dma_mapping_error(dev
, dma_addr
))
1264 entry
= dma_entry_alloc();
1269 entry
->type
= dma_debug_single
;
1270 entry
->pfn
= page_to_pfn(page
);
1271 entry
->offset
= offset
,
1272 entry
->dev_addr
= dma_addr
;
1274 entry
->direction
= direction
;
1275 entry
->map_err_type
= MAP_ERR_NOT_CHECKED
;
1277 check_for_stack(dev
, page
, offset
);
1279 if (!PageHighMem(page
)) {
1280 void *addr
= page_address(page
) + offset
;
1282 check_for_illegal_area(dev
, addr
, size
);
1285 add_dma_entry(entry
);
1287 EXPORT_SYMBOL(debug_dma_map_page
);
1289 void debug_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
1291 struct dma_debug_entry ref
;
1292 struct dma_debug_entry
*entry
;
1293 struct hash_bucket
*bucket
;
1294 unsigned long flags
;
1296 if (unlikely(dma_debug_disabled()))
1300 ref
.dev_addr
= dma_addr
;
1301 bucket
= get_hash_bucket(&ref
, &flags
);
1303 list_for_each_entry(entry
, &bucket
->list
, list
) {
1304 if (!exact_match(&ref
, entry
))
1308 * The same physical address can be mapped multiple
1309 * times. Without a hardware IOMMU this results in the
1310 * same device addresses being put into the dma-debug
1311 * hash multiple times too. This can result in false
1312 * positives being reported. Therefore we implement a
1313 * best-fit algorithm here which updates the first entry
1314 * from the hash which fits the reference value and is
1315 * not currently listed as being checked.
1317 if (entry
->map_err_type
== MAP_ERR_NOT_CHECKED
) {
1318 entry
->map_err_type
= MAP_ERR_CHECKED
;
1323 put_hash_bucket(bucket
, flags
);
1325 EXPORT_SYMBOL(debug_dma_mapping_error
);
1327 void debug_dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
1328 size_t size
, int direction
)
1330 struct dma_debug_entry ref
= {
1331 .type
= dma_debug_single
,
1335 .direction
= direction
,
1338 if (unlikely(dma_debug_disabled()))
1342 EXPORT_SYMBOL(debug_dma_unmap_page
);
1344 void debug_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
1345 int nents
, int mapped_ents
, int direction
)
1347 struct dma_debug_entry
*entry
;
1348 struct scatterlist
*s
;
1351 if (unlikely(dma_debug_disabled()))
1354 for_each_sg(sg
, s
, mapped_ents
, i
) {
1355 entry
= dma_entry_alloc();
1359 entry
->type
= dma_debug_sg
;
1361 entry
->pfn
= page_to_pfn(sg_page(s
));
1362 entry
->offset
= s
->offset
,
1363 entry
->size
= sg_dma_len(s
);
1364 entry
->dev_addr
= sg_dma_address(s
);
1365 entry
->direction
= direction
;
1366 entry
->sg_call_ents
= nents
;
1367 entry
->sg_mapped_ents
= mapped_ents
;
1369 check_for_stack(dev
, sg_page(s
), s
->offset
);
1371 if (!PageHighMem(sg_page(s
))) {
1372 check_for_illegal_area(dev
, sg_virt(s
), sg_dma_len(s
));
1375 check_sg_segment(dev
, s
);
1377 add_dma_entry(entry
);
1380 EXPORT_SYMBOL(debug_dma_map_sg
);
1382 static int get_nr_mapped_entries(struct device
*dev
,
1383 struct dma_debug_entry
*ref
)
1385 struct dma_debug_entry
*entry
;
1386 struct hash_bucket
*bucket
;
1387 unsigned long flags
;
1390 bucket
= get_hash_bucket(ref
, &flags
);
1391 entry
= bucket_find_exact(bucket
, ref
);
1395 mapped_ents
= entry
->sg_mapped_ents
;
1396 put_hash_bucket(bucket
, flags
);
1401 void debug_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
1402 int nelems
, int dir
)
1404 struct scatterlist
*s
;
1405 int mapped_ents
= 0, i
;
1407 if (unlikely(dma_debug_disabled()))
1410 for_each_sg(sglist
, s
, nelems
, i
) {
1412 struct dma_debug_entry ref
= {
1413 .type
= dma_debug_sg
,
1415 .pfn
= page_to_pfn(sg_page(s
)),
1416 .offset
= s
->offset
,
1417 .dev_addr
= sg_dma_address(s
),
1418 .size
= sg_dma_len(s
),
1420 .sg_call_ents
= nelems
,
1423 if (mapped_ents
&& i
>= mapped_ents
)
1427 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1432 EXPORT_SYMBOL(debug_dma_unmap_sg
);
1434 void debug_dma_alloc_coherent(struct device
*dev
, size_t size
,
1435 dma_addr_t dma_addr
, void *virt
)
1437 struct dma_debug_entry
*entry
;
1439 if (unlikely(dma_debug_disabled()))
1442 if (unlikely(virt
== NULL
))
1445 /* handle vmalloc and linear addresses */
1446 if (!is_vmalloc_addr(virt
) && !virt_addr_valid(virt
))
1449 entry
= dma_entry_alloc();
1453 entry
->type
= dma_debug_coherent
;
1455 entry
->offset
= offset_in_page(virt
);
1457 entry
->dev_addr
= dma_addr
;
1458 entry
->direction
= DMA_BIDIRECTIONAL
;
1460 if (is_vmalloc_addr(virt
))
1461 entry
->pfn
= vmalloc_to_pfn(virt
);
1463 entry
->pfn
= page_to_pfn(virt_to_page(virt
));
1465 add_dma_entry(entry
);
1468 void debug_dma_free_coherent(struct device
*dev
, size_t size
,
1469 void *virt
, dma_addr_t addr
)
1471 struct dma_debug_entry ref
= {
1472 .type
= dma_debug_coherent
,
1474 .offset
= offset_in_page(virt
),
1477 .direction
= DMA_BIDIRECTIONAL
,
1480 /* handle vmalloc and linear addresses */
1481 if (!is_vmalloc_addr(virt
) && !virt_addr_valid(virt
))
1484 if (is_vmalloc_addr(virt
))
1485 ref
.pfn
= vmalloc_to_pfn(virt
);
1487 ref
.pfn
= page_to_pfn(virt_to_page(virt
));
1489 if (unlikely(dma_debug_disabled()))
1495 void debug_dma_map_resource(struct device
*dev
, phys_addr_t addr
, size_t size
,
1496 int direction
, dma_addr_t dma_addr
)
1498 struct dma_debug_entry
*entry
;
1500 if (unlikely(dma_debug_disabled()))
1503 entry
= dma_entry_alloc();
1507 entry
->type
= dma_debug_resource
;
1509 entry
->pfn
= PHYS_PFN(addr
);
1510 entry
->offset
= offset_in_page(addr
);
1512 entry
->dev_addr
= dma_addr
;
1513 entry
->direction
= direction
;
1514 entry
->map_err_type
= MAP_ERR_NOT_CHECKED
;
1516 add_dma_entry(entry
);
1518 EXPORT_SYMBOL(debug_dma_map_resource
);
1520 void debug_dma_unmap_resource(struct device
*dev
, dma_addr_t dma_addr
,
1521 size_t size
, int direction
)
1523 struct dma_debug_entry ref
= {
1524 .type
= dma_debug_resource
,
1526 .dev_addr
= dma_addr
,
1528 .direction
= direction
,
1531 if (unlikely(dma_debug_disabled()))
1536 EXPORT_SYMBOL(debug_dma_unmap_resource
);
1538 void debug_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
1539 size_t size
, int direction
)
1541 struct dma_debug_entry ref
;
1543 if (unlikely(dma_debug_disabled()))
1546 ref
.type
= dma_debug_single
;
1548 ref
.dev_addr
= dma_handle
;
1550 ref
.direction
= direction
;
1551 ref
.sg_call_ents
= 0;
1553 check_sync(dev
, &ref
, true);
1555 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu
);
1557 void debug_dma_sync_single_for_device(struct device
*dev
,
1558 dma_addr_t dma_handle
, size_t size
,
1561 struct dma_debug_entry ref
;
1563 if (unlikely(dma_debug_disabled()))
1566 ref
.type
= dma_debug_single
;
1568 ref
.dev_addr
= dma_handle
;
1570 ref
.direction
= direction
;
1571 ref
.sg_call_ents
= 0;
1573 check_sync(dev
, &ref
, false);
1575 EXPORT_SYMBOL(debug_dma_sync_single_for_device
);
1577 void debug_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
1578 int nelems
, int direction
)
1580 struct scatterlist
*s
;
1581 int mapped_ents
= 0, i
;
1583 if (unlikely(dma_debug_disabled()))
1586 for_each_sg(sg
, s
, nelems
, i
) {
1588 struct dma_debug_entry ref
= {
1589 .type
= dma_debug_sg
,
1591 .pfn
= page_to_pfn(sg_page(s
)),
1592 .offset
= s
->offset
,
1593 .dev_addr
= sg_dma_address(s
),
1594 .size
= sg_dma_len(s
),
1595 .direction
= direction
,
1596 .sg_call_ents
= nelems
,
1600 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1602 if (i
>= mapped_ents
)
1605 check_sync(dev
, &ref
, true);
1608 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu
);
1610 void debug_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
1611 int nelems
, int direction
)
1613 struct scatterlist
*s
;
1614 int mapped_ents
= 0, i
;
1616 if (unlikely(dma_debug_disabled()))
1619 for_each_sg(sg
, s
, nelems
, i
) {
1621 struct dma_debug_entry ref
= {
1622 .type
= dma_debug_sg
,
1624 .pfn
= page_to_pfn(sg_page(s
)),
1625 .offset
= s
->offset
,
1626 .dev_addr
= sg_dma_address(s
),
1627 .size
= sg_dma_len(s
),
1628 .direction
= direction
,
1629 .sg_call_ents
= nelems
,
1632 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1634 if (i
>= mapped_ents
)
1637 check_sync(dev
, &ref
, false);
1640 EXPORT_SYMBOL(debug_dma_sync_sg_for_device
);
1642 static int __init
dma_debug_driver_setup(char *str
)
1646 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
, ++str
) {
1647 current_driver_name
[i
] = *str
;
1652 if (current_driver_name
[0])
1653 pr_info("enable driver filter for driver [%s]\n",
1654 current_driver_name
);
1659 __setup("dma_debug_driver=", dma_debug_driver_setup
);