Merge branch 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6.git] / lib / dma-debug.c
index d0618aa..db07bfd 100644 (file)
@@ -117,6 +117,11 @@ static const char *type2name[4] = { "single", "page",
 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
                                   "DMA_FROM_DEVICE", "DMA_NONE" };
 
+/* little merge helper - remove it after the merge window */
+#ifndef BUS_NOTIFY_UNBOUND_DRIVER
+#define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
+#endif
+
 /*
  * The access to some variables in this macro is racy. We can't use atomic_t
  * here because all these variables are exported to debugfs. Some of them even
@@ -134,7 +139,7 @@ static inline void dump_entry_trace(struct dma_debug_entry *entry)
 {
 #ifdef CONFIG_STACKTRACE
        if (entry) {
-               printk(KERN_WARNING "Mapped at:\n");
+               pr_warning("Mapped at:\n");
                print_stack_trace(&entry->stacktrace, 0);
        }
 #endif
@@ -142,53 +147,57 @@ static inline void dump_entry_trace(struct dma_debug_entry *entry)
 
 static bool driver_filter(struct device *dev)
 {
+       struct device_driver *drv;
+       unsigned long flags;
+       bool ret;
+
        /* driver filter off */
        if (likely(!current_driver_name[0]))
                return true;
 
        /* driver filter on and initialized */
-       if (current_driver && dev->driver == current_driver)
+       if (current_driver && dev && dev->driver == current_driver)
                return true;
 
-       /* driver filter on but not yet initialized */
-       if (!current_driver && current_driver_name[0]) {
-               struct device_driver *drv = get_driver(dev->driver);
-               unsigned long flags;
-               bool ret = false;
+       /* driver filter on, but we can't filter on a NULL device... */
+       if (!dev)
+               return false;
 
-               if (!drv)
-                       return false;
+       if (current_driver || !current_driver_name[0])
+               return false;
 
-               /* lock to protect against change of current_driver_name */
-               read_lock_irqsave(&driver_name_lock, flags);
-
-               if (drv->name &&
-                   strncmp(current_driver_name, drv->name,
-                           NAME_MAX_LEN-1) == 0) {
-                       current_driver = drv;
-                       ret = true;
-               }
+       /* driver filter on but not yet initialized */
+       drv = get_driver(dev->driver);
+       if (!drv)
+               return false;
 
-               read_unlock_irqrestore(&driver_name_lock, flags);
-               put_driver(drv);
+       /* lock to protect against change of current_driver_name */
+       read_lock_irqsave(&driver_name_lock, flags);
 
-               return ret;
+       ret = false;
+       if (drv->name &&
+           strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
+               current_driver = drv;
+               ret = true;
        }
 
-       return false;
+       read_unlock_irqrestore(&driver_name_lock, flags);
+       put_driver(drv);
+
+       return ret;
 }
 
