hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/kernel/dma/debug.c
....@@ -1,28 +1,17 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2008 Advanced Micro Devices, Inc.
34 *
45 * Author: Joerg Roedel <joerg.roedel@amd.com>
5
- *
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.
9
- *
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.
14
- *
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
186 */
7
+
8
+#define pr_fmt(fmt) "DMA-API: " fmt
199
2010 #include <linux/sched/task_stack.h>
2111 #include <linux/scatterlist.h>
22
-#include <linux/dma-mapping.h>
12
+#include <linux/dma-map-ops.h>
2313 #include <linux/sched/task.h>
2414 #include <linux/stacktrace.h>
25
-#include <linux/dma-debug.h>
2615 #include <linux/spinlock.h>
2716 #include <linux/vmalloc.h>
2817 #include <linux/debugfs.h>
....@@ -34,21 +23,19 @@
3423 #include <linux/ctype.h>
3524 #include <linux/list.h>
3625 #include <linux/slab.h>
37
-
3826 #include <asm/sections.h>
27
+#include "debug.h"
3928
40
-#define HASH_SIZE 1024ULL
29
+#define HASH_SIZE 16384ULL
4130 #define HASH_FN_SHIFT 13
4231 #define HASH_FN_MASK (HASH_SIZE - 1)
4332
44
-/* allow architectures to override this if absolutely required */
45
-#ifndef PREALLOC_DMA_DEBUG_ENTRIES
4633 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
47
-#endif
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))
4836
4937 enum {
5038 dma_debug_single,
51
- dma_debug_page,
5239 dma_debug_sg,
5340 dma_debug_coherent,
5441 dma_debug_resource,
....@@ -66,40 +53,40 @@
6653 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
6754 * @list: node on pre-allocated free_entries list
6855 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
69
- * @type: single, page, sg, coherent
70
- * @pfn: page frame of the start address
71
- * @offset: offset of mapping relative to pfn
7256 * @size: length of the mapping
57
+ * @type: single, page, sg, coherent
7358 * @direction: enum dma_data_direction
7459 * @sg_call_ents: 'nents' from dma_map_sg
7560 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
61
+ * @pfn: page frame of the start address
62
+ * @offset: offset of mapping relative to pfn
7663 * @map_err_type: track whether dma_mapping_error() was checked
7764 * @stacktrace: support backtraces when a violation is detected
7865 */
7966 struct dma_debug_entry {
8067 struct list_head list;
8168 struct device *dev;
82
- int type;
83
- unsigned long pfn;
84
- size_t offset;
8569 u64 dev_addr;
8670 u64 size;
71
+ int type;
8772 int direction;
8873 int sg_call_ents;
8974 int sg_mapped_ents;
75
+ unsigned long pfn;
76
+ size_t offset;
9077 enum map_err_types map_err_type;
9178 #ifdef CONFIG_STACKTRACE
92
- struct stack_trace stacktrace;
93
- unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
79
+ unsigned int stack_len;
80
+ unsigned long stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
9481 #endif
95
-};
82
+} ____cacheline_aligned_in_smp;
9683
9784 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
9885
9986 struct hash_bucket {
10087 struct list_head list;
10188 spinlock_t lock;
102
-} ____cacheline_aligned_in_smp;
89
+};
10390
10491 /* Hash list to save the allocated dma addresses */
10592 static struct hash_bucket dma_entry_hash[HASH_SIZE];
....@@ -134,16 +121,6 @@
134121 /* number of preallocated entries requested by kernel cmdline */
135122 static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
136123
137
-/* debugfs dentry's for the stuff above */
138
-static struct dentry *dma_debug_dent __read_mostly;
139
-static struct dentry *global_disable_dent __read_mostly;
140
-static struct dentry *error_count_dent __read_mostly;
141
-static struct dentry *show_all_errors_dent __read_mostly;
142
-static struct dentry *show_num_errors_dent __read_mostly;
143
-static struct dentry *num_free_entries_dent __read_mostly;
144
-static struct dentry *min_free_entries_dent __read_mostly;
145
-static struct dentry *filter_dent __read_mostly;
146
-
147124 /* per-driver filter related state */
148125
149126 #define NAME_MAX_LEN 64
....@@ -159,12 +136,19 @@
159136 [MAP_ERR_CHECKED] = "dma map error checked",
160137 };
161138
162
-static const char *type2name[5] = { "single", "page",
163
- "scather-gather", "coherent",
164
- "resource" };
139
+static const char *type2name[] = {
140
+ [dma_debug_single] = "single",
141
+ [dma_debug_sg] = "scather-gather",
142
+ [dma_debug_coherent] = "coherent",
143
+ [dma_debug_resource] = "resource",
144
+};
165145
166
-static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
167
- "DMA_FROM_DEVICE", "DMA_NONE" };
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",
151
+};
168152
169153 /*
170154 * The access to some variables in this macro is racy. We can't use atomic_t
....@@ -183,8 +167,8 @@
183167 {
184168 #ifdef CONFIG_STACKTRACE
185169 if (entry) {
186
- pr_warning("Mapped at:\n");
187
- print_stack_trace(&entry->stacktrace, 0);
170
+ pr_warn("Mapped at:\n");
171
+ stack_trace_print(entry->stack_entries, entry->stack_len, 0);
188172 }
189173 #endif
190174 }
....@@ -234,7 +218,7 @@
234218 error_count += 1; \
235219 if (driver_filter(dev) && \
236220 (show_all_errors || show_num_errors > 0)) { \
237
- WARN(1, "%s %s: " format, \
221
+ WARN(1, pr_fmt("%s %s: ") format, \
238222 dev ? dev_driver_string(dev) : "NULL", \
239223 dev ? dev_name(dev) : "NULL", ## arg); \
240224 dump_entry_trace(entry); \
....@@ -277,12 +261,10 @@
277261 * Give up exclusive access to the hash bucket
278262 */
279263 static void put_hash_bucket(struct hash_bucket *bucket,
280
- unsigned long *flags)
264
+ unsigned long flags)
281265 __releases(&bucket->lock)
282266 {
283
- unsigned long __flags = *flags;
284
-
285
- spin_unlock_irqrestore(&bucket->lock, __flags);
267
+ spin_unlock_irqrestore(&bucket->lock, flags);
286268 }
287269
288270 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
....@@ -381,7 +363,7 @@
381363 /*
382364 * Nothing found, go back a hash bucket
383365 */
384
- put_hash_bucket(*bucket, flags);
366
+ put_hash_bucket(*bucket, *flags);
385367 range += (1 << HASH_FN_SHIFT);
386368 index.dev_addr -= (1 << HASH_FN_SHIFT);
387369 *bucket = get_hash_bucket(&index, flags);
....@@ -465,11 +447,8 @@
465447 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
466448 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
467449 * entries into the tree.
468
- *
469
- * At any time debug_dma_assert_idle() can be called to trigger a
470
- * warning if any cachelines in the given page are in the active set.
471450 */
472
-static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
451
+static RADIX_TREE(dma_active_cacheline, GFP_ATOMIC);
473452 static DEFINE_SPINLOCK(radix_lock);
474453 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
475454 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
....@@ -514,13 +493,10 @@
514493 overlap = active_cacheline_set_overlap(cln, ++overlap);
515494
516495 /* If we overflowed the overlap counter then we're potentially
517
- * leaking dma-mappings. Otherwise, if maps and unmaps are
518
- * balanced then this overflow may cause false negatives in
519
- * debug_dma_assert_idle() as the cacheline may be marked idle
520
- * prematurely.
496
+ * leaking dma-mappings.
521497 */
522498 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
523
- "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
499
+ pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
524500 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
525501 }
526502
....@@ -572,53 +548,6 @@
572548 spin_unlock_irqrestore(&radix_lock, flags);
573549 }
574550
575
-/**
576
- * debug_dma_assert_idle() - assert that a page is not undergoing dma
577
- * @page: page to lookup in the dma_active_cacheline tree
578
- *
579
- * Place a call to this routine in cases where the cpu touching the page
580
- * before the dma completes (page is dma_unmapped) will lead to data
581
- * corruption.
582
- */
583
-void debug_dma_assert_idle(struct page *page)
584
-{
585
- static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
586
- struct dma_debug_entry *entry = NULL;
587
- void **results = (void **) &ents;
588
- unsigned int nents, i;
589
- unsigned long flags;
590
- phys_addr_t cln;
591
-
592
- if (dma_debug_disabled())
593
- return;
594
-
595
- if (!page)
596
- return;
597
-
598
- cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
599
- spin_lock_irqsave(&radix_lock, flags);
600
- nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
601
- CACHELINES_PER_PAGE);
602
- for (i = 0; i < nents; i++) {
603
- phys_addr_t ent_cln = to_cacheline_number(ents[i]);
604
-
605
- if (ent_cln == cln) {
606
- entry = ents[i];
607
- break;
608
- } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
609
- break;
610
- }
611
- spin_unlock_irqrestore(&radix_lock, flags);
612
-
613
- if (!entry)
614
- return;
615
-
616
- cln = to_cacheline_number(entry);
617
- err_printk(entry->dev, entry,
618
- "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
619
- &cln);
620
-}
621
-
622551 /*
623552 * Wrapper function for adding an entry to the hash.
624553 * This function takes care of locking itself.
....@@ -631,17 +560,35 @@
631560
632561 bucket = get_hash_bucket(entry, &flags);
633562 hash_bucket_add(bucket, entry);
634
- put_hash_bucket(bucket, &flags);
563
+ put_hash_bucket(bucket, flags);
635564
636565 rc = active_cacheline_insert(entry);
637566 if (rc == -ENOMEM) {
638
- pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
567
+ pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n");
639568 global_disable = true;
640569 }
641570
642571 /* TODO: report -EEXIST errors here as overlapping mappings are
643572 * not supported by the DMA API
644573 */
574
+}
575
+
576
+static int dma_debug_create_entries(gfp_t gfp)
577
+{
578
+ struct dma_debug_entry *entry;
579
+ int i;
580
+
581
+ entry = (void *)get_zeroed_page(gfp);
582
+ if (!entry)
583
+ return -ENOMEM;
584
+
585
+ for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
586
+ list_add_tail(&entry[i].list, &free_entries);
587
+
588
+ num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
589
+ nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
590
+
591
+ return 0;
645592 }
646593
647594 static struct dma_debug_entry *__dma_entry_alloc(void)
....@@ -659,6 +606,22 @@
659606 return entry;
660607 }
661608
609
+/*
610
+ * This should be called outside of free_entries_lock scope to avoid potential
611
+ * deadlocks with serial consoles that use DMA.
612
+ */
613
+static void __dma_entry_alloc_check_leak(u32 nr_entries)
614
+{
615
+ u32 tmp = nr_entries % nr_prealloc_entries;
616
+
617
+ /* Shout each time we tick over some multiple of the initial pool */
618
+ if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
619
+ pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
620
+ nr_entries,
621
+ (nr_entries / nr_prealloc_entries));
622
+ }
623
+}
624
+
662625 /* struct dma_entry allocator
663626 *
664627 * The next two functions implement the allocator for
....@@ -666,29 +629,35 @@
666629 */
667630 static struct dma_debug_entry *dma_entry_alloc(void)
668631 {
632
+ bool alloc_check_leak = false;
669633 struct dma_debug_entry *entry;
670634 unsigned long flags;
635
+ u32 nr_entries;
671636
672637 spin_lock_irqsave(&free_entries_lock, flags);
673
-
674
- if (list_empty(&free_entries)) {
675
- global_disable = true;
676
- spin_unlock_irqrestore(&free_entries_lock, flags);
677
- pr_err("DMA-API: debugging out of memory - disabling\n");
678
- return NULL;
638
+ if (num_free_entries == 0) {
639
+ if (dma_debug_create_entries(GFP_ATOMIC)) {
640
+ global_disable = true;
641
+ spin_unlock_irqrestore(&free_entries_lock, flags);
642
+ pr_err("debugging out of memory - disabling\n");
643
+ return NULL;
644
+ }
645
+ alloc_check_leak = true;
646
+ nr_entries = nr_total_entries;
679647 }
680648
681649 entry = __dma_entry_alloc();
682650
683651 spin_unlock_irqrestore(&free_entries_lock, flags);
684652
685
-#ifdef CONFIG_STACKTRACE
686
- entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
687
- entry->stacktrace.entries = entry->st_entries;
688
- entry->stacktrace.skip = 2;
689
- save_stack_trace(&entry->stacktrace);
690
-#endif
653
+ if (alloc_check_leak)
654
+ __dma_entry_alloc_check_leak(nr_entries);
691655
656
+#ifdef CONFIG_STACKTRACE
657
+ entry->stack_len = stack_trace_save(entry->stack_entries,
658
+ ARRAY_SIZE(entry->stack_entries),
659
+ 1);
660
+#endif
692661 return entry;
693662 }
694663
....@@ -708,52 +677,6 @@
708677 spin_unlock_irqrestore(&free_entries_lock, flags);
709678 }
710679
711
-int dma_debug_resize_entries(u32 num_entries)
712
-{
713
- int i, delta, ret = 0;
714
- unsigned long flags;
715
- struct dma_debug_entry *entry;
716
- LIST_HEAD(tmp);
717
-
718
- spin_lock_irqsave(&free_entries_lock, flags);
719
-
720
- if (nr_total_entries < num_entries) {
721
- delta = num_entries - nr_total_entries;
722
-
723
- spin_unlock_irqrestore(&free_entries_lock, flags);
724
-
725
- for (i = 0; i < delta; i++) {
726
- entry = kzalloc(sizeof(*entry), GFP_KERNEL);
727
- if (!entry)
728
- break;
729
-
730
- list_add_tail(&entry->list, &tmp);
731
- }
732
-
733
- spin_lock_irqsave(&free_entries_lock, flags);
734
-
735
- list_splice(&tmp, &free_entries);
736
- nr_total_entries += i;
737
- num_free_entries += i;
738
- } else {
739
- delta = nr_total_entries - num_entries;
740
-
741
- for (i = 0; i < delta && !list_empty(&free_entries); i++) {
742
- entry = __dma_entry_alloc();
743
- kfree(entry);
744
- }
745
-
746
- nr_total_entries -= i;
747
- }
748
-
749
- if (nr_total_entries != num_entries)
750
- ret = 1;
751
-
752
- spin_unlock_irqrestore(&free_entries_lock, flags);
753
-
754
- return ret;
755
-}
756
-
757680 /*
758681 * DMA-API debugging init code
759682 *
....@@ -761,36 +684,6 @@
761684 * 1. Initialize core data structures
762685 * 2. Preallocate a given number of dma_debug_entry structs
763686 */
764
-
765
-static int prealloc_memory(u32 num_entries)
766
-{
767
- struct dma_debug_entry *entry, *next_entry;
768
- int i;
769
-
770
- for (i = 0; i < num_entries; ++i) {
771
- entry = kzalloc(sizeof(*entry), GFP_KERNEL);
772
- if (!entry)
773
- goto out_err;
774
-
775
- list_add_tail(&entry->list, &free_entries);
776
- }
777
-
778
- num_free_entries = num_entries;
779
- min_free_entries = num_entries;
780
-
781
- pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
782
-
783
- return 0;
784
-
785
-out_err:
786
-
787
- list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
788
- list_del(&entry->list);
789
- kfree(entry);
790
- }
791
-
792
- return -ENOMEM;
793
-}
794687
795688 static ssize_t filter_read(struct file *file, char __user *user_buf,
796689 size_t count, loff_t *ppos)
....@@ -851,7 +744,7 @@
851744 * switched off.
852745 */
853746 if (current_driver_name[0])
854
- pr_info("DMA-API: switching off dma-debug driver filter\n");
747
+ pr_info("switching off dma-debug driver filter\n");
855748 current_driver_name[0] = 0;
856749 current_driver = NULL;
857750 goto out_unlock;
....@@ -869,7 +762,7 @@
869762 current_driver_name[i] = 0;
870763 current_driver = NULL;
871764
872
- pr_info("DMA-API: enable driver filter for driver [%s]\n",
765
+ pr_info("enable driver filter for driver [%s]\n",
873766 current_driver_name);
874767
875768 out_unlock:
....@@ -884,61 +777,50 @@
884777 .llseek = default_llseek,
885778 };
886779
887
-static int dma_debug_fs_init(void)
780
+static int dump_show(struct seq_file *seq, void *v)
888781 {
889
- dma_debug_dent = debugfs_create_dir("dma-api", NULL);
890
- if (!dma_debug_dent) {
891
- pr_err("DMA-API: can not create debugfs directory\n");
892
- return -ENOMEM;
782
+ int idx;
783
+
784
+ for (idx = 0; idx < HASH_SIZE; idx++) {
785
+ struct hash_bucket *bucket = &dma_entry_hash[idx];
786
+ struct dma_debug_entry *entry;
787
+ unsigned long flags;
788
+
789
+ spin_lock_irqsave(&bucket->lock, flags);
790
+ list_for_each_entry(entry, &bucket->list, list) {
791
+ seq_printf(seq,
792
+ "%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx %s %s\n",
793
+ dev_name(entry->dev),
794
+ dev_driver_string(entry->dev),
795
+ type2name[entry->type], idx,
796
+ phys_addr(entry), entry->pfn,
797
+ entry->dev_addr, entry->size,
798
+ dir2name[entry->direction],
799
+ maperr2str[entry->map_err_type]);
800
+ }
801
+ spin_unlock_irqrestore(&bucket->lock, flags);
893802 }
803
+ return 0;
804
+}
805
+DEFINE_SHOW_ATTRIBUTE(dump);
894806
895
- global_disable_dent = debugfs_create_bool("disabled", 0444,
896
- dma_debug_dent,
897
- &global_disable);
898
- if (!global_disable_dent)
899
- goto out_err;
807
+static int __init dma_debug_fs_init(void)
808
+{
809
+ struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
900810
901
- error_count_dent = debugfs_create_u32("error_count", 0444,
902
- dma_debug_dent, &error_count);
903
- if (!error_count_dent)
904
- goto out_err;
905
-
906
- show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
907
- dma_debug_dent,
908
- &show_all_errors);
909
- if (!show_all_errors_dent)
910
- goto out_err;
911
-
912
- show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
913
- dma_debug_dent,
914
- &show_num_errors);
915
- if (!show_num_errors_dent)
916
- goto out_err;
917
-
918
- num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
919
- dma_debug_dent,
920
- &num_free_entries);
921
- if (!num_free_entries_dent)
922
- goto out_err;
923
-
924
- min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
925
- dma_debug_dent,
926
- &min_free_entries);
927
- if (!min_free_entries_dent)
928
- goto out_err;
929
-
930
- filter_dent = debugfs_create_file("driver_filter", 0644,
931
- dma_debug_dent, NULL, &filter_fops);
932
- if (!filter_dent)
933
- goto out_err;
811
+ debugfs_create_bool("disabled", 0444, dentry, &global_disable);
812
+ debugfs_create_u32("error_count", 0444, dentry, &error_count);
813
+ debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
814
+ debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
815
+ debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
816
+ debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
817
+ debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
818
+ debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
819
+ debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
934820
935821 return 0;
936
-
937
-out_err:
938
- debugfs_remove_recursive(dma_debug_dent);
939
-
940
- return -ENOMEM;
941822 }
823
+core_initcall_sync(dma_debug_fs_init);
942824
943825 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
944826 {
....@@ -963,7 +845,7 @@
963845 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
964846 {
965847 struct device *dev = data;
966
- struct dma_debug_entry *uninitialized_var(entry);
848
+ struct dma_debug_entry *entry;
967849 int count;
968850
969851 if (dma_debug_disabled())
....@@ -974,7 +856,7 @@
974856 count = device_dma_allocations(dev, &entry);
975857 if (count == 0)
976858 break;
977
- err_printk(dev, entry, "DMA-API: device driver has pending "
859
+ err_printk(dev, entry, "device driver has pending "
978860 "DMA allocations while released from device "
979861 "[count=%d]\n"
980862 "One of leaked entries details: "
....@@ -1010,7 +892,7 @@
1010892
1011893 static int dma_debug_init(void)
1012894 {
1013
- int i;
895
+ int i, nr_pages;
1014896
1015897 /* Do not use dma_debug_initialized here, since we really want to be
1016898 * called to set dma_debug_initialized
....@@ -1023,25 +905,25 @@
1023905 spin_lock_init(&dma_entry_hash[i].lock);
1024906 }
1025907
1026
- if (dma_debug_fs_init() != 0) {
1027
- pr_err("DMA-API: error creating debugfs entries - disabling\n");
908
+ nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
909
+ for (i = 0; i < nr_pages; ++i)
910
+ dma_debug_create_entries(GFP_KERNEL);
911
+ if (num_free_entries >= nr_prealloc_entries) {
912
+ pr_info("preallocated %d debug entries\n", nr_total_entries);
913
+ } else if (num_free_entries > 0) {
914
+ pr_warn("%d debug entries requested but only %d allocated\n",
915
+ nr_prealloc_entries, nr_total_entries);
916
+ } else {
917
+ pr_err("debugging out of memory error - disabled\n");
1028918 global_disable = true;
1029919
1030920 return 0;
1031921 }
1032
-
1033
- if (prealloc_memory(nr_prealloc_entries) != 0) {
1034
- pr_err("DMA-API: debugging out of memory error - disabled\n");
1035
- global_disable = true;
1036
-
1037
- return 0;
1038
- }
1039
-
1040
- nr_total_entries = num_free_entries;
922
+ min_free_entries = num_free_entries;
1041923
1042924 dma_debug_initialized = true;
1043925
1044
- pr_info("DMA-API: debugging enabled by kernel config\n");
926
+ pr_info("debugging enabled by kernel config\n");
1045927 return 0;
1046928 }
1047929 core_initcall(dma_debug_init);
....@@ -1052,11 +934,11 @@
1052934 return -EINVAL;
1053935
1054936 if (strncmp(str, "off", 3) == 0) {
1055
- pr_info("DMA-API: debugging disabled on kernel command line\n");
937
+ pr_info("debugging disabled on kernel command line\n");
1056938 global_disable = true;
1057939 }
1058940
1059
- return 0;
941
+ return 1;
1060942 }
1061943
1062944 static __init int dma_debug_entries_cmdline(char *str)
....@@ -1065,7 +947,7 @@
1065947 return -EINVAL;
1066948 if (!get_option(&str, &nr_prealloc_entries))
1067949 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
1068
- return 0;
950
+ return 1;
1069951 }
1070952
1071953 __setup("dma_debug=", dma_debug_cmdline);
....@@ -1082,15 +964,15 @@
1082964
1083965 if (!entry) {
1084966 /* must drop lock before calling dma_mapping_error */
1085
- put_hash_bucket(bucket, &flags);
967
+ put_hash_bucket(bucket, flags);
1086968
1087969 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1088970 err_printk(ref->dev, NULL,
1089
- "DMA-API: device driver tries to free an "
971
+ "device driver tries to free an "
1090972 "invalid DMA memory address\n");
1091973 } else {
1092974 err_printk(ref->dev, NULL,
1093
- "DMA-API: device driver tries to free DMA "
975
+ "device driver tries to free DMA "
1094976 "memory it has not allocated [device "
1095977 "address=0x%016llx] [size=%llu bytes]\n",
1096978 ref->dev_addr, ref->size);
....@@ -1099,7 +981,7 @@
1099981 }
1100982
1101983 if (ref->size != entry->size) {
1102
- err_printk(ref->dev, entry, "DMA-API: device driver frees "
984
+ err_printk(ref->dev, entry, "device driver frees "
1103985 "DMA memory with different size "
1104986 "[device address=0x%016llx] [map size=%llu bytes] "
1105987 "[unmap size=%llu bytes]\n",
....@@ -1107,7 +989,7 @@
1107989 }
1108990
1109991 if (ref->type != entry->type) {
1110
- err_printk(ref->dev, entry, "DMA-API: device driver frees "
992
+ err_printk(ref->dev, entry, "device driver frees "
1111993 "DMA memory with wrong function "
1112994 "[device address=0x%016llx] [size=%llu bytes] "
1113995 "[mapped as %s] [unmapped as %s]\n",
....@@ -1115,7 +997,7 @@
1115997 type2name[entry->type], type2name[ref->type]);
1116998 } else if ((entry->type == dma_debug_coherent) &&
1117999 (phys_addr(ref) != phys_addr(entry))) {
1118
- err_printk(ref->dev, entry, "DMA-API: device driver frees "
1000
+ err_printk(ref->dev, entry, "device driver frees "
11191001 "DMA memory with different CPU address "
11201002 "[device address=0x%016llx] [size=%llu bytes] "
11211003 "[cpu alloc address=0x%016llx] "
....@@ -1127,7 +1009,7 @@
11271009
11281010 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
11291011 ref->sg_call_ents != entry->sg_call_ents) {
1130
- err_printk(ref->dev, entry, "DMA-API: device driver frees "
1012
+ err_printk(ref->dev, entry, "device driver frees "
11311013 "DMA sg list with different entry count "
11321014 "[map count=%d] [unmap count=%d]\n",
11331015 entry->sg_call_ents, ref->sg_call_ents);
....@@ -1138,7 +1020,7 @@
11381020 * DMA API don't handle this properly, so check for it here
11391021 */
11401022 if (ref->direction != entry->direction) {
1141
- err_printk(ref->dev, entry, "DMA-API: device driver frees "
1023
+ err_printk(ref->dev, entry, "device driver frees "
11421024 "DMA memory with different direction "
11431025 "[device address=0x%016llx] [size=%llu bytes] "
11441026 "[mapped with %s] [unmapped with %s]\n",
....@@ -1150,11 +1032,11 @@
11501032 /*
11511033 * Drivers should use dma_mapping_error() to check the returned
11521034 * addresses of dma_map_single() and dma_map_page().
1153
- * If not, print this warning message. See Documentation/DMA-API.txt.
1035
+ * If not, print this warning message. See Documentation/core-api/dma-api.rst.
11541036 */
11551037 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
11561038 err_printk(ref->dev, entry,
1157
- "DMA-API: device driver failed to check map error"
1039
+ "device driver failed to check map error"
11581040 "[device address=0x%016llx] [size=%llu bytes] "
11591041 "[mapped as %s]",
11601042 ref->dev_addr, ref->size,
....@@ -1164,7 +1046,7 @@
11641046 hash_bucket_del(entry);
11651047 dma_entry_free(entry);
11661048
1167
- put_hash_bucket(bucket, &flags);
1049
+ put_hash_bucket(bucket, flags);
11681050 }
11691051
11701052 static void check_for_stack(struct device *dev,
....@@ -1179,7 +1061,7 @@
11791061 return;
11801062 addr = page_address(page) + offset;
11811063 if (object_is_on_stack(addr))
1182
- err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [addr=%p]\n", addr);
1064
+ err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
11831065 } else {
11841066 /* Stack is vmalloced. */
11851067 int i;
....@@ -1189,7 +1071,7 @@
11891071 continue;
11901072
11911073 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1192
- err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [probable addr=%p]\n", addr);
1074
+ err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
11931075 break;
11941076 }
11951077 }
....@@ -1209,7 +1091,7 @@
12091091 {
12101092 if (overlap(addr, len, _stext, _etext) ||
12111093 overlap(addr, len, __start_rodata, __end_rodata))
1212
- err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1094
+ err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
12131095 }
12141096
12151097 static void check_sync(struct device *dev,
....@@ -1225,7 +1107,7 @@
12251107 entry = bucket_find_contain(&bucket, ref, &flags);
12261108
12271109 if (!entry) {
1228
- err_printk(dev, NULL, "DMA-API: device driver tries "
1110
+ err_printk(dev, NULL, "device driver tries "
12291111 "to sync DMA memory it has not allocated "
12301112 "[device address=0x%016llx] [size=%llu bytes]\n",
12311113 (unsigned long long)ref->dev_addr, ref->size);
....@@ -1233,7 +1115,7 @@
12331115 }
12341116
12351117 if (ref->size > entry->size) {
1236
- err_printk(dev, entry, "DMA-API: device driver syncs"
1118
+ err_printk(dev, entry, "device driver syncs"
12371119 " DMA memory outside allocated range "
12381120 "[device address=0x%016llx] "
12391121 "[allocation size=%llu bytes] "
....@@ -1246,7 +1128,7 @@
12461128 goto out;
12471129
12481130 if (ref->direction != entry->direction) {
1249
- err_printk(dev, entry, "DMA-API: device driver syncs "
1131
+ err_printk(dev, entry, "device driver syncs "
12501132 "DMA memory with different direction "
12511133 "[device address=0x%016llx] [size=%llu bytes] "
12521134 "[mapped with %s] [synced with %s]\n",
....@@ -1257,7 +1139,7 @@
12571139
12581140 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
12591141 !(ref->direction == DMA_TO_DEVICE))
1260
- err_printk(dev, entry, "DMA-API: device driver syncs "
1142
+ err_printk(dev, entry, "device driver syncs "
12611143 "device read-only DMA memory for cpu "
12621144 "[device address=0x%016llx] [size=%llu bytes] "
12631145 "[mapped with %s] [synced with %s]\n",
....@@ -1267,7 +1149,7 @@
12671149
12681150 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
12691151 !(ref->direction == DMA_FROM_DEVICE))
1270
- err_printk(dev, entry, "DMA-API: device driver syncs "
1152
+ err_printk(dev, entry, "device driver syncs "
12711153 "device write-only DMA memory to device "
12721154 "[device address=0x%016llx] [size=%llu bytes] "
12731155 "[mapped with %s] [synced with %s]\n",
....@@ -1275,16 +1157,17 @@
12751157 dir2name[entry->direction],
12761158 dir2name[ref->direction]);
12771159
1160
+ /* sg list count can be less than map count when partial cache sync */
12781161 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1279
- ref->sg_call_ents != entry->sg_call_ents) {
1280
- err_printk(ref->dev, entry, "DMA-API: device driver syncs "
1281
- "DMA sg list with different entry count "
1162
+ ref->sg_call_ents > entry->sg_call_ents) {
1163
+ err_printk(ref->dev, entry, "device driver syncs "
1164
+ "DMA sg list count larger than map count "
12821165 "[map count=%d] [sync count=%d]\n",
12831166 entry->sg_call_ents, ref->sg_call_ents);
12841167 }
12851168
12861169 out:
1287
- put_hash_bucket(bucket, &flags);
1170
+ put_hash_bucket(bucket, flags);
12881171 }
12891172
12901173 static void check_sg_segment(struct device *dev, struct scatterlist *sg)
....@@ -1298,7 +1181,7 @@
12981181 * whoever generated the list forgot to check them.
12991182 */
13001183 if (sg->length > max_seg)
1301
- err_printk(dev, NULL, "DMA-API: mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1184
+ err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
13021185 sg->length, max_seg);
13031186 /*
13041187 * In some cases this could potentially be the DMA API
....@@ -1308,14 +1191,29 @@
13081191 start = sg_dma_address(sg);
13091192 end = start + sg_dma_len(sg) - 1;
13101193 if ((start ^ end) & ~boundary)
1311
- err_printk(dev, NULL, "DMA-API: mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1194
+ err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
13121195 start, end, boundary);
13131196 #endif
13141197 }
13151198
1199
+void debug_dma_map_single(struct device *dev, const void *addr,
1200
+ unsigned long len)
1201
+{
1202
+ if (unlikely(dma_debug_disabled()))
1203
+ return;
1204
+
1205
+ if (!virt_addr_valid(addr))
1206
+ err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1207
+ addr, len);
1208
+
1209
+ if (is_vmalloc_addr(addr))
1210
+ err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1211
+ addr, len);
1212
+}
1213
+EXPORT_SYMBOL(debug_dma_map_single);
1214
+
13161215 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1317
- size_t size, int direction, dma_addr_t dma_addr,
1318
- bool map_single)
1216
+ size_t size, int direction, dma_addr_t dma_addr)
13191217 {
13201218 struct dma_debug_entry *entry;
13211219
....@@ -1330,16 +1228,13 @@
13301228 return;
13311229
13321230 entry->dev = dev;
1333
- entry->type = dma_debug_page;
1231
+ entry->type = dma_debug_single;
13341232 entry->pfn = page_to_pfn(page);
1335
- entry->offset = offset,
1233
+ entry->offset = offset;
13361234 entry->dev_addr = dma_addr;
13371235 entry->size = size;
13381236 entry->direction = direction;
13391237 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1340
-
1341
- if (map_single)
1342
- entry->type = dma_debug_single;
13431238
13441239 check_for_stack(dev, page, offset);
13451240
....@@ -1351,7 +1246,6 @@
13511246
13521247 add_dma_entry(entry);
13531248 }
1354
-EXPORT_SYMBOL(debug_dma_map_page);
13551249
13561250 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
13571251 {
....@@ -1387,15 +1281,15 @@
13871281 }
13881282 }
13891283
1390
- put_hash_bucket(bucket, &flags);
1284
+ put_hash_bucket(bucket, flags);
13911285 }
13921286 EXPORT_SYMBOL(debug_dma_mapping_error);
13931287
13941288 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1395
- size_t size, int direction, bool map_single)
1289
+ size_t size, int direction)
13961290 {
13971291 struct dma_debug_entry ref = {
1398
- .type = dma_debug_page,
1292
+ .type = dma_debug_single,
13991293 .dev = dev,
14001294 .dev_addr = addr,
14011295 .size = size,
....@@ -1404,13 +1298,8 @@
14041298
14051299 if (unlikely(dma_debug_disabled()))
14061300 return;
1407
-
1408
- if (map_single)
1409
- ref.type = dma_debug_single;
1410
-
14111301 check_unmap(&ref);
14121302 }
1413
-EXPORT_SYMBOL(debug_dma_unmap_page);
14141303
14151304 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
14161305 int nents, int mapped_ents, int direction)
....@@ -1436,7 +1325,7 @@
14361325 entry->type = dma_debug_sg;
14371326 entry->dev = dev;
14381327 entry->pfn = page_to_pfn(sg_page(s));
1439
- entry->offset = s->offset,
1328
+ entry->offset = s->offset;
14401329 entry->size = sg_dma_len(s);
14411330 entry->dev_addr = sg_dma_address(s);
14421331 entry->direction = direction;
....@@ -1448,7 +1337,6 @@
14481337 add_dma_entry(entry);
14491338 }
14501339 }
1451
-EXPORT_SYMBOL(debug_dma_map_sg);
14521340
14531341 static int get_nr_mapped_entries(struct device *dev,
14541342 struct dma_debug_entry *ref)
....@@ -1464,7 +1352,7 @@
14641352
14651353 if (entry)
14661354 mapped_ents = entry->sg_mapped_ents;
1467
- put_hash_bucket(bucket, &flags);
1355
+ put_hash_bucket(bucket, flags);
14681356
14691357 return mapped_ents;
14701358 }
....@@ -1500,7 +1388,6 @@
15001388 check_unmap(&ref);
15011389 }
15021390 }
1503
-EXPORT_SYMBOL(debug_dma_unmap_sg);
15041391
15051392 void debug_dma_alloc_coherent(struct device *dev, size_t size,
15061393 dma_addr_t dma_addr, void *virt)
....@@ -1535,7 +1422,6 @@
15351422
15361423 add_dma_entry(entry);
15371424 }
1538
-EXPORT_SYMBOL(debug_dma_alloc_coherent);
15391425
15401426 void debug_dma_free_coherent(struct device *dev, size_t size,
15411427 void *virt, dma_addr_t addr)
....@@ -1563,7 +1449,6 @@
15631449
15641450 check_unmap(&ref);
15651451 }
1566
-EXPORT_SYMBOL(debug_dma_free_coherent);
15671452
15681453 void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
15691454 int direction, dma_addr_t dma_addr)
....@@ -1588,7 +1473,6 @@
15881473
15891474 add_dma_entry(entry);
15901475 }
1591
-EXPORT_SYMBOL(debug_dma_map_resource);
15921476
15931477 void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
15941478 size_t size, int direction)
....@@ -1606,7 +1490,6 @@
16061490
16071491 check_unmap(&ref);
16081492 }
1609
-EXPORT_SYMBOL(debug_dma_unmap_resource);
16101493
16111494 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
16121495 size_t size, int direction)
....@@ -1625,7 +1508,6 @@
16251508
16261509 check_sync(dev, &ref, true);
16271510 }
1628
-EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
16291511
16301512 void debug_dma_sync_single_for_device(struct device *dev,
16311513 dma_addr_t dma_handle, size_t size,
....@@ -1645,49 +1527,6 @@
16451527
16461528 check_sync(dev, &ref, false);
16471529 }
1648
-EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1649
-
1650
-void debug_dma_sync_single_range_for_cpu(struct device *dev,
1651
- dma_addr_t dma_handle,
1652
- unsigned long offset, size_t size,
1653
- int direction)
1654
-{
1655
- struct dma_debug_entry ref;
1656
-
1657
- if (unlikely(dma_debug_disabled()))
1658
- return;
1659
-
1660
- ref.type = dma_debug_single;
1661
- ref.dev = dev;
1662
- ref.dev_addr = dma_handle;
1663
- ref.size = offset + size;
1664
- ref.direction = direction;
1665
- ref.sg_call_ents = 0;
1666
-
1667
- check_sync(dev, &ref, true);
1668
-}
1669
-EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1670
-
1671
-void debug_dma_sync_single_range_for_device(struct device *dev,
1672
- dma_addr_t dma_handle,
1673
- unsigned long offset,
1674
- size_t size, int direction)
1675
-{
1676
- struct dma_debug_entry ref;
1677
-
1678
- if (unlikely(dma_debug_disabled()))
1679
- return;
1680
-
1681
- ref.type = dma_debug_single;
1682
- ref.dev = dev;
1683
- ref.dev_addr = dma_handle;
1684
- ref.size = offset + size;
1685
- ref.direction = direction;
1686
- ref.sg_call_ents = 0;
1687
-
1688
- check_sync(dev, &ref, false);
1689
-}
1690
-EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
16911530
16921531 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
16931532 int nelems, int direction)
....@@ -1720,7 +1559,6 @@
17201559 check_sync(dev, &ref, true);
17211560 }
17221561 }
1723
-EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
17241562
17251563 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
17261564 int nelems, int direction)
....@@ -1752,7 +1590,6 @@
17521590 check_sync(dev, &ref, false);
17531591 }
17541592 }
1755
-EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
17561593
17571594 static int __init dma_debug_driver_setup(char *str)
17581595 {
....@@ -1765,7 +1602,7 @@
17651602 }
17661603
17671604 if (current_driver_name[0])
1768
- pr_info("DMA-API: enable driver filter for driver [%s]\n",
1605
+ pr_info("enable driver filter for driver [%s]\n",
17691606 current_driver_name);
17701607
17711608