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 /* Global error count */
91 static u32 error_count
;
93 /* Global error show enable*/
94 static u32 show_all_errors __read_mostly
;
95 /* Number of errors to show */
96 static u32 show_num_errors
= 1;
98 static u32 num_free_entries
;
99 static u32 min_free_entries
;
100 static u32 nr_total_entries
;
102 /* number of preallocated entries requested by kernel cmdline */
103 static u32 req_entries
;
105 /* debugfs dentry's for the stuff above */
106 static struct dentry
*dma_debug_dent __read_mostly
;
107 static struct dentry
*global_disable_dent __read_mostly
;
108 static struct dentry
*error_count_dent __read_mostly
;
109 static struct dentry
*show_all_errors_dent __read_mostly
;
110 static struct dentry
*show_num_errors_dent __read_mostly
;
111 static struct dentry
*num_free_entries_dent __read_mostly
;
112 static struct dentry
*min_free_entries_dent __read_mostly
;
113 static struct dentry
*filter_dent __read_mostly
;
115 /* per-driver filter related state */
117 #define NAME_MAX_LEN 64
119 static char current_driver_name
[NAME_MAX_LEN
] __read_mostly
;
120 static struct device_driver
*current_driver __read_mostly
;
122 static DEFINE_RWLOCK(driver_name_lock
);
124 static const char *const maperr2str
[] = {
125 [MAP_ERR_CHECK_NOT_APPLICABLE
] = "dma map error check not applicable",
126 [MAP_ERR_NOT_CHECKED
] = "dma map error not checked",
127 [MAP_ERR_CHECKED
] = "dma map error checked",
130 static const char *type2name
[4] = { "single", "page",
131 "scather-gather", "coherent" };
133 static const char *dir2name
[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
134 "DMA_FROM_DEVICE", "DMA_NONE" };
137 * The access to some variables in this macro is racy. We can't use atomic_t
138 * here because all these variables are exported to debugfs. Some of them even
139 * writeable. This is also the reason why a lock won't help much. But anyway,
140 * the races are no big deal. Here is why:
142 * error_count: the addition is racy, but the worst thing that can happen is
143 * that we don't count some errors
144 * show_num_errors: the subtraction is racy. Also no big deal because in
145 * worst case this will result in one warning more in the
146 * system log than the user configured. This variable is
147 * writeable via debugfs.
149 static inline void dump_entry_trace(struct dma_debug_entry
*entry
)
151 #ifdef CONFIG_STACKTRACE
153 pr_warning("Mapped at:\n");
154 print_stack_trace(&entry
->stacktrace
, 0);
159 static bool driver_filter(struct device
*dev
)
161 struct device_driver
*drv
;
165 /* driver filter off */
166 if (likely(!current_driver_name
[0]))
169 /* driver filter on and initialized */
170 if (current_driver
&& dev
&& dev
->driver
== current_driver
)
173 /* driver filter on, but we can't filter on a NULL device... */
177 if (current_driver
|| !current_driver_name
[0])
180 /* driver filter on but not yet initialized */
185 /* lock to protect against change of current_driver_name */
186 read_lock_irqsave(&driver_name_lock
, flags
);
190 strncmp(current_driver_name
, drv
->name
, NAME_MAX_LEN
- 1) == 0) {
191 current_driver
= drv
;
195 read_unlock_irqrestore(&driver_name_lock
, flags
);
200 #define err_printk(dev, entry, format, arg...) do { \
202 if (driver_filter(dev) && \
203 (show_all_errors || show_num_errors > 0)) { \
204 WARN(1, "%s %s: " format, \
205 dev ? dev_driver_string(dev) : "NULL", \
206 dev ? dev_name(dev) : "NULL", ## arg); \
207 dump_entry_trace(entry); \
209 if (!show_all_errors && show_num_errors > 0) \
210 show_num_errors -= 1; \
214 * Hash related functions
216 * Every DMA-API request is saved into a struct dma_debug_entry. To
217 * have quick access to these structs they are stored into a hash.
219 static int hash_fn(struct dma_debug_entry
*entry
)
222 * Hash function is based on the dma address.
223 * We use bits 20-27 here as the index into the hash
225 return (entry
->dev_addr
>> HASH_FN_SHIFT
) & HASH_FN_MASK
;
229 * Request exclusive access to a hash bucket for a given dma_debug_entry.
231 static struct hash_bucket
*get_hash_bucket(struct dma_debug_entry
*entry
,
232 unsigned long *flags
)
234 int idx
= hash_fn(entry
);
235 unsigned long __flags
;
237 spin_lock_irqsave(&dma_entry_hash
[idx
].lock
, __flags
);
239 return &dma_entry_hash
[idx
];
243 * Give up exclusive access to the hash bucket
245 static void put_hash_bucket(struct hash_bucket
*bucket
,
246 unsigned long *flags
)
248 unsigned long __flags
= *flags
;
250 spin_unlock_irqrestore(&bucket
->lock
, __flags
);
253 static bool exact_match(struct dma_debug_entry
*a
, struct dma_debug_entry
*b
)
255 return ((a
->dev_addr
== b
->dev_addr
) &&
256 (a
->dev
== b
->dev
)) ? true : false;
259 static bool containing_match(struct dma_debug_entry
*a
,
260 struct dma_debug_entry
*b
)
262 if (a
->dev
!= b
->dev
)
265 if ((b
->dev_addr
<= a
->dev_addr
) &&
266 ((b
->dev_addr
+ b
->size
) >= (a
->dev_addr
+ a
->size
)))
273 * Search a given entry in the hash bucket list
275 static struct dma_debug_entry
*__hash_bucket_find(struct hash_bucket
*bucket
,
276 struct dma_debug_entry
*ref
,
279 struct dma_debug_entry
*entry
, *ret
= NULL
;
280 int matches
= 0, match_lvl
, last_lvl
= -1;
282 list_for_each_entry(entry
, &bucket
->list
, list
) {
283 if (!match(ref
, entry
))
287 * Some drivers map the same physical address multiple
288 * times. Without a hardware IOMMU this results in the
289 * same device addresses being put into the dma-debug
290 * hash multiple times too. This can result in false
291 * positives being reported. Therefore we implement a
292 * best-fit algorithm here which returns the entry from
293 * the hash which fits best to the reference value
294 * instead of the first-fit.
298 entry
->size
== ref
->size
? ++match_lvl
: 0;
299 entry
->type
== ref
->type
? ++match_lvl
: 0;
300 entry
->direction
== ref
->direction
? ++match_lvl
: 0;
301 entry
->sg_call_ents
== ref
->sg_call_ents
? ++match_lvl
: 0;
303 if (match_lvl
== 4) {
304 /* perfect-fit - return the result */
306 } else if (match_lvl
> last_lvl
) {
308 * We found an entry that fits better then the
309 * previous one or it is the 1st match.
311 last_lvl
= match_lvl
;
317 * If we have multiple matches but no perfect-fit, just return
320 ret
= (matches
== 1) ? ret
: NULL
;
325 static struct dma_debug_entry
*bucket_find_exact(struct hash_bucket
*bucket
,
326 struct dma_debug_entry
*ref
)
328 return __hash_bucket_find(bucket
, ref
, exact_match
);
331 static struct dma_debug_entry
*bucket_find_contain(struct hash_bucket
**bucket
,
332 struct dma_debug_entry
*ref
,
333 unsigned long *flags
)
336 unsigned int max_range
= dma_get_max_seg_size(ref
->dev
);
337 struct dma_debug_entry
*entry
, index
= *ref
;
338 unsigned int range
= 0;
340 while (range
<= max_range
) {
341 entry
= __hash_bucket_find(*bucket
, &index
, containing_match
);
347 * Nothing found, go back a hash bucket
349 put_hash_bucket(*bucket
, flags
);
350 range
+= (1 << HASH_FN_SHIFT
);
351 index
.dev_addr
-= (1 << HASH_FN_SHIFT
);
352 *bucket
= get_hash_bucket(&index
, flags
);
359 * Add an entry to a hash bucket
361 static void hash_bucket_add(struct hash_bucket
*bucket
,
362 struct dma_debug_entry
*entry
)
364 list_add_tail(&entry
->list
, &bucket
->list
);
368 * Remove entry from a hash bucket list
370 static void hash_bucket_del(struct dma_debug_entry
*entry
)
372 list_del(&entry
->list
);
376 * Dump mapping entries for debugging purposes
378 void debug_dma_dump_mappings(struct device
*dev
)
382 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
383 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
384 struct dma_debug_entry
*entry
;
387 spin_lock_irqsave(&bucket
->lock
, flags
);
389 list_for_each_entry(entry
, &bucket
->list
, list
) {
390 if (!dev
|| dev
== entry
->dev
) {
392 "%s idx %d P=%Lx D=%Lx L=%Lx %s %s\n",
393 type2name
[entry
->type
], idx
,
394 (unsigned long long)entry
->paddr
,
395 entry
->dev_addr
, entry
->size
,
396 dir2name
[entry
->direction
],
397 maperr2str
[entry
->map_err_type
]);
401 spin_unlock_irqrestore(&bucket
->lock
, flags
);
404 EXPORT_SYMBOL(debug_dma_dump_mappings
);
407 * Wrapper function for adding an entry to the hash.
408 * This function takes care of locking itself.
410 static void add_dma_entry(struct dma_debug_entry
*entry
)
412 struct hash_bucket
*bucket
;
415 bucket
= get_hash_bucket(entry
, &flags
);
416 hash_bucket_add(bucket
, entry
);
417 put_hash_bucket(bucket
, &flags
);
420 static struct dma_debug_entry
*__dma_entry_alloc(void)
422 struct dma_debug_entry
*entry
;
424 entry
= list_entry(free_entries
.next
, struct dma_debug_entry
, list
);
425 list_del(&entry
->list
);
426 memset(entry
, 0, sizeof(*entry
));
428 num_free_entries
-= 1;
429 if (num_free_entries
< min_free_entries
)
430 min_free_entries
= num_free_entries
;
435 /* struct dma_entry allocator
437 * The next two functions implement the allocator for
438 * struct dma_debug_entries.
440 static struct dma_debug_entry
*dma_entry_alloc(void)
442 struct dma_debug_entry
*entry
;
445 spin_lock_irqsave(&free_entries_lock
, flags
);
447 if (list_empty(&free_entries
)) {
448 pr_err("DMA-API: debugging out of memory - disabling\n");
449 global_disable
= true;
450 spin_unlock_irqrestore(&free_entries_lock
, flags
);
454 entry
= __dma_entry_alloc();
456 spin_unlock_irqrestore(&free_entries_lock
, flags
);
458 #ifdef CONFIG_STACKTRACE
459 entry
->stacktrace
.max_entries
= DMA_DEBUG_STACKTRACE_ENTRIES
;
460 entry
->stacktrace
.entries
= entry
->st_entries
;
461 entry
->stacktrace
.skip
= 2;
462 save_stack_trace(&entry
->stacktrace
);
468 static void dma_entry_free(struct dma_debug_entry
*entry
)
473 * add to beginning of the list - this way the entries are
474 * more likely cache hot when they are reallocated.
476 spin_lock_irqsave(&free_entries_lock
, flags
);
477 list_add(&entry
->list
, &free_entries
);
478 num_free_entries
+= 1;
479 spin_unlock_irqrestore(&free_entries_lock
, flags
);
482 int dma_debug_resize_entries(u32 num_entries
)
484 int i
, delta
, ret
= 0;
486 struct dma_debug_entry
*entry
;
489 spin_lock_irqsave(&free_entries_lock
, flags
);
491 if (nr_total_entries
< num_entries
) {
492 delta
= num_entries
- nr_total_entries
;
494 spin_unlock_irqrestore(&free_entries_lock
, flags
);
496 for (i
= 0; i
< delta
; i
++) {
497 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
501 list_add_tail(&entry
->list
, &tmp
);
504 spin_lock_irqsave(&free_entries_lock
, flags
);
506 list_splice(&tmp
, &free_entries
);
507 nr_total_entries
+= i
;
508 num_free_entries
+= i
;
510 delta
= nr_total_entries
- num_entries
;
512 for (i
= 0; i
< delta
&& !list_empty(&free_entries
); i
++) {
513 entry
= __dma_entry_alloc();
517 nr_total_entries
-= i
;
520 if (nr_total_entries
!= num_entries
)
523 spin_unlock_irqrestore(&free_entries_lock
, flags
);
527 EXPORT_SYMBOL(dma_debug_resize_entries
);
530 * DMA-API debugging init code
532 * The init code does two things:
533 * 1. Initialize core data structures
534 * 2. Preallocate a given number of dma_debug_entry structs
537 static int prealloc_memory(u32 num_entries
)
539 struct dma_debug_entry
*entry
, *next_entry
;
542 for (i
= 0; i
< num_entries
; ++i
) {
543 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
547 list_add_tail(&entry
->list
, &free_entries
);
550 num_free_entries
= num_entries
;
551 min_free_entries
= num_entries
;
553 pr_info("DMA-API: preallocated %d debug entries\n", num_entries
);
559 list_for_each_entry_safe(entry
, next_entry
, &free_entries
, list
) {
560 list_del(&entry
->list
);
567 static ssize_t
filter_read(struct file
*file
, char __user
*user_buf
,
568 size_t count
, loff_t
*ppos
)
570 char buf
[NAME_MAX_LEN
+ 1];
574 if (!current_driver_name
[0])
578 * We can't copy to userspace directly because current_driver_name can
579 * only be read under the driver_name_lock with irqs disabled. So
580 * create a temporary copy first.
582 read_lock_irqsave(&driver_name_lock
, flags
);
583 len
= scnprintf(buf
, NAME_MAX_LEN
+ 1, "%s\n", current_driver_name
);
584 read_unlock_irqrestore(&driver_name_lock
, flags
);
586 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
589 static ssize_t
filter_write(struct file
*file
, const char __user
*userbuf
,
590 size_t count
, loff_t
*ppos
)
592 char buf
[NAME_MAX_LEN
];
598 * We can't copy from userspace directly. Access to
599 * current_driver_name is protected with a write_lock with irqs
600 * disabled. Since copy_from_user can fault and may sleep we
601 * need to copy to temporary buffer first
603 len
= min(count
, (size_t)(NAME_MAX_LEN
- 1));
604 if (copy_from_user(buf
, userbuf
, len
))
609 write_lock_irqsave(&driver_name_lock
, flags
);
612 * Now handle the string we got from userspace very carefully.
614 * - only use the first token we got
615 * - token delimiter is everything looking like a space
616 * character (' ', '\n', '\t' ...)
619 if (!isalnum(buf
[0])) {
621 * If the first character userspace gave us is not
622 * alphanumerical then assume the filter should be
625 if (current_driver_name
[0])
626 pr_info("DMA-API: switching off dma-debug driver filter\n");
627 current_driver_name
[0] = 0;
628 current_driver
= NULL
;
633 * Now parse out the first token and use it as the name for the
634 * driver to filter for.
636 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
) {
637 current_driver_name
[i
] = buf
[i
];
638 if (isspace(buf
[i
]) || buf
[i
] == ' ' || buf
[i
] == 0)
641 current_driver_name
[i
] = 0;
642 current_driver
= NULL
;
644 pr_info("DMA-API: enable driver filter for driver [%s]\n",
645 current_driver_name
);
648 write_unlock_irqrestore(&driver_name_lock
, flags
);
653 static const struct file_operations filter_fops
= {
655 .write
= filter_write
,
656 .llseek
= default_llseek
,
659 static int dma_debug_fs_init(void)
661 dma_debug_dent
= debugfs_create_dir("dma-api", NULL
);
662 if (!dma_debug_dent
) {
663 pr_err("DMA-API: can not create debugfs directory\n");
667 global_disable_dent
= debugfs_create_bool("disabled", 0444,
670 if (!global_disable_dent
)
673 error_count_dent
= debugfs_create_u32("error_count", 0444,
674 dma_debug_dent
, &error_count
);
675 if (!error_count_dent
)
678 show_all_errors_dent
= debugfs_create_u32("all_errors", 0644,
681 if (!show_all_errors_dent
)
684 show_num_errors_dent
= debugfs_create_u32("num_errors", 0644,
687 if (!show_num_errors_dent
)
690 num_free_entries_dent
= debugfs_create_u32("num_free_entries", 0444,
693 if (!num_free_entries_dent
)
696 min_free_entries_dent
= debugfs_create_u32("min_free_entries", 0444,
699 if (!min_free_entries_dent
)
702 filter_dent
= debugfs_create_file("driver_filter", 0644,
703 dma_debug_dent
, NULL
, &filter_fops
);
710 debugfs_remove_recursive(dma_debug_dent
);
715 static int device_dma_allocations(struct device
*dev
, struct dma_debug_entry
**out_entry
)
717 struct dma_debug_entry
*entry
;
721 local_irq_save(flags
);
723 for (i
= 0; i
< HASH_SIZE
; ++i
) {
724 spin_lock(&dma_entry_hash
[i
].lock
);
725 list_for_each_entry(entry
, &dma_entry_hash
[i
].list
, list
) {
726 if (entry
->dev
== dev
) {
731 spin_unlock(&dma_entry_hash
[i
].lock
);
734 local_irq_restore(flags
);
739 static int dma_debug_device_change(struct notifier_block
*nb
, unsigned long action
, void *data
)
741 struct device
*dev
= data
;
742 struct dma_debug_entry
*uninitialized_var(entry
);
749 case BUS_NOTIFY_UNBOUND_DRIVER
:
750 count
= device_dma_allocations(dev
, &entry
);
753 err_printk(dev
, entry
, "DMA-API: device driver has pending "
754 "DMA allocations while released from device "
756 "One of leaked entries details: "
757 "[device address=0x%016llx] [size=%llu bytes] "
758 "[mapped with %s] [mapped as %s]\n",
759 count
, entry
->dev_addr
, entry
->size
,
760 dir2name
[entry
->direction
], type2name
[entry
->type
]);
769 void dma_debug_add_bus(struct bus_type
*bus
)
771 struct notifier_block
*nb
;
776 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
778 pr_err("dma_debug_add_bus: out of memory\n");
782 nb
->notifier_call
= dma_debug_device_change
;
784 bus_register_notifier(bus
, nb
);
788 * Let the architectures decide how many entries should be preallocated.
790 void dma_debug_init(u32 num_entries
)
797 for (i
= 0; i
< HASH_SIZE
; ++i
) {
798 INIT_LIST_HEAD(&dma_entry_hash
[i
].list
);
799 spin_lock_init(&dma_entry_hash
[i
].lock
);
802 if (dma_debug_fs_init() != 0) {
803 pr_err("DMA-API: error creating debugfs entries - disabling\n");
804 global_disable
= true;
810 num_entries
= req_entries
;
812 if (prealloc_memory(num_entries
) != 0) {
813 pr_err("DMA-API: debugging out of memory error - disabled\n");
814 global_disable
= true;
819 nr_total_entries
= num_free_entries
;
821 pr_info("DMA-API: debugging enabled by kernel config\n");
824 static __init
int dma_debug_cmdline(char *str
)
829 if (strncmp(str
, "off", 3) == 0) {
830 pr_info("DMA-API: debugging disabled on kernel command line\n");
831 global_disable
= true;
837 static __init
int dma_debug_entries_cmdline(char *str
)
844 res
= get_option(&str
, &req_entries
);
852 __setup("dma_debug=", dma_debug_cmdline
);
853 __setup("dma_debug_entries=", dma_debug_entries_cmdline
);
855 static void check_unmap(struct dma_debug_entry
*ref
)
857 struct dma_debug_entry
*entry
;
858 struct hash_bucket
*bucket
;
861 bucket
= get_hash_bucket(ref
, &flags
);
862 entry
= bucket_find_exact(bucket
, ref
);
865 /* must drop lock before calling dma_mapping_error */
866 put_hash_bucket(bucket
, &flags
);
868 if (dma_mapping_error(ref
->dev
, ref
->dev_addr
)) {
869 err_printk(ref
->dev
, NULL
,
870 "DMA-API: device driver tries to free an "
871 "invalid DMA memory address\n");
873 err_printk(ref
->dev
, NULL
,
874 "DMA-API: device driver tries to free DMA "
875 "memory it has not allocated [device "
876 "address=0x%016llx] [size=%llu bytes]\n",
877 ref
->dev_addr
, ref
->size
);
882 if (ref
->size
!= entry
->size
) {
883 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
884 "DMA memory with different size "
885 "[device address=0x%016llx] [map size=%llu bytes] "
886 "[unmap size=%llu bytes]\n",
887 ref
->dev_addr
, entry
->size
, ref
->size
);
890 if (ref
->type
!= entry
->type
) {
891 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
892 "DMA memory with wrong function "
893 "[device address=0x%016llx] [size=%llu bytes] "
894 "[mapped as %s] [unmapped as %s]\n",
895 ref
->dev_addr
, ref
->size
,
896 type2name
[entry
->type
], type2name
[ref
->type
]);
897 } else if ((entry
->type
== dma_debug_coherent
) &&
898 (ref
->paddr
!= entry
->paddr
)) {
899 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
900 "DMA memory with different CPU address "
901 "[device address=0x%016llx] [size=%llu bytes] "
902 "[cpu alloc address=0x%016llx] "
903 "[cpu free address=0x%016llx]",
904 ref
->dev_addr
, ref
->size
,
905 (unsigned long long)entry
->paddr
,
906 (unsigned long long)ref
->paddr
);
909 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
910 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
911 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
912 "DMA sg list with different entry count "
913 "[map count=%d] [unmap count=%d]\n",
914 entry
->sg_call_ents
, ref
->sg_call_ents
);
918 * This may be no bug in reality - but most implementations of the
919 * DMA API don't handle this properly, so check for it here
921 if (ref
->direction
!= entry
->direction
) {
922 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
923 "DMA memory with different direction "
924 "[device address=0x%016llx] [size=%llu bytes] "
925 "[mapped with %s] [unmapped with %s]\n",
926 ref
->dev_addr
, ref
->size
,
927 dir2name
[entry
->direction
],
928 dir2name
[ref
->direction
]);
931 if (entry
->map_err_type
== MAP_ERR_NOT_CHECKED
) {
932 err_printk(ref
->dev
, entry
,
933 "DMA-API: device driver failed to check map error"
934 "[device address=0x%016llx] [size=%llu bytes] "
936 ref
->dev_addr
, ref
->size
,
937 type2name
[entry
->type
]);
940 hash_bucket_del(entry
);
941 dma_entry_free(entry
);
943 put_hash_bucket(bucket
, &flags
);
946 static void check_for_stack(struct device
*dev
, void *addr
)
948 if (object_is_on_stack(addr
))
949 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from"
950 "stack [addr=%p]\n", addr
);
953 static inline bool overlap(void *addr
, unsigned long len
, void *start
, void *end
)
955 unsigned long a1
= (unsigned long)addr
;
956 unsigned long b1
= a1
+ len
;
957 unsigned long a2
= (unsigned long)start
;
958 unsigned long b2
= (unsigned long)end
;
960 return !(b1
<= a2
|| a1
>= b2
);
963 static void check_for_illegal_area(struct device
*dev
, void *addr
, unsigned long len
)
965 if (overlap(addr
, len
, _text
, _etext
) ||
966 overlap(addr
, len
, __start_rodata
, __end_rodata
))
967 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr
, len
);
970 static void check_sync(struct device
*dev
,
971 struct dma_debug_entry
*ref
,
974 struct dma_debug_entry
*entry
;
975 struct hash_bucket
*bucket
;
978 bucket
= get_hash_bucket(ref
, &flags
);
980 entry
= bucket_find_contain(&bucket
, ref
, &flags
);
983 err_printk(dev
, NULL
, "DMA-API: device driver tries "
984 "to sync DMA memory it has not allocated "
985 "[device address=0x%016llx] [size=%llu bytes]\n",
986 (unsigned long long)ref
->dev_addr
, ref
->size
);
990 if (ref
->size
> entry
->size
) {
991 err_printk(dev
, entry
, "DMA-API: device driver syncs"
992 " DMA memory outside allocated range "
993 "[device address=0x%016llx] "
994 "[allocation size=%llu bytes] "
995 "[sync offset+size=%llu]\n",
996 entry
->dev_addr
, entry
->size
,
1000 if (entry
->direction
== DMA_BIDIRECTIONAL
)
1003 if (ref
->direction
!= entry
->direction
) {
1004 err_printk(dev
, entry
, "DMA-API: device driver syncs "
1005 "DMA memory with different direction "
1006 "[device address=0x%016llx] [size=%llu bytes] "
1007 "[mapped with %s] [synced with %s]\n",
1008 (unsigned long long)ref
->dev_addr
, entry
->size
,
1009 dir2name
[entry
->direction
],
1010 dir2name
[ref
->direction
]);
1013 if (to_cpu
&& !(entry
->direction
== DMA_FROM_DEVICE
) &&
1014 !(ref
->direction
== DMA_TO_DEVICE
))
1015 err_printk(dev
, entry
, "DMA-API: device driver syncs "
1016 "device read-only DMA memory for cpu "
1017 "[device address=0x%016llx] [size=%llu bytes] "
1018 "[mapped with %s] [synced with %s]\n",
1019 (unsigned long long)ref
->dev_addr
, entry
->size
,
1020 dir2name
[entry
->direction
],
1021 dir2name
[ref
->direction
]);
1023 if (!to_cpu
&& !(entry
->direction
== DMA_TO_DEVICE
) &&
1024 !(ref
->direction
== DMA_FROM_DEVICE
))
1025 err_printk(dev
, entry
, "DMA-API: device driver syncs "
1026 "device write-only DMA memory to device "
1027 "[device address=0x%016llx] [size=%llu bytes] "
1028 "[mapped with %s] [synced with %s]\n",
1029 (unsigned long long)ref
->dev_addr
, entry
->size
,
1030 dir2name
[entry
->direction
],
1031 dir2name
[ref
->direction
]);
1034 put_hash_bucket(bucket
, &flags
);
1037 void debug_dma_map_page(struct device
*dev
, struct page
*page
, size_t offset
,
1038 size_t size
, int direction
, dma_addr_t dma_addr
,
1041 struct dma_debug_entry
*entry
;
1043 if (unlikely(global_disable
))
1046 if (dma_mapping_error(dev
, dma_addr
))
1049 entry
= dma_entry_alloc();
1054 entry
->type
= dma_debug_page
;
1055 entry
->paddr
= page_to_phys(page
) + offset
;
1056 entry
->dev_addr
= dma_addr
;
1058 entry
->direction
= direction
;
1059 entry
->map_err_type
= MAP_ERR_NOT_CHECKED
;
1062 entry
->type
= dma_debug_single
;
1064 if (!PageHighMem(page
)) {
1065 void *addr
= page_address(page
) + offset
;
1067 check_for_stack(dev
, addr
);
1068 check_for_illegal_area(dev
, addr
, size
);
1071 add_dma_entry(entry
);
1073 EXPORT_SYMBOL(debug_dma_map_page
);
1075 void debug_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
1077 struct dma_debug_entry ref
;
1078 struct dma_debug_entry
*entry
;
1079 struct hash_bucket
*bucket
;
1080 unsigned long flags
;
1082 if (unlikely(global_disable
))
1086 ref
.dev_addr
= dma_addr
;
1087 bucket
= get_hash_bucket(&ref
, &flags
);
1089 list_for_each_entry(entry
, &bucket
->list
, list
) {
1090 if (!exact_match(&ref
, entry
))
1094 * The same physical address can be mapped multiple
1095 * times. Without a hardware IOMMU this results in the
1096 * same device addresses being put into the dma-debug
1097 * hash multiple times too. This can result in false
1098 * positives being reported. Therefore we implement a
1099 * best-fit algorithm here which updates the first entry
1100 * from the hash which fits the reference value and is
1101 * not currently listed as being checked.
1103 if (entry
->map_err_type
== MAP_ERR_NOT_CHECKED
) {
1104 entry
->map_err_type
= MAP_ERR_CHECKED
;
1109 put_hash_bucket(bucket
, &flags
);
1111 EXPORT_SYMBOL(debug_dma_mapping_error
);
1113 void debug_dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
1114 size_t size
, int direction
, bool map_single
)
1116 struct dma_debug_entry ref
= {
1117 .type
= dma_debug_page
,
1121 .direction
= direction
,
1124 if (unlikely(global_disable
))
1128 ref
.type
= dma_debug_single
;
1132 EXPORT_SYMBOL(debug_dma_unmap_page
);
1134 void debug_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
1135 int nents
, int mapped_ents
, int direction
)
1137 struct dma_debug_entry
*entry
;
1138 struct scatterlist
*s
;
1141 if (unlikely(global_disable
))
1144 for_each_sg(sg
, s
, mapped_ents
, i
) {
1145 entry
= dma_entry_alloc();
1149 entry
->type
= dma_debug_sg
;
1151 entry
->paddr
= sg_phys(s
);
1152 entry
->size
= sg_dma_len(s
);
1153 entry
->dev_addr
= sg_dma_address(s
);
1154 entry
->direction
= direction
;
1155 entry
->sg_call_ents
= nents
;
1156 entry
->sg_mapped_ents
= mapped_ents
;
1158 if (!PageHighMem(sg_page(s
))) {
1159 check_for_stack(dev
, sg_virt(s
));
1160 check_for_illegal_area(dev
, sg_virt(s
), sg_dma_len(s
));
1163 add_dma_entry(entry
);
1166 EXPORT_SYMBOL(debug_dma_map_sg
);
1168 static int get_nr_mapped_entries(struct device
*dev
,
1169 struct dma_debug_entry
*ref
)
1171 struct dma_debug_entry
*entry
;
1172 struct hash_bucket
*bucket
;
1173 unsigned long flags
;
1176 bucket
= get_hash_bucket(ref
, &flags
);
1177 entry
= bucket_find_exact(bucket
, ref
);
1181 mapped_ents
= entry
->sg_mapped_ents
;
1182 put_hash_bucket(bucket
, &flags
);
1187 void debug_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
1188 int nelems
, int dir
)
1190 struct scatterlist
*s
;
1191 int mapped_ents
= 0, i
;
1193 if (unlikely(global_disable
))
1196 for_each_sg(sglist
, s
, nelems
, i
) {
1198 struct dma_debug_entry ref
= {
1199 .type
= dma_debug_sg
,
1201 .paddr
= sg_phys(s
),
1202 .dev_addr
= sg_dma_address(s
),
1203 .size
= sg_dma_len(s
),
1205 .sg_call_ents
= nelems
,
1208 if (mapped_ents
&& i
>= mapped_ents
)
1212 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1217 EXPORT_SYMBOL(debug_dma_unmap_sg
);
1219 void debug_dma_alloc_coherent(struct device
*dev
, size_t size
,
1220 dma_addr_t dma_addr
, void *virt
)
1222 struct dma_debug_entry
*entry
;
1224 if (unlikely(global_disable
))
1227 if (unlikely(virt
== NULL
))
1230 entry
= dma_entry_alloc();
1234 entry
->type
= dma_debug_coherent
;
1236 entry
->paddr
= virt_to_phys(virt
);
1238 entry
->dev_addr
= dma_addr
;
1239 entry
->direction
= DMA_BIDIRECTIONAL
;
1241 add_dma_entry(entry
);
1243 EXPORT_SYMBOL(debug_dma_alloc_coherent
);
1245 void debug_dma_free_coherent(struct device
*dev
, size_t size
,
1246 void *virt
, dma_addr_t addr
)
1248 struct dma_debug_entry ref
= {
1249 .type
= dma_debug_coherent
,
1251 .paddr
= virt_to_phys(virt
),
1254 .direction
= DMA_BIDIRECTIONAL
,
1257 if (unlikely(global_disable
))
1262 EXPORT_SYMBOL(debug_dma_free_coherent
);
1264 void debug_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
1265 size_t size
, int direction
)
1267 struct dma_debug_entry ref
;
1269 if (unlikely(global_disable
))
1272 ref
.type
= dma_debug_single
;
1274 ref
.dev_addr
= dma_handle
;
1276 ref
.direction
= direction
;
1277 ref
.sg_call_ents
= 0;
1279 check_sync(dev
, &ref
, true);
1281 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu
);
1283 void debug_dma_sync_single_for_device(struct device
*dev
,
1284 dma_addr_t dma_handle
, size_t size
,
1287 struct dma_debug_entry ref
;
1289 if (unlikely(global_disable
))
1292 ref
.type
= dma_debug_single
;
1294 ref
.dev_addr
= dma_handle
;
1296 ref
.direction
= direction
;
1297 ref
.sg_call_ents
= 0;
1299 check_sync(dev
, &ref
, false);
1301 EXPORT_SYMBOL(debug_dma_sync_single_for_device
);
1303 void debug_dma_sync_single_range_for_cpu(struct device
*dev
,
1304 dma_addr_t dma_handle
,
1305 unsigned long offset
, size_t size
,
1308 struct dma_debug_entry ref
;
1310 if (unlikely(global_disable
))
1313 ref
.type
= dma_debug_single
;
1315 ref
.dev_addr
= dma_handle
;
1316 ref
.size
= offset
+ size
;
1317 ref
.direction
= direction
;
1318 ref
.sg_call_ents
= 0;
1320 check_sync(dev
, &ref
, true);
1322 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu
);
1324 void debug_dma_sync_single_range_for_device(struct device
*dev
,
1325 dma_addr_t dma_handle
,
1326 unsigned long offset
,
1327 size_t size
, int direction
)
1329 struct dma_debug_entry ref
;
1331 if (unlikely(global_disable
))
1334 ref
.type
= dma_debug_single
;
1336 ref
.dev_addr
= dma_handle
;
1337 ref
.size
= offset
+ size
;
1338 ref
.direction
= direction
;
1339 ref
.sg_call_ents
= 0;
1341 check_sync(dev
, &ref
, false);
1343 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device
);
1345 void debug_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
1346 int nelems
, int direction
)
1348 struct scatterlist
*s
;
1349 int mapped_ents
= 0, i
;
1351 if (unlikely(global_disable
))
1354 for_each_sg(sg
, s
, nelems
, i
) {
1356 struct dma_debug_entry ref
= {
1357 .type
= dma_debug_sg
,
1359 .paddr
= sg_phys(s
),
1360 .dev_addr
= sg_dma_address(s
),
1361 .size
= sg_dma_len(s
),
1362 .direction
= direction
,
1363 .sg_call_ents
= nelems
,
1367 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1369 if (i
>= mapped_ents
)
1372 check_sync(dev
, &ref
, true);
1375 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu
);
1377 void debug_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
1378 int nelems
, int direction
)
1380 struct scatterlist
*s
;
1381 int mapped_ents
= 0, i
;
1383 if (unlikely(global_disable
))
1386 for_each_sg(sg
, s
, nelems
, i
) {
1388 struct dma_debug_entry ref
= {
1389 .type
= dma_debug_sg
,
1391 .paddr
= sg_phys(s
),
1392 .dev_addr
= sg_dma_address(s
),
1393 .size
= sg_dma_len(s
),
1394 .direction
= direction
,
1395 .sg_call_ents
= nelems
,
1398 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1400 if (i
>= mapped_ents
)
1403 check_sync(dev
, &ref
, false);
1406 EXPORT_SYMBOL(debug_dma_sync_sg_for_device
);
1408 static int __init
dma_debug_driver_setup(char *str
)
1412 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
, ++str
) {
1413 current_driver_name
[i
] = *str
;
1418 if (current_driver_name
[0])
1419 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1420 current_driver_name
);
1425 __setup("dma_debug_driver=", dma_debug_driver_setup
);