1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
8 #define pr_fmt(fmt) "DMA-API: " fmt
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 <asm/sections.h>
29 #define HASH_SIZE 16384ULL
30 #define HASH_FN_SHIFT 13
31 #define HASH_FN_MASK (HASH_SIZE - 1)
33 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
34 /* If the pool runs out, add this many new entries at once */
35 #define DMA_DEBUG_DYNAMIC_ENTRIES (PAGE_SIZE / sizeof(struct dma_debug_entry))
45 MAP_ERR_CHECK_NOT_APPLICABLE,
50 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
53 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
54 * @list: node on pre-allocated free_entries list
55 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
56 * @dev_addr: dma address
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 * @paddr: physical start address of the mapping
63 * @map_err_type: track whether dma_mapping_error() was checked
64 * @stack_len: number of backtrace entries in @stack_entries
65 * @stack_entries: stack of backtrace history
67 struct dma_debug_entry {
68 struct list_head list;
77 enum map_err_types map_err_type;
78 #ifdef CONFIG_STACKTRACE
79 unsigned int stack_len;
80 unsigned long stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
82 } ____cacheline_aligned_in_smp;
84 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
87 struct list_head list;
91 /* Hash list to save the allocated dma addresses */
92 static struct hash_bucket dma_entry_hash[HASH_SIZE];
93 /* List of pre-allocated dma_debug_entry's */
94 static LIST_HEAD(free_entries);
95 /* Lock for the list above */
96 static DEFINE_SPINLOCK(free_entries_lock);
98 /* Global disable flag - will be set in case of an error */
99 static bool global_disable __read_mostly;
101 /* Early initialization disable flag, set at the end of dma_debug_init */
102 static bool dma_debug_initialized __read_mostly;
104 static inline bool dma_debug_disabled(void)
106 return global_disable || !dma_debug_initialized;
109 /* Global error count */
110 static u32 error_count;
112 /* Global error show enable*/
113 static u32 show_all_errors __read_mostly;
114 /* Number of errors to show */
115 static u32 show_num_errors = 1;
117 static u32 num_free_entries;
118 static u32 min_free_entries;
119 static u32 nr_total_entries;
121 /* number of preallocated entries requested by kernel cmdline */
122 static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
124 /* per-driver filter related state */
126 #define NAME_MAX_LEN 64
128 static char current_driver_name[NAME_MAX_LEN] __read_mostly;
129 static struct device_driver *current_driver __read_mostly;
131 static DEFINE_RWLOCK(driver_name_lock);
133 static const char *const maperr2str[] = {
134 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
135 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
136 [MAP_ERR_CHECKED] = "dma map error checked",
139 static const char *type2name[] = {
140 [dma_debug_single] = "single",
141 [dma_debug_sg] = "scatter-gather",
142 [dma_debug_coherent] = "coherent",
143 [dma_debug_resource] = "resource",
146 static const char *dir2name[] = {
147 [DMA_BIDIRECTIONAL] = "DMA_BIDIRECTIONAL",
148 [DMA_TO_DEVICE] = "DMA_TO_DEVICE",
149 [DMA_FROM_DEVICE] = "DMA_FROM_DEVICE",
150 [DMA_NONE] = "DMA_NONE",
154 * The access to some variables in this macro is racy. We can't use atomic_t
155 * here because all these variables are exported to debugfs. Some of them even
156 * writeable. This is also the reason why a lock won't help much. But anyway,
157 * the races are no big deal. Here is why:
159 * error_count: the addition is racy, but the worst thing that can happen is
160 * that we don't count some errors
161 * show_num_errors: the subtraction is racy. Also no big deal because in
162 * worst case this will result in one warning more in the
163 * system log than the user configured. This variable is
164 * writeable via debugfs.
166 static inline void dump_entry_trace(struct dma_debug_entry *entry)
168 #ifdef CONFIG_STACKTRACE
170 pr_warn("Mapped at:\n");
171 stack_trace_print(entry->stack_entries, entry->stack_len, 0);
176 static bool driver_filter(struct device *dev)
178 struct device_driver *drv;
182 /* driver filter off */
183 if (likely(!current_driver_name[0]))
186 /* driver filter on and initialized */
187 if (current_driver && dev && dev->driver == current_driver)
190 /* driver filter on, but we can't filter on a NULL device... */
194 if (current_driver || !current_driver_name[0])
197 /* driver filter on but not yet initialized */
202 /* lock to protect against change of current_driver_name */
203 read_lock_irqsave(&driver_name_lock, flags);
207 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
208 current_driver = drv;
212 read_unlock_irqrestore(&driver_name_lock, flags);
217 #define err_printk(dev, entry, format, arg...) do { \
219 if (driver_filter(dev) && \
220 (show_all_errors || show_num_errors > 0)) { \
221 WARN(1, pr_fmt("%s %s: ") format, \
222 dev ? dev_driver_string(dev) : "NULL", \
223 dev ? dev_name(dev) : "NULL", ## arg); \
224 dump_entry_trace(entry); \
226 if (!show_all_errors && show_num_errors > 0) \
227 show_num_errors -= 1; \
231 * Hash related functions
233 * Every DMA-API request is saved into a struct dma_debug_entry. To
234 * have quick access to these structs they are stored into a hash.
236 static int hash_fn(struct dma_debug_entry *entry)
239 * Hash function is based on the dma address.
240 * We use bits 20-27 here as the index into the hash
242 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
246 * Request exclusive access to a hash bucket for a given dma_debug_entry.
248 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
249 unsigned long *flags)
250 __acquires(&dma_entry_hash[idx].lock)
252 int idx = hash_fn(entry);
253 unsigned long __flags;
255 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
257 return &dma_entry_hash[idx];
261 * Give up exclusive access to the hash bucket
263 static void put_hash_bucket(struct hash_bucket *bucket,
265 __releases(&bucket->lock)
267 spin_unlock_irqrestore(&bucket->lock, flags);
270 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
272 return ((a->dev_addr == b->dev_addr) &&
273 (a->dev == b->dev)) ? true : false;
276 static bool containing_match(struct dma_debug_entry *a,
277 struct dma_debug_entry *b)
279 if (a->dev != b->dev)
282 if ((b->dev_addr <= a->dev_addr) &&
283 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
290 * Search a given entry in the hash bucket list
292 static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
293 struct dma_debug_entry *ref,
296 struct dma_debug_entry *entry, *ret = NULL;
297 int matches = 0, match_lvl, last_lvl = -1;
299 list_for_each_entry(entry, &bucket->list, list) {
300 if (!match(ref, entry))
304 * Some drivers map the same physical address multiple
305 * times. Without a hardware IOMMU this results in the
306 * same device addresses being put into the dma-debug
307 * hash multiple times too. This can result in false
308 * positives being reported. Therefore we implement a
309 * best-fit algorithm here which returns the entry from
310 * the hash which fits best to the reference value
311 * instead of the first-fit.
315 entry->size == ref->size ? ++match_lvl : 0;
316 entry->type == ref->type ? ++match_lvl : 0;
317 entry->direction == ref->direction ? ++match_lvl : 0;
318 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
320 if (match_lvl == 4) {
321 /* perfect-fit - return the result */
323 } else if (match_lvl > last_lvl) {
325 * We found an entry that fits better then the
326 * previous one or it is the 1st match.
328 last_lvl = match_lvl;
334 * If we have multiple matches but no perfect-fit, just return
337 ret = (matches == 1) ? ret : NULL;
342 static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
343 struct dma_debug_entry *ref)
345 return __hash_bucket_find(bucket, ref, exact_match);
348 static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
349 struct dma_debug_entry *ref,
350 unsigned long *flags)
353 struct dma_debug_entry *entry, index = *ref;
354 int limit = min(HASH_SIZE, (index.dev_addr >> HASH_FN_SHIFT) + 1);
356 for (int i = 0; i < limit; i++) {
357 entry = __hash_bucket_find(*bucket, ref, containing_match);
363 * Nothing found, go back a hash bucket
365 put_hash_bucket(*bucket, *flags);
366 index.dev_addr -= (1 << HASH_FN_SHIFT);
367 *bucket = get_hash_bucket(&index, flags);
374 * Add an entry to a hash bucket
376 static void hash_bucket_add(struct hash_bucket *bucket,
377 struct dma_debug_entry *entry)
379 list_add_tail(&entry->list, &bucket->list);
383 * Remove entry from a hash bucket list
385 static void hash_bucket_del(struct dma_debug_entry *entry)
387 list_del(&entry->list);
391 * For each mapping (initial cacheline in the case of
392 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
393 * scatterlist, or the cacheline specified in dma_map_single) insert
394 * into this tree using the cacheline as the key. At
395 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
396 * the entry already exists at insertion time add a tag as a reference
397 * count for the overlapping mappings. For now, the overlap tracking
398 * just ensures that 'unmaps' balance 'maps' before marking the
399 * cacheline idle, but we should also be flagging overlaps as an API
402 * Memory usage is mostly constrained by the maximum number of available
403 * dma-debug entries in that we need a free dma_debug_entry before
404 * inserting into the tree. In the case of dma_map_page and
405 * dma_alloc_coherent there is only one dma_debug_entry and one
406 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
407 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
408 * entries into the tree.
410 * Use __GFP_NOWARN because the printk from an OOM, to netconsole, could end
411 * up right back in the DMA debugging code, leading to a deadlock.
413 static RADIX_TREE(dma_active_cacheline, GFP_ATOMIC | __GFP_NOWARN);
414 static DEFINE_SPINLOCK(radix_lock);
415 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
416 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
417 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
419 static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
421 return ((entry->paddr >> PAGE_SHIFT) << CACHELINE_PER_PAGE_SHIFT) +
422 (offset_in_page(entry->paddr) >> L1_CACHE_SHIFT);
425 static int active_cacheline_read_overlap(phys_addr_t cln)
429 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
430 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
435 static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
439 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
442 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
443 if (overlap & 1 << i)
444 radix_tree_tag_set(&dma_active_cacheline, cln, i);
446 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
451 static void active_cacheline_inc_overlap(phys_addr_t cln)
453 int overlap = active_cacheline_read_overlap(cln);
455 overlap = active_cacheline_set_overlap(cln, ++overlap);
457 /* If we overflowed the overlap counter then we're potentially
458 * leaking dma-mappings.
460 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
461 pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
462 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
465 static int active_cacheline_dec_overlap(phys_addr_t cln)
467 int overlap = active_cacheline_read_overlap(cln);
469 return active_cacheline_set_overlap(cln, --overlap);
472 static int active_cacheline_insert(struct dma_debug_entry *entry)
474 phys_addr_t cln = to_cacheline_number(entry);
478 /* If the device is not writing memory then we don't have any
479 * concerns about the cpu consuming stale data. This mitigates
480 * legitimate usages of overlapping mappings.
482 if (entry->direction == DMA_TO_DEVICE)
485 spin_lock_irqsave(&radix_lock, flags);
486 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
488 active_cacheline_inc_overlap(cln);
489 spin_unlock_irqrestore(&radix_lock, flags);
494 static void active_cacheline_remove(struct dma_debug_entry *entry)
496 phys_addr_t cln = to_cacheline_number(entry);
499 /* ...mirror the insert case */
500 if (entry->direction == DMA_TO_DEVICE)
503 spin_lock_irqsave(&radix_lock, flags);
504 /* since we are counting overlaps the final put of the
505 * cacheline will occur when the overlap count is 0.
506 * active_cacheline_dec_overlap() returns -1 in that case
508 if (active_cacheline_dec_overlap(cln) < 0)
509 radix_tree_delete(&dma_active_cacheline, cln);
510 spin_unlock_irqrestore(&radix_lock, flags);
514 * Dump mappings entries on kernel space for debugging purposes
516 void debug_dma_dump_mappings(struct device *dev)
521 for (idx = 0; idx < HASH_SIZE; idx++) {
522 struct hash_bucket *bucket = &dma_entry_hash[idx];
523 struct dma_debug_entry *entry;
526 spin_lock_irqsave(&bucket->lock, flags);
527 list_for_each_entry(entry, &bucket->list, list) {
528 if (!dev || dev == entry->dev) {
529 cln = to_cacheline_number(entry);
531 "%s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
532 type2name[entry->type], idx,
533 &entry->paddr, entry->dev_addr,
535 dir2name[entry->direction],
536 maperr2str[entry->map_err_type]);
539 spin_unlock_irqrestore(&bucket->lock, flags);
546 * Dump mappings entries on user space via debugfs
548 static int dump_show(struct seq_file *seq, void *v)
553 for (idx = 0; idx < HASH_SIZE; idx++) {
554 struct hash_bucket *bucket = &dma_entry_hash[idx];
555 struct dma_debug_entry *entry;
558 spin_lock_irqsave(&bucket->lock, flags);
559 list_for_each_entry(entry, &bucket->list, list) {
560 cln = to_cacheline_number(entry);
562 "%s %s %s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s\n",
563 dev_driver_string(entry->dev),
564 dev_name(entry->dev),
565 type2name[entry->type], idx,
566 &entry->paddr, entry->dev_addr,
568 dir2name[entry->direction],
569 maperr2str[entry->map_err_type]);
571 spin_unlock_irqrestore(&bucket->lock, flags);
575 DEFINE_SHOW_ATTRIBUTE(dump);
578 * Wrapper function for adding an entry to the hash.
579 * This function takes care of locking itself.
581 static void add_dma_entry(struct dma_debug_entry *entry, unsigned long attrs)
583 struct hash_bucket *bucket;
587 bucket = get_hash_bucket(entry, &flags);
588 hash_bucket_add(bucket, entry);
589 put_hash_bucket(bucket, flags);
591 rc = active_cacheline_insert(entry);
593 pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n");
594 global_disable = true;
595 } else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
596 err_printk(entry->dev, entry,
597 "cacheline tracking EEXIST, overlapping mappings aren't supported\n");
601 static int dma_debug_create_entries(gfp_t gfp)
603 struct dma_debug_entry *entry;
606 entry = (void *)get_zeroed_page(gfp);
610 for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
611 list_add_tail(&entry[i].list, &free_entries);
613 num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
614 nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
619 static struct dma_debug_entry *__dma_entry_alloc(void)
621 struct dma_debug_entry *entry;
623 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
624 list_del(&entry->list);
625 memset(entry, 0, sizeof(*entry));
627 num_free_entries -= 1;
628 if (num_free_entries < min_free_entries)
629 min_free_entries = num_free_entries;
635 * This should be called outside of free_entries_lock scope to avoid potential
636 * deadlocks with serial consoles that use DMA.
638 static void __dma_entry_alloc_check_leak(u32 nr_entries)
640 u32 tmp = nr_entries % nr_prealloc_entries;
642 /* Shout each time we tick over some multiple of the initial pool */
643 if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
644 pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
646 (nr_entries / nr_prealloc_entries));
650 /* struct dma_entry allocator
652 * The next two functions implement the allocator for
653 * struct dma_debug_entries.
655 static struct dma_debug_entry *dma_entry_alloc(void)
657 bool alloc_check_leak = false;
658 struct dma_debug_entry *entry;
662 spin_lock_irqsave(&free_entries_lock, flags);
663 if (num_free_entries == 0) {
664 if (dma_debug_create_entries(GFP_ATOMIC)) {
665 global_disable = true;
666 spin_unlock_irqrestore(&free_entries_lock, flags);
667 pr_err("debugging out of memory - disabling\n");
670 alloc_check_leak = true;
671 nr_entries = nr_total_entries;
674 entry = __dma_entry_alloc();
676 spin_unlock_irqrestore(&free_entries_lock, flags);
678 if (alloc_check_leak)
679 __dma_entry_alloc_check_leak(nr_entries);
681 #ifdef CONFIG_STACKTRACE
682 entry->stack_len = stack_trace_save(entry->stack_entries,
683 ARRAY_SIZE(entry->stack_entries),
689 static void dma_entry_free(struct dma_debug_entry *entry)
693 active_cacheline_remove(entry);
696 * add to beginning of the list - this way the entries are
697 * more likely cache hot when they are reallocated.
699 spin_lock_irqsave(&free_entries_lock, flags);
700 list_add(&entry->list, &free_entries);
701 num_free_entries += 1;
702 spin_unlock_irqrestore(&free_entries_lock, flags);
706 * DMA-API debugging init code
708 * The init code does two things:
709 * 1. Initialize core data structures
710 * 2. Preallocate a given number of dma_debug_entry structs
713 static ssize_t filter_read(struct file *file, char __user *user_buf,
714 size_t count, loff_t *ppos)
716 char buf[NAME_MAX_LEN + 1];
720 if (!current_driver_name[0])
724 * We can't copy to userspace directly because current_driver_name can
725 * only be read under the driver_name_lock with irqs disabled. So
726 * create a temporary copy first.
728 read_lock_irqsave(&driver_name_lock, flags);
729 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
730 read_unlock_irqrestore(&driver_name_lock, flags);
732 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
735 static ssize_t filter_write(struct file *file, const char __user *userbuf,
736 size_t count, loff_t *ppos)
738 char buf[NAME_MAX_LEN];
744 * We can't copy from userspace directly. Access to
745 * current_driver_name is protected with a write_lock with irqs
746 * disabled. Since copy_from_user can fault and may sleep we
747 * need to copy to temporary buffer first
749 len = min(count, (size_t)(NAME_MAX_LEN - 1));
750 if (copy_from_user(buf, userbuf, len))
755 write_lock_irqsave(&driver_name_lock, flags);
758 * Now handle the string we got from userspace very carefully.
760 * - only use the first token we got
761 * - token delimiter is everything looking like a space
762 * character (' ', '\n', '\t' ...)
765 if (!isalnum(buf[0])) {
767 * If the first character userspace gave us is not
768 * alphanumerical then assume the filter should be
771 if (current_driver_name[0])
772 pr_info("switching off dma-debug driver filter\n");
773 current_driver_name[0] = 0;
774 current_driver = NULL;
779 * Now parse out the first token and use it as the name for the
780 * driver to filter for.
782 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
783 current_driver_name[i] = buf[i];
784 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
787 current_driver_name[i] = 0;
788 current_driver = NULL;
790 pr_info("enable driver filter for driver [%s]\n",
791 current_driver_name);
794 write_unlock_irqrestore(&driver_name_lock, flags);
799 static const struct file_operations filter_fops = {
801 .write = filter_write,
802 .llseek = default_llseek,
805 static int __init dma_debug_fs_init(void)
807 struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
809 debugfs_create_bool("disabled", 0444, dentry, &global_disable);
810 debugfs_create_u32("error_count", 0444, dentry, &error_count);
811 debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
812 debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
813 debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
814 debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
815 debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
816 debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
817 debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
821 core_initcall_sync(dma_debug_fs_init);
823 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
825 struct dma_debug_entry *entry;
829 for (i = 0; i < HASH_SIZE; ++i) {
830 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
831 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
832 if (entry->dev == dev) {
837 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
843 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
845 struct device *dev = data;
846 struct dma_debug_entry *entry;
849 if (dma_debug_disabled())
853 case BUS_NOTIFY_UNBOUND_DRIVER:
854 count = device_dma_allocations(dev, &entry);
857 err_printk(dev, entry, "device driver has pending "
858 "DMA allocations while released from device "
860 "One of leaked entries details: "
861 "[device address=0x%016llx] [size=%llu bytes] "
862 "[mapped with %s] [mapped as %s]\n",
863 count, entry->dev_addr, entry->size,
864 dir2name[entry->direction], type2name[entry->type]);
873 void dma_debug_add_bus(const struct bus_type *bus)
875 struct notifier_block *nb;
877 if (dma_debug_disabled())
880 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
882 pr_err("dma_debug_add_bus: out of memory\n");
886 nb->notifier_call = dma_debug_device_change;
888 bus_register_notifier(bus, nb);
891 static int dma_debug_init(void)
895 /* Do not use dma_debug_initialized here, since we really want to be
896 * called to set dma_debug_initialized
901 for (i = 0; i < HASH_SIZE; ++i) {
902 INIT_LIST_HEAD(&dma_entry_hash[i].list);
903 spin_lock_init(&dma_entry_hash[i].lock);
906 nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
907 for (i = 0; i < nr_pages; ++i)
908 dma_debug_create_entries(GFP_KERNEL);
909 if (num_free_entries >= nr_prealloc_entries) {
910 pr_info("preallocated %d debug entries\n", nr_total_entries);
911 } else if (num_free_entries > 0) {
912 pr_warn("%d debug entries requested but only %d allocated\n",
913 nr_prealloc_entries, nr_total_entries);
915 pr_err("debugging out of memory error - disabled\n");
916 global_disable = true;
920 min_free_entries = num_free_entries;
922 dma_debug_initialized = true;
924 pr_info("debugging enabled by kernel config\n");
927 core_initcall(dma_debug_init);
929 static __init int dma_debug_cmdline(char *str)
934 if (strncmp(str, "off", 3) == 0) {
935 pr_info("debugging disabled on kernel command line\n");
936 global_disable = true;
942 static __init int dma_debug_entries_cmdline(char *str)
946 if (!get_option(&str, &nr_prealloc_entries))
947 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
951 __setup("dma_debug=", dma_debug_cmdline);
952 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
954 static void check_unmap(struct dma_debug_entry *ref)
956 struct dma_debug_entry *entry;
957 struct hash_bucket *bucket;
960 bucket = get_hash_bucket(ref, &flags);
961 entry = bucket_find_exact(bucket, ref);
964 /* must drop lock before calling dma_mapping_error */
965 put_hash_bucket(bucket, flags);
967 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
968 err_printk(ref->dev, NULL,
969 "device driver tries to free an "
970 "invalid DMA memory address\n");
972 err_printk(ref->dev, NULL,
973 "device driver tries to free DMA "
974 "memory it has not allocated [device "
975 "address=0x%016llx] [size=%llu bytes]\n",
976 ref->dev_addr, ref->size);
981 if (ref->size != entry->size) {
982 err_printk(ref->dev, entry, "device driver frees "
983 "DMA memory with different size "
984 "[device address=0x%016llx] [map size=%llu bytes] "
985 "[unmap size=%llu bytes]\n",
986 ref->dev_addr, entry->size, ref->size);
989 if (ref->type != entry->type) {
990 err_printk(ref->dev, entry, "device driver frees "
991 "DMA memory with wrong function "
992 "[device address=0x%016llx] [size=%llu bytes] "
993 "[mapped as %s] [unmapped as %s]\n",
994 ref->dev_addr, ref->size,
995 type2name[entry->type], type2name[ref->type]);
996 } else if (entry->type == dma_debug_coherent &&
997 ref->paddr != entry->paddr) {
998 err_printk(ref->dev, entry, "device driver frees "
999 "DMA memory with different CPU address "
1000 "[device address=0x%016llx] [size=%llu bytes] "
1001 "[cpu alloc address=0x%pa] "
1002 "[cpu free address=0x%pa]",
1003 ref->dev_addr, ref->size,
1008 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1009 ref->sg_call_ents != entry->sg_call_ents) {
1010 err_printk(ref->dev, entry, "device driver frees "
1011 "DMA sg list with different entry count "
1012 "[map count=%d] [unmap count=%d]\n",
1013 entry->sg_call_ents, ref->sg_call_ents);
1017 * This may be no bug in reality - but most implementations of the
1018 * DMA API don't handle this properly, so check for it here
1020 if (ref->direction != entry->direction) {
1021 err_printk(ref->dev, entry, "device driver frees "
1022 "DMA memory with different direction "
1023 "[device address=0x%016llx] [size=%llu bytes] "
1024 "[mapped with %s] [unmapped with %s]\n",
1025 ref->dev_addr, ref->size,
1026 dir2name[entry->direction],
1027 dir2name[ref->direction]);
1031 * Drivers should use dma_mapping_error() to check the returned
1032 * addresses of dma_map_single() and dma_map_page().
1033 * If not, print this warning message. See Documentation/core-api/dma-api.rst.
1035 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1036 err_printk(ref->dev, entry,
1037 "device driver failed to check map error"
1038 "[device address=0x%016llx] [size=%llu bytes] "
1040 ref->dev_addr, ref->size,
1041 type2name[entry->type]);
1044 hash_bucket_del(entry);
1045 put_hash_bucket(bucket, flags);
1048 * Free the entry outside of bucket_lock to avoid ABBA deadlocks
1049 * between that and radix_lock.
1051 dma_entry_free(entry);
1054 static void check_for_stack(struct device *dev,
1055 struct page *page, size_t offset)
1058 struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1060 if (!stack_vm_area) {
1061 /* Stack is direct-mapped. */
1062 if (PageHighMem(page))
1064 addr = page_address(page) + offset;
1065 if (object_is_on_stack(addr))
1066 err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
1068 /* Stack is vmalloced. */
1071 for (i = 0; i < stack_vm_area->nr_pages; i++) {
1072 if (page != stack_vm_area->pages[i])
1075 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1076 err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
1082 static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1084 if (memory_intersects(_stext, _etext, addr, len) ||
1085 memory_intersects(__start_rodata, __end_rodata, addr, len))
1086 err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1089 static void check_sync(struct device *dev,
1090 struct dma_debug_entry *ref,
1093 struct dma_debug_entry *entry;
1094 struct hash_bucket *bucket;
1095 unsigned long flags;
1097 bucket = get_hash_bucket(ref, &flags);
1099 entry = bucket_find_contain(&bucket, ref, &flags);
1102 err_printk(dev, NULL, "device driver tries "
1103 "to sync DMA memory it has not allocated "
1104 "[device address=0x%016llx] [size=%llu bytes]\n",
1105 (unsigned long long)ref->dev_addr, ref->size);
1109 if (ref->size > entry->size) {
1110 err_printk(dev, entry, "device driver syncs"
1111 " DMA memory outside allocated range "
1112 "[device address=0x%016llx] "
1113 "[allocation size=%llu bytes] "
1114 "[sync offset+size=%llu]\n",
1115 entry->dev_addr, entry->size,
1119 if (entry->direction == DMA_BIDIRECTIONAL)
1122 if (ref->direction != entry->direction) {
1123 err_printk(dev, entry, "device driver syncs "
1124 "DMA memory with different direction "
1125 "[device address=0x%016llx] [size=%llu bytes] "
1126 "[mapped with %s] [synced with %s]\n",
1127 (unsigned long long)ref->dev_addr, entry->size,
1128 dir2name[entry->direction],
1129 dir2name[ref->direction]);
1132 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1133 !(ref->direction == DMA_TO_DEVICE))
1134 err_printk(dev, entry, "device driver syncs "
1135 "device read-only DMA memory for cpu "
1136 "[device address=0x%016llx] [size=%llu bytes] "
1137 "[mapped with %s] [synced with %s]\n",
1138 (unsigned long long)ref->dev_addr, entry->size,
1139 dir2name[entry->direction],
1140 dir2name[ref->direction]);
1142 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1143 !(ref->direction == DMA_FROM_DEVICE))
1144 err_printk(dev, entry, "device driver syncs "
1145 "device write-only DMA memory to device "
1146 "[device address=0x%016llx] [size=%llu bytes] "
1147 "[mapped with %s] [synced with %s]\n",
1148 (unsigned long long)ref->dev_addr, entry->size,
1149 dir2name[entry->direction],
1150 dir2name[ref->direction]);
1152 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1153 ref->sg_call_ents != entry->sg_call_ents) {
1154 err_printk(ref->dev, entry, "device driver syncs "
1155 "DMA sg list with different entry count "
1156 "[map count=%d] [sync count=%d]\n",
1157 entry->sg_call_ents, ref->sg_call_ents);
1161 put_hash_bucket(bucket, flags);
1164 static void check_sg_segment(struct device *dev, struct scatterlist *sg)
1166 unsigned int max_seg = dma_get_max_seg_size(dev);
1167 u64 start, end, boundary = dma_get_seg_boundary(dev);
1170 * Either the driver forgot to set dma_parms appropriately, or
1171 * whoever generated the list forgot to check them.
1173 if (sg->length > max_seg)
1174 err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1175 sg->length, max_seg);
1177 * In some cases this could potentially be the DMA API
1178 * implementation's fault, but it would usually imply that
1179 * the scatterlist was built inappropriately to begin with.
1181 start = sg_dma_address(sg);
1182 end = start + sg_dma_len(sg) - 1;
1183 if ((start ^ end) & ~boundary)
1184 err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1185 start, end, boundary);
1188 void debug_dma_map_single(struct device *dev, const void *addr,
1191 if (unlikely(dma_debug_disabled()))
1194 if (!virt_addr_valid(addr))
1195 err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1198 if (is_vmalloc_addr(addr))
1199 err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1202 EXPORT_SYMBOL(debug_dma_map_single);
1204 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1205 size_t size, int direction, dma_addr_t dma_addr,
1206 unsigned long attrs)
1208 struct dma_debug_entry *entry;
1210 if (unlikely(dma_debug_disabled()))
1213 if (dma_mapping_error(dev, dma_addr))
1216 entry = dma_entry_alloc();
1221 entry->type = dma_debug_single;
1222 entry->paddr = page_to_phys(page) + offset;
1223 entry->dev_addr = dma_addr;
1225 entry->direction = direction;
1226 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1228 check_for_stack(dev, page, offset);
1230 if (!PageHighMem(page)) {
1231 void *addr = page_address(page) + offset;
1233 check_for_illegal_area(dev, addr, size);
1236 add_dma_entry(entry, attrs);
1239 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1241 struct dma_debug_entry ref;
1242 struct dma_debug_entry *entry;
1243 struct hash_bucket *bucket;
1244 unsigned long flags;
1246 if (unlikely(dma_debug_disabled()))
1250 ref.dev_addr = dma_addr;
1251 bucket = get_hash_bucket(&ref, &flags);
1253 list_for_each_entry(entry, &bucket->list, list) {
1254 if (!exact_match(&ref, entry))
1258 * The same physical address can be mapped multiple
1259 * times. Without a hardware IOMMU this results in the
1260 * same device addresses being put into the dma-debug
1261 * hash multiple times too. This can result in false
1262 * positives being reported. Therefore we implement a
1263 * best-fit algorithm here which updates the first entry
1264 * from the hash which fits the reference value and is
1265 * not currently listed as being checked.
1267 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1268 entry->map_err_type = MAP_ERR_CHECKED;
1273 put_hash_bucket(bucket, flags);
1275 EXPORT_SYMBOL(debug_dma_mapping_error);
1277 void debug_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
1278 size_t size, int direction)
1280 struct dma_debug_entry ref = {
1281 .type = dma_debug_single,
1283 .dev_addr = dma_addr,
1285 .direction = direction,
1288 if (unlikely(dma_debug_disabled()))
1293 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1294 int nents, int mapped_ents, int direction,
1295 unsigned long attrs)
1297 struct dma_debug_entry *entry;
1298 struct scatterlist *s;
1301 if (unlikely(dma_debug_disabled()))
1304 for_each_sg(sg, s, nents, i) {
1305 check_for_stack(dev, sg_page(s), s->offset);
1306 if (!PageHighMem(sg_page(s)))
1307 check_for_illegal_area(dev, sg_virt(s), s->length);
1310 for_each_sg(sg, s, mapped_ents, i) {
1311 entry = dma_entry_alloc();
1315 entry->type = dma_debug_sg;
1317 entry->paddr = sg_phys(s);
1318 entry->size = sg_dma_len(s);
1319 entry->dev_addr = sg_dma_address(s);
1320 entry->direction = direction;
1321 entry->sg_call_ents = nents;
1322 entry->sg_mapped_ents = mapped_ents;
1324 check_sg_segment(dev, s);
1326 add_dma_entry(entry, attrs);
1330 static int get_nr_mapped_entries(struct device *dev,
1331 struct dma_debug_entry *ref)
1333 struct dma_debug_entry *entry;
1334 struct hash_bucket *bucket;
1335 unsigned long flags;
1338 bucket = get_hash_bucket(ref, &flags);
1339 entry = bucket_find_exact(bucket, ref);
1343 mapped_ents = entry->sg_mapped_ents;
1344 put_hash_bucket(bucket, flags);
1349 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1350 int nelems, int dir)
1352 struct scatterlist *s;
1353 int mapped_ents = 0, i;
1355 if (unlikely(dma_debug_disabled()))
1358 for_each_sg(sglist, s, nelems, i) {
1360 struct dma_debug_entry ref = {
1361 .type = dma_debug_sg,
1363 .paddr = sg_phys(s),
1364 .dev_addr = sg_dma_address(s),
1365 .size = sg_dma_len(s),
1367 .sg_call_ents = nelems,
1370 if (mapped_ents && i >= mapped_ents)
1374 mapped_ents = get_nr_mapped_entries(dev, &ref);
1380 static phys_addr_t virt_to_paddr(void *virt)
1384 if (is_vmalloc_addr(virt))
1385 page = vmalloc_to_page(virt);
1387 page = virt_to_page(virt);
1389 return page_to_phys(page) + offset_in_page(virt);
1392 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1393 dma_addr_t dma_addr, void *virt,
1394 unsigned long attrs)
1396 struct dma_debug_entry *entry;
1398 if (unlikely(dma_debug_disabled()))
1401 if (unlikely(virt == NULL))
1404 /* handle vmalloc and linear addresses */
1405 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1408 entry = dma_entry_alloc();
1412 entry->type = dma_debug_coherent;
1414 entry->paddr = virt_to_paddr(virt);
1416 entry->dev_addr = dma_addr;
1417 entry->direction = DMA_BIDIRECTIONAL;
1419 add_dma_entry(entry, attrs);
1422 void debug_dma_free_coherent(struct device *dev, size_t size,
1423 void *virt, dma_addr_t dma_addr)
1425 struct dma_debug_entry ref = {
1426 .type = dma_debug_coherent,
1428 .dev_addr = dma_addr,
1430 .direction = DMA_BIDIRECTIONAL,
1433 /* handle vmalloc and linear addresses */
1434 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1437 ref.paddr = virt_to_paddr(virt);
1439 if (unlikely(dma_debug_disabled()))
1445 void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1446 int direction, dma_addr_t dma_addr,
1447 unsigned long attrs)
1449 struct dma_debug_entry *entry;
1451 if (unlikely(dma_debug_disabled()))
1454 entry = dma_entry_alloc();
1458 entry->type = dma_debug_resource;
1460 entry->paddr = addr;
1462 entry->dev_addr = dma_addr;
1463 entry->direction = direction;
1464 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1466 add_dma_entry(entry, attrs);
1469 void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1470 size_t size, int direction)
1472 struct dma_debug_entry ref = {
1473 .type = dma_debug_resource,
1475 .dev_addr = dma_addr,
1477 .direction = direction,
1480 if (unlikely(dma_debug_disabled()))
1486 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1487 size_t size, int direction)
1489 struct dma_debug_entry ref;
1491 if (unlikely(dma_debug_disabled()))
1494 ref.type = dma_debug_single;
1496 ref.dev_addr = dma_handle;
1498 ref.direction = direction;
1499 ref.sg_call_ents = 0;
1501 check_sync(dev, &ref, true);
1504 void debug_dma_sync_single_for_device(struct device *dev,
1505 dma_addr_t dma_handle, size_t size,
1508 struct dma_debug_entry ref;
1510 if (unlikely(dma_debug_disabled()))
1513 ref.type = dma_debug_single;
1515 ref.dev_addr = dma_handle;
1517 ref.direction = direction;
1518 ref.sg_call_ents = 0;
1520 check_sync(dev, &ref, false);
1523 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1524 int nelems, int direction)
1526 struct scatterlist *s;
1527 int mapped_ents = 0, i;
1529 if (unlikely(dma_debug_disabled()))
1532 for_each_sg(sg, s, nelems, i) {
1534 struct dma_debug_entry ref = {
1535 .type = dma_debug_sg,
1537 .paddr = sg_phys(s),
1538 .dev_addr = sg_dma_address(s),
1539 .size = sg_dma_len(s),
1540 .direction = direction,
1541 .sg_call_ents = nelems,
1545 mapped_ents = get_nr_mapped_entries(dev, &ref);
1547 if (i >= mapped_ents)
1550 check_sync(dev, &ref, true);
1554 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1555 int nelems, int direction)
1557 struct scatterlist *s;
1558 int mapped_ents = 0, i;
1560 if (unlikely(dma_debug_disabled()))
1563 for_each_sg(sg, s, nelems, i) {
1565 struct dma_debug_entry ref = {
1566 .type = dma_debug_sg,
1568 .paddr = sg_phys(sg),
1569 .dev_addr = sg_dma_address(s),
1570 .size = sg_dma_len(s),
1571 .direction = direction,
1572 .sg_call_ents = nelems,
1575 mapped_ents = get_nr_mapped_entries(dev, &ref);
1577 if (i >= mapped_ents)
1580 check_sync(dev, &ref, false);
1584 static int __init dma_debug_driver_setup(char *str)
1588 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1589 current_driver_name[i] = *str;
1594 if (current_driver_name[0])
1595 pr_info("enable driver filter for driver [%s]\n",
1596 current_driver_name);
1601 __setup("dma_debug_driver=", dma_debug_driver_setup);