2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <joerg.roedel@amd.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/stacktrace.h>
23 #include <linux/dma-debug.h>
24 #include <linux/spinlock.h>
25 #include <linux/debugfs.h>
26 #include <linux/uaccess.h>
27 #include <linux/export.h>
28 #include <linux/device.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/ctype.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
35 #include <asm/sections.h>
37 #define HASH_SIZE 1024ULL
38 #define HASH_FN_SHIFT 13
39 #define HASH_FN_MASK (HASH_SIZE - 1)
49 MAP_ERR_CHECK_NOT_APPLICABLE,
54 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
56 struct dma_debug_entry {
57 struct list_head list;
66 enum map_err_types map_err_type;
67 #ifdef CONFIG_STACKTRACE
68 struct stack_trace stacktrace;
69 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
73 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
76 struct list_head list;
78 } ____cacheline_aligned_in_smp;
80 /* Hash list to save the allocated dma addresses */
81 static struct hash_bucket dma_entry_hash[HASH_SIZE];
82 /* List of pre-allocated dma_debug_entry's */
83 static LIST_HEAD(free_entries);
84 /* Lock for the list above */
85 static DEFINE_SPINLOCK(free_entries_lock);
87 /* Global disable flag - will be set in case of an error */
88 static u32 global_disable __read_mostly;
90 * The global_disable flag can be set in kernel command line, and
91 * dma_debug_init() is called much later than that. Mappings can happen before
92 * the init function is called, and thus no memory has been reserved for the
93 * entries. This out-of-memory situation flips enables the global_disable flag,
94 * which cannot then be enabled again. Use another flag for skipping the
95 * tracking if init hasn't been done yet.
97 static bool initialized __read_mostly;
99 /* Global error count */
100 static u32 error_count;
102 /* Global error show enable*/
103 static u32 show_all_errors __read_mostly;
104 /* Number of errors to show */
105 static u32 show_num_errors = 1;
107 static u32 num_free_entries;
108 static u32 min_free_entries;
109 static u32 nr_total_entries;
111 /* number of preallocated entries requested by kernel cmdline */
112 static u32 req_entries;
114 /* debugfs dentry's for the stuff above */
115 static struct dentry *dma_debug_dent __read_mostly;
116 static struct dentry *global_disable_dent __read_mostly;
117 static struct dentry *error_count_dent __read_mostly;
118 static struct dentry *show_all_errors_dent __read_mostly;
119 static struct dentry *show_num_errors_dent __read_mostly;
120 static struct dentry *num_free_entries_dent __read_mostly;
121 static struct dentry *min_free_entries_dent __read_mostly;
122 static struct dentry *filter_dent __read_mostly;
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[4] = { "single", "page",
140 "scather-gather", "coherent" };
142 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
143 "DMA_FROM_DEVICE", "DMA_NONE" };
145 /* dma statistics per device */
146 struct dma_dev_info {
147 struct list_head list;
149 spinlock_t lock; /* Protects dma_dev_info itself */
155 int current_alloc_size;
156 int total_alloc_size;
160 static LIST_HEAD(dev_info_list);
161 static DEFINE_SPINLOCK(dev_info_lock); /* Protects dev_info_list */
164 * The access to some variables in this macro is racy. We can't use atomic_t
165 * here because all these variables are exported to debugfs. Some of them even
166 * writeable. This is also the reason why a lock won't help much. But anyway,
167 * the races are no big deal. Here is why:
169 * error_count: the addition is racy, but the worst thing that can happen is
170 * that we don't count some errors
171 * show_num_errors: the subtraction is racy. Also no big deal because in
172 * worst case this will result in one warning more in the
173 * system log than the user configured. This variable is
174 * writeable via debugfs.
176 static inline void dump_entry_trace(struct dma_debug_entry *entry)
178 #ifdef CONFIG_STACKTRACE
180 pr_warning("Mapped at:\n");
181 print_stack_trace(&entry->stacktrace, 0);
186 static bool driver_filter(struct device *dev)
188 struct device_driver *drv;
192 /* driver filter off */
193 if (likely(!current_driver_name[0]))
196 /* driver filter on and initialized */
197 if (current_driver && dev && dev->driver == current_driver)
200 /* driver filter on, but we can't filter on a NULL device... */
204 if (current_driver || !current_driver_name[0])
207 /* driver filter on but not yet initialized */
212 /* lock to protect against change of current_driver_name */
213 read_lock_irqsave(&driver_name_lock, flags);
217 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
218 current_driver = drv;
222 read_unlock_irqrestore(&driver_name_lock, flags);
227 #define err_printk(dev, entry, format, arg...) do { \
229 if (driver_filter(dev) && \
230 (show_all_errors || show_num_errors > 0)) { \
231 WARN(1, "%s %s: " format, \
232 dev ? dev_driver_string(dev) : "NULL", \
233 dev ? dev_name(dev) : "NULL", ## arg); \
234 dump_entry_trace(entry); \
236 if (!show_all_errors && show_num_errors > 0) \
237 show_num_errors -= 1; \
241 * Hash related functions
243 * Every DMA-API request is saved into a struct dma_debug_entry. To
244 * have quick access to these structs they are stored into a hash.
246 static int hash_fn(struct dma_debug_entry *entry)
249 * Hash function is based on the dma address.
250 * We use bits 20-27 here as the index into the hash
252 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
256 * Request exclusive access to a hash bucket for a given dma_debug_entry.
258 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
259 unsigned long *flags)
261 int idx = hash_fn(entry);
262 unsigned long __flags;
264 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
266 return &dma_entry_hash[idx];
270 * Give up exclusive access to the hash bucket
272 static void put_hash_bucket(struct hash_bucket *bucket,
273 unsigned long *flags)
275 unsigned long __flags = *flags;
277 spin_unlock_irqrestore(&bucket->lock, __flags);
280 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
282 return ((a->dev_addr == b->dev_addr) &&
283 (a->dev == b->dev)) ? true : false;
286 static bool containing_match(struct dma_debug_entry *a,
287 struct dma_debug_entry *b)
289 if (a->dev != b->dev)
292 if ((b->dev_addr <= a->dev_addr) &&
293 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
300 * Search a given entry in the hash bucket list
302 static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
303 struct dma_debug_entry *ref,
306 struct dma_debug_entry *entry, *ret = NULL;
307 int matches = 0, match_lvl, last_lvl = -1;
309 list_for_each_entry(entry, &bucket->list, list) {
310 if (!match(ref, entry))
314 * Some drivers map the same physical address multiple
315 * times. Without a hardware IOMMU this results in the
316 * same device addresses being put into the dma-debug
317 * hash multiple times too. This can result in false
318 * positives being reported. Therefore we implement a
319 * best-fit algorithm here which returns the entry from
320 * the hash which fits best to the reference value
321 * instead of the first-fit.
325 entry->size == ref->size ? ++match_lvl : 0;
326 entry->type == ref->type ? ++match_lvl : 0;
327 entry->direction == ref->direction ? ++match_lvl : 0;
328 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
330 if (match_lvl == 4) {
331 /* perfect-fit - return the result */
333 } else if (match_lvl > last_lvl) {
335 * We found an entry that fits better then the
336 * previous one or it is the 1st match.
338 last_lvl = match_lvl;
344 * If we have multiple matches but no perfect-fit, just return
347 ret = (matches == 1) ? ret : NULL;
352 static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
353 struct dma_debug_entry *ref)
355 return __hash_bucket_find(bucket, ref, exact_match);
358 static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
359 struct dma_debug_entry *ref,
360 unsigned long *flags)
363 unsigned int max_range = dma_get_max_seg_size(ref->dev);
364 struct dma_debug_entry *entry, index = *ref;
365 unsigned int range = 0;
367 while (range <= max_range) {
368 entry = __hash_bucket_find(*bucket, &index, containing_match);
374 * Nothing found, go back a hash bucket
376 put_hash_bucket(*bucket, flags);
377 range += (1 << HASH_FN_SHIFT);
378 index.dev_addr -= (1 << HASH_FN_SHIFT);
379 *bucket = get_hash_bucket(&index, flags);
386 * Add an entry to a hash bucket
388 static void hash_bucket_add(struct hash_bucket *bucket,
389 struct dma_debug_entry *entry)
391 list_add_tail(&entry->list, &bucket->list);
395 * Remove entry from a hash bucket list
397 static void hash_bucket_del(struct dma_debug_entry *entry)
399 list_del(&entry->list);
403 * Dump mapping entries for debugging purposes
405 void debug_dma_dump_mappings(struct device *dev)
409 for (idx = 0; idx < HASH_SIZE; idx++) {
410 struct hash_bucket *bucket = &dma_entry_hash[idx];
411 struct dma_debug_entry *entry;
414 spin_lock_irqsave(&bucket->lock, flags);
416 list_for_each_entry(entry, &bucket->list, list) {
417 if (!dev || dev == entry->dev) {
419 "%s idx %d P=%Lx D=%Lx L=%Lx %s %s\n",
420 type2name[entry->type], idx,
421 (unsigned long long)entry->paddr,
422 entry->dev_addr, entry->size,
423 dir2name[entry->direction],
424 maperr2str[entry->map_err_type]);
428 spin_unlock_irqrestore(&bucket->lock, flags);
431 EXPORT_SYMBOL(debug_dma_dump_mappings);
434 * device info snapshot updating functions
436 static void ____dev_info_incr(struct dma_dev_info *info,
437 struct dma_debug_entry *entry)
441 spin_lock_irqsave(&info->lock, flags);
443 info->current_allocs++;
444 info->total_allocs++;
445 if (info->current_allocs > info->max_allocs)
446 info->max_allocs = info->current_allocs;
448 info->current_alloc_size += entry->size;
449 info->total_alloc_size += entry->size;
450 if (info->current_alloc_size > info->max_alloc_size)
451 info->max_alloc_size = info->current_alloc_size;
453 spin_unlock_irqrestore(&info->lock, flags);
456 static void ____dev_info_decr(struct dma_dev_info *info,
457 struct dma_debug_entry *entry)
461 spin_lock_irqsave(&info->lock, flags);
463 info->current_allocs--;
464 info->current_alloc_size -= entry->size;
466 spin_unlock_irqrestore(&info->lock, flags);
469 static void __dev_info_fn(struct dma_debug_entry *entry,
470 void (*fn)(struct dma_dev_info *, struct dma_debug_entry *))
472 struct dma_dev_info *info;
475 spin_lock_irqsave(&dev_info_lock, flags);
477 list_for_each_entry(info, &dev_info_list, list)
478 if (info->dev == entry->dev)
481 info = kzalloc(sizeof(*info), GFP_KERNEL);
483 dev_err(entry->dev, "Out of memory at %s\n", __func__);
484 spin_unlock_irqrestore(&dev_info_lock, flags);
488 spin_lock_init(&info->lock);
489 info->dev = entry->dev;
490 list_add(&info->list, &dev_info_list);
492 spin_unlock_irqrestore(&dev_info_lock, flags);
496 static inline void dev_info_alloc(struct dma_debug_entry *entry)
498 __dev_info_fn(entry, ____dev_info_incr);
501 static inline void dev_info_free(struct dma_debug_entry *entry)
503 __dev_info_fn(entry, ____dev_info_decr);
507 * Wrapper function for adding an entry to the hash.
508 * This function takes care of locking itself.
510 static void add_dma_entry(struct dma_debug_entry *entry)
512 struct hash_bucket *bucket;
515 bucket = get_hash_bucket(entry, &flags);
516 hash_bucket_add(bucket, entry);
517 put_hash_bucket(bucket, &flags);
519 dev_info_alloc(entry);
522 static struct dma_debug_entry *__dma_entry_alloc(void)
524 struct dma_debug_entry *entry;
526 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
527 list_del(&entry->list);
528 memset(entry, 0, sizeof(*entry));
530 num_free_entries -= 1;
531 if (num_free_entries < min_free_entries)
532 min_free_entries = num_free_entries;
537 /* struct dma_entry allocator
539 * The next two functions implement the allocator for
540 * struct dma_debug_entries.
542 static struct dma_debug_entry *dma_entry_alloc(void)
544 struct dma_debug_entry *entry;
547 spin_lock_irqsave(&free_entries_lock, flags);
549 if (list_empty(&free_entries)) {
550 pr_err("DMA-API: debugging out of memory - disabling\n");
551 global_disable = true;
552 spin_unlock_irqrestore(&free_entries_lock, flags);
556 entry = __dma_entry_alloc();
558 spin_unlock_irqrestore(&free_entries_lock, flags);
560 #ifdef CONFIG_STACKTRACE
561 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
562 entry->stacktrace.entries = entry->st_entries;
563 entry->stacktrace.skip = 2;
564 save_stack_trace(&entry->stacktrace);
570 static void dma_entry_free(struct dma_debug_entry *entry)
575 * add to beginning of the list - this way the entries are
576 * more likely cache hot when they are reallocated.
578 spin_lock_irqsave(&free_entries_lock, flags);
579 list_add(&entry->list, &free_entries);
580 num_free_entries += 1;
581 spin_unlock_irqrestore(&free_entries_lock, flags);
584 int dma_debug_resize_entries(u32 num_entries)
586 int i, delta, ret = 0;
588 struct dma_debug_entry *entry;
591 spin_lock_irqsave(&free_entries_lock, flags);
593 if (nr_total_entries < num_entries) {
594 delta = num_entries - nr_total_entries;
596 spin_unlock_irqrestore(&free_entries_lock, flags);
598 for (i = 0; i < delta; i++) {
599 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
603 list_add_tail(&entry->list, &tmp);
606 spin_lock_irqsave(&free_entries_lock, flags);
608 list_splice(&tmp, &free_entries);
609 nr_total_entries += i;
610 num_free_entries += i;
612 delta = nr_total_entries - num_entries;
614 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
615 entry = __dma_entry_alloc();
619 nr_total_entries -= i;
622 if (nr_total_entries != num_entries)
625 spin_unlock_irqrestore(&free_entries_lock, flags);
629 EXPORT_SYMBOL(dma_debug_resize_entries);
632 * DMA-API debugging init code
634 * The init code does two things:
635 * 1. Initialize core data structures
636 * 2. Preallocate a given number of dma_debug_entry structs
639 static int prealloc_memory(u32 num_entries)
641 struct dma_debug_entry *entry, *next_entry;
644 for (i = 0; i < num_entries; ++i) {
645 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
649 list_add_tail(&entry->list, &free_entries);
652 num_free_entries = num_entries;
653 min_free_entries = num_entries;
655 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
661 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
662 list_del(&entry->list);
669 static ssize_t filter_read(struct file *file, char __user *user_buf,
670 size_t count, loff_t *ppos)
672 char buf[NAME_MAX_LEN + 1];
676 if (!current_driver_name[0])
680 * We can't copy to userspace directly because current_driver_name can
681 * only be read under the driver_name_lock with irqs disabled. So
682 * create a temporary copy first.
684 read_lock_irqsave(&driver_name_lock, flags);
685 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
686 read_unlock_irqrestore(&driver_name_lock, flags);
688 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
691 static ssize_t filter_write(struct file *file, const char __user *userbuf,
692 size_t count, loff_t *ppos)
694 char buf[NAME_MAX_LEN];
700 * We can't copy from userspace directly. Access to
701 * current_driver_name is protected with a write_lock with irqs
702 * disabled. Since copy_from_user can fault and may sleep we
703 * need to copy to temporary buffer first
705 len = min(count, (size_t)(NAME_MAX_LEN - 1));
706 if (copy_from_user(buf, userbuf, len))
711 write_lock_irqsave(&driver_name_lock, flags);
714 * Now handle the string we got from userspace very carefully.
716 * - only use the first token we got
717 * - token delimiter is everything looking like a space
718 * character (' ', '\n', '\t' ...)
721 if (!isalnum(buf[0])) {
723 * If the first character userspace gave us is not
724 * alphanumerical then assume the filter should be
727 if (current_driver_name[0])
728 pr_info("DMA-API: switching off dma-debug driver filter\n");
729 current_driver_name[0] = 0;
730 current_driver = NULL;
735 * Now parse out the first token and use it as the name for the
736 * driver to filter for.
738 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
739 current_driver_name[i] = buf[i];
740 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
743 current_driver_name[i] = 0;
744 current_driver = NULL;
746 pr_info("DMA-API: enable driver filter for driver [%s]\n",
747 current_driver_name);
750 write_unlock_irqrestore(&driver_name_lock, flags);
755 static inline void seq_print_ip_sym(struct seq_file *s, unsigned long ip)
757 seq_printf(s, "[<%p>] %pS\n", (void *)ip, (void *)ip);
760 void seq_print_trace(struct seq_file *s, struct stack_trace *trace)
764 if (WARN_ON(!trace->entries))
767 for (i = trace->skip; i < trace->nr_entries; i++)
768 seq_print_ip_sym(s, trace->entries[i]);
772 * Print all map entries just in the order they are stored. We assume that the
773 * user will be able to parse this later anyway. Detailed output includes stack
774 * traces of allocations.
776 void seq_print_dma_mappings(struct seq_file *s, int detail)
780 for (idx = 0; idx < HASH_SIZE; idx++) {
781 struct hash_bucket *bucket = &dma_entry_hash[idx];
782 struct dma_debug_entry *entry;
785 spin_lock_irqsave(&bucket->lock, flags);
787 list_for_each_entry(entry, &bucket->list, list) {
789 " %s %s idx %d P=%llx D=%llx L=%llx %s A=%s\n",
790 dev_name(entry->dev),
791 type2name[entry->type], idx,
793 entry->dev_addr, entry->size,
794 dir2name[entry->direction],
795 debug_dma_platformdata(entry->dev));
798 seq_print_trace(s, &entry->stacktrace);
801 spin_unlock_irqrestore(&bucket->lock, flags);
805 void __weak dma_debugfs_platform_info(struct dentry *dent)
809 static int _dump_allocs(struct seq_file *s, void *data)
811 int detail = (int)s->private;
813 seq_print_dma_mappings(s, detail);
817 static int _dump_dev_info(struct seq_file *s, void *data)
819 struct dma_dev_info *i;
822 spin_lock_irqsave(&dev_info_lock, flags);
824 list_for_each_entry(i, &dev_info_list, list)
826 "dev=%s curallocs=%d totallocs=%d maxallocs=%d cursize=%d totsize=%d maxsize=%d\n",
827 dev_name(i->dev), i->current_allocs, i->total_allocs,
828 i->max_allocs, i->current_alloc_size,
829 i->total_alloc_size, i->max_alloc_size);
831 spin_unlock_irqrestore(&dev_info_lock, flags);
835 #define DEFINE_DEBUGFS(__name, __func, __data) \
836 static int __name ## _open(struct inode *inode, struct file *file) \
838 return single_open(file, __func, __data); \
840 static const struct file_operations __name ## _fops = { \
841 .open = __name ## _open, \
843 .llseek = seq_lseek, \
844 .release = single_release, \
847 DEFINE_DEBUGFS(_dump_allocs, _dump_allocs, NULL);
848 DEFINE_DEBUGFS(_dump_allocs_detail, _dump_allocs, (void *)1);
849 DEFINE_DEBUGFS(_dump_dev_info, _dump_dev_info, NULL);
850 #undef DEFINE_DEBUGFS
852 static int map_dump_debug_fs_init(void)
854 #define CREATE_FILE(name) \
855 debugfs_create_file(#name, S_IRUGO, \
856 dma_debug_dent, NULL, \
859 if (!CREATE_FILE(dump_allocs))
862 if (!CREATE_FILE(dump_allocs_detail))
865 if (!CREATE_FILE(dump_dev_info))
870 dma_debugfs_platform_info(dma_debug_dent);
874 static const struct file_operations filter_fops = {
876 .write = filter_write,
877 .llseek = default_llseek,
880 static int dma_debug_fs_init(void)
882 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
883 if (!dma_debug_dent) {
884 pr_err("DMA-API: can not create debugfs directory\n");
888 global_disable_dent = debugfs_create_bool("disabled", 0444,
891 if (!global_disable_dent)
894 error_count_dent = debugfs_create_u32("error_count", 0444,
895 dma_debug_dent, &error_count);
896 if (!error_count_dent)
899 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
902 if (!show_all_errors_dent)
905 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
908 if (!show_num_errors_dent)
911 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
914 if (!num_free_entries_dent)
917 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
920 if (!min_free_entries_dent)
923 filter_dent = debugfs_create_file("driver_filter", 0644,
924 dma_debug_dent, NULL, &filter_fops);
928 if (map_dump_debug_fs_init())
934 debugfs_remove_recursive(dma_debug_dent);
939 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
941 struct dma_debug_entry *entry;
945 local_irq_save(flags);
947 for (i = 0; i < HASH_SIZE; ++i) {
948 spin_lock(&dma_entry_hash[i].lock);
949 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
950 if (entry->dev == dev) {
955 spin_unlock(&dma_entry_hash[i].lock);
958 local_irq_restore(flags);
963 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
965 struct device *dev = data;
966 struct dma_debug_entry *uninitialized_var(entry);
969 if (!initialized || global_disable)
973 case BUS_NOTIFY_UNBOUND_DRIVER:
974 count = device_dma_allocations(dev, &entry);
977 err_printk(dev, entry, "DMA-API: device driver has pending "
978 "DMA allocations while released from device "
980 "One of leaked entries details: "
981 "[device address=0x%016llx] [size=%llu bytes] "
982 "[mapped with %s] [mapped as %s]\n",
983 count, entry->dev_addr, entry->size,
984 dir2name[entry->direction], type2name[entry->type]);
993 void dma_debug_add_bus(struct bus_type *bus)
995 struct notifier_block *nb;
997 if (!initialized || global_disable)
1000 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1002 pr_err("dma_debug_add_bus: out of memory\n");
1006 nb->notifier_call = dma_debug_device_change;
1008 bus_register_notifier(bus, nb);
1012 * Let the architectures decide how many entries should be preallocated.
1014 void dma_debug_init(u32 num_entries)
1021 for (i = 0; i < HASH_SIZE; ++i) {
1022 INIT_LIST_HEAD(&dma_entry_hash[i].list);
1023 spin_lock_init(&dma_entry_hash[i].lock);
1026 if (dma_debug_fs_init() != 0) {
1027 pr_err("DMA-API: error creating debugfs entries - disabling\n");
1028 global_disable = true;
1034 num_entries = req_entries;
1036 if (prealloc_memory(num_entries) != 0) {
1037 pr_err("DMA-API: debugging out of memory error - disabled\n");
1038 global_disable = true;
1043 nr_total_entries = num_free_entries;
1045 pr_info("DMA-API: debugging enabled by kernel config\n");
1049 static __init int dma_debug_cmdline(char *str)
1054 if (strncmp(str, "off", 3) == 0) {
1055 pr_info("DMA-API: debugging disabled on kernel command line\n");
1056 global_disable = true;
1062 static __init int dma_debug_entries_cmdline(char *str)
1069 res = get_option(&str, &req_entries);
1077 __setup("dma_debug=", dma_debug_cmdline);
1078 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
1080 static void check_unmap(struct dma_debug_entry *ref)
1082 struct dma_debug_entry *entry;
1083 struct hash_bucket *bucket;
1084 unsigned long flags;
1086 bucket = get_hash_bucket(ref, &flags);
1087 entry = bucket_find_exact(bucket, ref);
1090 /* must drop lock before calling dma_mapping_error */
1091 put_hash_bucket(bucket, &flags);
1093 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1094 err_printk(ref->dev, NULL,
1095 "DMA-API: device driver tries to free an "
1096 "invalid DMA memory address\n");
1098 err_printk(ref->dev, NULL,
1099 "DMA-API: device driver tries to free DMA "
1100 "memory it has not allocated [device "
1101 "address=0x%016llx] [size=%llu bytes]\n",
1102 ref->dev_addr, ref->size);
1107 if (ref->size != entry->size) {
1108 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1109 "DMA memory with different size "
1110 "[device address=0x%016llx] [map size=%llu bytes] "
1111 "[unmap size=%llu bytes]\n",
1112 ref->dev_addr, entry->size, ref->size);
1115 if (ref->type != entry->type) {
1116 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1117 "DMA memory with wrong function "
1118 "[device address=0x%016llx] [size=%llu bytes] "
1119 "[mapped as %s] [unmapped as %s]\n",
1120 ref->dev_addr, ref->size,
1121 type2name[entry->type], type2name[ref->type]);
1122 } else if ((entry->type == dma_debug_coherent) &&
1123 (ref->paddr != entry->paddr)) {
1124 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1125 "DMA memory with different CPU address "
1126 "[device address=0x%016llx] [size=%llu bytes] "
1127 "[cpu alloc address=0x%016llx] "
1128 "[cpu free address=0x%016llx]",
1129 ref->dev_addr, ref->size,
1130 (unsigned long long)entry->paddr,
1131 (unsigned long long)ref->paddr);
1134 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1135 ref->sg_call_ents != entry->sg_call_ents) {
1136 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1137 "DMA sg list with different entry count "
1138 "[map count=%d] [unmap count=%d]\n",
1139 entry->sg_call_ents, ref->sg_call_ents);
1143 * This may be no bug in reality - but most implementations of the
1144 * DMA API don't handle this properly, so check for it here
1146 if (ref->direction != entry->direction) {
1147 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1148 "DMA memory with different direction "
1149 "[device address=0x%016llx] [size=%llu bytes] "
1150 "[mapped with %s] [unmapped with %s]\n",
1151 ref->dev_addr, ref->size,
1152 dir2name[entry->direction],
1153 dir2name[ref->direction]);
1156 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1157 err_printk(ref->dev, entry,
1158 "DMA-API: device driver failed to check map error"
1159 "[device address=0x%016llx] [size=%llu bytes] "
1161 ref->dev_addr, ref->size,
1162 type2name[entry->type]);
1165 dev_info_free(entry);
1167 hash_bucket_del(entry);
1168 dma_entry_free(entry);
1170 put_hash_bucket(bucket, &flags);
1173 static void check_for_stack(struct device *dev, void *addr)
1175 if (object_is_on_stack(addr))
1176 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
1177 "stack [addr=%p]\n", addr);
1180 static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
1182 unsigned long a1 = (unsigned long)addr;
1183 unsigned long b1 = a1 + len;
1184 unsigned long a2 = (unsigned long)start;
1185 unsigned long b2 = (unsigned long)end;
1187 return !(b1 <= a2 || a1 >= b2);
1190 static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1192 if (overlap(addr, len, _text, _etext) ||
1193 overlap(addr, len, __start_rodata, __end_rodata))
1194 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1197 static void check_sync(struct device *dev,
1198 struct dma_debug_entry *ref,
1201 struct dma_debug_entry *entry;
1202 struct hash_bucket *bucket;
1203 unsigned long flags;
1205 bucket = get_hash_bucket(ref, &flags);
1207 entry = bucket_find_contain(&bucket, ref, &flags);
1210 err_printk(dev, NULL, "DMA-API: device driver tries "
1211 "to sync DMA memory it has not allocated "
1212 "[device address=0x%016llx] [size=%llu bytes]\n",
1213 (unsigned long long)ref->dev_addr, ref->size);
1217 if (ref->size > entry->size) {
1218 err_printk(dev, entry, "DMA-API: device driver syncs"
1219 " DMA memory outside allocated range "
1220 "[device address=0x%016llx] "
1221 "[allocation size=%llu bytes] "
1222 "[sync offset+size=%llu]\n",
1223 entry->dev_addr, entry->size,
1227 if (entry->direction == DMA_BIDIRECTIONAL)
1230 if (ref->direction != entry->direction) {
1231 err_printk(dev, entry, "DMA-API: device driver syncs "
1232 "DMA memory with different direction "
1233 "[device address=0x%016llx] [size=%llu bytes] "
1234 "[mapped with %s] [synced with %s]\n",
1235 (unsigned long long)ref->dev_addr, entry->size,
1236 dir2name[entry->direction],
1237 dir2name[ref->direction]);
1240 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1241 !(ref->direction == DMA_TO_DEVICE))
1242 err_printk(dev, entry, "DMA-API: device driver syncs "
1243 "device read-only DMA memory for cpu "
1244 "[device address=0x%016llx] [size=%llu bytes] "
1245 "[mapped with %s] [synced with %s]\n",
1246 (unsigned long long)ref->dev_addr, entry->size,
1247 dir2name[entry->direction],
1248 dir2name[ref->direction]);
1250 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1251 !(ref->direction == DMA_FROM_DEVICE))
1252 err_printk(dev, entry, "DMA-API: device driver syncs "
1253 "device write-only DMA memory to device "
1254 "[device address=0x%016llx] [size=%llu bytes] "
1255 "[mapped with %s] [synced with %s]\n",
1256 (unsigned long long)ref->dev_addr, entry->size,
1257 dir2name[entry->direction],
1258 dir2name[ref->direction]);
1261 put_hash_bucket(bucket, &flags);
1264 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1265 size_t size, int direction, dma_addr_t dma_addr,
1268 struct dma_debug_entry *entry;
1270 if (unlikely(!initialized || global_disable))
1273 if (dma_mapping_error(dev, dma_addr))
1276 entry = dma_entry_alloc();
1281 entry->type = dma_debug_page;
1282 entry->paddr = page_to_phys(page) + offset;
1283 entry->dev_addr = dma_addr;
1285 entry->direction = direction;
1286 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1289 entry->type = dma_debug_single;
1291 if (!PageHighMem(page)) {
1292 void *addr = page_address(page) + offset;
1294 check_for_stack(dev, addr);
1295 check_for_illegal_area(dev, addr, size);
1298 add_dma_entry(entry);
1300 EXPORT_SYMBOL(debug_dma_map_page);
1302 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1304 struct dma_debug_entry ref;
1305 struct dma_debug_entry *entry;
1306 struct hash_bucket *bucket;
1307 unsigned long flags;
1309 if (unlikely(!initialized || global_disable))
1313 ref.dev_addr = dma_addr;
1314 bucket = get_hash_bucket(&ref, &flags);
1316 list_for_each_entry(entry, &bucket->list, list) {
1317 if (!exact_match(&ref, entry))
1321 * The same physical address can be mapped multiple
1322 * times. Without a hardware IOMMU this results in the
1323 * same device addresses being put into the dma-debug
1324 * hash multiple times too. This can result in false
1325 * positives being reported. Therefore we implement a
1326 * best-fit algorithm here which updates the first entry
1327 * from the hash which fits the reference value and is
1328 * not currently listed as being checked.
1330 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1331 entry->map_err_type = MAP_ERR_CHECKED;
1336 put_hash_bucket(bucket, &flags);
1338 EXPORT_SYMBOL(debug_dma_mapping_error);
1340 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1341 size_t size, int direction, bool map_single)
1343 struct dma_debug_entry ref = {
1344 .type = dma_debug_page,
1348 .direction = direction,
1351 if (unlikely(!initialized || global_disable))
1355 ref.type = dma_debug_single;
1359 EXPORT_SYMBOL(debug_dma_unmap_page);
1361 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1362 int nents, int mapped_ents, int direction)
1364 struct dma_debug_entry *entry;
1365 struct scatterlist *s;
1368 if (unlikely(!initialized || global_disable))
1371 for_each_sg(sg, s, mapped_ents, i) {
1372 entry = dma_entry_alloc();
1376 entry->type = dma_debug_sg;
1378 entry->paddr = sg_phys(s);
1379 entry->size = sg_dma_len(s);
1380 entry->dev_addr = sg_dma_address(s);
1381 entry->direction = direction;
1382 entry->sg_call_ents = nents;
1383 entry->sg_mapped_ents = mapped_ents;
1385 if (!PageHighMem(sg_page(s))) {
1386 check_for_stack(dev, sg_virt(s));
1387 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
1390 add_dma_entry(entry);
1393 EXPORT_SYMBOL(debug_dma_map_sg);
1395 static int get_nr_mapped_entries(struct device *dev,
1396 struct dma_debug_entry *ref)
1398 struct dma_debug_entry *entry;
1399 struct hash_bucket *bucket;
1400 unsigned long flags;
1403 bucket = get_hash_bucket(ref, &flags);
1404 entry = bucket_find_exact(bucket, ref);
1408 mapped_ents = entry->sg_mapped_ents;
1409 put_hash_bucket(bucket, &flags);
1414 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1415 int nelems, int dir)
1417 struct scatterlist *s;
1418 int mapped_ents = 0, i;
1420 if (unlikely(!initialized || global_disable))
1423 for_each_sg(sglist, s, nelems, i) {
1425 struct dma_debug_entry ref = {
1426 .type = dma_debug_sg,
1428 .paddr = sg_phys(s),
1429 .dev_addr = sg_dma_address(s),
1430 .size = sg_dma_len(s),
1432 .sg_call_ents = nelems,
1435 if (mapped_ents && i >= mapped_ents)
1439 mapped_ents = get_nr_mapped_entries(dev, &ref);
1444 EXPORT_SYMBOL(debug_dma_unmap_sg);
1446 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1447 dma_addr_t dma_addr, void *virt)
1449 struct dma_debug_entry *entry;
1451 if (unlikely(!initialized || global_disable))
1454 if (unlikely(virt == NULL))
1457 entry = dma_entry_alloc();
1461 entry->type = dma_debug_coherent;
1463 entry->paddr = virt_to_phys(virt);
1465 entry->dev_addr = dma_addr;
1466 entry->direction = DMA_BIDIRECTIONAL;
1468 add_dma_entry(entry);
1470 EXPORT_SYMBOL(debug_dma_alloc_coherent);
1472 void debug_dma_free_coherent(struct device *dev, size_t size,
1473 void *virt, dma_addr_t addr)
1475 struct dma_debug_entry ref = {
1476 .type = dma_debug_coherent,
1478 .paddr = virt_to_phys(virt),
1481 .direction = DMA_BIDIRECTIONAL,
1484 if (unlikely(!initialized || global_disable))
1489 EXPORT_SYMBOL(debug_dma_free_coherent);
1491 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1492 size_t size, int direction)
1494 struct dma_debug_entry ref;
1496 if (unlikely(!initialized || global_disable))
1499 ref.type = dma_debug_single;
1501 ref.dev_addr = dma_handle;
1503 ref.direction = direction;
1504 ref.sg_call_ents = 0;
1506 check_sync(dev, &ref, true);
1508 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1510 void debug_dma_sync_single_for_device(struct device *dev,
1511 dma_addr_t dma_handle, size_t size,
1514 struct dma_debug_entry ref;
1516 if (unlikely(!initialized || global_disable))
1519 ref.type = dma_debug_single;
1521 ref.dev_addr = dma_handle;
1523 ref.direction = direction;
1524 ref.sg_call_ents = 0;
1526 check_sync(dev, &ref, false);
1528 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1530 void debug_dma_sync_single_range_for_cpu(struct device *dev,
1531 dma_addr_t dma_handle,
1532 unsigned long offset, size_t size,
1535 struct dma_debug_entry ref;
1537 if (unlikely(!initialized || global_disable))
1540 ref.type = dma_debug_single;
1542 ref.dev_addr = dma_handle;
1543 ref.size = offset + size;
1544 ref.direction = direction;
1545 ref.sg_call_ents = 0;
1547 check_sync(dev, &ref, true);
1549 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1551 void debug_dma_sync_single_range_for_device(struct device *dev,
1552 dma_addr_t dma_handle,
1553 unsigned long offset,
1554 size_t size, int direction)
1556 struct dma_debug_entry ref;
1558 if (unlikely(!initialized || global_disable))
1561 ref.type = dma_debug_single;
1563 ref.dev_addr = dma_handle;
1564 ref.size = offset + size;
1565 ref.direction = direction;
1566 ref.sg_call_ents = 0;
1568 check_sync(dev, &ref, false);
1570 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1572 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1573 int nelems, int direction)
1575 struct scatterlist *s;
1576 int mapped_ents = 0, i;
1578 if (unlikely(!initialized || global_disable))
1581 for_each_sg(sg, s, nelems, i) {
1583 struct dma_debug_entry ref = {
1584 .type = dma_debug_sg,
1586 .paddr = sg_phys(s),
1587 .dev_addr = sg_dma_address(s),
1588 .size = sg_dma_len(s),
1589 .direction = direction,
1590 .sg_call_ents = nelems,
1594 mapped_ents = get_nr_mapped_entries(dev, &ref);
1596 if (i >= mapped_ents)
1599 check_sync(dev, &ref, true);
1602 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1604 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1605 int nelems, int direction)
1607 struct scatterlist *s;
1608 int mapped_ents = 0, i;
1610 if (unlikely(!initialized || global_disable))
1613 for_each_sg(sg, s, nelems, i) {
1615 struct dma_debug_entry ref = {
1616 .type = dma_debug_sg,
1618 .paddr = sg_phys(s),
1619 .dev_addr = sg_dma_address(s),
1620 .size = sg_dma_len(s),
1621 .direction = direction,
1622 .sg_call_ents = nelems,
1625 mapped_ents = get_nr_mapped_entries(dev, &ref);
1627 if (i >= mapped_ents)
1630 check_sync(dev, &ref, false);
1633 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1635 static int __init dma_debug_driver_setup(char *str)
1639 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1640 current_driver_name[i] = *str;
1645 if (current_driver_name[0])
1646 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1647 current_driver_name);
1652 __setup("dma_debug_driver=", dma_debug_driver_setup);