-#define err_printk(dev, entry, format, arg...) do {            \
-               error_count += 1;                               \
-               if (driver_filter(dev) &&                       \
-                   (show_all_errors || show_num_errors > 0)) { \
-                       WARN(1, "%s %s: " format,               \
-                            dev_driver_string(dev),            \
-                            dev_name(dev) , ## arg);           \
-                       dump_entry_trace(entry);                \
-               }                                               \
-               if (!show_all_errors && show_num_errors > 0)    \
-                       show_num_errors -= 1;                   \
+#define err_printk(dev, entry, format, arg...) do {                    \
+               error_count += 1;                                       \
+               if (driver_filter(dev) &&                               \
+                   (show_all_errors || show_num_errors > 0)) {         \
+                       WARN(1, "%s %s: " format,                       \
+                            dev ? dev_driver_string(dev) : "NULL",     \
+                            dev ? dev_name(dev) : "NULL", ## arg);     \
+                       dump_entry_trace(entry);                        \
+               }                                                       \
+               if (!show_all_errors && show_num_errors > 0)            \
+                       show_num_errors -= 1;                           \
        } while (0);
 
 /*
@@ -237,15 +246,51 @@ static void put_hash_bucket(struct hash_bucket *bucket,
 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
                                                struct dma_debug_entry *ref)
 {
-       struct dma_debug_entry *entry;
+       struct dma_debug_entry *entry, *ret = NULL;
+       int matches = 0, match_lvl, last_lvl = 0;
 
        list_for_each_entry(entry, &bucket->list, list) {
-               if ((entry->dev_addr == ref->dev_addr) &&
-                   (entry->dev == ref->dev))
+               if ((entry->dev_addr != ref->dev_addr) ||
+                   (entry->dev != ref->dev))
+                       continue;
+
+               /*
+                * Some drivers map the same physical address multiple
+                * times. Without a hardware IOMMU this results in the
+                * same device addresses being put into the dma-debug
+                * hash multiple times too. This can result in false
+                * positives being reported. Therefore we implement a
+                * best-fit algorithm here which returns the entry from
+                * the hash which fits best to the reference value
+                * instead of the first-fit.
+                */
+               matches += 1;
+               match_lvl = 0;
+               entry->size         == ref->size         ? ++match_lvl : 0;
+               entry->type         == ref->type         ? ++match_lvl : 0;
+               entry->direction    == ref->direction    ? ++match_lvl : 0;
+               entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
+
+               if (match_lvl == 4) {
+                       /* perfect-fit - return the result */
                        return entry;
+               } else if (match_lvl > last_lvl) {
+                       /*
+                        * We found an entry that fits better then the
+                        * previous one
+                        */
+                       last_lvl = match_lvl;
+                       ret      = entry;
+               }
        }
 
-       return NULL;
+       /*
+        * If we have multiple matches but no perfect-fit, just return
+        * NULL.
+        */
+       ret = (matches == 1) ? ret : NULL;
+
+       return ret;
 }
 
 /*
@@ -337,8 +382,7 @@ static struct dma_debug_entry *dma_entry_alloc(void)
        spin_lock_irqsave(&free_entries_lock, flags);
 
        if (list_empty(&free_entries)) {
-               printk(KERN_ERR "DMA-API: debugging out of memory "
-                               "- disabling\n");
+               pr_err("DMA-API: debugging out of memory - disabling\n");
                global_disable = true;
                goto out;
        }
@@ -443,8 +487,7 @@ static int prealloc_memory(u32 num_entries)
        num_free_entries = num_entries;
        min_free_entries = num_entries;
 
-       printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
-                       num_entries);
+       pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
 
        return 0;
 
@@ -461,8 +504,8 @@ out_err:
 static ssize_t filter_read(struct file *file, char __user *user_buf,
                           size_t count, loff_t *ppos)
 {
-       unsigned long flags;
        char buf[NAME_MAX_LEN + 1];
+       unsigned long flags;
        int len;
 
        if (!current_driver_name[0])
@@ -483,9 +526,9 @@ static ssize_t filter_read(struct file *file, char __user *user_buf,
 static ssize_t filter_write(struct file *file, const char __user *userbuf,
                            size_t count, loff_t *ppos)
 {
-       unsigned long flags;
        char buf[NAME_MAX_LEN];
-       size_t len = NAME_MAX_LEN - 1;
+       unsigned long flags;
+       size_t len;
        int i;
 
        /*
@@ -494,7 +537,7 @@ static ssize_t filter_write(struct file *file, const char __user *userbuf,
         * disabled. Since copy_from_user can fault and may sleep we
         * need to copy to temporary buffer first
         */
-       len = min(count, len);
+       len = min(count, (size_t)(NAME_MAX_LEN - 1));
        if (copy_from_user(buf, userbuf, len))
                return -EFAULT;
 
