xref: /openbmc/linux/kernel/dma/debug.c (revision c212d910)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2008 Advanced Micro Devices, Inc.
4  *
5  * Author: Joerg Roedel <joerg.roedel@amd.com>
6  */
7 
8 #define pr_fmt(fmt)	"DMA-API: " fmt
9 
10 #include <linux/sched/task_stack.h>
11 #include <linux/scatterlist.h>
12 #include <linux/dma-map-ops.h>
13 #include <linux/sched/task.h>
14 #include <linux/stacktrace.h>
15 #include <linux/spinlock.h>
16 #include <linux/vmalloc.h>
17 #include <linux/debugfs.h>
18 #include <linux/uaccess.h>
19 #include <linux/export.h>
20 #include <linux/device.h>
21 #include <linux/types.h>
22 #include <linux/sched.h>
23 #include <linux/ctype.h>
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <asm/sections.h>
27 #include "debug.h"
28 
29 #define HASH_SIZE       16384ULL
30 #define HASH_FN_SHIFT   13
31 #define HASH_FN_MASK    (HASH_SIZE - 1)
32 
33 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
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))
36 
37 enum {
38 	dma_debug_single,
39 	dma_debug_sg,
40 	dma_debug_coherent,
41 	dma_debug_resource,
42 };
43 
44 enum map_err_types {
45 	MAP_ERR_CHECK_NOT_APPLICABLE,
46 	MAP_ERR_NOT_CHECKED,
47 	MAP_ERR_CHECKED,
48 };
49 
50 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
51 
52 /**
53  * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
54  * @list: node on pre-allocated free_entries list
55  * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
56  * @dev_addr: dma address
57  * @size: length of the mapping
58  * @type: single, page, sg, coherent
59  * @direction: enum dma_data_direction
60  * @sg_call_ents: 'nents' from dma_map_sg
61  * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
62  * @pfn: page frame of the start address
63  * @offset: offset of mapping relative to pfn
64  * @map_err_type: track whether dma_mapping_error() was checked
65  * @stacktrace: support backtraces when a violation is detected
66  */
67 struct dma_debug_entry {
68 	struct list_head list;
69 	struct device    *dev;
70 	u64              dev_addr;
71 	u64              size;
72 	int              type;
73 	int              direction;
74 	int		 sg_call_ents;
75 	int		 sg_mapped_ents;
76 	unsigned long	 pfn;
77 	size_t		 offset;
78 	enum map_err_types  map_err_type;
79 #ifdef CONFIG_STACKTRACE
80 	unsigned int	stack_len;
81 	unsigned long	stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
82 #endif
83 } ____cacheline_aligned_in_smp;
84 
85 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
86 
87 struct hash_bucket {
88 	struct list_head list;
89 	spinlock_t lock;
90 };
91 
92 /* Hash list to save the allocated dma addresses */
93 static struct hash_bucket dma_entry_hash[HASH_SIZE];
94 /* List of pre-allocated dma_debug_entry's */
95 static LIST_HEAD(free_entries);
96 /* Lock for the list above */
97 static DEFINE_SPINLOCK(free_entries_lock);
98 
99 /* Global disable flag - will be set in case of an error */
100 static bool global_disable __read_mostly;
101 
102 /* Early initialization disable flag, set at the end of dma_debug_init */
103 static bool dma_debug_initialized __read_mostly;
104 
dma_debug_disabled(void)105 static inline bool dma_debug_disabled(void)
106 {
107 	return global_disable || !dma_debug_initialized;
108 }
109 
110 /* Global error count */
111 static u32 error_count;
112 
113 /* Global error show enable*/
114 static u32 show_all_errors __read_mostly;
115 /* Number of errors to show */
116 static u32 show_num_errors = 1;
117 
118 static u32 num_free_entries;
119 static u32 min_free_entries;
120 static u32 nr_total_entries;
121 
122 /* number of preallocated entries requested by kernel cmdline */
123 static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
124 
125 /* per-driver filter related state */
126 
127 #define NAME_MAX_LEN	64
128 
129 static char                  current_driver_name[NAME_MAX_LEN] __read_mostly;
130 static struct device_driver *current_driver                    __read_mostly;
131 
132 static DEFINE_RWLOCK(driver_name_lock);
133 
134 static const char *const maperr2str[] = {
135 	[MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
136 	[MAP_ERR_NOT_CHECKED] = "dma map error not checked",
137 	[MAP_ERR_CHECKED] = "dma map error checked",
138 };
139 
140 static const char *type2name[] = {
141 	[dma_debug_single] = "single",
142 	[dma_debug_sg] = "scather-gather",
143 	[dma_debug_coherent] = "coherent",
144 	[dma_debug_resource] = "resource",
145 };
146 
147 static const char *dir2name[] = {
148 	[DMA_BIDIRECTIONAL]	= "DMA_BIDIRECTIONAL",
149 	[DMA_TO_DEVICE]		= "DMA_TO_DEVICE",
150 	[DMA_FROM_DEVICE]	= "DMA_FROM_DEVICE",
151 	[DMA_NONE]		= "DMA_NONE",
152 };
153 
154 /*
155  * The access to some variables in this macro is racy. We can't use atomic_t
156  * here because all these variables are exported to debugfs. Some of them even
157  * writeable. This is also the reason why a lock won't help much. But anyway,
158  * the races are no big deal. Here is why:
159  *
160  *   error_count: the addition is racy, but the worst thing that can happen is
161  *                that we don't count some errors
162  *   show_num_errors: the subtraction is racy. Also no big deal because in
163  *                    worst case this will result in one warning more in the
164  *                    system log than the user configured. This variable is
165  *                    writeable via debugfs.
166  */
dump_entry_trace(struct dma_debug_entry * entry)167 static inline void dump_entry_trace(struct dma_debug_entry *entry)
168 {
169 #ifdef CONFIG_STACKTRACE
170 	if (entry) {
171 		pr_warn("Mapped at:\n");
172 		stack_trace_print(entry->stack_entries, entry->stack_len, 0);
173 	}
174 #endif
175 }
176 
driver_filter(struct device * dev)177 static bool driver_filter(struct device *dev)
178 {
179 	struct device_driver *drv;
180 	unsigned long flags;
181 	bool ret;
182 
183 	/* driver filter off */
184 	if (likely(!current_driver_name[0]))
185 		return true;
186 
187 	/* driver filter on and initialized */
188 	if (current_driver && dev && dev->driver == current_driver)
189 		return true;
190 
191 	/* driver filter on, but we can't filter on a NULL device... */
192 	if (!dev)
193 		return false;
194 
195 	if (current_driver || !current_driver_name[0])
196 		return false;
197 
198 	/* driver filter on but not yet initialized */
199 	drv = dev->driver;
200 	if (!drv)
201 		return false;
202 
203 	/* lock to protect against change of current_driver_name */
204 	read_lock_irqsave(&driver_name_lock, flags);
205 
206 	ret = false;
207 	if (drv->name &&
208 	    strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
209 		current_driver = drv;
210 		ret = true;
211 	}
212 
213 	read_unlock_irqrestore(&driver_name_lock, flags);
214 
215 	return ret;
216 }
217 
218 #define err_printk(dev, entry, format, arg...) do {			\
219 		error_count += 1;					\
220 		if (driver_filter(dev) &&				\
221 		    (show_all_errors || show_num_errors > 0)) {		\
222 			WARN(1, pr_fmt("%s %s: ") format,		\
223 			     dev ? dev_driver_string(dev) : "NULL",	\
224 			     dev ? dev_name(dev) : "NULL", ## arg);	\
225 			dump_entry_trace(entry);			\
226 		}							\
227 		if (!show_all_errors && show_num_errors > 0)		\
228 			show_num_errors -= 1;				\
229 	} while (0);
230 
231 /*
232  * Hash related functions
233  *
234  * Every DMA-API request is saved into a struct dma_debug_entry. To
235  * have quick access to these structs they are stored into a hash.
236  */
hash_fn(struct dma_debug_entry * entry)237 static int hash_fn(struct dma_debug_entry *entry)
238 {
239 	/*
240 	 * Hash function is based on the dma address.
241 	 * We use bits 20-27 here as the index into the hash
242 	 */
243 	return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
244 }
245 
246 /*
247  * Request exclusive access to a hash bucket for a given dma_debug_entry.
248  */
get_hash_bucket(struct dma_debug_entry * entry,unsigned long * flags)249 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
250 					   unsigned long *flags)
251 	__acquires(&dma_entry_hash[idx].lock)
252 {
253 	int idx = hash_fn(entry);
254 	unsigned long __flags;
255 
256 	spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
257 	*flags = __flags;
258 	return &dma_entry_hash[idx];
259 }
260 
261 /*
262  * Give up exclusive access to the hash bucket
263  */
put_hash_bucket(struct hash_bucket * bucket,unsigned long flags)264 static void put_hash_bucket(struct hash_bucket *bucket,
265 			    unsigned long flags)
266 	__releases(&bucket->lock)
267 {
268 	spin_unlock_irqrestore(&bucket->lock, flags);
269 }
270 
exact_match(struct dma_debug_entry * a,struct dma_debug_entry * b)271 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
272 {
273 	return ((a->dev_addr == b->dev_addr) &&
274 		(a->dev == b->dev)) ? true : false;
275 }
276 
containing_match(struct dma_debug_entry * a,struct dma_debug_entry * b)277 static bool containing_match(struct dma_debug_entry *a,
278 			     struct dma_debug_entry *b)
279 {
280 	if (a->dev != b->dev)
281 		return false;
282 
283 	if ((b->dev_addr <= a->dev_addr) &&
284 	    ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
285 		return true;
286 
287 	return false;
288 }
289 
290 /*
291  * Search a given entry in the hash bucket list
292  */
__hash_bucket_find(struct hash_bucket * bucket,struct dma_debug_entry * ref,match_fn match)293 static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
294 						  struct dma_debug_entry *ref,
295 						  match_fn match)
296 {
297 	struct dma_debug_entry *entry, *ret = NULL;
298 	int matches = 0, match_lvl, last_lvl = -1;
299 
300 	list_for_each_entry(entry, &bucket->list, list) {
301 		if (!match(ref, entry))
302 			continue;
303 
304 		/*
305 		 * Some drivers map the same physical address multiple
306 		 * times. Without a hardware IOMMU this results in the
307 		 * same device addresses being put into the dma-debug
308 		 * hash multiple times too. This can result in false
309 		 * positives being reported. Therefore we implement a
310 		 * best-fit algorithm here which returns the entry from
311 		 * the hash which fits best to the reference value
312 		 * instead of the first-fit.
313 		 */
314 		matches += 1;
315 		match_lvl = 0;
316 		entry->size         == ref->size         ? ++match_lvl : 0;
317 		entry->type         == ref->type         ? ++match_lvl : 0;
318 		entry->direction    == ref->direction    ? ++match_lvl : 0;
319 		entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
320 
321 		if (match_lvl == 4) {
322 			/* perfect-fit - return the result */
323 			return entry;
324 		} else if (match_lvl > last_lvl) {
325 			/*
326 			 * We found an entry that fits better then the
327 			 * previous one or it is the 1st match.
328 			 */
329 			last_lvl = match_lvl;
330 			ret      = entry;
331 		}
332 	}
333 
334 	/*
335 	 * If we have multiple matches but no perfect-fit, just return
336 	 * NULL.
337 	 */
338 	ret = (matches == 1) ? ret : NULL;
339 
340 	return ret;
341 }
342 
bucket_find_exact(struct hash_bucket * bucket,struct dma_debug_entry * ref)343 static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
344 						 struct dma_debug_entry *ref)
345 {
346 	return __hash_bucket_find(bucket, ref, exact_match);
347 }
348 
bucket_find_contain(struct hash_bucket ** bucket,struct dma_debug_entry * ref,unsigned long * flags)349 static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
350 						   struct dma_debug_entry *ref,
351 						   unsigned long *flags)
352 {
353 
354 	struct dma_debug_entry *entry, index = *ref;
355 	int limit = min(HASH_SIZE, (index.dev_addr >> HASH_FN_SHIFT) + 1);
356 
357 	for (int i = 0; i < limit; i++) {
358 		entry = __hash_bucket_find(*bucket, ref, containing_match);
359 
360 		if (entry)
361 			return entry;
362 
363 		/*
364 		 * Nothing found, go back a hash bucket
365 		 */
366 		put_hash_bucket(*bucket, *flags);
367 		index.dev_addr -= (1 << HASH_FN_SHIFT);
368 		*bucket = get_hash_bucket(&index, flags);
369 	}
370 
371 	return NULL;
372 }
373 
374 /*
375  * Add an entry to a hash bucket
376  */
hash_bucket_add(struct hash_bucket * bucket,struct dma_debug_entry * entry)377 static void hash_bucket_add(struct hash_bucket *bucket,
378 			    struct dma_debug_entry *entry)
379 {
380 	list_add_tail(&entry->list, &bucket->list);
381 }
382 
383 /*
384  * Remove entry from a hash bucket list
385  */
hash_bucket_del(struct dma_debug_entry * entry)386 static void hash_bucket_del(struct dma_debug_entry *entry)
387 {
388 	list_del(&entry->list);
389 }
390 
phys_addr(struct dma_debug_entry * entry)391 static unsigned long long phys_addr(struct dma_debug_entry *entry)
392 {
393 	if (entry->type == dma_debug_resource)
394 		return __pfn_to_phys(entry->pfn) + entry->offset;
395 
396 	return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
397 }
398 
399 /*
400  * For each mapping (initial cacheline in the case of
401  * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
402  * scatterlist, or the cacheline specified in dma_map_single) insert
403  * into this tree using the cacheline as the key. At
404  * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry.  If
405  * the entry already exists at insertion time add a tag as a reference
406  * count for the overlapping mappings.  For now, the overlap tracking
407  * just ensures that 'unmaps' balance 'maps' before marking the
408  * cacheline idle, but we should also be flagging overlaps as an API
409  * violation.
410  *
411  * Memory usage is mostly constrained by the maximum number of available
412  * dma-debug entries in that we need a free dma_debug_entry before
413  * inserting into the tree.  In the case of dma_map_page and
414  * dma_alloc_coherent there is only one dma_debug_entry and one
415  * dma_active_cacheline entry to track per event.  dma_map_sg(), on the
416  * other hand, consumes a single dma_debug_entry, but inserts 'nents'
417  * entries into the tree.
418  *
419  * Use __GFP_NOWARN because the printk from an OOM, to netconsole, could end
420  * up right back in the DMA debugging code, leading to a deadlock.
421  */
422 static RADIX_TREE(dma_active_cacheline, GFP_ATOMIC | __GFP_NOWARN);
423 static DEFINE_SPINLOCK(radix_lock);
424 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
425 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
426 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
427 
to_cacheline_number(struct dma_debug_entry * entry)428 static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
429 {
430 	return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
431 		(entry->offset >> L1_CACHE_SHIFT);
432 }
433 
active_cacheline_read_overlap(phys_addr_t cln)434 static int active_cacheline_read_overlap(phys_addr_t cln)
435 {
436 	int overlap = 0, i;
437 
438 	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
439 		if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
440 			overlap |= 1 << i;
441 	return overlap;
442 }
443 
active_cacheline_set_overlap(phys_addr_t cln,int overlap)444 static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
445 {
446 	int i;
447 
448 	if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
449 		return overlap;
450 
451 	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
452 		if (overlap & 1 << i)
453 			radix_tree_tag_set(&dma_active_cacheline, cln, i);
454 		else
455 			radix_tree_tag_clear(&dma_active_cacheline, cln, i);
456 
457 	return overlap;
458 }
459 
active_cacheline_inc_overlap(phys_addr_t cln)460 static void active_cacheline_inc_overlap(phys_addr_t cln)
461 {
462 	int overlap = active_cacheline_read_overlap(cln);
463 
464 	overlap = active_cacheline_set_overlap(cln, ++overlap);
465 
466 	/* If we overflowed the overlap counter then we're potentially
467 	 * leaking dma-mappings.
468 	 */
469 	WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
470 		  pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
471 		  ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
472 }
473 
active_cacheline_dec_overlap(phys_addr_t cln)474 static int active_cacheline_dec_overlap(phys_addr_t cln)
475 {
476 	int overlap = active_cacheline_read_overlap(cln);
477 
478 	return active_cacheline_set_overlap(cln, --overlap);
479 }
480 
active_cacheline_insert(struct dma_debug_entry * entry)481 static int active_cacheline_insert(struct dma_debug_entry *entry)
482 {
483 	phys_addr_t cln = to_cacheline_number(entry);
484 	unsigned long flags;
485 	int rc;
486 
487 	/* If the device is not writing memory then we don't have any
488 	 * concerns about the cpu consuming stale data.  This mitigates
489 	 * legitimate usages of overlapping mappings.
490 	 */
491 	if (entry->direction == DMA_TO_DEVICE)
492 		return 0;
493 
494 	spin_lock_irqsave(&radix_lock, flags);
495 	rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
496 	if (rc == -EEXIST)
497 		active_cacheline_inc_overlap(cln);
498 	spin_unlock_irqrestore(&radix_lock, flags);
499 
500 	return rc;
501 }
502 
active_cacheline_remove(struct dma_debug_entry * entry)503 static void active_cacheline_remove(struct dma_debug_entry *entry)
504 {
505 	phys_addr_t cln = to_cacheline_number(entry);
506 	unsigned long flags;
507 
508 	/* ...mirror the insert case */
509 	if (entry->direction == DMA_TO_DEVICE)
510 		return;
511 
512 	spin_lock_irqsave(&radix_lock, flags);
513 	/* since we are counting overlaps the final put of the
514 	 * cacheline will occur when the overlap count is 0.
515 	 * active_cacheline_dec_overlap() returns -1 in that case
516 	 */
517 	if (active_cacheline_dec_overlap(cln) < 0)
518 		radix_tree_delete(&dma_active_cacheline, cln);
519 	spin_unlock_irqrestore(&radix_lock, flags);
520 }
521 
522 /*
523  * Dump mappings entries on kernel space for debugging purposes
524  */
debug_dma_dump_mappings(struct device * dev)525 void debug_dma_dump_mappings(struct device *dev)
526 {
527 	int idx;
528 	phys_addr_t cln;
529 
530 	for (idx = 0; idx < HASH_SIZE; idx++) {
531 		struct hash_bucket *bucket = &dma_entry_hash[idx];
532 		struct dma_debug_entry *entry;
533 		unsigned long flags;
534 
535 		spin_lock_irqsave(&bucket->lock, flags);
536 		list_for_each_entry(entry, &bucket->list, list) {
537 			if (!dev || dev == entry->dev) {
538 				cln = to_cacheline_number(entry);
539 				dev_info(entry->dev,
540 					 "%s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
541 					 type2name[entry->type], idx,
542 					 phys_addr(entry), entry->pfn,
543 					 entry->dev_addr, entry->size,
544 					 &cln, dir2name[entry->direction],
545 					 maperr2str[entry->map_err_type]);
546 			}
547 		}
548 		spin_unlock_irqrestore(&bucket->lock, flags);
549 
550 		cond_resched();
551 	}
552 }
553 
554 /*
555  * Dump mappings entries on user space via debugfs
556  */
dump_show(struct seq_file * seq,void * v)557 static int dump_show(struct seq_file *seq, void *v)
558 {
559 	int idx;
560 	phys_addr_t cln;
561 
562 	for (idx = 0; idx < HASH_SIZE; idx++) {
563 		struct hash_bucket *bucket = &dma_entry_hash[idx];
564 		struct dma_debug_entry *entry;
565 		unsigned long flags;
566 
567 		spin_lock_irqsave(&bucket->lock, flags);
568 		list_for_each_entry(entry, &bucket->list, list) {
569 			cln = to_cacheline_number(entry);
570 			seq_printf(seq,
571 				   "%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx cln=%pa %s %s\n",
572 				   dev_driver_string(entry->dev),
573 				   dev_name(entry->dev),
574 				   type2name[entry->type], idx,
575 				   phys_addr(entry), entry->pfn,
576 				   entry->dev_addr, entry->size,
577 				   &cln, dir2name[entry->direction],
578 				   maperr2str[entry->map_err_type]);
579 		}
580 		spin_unlock_irqrestore(&bucket->lock, flags);
581 	}
582 	return 0;
583 }
584 DEFINE_SHOW_ATTRIBUTE(dump);
585 
586 /*
587  * Wrapper function for adding an entry to the hash.
588  * This function takes care of locking itself.
589  */
add_dma_entry(struct dma_debug_entry * entry,unsigned long attrs)590 static void add_dma_entry(struct dma_debug_entry *entry, unsigned long attrs)
591 {
592 	struct hash_bucket *bucket;
593 	unsigned long flags;
594 	int rc;
595 
596 	bucket = get_hash_bucket(entry, &flags);
597 	hash_bucket_add(bucket, entry);
598 	put_hash_bucket(bucket, flags);
599 
600 	rc = active_cacheline_insert(entry);
601 	if (rc == -ENOMEM) {
602 		pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n");
603 		global_disable = true;
604 	} else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
605 		err_printk(entry->dev, entry,
606 			"cacheline tracking EEXIST, overlapping mappings aren't supported\n");
607 	}
608 }
609 
dma_debug_create_entries(gfp_t gfp)610 static int dma_debug_create_entries(gfp_t gfp)
611 {
612 	struct dma_debug_entry *entry;
613 	int i;
614 
615 	entry = (void *)get_zeroed_page(gfp);
616 	if (!entry)
617 		return -ENOMEM;
618 
619 	for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
620 		list_add_tail(&entry[i].list, &free_entries);
621 
622 	num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
623 	nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
624 
625 	return 0;
626 }
627 
__dma_entry_alloc(void)628 static struct dma_debug_entry *__dma_entry_alloc(void)
629 {
630 	struct dma_debug_entry *entry;
631 
632 	entry = list_entry(free_entries.next, struct dma_debug_entry, list);
633 	list_del(&entry->list);
634 	memset(entry, 0, sizeof(*entry));
635 
636 	num_free_entries -= 1;
637 	if (num_free_entries < min_free_entries)
638 		min_free_entries = num_free_entries;
639 
640 	return entry;
641 }
642 
643 /*
644  * This should be called outside of free_entries_lock scope to avoid potential
645  * deadlocks with serial consoles that use DMA.
646  */
__dma_entry_alloc_check_leak(u32 nr_entries)647 static void __dma_entry_alloc_check_leak(u32 nr_entries)
648 {
649 	u32 tmp = nr_entries % nr_prealloc_entries;
650 
651 	/* Shout each time we tick over some multiple of the initial pool */
652 	if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
653 		pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
654 			nr_entries,
655 			(nr_entries / nr_prealloc_entries));
656 	}
657 }
658 
659 /* struct dma_entry allocator
660  *
661  * The next two functions implement the allocator for
662  * struct dma_debug_entries.
663  */
dma_entry_alloc(void)664 static struct dma_debug_entry *dma_entry_alloc(void)
665 {
666 	bool alloc_check_leak = false;
667 	struct dma_debug_entry *entry;
668 	unsigned long flags;
669 	u32 nr_entries;
670 
671 	spin_lock_irqsave(&free_entries_lock, flags);
672 	if (num_free_entries == 0) {
673 		if (dma_debug_create_entries(GFP_ATOMIC)) {
674 			global_disable = true;
675 			spin_unlock_irqrestore(&free_entries_lock, flags);
676 			pr_err("debugging out of memory - disabling\n");
677 			return NULL;
678 		}
679 		alloc_check_leak = true;
680 		nr_entries = nr_total_entries;
681 	}
682 
683 	entry = __dma_entry_alloc();
684 
685 	spin_unlock_irqrestore(&free_entries_lock, flags);
686 
687 	if (alloc_check_leak)
688 		__dma_entry_alloc_check_leak(nr_entries);
689 
690 #ifdef CONFIG_STACKTRACE
691 	entry->stack_len = stack_trace_save(entry->stack_entries,
692 					    ARRAY_SIZE(entry->stack_entries),
693 					    1);
694 #endif
695 	return entry;
696 }
697 
dma_entry_free(struct dma_debug_entry * entry)698 static void dma_entry_free(struct dma_debug_entry *entry)
699 {
700 	unsigned long flags;
701 
702 	active_cacheline_remove(entry);
703 
704 	/*
705 	 * add to beginning of the list - this way the entries are
706 	 * more likely cache hot when they are reallocated.
707 	 */
708 	spin_lock_irqsave(&free_entries_lock, flags);
709 	list_add(&entry->list, &free_entries);
710 	num_free_entries += 1;
711 	spin_unlock_irqrestore(&free_entries_lock, flags);
712 }
713 
714 /*
715  * DMA-API debugging init code
716  *
717  * The init code does two things:
718  *   1. Initialize core data structures
719  *   2. Preallocate a given number of dma_debug_entry structs
720  */
721 
filter_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)722 static ssize_t filter_read(struct file *file, char __user *user_buf,
723 			   size_t count, loff_t *ppos)
724 {
725 	char buf[NAME_MAX_LEN + 1];
726 	unsigned long flags;
727 	int len;
728 
729 	if (!current_driver_name[0])
730 		return 0;
731 
732 	/*
733 	 * We can't copy to userspace directly because current_driver_name can
734 	 * only be read under the driver_name_lock with irqs disabled. So
735 	 * create a temporary copy first.
736 	 */
737 	read_lock_irqsave(&driver_name_lock, flags);
738 	len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
739 	read_unlock_irqrestore(&driver_name_lock, flags);
740 
741 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
742 }
743 
filter_write(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)744 static ssize_t filter_write(struct file *file, const char __user *userbuf,
745 			    size_t count, loff_t *ppos)
746 {
747 	char buf[NAME_MAX_LEN];
748 	unsigned long flags;
749 	size_t len;
750 	int i;
751 
752 	/*
753 	 * We can't copy from userspace directly. Access to
754 	 * current_driver_name is protected with a write_lock with irqs
755 	 * disabled. Since copy_from_user can fault and may sleep we
756 	 * need to copy to temporary buffer first
757 	 */
758 	len = min(count, (size_t)(NAME_MAX_LEN - 1));
759 	if (copy_from_user(buf, userbuf, len))
760 		return -EFAULT;
761 
762 	buf[len] = 0;
763 
764 	write_lock_irqsave(&driver_name_lock, flags);
765 
766 	/*
767 	 * Now handle the string we got from userspace very carefully.
768 	 * The rules are:
769 	 *         - only use the first token we got
770 	 *         - token delimiter is everything looking like a space
771 	 *           character (' ', '\n', '\t' ...)
772 	 *
773 	 */
774 	if (!isalnum(buf[0])) {
775 		/*
776 		 * If the first character userspace gave us is not
777 		 * alphanumerical then assume the filter should be
778 		 * switched off.
779 		 */
780 		if (current_driver_name[0])
781 			pr_info("switching off dma-debug driver filter\n");
782 		current_driver_name[0] = 0;
783 		current_driver = NULL;
784 		goto out_unlock;
785 	}
786 
787 	/*
788 	 * Now parse out the first token and use it as the name for the
789 	 * driver to filter for.
790 	 */
791 	for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
792 		current_driver_name[i] = buf[i];
793 		if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
794 			break;
795 	}
796 	current_driver_name[i] = 0;
797 	current_driver = NULL;
798 
799 	pr_info("enable driver filter for driver [%s]\n",
800 		current_driver_name);
801 
802 out_unlock:
803 	write_unlock_irqrestore(&driver_name_lock, flags);
804 
805 	return count;
806 }
807 
808 static const struct file_operations filter_fops = {
809 	.read  = filter_read,
810 	.write = filter_write,
811 	.llseek = default_llseek,
812 };
813 
dma_debug_fs_init(void)814 static int __init dma_debug_fs_init(void)
815 {
816 	struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
817 
818 	debugfs_create_bool("disabled", 0444, dentry, &global_disable);
819 	debugfs_create_u32("error_count", 0444, dentry, &error_count);
820 	debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
821 	debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
822 	debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
823 	debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
824 	debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
825 	debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
826 	debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
827 
828 	return 0;
829 }
830 core_initcall_sync(dma_debug_fs_init);
831 
device_dma_allocations(struct device * dev,struct dma_debug_entry ** out_entry)832 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
833 {
834 	struct dma_debug_entry *entry;
835 	unsigned long flags;
836 	int count = 0, i;
837 
838 	for (i = 0; i < HASH_SIZE; ++i) {
839 		spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
840 		list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
841 			if (entry->dev == dev) {
842 				count += 1;
843 				*out_entry = entry;
844 			}
845 		}
846 		spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
847 	}
848 
849 	return count;
850 }
851 
dma_debug_device_change(struct notifier_block * nb,unsigned long action,void * data)852 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
853 {
854 	struct device *dev = data;
855 	struct dma_debug_entry *entry;
856 	int count;
857 
858 	if (dma_debug_disabled())
859 		return 0;
860 
861 	switch (action) {
862 	case BUS_NOTIFY_UNBOUND_DRIVER:
863 		count = device_dma_allocations(dev, &entry);
864 		if (count == 0)
865 			break;
866 		err_printk(dev, entry, "device driver has pending "
867 				"DMA allocations while released from device "
868 				"[count=%d]\n"
869 				"One of leaked entries details: "
870 				"[device address=0x%016llx] [size=%llu bytes] "
871 				"[mapped with %s] [mapped as %s]\n",
872 			count, entry->dev_addr, entry->size,
873 			dir2name[entry->direction], type2name[entry->type]);
874 		break;
875 	default:
876 		break;
877 	}
878 
879 	return 0;
880 }
881 
dma_debug_add_bus(struct bus_type * bus)882 void dma_debug_add_bus(struct bus_type *bus)
883 {
884 	struct notifier_block *nb;
885 
886 	if (dma_debug_disabled())
887 		return;
888 
889 	nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
890 	if (nb == NULL) {
891 		pr_err("dma_debug_add_bus: out of memory\n");
892 		return;
893 	}
894 
895 	nb->notifier_call = dma_debug_device_change;
896 
897 	bus_register_notifier(bus, nb);
898 }
899 
dma_debug_init(void)900 static int dma_debug_init(void)
901 {
902 	int i, nr_pages;
903 
904 	/* Do not use dma_debug_initialized here, since we really want to be
905 	 * called to set dma_debug_initialized
906 	 */
907 	if (global_disable)
908 		return 0;
909 
910 	for (i = 0; i < HASH_SIZE; ++i) {
911 		INIT_LIST_HEAD(&dma_entry_hash[i].list);
912 		spin_lock_init(&dma_entry_hash[i].lock);
913 	}
914 
915 	nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
916 	for (i = 0; i < nr_pages; ++i)
917 		dma_debug_create_entries(GFP_KERNEL);
918 	if (num_free_entries >= nr_prealloc_entries) {
919 		pr_info("preallocated %d debug entries\n", nr_total_entries);
920 	} else if (num_free_entries > 0) {
921 		pr_warn("%d debug entries requested but only %d allocated\n",
922 			nr_prealloc_entries, nr_total_entries);
923 	} else {
924 		pr_err("debugging out of memory error - disabled\n");
925 		global_disable = true;
926 
927 		return 0;
928 	}
929 	min_free_entries = num_free_entries;
930 
931 	dma_debug_initialized = true;
932 
933 	pr_info("debugging enabled by kernel config\n");
934 	return 0;
935 }
936 core_initcall(dma_debug_init);
937 
dma_debug_cmdline(char * str)938 static __init int dma_debug_cmdline(char *str)
939 {
940 	if (!str)
941 		return -EINVAL;
942 
943 	if (strncmp(str, "off", 3) == 0) {
944 		pr_info("debugging disabled on kernel command line\n");
945 		global_disable = true;
946 	}
947 
948 	return 1;
949 }
950 
dma_debug_entries_cmdline(char * str)951 static __init int dma_debug_entries_cmdline(char *str)
952 {
953 	if (!str)
954 		return -EINVAL;
955 	if (!get_option(&str, &nr_prealloc_entries))
956 		nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
957 	return 1;
958 }
959 
960 __setup("dma_debug=", dma_debug_cmdline);
961 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
962 
check_unmap(struct dma_debug_entry * ref)963 static void check_unmap(struct dma_debug_entry *ref)
964 {
965 	struct dma_debug_entry *entry;
966 	struct hash_bucket *bucket;
967 	unsigned long flags;
968 
969 	bucket = get_hash_bucket(ref, &flags);
970 	entry = bucket_find_exact(bucket, ref);
971 
972 	if (!entry) {
973 		/* must drop lock before calling dma_mapping_error */
974 		put_hash_bucket(bucket, flags);
975 
976 		if (dma_mapping_error(ref->dev, ref->dev_addr)) {
977 			err_printk(ref->dev, NULL,
978 				   "device driver tries to free an "
979 				   "invalid DMA memory address\n");
980 		} else {
981 			err_printk(ref->dev, NULL,
982 				   "device driver tries to free DMA "
983 				   "memory it has not allocated [device "
984 				   "address=0x%016llx] [size=%llu bytes]\n",
985 				   ref->dev_addr, ref->size);
986 		}
987 		return;
988 	}
989 
990 	if (ref->size != entry->size) {
991 		err_printk(ref->dev, entry, "device driver frees "
992 			   "DMA memory with different size "
993 			   "[device address=0x%016llx] [map size=%llu bytes] "
994 			   "[unmap size=%llu bytes]\n",
995 			   ref->dev_addr, entry->size, ref->size);
996 	}
997 
998 	if (ref->type != entry->type) {
999 		err_printk(ref->dev, entry, "device driver frees "
1000 			   "DMA memory with wrong function "
1001 			   "[device address=0x%016llx] [size=%llu bytes] "
1002 			   "[mapped as %s] [unmapped as %s]\n",
1003 			   ref->dev_addr, ref->size,
1004 			   type2name[entry->type], type2name[ref->type]);
1005 	} else if ((entry->type == dma_debug_coherent) &&
1006 		   (phys_addr(ref) != phys_addr(entry))) {
1007 		err_printk(ref->dev, entry, "device driver frees "
1008 			   "DMA memory with different CPU address "
1009 			   "[device address=0x%016llx] [size=%llu bytes] "
1010 			   "[cpu alloc address=0x%016llx] "
1011 			   "[cpu free address=0x%016llx]",
1012 			   ref->dev_addr, ref->size,
1013 			   phys_addr(entry),
1014 			   phys_addr(ref));
1015 	}
1016 
1017 	if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1018 	    ref->sg_call_ents != entry->sg_call_ents) {
1019 		err_printk(ref->dev, entry, "device driver frees "
1020 			   "DMA sg list with different entry count "
1021 			   "[map count=%d] [unmap count=%d]\n",
1022 			   entry->sg_call_ents, ref->sg_call_ents);
1023 	}
1024 
1025 	/*
1026 	 * This may be no bug in reality - but most implementations of the
1027 	 * DMA API don't handle this properly, so check for it here
1028 	 */
1029 	if (ref->direction != entry->direction) {
1030 		err_printk(ref->dev, entry, "device driver frees "
1031 			   "DMA memory with different direction "
1032 			   "[device address=0x%016llx] [size=%llu bytes] "
1033 			   "[mapped with %s] [unmapped with %s]\n",
1034 			   ref->dev_addr, ref->size,
1035 			   dir2name[entry->direction],
1036 			   dir2name[ref->direction]);
1037 	}
1038 
1039 	/*
1040 	 * Drivers should use dma_mapping_error() to check the returned
1041 	 * addresses of dma_map_single() and dma_map_page().
1042 	 * If not, print this warning message. See Documentation/core-api/dma-api.rst.
1043 	 */
1044 	if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1045 		err_printk(ref->dev, entry,
1046 			   "device driver failed to check map error"
1047 			   "[device address=0x%016llx] [size=%llu bytes] "
1048 			   "[mapped as %s]",
1049 			   ref->dev_addr, ref->size,
1050 			   type2name[entry->type]);
1051 	}
1052 
1053 	hash_bucket_del(entry);
1054 	put_hash_bucket(bucket, flags);
1055 
1056 	/*
1057 	 * Free the entry outside of bucket_lock to avoid ABBA deadlocks
1058 	 * between that and radix_lock.
1059 	 */
1060 	dma_entry_free(entry);
1061 }
1062 
check_for_stack(struct device * dev,struct page * page,size_t offset)1063 static void check_for_stack(struct device *dev,
1064 			    struct page *page, size_t offset)
1065 {
1066 	void *addr;
1067 	struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1068 
1069 	if (!stack_vm_area) {
1070 		/* Stack is direct-mapped. */
1071 		if (PageHighMem(page))
1072 			return;
1073 		addr = page_address(page) + offset;
1074 		if (object_is_on_stack(addr))
1075 			err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
1076 	} else {
1077 		/* Stack is vmalloced. */
1078 		int i;
1079 
1080 		for (i = 0; i < stack_vm_area->nr_pages; i++) {
1081 			if (page != stack_vm_area->pages[i])
1082 				continue;
1083 
1084 			addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1085 			err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
1086 			break;
1087 		}
1088 	}
1089 }
1090 
check_for_illegal_area(struct device * dev,void * addr,unsigned long len)1091 static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1092 {
1093 	if (memory_intersects(_stext, _etext, addr, len) ||
1094 	    memory_intersects(__start_rodata, __end_rodata, addr, len))
1095 		err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1096 }
1097 
check_sync(struct device * dev,struct dma_debug_entry * ref,bool to_cpu)1098 static void check_sync(struct device *dev,
1099 		       struct dma_debug_entry *ref,
1100 		       bool to_cpu)
1101 {
1102 	struct dma_debug_entry *entry;
1103 	struct hash_bucket *bucket;
1104 	unsigned long flags;
1105 
1106 	bucket = get_hash_bucket(ref, &flags);
1107 
1108 	entry = bucket_find_contain(&bucket, ref, &flags);
1109 
1110 	if (!entry) {
1111 		err_printk(dev, NULL, "device driver tries "
1112 				"to sync DMA memory it has not allocated "
1113 				"[device address=0x%016llx] [size=%llu bytes]\n",
1114 				(unsigned long long)ref->dev_addr, ref->size);
1115 		goto out;
1116 	}
1117 
1118 	if (ref->size > entry->size) {
1119 		err_printk(dev, entry, "device driver syncs"
1120 				" DMA memory outside allocated range "
1121 				"[device address=0x%016llx] "
1122 				"[allocation size=%llu bytes] "
1123 				"[sync offset+size=%llu]\n",
1124 				entry->dev_addr, entry->size,
1125 				ref->size);
1126 	}
1127 
1128 	if (entry->direction == DMA_BIDIRECTIONAL)
1129 		goto out;
1130 
1131 	if (ref->direction != entry->direction) {
1132 		err_printk(dev, entry, "device driver syncs "
1133 				"DMA memory with different direction "
1134 				"[device address=0x%016llx] [size=%llu bytes] "
1135 				"[mapped with %s] [synced with %s]\n",
1136 				(unsigned long long)ref->dev_addr, entry->size,
1137 				dir2name[entry->direction],
1138 				dir2name[ref->direction]);
1139 	}
1140 
1141 	if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1142 		      !(ref->direction == DMA_TO_DEVICE))
1143 		err_printk(dev, entry, "device driver syncs "
1144 				"device read-only DMA memory for cpu "
1145 				"[device address=0x%016llx] [size=%llu bytes] "
1146 				"[mapped with %s] [synced with %s]\n",
1147 				(unsigned long long)ref->dev_addr, entry->size,
1148 				dir2name[entry->direction],
1149 				dir2name[ref->direction]);
1150 
1151 	if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1152 		       !(ref->direction == DMA_FROM_DEVICE))
1153 		err_printk(dev, entry, "device driver syncs "
1154 				"device write-only DMA memory to device "
1155 				"[device address=0x%016llx] [size=%llu bytes] "
1156 				"[mapped with %s] [synced with %s]\n",
1157 				(unsigned long long)ref->dev_addr, entry->size,
1158 				dir2name[entry->direction],
1159 				dir2name[ref->direction]);
1160 
1161 	if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1162 	    ref->sg_call_ents != entry->sg_call_ents) {
1163 		err_printk(ref->dev, entry, "device driver syncs "
1164 			   "DMA sg list with different entry count "
1165 			   "[map count=%d] [sync count=%d]\n",
1166 			   entry->sg_call_ents, ref->sg_call_ents);
1167 	}
1168 
1169 out:
1170 	put_hash_bucket(bucket, flags);
1171 }
1172 
check_sg_segment(struct device * dev,struct scatterlist * sg)1173 static void check_sg_segment(struct device *dev, struct scatterlist *sg)
1174 {
1175 #ifdef CONFIG_DMA_API_DEBUG_SG
1176 	unsigned int max_seg = dma_get_max_seg_size(dev);
1177 	u64 start, end, boundary = dma_get_seg_boundary(dev);
1178 
1179 	/*
1180 	 * Either the driver forgot to set dma_parms appropriately, or
1181 	 * whoever generated the list forgot to check them.
1182 	 */
1183 	if (sg->length > max_seg)
1184 		err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1185 			   sg->length, max_seg);
1186 	/*
1187 	 * In some cases this could potentially be the DMA API
1188 	 * implementation's fault, but it would usually imply that
1189 	 * the scatterlist was built inappropriately to begin with.
1190 	 */
1191 	start = sg_dma_address(sg);
1192 	end = start + sg_dma_len(sg) - 1;
1193 	if ((start ^ end) & ~boundary)
1194 		err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1195 			   start, end, boundary);
1196 #endif
1197 }
1198 
debug_dma_map_single(struct device * dev,const void * addr,unsigned long len)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 
debug_dma_map_page(struct device * dev,struct page * page,size_t offset,size_t size,int direction,dma_addr_t dma_addr,unsigned long attrs)1215 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1216 			size_t size, int direction, dma_addr_t dma_addr,
1217 			unsigned long attrs)
1218 {
1219 	struct dma_debug_entry *entry;
1220 
1221 	if (unlikely(dma_debug_disabled()))
1222 		return;
1223 
1224 	if (dma_mapping_error(dev, dma_addr))
1225 		return;
1226 
1227 	entry = dma_entry_alloc();
1228 	if (!entry)
1229 		return;
1230 
1231 	entry->dev       = dev;
1232 	entry->type      = dma_debug_single;
1233 	entry->pfn	 = page_to_pfn(page);
1234 	entry->offset	 = offset;
1235 	entry->dev_addr  = dma_addr;
1236 	entry->size      = size;
1237 	entry->direction = direction;
1238 	entry->map_err_type = MAP_ERR_NOT_CHECKED;
1239 
1240 	check_for_stack(dev, page, offset);
1241 
1242 	if (!PageHighMem(page)) {
1243 		void *addr = page_address(page) + offset;
1244 
1245 		check_for_illegal_area(dev, addr, size);
1246 	}
1247 
1248 	add_dma_entry(entry, attrs);
1249 }
1250 
debug_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)1251 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1252 {
1253 	struct dma_debug_entry ref;
1254 	struct dma_debug_entry *entry;
1255 	struct hash_bucket *bucket;
1256 	unsigned long flags;
1257 
1258 	if (unlikely(dma_debug_disabled()))
1259 		return;
1260 
1261 	ref.dev = dev;
1262 	ref.dev_addr = dma_addr;
1263 	bucket = get_hash_bucket(&ref, &flags);
1264 
1265 	list_for_each_entry(entry, &bucket->list, list) {
1266 		if (!exact_match(&ref, entry))
1267 			continue;
1268 
1269 		/*
1270 		 * The same physical address can be mapped multiple
1271 		 * times. Without a hardware IOMMU this results in the
1272 		 * same device addresses being put into the dma-debug
1273 		 * hash multiple times too. This can result in false
1274 		 * positives being reported. Therefore we implement a
1275 		 * best-fit algorithm here which updates the first entry
1276 		 * from the hash which fits the reference value and is
1277 		 * not currently listed as being checked.
1278 		 */
1279 		if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1280 			entry->map_err_type = MAP_ERR_CHECKED;
1281 			break;
1282 		}
1283 	}
1284 
1285 	put_hash_bucket(bucket, flags);
1286 }
1287 EXPORT_SYMBOL(debug_dma_mapping_error);
1288 
debug_dma_unmap_page(struct device * dev,dma_addr_t dma_addr,size_t size,int direction)1289 void debug_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
1290 			  size_t size, int direction)
1291 {
1292 	struct dma_debug_entry ref = {
1293 		.type           = dma_debug_single,
1294 		.dev            = dev,
1295 		.dev_addr       = dma_addr,
1296 		.size           = size,
1297 		.direction      = direction,
1298 	};
1299 
1300 	if (unlikely(dma_debug_disabled()))
1301 		return;
1302 	check_unmap(&ref);
1303 }
1304 
debug_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,int mapped_ents,int direction,unsigned long attrs)1305 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1306 		      int nents, int mapped_ents, int direction,
1307 		      unsigned long attrs)
1308 {
1309 	struct dma_debug_entry *entry;
1310 	struct scatterlist *s;
1311 	int i;
1312 
1313 	if (unlikely(dma_debug_disabled()))
1314 		return;
1315 
1316 	for_each_sg(sg, s, nents, i) {
1317 		check_for_stack(dev, sg_page(s), s->offset);
1318 		if (!PageHighMem(sg_page(s)))
1319 			check_for_illegal_area(dev, sg_virt(s), s->length);
1320 	}
1321 
1322 	for_each_sg(sg, s, mapped_ents, i) {
1323 		entry = dma_entry_alloc();
1324 		if (!entry)
1325 			return;
1326 
1327 		entry->type           = dma_debug_sg;
1328 		entry->dev            = dev;
1329 		entry->pfn	      = page_to_pfn(sg_page(s));
1330 		entry->offset	      = s->offset;
1331 		entry->size           = sg_dma_len(s);
1332 		entry->dev_addr       = sg_dma_address(s);
1333 		entry->direction      = direction;
1334 		entry->sg_call_ents   = nents;
1335 		entry->sg_mapped_ents = mapped_ents;
1336 
1337 		check_sg_segment(dev, s);
1338 
1339 		add_dma_entry(entry, attrs);
1340 	}
1341 }
1342 
get_nr_mapped_entries(struct device * dev,struct dma_debug_entry * ref)1343 static int get_nr_mapped_entries(struct device *dev,
1344 				 struct dma_debug_entry *ref)
1345 {
1346 	struct dma_debug_entry *entry;
1347 	struct hash_bucket *bucket;
1348 	unsigned long flags;
1349 	int mapped_ents;
1350 
1351 	bucket       = get_hash_bucket(ref, &flags);
1352 	entry        = bucket_find_exact(bucket, ref);
1353 	mapped_ents  = 0;
1354 
1355 	if (entry)
1356 		mapped_ents = entry->sg_mapped_ents;
1357 	put_hash_bucket(bucket, flags);
1358 
1359 	return mapped_ents;
1360 }
1361 
debug_dma_unmap_sg(struct device * dev,struct scatterlist * sglist,int nelems,int dir)1362 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1363 			int nelems, int dir)
1364 {
1365 	struct scatterlist *s;
1366 	int mapped_ents = 0, i;
1367 
1368 	if (unlikely(dma_debug_disabled()))
1369 		return;
1370 
1371 	for_each_sg(sglist, s, nelems, i) {
1372 
1373 		struct dma_debug_entry ref = {
1374 			.type           = dma_debug_sg,
1375 			.dev            = dev,
1376 			.pfn		= page_to_pfn(sg_page(s)),
1377 			.offset		= s->offset,
1378 			.dev_addr       = sg_dma_address(s),
1379 			.size           = sg_dma_len(s),
1380 			.direction      = dir,
1381 			.sg_call_ents   = nelems,
1382 		};
1383 
1384 		if (mapped_ents && i >= mapped_ents)
1385 			break;
1386 
1387 		if (!i)
1388 			mapped_ents = get_nr_mapped_entries(dev, &ref);
1389 
1390 		check_unmap(&ref);
1391 	}
1392 }
1393 
debug_dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t dma_addr,void * virt,unsigned long attrs)1394 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1395 			      dma_addr_t dma_addr, void *virt,
1396 			      unsigned long attrs)
1397 {
1398 	struct dma_debug_entry *entry;
1399 
1400 	if (unlikely(dma_debug_disabled()))
1401 		return;
1402 
1403 	if (unlikely(virt == NULL))
1404 		return;
1405 
1406 	/* handle vmalloc and linear addresses */
1407 	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1408 		return;
1409 
1410 	entry = dma_entry_alloc();
1411 	if (!entry)
1412 		return;
1413 
1414 	entry->type      = dma_debug_coherent;
1415 	entry->dev       = dev;
1416 	entry->offset	 = offset_in_page(virt);
1417 	entry->size      = size;
1418 	entry->dev_addr  = dma_addr;
1419 	entry->direction = DMA_BIDIRECTIONAL;
1420 
1421 	if (is_vmalloc_addr(virt))
1422 		entry->pfn = vmalloc_to_pfn(virt);
1423 	else
1424 		entry->pfn = page_to_pfn(virt_to_page(virt));
1425 
1426 	add_dma_entry(entry, attrs);
1427 }
1428 
debug_dma_free_coherent(struct device * dev,size_t size,void * virt,dma_addr_t dma_addr)1429 void debug_dma_free_coherent(struct device *dev, size_t size,
1430 			 void *virt, dma_addr_t dma_addr)
1431 {
1432 	struct dma_debug_entry ref = {
1433 		.type           = dma_debug_coherent,
1434 		.dev            = dev,
1435 		.offset		= offset_in_page(virt),
1436 		.dev_addr       = dma_addr,
1437 		.size           = size,
1438 		.direction      = DMA_BIDIRECTIONAL,
1439 	};
1440 
1441 	/* handle vmalloc and linear addresses */
1442 	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1443 		return;
1444 
1445 	if (is_vmalloc_addr(virt))
1446 		ref.pfn = vmalloc_to_pfn(virt);
1447 	else
1448 		ref.pfn = page_to_pfn(virt_to_page(virt));
1449 
1450 	if (unlikely(dma_debug_disabled()))
1451 		return;
1452 
1453 	check_unmap(&ref);
1454 }
1455 
debug_dma_map_resource(struct device * dev,phys_addr_t addr,size_t size,int direction,dma_addr_t dma_addr,unsigned long attrs)1456 void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1457 			    int direction, dma_addr_t dma_addr,
1458 			    unsigned long attrs)
1459 {
1460 	struct dma_debug_entry *entry;
1461 
1462 	if (unlikely(dma_debug_disabled()))
1463 		return;
1464 
1465 	entry = dma_entry_alloc();
1466 	if (!entry)
1467 		return;
1468 
1469 	entry->type		= dma_debug_resource;
1470 	entry->dev		= dev;
1471 	entry->pfn		= PHYS_PFN(addr);
1472 	entry->offset		= offset_in_page(addr);
1473 	entry->size		= size;
1474 	entry->dev_addr		= dma_addr;
1475 	entry->direction	= direction;
1476 	entry->map_err_type	= MAP_ERR_NOT_CHECKED;
1477 
1478 	add_dma_entry(entry, attrs);
1479 }
1480 
debug_dma_unmap_resource(struct device * dev,dma_addr_t dma_addr,size_t size,int direction)1481 void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1482 			      size_t size, int direction)
1483 {
1484 	struct dma_debug_entry ref = {
1485 		.type           = dma_debug_resource,
1486 		.dev            = dev,
1487 		.dev_addr       = dma_addr,
1488 		.size           = size,
1489 		.direction      = direction,
1490 	};
1491 
1492 	if (unlikely(dma_debug_disabled()))
1493 		return;
1494 
1495 	check_unmap(&ref);
1496 }
1497 
debug_dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma_handle,size_t size,int direction)1498 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1499 				   size_t size, int direction)
1500 {
1501 	struct dma_debug_entry ref;
1502 
1503 	if (unlikely(dma_debug_disabled()))
1504 		return;
1505 
1506 	ref.type         = dma_debug_single;
1507 	ref.dev          = dev;
1508 	ref.dev_addr     = dma_handle;
1509 	ref.size         = size;
1510 	ref.direction    = direction;
1511 	ref.sg_call_ents = 0;
1512 
1513 	check_sync(dev, &ref, true);
1514 }
1515 
debug_dma_sync_single_for_device(struct device * dev,dma_addr_t dma_handle,size_t size,int direction)1516 void debug_dma_sync_single_for_device(struct device *dev,
1517 				      dma_addr_t dma_handle, size_t size,
1518 				      int direction)
1519 {
1520 	struct dma_debug_entry ref;
1521 
1522 	if (unlikely(dma_debug_disabled()))
1523 		return;
1524 
1525 	ref.type         = dma_debug_single;
1526 	ref.dev          = dev;
1527 	ref.dev_addr     = dma_handle;
1528 	ref.size         = size;
1529 	ref.direction    = direction;
1530 	ref.sg_call_ents = 0;
1531 
1532 	check_sync(dev, &ref, false);
1533 }
1534 
debug_dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,int direction)1535 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1536 			       int nelems, int direction)
1537 {
1538 	struct scatterlist *s;
1539 	int mapped_ents = 0, i;
1540 
1541 	if (unlikely(dma_debug_disabled()))
1542 		return;
1543 
1544 	for_each_sg(sg, s, nelems, i) {
1545 
1546 		struct dma_debug_entry ref = {
1547 			.type           = dma_debug_sg,
1548 			.dev            = dev,
1549 			.pfn		= page_to_pfn(sg_page(s)),
1550 			.offset		= s->offset,
1551 			.dev_addr       = sg_dma_address(s),
1552 			.size           = sg_dma_len(s),
1553 			.direction      = direction,
1554 			.sg_call_ents   = nelems,
1555 		};
1556 
1557 		if (!i)
1558 			mapped_ents = get_nr_mapped_entries(dev, &ref);
1559 
1560 		if (i >= mapped_ents)
1561 			break;
1562 
1563 		check_sync(dev, &ref, true);
1564 	}
1565 }
1566 
debug_dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,int direction)1567 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1568 				  int nelems, int direction)
1569 {
1570 	struct scatterlist *s;
1571 	int mapped_ents = 0, i;
1572 
1573 	if (unlikely(dma_debug_disabled()))
1574 		return;
1575 
1576 	for_each_sg(sg, s, nelems, i) {
1577 
1578 		struct dma_debug_entry ref = {
1579 			.type           = dma_debug_sg,
1580 			.dev            = dev,
1581 			.pfn		= page_to_pfn(sg_page(s)),
1582 			.offset		= s->offset,
1583 			.dev_addr       = sg_dma_address(s),
1584 			.size           = sg_dma_len(s),
1585 			.direction      = direction,
1586 			.sg_call_ents   = nelems,
1587 		};
1588 		if (!i)
1589 			mapped_ents = get_nr_mapped_entries(dev, &ref);
1590 
1591 		if (i >= mapped_ents)
1592 			break;
1593 
1594 		check_sync(dev, &ref, false);
1595 	}
1596 }
1597 
dma_debug_driver_setup(char * str)1598 static int __init dma_debug_driver_setup(char *str)
1599 {
1600 	int i;
1601 
1602 	for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1603 		current_driver_name[i] = *str;
1604 		if (*str == 0)
1605 			break;
1606 	}
1607 
1608 	if (current_driver_name[0])
1609 		pr_info("enable driver filter for driver [%s]\n",
1610 			current_driver_name);
1611 
1612 
1613 	return 1;
1614 }
1615 __setup("dma_debug_driver=", dma_debug_driver_setup);
1616