2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <joerg.roedel@amd.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/sched/task_stack.h>
21 #include <linux/scatterlist.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/sched/task.h>
24 #include <linux/stacktrace.h>
25 #include <linux/dma-debug.h>
26 #include <linux/spinlock.h>
27 #include <linux/vmalloc.h>
28 #include <linux/debugfs.h>
29 #include <linux/uaccess.h>
30 #include <linux/export.h>
31 #include <linux/device.h>
32 #include <linux/types.h>
33 #include <linux/sched.h>
34 #include <linux/ctype.h>
35 #include <linux/list.h>
36 #include <linux/slab.h>
38 #include <asm/sections.h>
40 #define HASH_SIZE 1024ULL
41 #define HASH_FN_SHIFT 13
42 #define HASH_FN_MASK (HASH_SIZE - 1)
44 /* allow architectures to override this if absolutely required */
45 #ifndef PREALLOC_DMA_DEBUG_ENTRIES
46 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
58 MAP_ERR_CHECK_NOT_APPLICABLE,
63 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
66 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
67 * @list: node on pre-allocated free_entries list
68 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
69 * @type: single, page, sg, coherent
70 * @pfn: page frame of the start address
71 * @offset: offset of mapping relative to pfn
72 * @size: length of the mapping
73 * @direction: enum dma_data_direction
74 * @sg_call_ents: 'nents' from dma_map_sg
75 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
76 * @map_err_type: track whether dma_mapping_error() was checked
77 * @stacktrace: support backtraces when a violation is detected
79 struct dma_debug_entry {
80 struct list_head list;
90 enum map_err_types map_err_type;
91 #ifdef CONFIG_STACKTRACE
92 struct stack_trace stacktrace;
93 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
97 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
100 struct list_head list;
102 } ____cacheline_aligned_in_smp;
104 /* Hash list to save the allocated dma addresses */
105 static struct hash_bucket dma_entry_hash[HASH_SIZE];
106 /* List of pre-allocated dma_debug_entry's */
107 static LIST_HEAD(free_entries);
108 /* Lock for the list above */
109 static DEFINE_SPINLOCK(free_entries_lock);
111 /* Global disable flag - will be set in case of an error */
112 static bool global_disable __read_mostly;
114 /* Early initialization disable flag, set at the end of dma_debug_init */
115 static bool dma_debug_initialized __read_mostly;
117 static inline bool dma_debug_disabled(void)
119 return global_disable || !dma_debug_initialized;
122 /* Global error count */
123 static u32 error_count;
125 /* Global error show enable*/
126 static u32 show_all_errors __read_mostly;
127 /* Number of errors to show */
128 static u32 show_num_errors = 1;
130 static u32 num_free_entries;
131 static u32 min_free_entries;
132 static u32 nr_total_entries;
134 /* number of preallocated entries requested by kernel cmdline */
135 static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
137 /* debugfs dentry's for the stuff above */
138 static struct dentry *dma_debug_dent __read_mostly;
139 static struct dentry *global_disable_dent __read_mostly;
140 static struct dentry *error_count_dent __read_mostly;
141 static struct dentry *show_all_errors_dent __read_mostly;
142 static struct dentry *show_num_errors_dent __read_mostly;
143 static struct dentry *num_free_entries_dent __read_mostly;
144 static struct dentry *min_free_entries_dent __read_mostly;
145 static struct dentry *filter_dent __read_mostly;
147 /* per-driver filter related state */
149 #define NAME_MAX_LEN 64
151 static char current_driver_name[NAME_MAX_LEN] __read_mostly;
152 static struct device_driver *current_driver __read_mostly;
154 static DEFINE_RWLOCK(driver_name_lock);
156 static const char *const maperr2str[] = {
157 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
158 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
159 [MAP_ERR_CHECKED] = "dma map error checked",
162 static const char *type2name[5] = { "single", "page",
163 "scather-gather", "coherent",
166 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
167 "DMA_FROM_DEVICE", "DMA_NONE" };
170 * The access to some variables in this macro is racy. We can't use atomic_t
171 * here because all these variables are exported to debugfs. Some of them even
172 * writeable. This is also the reason why a lock won't help much. But anyway,
173 * the races are no big deal. Here is why:
175 * error_count: the addition is racy, but the worst thing that can happen is
176 * that we don't count some errors
177 * show_num_errors: the subtraction is racy. Also no big deal because in
178 * worst case this will result in one warning more in the
179 * system log than the user configured. This variable is
180 * writeable via debugfs.
182 static inline void dump_entry_trace(struct dma_debug_entry *entry)
184 #ifdef CONFIG_STACKTRACE
186 pr_warning("Mapped at:\n");
187 print_stack_trace(&entry->stacktrace, 0);
192 static bool driver_filter(struct device *dev)
194 struct device_driver *drv;
198 /* driver filter off */
199 if (likely(!current_driver_name[0]))
202 /* driver filter on and initialized */
203 if (current_driver && dev && dev->driver == current_driver)
206 /* driver filter on, but we can't filter on a NULL device... */
210 if (current_driver || !current_driver_name[0])
213 /* driver filter on but not yet initialized */
218 /* lock to protect against change of current_driver_name */
219 read_lock_irqsave(&driver_name_lock, flags);
223 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
224 current_driver = drv;
228 read_unlock_irqrestore(&driver_name_lock, flags);
233 #define err_printk(dev, entry, format, arg...) do { \
235 if (driver_filter(dev) && \
236 (show_all_errors || show_num_errors > 0)) { \
237 WARN(1, "%s %s: " format, \
238 dev ? dev_driver_string(dev) : "NULL", \
239 dev ? dev_name(dev) : "NULL", ## arg); \
240 dump_entry_trace(entry); \
242 if (!show_all_errors && show_num_errors > 0) \
243 show_num_errors -= 1; \
247 * Hash related functions
249 * Every DMA-API request is saved into a struct dma_debug_entry. To
250 * have quick access to these structs they are stored into a hash.
252 static int hash_fn(struct dma_debug_entry *entry)
255 * Hash function is based on the dma address.
256 * We use bits 20-27 here as the index into the hash
258 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
262 * Request exclusive access to a hash bucket for a given dma_debug_entry.
264 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
265 unsigned long *flags)
266 __acquires(&dma_entry_hash[idx].lock)
268 int idx = hash_fn(entry);
269 unsigned long __flags;
271 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
273 return &dma_entry_hash[idx];
277 * Give up exclusive access to the hash bucket
279 static void put_hash_bucket(struct hash_bucket *bucket,
280 unsigned long *flags)
281 __releases(&bucket->lock)
283 unsigned long __flags = *flags;
285 spin_unlock_irqrestore(&bucket->lock, __flags);
288 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
290 return ((a->dev_addr == b->dev_addr) &&
291 (a->dev == b->dev)) ? true : false;
294 static bool containing_match(struct dma_debug_entry *a,
295 struct dma_debug_entry *b)
297 if (a->dev != b->dev)
300 if ((b->dev_addr <= a->dev_addr) &&
301 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
308 * Search a given entry in the hash bucket list
310 static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
311 struct dma_debug_entry *ref,
314 struct dma_debug_entry *entry, *ret = NULL;
315 int matches = 0, match_lvl, last_lvl = -1;
317 list_for_each_entry(entry, &bucket->list, list) {
318 if (!match(ref, entry))
322 * Some drivers map the same physical address multiple
323 * times. Without a hardware IOMMU this results in the
324 * same device addresses being put into the dma-debug
325 * hash multiple times too. This can result in false
326 * positives being reported. Therefore we implement a
327 * best-fit algorithm here which returns the entry from
328 * the hash which fits best to the reference value
329 * instead of the first-fit.
333 entry->size == ref->size ? ++match_lvl : 0;
334 entry->type == ref->type ? ++match_lvl : 0;
335 entry->direction == ref->direction ? ++match_lvl : 0;
336 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
338 if (match_lvl == 4) {
339 /* perfect-fit - return the result */
341 } else if (match_lvl > last_lvl) {
343 * We found an entry that fits better then the
344 * previous one or it is the 1st match.
346 last_lvl = match_lvl;
352 * If we have multiple matches but no perfect-fit, just return
355 ret = (matches == 1) ? ret : NULL;
360 static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
361 struct dma_debug_entry *ref)
363 return __hash_bucket_find(bucket, ref, exact_match);
366 static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
367 struct dma_debug_entry *ref,
368 unsigned long *flags)
371 unsigned int max_range = dma_get_max_seg_size(ref->dev);
372 struct dma_debug_entry *entry, index = *ref;
373 unsigned int range = 0;
375 while (range <= max_range) {
376 entry = __hash_bucket_find(*bucket, ref, containing_match);
382 * Nothing found, go back a hash bucket
384 put_hash_bucket(*bucket, flags);
385 range += (1 << HASH_FN_SHIFT);
386 index.dev_addr -= (1 << HASH_FN_SHIFT);
387 *bucket = get_hash_bucket(&index, flags);
394 * Add an entry to a hash bucket
396 static void hash_bucket_add(struct hash_bucket *bucket,
397 struct dma_debug_entry *entry)
399 list_add_tail(&entry->list, &bucket->list);
403 * Remove entry from a hash bucket list
405 static void hash_bucket_del(struct dma_debug_entry *entry)
407 list_del(&entry->list);
410 static unsigned long long phys_addr(struct dma_debug_entry *entry)
412 if (entry->type == dma_debug_resource)
413 return __pfn_to_phys(entry->pfn) + entry->offset;
415 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
419 * Dump mapping entries for debugging purposes
421 void debug_dma_dump_mappings(struct device *dev)
425 for (idx = 0; idx < HASH_SIZE; idx++) {
426 struct hash_bucket *bucket = &dma_entry_hash[idx];
427 struct dma_debug_entry *entry;
430 spin_lock_irqsave(&bucket->lock, flags);
432 list_for_each_entry(entry, &bucket->list, list) {
433 if (!dev || dev == entry->dev) {
435 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
436 type2name[entry->type], idx,
437 phys_addr(entry), entry->pfn,
438 entry->dev_addr, entry->size,
439 dir2name[entry->direction],
440 maperr2str[entry->map_err_type]);
444 spin_unlock_irqrestore(&bucket->lock, flags);
449 * For each mapping (initial cacheline in the case of
450 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
451 * scatterlist, or the cacheline specified in dma_map_single) insert
452 * into this tree using the cacheline as the key. At
453 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
454 * the entry already exists at insertion time add a tag as a reference
455 * count for the overlapping mappings. For now, the overlap tracking
456 * just ensures that 'unmaps' balance 'maps' before marking the
457 * cacheline idle, but we should also be flagging overlaps as an API
460 * Memory usage is mostly constrained by the maximum number of available
461 * dma-debug entries in that we need a free dma_debug_entry before
462 * inserting into the tree. In the case of dma_map_page and
463 * dma_alloc_coherent there is only one dma_debug_entry and one
464 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
465 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
466 * entries into the tree.
468 * At any time debug_dma_assert_idle() can be called to trigger a
469 * warning if any cachelines in the given page are in the active set.
471 static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
472 static DEFINE_SPINLOCK(radix_lock);
473 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
474 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
475 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
477 static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
479 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
480 (entry->offset >> L1_CACHE_SHIFT);
483 static int active_cacheline_read_overlap(phys_addr_t cln)
487 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
488 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
493 static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
497 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
500 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
501 if (overlap & 1 << i)
502 radix_tree_tag_set(&dma_active_cacheline, cln, i);
504 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
509 static void active_cacheline_inc_overlap(phys_addr_t cln)
511 int overlap = active_cacheline_read_overlap(cln);
513 overlap = active_cacheline_set_overlap(cln, ++overlap);
515 /* If we overflowed the overlap counter then we're potentially
516 * leaking dma-mappings. Otherwise, if maps and unmaps are
517 * balanced then this overflow may cause false negatives in
518 * debug_dma_assert_idle() as the cacheline may be marked idle
521 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
522 "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
523 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
526 static int active_cacheline_dec_overlap(phys_addr_t cln)
528 int overlap = active_cacheline_read_overlap(cln);
530 return active_cacheline_set_overlap(cln, --overlap);
533 static int active_cacheline_insert(struct dma_debug_entry *entry)
535 phys_addr_t cln = to_cacheline_number(entry);
539 /* If the device is not writing memory then we don't have any
540 * concerns about the cpu consuming stale data. This mitigates
541 * legitimate usages of overlapping mappings.
543 if (entry->direction == DMA_TO_DEVICE)
546 spin_lock_irqsave(&radix_lock, flags);
547 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
549 active_cacheline_inc_overlap(cln);
550 spin_unlock_irqrestore(&radix_lock, flags);
555 static void active_cacheline_remove(struct dma_debug_entry *entry)
557 phys_addr_t cln = to_cacheline_number(entry);
560 /* ...mirror the insert case */
561 if (entry->direction == DMA_TO_DEVICE)
564 spin_lock_irqsave(&radix_lock, flags);
565 /* since we are counting overlaps the final put of the
566 * cacheline will occur when the overlap count is 0.
567 * active_cacheline_dec_overlap() returns -1 in that case
569 if (active_cacheline_dec_overlap(cln) < 0)
570 radix_tree_delete(&dma_active_cacheline, cln);
571 spin_unlock_irqrestore(&radix_lock, flags);
575 * debug_dma_assert_idle() - assert that a page is not undergoing dma
576 * @page: page to lookup in the dma_active_cacheline tree
578 * Place a call to this routine in cases where the cpu touching the page
579 * before the dma completes (page is dma_unmapped) will lead to data
582 void debug_dma_assert_idle(struct page *page)
584 static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
585 struct dma_debug_entry *entry = NULL;
586 void **results = (void **) &ents;
587 unsigned int nents, i;
591 if (dma_debug_disabled())
597 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
598 spin_lock_irqsave(&radix_lock, flags);
599 nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
600 CACHELINES_PER_PAGE);
601 for (i = 0; i < nents; i++) {
602 phys_addr_t ent_cln = to_cacheline_number(ents[i]);
604 if (ent_cln == cln) {
607 } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
610 spin_unlock_irqrestore(&radix_lock, flags);
615 cln = to_cacheline_number(entry);
616 err_printk(entry->dev, entry,
617 "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
622 * Wrapper function for adding an entry to the hash.
623 * This function takes care of locking itself.
625 static void add_dma_entry(struct dma_debug_entry *entry)
627 struct hash_bucket *bucket;
631 bucket = get_hash_bucket(entry, &flags);
632 hash_bucket_add(bucket, entry);
633 put_hash_bucket(bucket, &flags);
635 rc = active_cacheline_insert(entry);
637 pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
638 global_disable = true;
641 /* TODO: report -EEXIST errors here as overlapping mappings are
642 * not supported by the DMA API
646 static struct dma_debug_entry *__dma_entry_alloc(void)
648 struct dma_debug_entry *entry;
650 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
651 list_del(&entry->list);
652 memset(entry, 0, sizeof(*entry));
654 num_free_entries -= 1;
655 if (num_free_entries < min_free_entries)
656 min_free_entries = num_free_entries;
661 /* struct dma_entry allocator
663 * The next two functions implement the allocator for
664 * struct dma_debug_entries.
666 static struct dma_debug_entry *dma_entry_alloc(void)
668 struct dma_debug_entry *entry;
671 spin_lock_irqsave(&free_entries_lock, flags);
673 if (list_empty(&free_entries)) {
674 global_disable = true;
675 spin_unlock_irqrestore(&free_entries_lock, flags);
676 pr_err("DMA-API: debugging out of memory - disabling\n");
680 entry = __dma_entry_alloc();
682 spin_unlock_irqrestore(&free_entries_lock, flags);
684 #ifdef CONFIG_STACKTRACE
685 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
686 entry->stacktrace.entries = entry->st_entries;
687 entry->stacktrace.skip = 2;
688 save_stack_trace(&entry->stacktrace);
694 static void dma_entry_free(struct dma_debug_entry *entry)
698 active_cacheline_remove(entry);
701 * add to beginning of the list - this way the entries are
702 * more likely cache hot when they are reallocated.
704 spin_lock_irqsave(&free_entries_lock, flags);
705 list_add(&entry->list, &free_entries);
706 num_free_entries += 1;
707 spin_unlock_irqrestore(&free_entries_lock, flags);
710 int dma_debug_resize_entries(u32 num_entries)
712 int i, delta, ret = 0;
714 struct dma_debug_entry *entry;
717 spin_lock_irqsave(&free_entries_lock, flags);
719 if (nr_total_entries < num_entries) {
720 delta = num_entries - nr_total_entries;
722 spin_unlock_irqrestore(&free_entries_lock, flags);
724 for (i = 0; i < delta; i++) {
725 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
729 list_add_tail(&entry->list, &tmp);
732 spin_lock_irqsave(&free_entries_lock, flags);
734 list_splice(&tmp, &free_entries);
735 nr_total_entries += i;
736 num_free_entries += i;
738 delta = nr_total_entries - num_entries;
740 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
741 entry = __dma_entry_alloc();
745 nr_total_entries -= i;
748 if (nr_total_entries != num_entries)
751 spin_unlock_irqrestore(&free_entries_lock, flags);
757 * DMA-API debugging init code
759 * The init code does two things:
760 * 1. Initialize core data structures
761 * 2. Preallocate a given number of dma_debug_entry structs
764 static int prealloc_memory(u32 num_entries)
766 struct dma_debug_entry *entry, *next_entry;
769 for (i = 0; i < num_entries; ++i) {
770 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
774 list_add_tail(&entry->list, &free_entries);
777 num_free_entries = num_entries;
778 min_free_entries = num_entries;
780 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
786 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
787 list_del(&entry->list);
794 static ssize_t filter_read(struct file *file, char __user *user_buf,
795 size_t count, loff_t *ppos)
797 char buf[NAME_MAX_LEN + 1];
801 if (!current_driver_name[0])
805 * We can't copy to userspace directly because current_driver_name can
806 * only be read under the driver_name_lock with irqs disabled. So
807 * create a temporary copy first.
809 read_lock_irqsave(&driver_name_lock, flags);
810 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
811 read_unlock_irqrestore(&driver_name_lock, flags);
813 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
816 static ssize_t filter_write(struct file *file, const char __user *userbuf,
817 size_t count, loff_t *ppos)
819 char buf[NAME_MAX_LEN];
825 * We can't copy from userspace directly. Access to
826 * current_driver_name is protected with a write_lock with irqs
827 * disabled. Since copy_from_user can fault and may sleep we
828 * need to copy to temporary buffer first
830 len = min(count, (size_t)(NAME_MAX_LEN - 1));
831 if (copy_from_user(buf, userbuf, len))
836 write_lock_irqsave(&driver_name_lock, flags);
839 * Now handle the string we got from userspace very carefully.
841 * - only use the first token we got
842 * - token delimiter is everything looking like a space
843 * character (' ', '\n', '\t' ...)
846 if (!isalnum(buf[0])) {
848 * If the first character userspace gave us is not
849 * alphanumerical then assume the filter should be
852 if (current_driver_name[0])
853 pr_info("DMA-API: switching off dma-debug driver filter\n");
854 current_driver_name[0] = 0;
855 current_driver = NULL;
860 * Now parse out the first token and use it as the name for the
861 * driver to filter for.
863 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
864 current_driver_name[i] = buf[i];
865 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
868 current_driver_name[i] = 0;
869 current_driver = NULL;
871 pr_info("DMA-API: enable driver filter for driver [%s]\n",
872 current_driver_name);
875 write_unlock_irqrestore(&driver_name_lock, flags);
880 static const struct file_operations filter_fops = {
882 .write = filter_write,
883 .llseek = default_llseek,
886 static int dma_debug_fs_init(void)
888 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
889 if (!dma_debug_dent) {
890 pr_err("DMA-API: can not create debugfs directory\n");
894 global_disable_dent = debugfs_create_bool("disabled", 0444,
897 if (!global_disable_dent)
900 error_count_dent = debugfs_create_u32("error_count", 0444,
901 dma_debug_dent, &error_count);
902 if (!error_count_dent)
905 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
908 if (!show_all_errors_dent)
911 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
914 if (!show_num_errors_dent)
917 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
920 if (!num_free_entries_dent)
923 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
926 if (!min_free_entries_dent)
929 filter_dent = debugfs_create_file("driver_filter", 0644,
930 dma_debug_dent, NULL, &filter_fops);
937 debugfs_remove_recursive(dma_debug_dent);
942 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
944 struct dma_debug_entry *entry;
948 for (i = 0; i < HASH_SIZE; ++i) {
949 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
950 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
951 if (entry->dev == dev) {
956 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
962 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
964 struct device *dev = data;
965 struct dma_debug_entry *uninitialized_var(entry);
968 if (dma_debug_disabled())
972 case BUS_NOTIFY_UNBOUND_DRIVER:
973 count = device_dma_allocations(dev, &entry);
976 err_printk(dev, entry, "DMA-API: device driver has pending "
977 "DMA allocations while released from device "
979 "One of leaked entries details: "
980 "[device address=0x%016llx] [size=%llu bytes] "
981 "[mapped with %s] [mapped as %s]\n",
982 count, entry->dev_addr, entry->size,
983 dir2name[entry->direction], type2name[entry->type]);
992 void dma_debug_add_bus(struct bus_type *bus)
994 struct notifier_block *nb;
996 if (dma_debug_disabled())
999 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1001 pr_err("dma_debug_add_bus: out of memory\n");
1005 nb->notifier_call = dma_debug_device_change;
1007 bus_register_notifier(bus, nb);
1010 static int dma_debug_init(void)
1014 /* Do not use dma_debug_initialized here, since we really want to be
1015 * called to set dma_debug_initialized
1020 for (i = 0; i < HASH_SIZE; ++i) {
1021 INIT_LIST_HEAD(&dma_entry_hash[i].list);
1022 spin_lock_init(&dma_entry_hash[i].lock);
1025 if (dma_debug_fs_init() != 0) {
1026 pr_err("DMA-API: error creating debugfs entries - disabling\n");
1027 global_disable = true;
1032 if (prealloc_memory(nr_prealloc_entries) != 0) {
1033 pr_err("DMA-API: debugging out of memory error - disabled\n");
1034 global_disable = true;
1039 nr_total_entries = num_free_entries;
1041 dma_debug_initialized = true;
1043 pr_info("DMA-API: debugging enabled by kernel config\n");
1046 core_initcall(dma_debug_init);
1048 static __init int dma_debug_cmdline(char *str)
1053 if (strncmp(str, "off", 3) == 0) {
1054 pr_info("DMA-API: debugging disabled on kernel command line\n");
1055 global_disable = true;
1061 static __init int dma_debug_entries_cmdline(char *str)
1065 if (!get_option(&str, &nr_prealloc_entries))
1066 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
1070 __setup("dma_debug=", dma_debug_cmdline);
1071 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
1073 static void check_unmap(struct dma_debug_entry *ref)
1075 struct dma_debug_entry *entry;
1076 struct hash_bucket *bucket;
1077 unsigned long flags;
1079 bucket = get_hash_bucket(ref, &flags);
1080 entry = bucket_find_exact(bucket, ref);
1083 /* must drop lock before calling dma_mapping_error */
1084 put_hash_bucket(bucket, &flags);
1086 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1087 err_printk(ref->dev, NULL,
1088 "DMA-API: device driver tries to free an "
1089 "invalid DMA memory address\n");
1091 err_printk(ref->dev, NULL,
1092 "DMA-API: device driver tries to free DMA "
1093 "memory it has not allocated [device "
1094 "address=0x%016llx] [size=%llu bytes]\n",
1095 ref->dev_addr, ref->size);
1100 if (ref->size != entry->size) {
1101 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1102 "DMA memory with different size "
1103 "[device address=0x%016llx] [map size=%llu bytes] "
1104 "[unmap size=%llu bytes]\n",
1105 ref->dev_addr, entry->size, ref->size);
1108 if (ref->type != entry->type) {
1109 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1110 "DMA memory with wrong function "
1111 "[device address=0x%016llx] [size=%llu bytes] "
1112 "[mapped as %s] [unmapped as %s]\n",
1113 ref->dev_addr, ref->size,
1114 type2name[entry->type], type2name[ref->type]);
1115 } else if ((entry->type == dma_debug_coherent) &&
1116 (phys_addr(ref) != phys_addr(entry))) {
1117 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1118 "DMA memory with different CPU address "
1119 "[device address=0x%016llx] [size=%llu bytes] "
1120 "[cpu alloc address=0x%016llx] "
1121 "[cpu free address=0x%016llx]",
1122 ref->dev_addr, ref->size,
1127 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1128 ref->sg_call_ents != entry->sg_call_ents) {
1129 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1130 "DMA sg list with different entry count "
1131 "[map count=%d] [unmap count=%d]\n",
1132 entry->sg_call_ents, ref->sg_call_ents);
1136 * This may be no bug in reality - but most implementations of the
1137 * DMA API don't handle this properly, so check for it here
1139 if (ref->direction != entry->direction) {
1140 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1141 "DMA memory with different direction "
1142 "[device address=0x%016llx] [size=%llu bytes] "
1143 "[mapped with %s] [unmapped with %s]\n",
1144 ref->dev_addr, ref->size,
1145 dir2name[entry->direction],
1146 dir2name[ref->direction]);
1150 * Drivers should use dma_mapping_error() to check the returned
1151 * addresses of dma_map_single() and dma_map_page().
1152 * If not, print this warning message. See Documentation/DMA-API.txt.
1154 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1155 err_printk(ref->dev, entry,
1156 "DMA-API: device driver failed to check map error"
1157 "[device address=0x%016llx] [size=%llu bytes] "
1159 ref->dev_addr, ref->size,
1160 type2name[entry->type]);
1163 hash_bucket_del(entry);
1164 dma_entry_free(entry);
1166 put_hash_bucket(bucket, &flags);
1169 static void check_for_stack(struct device *dev,
1170 struct page *page, size_t offset)
1173 struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1175 if (!stack_vm_area) {
1176 /* Stack is direct-mapped. */
1177 if (PageHighMem(page))
1179 addr = page_address(page) + offset;
1180 if (object_is_on_stack(addr))
1181 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [addr=%p]\n", addr);
1183 /* Stack is vmalloced. */
1186 for (i = 0; i < stack_vm_area->nr_pages; i++) {
1187 if (page != stack_vm_area->pages[i])
1190 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1191 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [probable addr=%p]\n", addr);
1197 static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
1199 unsigned long a1 = (unsigned long)addr;
1200 unsigned long b1 = a1 + len;
1201 unsigned long a2 = (unsigned long)start;
1202 unsigned long b2 = (unsigned long)end;
1204 return !(b1 <= a2 || a1 >= b2);
1207 static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1209 if (overlap(addr, len, _stext, _etext) ||
1210 overlap(addr, len, __start_rodata, __end_rodata))
1211 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1214 static void check_sync(struct device *dev,
1215 struct dma_debug_entry *ref,
1218 struct dma_debug_entry *entry;
1219 struct hash_bucket *bucket;
1220 unsigned long flags;
1222 bucket = get_hash_bucket(ref, &flags);
1224 entry = bucket_find_contain(&bucket, ref, &flags);
1227 err_printk(dev, NULL, "DMA-API: device driver tries "
1228 "to sync DMA memory it has not allocated "
1229 "[device address=0x%016llx] [size=%llu bytes]\n",
1230 (unsigned long long)ref->dev_addr, ref->size);
1234 if (ref->size > entry->size) {
1235 err_printk(dev, entry, "DMA-API: device driver syncs"
1236 " DMA memory outside allocated range "
1237 "[device address=0x%016llx] "
1238 "[allocation size=%llu bytes] "
1239 "[sync offset+size=%llu]\n",
1240 entry->dev_addr, entry->size,
1244 if (entry->direction == DMA_BIDIRECTIONAL)
1247 if (ref->direction != entry->direction) {
1248 err_printk(dev, entry, "DMA-API: device driver syncs "
1249 "DMA memory with different direction "
1250 "[device address=0x%016llx] [size=%llu bytes] "
1251 "[mapped with %s] [synced with %s]\n",
1252 (unsigned long long)ref->dev_addr, entry->size,
1253 dir2name[entry->direction],
1254 dir2name[ref->direction]);
1257 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1258 !(ref->direction == DMA_TO_DEVICE))
1259 err_printk(dev, entry, "DMA-API: device driver syncs "
1260 "device read-only DMA memory for cpu "
1261 "[device address=0x%016llx] [size=%llu bytes] "
1262 "[mapped with %s] [synced with %s]\n",
1263 (unsigned long long)ref->dev_addr, entry->size,
1264 dir2name[entry->direction],
1265 dir2name[ref->direction]);
1267 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1268 !(ref->direction == DMA_FROM_DEVICE))
1269 err_printk(dev, entry, "DMA-API: device driver syncs "
1270 "device write-only DMA memory to device "
1271 "[device address=0x%016llx] [size=%llu bytes] "
1272 "[mapped with %s] [synced with %s]\n",
1273 (unsigned long long)ref->dev_addr, entry->size,
1274 dir2name[entry->direction],
1275 dir2name[ref->direction]);
1277 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1278 ref->sg_call_ents != entry->sg_call_ents) {
1279 err_printk(ref->dev, entry, "DMA-API: device driver syncs "
1280 "DMA sg list with different entry count "
1281 "[map count=%d] [sync count=%d]\n",
1282 entry->sg_call_ents, ref->sg_call_ents);
1286 put_hash_bucket(bucket, &flags);
1289 static void check_sg_segment(struct device *dev, struct scatterlist *sg)
1291 #ifdef CONFIG_DMA_API_DEBUG_SG
1292 unsigned int max_seg = dma_get_max_seg_size(dev);
1293 u64 start, end, boundary = dma_get_seg_boundary(dev);
1296 * Either the driver forgot to set dma_parms appropriately, or
1297 * whoever generated the list forgot to check them.
1299 if (sg->length > max_seg)
1300 err_printk(dev, NULL, "DMA-API: mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
1301 sg->length, max_seg);
1303 * In some cases this could potentially be the DMA API
1304 * implementation's fault, but it would usually imply that
1305 * the scatterlist was built inappropriately to begin with.
1307 start = sg_dma_address(sg);
1308 end = start + sg_dma_len(sg) - 1;
1309 if ((start ^ end) & ~boundary)
1310 err_printk(dev, NULL, "DMA-API: mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
1311 start, end, boundary);
1315 void debug_dma_map_single(struct device *dev, const void *addr,
1318 if (unlikely(dma_debug_disabled()))
1321 if (!virt_addr_valid(addr))
1322 err_printk(dev, NULL, "DMA-API: device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1325 if (is_vmalloc_addr(addr))
1326 err_printk(dev, NULL, "DMA-API: device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1329 EXPORT_SYMBOL(debug_dma_map_single);
1331 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1332 size_t size, int direction, dma_addr_t dma_addr,
1335 struct dma_debug_entry *entry;
1337 if (unlikely(dma_debug_disabled()))
1340 if (dma_mapping_error(dev, dma_addr))
1343 entry = dma_entry_alloc();
1348 entry->type = dma_debug_page;
1349 entry->pfn = page_to_pfn(page);
1350 entry->offset = offset,
1351 entry->dev_addr = dma_addr;
1353 entry->direction = direction;
1354 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1357 entry->type = dma_debug_single;
1359 check_for_stack(dev, page, offset);
1361 if (!PageHighMem(page)) {
1362 void *addr = page_address(page) + offset;
1364 check_for_illegal_area(dev, addr, size);
1367 add_dma_entry(entry);
1369 EXPORT_SYMBOL(debug_dma_map_page);
1371 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1373 struct dma_debug_entry ref;
1374 struct dma_debug_entry *entry;
1375 struct hash_bucket *bucket;
1376 unsigned long flags;
1378 if (unlikely(dma_debug_disabled()))
1382 ref.dev_addr = dma_addr;
1383 bucket = get_hash_bucket(&ref, &flags);
1385 list_for_each_entry(entry, &bucket->list, list) {
1386 if (!exact_match(&ref, entry))
1390 * The same physical address can be mapped multiple
1391 * times. Without a hardware IOMMU this results in the
1392 * same device addresses being put into the dma-debug
1393 * hash multiple times too. This can result in false
1394 * positives being reported. Therefore we implement a
1395 * best-fit algorithm here which updates the first entry
1396 * from the hash which fits the reference value and is
1397 * not currently listed as being checked.
1399 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1400 entry->map_err_type = MAP_ERR_CHECKED;
1405 put_hash_bucket(bucket, &flags);
1407 EXPORT_SYMBOL(debug_dma_mapping_error);
1409 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1410 size_t size, int direction, bool map_single)
1412 struct dma_debug_entry ref = {
1413 .type = dma_debug_page,
1417 .direction = direction,
1420 if (unlikely(dma_debug_disabled()))
1424 ref.type = dma_debug_single;
1428 EXPORT_SYMBOL(debug_dma_unmap_page);
1430 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1431 int nents, int mapped_ents, int direction)
1433 struct dma_debug_entry *entry;
1434 struct scatterlist *s;
1437 if (unlikely(dma_debug_disabled()))
1440 for_each_sg(sg, s, mapped_ents, i) {
1441 entry = dma_entry_alloc();
1445 entry->type = dma_debug_sg;
1447 entry->pfn = page_to_pfn(sg_page(s));
1448 entry->offset = s->offset,
1449 entry->size = sg_dma_len(s);
1450 entry->dev_addr = sg_dma_address(s);
1451 entry->direction = direction;
1452 entry->sg_call_ents = nents;
1453 entry->sg_mapped_ents = mapped_ents;
1455 check_for_stack(dev, sg_page(s), s->offset);
1457 if (!PageHighMem(sg_page(s))) {
1458 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
1461 check_sg_segment(dev, s);
1463 add_dma_entry(entry);
1466 EXPORT_SYMBOL(debug_dma_map_sg);
1468 static int get_nr_mapped_entries(struct device *dev,
1469 struct dma_debug_entry *ref)
1471 struct dma_debug_entry *entry;
1472 struct hash_bucket *bucket;
1473 unsigned long flags;
1476 bucket = get_hash_bucket(ref, &flags);
1477 entry = bucket_find_exact(bucket, ref);
1481 mapped_ents = entry->sg_mapped_ents;
1482 put_hash_bucket(bucket, &flags);
1487 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1488 int nelems, int dir)
1490 struct scatterlist *s;
1491 int mapped_ents = 0, i;
1493 if (unlikely(dma_debug_disabled()))
1496 for_each_sg(sglist, s, nelems, i) {
1498 struct dma_debug_entry ref = {
1499 .type = dma_debug_sg,
1501 .pfn = page_to_pfn(sg_page(s)),
1502 .offset = s->offset,
1503 .dev_addr = sg_dma_address(s),
1504 .size = sg_dma_len(s),
1506 .sg_call_ents = nelems,
1509 if (mapped_ents && i >= mapped_ents)
1513 mapped_ents = get_nr_mapped_entries(dev, &ref);
1518 EXPORT_SYMBOL(debug_dma_unmap_sg);
1520 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1521 dma_addr_t dma_addr, void *virt)
1523 struct dma_debug_entry *entry;
1525 if (unlikely(dma_debug_disabled()))
1528 if (unlikely(virt == NULL))
1531 /* handle vmalloc and linear addresses */
1532 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1535 entry = dma_entry_alloc();
1539 entry->type = dma_debug_coherent;
1541 entry->offset = offset_in_page(virt);
1543 entry->dev_addr = dma_addr;
1544 entry->direction = DMA_BIDIRECTIONAL;
1546 if (is_vmalloc_addr(virt))
1547 entry->pfn = vmalloc_to_pfn(virt);
1549 entry->pfn = page_to_pfn(virt_to_page(virt));
1551 add_dma_entry(entry);
1553 EXPORT_SYMBOL(debug_dma_alloc_coherent);
1555 void debug_dma_free_coherent(struct device *dev, size_t size,
1556 void *virt, dma_addr_t addr)
1558 struct dma_debug_entry ref = {
1559 .type = dma_debug_coherent,
1561 .offset = offset_in_page(virt),
1564 .direction = DMA_BIDIRECTIONAL,
1567 /* handle vmalloc and linear addresses */
1568 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
1571 if (is_vmalloc_addr(virt))
1572 ref.pfn = vmalloc_to_pfn(virt);
1574 ref.pfn = page_to_pfn(virt_to_page(virt));
1576 if (unlikely(dma_debug_disabled()))
1581 EXPORT_SYMBOL(debug_dma_free_coherent);
1583 void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1584 int direction, dma_addr_t dma_addr)
1586 struct dma_debug_entry *entry;
1588 if (unlikely(dma_debug_disabled()))
1591 entry = dma_entry_alloc();
1595 entry->type = dma_debug_resource;
1597 entry->pfn = PHYS_PFN(addr);
1598 entry->offset = offset_in_page(addr);
1600 entry->dev_addr = dma_addr;
1601 entry->direction = direction;
1602 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1604 add_dma_entry(entry);
1606 EXPORT_SYMBOL(debug_dma_map_resource);
1608 void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1609 size_t size, int direction)
1611 struct dma_debug_entry ref = {
1612 .type = dma_debug_resource,
1614 .dev_addr = dma_addr,
1616 .direction = direction,
1619 if (unlikely(dma_debug_disabled()))
1624 EXPORT_SYMBOL(debug_dma_unmap_resource);
1626 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1627 size_t size, int direction)
1629 struct dma_debug_entry ref;
1631 if (unlikely(dma_debug_disabled()))
1634 ref.type = dma_debug_single;
1636 ref.dev_addr = dma_handle;
1638 ref.direction = direction;
1639 ref.sg_call_ents = 0;
1641 check_sync(dev, &ref, true);
1643 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1645 void debug_dma_sync_single_for_device(struct device *dev,
1646 dma_addr_t dma_handle, size_t size,
1649 struct dma_debug_entry ref;
1651 if (unlikely(dma_debug_disabled()))
1654 ref.type = dma_debug_single;
1656 ref.dev_addr = dma_handle;
1658 ref.direction = direction;
1659 ref.sg_call_ents = 0;
1661 check_sync(dev, &ref, false);
1663 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1665 void debug_dma_sync_single_range_for_cpu(struct device *dev,
1666 dma_addr_t dma_handle,
1667 unsigned long offset, size_t size,
1670 struct dma_debug_entry ref;
1672 if (unlikely(dma_debug_disabled()))
1675 ref.type = dma_debug_single;
1677 ref.dev_addr = dma_handle;
1678 ref.size = offset + size;
1679 ref.direction = direction;
1680 ref.sg_call_ents = 0;
1682 check_sync(dev, &ref, true);
1684 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1686 void debug_dma_sync_single_range_for_device(struct device *dev,
1687 dma_addr_t dma_handle,
1688 unsigned long offset,
1689 size_t size, int direction)
1691 struct dma_debug_entry ref;
1693 if (unlikely(dma_debug_disabled()))
1696 ref.type = dma_debug_single;
1698 ref.dev_addr = dma_handle;
1699 ref.size = offset + size;
1700 ref.direction = direction;
1701 ref.sg_call_ents = 0;
1703 check_sync(dev, &ref, false);
1705 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1707 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1708 int nelems, int direction)
1710 struct scatterlist *s;
1711 int mapped_ents = 0, i;
1713 if (unlikely(dma_debug_disabled()))
1716 for_each_sg(sg, s, nelems, i) {
1718 struct dma_debug_entry ref = {
1719 .type = dma_debug_sg,
1721 .pfn = page_to_pfn(sg_page(s)),
1722 .offset = s->offset,
1723 .dev_addr = sg_dma_address(s),
1724 .size = sg_dma_len(s),
1725 .direction = direction,
1726 .sg_call_ents = nelems,
1730 mapped_ents = get_nr_mapped_entries(dev, &ref);
1732 if (i >= mapped_ents)
1735 check_sync(dev, &ref, true);
1738 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1740 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1741 int nelems, int direction)
1743 struct scatterlist *s;
1744 int mapped_ents = 0, i;
1746 if (unlikely(dma_debug_disabled()))
1749 for_each_sg(sg, s, nelems, i) {
1751 struct dma_debug_entry ref = {
1752 .type = dma_debug_sg,
1754 .pfn = page_to_pfn(sg_page(s)),
1755 .offset = s->offset,
1756 .dev_addr = sg_dma_address(s),
1757 .size = sg_dma_len(s),
1758 .direction = direction,
1759 .sg_call_ents = nelems,
1762 mapped_ents = get_nr_mapped_entries(dev, &ref);
1764 if (i >= mapped_ents)
1767 check_sync(dev, &ref, false);
1770 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1772 static int __init dma_debug_driver_setup(char *str)
1776 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1777 current_driver_name[i] = *str;
1782 if (current_driver_name[0])
1783 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1784 current_driver_name);
1789 __setup("dma_debug_driver=", dma_debug_driver_setup);