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