blob: 9e1777c81f5599d8748cdc7d573451b4c46faa7e [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedelf2f45e52009-01-09 12:19:52 +01002/*
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 *
5 * Author: Joerg Roedel <joerg.roedel@amd.com>
Joerg Roedelf2f45e52009-01-09 12:19:52 +01006 */
7
Robin Murphyf737b092018-12-10 14:00:27 +00008#define pr_fmt(fmt) "DMA-API: " fmt
9
Ingo Molnar68db0cf2017-02-08 18:51:37 +010010#include <linux/sched/task_stack.h>
Joerg Roedel972aa452009-01-09 14:19:54 +010011#include <linux/scatterlist.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010012#include <linux/dma-mapping.h>
Ingo Molnar29930022017-02-08 18:51:36 +010013#include <linux/sched/task.h>
David Woodhouse6c132d12009-01-19 16:52:39 +010014#include <linux/stacktrace.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010015#include <linux/dma-debug.h>
Joerg Roedel30dfa902009-01-09 12:34:49 +010016#include <linux/spinlock.h>
Andy Lutomirskib4a0f532016-08-11 02:35:22 -070017#include <linux/vmalloc.h>
Joerg Roedel788dcfa2009-01-09 13:13:27 +010018#include <linux/debugfs.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020019#include <linux/uaccess.h>
Paul Gortmaker23a7bfa2011-07-01 16:23:59 -040020#include <linux/export.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010021#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010022#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010023#include <linux/sched.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020024#include <linux/ctype.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010025#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010026#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010027
Joerg Roedel2e34bde2009-03-16 16:51:55 +010028#include <asm/sections.h>
29
Eric Dumazet5e76f562019-10-30 11:48:44 -070030#define HASH_SIZE 16384ULL
Joerg Roedel30dfa902009-01-09 12:34:49 +010031#define HASH_FN_SHIFT 13
32#define HASH_FN_MASK (HASH_SIZE - 1)
33
Christoph Hellwig15b28bb2018-04-16 17:22:28 +020034#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
Robin Murphy2b9d9ac2018-12-10 14:00:29 +000035/* If the pool runs out, add this many new entries at once */
Robin Murphyad78dee2018-12-10 14:00:33 +000036#define DMA_DEBUG_DYNAMIC_ENTRIES (PAGE_SIZE / sizeof(struct dma_debug_entry))
Christoph Hellwig15b28bb2018-04-16 17:22:28 +020037
Joerg Roedelf2f45e52009-01-09 12:19:52 +010038enum {
39 dma_debug_single,
Joerg Roedelf2f45e52009-01-09 12:19:52 +010040 dma_debug_sg,
41 dma_debug_coherent,
Niklas Söderlund0e74b342016-08-10 13:22:15 +020042 dma_debug_resource,
Joerg Roedelf2f45e52009-01-09 12:19:52 +010043};
44
Shuah Khan6c9c6d62012-10-08 11:08:06 -060045enum map_err_types {
46 MAP_ERR_CHECK_NOT_APPLICABLE,
47 MAP_ERR_NOT_CHECKED,
48 MAP_ERR_CHECKED,
49};
50
David Woodhouse6c132d12009-01-19 16:52:39 +010051#define DMA_DEBUG_STACKTRACE_ENTRIES 5
52
Dan Williams0abdd7a2014-01-21 15:48:12 -080053/**
54 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
55 * @list: node on pre-allocated free_entries list
56 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
Dan Williams0abdd7a2014-01-21 15:48:12 -080057 * @size: length of the mapping
Eric Dumazetd3694f302019-10-30 12:32:04 -070058 * @type: single, page, sg, coherent
Dan Williams0abdd7a2014-01-21 15:48:12 -080059 * @direction: enum dma_data_direction
60 * @sg_call_ents: 'nents' from dma_map_sg
61 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
Eric Dumazetd3694f302019-10-30 12:32:04 -070062 * @pfn: page frame of the start address
63 * @offset: offset of mapping relative to pfn
Dan Williams0abdd7a2014-01-21 15:48:12 -080064 * @map_err_type: track whether dma_mapping_error() was checked
65 * @stacktrace: support backtraces when a violation is detected
66 */
Joerg Roedelf2f45e52009-01-09 12:19:52 +010067struct dma_debug_entry {
68 struct list_head list;
69 struct device *dev;
Joerg Roedelf2f45e52009-01-09 12:19:52 +010070 u64 dev_addr;
71 u64 size;
Eric Dumazetd3694f302019-10-30 12:32:04 -070072 int type;
Joerg Roedelf2f45e52009-01-09 12:19:52 +010073 int direction;
74 int sg_call_ents;
75 int sg_mapped_ents;
Eric Dumazetd3694f302019-10-30 12:32:04 -070076 unsigned long pfn;
77 size_t offset;
Shuah Khan6c9c6d62012-10-08 11:08:06 -060078 enum map_err_types map_err_type;
David Woodhouse6c132d12009-01-19 16:52:39 +010079#ifdef CONFIG_STACKTRACE
Thomas Gleixner746017e2019-04-25 11:45:05 +020080 unsigned int stack_len;
81 unsigned long stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
David Woodhouse6c132d12009-01-19 16:52:39 +010082#endif
Eric Dumazetd3694f302019-10-30 12:32:04 -070083} ____cacheline_aligned_in_smp;
Joerg Roedelf2f45e52009-01-09 12:19:52 +010084
Neil Hormanc6a21d02011-08-08 15:13:54 -040085typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
86
Joerg Roedel30dfa902009-01-09 12:34:49 +010087struct hash_bucket {
88 struct list_head list;
89 spinlock_t lock;
Eric Dumazet5e76f562019-10-30 11:48:44 -070090};
Joerg Roedel30dfa902009-01-09 12:34:49 +010091
92/* Hash list to save the allocated dma addresses */
93static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010094/* List of pre-allocated dma_debug_entry's */
95static LIST_HEAD(free_entries);
96/* Lock for the list above */
97static DEFINE_SPINLOCK(free_entries_lock);
98
99/* Global disable flag - will be set in case of an error */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700100static bool global_disable __read_mostly;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100101
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800102/* Early initialization disable flag, set at the end of dma_debug_init */
103static bool dma_debug_initialized __read_mostly;
104
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800105static inline bool dma_debug_disabled(void)
106{
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800107 return global_disable || !dma_debug_initialized;
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800108}
109
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100110/* Global error count */
111static u32 error_count;
112
113/* Global error show enable*/
114static u32 show_all_errors __read_mostly;
115/* Number of errors to show */
116static u32 show_num_errors = 1;
117
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100118static u32 num_free_entries;
119static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900120static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100121
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100122/* number of preallocated entries requested by kernel cmdline */
Christoph Hellwigbcebe322018-04-24 09:40:51 +0200123static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100124
Joerg Roedel2e507d82009-05-22 18:24:20 +0200125/* per-driver filter related state */
126
127#define NAME_MAX_LEN 64
128
129static char current_driver_name[NAME_MAX_LEN] __read_mostly;
130static struct device_driver *current_driver __read_mostly;
131
132static DEFINE_RWLOCK(driver_name_lock);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100133
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600134static const char *const maperr2str[] = {
135 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
136 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
137 [MAP_ERR_CHECKED] = "dma map error checked",
138};
139
Grygorii Strashko9bb50ed2020-04-08 22:43:00 +0300140static const char *type2name[] = {
141 [dma_debug_single] = "single",
142 [dma_debug_sg] = "scather-gather",
143 [dma_debug_coherent] = "coherent",
144 [dma_debug_resource] = "resource",
145};
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100146
147static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
148 "DMA_FROM_DEVICE", "DMA_NONE" };
149
150/*
151 * The access to some variables in this macro is racy. We can't use atomic_t
152 * here because all these variables are exported to debugfs. Some of them even
153 * writeable. This is also the reason why a lock won't help much. But anyway,
154 * the races are no big deal. Here is why:
155 *
156 * error_count: the addition is racy, but the worst thing that can happen is
157 * that we don't count some errors
158 * show_num_errors: the subtraction is racy. Also no big deal because in
159 * worst case this will result in one warning more in the
160 * system log than the user configured. This variable is
161 * writeable via debugfs.
162 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100163static inline void dump_entry_trace(struct dma_debug_entry *entry)
164{
165#ifdef CONFIG_STACKTRACE
166 if (entry) {
Kefeng Wangfc651042019-10-18 11:18:42 +0800167 pr_warn("Mapped at:\n");
Thomas Gleixner746017e2019-04-25 11:45:05 +0200168 stack_trace_print(entry->stack_entries, entry->stack_len, 0);
David Woodhouse6c132d12009-01-19 16:52:39 +0100169 }
170#endif
171}
172
Joerg Roedel2e507d82009-05-22 18:24:20 +0200173static bool driver_filter(struct device *dev)
174{
Joerg Roedel0bf84122009-06-08 15:53:46 +0200175 struct device_driver *drv;
176 unsigned long flags;
177 bool ret;
178
Joerg Roedel2e507d82009-05-22 18:24:20 +0200179 /* driver filter off */
180 if (likely(!current_driver_name[0]))
181 return true;
182
183 /* driver filter on and initialized */
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400184 if (current_driver && dev && dev->driver == current_driver)
Joerg Roedel2e507d82009-05-22 18:24:20 +0200185 return true;
186
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400187 /* driver filter on, but we can't filter on a NULL device... */
188 if (!dev)
189 return false;
190
Joerg Roedel0bf84122009-06-08 15:53:46 +0200191 if (current_driver || !current_driver_name[0])
192 return false;
193
Joerg Roedel2e507d82009-05-22 18:24:20 +0200194 /* driver filter on but not yet initialized */
Alan Sternf3ff9242012-01-24 13:35:24 -0500195 drv = dev->driver;
Joerg Roedel0bf84122009-06-08 15:53:46 +0200196 if (!drv)
197 return false;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200198
Joerg Roedel0bf84122009-06-08 15:53:46 +0200199 /* lock to protect against change of current_driver_name */
200 read_lock_irqsave(&driver_name_lock, flags);
Joerg Roedel2e507d82009-05-22 18:24:20 +0200201
Joerg Roedel0bf84122009-06-08 15:53:46 +0200202 ret = false;
203 if (drv->name &&
204 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
205 current_driver = drv;
206 ret = true;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200207 }
208
Joerg Roedel0bf84122009-06-08 15:53:46 +0200209 read_unlock_irqrestore(&driver_name_lock, flags);
Joerg Roedel0bf84122009-06-08 15:53:46 +0200210
211 return ret;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200212}
213
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400214#define err_printk(dev, entry, format, arg...) do { \
215 error_count += 1; \
216 if (driver_filter(dev) && \
217 (show_all_errors || show_num_errors > 0)) { \
Robin Murphyf737b092018-12-10 14:00:27 +0000218 WARN(1, pr_fmt("%s %s: ") format, \
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400219 dev ? dev_driver_string(dev) : "NULL", \
220 dev ? dev_name(dev) : "NULL", ## arg); \
221 dump_entry_trace(entry); \
222 } \
223 if (!show_all_errors && show_num_errors > 0) \
224 show_num_errors -= 1; \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100225 } while (0);
226
Joerg Roedel30dfa902009-01-09 12:34:49 +0100227/*
228 * Hash related functions
229 *
230 * Every DMA-API request is saved into a struct dma_debug_entry. To
231 * have quick access to these structs they are stored into a hash.
232 */
233static int hash_fn(struct dma_debug_entry *entry)
234{
235 /*
236 * Hash function is based on the dma address.
237 * We use bits 20-27 here as the index into the hash
238 */
239 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
240}
241
242/*
243 * Request exclusive access to a hash bucket for a given dma_debug_entry.
244 */
245static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
246 unsigned long *flags)
Stephen Boydd5dfc802016-07-26 15:21:08 -0700247 __acquires(&dma_entry_hash[idx].lock)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100248{
249 int idx = hash_fn(entry);
250 unsigned long __flags;
251
252 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
253 *flags = __flags;
254 return &dma_entry_hash[idx];
255}
256
257/*
258 * Give up exclusive access to the hash bucket
259 */
260static void put_hash_bucket(struct hash_bucket *bucket,
Dan Carpenter50f579a2019-11-19 09:18:19 +0300261 unsigned long flags)
Stephen Boydd5dfc802016-07-26 15:21:08 -0700262 __releases(&bucket->lock)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100263{
Dan Carpenter50f579a2019-11-19 09:18:19 +0300264 spin_unlock_irqrestore(&bucket->lock, flags);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100265}
266
Neil Hormanc6a21d02011-08-08 15:13:54 -0400267static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
268{
Thomas Jarosch91ec37c2011-11-17 20:31:02 +0100269 return ((a->dev_addr == b->dev_addr) &&
Neil Hormanc6a21d02011-08-08 15:13:54 -0400270 (a->dev == b->dev)) ? true : false;
271}
272
273static bool containing_match(struct dma_debug_entry *a,
274 struct dma_debug_entry *b)
275{
276 if (a->dev != b->dev)
277 return false;
278
279 if ((b->dev_addr <= a->dev_addr) &&
280 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
281 return true;
282
283 return false;
284}
285
Joerg Roedel30dfa902009-01-09 12:34:49 +0100286/*
287 * Search a given entry in the hash bucket list
288 */
Neil Hormanc6a21d02011-08-08 15:13:54 -0400289static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
290 struct dma_debug_entry *ref,
291 match_fn match)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100292{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200293 struct dma_debug_entry *entry, *ret = NULL;
Ming Leife73fbe2012-10-19 13:57:01 -0700294 int matches = 0, match_lvl, last_lvl = -1;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100295
296 list_for_each_entry(entry, &bucket->list, list) {
Neil Hormanc6a21d02011-08-08 15:13:54 -0400297 if (!match(ref, entry))
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200298 continue;
299
300 /*
301 * Some drivers map the same physical address multiple
302 * times. Without a hardware IOMMU this results in the
303 * same device addresses being put into the dma-debug
304 * hash multiple times too. This can result in false
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200305 * positives being reported. Therefore we implement a
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200306 * best-fit algorithm here which returns the entry from
307 * the hash which fits best to the reference value
308 * instead of the first-fit.
309 */
310 matches += 1;
311 match_lvl = 0;
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200312 entry->size == ref->size ? ++match_lvl : 0;
313 entry->type == ref->type ? ++match_lvl : 0;
314 entry->direction == ref->direction ? ++match_lvl : 0;
315 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200316
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200317 if (match_lvl == 4) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200318 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100319 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200320 } else if (match_lvl > last_lvl) {
321 /*
322 * We found an entry that fits better then the
Ming Leife73fbe2012-10-19 13:57:01 -0700323 * previous one or it is the 1st match.
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200324 */
325 last_lvl = match_lvl;
326 ret = entry;
327 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100328 }
329
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200330 /*
331 * If we have multiple matches but no perfect-fit, just return
332 * NULL.
333 */
334 ret = (matches == 1) ? ret : NULL;
335
336 return ret;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100337}
338
Neil Hormanc6a21d02011-08-08 15:13:54 -0400339static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
340 struct dma_debug_entry *ref)
341{
342 return __hash_bucket_find(bucket, ref, exact_match);
343}
344
345static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
346 struct dma_debug_entry *ref,
347 unsigned long *flags)
348{
349
350 unsigned int max_range = dma_get_max_seg_size(ref->dev);
351 struct dma_debug_entry *entry, index = *ref;
352 unsigned int range = 0;
353
354 while (range <= max_range) {
Sebastian Otta7a2c022015-04-16 12:43:25 -0700355 entry = __hash_bucket_find(*bucket, ref, containing_match);
Neil Hormanc6a21d02011-08-08 15:13:54 -0400356
357 if (entry)
358 return entry;
359
360 /*
361 * Nothing found, go back a hash bucket
362 */
Dan Carpenter50f579a2019-11-19 09:18:19 +0300363 put_hash_bucket(*bucket, *flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -0400364 range += (1 << HASH_FN_SHIFT);
365 index.dev_addr -= (1 << HASH_FN_SHIFT);
366 *bucket = get_hash_bucket(&index, flags);
367 }
368
369 return NULL;
370}
371
Joerg Roedel30dfa902009-01-09 12:34:49 +0100372/*
373 * Add an entry to a hash bucket
374 */
375static void hash_bucket_add(struct hash_bucket *bucket,
376 struct dma_debug_entry *entry)
377{
378 list_add_tail(&entry->list, &bucket->list);
379}
380
381/*
382 * Remove entry from a hash bucket list
383 */
384static void hash_bucket_del(struct dma_debug_entry *entry)
385{
386 list_del(&entry->list);
387}
388
Dan Williams0abdd7a2014-01-21 15:48:12 -0800389static unsigned long long phys_addr(struct dma_debug_entry *entry)
390{
Niklas Söderlund0e74b342016-08-10 13:22:15 +0200391 if (entry->type == dma_debug_resource)
392 return __pfn_to_phys(entry->pfn) + entry->offset;
393
Dan Williams0abdd7a2014-01-21 15:48:12 -0800394 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
395}
396
Joerg Roedel30dfa902009-01-09 12:34:49 +0100397/*
David Woodhouseac26c182009-02-12 16:19:13 +0100398 * Dump mapping entries for debugging purposes
399 */
400void debug_dma_dump_mappings(struct device *dev)
401{
402 int idx;
403
404 for (idx = 0; idx < HASH_SIZE; idx++) {
405 struct hash_bucket *bucket = &dma_entry_hash[idx];
406 struct dma_debug_entry *entry;
407 unsigned long flags;
408
409 spin_lock_irqsave(&bucket->lock, flags);
410
411 list_for_each_entry(entry, &bucket->list, list) {
412 if (!dev || dev == entry->dev) {
413 dev_info(entry->dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800414 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
David Woodhouseac26c182009-02-12 16:19:13 +0100415 type2name[entry->type], idx,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800416 phys_addr(entry), entry->pfn,
David Woodhouseac26c182009-02-12 16:19:13 +0100417 entry->dev_addr, entry->size,
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600418 dir2name[entry->direction],
419 maperr2str[entry->map_err_type]);
David Woodhouseac26c182009-02-12 16:19:13 +0100420 }
421 }
422
423 spin_unlock_irqrestore(&bucket->lock, flags);
Eric Dumazet9ff6aa02019-10-28 14:56:46 -0700424 cond_resched();
David Woodhouseac26c182009-02-12 16:19:13 +0100425 }
426}
David Woodhouseac26c182009-02-12 16:19:13 +0100427
428/*
Dan Williams3b7a6412014-03-03 15:38:21 -0800429 * For each mapping (initial cacheline in the case of
430 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
431 * scatterlist, or the cacheline specified in dma_map_single) insert
432 * into this tree using the cacheline as the key. At
Dan Williams0abdd7a2014-01-21 15:48:12 -0800433 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
Dan Williams3b7a6412014-03-03 15:38:21 -0800434 * the entry already exists at insertion time add a tag as a reference
Dan Williams0abdd7a2014-01-21 15:48:12 -0800435 * count for the overlapping mappings. For now, the overlap tracking
Dan Williams3b7a6412014-03-03 15:38:21 -0800436 * just ensures that 'unmaps' balance 'maps' before marking the
437 * cacheline idle, but we should also be flagging overlaps as an API
438 * violation.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800439 *
440 * Memory usage is mostly constrained by the maximum number of available
441 * dma-debug entries in that we need a free dma_debug_entry before
Dan Williams3b7a6412014-03-03 15:38:21 -0800442 * inserting into the tree. In the case of dma_map_page and
443 * dma_alloc_coherent there is only one dma_debug_entry and one
444 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
445 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
446 * entries into the tree.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800447 *
448 * At any time debug_dma_assert_idle() can be called to trigger a
Dan Williams3b7a6412014-03-03 15:38:21 -0800449 * warning if any cachelines in the given page are in the active set.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800450 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800451static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800452static DEFINE_SPINLOCK(radix_lock);
Dan Williams3b7a6412014-03-03 15:38:21 -0800453#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)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800456
Dan Williams3b7a6412014-03-03 15:38:21 -0800457static 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)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800464{
465 int overlap = 0, i;
466
467 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
Dan Williams3b7a6412014-03-03 15:38:21 -0800468 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
Dan Williams0abdd7a2014-01-21 15:48:12 -0800469 overlap |= 1 << i;
470 return overlap;
471}
472
Dan Williams3b7a6412014-03-03 15:38:21 -0800473static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800474{
475 int i;
476
Dan Williams3b7a6412014-03-03 15:38:21 -0800477 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
Dan Williams59f2e7d2014-01-29 14:05:53 -0800478 return overlap;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800479
480 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
481 if (overlap & 1 << i)
Dan Williams3b7a6412014-03-03 15:38:21 -0800482 radix_tree_tag_set(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800483 else
Dan Williams3b7a6412014-03-03 15:38:21 -0800484 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800485
486 return overlap;
487}
488
Dan Williams3b7a6412014-03-03 15:38:21 -0800489static void active_cacheline_inc_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800490{
Dan Williams3b7a6412014-03-03 15:38:21 -0800491 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800492
Dan Williams3b7a6412014-03-03 15:38:21 -0800493 overlap = active_cacheline_set_overlap(cln, ++overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800494
495 /* If we overflowed the overlap counter then we're potentially
496 * leaking dma-mappings. Otherwise, if maps and unmaps are
497 * balanced then this overflow may cause false negatives in
Dan Williams3b7a6412014-03-03 15:38:21 -0800498 * debug_dma_assert_idle() as the cacheline may be marked idle
Dan Williams0abdd7a2014-01-21 15:48:12 -0800499 * prematurely.
500 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800501 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
Robin Murphyf737b092018-12-10 14:00:27 +0000502 pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
Dan Williams3b7a6412014-03-03 15:38:21 -0800503 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800504}
505
Dan Williams3b7a6412014-03-03 15:38:21 -0800506static int active_cacheline_dec_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800507{
Dan Williams3b7a6412014-03-03 15:38:21 -0800508 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800509
Dan Williams3b7a6412014-03-03 15:38:21 -0800510 return active_cacheline_set_overlap(cln, --overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800511}
512
Dan Williams3b7a6412014-03-03 15:38:21 -0800513static int active_cacheline_insert(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800514{
Dan Williams3b7a6412014-03-03 15:38:21 -0800515 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800516 unsigned long flags;
517 int rc;
518
Dan Williams3b7a6412014-03-03 15:38:21 -0800519 /* If the device is not writing memory then we don't have any
520 * concerns about the cpu consuming stale data. This mitigates
521 * legitimate usages of overlapping mappings.
522 */
523 if (entry->direction == DMA_TO_DEVICE)
524 return 0;
525
Dan Williams0abdd7a2014-01-21 15:48:12 -0800526 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800527 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800528 if (rc == -EEXIST)
Dan Williams3b7a6412014-03-03 15:38:21 -0800529 active_cacheline_inc_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800530 spin_unlock_irqrestore(&radix_lock, flags);
531
532 return rc;
533}
534
Dan Williams3b7a6412014-03-03 15:38:21 -0800535static void active_cacheline_remove(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800536{
Dan Williams3b7a6412014-03-03 15:38:21 -0800537 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800538 unsigned long flags;
539
Dan Williams3b7a6412014-03-03 15:38:21 -0800540 /* ...mirror the insert case */
541 if (entry->direction == DMA_TO_DEVICE)
542 return;
543
Dan Williams0abdd7a2014-01-21 15:48:12 -0800544 spin_lock_irqsave(&radix_lock, flags);
Dan Williams59f2e7d2014-01-29 14:05:53 -0800545 /* since we are counting overlaps the final put of the
Dan Williams3b7a6412014-03-03 15:38:21 -0800546 * cacheline will occur when the overlap count is 0.
547 * active_cacheline_dec_overlap() returns -1 in that case
Dan Williams59f2e7d2014-01-29 14:05:53 -0800548 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800549 if (active_cacheline_dec_overlap(cln) < 0)
550 radix_tree_delete(&dma_active_cacheline, cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800551 spin_unlock_irqrestore(&radix_lock, flags);
552}
553
554/**
555 * debug_dma_assert_idle() - assert that a page is not undergoing dma
Dan Williams3b7a6412014-03-03 15:38:21 -0800556 * @page: page to lookup in the dma_active_cacheline tree
Dan Williams0abdd7a2014-01-21 15:48:12 -0800557 *
558 * Place a call to this routine in cases where the cpu touching the page
559 * before the dma completes (page is dma_unmapped) will lead to data
560 * corruption.
561 */
562void debug_dma_assert_idle(struct page *page)
563{
Dan Williams3b7a6412014-03-03 15:38:21 -0800564 static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
565 struct dma_debug_entry *entry = NULL;
566 void **results = (void **) &ents;
567 unsigned int nents, i;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800568 unsigned long flags;
Dan Williams3b7a6412014-03-03 15:38:21 -0800569 phys_addr_t cln;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800570
Haggai Eranc9d120b2015-07-17 16:24:06 -0700571 if (dma_debug_disabled())
572 return;
573
Dan Williams0abdd7a2014-01-21 15:48:12 -0800574 if (!page)
575 return;
576
Dan Williams3b7a6412014-03-03 15:38:21 -0800577 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800578 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800579 nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
580 CACHELINES_PER_PAGE);
581 for (i = 0; i < nents; i++) {
582 phys_addr_t ent_cln = to_cacheline_number(ents[i]);
583
584 if (ent_cln == cln) {
585 entry = ents[i];
586 break;
587 } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
588 break;
589 }
Dan Williams0abdd7a2014-01-21 15:48:12 -0800590 spin_unlock_irqrestore(&radix_lock, flags);
591
592 if (!entry)
593 return;
594
Dan Williams3b7a6412014-03-03 15:38:21 -0800595 cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800596 err_printk(entry->dev, entry,
Robin Murphyf737b092018-12-10 14:00:27 +0000597 "cpu touching an active dma mapped cacheline [cln=%pa]\n",
Dan Williams3b7a6412014-03-03 15:38:21 -0800598 &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800599}
600
601/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100602 * Wrapper function for adding an entry to the hash.
603 * This function takes care of locking itself.
604 */
605static void add_dma_entry(struct dma_debug_entry *entry)
606{
607 struct hash_bucket *bucket;
608 unsigned long flags;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800609 int rc;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100610
611 bucket = get_hash_bucket(entry, &flags);
612 hash_bucket_add(bucket, entry);
Dan Carpenter50f579a2019-11-19 09:18:19 +0300613 put_hash_bucket(bucket, flags);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800614
Dan Williams3b7a6412014-03-03 15:38:21 -0800615 rc = active_cacheline_insert(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800616 if (rc == -ENOMEM) {
Robin Murphyf737b092018-12-10 14:00:27 +0000617 pr_err("cacheline tracking ENOMEM, dma-debug disabled\n");
Dan Williams0abdd7a2014-01-21 15:48:12 -0800618 global_disable = true;
619 }
620
621 /* TODO: report -EEXIST errors here as overlapping mappings are
622 * not supported by the DMA API
623 */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100624}
625
Robin Murphyad78dee2018-12-10 14:00:33 +0000626static int dma_debug_create_entries(gfp_t gfp)
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000627{
Robin Murphyad78dee2018-12-10 14:00:33 +0000628 struct dma_debug_entry *entry;
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000629 int i;
630
Robin Murphyad78dee2018-12-10 14:00:33 +0000631 entry = (void *)get_zeroed_page(gfp);
632 if (!entry)
633 return -ENOMEM;
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000634
Robin Murphyad78dee2018-12-10 14:00:33 +0000635 for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
636 list_add_tail(&entry[i].list, &free_entries);
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000637
Robin Murphyad78dee2018-12-10 14:00:33 +0000638 num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
639 nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000640
641 return 0;
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000642}
643
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900644static struct dma_debug_entry *__dma_entry_alloc(void)
645{
646 struct dma_debug_entry *entry;
647
648 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
649 list_del(&entry->list);
650 memset(entry, 0, sizeof(*entry));
651
652 num_free_entries -= 1;
653 if (num_free_entries < min_free_entries)
654 min_free_entries = num_free_entries;
655
656 return entry;
657}
658
Robin Murphyceb51172018-12-10 14:00:30 +0000659void __dma_entry_alloc_check_leak(void)
660{
661 u32 tmp = nr_total_entries % nr_prealloc_entries;
662
663 /* Shout each time we tick over some multiple of the initial pool */
664 if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
665 pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
666 nr_total_entries,
667 (nr_total_entries / nr_prealloc_entries));
668 }
669}
670
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100671/* struct dma_entry allocator
672 *
673 * The next two functions implement the allocator for
674 * struct dma_debug_entries.
675 */
676static struct dma_debug_entry *dma_entry_alloc(void)
677{
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200678 struct dma_debug_entry *entry;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100679 unsigned long flags;
680
681 spin_lock_irqsave(&free_entries_lock, flags);
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000682 if (num_free_entries == 0) {
Robin Murphyad78dee2018-12-10 14:00:33 +0000683 if (dma_debug_create_entries(GFP_ATOMIC)) {
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000684 global_disable = true;
685 spin_unlock_irqrestore(&free_entries_lock, flags);
686 pr_err("debugging out of memory - disabling\n");
687 return NULL;
688 }
Robin Murphyceb51172018-12-10 14:00:30 +0000689 __dma_entry_alloc_check_leak();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100690 }
691
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900692 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100693
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200694 spin_unlock_irqrestore(&free_entries_lock, flags);
695
David Woodhouse6c132d12009-01-19 16:52:39 +0100696#ifdef CONFIG_STACKTRACE
Thomas Gleixner746017e2019-04-25 11:45:05 +0200697 entry->stack_len = stack_trace_save(entry->stack_entries,
698 ARRAY_SIZE(entry->stack_entries),
699 1);
David Woodhouse6c132d12009-01-19 16:52:39 +0100700#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100701 return entry;
702}
703
704static void dma_entry_free(struct dma_debug_entry *entry)
705{
706 unsigned long flags;
707
Dan Williams3b7a6412014-03-03 15:38:21 -0800708 active_cacheline_remove(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800709
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100710 /*
711 * add to beginning of the list - this way the entries are
712 * more likely cache hot when they are reallocated.
713 */
714 spin_lock_irqsave(&free_entries_lock, flags);
715 list_add(&entry->list, &free_entries);
716 num_free_entries += 1;
717 spin_unlock_irqrestore(&free_entries_lock, flags);
718}
719
Joerg Roedel6bf07872009-01-09 12:54:42 +0100720/*
721 * DMA-API debugging init code
722 *
723 * The init code does two things:
724 * 1. Initialize core data structures
725 * 2. Preallocate a given number of dma_debug_entry structs
726 */
727
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200728static ssize_t filter_read(struct file *file, char __user *user_buf,
729 size_t count, loff_t *ppos)
730{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200731 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200732 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200733 int len;
734
735 if (!current_driver_name[0])
736 return 0;
737
738 /*
739 * We can't copy to userspace directly because current_driver_name can
740 * only be read under the driver_name_lock with irqs disabled. So
741 * create a temporary copy first.
742 */
743 read_lock_irqsave(&driver_name_lock, flags);
744 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
745 read_unlock_irqrestore(&driver_name_lock, flags);
746
747 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
748}
749
750static ssize_t filter_write(struct file *file, const char __user *userbuf,
751 size_t count, loff_t *ppos)
752{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200753 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200754 unsigned long flags;
755 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200756 int i;
757
758 /*
759 * We can't copy from userspace directly. Access to
760 * current_driver_name is protected with a write_lock with irqs
761 * disabled. Since copy_from_user can fault and may sleep we
762 * need to copy to temporary buffer first
763 */
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200764 len = min(count, (size_t)(NAME_MAX_LEN - 1));
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200765 if (copy_from_user(buf, userbuf, len))
766 return -EFAULT;
767
768 buf[len] = 0;
769
770 write_lock_irqsave(&driver_name_lock, flags);
771
Joerg Roedel312325092009-06-08 15:07:08 +0200772 /*
773 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200774 * The rules are:
775 * - only use the first token we got
776 * - token delimiter is everything looking like a space
777 * character (' ', '\n', '\t' ...)
778 *
779 */
780 if (!isalnum(buf[0])) {
781 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200782 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200783 * alphanumerical then assume the filter should be
784 * switched off.
785 */
786 if (current_driver_name[0])
Robin Murphyf737b092018-12-10 14:00:27 +0000787 pr_info("switching off dma-debug driver filter\n");
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200788 current_driver_name[0] = 0;
789 current_driver = NULL;
790 goto out_unlock;
791 }
792
793 /*
794 * Now parse out the first token and use it as the name for the
795 * driver to filter for.
796 */
Dan Carpenter39a37ce2010-04-06 19:45:12 +0300797 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200798 current_driver_name[i] = buf[i];
799 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
800 break;
801 }
802 current_driver_name[i] = 0;
803 current_driver = NULL;
804
Robin Murphyf737b092018-12-10 14:00:27 +0000805 pr_info("enable driver filter for driver [%s]\n",
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200806 current_driver_name);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200807
808out_unlock:
809 write_unlock_irqrestore(&driver_name_lock, flags);
810
811 return count;
812}
813
Thiago Farinaaeb583d2010-01-18 18:57:33 -0500814static const struct file_operations filter_fops = {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200815 .read = filter_read,
816 .write = filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200817 .llseek = default_llseek,
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200818};
819
Corentin Labbe0a3b1922019-01-18 13:44:18 +0000820static int dump_show(struct seq_file *seq, void *v)
821{
822 int idx;
823
824 for (idx = 0; idx < HASH_SIZE; idx++) {
825 struct hash_bucket *bucket = &dma_entry_hash[idx];
826 struct dma_debug_entry *entry;
827 unsigned long flags;
828
829 spin_lock_irqsave(&bucket->lock, flags);
830 list_for_each_entry(entry, &bucket->list, list) {
831 seq_printf(seq,
832 "%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx %s %s\n",
833 dev_name(entry->dev),
834 dev_driver_string(entry->dev),
835 type2name[entry->type], idx,
836 phys_addr(entry), entry->pfn,
837 entry->dev_addr, entry->size,
838 dir2name[entry->direction],
839 maperr2str[entry->map_err_type]);
840 }
841 spin_unlock_irqrestore(&bucket->lock, flags);
842 }
843 return 0;
844}
845DEFINE_SHOW_ATTRIBUTE(dump);
846
Greg Kroah-Hartman8e4d81b2019-01-22 16:21:38 +0100847static void dma_debug_fs_init(void)
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100848{
Greg Kroah-Hartman8e4d81b2019-01-22 16:21:38 +0100849 struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100850
Greg Kroah-Hartman8e4d81b2019-01-22 16:21:38 +0100851 debugfs_create_bool("disabled", 0444, dentry, &global_disable);
852 debugfs_create_u32("error_count", 0444, dentry, &error_count);
853 debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
854 debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
855 debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
856 debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
857 debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
858 debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
Corentin Labbe0a3b1922019-01-18 13:44:18 +0000859 debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100860}
861
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400862static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200863{
864 struct dma_debug_entry *entry;
865 unsigned long flags;
866 int count = 0, i;
867
868 for (i = 0; i < HASH_SIZE; ++i) {
Pankaj Gupta6a5cd602017-05-03 14:51:28 -0700869 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200870 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400871 if (entry->dev == dev) {
Joerg Roedeled888ae2009-05-22 17:16:04 +0200872 count += 1;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400873 *out_entry = entry;
874 }
Joerg Roedeled888ae2009-05-22 17:16:04 +0200875 }
Pankaj Gupta6a5cd602017-05-03 14:51:28 -0700876 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200877 }
878
879 return count;
880}
881
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100882static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200883{
884 struct device *dev = data;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400885 struct dma_debug_entry *uninitialized_var(entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200886 int count;
887
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800888 if (dma_debug_disabled())
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100889 return 0;
Joerg Roedeled888ae2009-05-22 17:16:04 +0200890
891 switch (action) {
892 case BUS_NOTIFY_UNBOUND_DRIVER:
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400893 count = device_dma_allocations(dev, &entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200894 if (count == 0)
895 break;
Robin Murphyf737b092018-12-10 14:00:27 +0000896 err_printk(dev, entry, "device driver has pending "
Joerg Roedeled888ae2009-05-22 17:16:04 +0200897 "DMA allocations while released from device "
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400898 "[count=%d]\n"
899 "One of leaked entries details: "
900 "[device address=0x%016llx] [size=%llu bytes] "
901 "[mapped with %s] [mapped as %s]\n",
902 count, entry->dev_addr, entry->size,
903 dir2name[entry->direction], type2name[entry->type]);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200904 break;
905 default:
906 break;
907 }
908
909 return 0;
910}
911
Joerg Roedel41531c82009-03-16 17:32:14 +0100912void dma_debug_add_bus(struct bus_type *bus)
913{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200914 struct notifier_block *nb;
915
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800916 if (dma_debug_disabled())
Shaun Ruffellf797d982009-12-17 18:00:36 -0600917 return;
918
Joerg Roedeled888ae2009-05-22 17:16:04 +0200919 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
920 if (nb == NULL) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200921 pr_err("dma_debug_add_bus: out of memory\n");
Joerg Roedeled888ae2009-05-22 17:16:04 +0200922 return;
923 }
924
925 nb->notifier_call = dma_debug_device_change;
926
927 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100928}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100929
Christoph Hellwig15b28bb2018-04-16 17:22:28 +0200930static int dma_debug_init(void)
Joerg Roedel6bf07872009-01-09 12:54:42 +0100931{
Robin Murphyad78dee2018-12-10 14:00:33 +0000932 int i, nr_pages;
Joerg Roedel6bf07872009-01-09 12:54:42 +0100933
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800934 /* Do not use dma_debug_initialized here, since we really want to be
935 * called to set dma_debug_initialized
936 */
937 if (global_disable)
Christoph Hellwig15b28bb2018-04-16 17:22:28 +0200938 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +0100939
940 for (i = 0; i < HASH_SIZE; ++i) {
941 INIT_LIST_HEAD(&dma_entry_hash[i].list);
Ingo Molnarb0a5b832009-06-16 16:11:14 +0200942 spin_lock_init(&dma_entry_hash[i].lock);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100943 }
944
Greg Kroah-Hartman8e4d81b2019-01-22 16:21:38 +0100945 dma_debug_fs_init();
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100946
Robin Murphyad78dee2018-12-10 14:00:33 +0000947 nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
948 for (i = 0; i < nr_pages; ++i)
949 dma_debug_create_entries(GFP_KERNEL);
950 if (num_free_entries >= nr_prealloc_entries) {
951 pr_info("preallocated %d debug entries\n", nr_total_entries);
952 } else if (num_free_entries > 0) {
953 pr_warn("%d debug entries requested but only %d allocated\n",
954 nr_prealloc_entries, nr_total_entries);
955 } else {
Robin Murphyf737b092018-12-10 14:00:27 +0000956 pr_err("debugging out of memory error - disabled\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +0100957 global_disable = true;
958
Christoph Hellwig15b28bb2018-04-16 17:22:28 +0200959 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +0100960 }
Robin Murphy2b9d9ac2018-12-10 14:00:29 +0000961 min_free_entries = num_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900962
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800963 dma_debug_initialized = true;
964
Robin Murphyf737b092018-12-10 14:00:27 +0000965 pr_info("debugging enabled by kernel config\n");
Christoph Hellwig15b28bb2018-04-16 17:22:28 +0200966 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +0100967}
Christoph Hellwig15b28bb2018-04-16 17:22:28 +0200968core_initcall(dma_debug_init);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100969
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100970static __init int dma_debug_cmdline(char *str)
971{
972 if (!str)
973 return -EINVAL;
974
975 if (strncmp(str, "off", 3) == 0) {
Robin Murphyf737b092018-12-10 14:00:27 +0000976 pr_info("debugging disabled on kernel command line\n");
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100977 global_disable = true;
978 }
979
980 return 0;
981}
982
983static __init int dma_debug_entries_cmdline(char *str)
984{
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100985 if (!str)
986 return -EINVAL;
Christoph Hellwigbcebe322018-04-24 09:40:51 +0200987 if (!get_option(&str, &nr_prealloc_entries))
988 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100989 return 0;
990}
991
992__setup("dma_debug=", dma_debug_cmdline);
993__setup("dma_debug_entries=", dma_debug_entries_cmdline);
994
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100995static void check_unmap(struct dma_debug_entry *ref)
996{
997 struct dma_debug_entry *entry;
998 struct hash_bucket *bucket;
999 unsigned long flags;
1000
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001001 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001002 entry = bucket_find_exact(bucket, ref);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001003
1004 if (!entry) {
Alexander Duyck8d640a52013-03-22 15:04:48 -07001005 /* must drop lock before calling dma_mapping_error */
Dan Carpenter50f579a2019-11-19 09:18:19 +03001006 put_hash_bucket(bucket, flags);
Alexander Duyck8d640a52013-03-22 15:04:48 -07001007
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001008 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1009 err_printk(ref->dev, NULL,
Robin Murphyf737b092018-12-10 14:00:27 +00001010 "device driver tries to free an "
Alexander Duyck8d640a52013-03-22 15:04:48 -07001011 "invalid DMA memory address\n");
1012 } else {
1013 err_printk(ref->dev, NULL,
Robin Murphyf737b092018-12-10 14:00:27 +00001014 "device driver tries to free DMA "
Alexander Duyck8d640a52013-03-22 15:04:48 -07001015 "memory it has not allocated [device "
1016 "address=0x%016llx] [size=%llu bytes]\n",
1017 ref->dev_addr, ref->size);
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001018 }
Alexander Duyck8d640a52013-03-22 15:04:48 -07001019 return;
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001020 }
1021
1022 if (ref->size != entry->size) {
Robin Murphyf737b092018-12-10 14:00:27 +00001023 err_printk(ref->dev, entry, "device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001024 "DMA memory with different size "
1025 "[device address=0x%016llx] [map size=%llu bytes] "
1026 "[unmap size=%llu bytes]\n",
1027 ref->dev_addr, entry->size, ref->size);
1028 }
1029
1030 if (ref->type != entry->type) {
Robin Murphyf737b092018-12-10 14:00:27 +00001031 err_printk(ref->dev, entry, "device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001032 "DMA memory with wrong function "
1033 "[device address=0x%016llx] [size=%llu bytes] "
1034 "[mapped as %s] [unmapped as %s]\n",
1035 ref->dev_addr, ref->size,
1036 type2name[entry->type], type2name[ref->type]);
1037 } else if ((entry->type == dma_debug_coherent) &&
Dan Williams0abdd7a2014-01-21 15:48:12 -08001038 (phys_addr(ref) != phys_addr(entry))) {
Robin Murphyf737b092018-12-10 14:00:27 +00001039 err_printk(ref->dev, entry, "device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001040 "DMA memory with different CPU address "
1041 "[device address=0x%016llx] [size=%llu bytes] "
Joerg Roedel59a40e702009-10-29 16:25:50 +01001042 "[cpu alloc address=0x%016llx] "
1043 "[cpu free address=0x%016llx]",
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001044 ref->dev_addr, ref->size,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001045 phys_addr(entry),
1046 phys_addr(ref));
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001047 }
1048
1049 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1050 ref->sg_call_ents != entry->sg_call_ents) {
Robin Murphyf737b092018-12-10 14:00:27 +00001051 err_printk(ref->dev, entry, "device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001052 "DMA sg list with different entry count "
1053 "[map count=%d] [unmap count=%d]\n",
1054 entry->sg_call_ents, ref->sg_call_ents);
1055 }
1056
1057 /*
1058 * This may be no bug in reality - but most implementations of the
1059 * DMA API don't handle this properly, so check for it here
1060 */
1061 if (ref->direction != entry->direction) {
Robin Murphyf737b092018-12-10 14:00:27 +00001062 err_printk(ref->dev, entry, "device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001063 "DMA memory with different direction "
1064 "[device address=0x%016llx] [size=%llu bytes] "
1065 "[mapped with %s] [unmapped with %s]\n",
1066 ref->dev_addr, ref->size,
1067 dir2name[entry->direction],
1068 dir2name[ref->direction]);
1069 }
1070
Miles Chena5759b22017-02-22 15:40:09 -08001071 /*
1072 * Drivers should use dma_mapping_error() to check the returned
1073 * addresses of dma_map_single() and dma_map_page().
1074 * If not, print this warning message. See Documentation/DMA-API.txt.
1075 */
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001076 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1077 err_printk(ref->dev, entry,
Robin Murphyf737b092018-12-10 14:00:27 +00001078 "device driver failed to check map error"
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001079 "[device address=0x%016llx] [size=%llu bytes] "
1080 "[mapped as %s]",
1081 ref->dev_addr, ref->size,
1082 type2name[entry->type]);
1083 }
1084
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001085 hash_bucket_del(entry);
1086 dma_entry_free(entry);
1087
Dan Carpenter50f579a2019-11-19 09:18:19 +03001088 put_hash_bucket(bucket, flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001089}
1090
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001091static void check_for_stack(struct device *dev,
1092 struct page *page, size_t offset)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001093{
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001094 void *addr;
1095 struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1096
1097 if (!stack_vm_area) {
1098 /* Stack is direct-mapped. */
1099 if (PageHighMem(page))
1100 return;
1101 addr = page_address(page) + offset;
1102 if (object_is_on_stack(addr))
Robin Murphyf737b092018-12-10 14:00:27 +00001103 err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001104 } else {
1105 /* Stack is vmalloced. */
1106 int i;
1107
1108 for (i = 0; i < stack_vm_area->nr_pages; i++) {
1109 if (page != stack_vm_area->pages[i])
1110 continue;
1111
1112 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
Robin Murphyf737b092018-12-10 14:00:27 +00001113 err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001114 break;
1115 }
1116 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001117}
1118
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001119static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001120{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001121 unsigned long a1 = (unsigned long)addr;
1122 unsigned long b1 = a1 + len;
1123 unsigned long a2 = (unsigned long)start;
1124 unsigned long b2 = (unsigned long)end;
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001125
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001126 return !(b1 <= a2 || a1 >= b2);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001127}
1128
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001129static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001130{
Laura Abbottea535e42016-01-14 15:16:50 -08001131 if (overlap(addr, len, _stext, _etext) ||
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001132 overlap(addr, len, __start_rodata, __end_rodata))
Robin Murphyf737b092018-12-10 14:00:27 +00001133 err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001134}
1135
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001136static void check_sync(struct device *dev,
1137 struct dma_debug_entry *ref,
1138 bool to_cpu)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001139{
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001140 struct dma_debug_entry *entry;
1141 struct hash_bucket *bucket;
1142 unsigned long flags;
1143
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001144 bucket = get_hash_bucket(ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001145
Neil Hormanc6a21d02011-08-08 15:13:54 -04001146 entry = bucket_find_contain(&bucket, ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001147
1148 if (!entry) {
Robin Murphyf737b092018-12-10 14:00:27 +00001149 err_printk(dev, NULL, "device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001150 "to sync DMA memory it has not allocated "
1151 "[device address=0x%016llx] [size=%llu bytes]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001152 (unsigned long long)ref->dev_addr, ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001153 goto out;
1154 }
1155
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001156 if (ref->size > entry->size) {
Robin Murphyf737b092018-12-10 14:00:27 +00001157 err_printk(dev, entry, "device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001158 " DMA memory outside allocated range "
1159 "[device address=0x%016llx] "
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001160 "[allocation size=%llu bytes] "
1161 "[sync offset+size=%llu]\n",
1162 entry->dev_addr, entry->size,
1163 ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001164 }
1165
Krzysztof Halasa42d53b42010-01-08 14:42:36 -08001166 if (entry->direction == DMA_BIDIRECTIONAL)
1167 goto out;
1168
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001169 if (ref->direction != entry->direction) {
Robin Murphyf737b092018-12-10 14:00:27 +00001170 err_printk(dev, entry, "device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001171 "DMA memory with different direction "
1172 "[device address=0x%016llx] [size=%llu bytes] "
1173 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001174 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001175 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001176 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001177 }
1178
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001179 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001180 !(ref->direction == DMA_TO_DEVICE))
Robin Murphyf737b092018-12-10 14:00:27 +00001181 err_printk(dev, entry, "device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001182 "device read-only DMA memory for cpu "
1183 "[device address=0x%016llx] [size=%llu bytes] "
1184 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001185 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001186 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001187 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001188
1189 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001190 !(ref->direction == DMA_FROM_DEVICE))
Robin Murphyf737b092018-12-10 14:00:27 +00001191 err_printk(dev, entry, "device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001192 "device write-only DMA memory to device "
1193 "[device address=0x%016llx] [size=%llu bytes] "
1194 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001195 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001196 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001197 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001198
Robin Murphy7f830642015-11-06 16:32:55 -08001199 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1200 ref->sg_call_ents != entry->sg_call_ents) {
Robin Murphyf737b092018-12-10 14:00:27 +00001201 err_printk(ref->dev, entry, "device driver syncs "
Robin Murphy7f830642015-11-06 16:32:55 -08001202 "DMA sg list with different entry count "
1203 "[map count=%d] [sync count=%d]\n",
1204 entry->sg_call_ents, ref->sg_call_ents);
1205 }
1206
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001207out:
Dan Carpenter50f579a2019-11-19 09:18:19 +03001208 put_hash_bucket(bucket, flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001209}
1210
Robin Murphy78c47832018-05-21 12:35:13 +01001211static void check_sg_segment(struct device *dev, struct scatterlist *sg)
1212{
1213#ifdef CONFIG_DMA_API_DEBUG_SG
1214 unsigned int max_seg = dma_get_max_seg_size(dev);
1215 u64 start, end, boundary = dma_get_seg_boundary(dev);
1216
1217 /*
1218 * Either the driver forgot to set dma_parms appropriately, or
1219 * whoever generated the list forgot to check them.
1220 */
1221 if (sg->length > max_seg)
Robin Murphyf737b092018-12-10 14:00:27 +00001222 err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
Robin Murphy78c47832018-05-21 12:35:13 +01001223 sg->length, max_seg);
1224 /*
1225 * In some cases this could potentially be the DMA API
1226 * implementation's fault, but it would usually imply that
1227 * the scatterlist was built inappropriately to begin with.
1228 */
1229 start = sg_dma_address(sg);
1230 end = start + sg_dma_len(sg) - 1;
1231 if ((start ^ end) & ~boundary)
Robin Murphyf737b092018-12-10 14:00:27 +00001232 err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
Robin Murphy78c47832018-05-21 12:35:13 +01001233 start, end, boundary);
1234#endif
1235}
1236
Stephen Boyd99c65fa2018-10-08 00:20:07 -07001237void debug_dma_map_single(struct device *dev, const void *addr,
1238 unsigned long len)
1239{
1240 if (unlikely(dma_debug_disabled()))
1241 return;
1242
1243 if (!virt_addr_valid(addr))
Robin Murphyf737b092018-12-10 14:00:27 +00001244 err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
Stephen Boyd99c65fa2018-10-08 00:20:07 -07001245 addr, len);
1246
1247 if (is_vmalloc_addr(addr))
Robin Murphyf737b092018-12-10 14:00:27 +00001248 err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
Stephen Boyd99c65fa2018-10-08 00:20:07 -07001249 addr, len);
1250}
1251EXPORT_SYMBOL(debug_dma_map_single);
1252
Joerg Roedelf62bc982009-01-09 14:14:49 +01001253void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
Christoph Hellwig2e05ea52018-12-25 08:50:35 +01001254 size_t size, int direction, dma_addr_t dma_addr)
Joerg Roedelf62bc982009-01-09 14:14:49 +01001255{
1256 struct dma_debug_entry *entry;
1257
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001258 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001259 return;
1260
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001261 if (dma_mapping_error(dev, dma_addr))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001262 return;
1263
1264 entry = dma_entry_alloc();
1265 if (!entry)
1266 return;
1267
1268 entry->dev = dev;
Christoph Hellwig2e05ea52018-12-25 08:50:35 +01001269 entry->type = dma_debug_single;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001270 entry->pfn = page_to_pfn(page);
1271 entry->offset = offset,
Joerg Roedelf62bc982009-01-09 14:14:49 +01001272 entry->dev_addr = dma_addr;
1273 entry->size = size;
1274 entry->direction = direction;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001275 entry->map_err_type = MAP_ERR_NOT_CHECKED;
Joerg Roedelf62bc982009-01-09 14:14:49 +01001276
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001277 check_for_stack(dev, page, offset);
1278
Joerg Roedel9537a482009-03-23 15:35:08 +01001279 if (!PageHighMem(page)) {
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001280 void *addr = page_address(page) + offset;
1281
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001282 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +01001283 }
1284
1285 add_dma_entry(entry);
1286}
1287EXPORT_SYMBOL(debug_dma_map_page);
1288
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001289void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1290{
1291 struct dma_debug_entry ref;
1292 struct dma_debug_entry *entry;
1293 struct hash_bucket *bucket;
1294 unsigned long flags;
1295
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001296 if (unlikely(dma_debug_disabled()))
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001297 return;
1298
1299 ref.dev = dev;
1300 ref.dev_addr = dma_addr;
1301 bucket = get_hash_bucket(&ref, &flags);
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001302
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001303 list_for_each_entry(entry, &bucket->list, list) {
1304 if (!exact_match(&ref, entry))
1305 continue;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001306
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001307 /*
1308 * The same physical address can be mapped multiple
1309 * times. Without a hardware IOMMU this results in the
1310 * same device addresses being put into the dma-debug
1311 * hash multiple times too. This can result in false
1312 * positives being reported. Therefore we implement a
1313 * best-fit algorithm here which updates the first entry
1314 * from the hash which fits the reference value and is
1315 * not currently listed as being checked.
1316 */
1317 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1318 entry->map_err_type = MAP_ERR_CHECKED;
1319 break;
1320 }
1321 }
1322
Dan Carpenter50f579a2019-11-19 09:18:19 +03001323 put_hash_bucket(bucket, flags);
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001324}
1325EXPORT_SYMBOL(debug_dma_mapping_error);
1326
Joerg Roedelf62bc982009-01-09 14:14:49 +01001327void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
Christoph Hellwig2e05ea52018-12-25 08:50:35 +01001328 size_t size, int direction)
Joerg Roedelf62bc982009-01-09 14:14:49 +01001329{
1330 struct dma_debug_entry ref = {
Christoph Hellwig2e05ea52018-12-25 08:50:35 +01001331 .type = dma_debug_single,
Joerg Roedelf62bc982009-01-09 14:14:49 +01001332 .dev = dev,
1333 .dev_addr = addr,
1334 .size = size,
1335 .direction = direction,
1336 };
1337
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001338 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001339 return;
Joerg Roedelf62bc982009-01-09 14:14:49 +01001340 check_unmap(&ref);
1341}
1342EXPORT_SYMBOL(debug_dma_unmap_page);
1343
Joerg Roedel972aa452009-01-09 14:19:54 +01001344void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1345 int nents, int mapped_ents, int direction)
1346{
1347 struct dma_debug_entry *entry;
1348 struct scatterlist *s;
1349 int i;
1350
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001351 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001352 return;
1353
1354 for_each_sg(sg, s, mapped_ents, i) {
1355 entry = dma_entry_alloc();
1356 if (!entry)
1357 return;
1358
1359 entry->type = dma_debug_sg;
1360 entry->dev = dev;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001361 entry->pfn = page_to_pfn(sg_page(s));
1362 entry->offset = s->offset,
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001363 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001364 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001365 entry->direction = direction;
1366 entry->sg_call_ents = nents;
1367 entry->sg_mapped_ents = mapped_ents;
1368
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001369 check_for_stack(dev, sg_page(s), s->offset);
1370
Joerg Roedel9537a482009-03-23 15:35:08 +01001371 if (!PageHighMem(sg_page(s))) {
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001372 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001373 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001374
Robin Murphy78c47832018-05-21 12:35:13 +01001375 check_sg_segment(dev, s);
1376
Joerg Roedel972aa452009-01-09 14:19:54 +01001377 add_dma_entry(entry);
1378 }
1379}
1380EXPORT_SYMBOL(debug_dma_map_sg);
1381
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001382static int get_nr_mapped_entries(struct device *dev,
1383 struct dma_debug_entry *ref)
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001384{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001385 struct dma_debug_entry *entry;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001386 struct hash_bucket *bucket;
1387 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001388 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001389
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001390 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001391 entry = bucket_find_exact(bucket, ref);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001392 mapped_ents = 0;
1393
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001394 if (entry)
1395 mapped_ents = entry->sg_mapped_ents;
Dan Carpenter50f579a2019-11-19 09:18:19 +03001396 put_hash_bucket(bucket, flags);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001397
1398 return mapped_ents;
1399}
1400
Joerg Roedel972aa452009-01-09 14:19:54 +01001401void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1402 int nelems, int dir)
1403{
Joerg Roedel972aa452009-01-09 14:19:54 +01001404 struct scatterlist *s;
1405 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001406
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001407 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001408 return;
1409
1410 for_each_sg(sglist, s, nelems, i) {
1411
1412 struct dma_debug_entry ref = {
1413 .type = dma_debug_sg,
1414 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001415 .pfn = page_to_pfn(sg_page(s)),
1416 .offset = s->offset,
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001417 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001418 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001419 .direction = dir,
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001420 .sg_call_ents = nelems,
Joerg Roedel972aa452009-01-09 14:19:54 +01001421 };
1422
1423 if (mapped_ents && i >= mapped_ents)
1424 break;
1425
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001426 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001427 mapped_ents = get_nr_mapped_entries(dev, &ref);
Joerg Roedel972aa452009-01-09 14:19:54 +01001428
1429 check_unmap(&ref);
1430 }
1431}
1432EXPORT_SYMBOL(debug_dma_unmap_sg);
1433
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001434void debug_dma_alloc_coherent(struct device *dev, size_t size,
1435 dma_addr_t dma_addr, void *virt)
1436{
1437 struct dma_debug_entry *entry;
1438
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001439 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001440 return;
1441
1442 if (unlikely(virt == NULL))
1443 return;
1444
Miles Chenaf1da682018-02-22 19:22:20 +08001445 /* handle vmalloc and linear addresses */
1446 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001447 return;
1448
Miles Chenaf1da682018-02-22 19:22:20 +08001449 entry = dma_entry_alloc();
1450 if (!entry)
Miles Chen3aaabbf2017-11-17 15:26:19 -08001451 return;
1452
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001453 entry->type = dma_debug_coherent;
1454 entry->dev = dev;
Geliang Tange57d0552017-04-22 09:18:05 +08001455 entry->offset = offset_in_page(virt);
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001456 entry->size = size;
1457 entry->dev_addr = dma_addr;
1458 entry->direction = DMA_BIDIRECTIONAL;
1459
Miles Chen3aaabbf2017-11-17 15:26:19 -08001460 if (is_vmalloc_addr(virt))
1461 entry->pfn = vmalloc_to_pfn(virt);
1462 else
1463 entry->pfn = page_to_pfn(virt_to_page(virt));
1464
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001465 add_dma_entry(entry);
1466}
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001467
1468void debug_dma_free_coherent(struct device *dev, size_t size,
1469 void *virt, dma_addr_t addr)
1470{
1471 struct dma_debug_entry ref = {
1472 .type = dma_debug_coherent,
1473 .dev = dev,
Geliang Tange57d0552017-04-22 09:18:05 +08001474 .offset = offset_in_page(virt),
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001475 .dev_addr = addr,
1476 .size = size,
1477 .direction = DMA_BIDIRECTIONAL,
1478 };
1479
Miles Chen3aaabbf2017-11-17 15:26:19 -08001480 /* handle vmalloc and linear addresses */
Miles Chenaf1da682018-02-22 19:22:20 +08001481 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
Miles Chen3aaabbf2017-11-17 15:26:19 -08001482 return;
1483
1484 if (is_vmalloc_addr(virt))
1485 ref.pfn = vmalloc_to_pfn(virt);
1486 else
1487 ref.pfn = page_to_pfn(virt_to_page(virt));
1488
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001489 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001490 return;
1491
1492 check_unmap(&ref);
1493}
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001494
Niklas Söderlund0e74b342016-08-10 13:22:15 +02001495void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1496 int direction, dma_addr_t dma_addr)
1497{
1498 struct dma_debug_entry *entry;
1499
1500 if (unlikely(dma_debug_disabled()))
1501 return;
1502
1503 entry = dma_entry_alloc();
1504 if (!entry)
1505 return;
1506
1507 entry->type = dma_debug_resource;
1508 entry->dev = dev;
Niklas Söderlund2e0cc302016-09-29 21:59:15 +02001509 entry->pfn = PHYS_PFN(addr);
Niklas Söderlund0e74b342016-08-10 13:22:15 +02001510 entry->offset = offset_in_page(addr);
1511 entry->size = size;
1512 entry->dev_addr = dma_addr;
1513 entry->direction = direction;
1514 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1515
1516 add_dma_entry(entry);
1517}
1518EXPORT_SYMBOL(debug_dma_map_resource);
1519
1520void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1521 size_t size, int direction)
1522{
1523 struct dma_debug_entry ref = {
1524 .type = dma_debug_resource,
1525 .dev = dev,
1526 .dev_addr = dma_addr,
1527 .size = size,
1528 .direction = direction,
1529 };
1530
1531 if (unlikely(dma_debug_disabled()))
1532 return;
1533
1534 check_unmap(&ref);
1535}
1536EXPORT_SYMBOL(debug_dma_unmap_resource);
1537
Joerg Roedelb9d23172009-01-09 14:43:04 +01001538void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1539 size_t size, int direction)
1540{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001541 struct dma_debug_entry ref;
1542
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001543 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001544 return;
1545
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001546 ref.type = dma_debug_single;
1547 ref.dev = dev;
1548 ref.dev_addr = dma_handle;
1549 ref.size = size;
1550 ref.direction = direction;
1551 ref.sg_call_ents = 0;
1552
1553 check_sync(dev, &ref, true);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001554}
1555EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1556
1557void debug_dma_sync_single_for_device(struct device *dev,
1558 dma_addr_t dma_handle, size_t size,
1559 int direction)
1560{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001561 struct dma_debug_entry ref;
1562
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001563 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001564 return;
1565
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001566 ref.type = dma_debug_single;
1567 ref.dev = dev;
1568 ref.dev_addr = dma_handle;
1569 ref.size = size;
1570 ref.direction = direction;
1571 ref.sg_call_ents = 0;
1572
1573 check_sync(dev, &ref, false);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001574}
1575EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1576
Joerg Roedela31fba52009-01-09 15:01:12 +01001577void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1578 int nelems, int direction)
1579{
1580 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001581 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001582
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001583 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001584 return;
1585
1586 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001587
1588 struct dma_debug_entry ref = {
1589 .type = dma_debug_sg,
1590 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001591 .pfn = page_to_pfn(sg_page(s)),
1592 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001593 .dev_addr = sg_dma_address(s),
1594 .size = sg_dma_len(s),
1595 .direction = direction,
1596 .sg_call_ents = nelems,
1597 };
1598
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001599 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001600 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001601
1602 if (i >= mapped_ents)
1603 break;
1604
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001605 check_sync(dev, &ref, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001606 }
1607}
1608EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1609
1610void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1611 int nelems, int direction)
1612{
1613 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001614 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001615
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001616 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001617 return;
1618
1619 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001620
1621 struct dma_debug_entry ref = {
1622 .type = dma_debug_sg,
1623 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001624 .pfn = page_to_pfn(sg_page(s)),
1625 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001626 .dev_addr = sg_dma_address(s),
1627 .size = sg_dma_len(s),
1628 .direction = direction,
1629 .sg_call_ents = nelems,
1630 };
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001631 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001632 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001633
1634 if (i >= mapped_ents)
1635 break;
1636
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001637 check_sync(dev, &ref, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001638 }
1639}
1640EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1641
Joerg Roedel1745de52009-05-22 21:49:51 +02001642static int __init dma_debug_driver_setup(char *str)
1643{
1644 int i;
1645
1646 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1647 current_driver_name[i] = *str;
1648 if (*str == 0)
1649 break;
1650 }
1651
1652 if (current_driver_name[0])
Robin Murphyf737b092018-12-10 14:00:27 +00001653 pr_info("enable driver filter for driver [%s]\n",
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001654 current_driver_name);
Joerg Roedel1745de52009-05-22 21:49:51 +02001655
1656
1657 return 1;
1658}
1659__setup("dma_debug_driver=", dma_debug_driver_setup);