@@ -502,7 +545,8 @@ static ssize_t filter_write(struct file *file, const char __user *userbuf,
 
        write_lock_irqsave(&driver_name_lock, flags);
 
-       /* Now handle the string we got from userspace very carefully.
+       /*
+        * Now handle the string we got from userspace very carefully.
         * The rules are:
         *         - only use the first token we got
         *         - token delimiter is everything looking like a space
@@ -511,13 +555,12 @@ static ssize_t filter_write(struct file *file, const char __user *userbuf,
         */
        if (!isalnum(buf[0])) {
                /*
-                  If the first character userspace gave us is not
+                * If the first character userspace gave us is not
                 * alphanumerical then assume the filter should be
                 * switched off.
                 */
                if (current_driver_name[0])
-                       printk(KERN_INFO "DMA-API: switching off dma-debug "
-                                        "driver filter\n");
+                       pr_info("DMA-API: switching off dma-debug driver filter\n");
                current_driver_name[0] = 0;
                current_driver = NULL;
                goto out_unlock;
@@ -527,7 +570,7 @@ static ssize_t filter_write(struct file *file, const char __user *userbuf,
         * Now parse out the first token and use it as the name for the
         * driver to filter for.
         */
-       for (i = 0; i < NAME_MAX_LEN; ++i) {
+       for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
                current_driver_name[i] = buf[i];
                if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
                        break;
@@ -535,8 +578,8 @@ static ssize_t filter_write(struct file *file, const char __user *userbuf,
        current_driver_name[i] = 0;
        current_driver = NULL;
 
-       printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n",
-              current_driver_name);
+       pr_info("DMA-API: enable driver filter for driver [%s]\n",
+               current_driver_name);
 
 out_unlock:
        write_unlock_irqrestore(&driver_name_lock, flags);
@@ -544,16 +587,17 @@ out_unlock:
        return count;
 }
 
-const struct file_operations filter_fops = {
+static const struct file_operations filter_fops = {
        .read  = filter_read,
        .write = filter_write,
+       .llseek = default_llseek,
 };
 
 static int dma_debug_fs_init(void)
 {
        dma_debug_dent = debugfs_create_dir("dma-api", NULL);
        if (!dma_debug_dent) {
-               printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
+               pr_err("DMA-API: can not create debugfs directory\n");
                return -ENOMEM;
        }
 
@@ -605,9 +649,76 @@ out_err:
        return -ENOMEM;
 }
 
+static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
+{
+       struct dma_debug_entry *entry;
+       unsigned long flags;
+       int count = 0, i;
+
+       local_irq_save(flags);
+
+       for (i = 0; i < HASH_SIZE; ++i) {
+               spin_lock(&dma_entry_hash[i].lock);
+               list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
+                       if (entry->dev == dev) {
+                               count += 1;
+                               *out_entry = entry;
+                       }
+               }
+               spin_unlock(&dma_entry_hash[i].lock);
+       }
+
+       local_irq_restore(flags);
+
+       return count;
+}
+
+static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
+{
+       struct device *dev = data;
+       struct dma_debug_entry *uninitialized_var(entry);
+       int count;
+
+       if (global_disable)
+               return 0;
+
+       switch (action) {
+       case BUS_NOTIFY_UNBOUND_DRIVER:
+               count = device_dma_allocations(dev, &entry);
+               if (count == 0)
+                       break;
+               err_printk(dev, entry, "DMA-API: device driver has pending "
+                               "DMA allocations while released from device "
+                               "[count=%d]\n"
+                               "One of leaked entries details: "
+                               "[device address=0x%016llx] [size=%llu bytes] "
+                               "[mapped with %s] [mapped as %s]\n",
+                       count, entry->dev_addr, entry->size,
+                       dir2name[entry->direction], type2name[entry->type]);
+               break;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
 void dma_debug_add_bus(struct bus_type *bus)
 {
-       /* FIXME: register notifier */
+       struct notifier_block *nb;
+
+       if (global_disable)
+               return;
+
+       nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
+       if (nb == NULL) {
+               pr_err("dma_debug_add_bus: out of memory\n");
+               return;
+       }
+
+       nb->notifier_call = dma_debug_device_change;
+
+       bus_register_notifier(bus, nb);
 }
 
 /*
@@ -622,12 +733,11 @@ void dma_debug_init(u32 num_entries)
 
        for (i = 0; i < HASH_SIZE; ++i) {
                INIT_LIST_HEAD(&dma_entry_hash[i].list);
-               dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
+               spin_lock_init(&dma_entry_hash[i].lock);
        }
 
        if (dma_debug_fs_init() != 0) {
-               printk(KERN_ERR "DMA-API: error creating debugfs entries "
-                               "- disabling\n");
+               pr_err("DMA-API: error creating debugfs entries - disabling\n");
                global_disable = true;
 
                return;
@@ -637,8 +747,7 @@ void dma_debug_init(u32 num_entries)
                num_entries = req_entries;
 
        if (prealloc_memory(num_entries) != 0) {
-               printk(KERN_ERR "DMA-API: debugging out of memory error "
-                               "- disabled\n");
+               pr_err("DMA-API: debugging out of memory error - disabled\n");
                global_disable = true;
 
                return;
@@ -646,7 +755,7 @@ void dma_debug_init(u32 num_entries)
 
        nr_total_entries = num_free_entries;
 
-       printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
+       pr_info("DMA-API: debugging enabled by kernel config\n");
 }
 
 static __init int dma_debug_cmdline(char *str)
@@ -655,8 +764,7 @@ static __init int dma_debug_cmdline(char *str)
                return -EINVAL;
 
        if (strncmp(str, "off", 3) == 0) {
-               printk(KERN_INFO "DMA-API: debugging disabled on kernel "
-                                "command line\n");
+               pr_info("DMA-API: debugging disabled on kernel command line\n");
                global_disable = true;
        }
 
@@ -724,9 +832,11 @@ static void check_unmap(struct dma_debug_entry *ref)
                err_printk(ref->dev, entry, "DMA-API: device driver frees "
                           "DMA memory with different CPU address "
                           "[device address=0x%016llx] [size=%llu bytes] "
-                          "[cpu alloc address=%p] [cpu free address=%p]",
+                          "[cpu alloc address=0x%016llx] "
+                          "[cpu free address=0x%016llx]",
                           ref->dev_addr, ref->size,
-                          (void *)entry->paddr, (void *)ref->paddr);
+                          (unsigned long long)entry->paddr,
+                          (unsigned long long)ref->paddr);
        }
 
        if (ref->sg_call_ents && ref->type == dma_debug_sg &&
@@ -765,94 +875,88 @@ static void check_for_stack(struct device *dev, void *addr)
                                "stack [addr=%p]\n", addr);
 }
 
-static inline bool overlap(void *addr, u64 size, void *start, void *end)
+static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
 {
-       void *addr2 = (char *)addr + size;
+       unsigned long a1 = (unsigned long)addr;
+       unsigned long b1 = a1 + len;
+       unsigned long a2 = (unsigned long)start;
+       unsigned long b2 = (unsigned long)end;
 
-       return ((addr >= start && addr < end) ||
-               (addr2 >= start && addr2 < end) ||
-               ((addr < start) && (addr2 >= end)));
+       return !(b1 <= a2 || a1 >= b2);
 }
 
-static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
+static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
 {
-       if (overlap(addr, size, _text, _etext) ||
-           overlap(addr, size, __start_rodata, __end_rodata))
-               err_printk(dev, NULL, "DMA-API: device driver maps "
-                               "memory from kernel text or rodata "
-                               "[addr=%p] [size=%llu]\n", addr, size);
+       if (overlap(addr, len, _text, _etext) ||
+           overlap(addr, len, __start_rodata, __end_rodata))
+               err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
 }
 
-static void check_sync(struct device *dev, dma_addr_t addr,
-                      u64 size, u64 offset, int direction, bool to_cpu)
+static void check_sync(struct device *dev,
+                      struct dma_debug_entry *ref,
+                      bool to_cpu)
 {
-       struct dma_debug_entry ref = {
-               .dev            = dev,
-               .dev_addr       = addr,
-               .size           = size,
-               .direction      = direction,
-       };
        struct dma_debug_entry *entry;
        struct hash_bucket *bucket;
        unsigned long flags;
 
-       bucket = get_hash_bucket(&ref, &flags);
+       bucket = get_hash_bucket(ref, &flags);
 
-       entry = hash_bucket_find(bucket, &ref);
+       entry = hash_bucket_find(bucket, ref);
 
        if (!entry) {
                err_printk(dev, NULL, "DMA-API: device driver tries "
                                "to sync DMA memory it has not allocated "
                                "[device address=0x%016llx] [size=%llu bytes]\n",
-                               (unsigned long long)addr, size);
+                               (unsigned long long)ref->dev_addr, ref->size);
                goto out;
        }
 
-       if ((offset + size) > entry->size) {
+       if (ref->size > entry->size) {
                err_printk(dev, entry, "DMA-API: device driver syncs"
                                " DMA memory outside allocated range "
                                "[device address=0x%016llx] "
-                               "[allocation size=%llu bytes] [sync offset=%llu] "
-                               "[sync size=%llu]\n", entry->dev_addr, entry->size,
-                               offset, size);
+                               "[allocation size=%llu bytes] "
+                               "[sync offset+size=%llu]\n",
+                               entry->dev_addr, entry->size,
+                               ref->size);
        }
 
-       if (direction != entry->direction) {
+       if (entry->direction == DMA_BIDIRECTIONAL)
+               goto out;
+
+       if (ref->direction != entry->direction) {
                err_printk(dev, entry, "DMA-API: device driver syncs "
                                "DMA memory with different direction "
                                "[device address=0x%016llx] [size=%llu bytes] "
                                "[mapped with %s] [synced with %s]\n",
-                               (unsigned long long)addr, entry->size,
+                               (unsigned long long)ref->dev_addr, entry->size,
                                dir2name[entry->direction],
-                               dir2name[direction]);
+                               dir2name[ref->direction]);
        }
 
-       if (entry->direction == DMA_BIDIRECTIONAL)
-               goto out;
-
        if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
-                     !(direction == DMA_TO_DEVICE))
+                     !(ref->direction == DMA_TO_DEVICE))
                err_printk(dev, entry, "DMA-API: device driver syncs "
                                "device read-only DMA memory for cpu "
                                "[device address=0x%016llx] [size=%llu bytes] "
                                "[mapped with %s] [synced with %s]\n",
-                               (unsigned long long)addr, entry->size,
+                               (unsigned long long)ref->dev_addr, entry->size,
                                dir2name[entry->direction],
-                               dir2name[direction]);
+                               dir2name[ref->direction]);
 
        if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
-                      !(direction == DMA_FROM_DEVICE))
+                      !(ref->direction == DMA_FROM_DEVICE))
                err_printk(dev, entry, "DMA-API: device driver syncs "
                                "device write-only DMA memory to device "
                                "[device address=0x%016llx] [size=%llu bytes] "
                                "[mapped with %s] [synced with %s]\n",
-                               (unsigned long long)addr, entry->size,
+                               (unsigned long long)ref->dev_addr, entry->size,
                                dir2name[entry->direction],
-                               dir2name[direction]);
+                               dir2name[ref->direction]);
 
 out:
        put_hash_bucket(bucket, &flags);
-
 }
 
 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
@@ -882,7 +986,8 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
                entry->type = dma_debug_single;
 
        if (!PageHighMem(page)) {
-               void *addr = ((char *)page_address(page)) + offset;
+               void *addr = page_address(page) + offset;
+
                check_for_stack(dev, addr);
                check_for_illegal_area(dev, addr, size);
        }
@@ -930,15 +1035,15 @@ void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
                entry->type           = dma_debug_sg;
                entry->dev            = dev;
                entry->paddr          = sg_phys(s);
-               entry->size           = s->length;
-               entry->dev_addr       = s->dma_address;
+               entry->size           = sg_dma_len(s);
+               entry->dev_addr       = sg_dma_address(s);
                entry->direction      = direction;
                entry->sg_call_ents   = nents;
                entry->sg_mapped_ents = mapped_ents;
 
                if (!PageHighMem(sg_page(s))) {
                        check_for_stack(dev, sg_virt(s));
-                       check_for_illegal_area(dev, sg_virt(s), s->length);
+                       check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
                }
 
                add_dma_entry(entry);
@@ -946,13 +1051,30 @@ void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
 }
 EXPORT_SYMBOL(debug_dma_map_sg);
 
+static int get_nr_mapped_entries(struct device *dev,
+                                struct dma_debug_entry *ref)
+{
+       struct dma_debug_entry *entry;
+       struct hash_bucket *bucket;
+       unsigned long flags;
+       int mapped_ents;
+
+       bucket       = get_hash_bucket(ref, &flags);
+       entry        = hash_bucket_find(bucket, ref);
+       mapped_ents  = 0;
+
+       if (entry)
+               mapped_ents = entry->sg_mapped_ents;
+       put_hash_bucket(bucket, &flags);
+
+       return mapped_ents;
+}
+
 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
                        int nelems, int dir)
 {
-       struct dma_debug_entry *entry;
        struct scatterlist *s;
        int mapped_ents = 0, i;
-       unsigned long flags;
 
        if (unlikely(global_disable))
                return;
@@ -963,24 +1085,17 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
                        .type           = dma_debug_sg,
                        .dev            = dev,
                        .paddr          = sg_phys(s),
-                       .dev_addr       = s->dma_address,
-                       .size           = s->length,
+                       .dev_addr       = sg_dma_address(s),
+                       .size           = sg_dma_len(s),
                        .direction      = dir,
-                       .sg_call_ents   = 0,
+                       .sg_call_ents   = nelems,
                };
 
                if (mapped_ents && i >= mapped_ents)
                        break;
 
-               if (mapped_ents == 0) {
-                       struct hash_bucket *bucket;
-                       ref.sg_call_ents = nelems;
-                       bucket = get_hash_bucket(&ref, &flags);
-                       entry = hash_bucket_find(bucket, &ref);
-                       if (entry)
-                               mapped_ents = entry->sg_mapped_ents;
-                       put_hash_bucket(bucket, &flags);
-               }
+               if (!i)
+                       mapped_ents = get_nr_mapped_entries(dev, &ref);
 
                check_unmap(&ref);
        }
@@ -1035,10 +1150,19 @@ EXPORT_SYMBOL(debug_dma_free_coherent);
 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
                                   size_t size, int direction)
 {
+       struct dma_debug_entry ref;
+
        if (unlikely(global_disable))
                return;
 
-       check_sync(dev, dma_handle, size, 0, direction, true);
+       ref.type         = dma_debug_single;
+       ref.dev          = dev;
+       ref.dev_addr     = dma_handle;
+       ref.size         = size;
+       ref.direction    = direction;
+       ref.sg_call_ents = 0;
+
+       check_sync(dev, &ref, true);
 }
 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
 
@@ -1046,10 +1170,19 @@ void debug_dma_sync_single_for_device(struct device *dev,
                                      dma_addr_t dma_handle, size_t size,
                                      int direction)
 {
+       struct dma_debug_entry ref;
+
        if (unlikely(global_disable))
                return;
 
-       check_sync(dev, dma_handle, size, 0, direction, false);
+       ref.type         = dma_debug_single;
+       ref.dev          = dev;
+       ref.dev_addr     = dma_handle;
+       ref.size         = size;
+       ref.direction    = direction;
+       ref.sg_call_ents = 0;
+
+       check_sync(dev, &ref, false);
 }
 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
 
@@ -1058,10 +1191,19 @@ void debug_dma_sync_single_range_for_cpu(struct device *dev,
                                         unsigned long offset, size_t size,
                                         int direction)
 {
+       struct dma_debug_entry ref;
+
        if (unlikely(global_disable))
                return;
 
-       check_sync(dev, dma_handle, size, offset, direction, true);
+       ref.type         = dma_debug_single;
+       ref.dev          = dev;
+       ref.dev_addr     = dma_handle;
+       ref.size         = offset + size;
+       ref.direction    = direction;
+       ref.sg_call_ents = 0;
+
+       check_sync(dev, &ref, true);
 }
 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
 
@@ -1070,10 +1212,19 @@ void debug_dma_sync_single_range_for_device(struct device *dev,
                                            unsigned long offset,
                                            size_t size, int direction)
 {
+       struct dma_debug_entry ref;
+
        if (unlikely(global_disable))
                return;
 
-       check_sync(dev, dma_handle, size, offset, direction, false);
+       ref.type         = dma_debug_single;
+       ref.dev          = dev;
+       ref.dev_addr     = dma_handle;
+       ref.size         = offset + size;
+       ref.direction    = direction;
+       ref.sg_call_ents = 0;
+
+       check_sync(dev, &ref, false);
 }
 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
 
@@ -1081,14 +1232,30 @@ void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
                               int nelems, int direction)
 {
        struct scatterlist *s;
-       int i;
+       int mapped_ents = 0, i;
 
        if (unlikely(global_disable))
                return;
 
        for_each_sg(sg, s, nelems, i) {
-               check_sync(dev, s->dma_address, s->dma_length, 0,
-                               direction, true);
+
+               struct dma_debug_entry ref = {
+                       .type           = dma_debug_sg,
+                       .dev            = dev,
+                       .paddr          = sg_phys(s),
+                       .dev_addr       = sg_dma_address(s),
+                       .size           = sg_dma_len(s),
+                       .direction      = direction,
+                       .sg_call_ents   = nelems,
+               };
+
+               if (!i)
+                       mapped_ents = get_nr_mapped_entries(dev, &ref);
+
+               if (i >= mapped_ents)
+                       break;
+
+               check_sync(dev, &ref, true);
        }
 }
 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
@@ -1097,14 +1264,29 @@ void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
                                  int nelems, int direction)
 {
        struct scatterlist *s;
-       int i;
+       int mapped_ents = 0, i;
 
        if (unlikely(global_disable))
                return;
 
        for_each_sg(sg, s, nelems, i) {
-               check_sync(dev, s->dma_address, s->dma_length, 0,
-                               direction, false);
+
+               struct dma_debug_entry ref = {
+                       .type           = dma_debug_sg,
+                       .dev            = dev,
+                       .paddr          = sg_phys(s),
+                       .dev_addr       = sg_dma_address(s),
+                       .size           = sg_dma_len(s),
+                       .direction      = direction,
+                       .sg_call_ents   = nelems,
+               };
+               if (!i)
+                       mapped_ents = get_nr_mapped_entries(dev, &ref);
+
+               if (i >= mapped_ents)
+                       break;
+
+               check_sync(dev, &ref, false);
        }
 }
 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
@@ -1120,8 +1302,8 @@ static int __init dma_debug_driver_setup(char *str)
        }
 
        if (current_driver_name[0])
-               printk(KERN_INFO "DMA-API: enable driver filter for "
-                                "driver [%s]\n", current_driver_name);
+               pr_info("DMA-API: enable driver filter for driver [%s]\n",
+                       current_driver_name);
 
 
        return 1;