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