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