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