Commit 41531c8f5f05aba5ec645d9770557eedbf75b422

Authored by Joerg Roedel
1 parent 2e34bde185

dma-debug: add a check dma memory leaks

Impact: allow architectures to monitor busses for dma mem leakage

This patch adds checking code to detect if a device has pending DMA
operations when it is about to be unbound from its device driver.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>

Showing 2 changed files with 62 additions and 0 deletions Inline Diff

include/linux/dma-debug.h
1 /* 1 /*
2 * Copyright (C) 2008 Advanced Micro Devices, Inc. 2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
3 * 3 *
4 * Author: Joerg Roedel <joerg.roedel@amd.com> 4 * Author: Joerg Roedel <joerg.roedel@amd.com>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify it 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 7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation. 8 * by the Free Software Foundation.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 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 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 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 18 */
19 19
20 #ifndef __DMA_DEBUG_H 20 #ifndef __DMA_DEBUG_H
21 #define __DMA_DEBUG_H 21 #define __DMA_DEBUG_H
22 22
23 #include <linux/types.h> 23 #include <linux/types.h>
24 24
25 struct device; 25 struct device;
26 struct scatterlist; 26 struct scatterlist;
27 struct bus_type;
27 28
28 #ifdef CONFIG_DMA_API_DEBUG 29 #ifdef CONFIG_DMA_API_DEBUG
29 30
31 extern void dma_debug_add_bus(struct bus_type *bus);
32
30 extern void dma_debug_init(u32 num_entries); 33 extern void dma_debug_init(u32 num_entries);
31 34
32 extern void debug_dma_map_page(struct device *dev, struct page *page, 35 extern void debug_dma_map_page(struct device *dev, struct page *page,
33 size_t offset, size_t size, 36 size_t offset, size_t size,
34 int direction, dma_addr_t dma_addr, 37 int direction, dma_addr_t dma_addr,
35 bool map_single); 38 bool map_single);
36 39
37 extern void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, 40 extern void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
38 size_t size, int direction, bool map_single); 41 size_t size, int direction, bool map_single);
39 42
40 extern void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, 43 extern void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
41 int nents, int mapped_ents, int direction); 44 int nents, int mapped_ents, int direction);
42 45
43 extern void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, 46 extern void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
44 int nelems, int dir); 47 int nelems, int dir);
45 48
46 extern void debug_dma_alloc_coherent(struct device *dev, size_t size, 49 extern void debug_dma_alloc_coherent(struct device *dev, size_t size,
47 dma_addr_t dma_addr, void *virt); 50 dma_addr_t dma_addr, void *virt);
48 51
49 extern void debug_dma_free_coherent(struct device *dev, size_t size, 52 extern void debug_dma_free_coherent(struct device *dev, size_t size,
50 void *virt, dma_addr_t addr); 53 void *virt, dma_addr_t addr);
51 54
52 extern void debug_dma_sync_single_for_cpu(struct device *dev, 55 extern void debug_dma_sync_single_for_cpu(struct device *dev,
53 dma_addr_t dma_handle, size_t size, 56 dma_addr_t dma_handle, size_t size,
54 int direction); 57 int direction);
55 58
56 extern void debug_dma_sync_single_for_device(struct device *dev, 59 extern void debug_dma_sync_single_for_device(struct device *dev,
57 dma_addr_t dma_handle, 60 dma_addr_t dma_handle,
58 size_t size, int direction); 61 size_t size, int direction);
59 62
60 extern void debug_dma_sync_single_range_for_cpu(struct device *dev, 63 extern void debug_dma_sync_single_range_for_cpu(struct device *dev,
61 dma_addr_t dma_handle, 64 dma_addr_t dma_handle,
62 unsigned long offset, 65 unsigned long offset,
63 size_t size, 66 size_t size,
64 int direction); 67 int direction);
65 68
66 extern void debug_dma_sync_single_range_for_device(struct device *dev, 69 extern void debug_dma_sync_single_range_for_device(struct device *dev,
67 dma_addr_t dma_handle, 70 dma_addr_t dma_handle,
68 unsigned long offset, 71 unsigned long offset,
69 size_t size, int direction); 72 size_t size, int direction);
70 73
71 extern void debug_dma_sync_sg_for_cpu(struct device *dev, 74 extern void debug_dma_sync_sg_for_cpu(struct device *dev,
72 struct scatterlist *sg, 75 struct scatterlist *sg,
73 int nelems, int direction); 76 int nelems, int direction);
74 77
75 extern void debug_dma_sync_sg_for_device(struct device *dev, 78 extern void debug_dma_sync_sg_for_device(struct device *dev,
76 struct scatterlist *sg, 79 struct scatterlist *sg,
77 int nelems, int direction); 80 int nelems, int direction);
78 81
79 extern void debug_dma_dump_mappings(struct device *dev); 82 extern void debug_dma_dump_mappings(struct device *dev);
80 83
81 #else /* CONFIG_DMA_API_DEBUG */ 84 #else /* CONFIG_DMA_API_DEBUG */
85
86 void dma_debug_add_bus(struct bus_type *bus)
87 {
88 }
82 89
83 static inline void dma_debug_init(u32 num_entries) 90 static inline void dma_debug_init(u32 num_entries)
84 { 91 {
85 } 92 }
86 93
87 static inline void debug_dma_map_page(struct device *dev, struct page *page, 94 static inline void debug_dma_map_page(struct device *dev, struct page *page,
88 size_t offset, size_t size, 95 size_t offset, size_t size,
89 int direction, dma_addr_t dma_addr, 96 int direction, dma_addr_t dma_addr,
90 bool map_single) 97 bool map_single)
91 { 98 {
92 } 99 }
93 100
94 static inline void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, 101 static inline void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
95 size_t size, int direction, 102 size_t size, int direction,
96 bool map_single) 103 bool map_single)
97 { 104 {
98 } 105 }
99 106
100 static inline void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, 107 static inline void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
101 int nents, int mapped_ents, int direction) 108 int nents, int mapped_ents, int direction)
102 { 109 {
103 } 110 }
104 111
105 static inline void debug_dma_unmap_sg(struct device *dev, 112 static inline void debug_dma_unmap_sg(struct device *dev,
106 struct scatterlist *sglist, 113 struct scatterlist *sglist,
107 int nelems, int dir) 114 int nelems, int dir)
108 { 115 {
109 } 116 }
110 117
111 static inline void debug_dma_alloc_coherent(struct device *dev, size_t size, 118 static inline void debug_dma_alloc_coherent(struct device *dev, size_t size,
112 dma_addr_t dma_addr, void *virt) 119 dma_addr_t dma_addr, void *virt)
113 { 120 {
114 } 121 }
115 122
116 static inline void debug_dma_free_coherent(struct device *dev, size_t size, 123 static inline void debug_dma_free_coherent(struct device *dev, size_t size,
117 void *virt, dma_addr_t addr) 124 void *virt, dma_addr_t addr)
118 { 125 {
119 } 126 }
120 127
121 static inline void debug_dma_sync_single_for_cpu(struct device *dev, 128 static inline void debug_dma_sync_single_for_cpu(struct device *dev,
122 dma_addr_t dma_handle, 129 dma_addr_t dma_handle,
123 size_t size, int direction) 130 size_t size, int direction)
124 { 131 {
125 } 132 }
126 133
127 static inline void debug_dma_sync_single_for_device(struct device *dev, 134 static inline void debug_dma_sync_single_for_device(struct device *dev,
128 dma_addr_t dma_handle, 135 dma_addr_t dma_handle,
129 size_t size, int direction) 136 size_t size, int direction)
130 { 137 {
131 } 138 }
132 139
133 static inline void debug_dma_sync_single_range_for_cpu(struct device *dev, 140 static inline void debug_dma_sync_single_range_for_cpu(struct device *dev,
134 dma_addr_t dma_handle, 141 dma_addr_t dma_handle,
135 unsigned long offset, 142 unsigned long offset,
136 size_t size, 143 size_t size,
137 int direction) 144 int direction)
138 { 145 {
139 } 146 }
140 147
141 static inline void debug_dma_sync_single_range_for_device(struct device *dev, 148 static inline void debug_dma_sync_single_range_for_device(struct device *dev,
142 dma_addr_t dma_handle, 149 dma_addr_t dma_handle,
143 unsigned long offset, 150 unsigned long offset,
144 size_t size, 151 size_t size,
145 int direction) 152 int direction)
146 { 153 {
147 } 154 }
148 155
149 static inline void debug_dma_sync_sg_for_cpu(struct device *dev, 156 static inline void debug_dma_sync_sg_for_cpu(struct device *dev,
150 struct scatterlist *sg, 157 struct scatterlist *sg,
151 int nelems, int direction) 158 int nelems, int direction)
152 { 159 {
153 } 160 }
154 161
155 static inline void debug_dma_sync_sg_for_device(struct device *dev, 162 static inline void debug_dma_sync_sg_for_device(struct device *dev,
156 struct scatterlist *sg, 163 struct scatterlist *sg,
157 int nelems, int direction) 164 int nelems, int direction)
158 { 165 {
159 } 166 }
160 167
161 static inline void debug_dma_dump_mappings(struct device *dev) 168 static inline void debug_dma_dump_mappings(struct device *dev)
162 { 169 {
163 } 170 }
164 171
165 #endif /* CONFIG_DMA_API_DEBUG */ 172 #endif /* CONFIG_DMA_API_DEBUG */
166 173
167 #endif /* __DMA_DEBUG_H */ 174 #endif /* __DMA_DEBUG_H */
168 175
1 /* 1 /*
2 * Copyright (C) 2008 Advanced Micro Devices, Inc. 2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
3 * 3 *
4 * Author: Joerg Roedel <joerg.roedel@amd.com> 4 * Author: Joerg Roedel <joerg.roedel@amd.com>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify it 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 7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation. 8 * by the Free Software Foundation.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 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 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 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 18 */
19 19
20 #include <linux/scatterlist.h> 20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h> 21 #include <linux/dma-mapping.h>
22 #include <linux/stacktrace.h> 22 #include <linux/stacktrace.h>
23 #include <linux/dma-debug.h> 23 #include <linux/dma-debug.h>
24 #include <linux/spinlock.h> 24 #include <linux/spinlock.h>
25 #include <linux/debugfs.h> 25 #include <linux/debugfs.h>
26 #include <linux/device.h> 26 #include <linux/device.h>
27 #include <linux/types.h> 27 #include <linux/types.h>
28 #include <linux/sched.h> 28 #include <linux/sched.h>
29 #include <linux/list.h> 29 #include <linux/list.h>
30 #include <linux/slab.h> 30 #include <linux/slab.h>
31 31
32 #include <asm/sections.h> 32 #include <asm/sections.h>
33 33
34 #define HASH_SIZE 1024ULL 34 #define HASH_SIZE 1024ULL
35 #define HASH_FN_SHIFT 13 35 #define HASH_FN_SHIFT 13
36 #define HASH_FN_MASK (HASH_SIZE - 1) 36 #define HASH_FN_MASK (HASH_SIZE - 1)
37 37
38 enum { 38 enum {
39 dma_debug_single, 39 dma_debug_single,
40 dma_debug_page, 40 dma_debug_page,
41 dma_debug_sg, 41 dma_debug_sg,
42 dma_debug_coherent, 42 dma_debug_coherent,
43 }; 43 };
44 44
45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5 45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
46 46
47 struct dma_debug_entry { 47 struct dma_debug_entry {
48 struct list_head list; 48 struct list_head list;
49 struct device *dev; 49 struct device *dev;
50 int type; 50 int type;
51 phys_addr_t paddr; 51 phys_addr_t paddr;
52 u64 dev_addr; 52 u64 dev_addr;
53 u64 size; 53 u64 size;
54 int direction; 54 int direction;
55 int sg_call_ents; 55 int sg_call_ents;
56 int sg_mapped_ents; 56 int sg_mapped_ents;
57 #ifdef CONFIG_STACKTRACE 57 #ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace; 58 struct stack_trace stacktrace;
59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES]; 59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
60 #endif 60 #endif
61 }; 61 };
62 62
63 struct hash_bucket { 63 struct hash_bucket {
64 struct list_head list; 64 struct list_head list;
65 spinlock_t lock; 65 spinlock_t lock;
66 } ____cacheline_aligned_in_smp; 66 } ____cacheline_aligned_in_smp;
67 67
68 /* Hash list to save the allocated dma addresses */ 68 /* Hash list to save the allocated dma addresses */
69 static struct hash_bucket dma_entry_hash[HASH_SIZE]; 69 static struct hash_bucket dma_entry_hash[HASH_SIZE];
70 /* List of pre-allocated dma_debug_entry's */ 70 /* List of pre-allocated dma_debug_entry's */
71 static LIST_HEAD(free_entries); 71 static LIST_HEAD(free_entries);
72 /* Lock for the list above */ 72 /* Lock for the list above */
73 static DEFINE_SPINLOCK(free_entries_lock); 73 static DEFINE_SPINLOCK(free_entries_lock);
74 74
75 /* Global disable flag - will be set in case of an error */ 75 /* Global disable flag - will be set in case of an error */
76 static bool global_disable __read_mostly; 76 static bool global_disable __read_mostly;
77 77
78 /* Global error count */ 78 /* Global error count */
79 static u32 error_count; 79 static u32 error_count;
80 80
81 /* Global error show enable*/ 81 /* Global error show enable*/
82 static u32 show_all_errors __read_mostly; 82 static u32 show_all_errors __read_mostly;
83 /* Number of errors to show */ 83 /* Number of errors to show */
84 static u32 show_num_errors = 1; 84 static u32 show_num_errors = 1;
85 85
86 static u32 num_free_entries; 86 static u32 num_free_entries;
87 static u32 min_free_entries; 87 static u32 min_free_entries;
88 88
89 /* number of preallocated entries requested by kernel cmdline */ 89 /* number of preallocated entries requested by kernel cmdline */
90 static u32 req_entries; 90 static u32 req_entries;
91 91
92 /* debugfs dentry's for the stuff above */ 92 /* debugfs dentry's for the stuff above */
93 static struct dentry *dma_debug_dent __read_mostly; 93 static struct dentry *dma_debug_dent __read_mostly;
94 static struct dentry *global_disable_dent __read_mostly; 94 static struct dentry *global_disable_dent __read_mostly;
95 static struct dentry *error_count_dent __read_mostly; 95 static struct dentry *error_count_dent __read_mostly;
96 static struct dentry *show_all_errors_dent __read_mostly; 96 static struct dentry *show_all_errors_dent __read_mostly;
97 static struct dentry *show_num_errors_dent __read_mostly; 97 static struct dentry *show_num_errors_dent __read_mostly;
98 static struct dentry *num_free_entries_dent __read_mostly; 98 static struct dentry *num_free_entries_dent __read_mostly;
99 static struct dentry *min_free_entries_dent __read_mostly; 99 static struct dentry *min_free_entries_dent __read_mostly;
100 100
101 static const char *type2name[4] = { "single", "page", 101 static const char *type2name[4] = { "single", "page",
102 "scather-gather", "coherent" }; 102 "scather-gather", "coherent" };
103 103
104 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", 104 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
105 "DMA_FROM_DEVICE", "DMA_NONE" }; 105 "DMA_FROM_DEVICE", "DMA_NONE" };
106 106
107 /* 107 /*
108 * The access to some variables in this macro is racy. We can't use atomic_t 108 * The access to some variables in this macro is racy. We can't use atomic_t
109 * here because all these variables are exported to debugfs. Some of them even 109 * here because all these variables are exported to debugfs. Some of them even
110 * writeable. This is also the reason why a lock won't help much. But anyway, 110 * writeable. This is also the reason why a lock won't help much. But anyway,
111 * the races are no big deal. Here is why: 111 * the races are no big deal. Here is why:
112 * 112 *
113 * error_count: the addition is racy, but the worst thing that can happen is 113 * error_count: the addition is racy, but the worst thing that can happen is
114 * that we don't count some errors 114 * that we don't count some errors
115 * show_num_errors: the subtraction is racy. Also no big deal because in 115 * show_num_errors: the subtraction is racy. Also no big deal because in
116 * worst case this will result in one warning more in the 116 * worst case this will result in one warning more in the
117 * system log than the user configured. This variable is 117 * system log than the user configured. This variable is
118 * writeable via debugfs. 118 * writeable via debugfs.
119 */ 119 */
120 static inline void dump_entry_trace(struct dma_debug_entry *entry) 120 static inline void dump_entry_trace(struct dma_debug_entry *entry)
121 { 121 {
122 #ifdef CONFIG_STACKTRACE 122 #ifdef CONFIG_STACKTRACE
123 if (entry) { 123 if (entry) {
124 printk(KERN_WARNING "Mapped at:\n"); 124 printk(KERN_WARNING "Mapped at:\n");
125 print_stack_trace(&entry->stacktrace, 0); 125 print_stack_trace(&entry->stacktrace, 0);
126 } 126 }
127 #endif 127 #endif
128 } 128 }
129 129
130 #define err_printk(dev, entry, format, arg...) do { \ 130 #define err_printk(dev, entry, format, arg...) do { \
131 error_count += 1; \ 131 error_count += 1; \
132 if (show_all_errors || show_num_errors > 0) { \ 132 if (show_all_errors || show_num_errors > 0) { \
133 WARN(1, "%s %s: " format, \ 133 WARN(1, "%s %s: " format, \
134 dev_driver_string(dev), \ 134 dev_driver_string(dev), \
135 dev_name(dev) , ## arg); \ 135 dev_name(dev) , ## arg); \
136 dump_entry_trace(entry); \ 136 dump_entry_trace(entry); \
137 } \ 137 } \
138 if (!show_all_errors && show_num_errors > 0) \ 138 if (!show_all_errors && show_num_errors > 0) \
139 show_num_errors -= 1; \ 139 show_num_errors -= 1; \
140 } while (0); 140 } while (0);
141 141
142 /* 142 /*
143 * Hash related functions 143 * Hash related functions
144 * 144 *
145 * Every DMA-API request is saved into a struct dma_debug_entry. To 145 * Every DMA-API request is saved into a struct dma_debug_entry. To
146 * have quick access to these structs they are stored into a hash. 146 * have quick access to these structs they are stored into a hash.
147 */ 147 */
148 static int hash_fn(struct dma_debug_entry *entry) 148 static int hash_fn(struct dma_debug_entry *entry)
149 { 149 {
150 /* 150 /*
151 * Hash function is based on the dma address. 151 * Hash function is based on the dma address.
152 * We use bits 20-27 here as the index into the hash 152 * We use bits 20-27 here as the index into the hash
153 */ 153 */
154 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; 154 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
155 } 155 }
156 156
157 /* 157 /*
158 * Request exclusive access to a hash bucket for a given dma_debug_entry. 158 * Request exclusive access to a hash bucket for a given dma_debug_entry.
159 */ 159 */
160 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, 160 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
161 unsigned long *flags) 161 unsigned long *flags)
162 { 162 {
163 int idx = hash_fn(entry); 163 int idx = hash_fn(entry);
164 unsigned long __flags; 164 unsigned long __flags;
165 165
166 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags); 166 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
167 *flags = __flags; 167 *flags = __flags;
168 return &dma_entry_hash[idx]; 168 return &dma_entry_hash[idx];
169 } 169 }
170 170
171 /* 171 /*
172 * Give up exclusive access to the hash bucket 172 * Give up exclusive access to the hash bucket
173 */ 173 */
174 static void put_hash_bucket(struct hash_bucket *bucket, 174 static void put_hash_bucket(struct hash_bucket *bucket,
175 unsigned long *flags) 175 unsigned long *flags)
176 { 176 {
177 unsigned long __flags = *flags; 177 unsigned long __flags = *flags;
178 178
179 spin_unlock_irqrestore(&bucket->lock, __flags); 179 spin_unlock_irqrestore(&bucket->lock, __flags);
180 } 180 }
181 181
182 /* 182 /*
183 * Search a given entry in the hash bucket list 183 * Search a given entry in the hash bucket list
184 */ 184 */
185 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket, 185 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
186 struct dma_debug_entry *ref) 186 struct dma_debug_entry *ref)
187 { 187 {
188 struct dma_debug_entry *entry; 188 struct dma_debug_entry *entry;
189 189
190 list_for_each_entry(entry, &bucket->list, list) { 190 list_for_each_entry(entry, &bucket->list, list) {
191 if ((entry->dev_addr == ref->dev_addr) && 191 if ((entry->dev_addr == ref->dev_addr) &&
192 (entry->dev == ref->dev)) 192 (entry->dev == ref->dev))
193 return entry; 193 return entry;
194 } 194 }
195 195
196 return NULL; 196 return NULL;
197 } 197 }
198 198
199 /* 199 /*
200 * Add an entry to a hash bucket 200 * Add an entry to a hash bucket
201 */ 201 */
202 static void hash_bucket_add(struct hash_bucket *bucket, 202 static void hash_bucket_add(struct hash_bucket *bucket,
203 struct dma_debug_entry *entry) 203 struct dma_debug_entry *entry)
204 { 204 {
205 list_add_tail(&entry->list, &bucket->list); 205 list_add_tail(&entry->list, &bucket->list);
206 } 206 }
207 207
208 /* 208 /*
209 * Remove entry from a hash bucket list 209 * Remove entry from a hash bucket list
210 */ 210 */
211 static void hash_bucket_del(struct dma_debug_entry *entry) 211 static void hash_bucket_del(struct dma_debug_entry *entry)
212 { 212 {
213 list_del(&entry->list); 213 list_del(&entry->list);
214 } 214 }
215 215
216 /* 216 /*
217 * Dump mapping entries for debugging purposes 217 * Dump mapping entries for debugging purposes
218 */ 218 */
219 void debug_dma_dump_mappings(struct device *dev) 219 void debug_dma_dump_mappings(struct device *dev)
220 { 220 {
221 int idx; 221 int idx;
222 222
223 for (idx = 0; idx < HASH_SIZE; idx++) { 223 for (idx = 0; idx < HASH_SIZE; idx++) {
224 struct hash_bucket *bucket = &dma_entry_hash[idx]; 224 struct hash_bucket *bucket = &dma_entry_hash[idx];
225 struct dma_debug_entry *entry; 225 struct dma_debug_entry *entry;
226 unsigned long flags; 226 unsigned long flags;
227 227
228 spin_lock_irqsave(&bucket->lock, flags); 228 spin_lock_irqsave(&bucket->lock, flags);
229 229
230 list_for_each_entry(entry, &bucket->list, list) { 230 list_for_each_entry(entry, &bucket->list, list) {
231 if (!dev || dev == entry->dev) { 231 if (!dev || dev == entry->dev) {
232 dev_info(entry->dev, 232 dev_info(entry->dev,
233 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n", 233 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
234 type2name[entry->type], idx, 234 type2name[entry->type], idx,
235 (unsigned long long)entry->paddr, 235 (unsigned long long)entry->paddr,
236 entry->dev_addr, entry->size, 236 entry->dev_addr, entry->size,
237 dir2name[entry->direction]); 237 dir2name[entry->direction]);
238 } 238 }
239 } 239 }
240 240
241 spin_unlock_irqrestore(&bucket->lock, flags); 241 spin_unlock_irqrestore(&bucket->lock, flags);
242 } 242 }
243 } 243 }
244 EXPORT_SYMBOL(debug_dma_dump_mappings); 244 EXPORT_SYMBOL(debug_dma_dump_mappings);
245 245
246 /* 246 /*
247 * Wrapper function for adding an entry to the hash. 247 * Wrapper function for adding an entry to the hash.
248 * This function takes care of locking itself. 248 * This function takes care of locking itself.
249 */ 249 */
250 static void add_dma_entry(struct dma_debug_entry *entry) 250 static void add_dma_entry(struct dma_debug_entry *entry)
251 { 251 {
252 struct hash_bucket *bucket; 252 struct hash_bucket *bucket;
253 unsigned long flags; 253 unsigned long flags;
254 254
255 bucket = get_hash_bucket(entry, &flags); 255 bucket = get_hash_bucket(entry, &flags);
256 hash_bucket_add(bucket, entry); 256 hash_bucket_add(bucket, entry);
257 put_hash_bucket(bucket, &flags); 257 put_hash_bucket(bucket, &flags);
258 } 258 }
259 259
260 /* struct dma_entry allocator 260 /* struct dma_entry allocator
261 * 261 *
262 * The next two functions implement the allocator for 262 * The next two functions implement the allocator for
263 * struct dma_debug_entries. 263 * struct dma_debug_entries.
264 */ 264 */
265 static struct dma_debug_entry *dma_entry_alloc(void) 265 static struct dma_debug_entry *dma_entry_alloc(void)
266 { 266 {
267 struct dma_debug_entry *entry = NULL; 267 struct dma_debug_entry *entry = NULL;
268 unsigned long flags; 268 unsigned long flags;
269 269
270 spin_lock_irqsave(&free_entries_lock, flags); 270 spin_lock_irqsave(&free_entries_lock, flags);
271 271
272 if (list_empty(&free_entries)) { 272 if (list_empty(&free_entries)) {
273 printk(KERN_ERR "DMA-API: debugging out of memory " 273 printk(KERN_ERR "DMA-API: debugging out of memory "
274 "- disabling\n"); 274 "- disabling\n");
275 global_disable = true; 275 global_disable = true;
276 goto out; 276 goto out;
277 } 277 }
278 278
279 entry = list_entry(free_entries.next, struct dma_debug_entry, list); 279 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
280 list_del(&entry->list); 280 list_del(&entry->list);
281 memset(entry, 0, sizeof(*entry)); 281 memset(entry, 0, sizeof(*entry));
282 282
283 #ifdef CONFIG_STACKTRACE 283 #ifdef CONFIG_STACKTRACE
284 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; 284 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
285 entry->stacktrace.entries = entry->st_entries; 285 entry->stacktrace.entries = entry->st_entries;
286 entry->stacktrace.skip = 2; 286 entry->stacktrace.skip = 2;
287 save_stack_trace(&entry->stacktrace); 287 save_stack_trace(&entry->stacktrace);
288 #endif 288 #endif
289 num_free_entries -= 1; 289 num_free_entries -= 1;
290 if (num_free_entries < min_free_entries) 290 if (num_free_entries < min_free_entries)
291 min_free_entries = num_free_entries; 291 min_free_entries = num_free_entries;
292 292
293 out: 293 out:
294 spin_unlock_irqrestore(&free_entries_lock, flags); 294 spin_unlock_irqrestore(&free_entries_lock, flags);
295 295
296 return entry; 296 return entry;
297 } 297 }
298 298
299 static void dma_entry_free(struct dma_debug_entry *entry) 299 static void dma_entry_free(struct dma_debug_entry *entry)
300 { 300 {
301 unsigned long flags; 301 unsigned long flags;
302 302
303 /* 303 /*
304 * add to beginning of the list - this way the entries are 304 * add to beginning of the list - this way the entries are
305 * more likely cache hot when they are reallocated. 305 * more likely cache hot when they are reallocated.
306 */ 306 */
307 spin_lock_irqsave(&free_entries_lock, flags); 307 spin_lock_irqsave(&free_entries_lock, flags);
308 list_add(&entry->list, &free_entries); 308 list_add(&entry->list, &free_entries);
309 num_free_entries += 1; 309 num_free_entries += 1;
310 spin_unlock_irqrestore(&free_entries_lock, flags); 310 spin_unlock_irqrestore(&free_entries_lock, flags);
311 } 311 }
312 312
313 /* 313 /*
314 * DMA-API debugging init code 314 * DMA-API debugging init code
315 * 315 *
316 * The init code does two things: 316 * The init code does two things:
317 * 1. Initialize core data structures 317 * 1. Initialize core data structures
318 * 2. Preallocate a given number of dma_debug_entry structs 318 * 2. Preallocate a given number of dma_debug_entry structs
319 */ 319 */
320 320
321 static int prealloc_memory(u32 num_entries) 321 static int prealloc_memory(u32 num_entries)
322 { 322 {
323 struct dma_debug_entry *entry, *next_entry; 323 struct dma_debug_entry *entry, *next_entry;
324 int i; 324 int i;
325 325
326 for (i = 0; i < num_entries; ++i) { 326 for (i = 0; i < num_entries; ++i) {
327 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 327 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
328 if (!entry) 328 if (!entry)
329 goto out_err; 329 goto out_err;
330 330
331 list_add_tail(&entry->list, &free_entries); 331 list_add_tail(&entry->list, &free_entries);
332 } 332 }
333 333
334 num_free_entries = num_entries; 334 num_free_entries = num_entries;
335 min_free_entries = num_entries; 335 min_free_entries = num_entries;
336 336
337 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n", 337 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
338 num_entries); 338 num_entries);
339 339
340 return 0; 340 return 0;
341 341
342 out_err: 342 out_err:
343 343
344 list_for_each_entry_safe(entry, next_entry, &free_entries, list) { 344 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
345 list_del(&entry->list); 345 list_del(&entry->list);
346 kfree(entry); 346 kfree(entry);
347 } 347 }
348 348
349 return -ENOMEM; 349 return -ENOMEM;
350 } 350 }
351 351
352 static int dma_debug_fs_init(void) 352 static int dma_debug_fs_init(void)
353 { 353 {
354 dma_debug_dent = debugfs_create_dir("dma-api", NULL); 354 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
355 if (!dma_debug_dent) { 355 if (!dma_debug_dent) {
356 printk(KERN_ERR "DMA-API: can not create debugfs directory\n"); 356 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
357 return -ENOMEM; 357 return -ENOMEM;
358 } 358 }
359 359
360 global_disable_dent = debugfs_create_bool("disabled", 0444, 360 global_disable_dent = debugfs_create_bool("disabled", 0444,
361 dma_debug_dent, 361 dma_debug_dent,
362 (u32 *)&global_disable); 362 (u32 *)&global_disable);
363 if (!global_disable_dent) 363 if (!global_disable_dent)
364 goto out_err; 364 goto out_err;
365 365
366 error_count_dent = debugfs_create_u32("error_count", 0444, 366 error_count_dent = debugfs_create_u32("error_count", 0444,
367 dma_debug_dent, &error_count); 367 dma_debug_dent, &error_count);
368 if (!error_count_dent) 368 if (!error_count_dent)
369 goto out_err; 369 goto out_err;
370 370
371 show_all_errors_dent = debugfs_create_u32("all_errors", 0644, 371 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
372 dma_debug_dent, 372 dma_debug_dent,
373 &show_all_errors); 373 &show_all_errors);
374 if (!show_all_errors_dent) 374 if (!show_all_errors_dent)
375 goto out_err; 375 goto out_err;
376 376
377 show_num_errors_dent = debugfs_create_u32("num_errors", 0644, 377 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
378 dma_debug_dent, 378 dma_debug_dent,
379 &show_num_errors); 379 &show_num_errors);
380 if (!show_num_errors_dent) 380 if (!show_num_errors_dent)
381 goto out_err; 381 goto out_err;
382 382
383 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444, 383 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
384 dma_debug_dent, 384 dma_debug_dent,
385 &num_free_entries); 385 &num_free_entries);
386 if (!num_free_entries_dent) 386 if (!num_free_entries_dent)
387 goto out_err; 387 goto out_err;
388 388
389 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444, 389 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
390 dma_debug_dent, 390 dma_debug_dent,
391 &min_free_entries); 391 &min_free_entries);
392 if (!min_free_entries_dent) 392 if (!min_free_entries_dent)
393 goto out_err; 393 goto out_err;
394 394
395 return 0; 395 return 0;
396 396
397 out_err: 397 out_err:
398 debugfs_remove_recursive(dma_debug_dent); 398 debugfs_remove_recursive(dma_debug_dent);
399 399
400 return -ENOMEM; 400 return -ENOMEM;
401 } 401 }
402 402
403 static int device_dma_allocations(struct device *dev)
404 {
405 struct dma_debug_entry *entry;
406 unsigned long flags;
407 int count = 0, i;
408
409 for (i = 0; i < HASH_SIZE; ++i) {
410 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
411 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
412 if (entry->dev == dev)
413 count += 1;
414 }
415 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
416 }
417
418 return count;
419 }
420
421 static int dma_debug_device_change(struct notifier_block *nb,
422 unsigned long action, void *data)
423 {
424 struct device *dev = data;
425 int count;
426
427
428 switch (action) {
429 case BUS_NOTIFY_UNBIND_DRIVER:
430 count = device_dma_allocations(dev);
431 if (count == 0)
432 break;
433 err_printk(dev, NULL, "DMA-API: device driver has pending "
434 "DMA allocations while released from device "
435 "[count=%d]\n", count);
436 break;
437 default:
438 break;
439 }
440
441 return 0;
442 }
443
444 void dma_debug_add_bus(struct bus_type *bus)
445 {
446 struct notifier_block *nb;
447
448 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
449 if (nb == NULL) {
450 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
451 return;
452 }
453
454 nb->notifier_call = dma_debug_device_change;
455
456 bus_register_notifier(bus, nb);
457 }
403 458
404 /* 459 /*
405 * Let the architectures decide how many entries should be preallocated. 460 * Let the architectures decide how many entries should be preallocated.
406 */ 461 */
407 void dma_debug_init(u32 num_entries) 462 void dma_debug_init(u32 num_entries)
408 { 463 {
409 int i; 464 int i;
410 465
411 if (global_disable) 466 if (global_disable)
412 return; 467 return;
413 468
414 for (i = 0; i < HASH_SIZE; ++i) { 469 for (i = 0; i < HASH_SIZE; ++i) {
415 INIT_LIST_HEAD(&dma_entry_hash[i].list); 470 INIT_LIST_HEAD(&dma_entry_hash[i].list);
416 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED; 471 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
417 } 472 }
418 473
419 if (dma_debug_fs_init() != 0) { 474 if (dma_debug_fs_init() != 0) {
420 printk(KERN_ERR "DMA-API: error creating debugfs entries " 475 printk(KERN_ERR "DMA-API: error creating debugfs entries "
421 "- disabling\n"); 476 "- disabling\n");
422 global_disable = true; 477 global_disable = true;
423 478
424 return; 479 return;
425 } 480 }
426 481
427 if (req_entries) 482 if (req_entries)
428 num_entries = req_entries; 483 num_entries = req_entries;
429 484
430 if (prealloc_memory(num_entries) != 0) { 485 if (prealloc_memory(num_entries) != 0) {
431 printk(KERN_ERR "DMA-API: debugging out of memory error " 486 printk(KERN_ERR "DMA-API: debugging out of memory error "
432 "- disabled\n"); 487 "- disabled\n");
433 global_disable = true; 488 global_disable = true;
434 489
435 return; 490 return;
436 } 491 }
437 492
438 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n"); 493 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
439 } 494 }
440 495
441 static __init int dma_debug_cmdline(char *str) 496 static __init int dma_debug_cmdline(char *str)
442 { 497 {
443 if (!str) 498 if (!str)
444 return -EINVAL; 499 return -EINVAL;
445 500
446 if (strncmp(str, "off", 3) == 0) { 501 if (strncmp(str, "off", 3) == 0) {
447 printk(KERN_INFO "DMA-API: debugging disabled on kernel " 502 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
448 "command line\n"); 503 "command line\n");
449 global_disable = true; 504 global_disable = true;
450 } 505 }
451 506
452 return 0; 507 return 0;
453 } 508 }
454 509
455 static __init int dma_debug_entries_cmdline(char *str) 510 static __init int dma_debug_entries_cmdline(char *str)
456 { 511 {
457 int res; 512 int res;
458 513
459 if (!str) 514 if (!str)
460 return -EINVAL; 515 return -EINVAL;
461 516
462 res = get_option(&str, &req_entries); 517 res = get_option(&str, &req_entries);
463 518
464 if (!res) 519 if (!res)
465 req_entries = 0; 520 req_entries = 0;
466 521
467 return 0; 522 return 0;
468 } 523 }
469 524
470 __setup("dma_debug=", dma_debug_cmdline); 525 __setup("dma_debug=", dma_debug_cmdline);
471 __setup("dma_debug_entries=", dma_debug_entries_cmdline); 526 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
472 527
473 static void check_unmap(struct dma_debug_entry *ref) 528 static void check_unmap(struct dma_debug_entry *ref)
474 { 529 {
475 struct dma_debug_entry *entry; 530 struct dma_debug_entry *entry;
476 struct hash_bucket *bucket; 531 struct hash_bucket *bucket;
477 unsigned long flags; 532 unsigned long flags;
478 533
479 if (dma_mapping_error(ref->dev, ref->dev_addr)) 534 if (dma_mapping_error(ref->dev, ref->dev_addr))
480 return; 535 return;
481 536
482 bucket = get_hash_bucket(ref, &flags); 537 bucket = get_hash_bucket(ref, &flags);
483 entry = hash_bucket_find(bucket, ref); 538 entry = hash_bucket_find(bucket, ref);
484 539
485 if (!entry) { 540 if (!entry) {
486 err_printk(ref->dev, NULL, "DMA-API: device driver tries " 541 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
487 "to free DMA memory it has not allocated " 542 "to free DMA memory it has not allocated "
488 "[device address=0x%016llx] [size=%llu bytes]\n", 543 "[device address=0x%016llx] [size=%llu bytes]\n",
489 ref->dev_addr, ref->size); 544 ref->dev_addr, ref->size);
490 goto out; 545 goto out;
491 } 546 }
492 547
493 if (ref->size != entry->size) { 548 if (ref->size != entry->size) {
494 err_printk(ref->dev, entry, "DMA-API: device driver frees " 549 err_printk(ref->dev, entry, "DMA-API: device driver frees "
495 "DMA memory with different size " 550 "DMA memory with different size "
496 "[device address=0x%016llx] [map size=%llu bytes] " 551 "[device address=0x%016llx] [map size=%llu bytes] "
497 "[unmap size=%llu bytes]\n", 552 "[unmap size=%llu bytes]\n",
498 ref->dev_addr, entry->size, ref->size); 553 ref->dev_addr, entry->size, ref->size);
499 } 554 }
500 555
501 if (ref->type != entry->type) { 556 if (ref->type != entry->type) {
502 err_printk(ref->dev, entry, "DMA-API: device driver frees " 557 err_printk(ref->dev, entry, "DMA-API: device driver frees "
503 "DMA memory with wrong function " 558 "DMA memory with wrong function "
504 "[device address=0x%016llx] [size=%llu bytes] " 559 "[device address=0x%016llx] [size=%llu bytes] "
505 "[mapped as %s] [unmapped as %s]\n", 560 "[mapped as %s] [unmapped as %s]\n",
506 ref->dev_addr, ref->size, 561 ref->dev_addr, ref->size,
507 type2name[entry->type], type2name[ref->type]); 562 type2name[entry->type], type2name[ref->type]);
508 } else if ((entry->type == dma_debug_coherent) && 563 } else if ((entry->type == dma_debug_coherent) &&
509 (ref->paddr != entry->paddr)) { 564 (ref->paddr != entry->paddr)) {
510 err_printk(ref->dev, entry, "DMA-API: device driver frees " 565 err_printk(ref->dev, entry, "DMA-API: device driver frees "
511 "DMA memory with different CPU address " 566 "DMA memory with different CPU address "
512 "[device address=0x%016llx] [size=%llu bytes] " 567 "[device address=0x%016llx] [size=%llu bytes] "
513 "[cpu alloc address=%p] [cpu free address=%p]", 568 "[cpu alloc address=%p] [cpu free address=%p]",
514 ref->dev_addr, ref->size, 569 ref->dev_addr, ref->size,
515 (void *)entry->paddr, (void *)ref->paddr); 570 (void *)entry->paddr, (void *)ref->paddr);
516 } 571 }
517 572
518 if (ref->sg_call_ents && ref->type == dma_debug_sg && 573 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
519 ref->sg_call_ents != entry->sg_call_ents) { 574 ref->sg_call_ents != entry->sg_call_ents) {
520 err_printk(ref->dev, entry, "DMA-API: device driver frees " 575 err_printk(ref->dev, entry, "DMA-API: device driver frees "
521 "DMA sg list with different entry count " 576 "DMA sg list with different entry count "
522 "[map count=%d] [unmap count=%d]\n", 577 "[map count=%d] [unmap count=%d]\n",
523 entry->sg_call_ents, ref->sg_call_ents); 578 entry->sg_call_ents, ref->sg_call_ents);
524 } 579 }
525 580
526 /* 581 /*
527 * This may be no bug in reality - but most implementations of the 582 * This may be no bug in reality - but most implementations of the
528 * DMA API don't handle this properly, so check for it here 583 * DMA API don't handle this properly, so check for it here
529 */ 584 */
530 if (ref->direction != entry->direction) { 585 if (ref->direction != entry->direction) {
531 err_printk(ref->dev, entry, "DMA-API: device driver frees " 586 err_printk(ref->dev, entry, "DMA-API: device driver frees "
532 "DMA memory with different direction " 587 "DMA memory with different direction "
533 "[device address=0x%016llx] [size=%llu bytes] " 588 "[device address=0x%016llx] [size=%llu bytes] "
534 "[mapped with %s] [unmapped with %s]\n", 589 "[mapped with %s] [unmapped with %s]\n",
535 ref->dev_addr, ref->size, 590 ref->dev_addr, ref->size,
536 dir2name[entry->direction], 591 dir2name[entry->direction],
537 dir2name[ref->direction]); 592 dir2name[ref->direction]);
538 } 593 }
539 594
540 hash_bucket_del(entry); 595 hash_bucket_del(entry);
541 dma_entry_free(entry); 596 dma_entry_free(entry);
542 597
543 out: 598 out:
544 put_hash_bucket(bucket, &flags); 599 put_hash_bucket(bucket, &flags);
545 } 600 }
546 601
547 static void check_for_stack(struct device *dev, void *addr) 602 static void check_for_stack(struct device *dev, void *addr)
548 { 603 {
549 if (object_is_on_stack(addr)) 604 if (object_is_on_stack(addr))
550 err_printk(dev, NULL, "DMA-API: device driver maps memory from" 605 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
551 "stack [addr=%p]\n", addr); 606 "stack [addr=%p]\n", addr);
552 } 607 }
553 608
554 static inline bool overlap(void *addr, u64 size, void *start, void *end) 609 static inline bool overlap(void *addr, u64 size, void *start, void *end)
555 { 610 {
556 void *addr2 = (char *)addr + size; 611 void *addr2 = (char *)addr + size;
557 612
558 return ((addr >= start && addr < end) || 613 return ((addr >= start && addr < end) ||
559 (addr2 >= start && addr2 < end) || 614 (addr2 >= start && addr2 < end) ||
560 ((addr < start) && (addr2 >= end))); 615 ((addr < start) && (addr2 >= end)));
561 } 616 }
562 617
563 static void check_for_illegal_area(struct device *dev, void *addr, u64 size) 618 static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
564 { 619 {
565 if (overlap(addr, size, _text, _etext) || 620 if (overlap(addr, size, _text, _etext) ||
566 overlap(addr, size, __start_rodata, __end_rodata)) 621 overlap(addr, size, __start_rodata, __end_rodata))
567 err_printk(dev, NULL, "DMA-API: device driver maps " 622 err_printk(dev, NULL, "DMA-API: device driver maps "
568 "memory from kernel text or rodata " 623 "memory from kernel text or rodata "
569 "[addr=%p] [size=%llu]\n", addr, size); 624 "[addr=%p] [size=%llu]\n", addr, size);
570 } 625 }
571 626
572 static void check_sync(struct device *dev, dma_addr_t addr, 627 static void check_sync(struct device *dev, dma_addr_t addr,
573 u64 size, u64 offset, int direction, bool to_cpu) 628 u64 size, u64 offset, int direction, bool to_cpu)
574 { 629 {
575 struct dma_debug_entry ref = { 630 struct dma_debug_entry ref = {
576 .dev = dev, 631 .dev = dev,
577 .dev_addr = addr, 632 .dev_addr = addr,
578 .size = size, 633 .size = size,
579 .direction = direction, 634 .direction = direction,
580 }; 635 };
581 struct dma_debug_entry *entry; 636 struct dma_debug_entry *entry;
582 struct hash_bucket *bucket; 637 struct hash_bucket *bucket;
583 unsigned long flags; 638 unsigned long flags;
584 639
585 bucket = get_hash_bucket(&ref, &flags); 640 bucket = get_hash_bucket(&ref, &flags);
586 641
587 entry = hash_bucket_find(bucket, &ref); 642 entry = hash_bucket_find(bucket, &ref);
588 643
589 if (!entry) { 644 if (!entry) {
590 err_printk(dev, NULL, "DMA-API: device driver tries " 645 err_printk(dev, NULL, "DMA-API: device driver tries "
591 "to sync DMA memory it has not allocated " 646 "to sync DMA memory it has not allocated "
592 "[device address=0x%016llx] [size=%llu bytes]\n", 647 "[device address=0x%016llx] [size=%llu bytes]\n",
593 addr, size); 648 addr, size);
594 goto out; 649 goto out;
595 } 650 }
596 651
597 if ((offset + size) > entry->size) { 652 if ((offset + size) > entry->size) {
598 err_printk(dev, entry, "DMA-API: device driver syncs" 653 err_printk(dev, entry, "DMA-API: device driver syncs"
599 " DMA memory outside allocated range " 654 " DMA memory outside allocated range "
600 "[device address=0x%016llx] " 655 "[device address=0x%016llx] "
601 "[allocation size=%llu bytes] [sync offset=%llu] " 656 "[allocation size=%llu bytes] [sync offset=%llu] "
602 "[sync size=%llu]\n", entry->dev_addr, entry->size, 657 "[sync size=%llu]\n", entry->dev_addr, entry->size,
603 offset, size); 658 offset, size);
604 } 659 }
605 660
606 if (direction != entry->direction) { 661 if (direction != entry->direction) {
607 err_printk(dev, entry, "DMA-API: device driver syncs " 662 err_printk(dev, entry, "DMA-API: device driver syncs "
608 "DMA memory with different direction " 663 "DMA memory with different direction "
609 "[device address=0x%016llx] [size=%llu bytes] " 664 "[device address=0x%016llx] [size=%llu bytes] "
610 "[mapped with %s] [synced with %s]\n", 665 "[mapped with %s] [synced with %s]\n",
611 addr, entry->size, 666 addr, entry->size,
612 dir2name[entry->direction], 667 dir2name[entry->direction],
613 dir2name[direction]); 668 dir2name[direction]);
614 } 669 }
615 670
616 if (entry->direction == DMA_BIDIRECTIONAL) 671 if (entry->direction == DMA_BIDIRECTIONAL)
617 goto out; 672 goto out;
618 673
619 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && 674 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
620 !(direction == DMA_TO_DEVICE)) 675 !(direction == DMA_TO_DEVICE))
621 err_printk(dev, entry, "DMA-API: device driver syncs " 676 err_printk(dev, entry, "DMA-API: device driver syncs "
622 "device read-only DMA memory for cpu " 677 "device read-only DMA memory for cpu "
623 "[device address=0x%016llx] [size=%llu bytes] " 678 "[device address=0x%016llx] [size=%llu bytes] "
624 "[mapped with %s] [synced with %s]\n", 679 "[mapped with %s] [synced with %s]\n",
625 addr, entry->size, 680 addr, entry->size,
626 dir2name[entry->direction], 681 dir2name[entry->direction],
627 dir2name[direction]); 682 dir2name[direction]);
628 683
629 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && 684 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
630 !(direction == DMA_FROM_DEVICE)) 685 !(direction == DMA_FROM_DEVICE))
631 err_printk(dev, entry, "DMA-API: device driver syncs " 686 err_printk(dev, entry, "DMA-API: device driver syncs "
632 "device write-only DMA memory to device " 687 "device write-only DMA memory to device "
633 "[device address=0x%016llx] [size=%llu bytes] " 688 "[device address=0x%016llx] [size=%llu bytes] "
634 "[mapped with %s] [synced with %s]\n", 689 "[mapped with %s] [synced with %s]\n",
635 addr, entry->size, 690 addr, entry->size,
636 dir2name[entry->direction], 691 dir2name[entry->direction],
637 dir2name[direction]); 692 dir2name[direction]);
638 693
639 out: 694 out:
640 put_hash_bucket(bucket, &flags); 695 put_hash_bucket(bucket, &flags);
641 696
642 } 697 }
643 698
644 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, 699 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
645 size_t size, int direction, dma_addr_t dma_addr, 700 size_t size, int direction, dma_addr_t dma_addr,
646 bool map_single) 701 bool map_single)
647 { 702 {
648 struct dma_debug_entry *entry; 703 struct dma_debug_entry *entry;
649 704
650 if (unlikely(global_disable)) 705 if (unlikely(global_disable))
651 return; 706 return;
652 707
653 if (unlikely(dma_mapping_error(dev, dma_addr))) 708 if (unlikely(dma_mapping_error(dev, dma_addr)))
654 return; 709 return;
655 710
656 entry = dma_entry_alloc(); 711 entry = dma_entry_alloc();
657 if (!entry) 712 if (!entry)
658 return; 713 return;
659 714
660 entry->dev = dev; 715 entry->dev = dev;
661 entry->type = dma_debug_page; 716 entry->type = dma_debug_page;
662 entry->paddr = page_to_phys(page) + offset; 717 entry->paddr = page_to_phys(page) + offset;
663 entry->dev_addr = dma_addr; 718 entry->dev_addr = dma_addr;
664 entry->size = size; 719 entry->size = size;
665 entry->direction = direction; 720 entry->direction = direction;
666 721
667 if (map_single) { 722 if (map_single) {
668 void *addr = ((char *)page_address(page)) + offset; 723 void *addr = ((char *)page_address(page)) + offset;
669 724
670 entry->type = dma_debug_single; 725 entry->type = dma_debug_single;
671 check_for_stack(dev, addr); 726 check_for_stack(dev, addr);
672 check_for_illegal_area(dev, addr, size); 727 check_for_illegal_area(dev, addr, size);
673 } 728 }
674 729
675 add_dma_entry(entry); 730 add_dma_entry(entry);
676 } 731 }
677 EXPORT_SYMBOL(debug_dma_map_page); 732 EXPORT_SYMBOL(debug_dma_map_page);
678 733
679 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, 734 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
680 size_t size, int direction, bool map_single) 735 size_t size, int direction, bool map_single)
681 { 736 {
682 struct dma_debug_entry ref = { 737 struct dma_debug_entry ref = {
683 .type = dma_debug_page, 738 .type = dma_debug_page,
684 .dev = dev, 739 .dev = dev,
685 .dev_addr = addr, 740 .dev_addr = addr,
686 .size = size, 741 .size = size,
687 .direction = direction, 742 .direction = direction,
688 }; 743 };
689 744
690 if (unlikely(global_disable)) 745 if (unlikely(global_disable))
691 return; 746 return;
692 747
693 if (map_single) 748 if (map_single)
694 ref.type = dma_debug_single; 749 ref.type = dma_debug_single;
695 750
696 check_unmap(&ref); 751 check_unmap(&ref);
697 } 752 }
698 EXPORT_SYMBOL(debug_dma_unmap_page); 753 EXPORT_SYMBOL(debug_dma_unmap_page);
699 754
700 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, 755 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
701 int nents, int mapped_ents, int direction) 756 int nents, int mapped_ents, int direction)
702 { 757 {
703 struct dma_debug_entry *entry; 758 struct dma_debug_entry *entry;
704 struct scatterlist *s; 759 struct scatterlist *s;
705 int i; 760 int i;
706 761
707 if (unlikely(global_disable)) 762 if (unlikely(global_disable))
708 return; 763 return;
709 764
710 for_each_sg(sg, s, mapped_ents, i) { 765 for_each_sg(sg, s, mapped_ents, i) {
711 entry = dma_entry_alloc(); 766 entry = dma_entry_alloc();
712 if (!entry) 767 if (!entry)
713 return; 768 return;
714 769
715 entry->type = dma_debug_sg; 770 entry->type = dma_debug_sg;
716 entry->dev = dev; 771 entry->dev = dev;
717 entry->paddr = sg_phys(s); 772 entry->paddr = sg_phys(s);
718 entry->size = s->length; 773 entry->size = s->length;
719 entry->dev_addr = s->dma_address; 774 entry->dev_addr = s->dma_address;
720 entry->direction = direction; 775 entry->direction = direction;
721 entry->sg_call_ents = nents; 776 entry->sg_call_ents = nents;
722 entry->sg_mapped_ents = mapped_ents; 777 entry->sg_mapped_ents = mapped_ents;
723 778
724 check_for_stack(dev, sg_virt(s)); 779 check_for_stack(dev, sg_virt(s));
725 check_for_illegal_area(dev, sg_virt(s), s->length); 780 check_for_illegal_area(dev, sg_virt(s), s->length);
726 781
727 add_dma_entry(entry); 782 add_dma_entry(entry);
728 } 783 }
729 } 784 }
730 EXPORT_SYMBOL(debug_dma_map_sg); 785 EXPORT_SYMBOL(debug_dma_map_sg);
731 786
732 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, 787 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
733 int nelems, int dir) 788 int nelems, int dir)
734 { 789 {
735 struct dma_debug_entry *entry; 790 struct dma_debug_entry *entry;
736 struct scatterlist *s; 791 struct scatterlist *s;
737 int mapped_ents = 0, i; 792 int mapped_ents = 0, i;
738 unsigned long flags; 793 unsigned long flags;
739 794
740 if (unlikely(global_disable)) 795 if (unlikely(global_disable))
741 return; 796 return;
742 797
743 for_each_sg(sglist, s, nelems, i) { 798 for_each_sg(sglist, s, nelems, i) {
744 799
745 struct dma_debug_entry ref = { 800 struct dma_debug_entry ref = {
746 .type = dma_debug_sg, 801 .type = dma_debug_sg,
747 .dev = dev, 802 .dev = dev,
748 .paddr = sg_phys(s), 803 .paddr = sg_phys(s),
749 .dev_addr = s->dma_address, 804 .dev_addr = s->dma_address,
750 .size = s->length, 805 .size = s->length,
751 .direction = dir, 806 .direction = dir,
752 .sg_call_ents = 0, 807 .sg_call_ents = 0,
753 }; 808 };
754 809
755 if (mapped_ents && i >= mapped_ents) 810 if (mapped_ents && i >= mapped_ents)
756 break; 811 break;
757 812
758 if (mapped_ents == 0) { 813 if (mapped_ents == 0) {
759 struct hash_bucket *bucket; 814 struct hash_bucket *bucket;
760 ref.sg_call_ents = nelems; 815 ref.sg_call_ents = nelems;
761 bucket = get_hash_bucket(&ref, &flags); 816 bucket = get_hash_bucket(&ref, &flags);
762 entry = hash_bucket_find(bucket, &ref); 817 entry = hash_bucket_find(bucket, &ref);
763 if (entry) 818 if (entry)
764 mapped_ents = entry->sg_mapped_ents; 819 mapped_ents = entry->sg_mapped_ents;
765 put_hash_bucket(bucket, &flags); 820 put_hash_bucket(bucket, &flags);
766 } 821 }
767 822
768 check_unmap(&ref); 823 check_unmap(&ref);
769 } 824 }
770 } 825 }
771 EXPORT_SYMBOL(debug_dma_unmap_sg); 826 EXPORT_SYMBOL(debug_dma_unmap_sg);
772 827
773 void debug_dma_alloc_coherent(struct device *dev, size_t size, 828 void debug_dma_alloc_coherent(struct device *dev, size_t size,
774 dma_addr_t dma_addr, void *virt) 829 dma_addr_t dma_addr, void *virt)
775 { 830 {
776 struct dma_debug_entry *entry; 831 struct dma_debug_entry *entry;
777 832
778 if (unlikely(global_disable)) 833 if (unlikely(global_disable))
779 return; 834 return;
780 835
781 if (unlikely(virt == NULL)) 836 if (unlikely(virt == NULL))
782 return; 837 return;
783 838
784 entry = dma_entry_alloc(); 839 entry = dma_entry_alloc();
785 if (!entry) 840 if (!entry)
786 return; 841 return;
787 842
788 entry->type = dma_debug_coherent; 843 entry->type = dma_debug_coherent;
789 entry->dev = dev; 844 entry->dev = dev;
790 entry->paddr = virt_to_phys(virt); 845 entry->paddr = virt_to_phys(virt);
791 entry->size = size; 846 entry->size = size;
792 entry->dev_addr = dma_addr; 847 entry->dev_addr = dma_addr;
793 entry->direction = DMA_BIDIRECTIONAL; 848 entry->direction = DMA_BIDIRECTIONAL;
794 849
795 add_dma_entry(entry); 850 add_dma_entry(entry);
796 } 851 }
797 EXPORT_SYMBOL(debug_dma_alloc_coherent); 852 EXPORT_SYMBOL(debug_dma_alloc_coherent);
798 853
799 void debug_dma_free_coherent(struct device *dev, size_t size, 854 void debug_dma_free_coherent(struct device *dev, size_t size,
800 void *virt, dma_addr_t addr) 855 void *virt, dma_addr_t addr)
801 { 856 {
802 struct dma_debug_entry ref = { 857 struct dma_debug_entry ref = {
803 .type = dma_debug_coherent, 858 .type = dma_debug_coherent,
804 .dev = dev, 859 .dev = dev,
805 .paddr = virt_to_phys(virt), 860 .paddr = virt_to_phys(virt),
806 .dev_addr = addr, 861 .dev_addr = addr,
807 .size = size, 862 .size = size,
808 .direction = DMA_BIDIRECTIONAL, 863 .direction = DMA_BIDIRECTIONAL,
809 }; 864 };
810 865
811 if (unlikely(global_disable)) 866 if (unlikely(global_disable))
812 return; 867 return;
813 868
814 check_unmap(&ref); 869 check_unmap(&ref);
815 } 870 }
816 EXPORT_SYMBOL(debug_dma_free_coherent); 871 EXPORT_SYMBOL(debug_dma_free_coherent);
817 872
818 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, 873 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
819 size_t size, int direction) 874 size_t size, int direction)
820 { 875 {
821 if (unlikely(global_disable)) 876 if (unlikely(global_disable))
822 return; 877 return;
823 878
824 check_sync(dev, dma_handle, size, 0, direction, true); 879 check_sync(dev, dma_handle, size, 0, direction, true);
825 } 880 }
826 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu); 881 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
827 882
828 void debug_dma_sync_single_for_device(struct device *dev, 883 void debug_dma_sync_single_for_device(struct device *dev,
829 dma_addr_t dma_handle, size_t size, 884 dma_addr_t dma_handle, size_t size,
830 int direction) 885 int direction)
831 { 886 {
832 if (unlikely(global_disable)) 887 if (unlikely(global_disable))
833 return; 888 return;
834 889
835 check_sync(dev, dma_handle, size, 0, direction, false); 890 check_sync(dev, dma_handle, size, 0, direction, false);
836 } 891 }
837 EXPORT_SYMBOL(debug_dma_sync_single_for_device); 892 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
838 893
839 void debug_dma_sync_single_range_for_cpu(struct device *dev, 894 void debug_dma_sync_single_range_for_cpu(struct device *dev,
840 dma_addr_t dma_handle, 895 dma_addr_t dma_handle,
841 unsigned long offset, size_t size, 896 unsigned long offset, size_t size,
842 int direction) 897 int direction)
843 { 898 {
844 if (unlikely(global_disable)) 899 if (unlikely(global_disable))
845 return; 900 return;
846 901
847 check_sync(dev, dma_handle, size, offset, direction, true); 902 check_sync(dev, dma_handle, size, offset, direction, true);
848 } 903 }
849 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu); 904 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
850 905
851 void debug_dma_sync_single_range_for_device(struct device *dev, 906 void debug_dma_sync_single_range_for_device(struct device *dev,
852 dma_addr_t dma_handle, 907 dma_addr_t dma_handle,
853 unsigned long offset, 908 unsigned long offset,
854 size_t size, int direction) 909 size_t size, int direction)
855 { 910 {
856 if (unlikely(global_disable)) 911 if (unlikely(global_disable))
857 return; 912 return;
858 913
859 check_sync(dev, dma_handle, size, offset, direction, false); 914 check_sync(dev, dma_handle, size, offset, direction, false);
860 } 915 }
861 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device); 916 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
862 917
863 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 918 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
864 int nelems, int direction) 919 int nelems, int direction)
865 { 920 {
866 struct scatterlist *s; 921 struct scatterlist *s;
867 int i; 922 int i;
868 923
869 if (unlikely(global_disable)) 924 if (unlikely(global_disable))
870 return; 925 return;
871 926
872 for_each_sg(sg, s, nelems, i) { 927 for_each_sg(sg, s, nelems, i) {
873 check_sync(dev, s->dma_address, s->dma_length, 0, 928 check_sync(dev, s->dma_address, s->dma_length, 0,
874 direction, true); 929 direction, true);
875 } 930 }
876 } 931 }
877 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); 932 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
878 933
879 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 934 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
880 int nelems, int direction) 935 int nelems, int direction)
881 { 936 {
882 struct scatterlist *s; 937 struct scatterlist *s;
883 int i; 938 int i;
884 939
885 if (unlikely(global_disable)) 940 if (unlikely(global_disable))
886 return; 941 return;
887 942
888 for_each_sg(sg, s, nelems, i) { 943 for_each_sg(sg, s, nelems, i) {
889 check_sync(dev, s->dma_address, s->dma_length, 0, 944 check_sync(dev, s->dma_address, s->dma_length, 0,
890 direction, false); 945 direction, false);
891 } 946 }
892 } 947 }
893 EXPORT_SYMBOL(debug_dma_sync_sg_for_device); 948 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
894 949
895 950