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)
48 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
60 #ifdef CONFIG_STACKTRACE
84 static u32 error_count;
89 static u32 show_num_errors = 1;
91 static u32 num_free_entries;
92 static u32 min_free_entries;
93 static u32 nr_total_entries;
96 static u32 req_entries;
110 #define NAME_MAX_LEN 64
117 static const char *type2name[4] = {
"single",
"page",
118 "scather-gather",
"coherent" };
120 static const char *dir2name[4] = {
"DMA_BIDIRECTIONAL",
"DMA_TO_DEVICE",
121 "DMA_FROM_DEVICE",
"DMA_NONE" };
138 #ifdef CONFIG_STACKTRACE
146 static bool driver_filter(
struct device *
dev)
153 if (
likely(!current_driver_name[0]))
157 if (current_driver && dev && dev->
driver == current_driver)
164 if (current_driver || !current_driver_name[0])
178 current_driver = drv;
187 #define err_printk(dev, entry, format, arg...) do { \
189 if (driver_filter(dev) && \
190 (show_all_errors || show_num_errors > 0)) { \
191 WARN(1, "%s %s: " format, \
192 dev ? dev_driver_string(dev) : "NULL", \
193 dev ? dev_name(dev) : "NULL", ## arg); \
194 dump_entry_trace(entry); \
196 if (!show_all_errors && show_num_errors > 0) \
197 show_num_errors -= 1; \
219 unsigned long *flags)
221 int idx = hash_fn(entry);
222 unsigned long __flags;
226 return &dma_entry_hash[
idx];
232 static void put_hash_bucket(
struct hash_bucket *bucket,
233 unsigned long *flags)
235 unsigned long __flags = *
flags;
237 spin_unlock_irqrestore(&bucket->
lock, __flags);
243 (a->
dev == b->
dev)) ?
true :
false;
267 int matches = 0, match_lvl, last_lvl = -1;
270 if (!
match(ref, entry))
285 entry->
size == ref->
size ? ++match_lvl : 0;
286 entry->
type == ref->
type ? ++match_lvl : 0;
290 if (match_lvl == 4) {
293 }
else if (match_lvl > last_lvl) {
298 last_lvl = match_lvl;
307 ret = (matches == 1) ? ret :
NULL;
315 return __hash_bucket_find(bucket, ref, exact_match);
320 unsigned long *flags)
323 unsigned int max_range = dma_get_max_seg_size(ref->
dev);
325 unsigned int range = 0;
327 while (range <= max_range) {
328 entry = __hash_bucket_find(*bucket, &index, containing_match);
336 put_hash_bucket(*bucket, flags);
339 *bucket = get_hash_bucket(&index, flags);
348 static void hash_bucket_add(
struct hash_bucket *bucket,
377 if (!dev || dev == entry->
dev) {
379 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
380 type2name[entry->
type], idx,
381 (
unsigned long long)entry->
paddr,
387 spin_unlock_irqrestore(&bucket->
lock, flags);
401 bucket = get_hash_bucket(entry, &flags);
402 hash_bucket_add(bucket, entry);
403 put_hash_bucket(bucket, &flags);
412 memset(entry, 0,
sizeof(*entry));
414 num_free_entries -= 1;
415 if (num_free_entries < min_free_entries)
416 min_free_entries = num_free_entries;
433 if (list_empty(&free_entries)) {
434 pr_err(
"DMA-API: debugging out of memory - disabling\n");
435 global_disable =
true;
436 spin_unlock_irqrestore(&free_entries_lock, flags);
440 entry = __dma_entry_alloc();
442 spin_unlock_irqrestore(&free_entries_lock, flags);
444 #ifdef CONFIG_STACKTRACE
446 entry->stacktrace.entries = entry->st_entries;
447 entry->stacktrace.skip = 2;
463 list_add(&entry->
list, &free_entries);
464 num_free_entries += 1;
465 spin_unlock_irqrestore(&free_entries_lock, flags);
477 if (nr_total_entries < num_entries) {
478 delta = num_entries - nr_total_entries;
480 spin_unlock_irqrestore(&free_entries_lock, flags);
482 for (i = 0; i <
delta; i++) {
492 list_splice(&
tmp, &free_entries);
493 nr_total_entries +=
i;
494 num_free_entries +=
i;
498 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
499 entry = __dma_entry_alloc();
503 nr_total_entries -=
i;
506 if (nr_total_entries != num_entries)
509 spin_unlock_irqrestore(&free_entries_lock, flags);
539 pr_info(
"DMA-API: preallocated %d debug entries\n", num_entries);
554 size_t count, loff_t *ppos)
560 if (!current_driver_name[0])
575 static ssize_t filter_write(
struct file *file,
const char __user *userbuf,
576 size_t count, loff_t *ppos)
611 if (current_driver_name[0])
612 pr_info(
"DMA-API: switching off dma-debug driver filter\n");
613 current_driver_name[0] = 0;
614 current_driver =
NULL;
623 current_driver_name[
i] = buf[
i];
624 if (
isspace(buf[i]) || buf[i] ==
' ' || buf[i] == 0)
627 current_driver_name[
i] = 0;
628 current_driver =
NULL;
630 pr_info(
"DMA-API: enable driver filter for driver [%s]\n",
631 current_driver_name);
641 .write = filter_write,
645 static int dma_debug_fs_init(
void)
648 if (!dma_debug_dent) {
649 pr_err(
"DMA-API: can not create debugfs directory\n");
656 if (!global_disable_dent)
660 dma_debug_dent, &error_count);
661 if (!error_count_dent)
667 if (!show_all_errors_dent)
673 if (!show_num_errors_dent)
679 if (!num_free_entries_dent)
685 if (!min_free_entries_dent)
689 dma_debug_dent,
NULL, &filter_fops);
710 spin_lock(&dma_entry_hash[i].lock);
712 if (entry->
dev == dev) {
717 spin_unlock(&dma_entry_hash[i].lock);
736 count = device_dma_allocations(dev, &entry);
739 err_printk(dev, entry,
"DMA-API: device driver has pending "
740 "DMA allocations while released from device "
742 "One of leaked entries details: "
743 "[device address=0x%016llx] [size=%llu bytes] "
744 "[mapped with %s] [mapped as %s]\n",
764 pr_err(
"dma_debug_add_bus: out of memory\n");
784 INIT_LIST_HEAD(&dma_entry_hash[i].
list);
788 if (dma_debug_fs_init() != 0) {
789 pr_err(
"DMA-API: error creating debugfs entries - disabling\n");
790 global_disable =
true;
796 num_entries = req_entries;
798 if (prealloc_memory(num_entries) != 0) {
799 pr_err(
"DMA-API: debugging out of memory error - disabled\n");
800 global_disable =
true;
805 nr_total_entries = num_free_entries;
807 pr_info(
"DMA-API: debugging enabled by kernel config\n");
810 static __init int dma_debug_cmdline(
char *
str)
815 if (
strncmp(str,
"off", 3) == 0) {
816 pr_info(
"DMA-API: debugging disabled on kernel command line\n");
817 global_disable =
true;
823 static __init int dma_debug_entries_cmdline(
char *str)
838 __setup(
"dma_debug=", dma_debug_cmdline);
839 __setup(
"dma_debug_entries=", dma_debug_entries_cmdline);
849 "to free an invalid DMA memory address\n");
853 bucket = get_hash_bucket(ref, &flags);
854 entry = bucket_find_exact(bucket, ref);
858 "to free DMA memory it has not allocated "
859 "[device address=0x%016llx] [size=%llu bytes]\n",
866 "DMA memory with different size "
867 "[device address=0x%016llx] [map size=%llu bytes] "
868 "[unmap size=%llu bytes]\n",
874 "DMA memory with wrong function "
875 "[device address=0x%016llx] [size=%llu bytes] "
876 "[mapped as %s] [unmapped as %s]\n",
878 type2name[entry->
type], type2name[ref->
type]);
882 "DMA memory with different CPU address "
883 "[device address=0x%016llx] [size=%llu bytes] "
884 "[cpu alloc address=0x%016llx] "
885 "[cpu free address=0x%016llx]",
887 (
unsigned long long)entry->
paddr,
888 (
unsigned long long)ref->
paddr);
894 "DMA sg list with different entry count "
895 "[map count=%d] [unmap count=%d]\n",
905 "DMA memory with different direction "
906 "[device address=0x%016llx] [size=%llu bytes] "
907 "[mapped with %s] [unmapped with %s]\n",
913 hash_bucket_del(entry);
914 dma_entry_free(entry);
917 put_hash_bucket(bucket, &flags);
920 static void check_for_stack(
struct device *dev,
void *
addr)
922 if (object_is_on_stack(addr))
924 "stack [addr=%p]\n", addr);
927 static inline bool overlap(
void *addr,
unsigned long len,
void *
start,
void *
end)
929 unsigned long a1 = (
unsigned long)addr;
930 unsigned long b1 = a1 + len;
931 unsigned long a2 = (
unsigned long)start;
932 unsigned long b2 = (
unsigned long)end;
934 return !(b1 <= a2 || a1 >= b2);
937 static void check_for_illegal_area(
struct device *dev,
void *addr,
unsigned long len)
939 if (overlap(addr, len, _text, _etext) ||
940 overlap(addr, len, __start_rodata, __end_rodata))
941 err_printk(dev,
NULL,
"DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
944 static void check_sync(
struct device *dev,
952 bucket = get_hash_bucket(ref, &flags);
954 entry = bucket_find_contain(&bucket, ref, &flags);
958 "to sync DMA memory it has not allocated "
959 "[device address=0x%016llx] [size=%llu bytes]\n",
965 err_printk(dev, entry,
"DMA-API: device driver syncs"
966 " DMA memory outside allocated range "
967 "[device address=0x%016llx] "
968 "[allocation size=%llu bytes] "
969 "[sync offset+size=%llu]\n",
978 err_printk(dev, entry,
"DMA-API: device driver syncs "
979 "DMA memory with different direction "
980 "[device address=0x%016llx] [size=%llu bytes] "
981 "[mapped with %s] [synced with %s]\n",
989 err_printk(dev, entry,
"DMA-API: device driver syncs "
990 "device read-only DMA memory for cpu "
991 "[device address=0x%016llx] [size=%llu bytes] "
992 "[mapped with %s] [synced with %s]\n",
999 err_printk(dev, entry,
"DMA-API: device driver syncs "
1000 "device write-only DMA memory to device "
1001 "[device address=0x%016llx] [size=%llu bytes] "
1002 "[mapped with %s] [synced with %s]\n",
1008 put_hash_bucket(bucket, &flags);
1023 entry = dma_entry_alloc();
1037 if (!PageHighMem(page)) {
1040 check_for_stack(dev, addr);
1041 check_for_illegal_area(dev, addr, size);
1044 add_dma_entry(entry);
1070 int nents,
int mapped_ents,
int direction)
1080 entry = dma_entry_alloc();
1093 if (!PageHighMem(sg_page(s))) {
1094 check_for_stack(dev, sg_virt(s));
1095 check_for_illegal_area(dev, sg_virt(s),
sg_dma_len(s));
1098 add_dma_entry(entry);
1103 static int get_nr_mapped_entries(
struct device *dev,
1108 unsigned long flags;
1111 bucket = get_hash_bucket(ref, &flags);
1112 entry = bucket_find_exact(bucket, ref);
1117 put_hash_bucket(bucket, &flags);
1123 int nelems,
int dir)
1126 int mapped_ents = 0,
i;
1140 .sg_call_ents = nelems,
1143 if (mapped_ents && i >= mapped_ents)
1147 mapped_ents = get_nr_mapped_entries(dev, &ref);
1165 entry = dma_entry_alloc();
1176 add_dma_entry(entry);
1214 check_sync(dev, &ref,
true);
1234 check_sync(dev, &ref,
false);
1255 check_sync(dev, &ref,
true);
1276 check_sync(dev, &ref,
false);
1284 int mapped_ents = 0,
i;
1298 .sg_call_ents = nelems,
1302 mapped_ents = get_nr_mapped_entries(dev, &ref);
1304 if (i >= mapped_ents)
1307 check_sync(dev, &ref,
true);
1316 int mapped_ents = 0,
i;
1330 .sg_call_ents = nelems,
1333 mapped_ents = get_nr_mapped_entries(dev, &ref);
1335 if (i >= mapped_ents)
1338 check_sync(dev, &ref,
false);
1343 static int __init dma_debug_driver_setup(
char *str)
1347 for (i = 0; i < NAME_MAX_LEN - 1; ++
i, ++
str) {
1348 current_driver_name[
i] = *
str;
1353 if (current_driver_name[0])
1354 pr_info(
"DMA-API: enable driver filter for driver [%s]\n",
1355 current_driver_name);
1360 __setup(
"dma_debug_driver=", dma_debug_driver_setup);