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