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