Lines Matching +full:entry +full:- +full:address
1 // SPDX-License-Identifier: GPL-2.0-only
8 #define pr_fmt(fmt) "DMA-API: " fmt
12 #include <linux/dma-map-ops.h>
31 #define HASH_FN_MASK (HASH_SIZE - 1)
53 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
54 * @list: node on pre-allocated free_entries list
56 * @dev_addr: dma address
62 * @pfn: page frame of the start address
95 /* List of pre-allocated dma_debug_entry's */
100 /* Global disable flag - will be set in case of an error */
126 /* per-driver filter related state */
143 [dma_debug_sg] = "scatter-gather",
168 static inline void dump_entry_trace(struct dma_debug_entry *entry) in dump_entry_trace() argument
171 if (entry) { in dump_entry_trace()
173 stack_trace_print(entry->stack_entries, entry->stack_len, 0); in dump_entry_trace()
189 if (current_driver && dev && dev->driver == current_driver) in driver_filter()
200 drv = dev->driver; in driver_filter()
208 if (drv->name && in driver_filter()
209 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) { in driver_filter()
219 #define err_printk(dev, entry, format, arg...) do { \ argument
226 dump_entry_trace(entry); \
229 show_num_errors -= 1; \
235 * Every DMA-API request is saved into a struct dma_debug_entry. To
238 static int hash_fn(struct dma_debug_entry *entry) in hash_fn() argument
241 * Hash function is based on the dma address. in hash_fn()
242 * We use bits 20-27 here as the index into the hash in hash_fn()
244 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; in hash_fn()
250 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, in get_hash_bucket() argument
254 int idx = hash_fn(entry); in get_hash_bucket()
267 __releases(&bucket->lock) in put_hash_bucket()
269 spin_unlock_irqrestore(&bucket->lock, flags); in put_hash_bucket()
274 return ((a->dev_addr == b->dev_addr) && in exact_match()
275 (a->dev == b->dev)) ? true : false; in exact_match()
281 if (a->dev != b->dev) in containing_match()
284 if ((b->dev_addr <= a->dev_addr) && in containing_match()
285 ((b->dev_addr + b->size) >= (a->dev_addr + a->size))) in containing_match()
292 * Search a given entry in the hash bucket list
298 struct dma_debug_entry *entry, *ret = NULL; in __hash_bucket_find() local
299 int matches = 0, match_lvl, last_lvl = -1; in __hash_bucket_find()
301 list_for_each_entry(entry, &bucket->list, list) { in __hash_bucket_find()
302 if (!match(ref, entry)) in __hash_bucket_find()
306 * Some drivers map the same physical address multiple in __hash_bucket_find()
308 * same device addresses being put into the dma-debug in __hash_bucket_find()
311 * best-fit algorithm here which returns the entry from in __hash_bucket_find()
313 * instead of the first-fit. in __hash_bucket_find()
317 entry->size == ref->size ? ++match_lvl : 0; in __hash_bucket_find()
318 entry->type == ref->type ? ++match_lvl : 0; in __hash_bucket_find()
319 entry->direction == ref->direction ? ++match_lvl : 0; in __hash_bucket_find()
320 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0; in __hash_bucket_find()
323 /* perfect-fit - return the result */ in __hash_bucket_find()
324 return entry; in __hash_bucket_find()
327 * We found an entry that fits better then the in __hash_bucket_find()
331 ret = entry; in __hash_bucket_find()
336 * If we have multiple matches but no perfect-fit, just return in __hash_bucket_find()
355 struct dma_debug_entry *entry, index = *ref; in bucket_find_contain() local
359 entry = __hash_bucket_find(*bucket, ref, containing_match); in bucket_find_contain()
361 if (entry) in bucket_find_contain()
362 return entry; in bucket_find_contain()
368 index.dev_addr -= (1 << HASH_FN_SHIFT); in bucket_find_contain()
376 * Add an entry to a hash bucket
379 struct dma_debug_entry *entry) in hash_bucket_add() argument
381 list_add_tail(&entry->list, &bucket->list); in hash_bucket_add()
385 * Remove entry from a hash bucket list
387 static void hash_bucket_del(struct dma_debug_entry *entry) in hash_bucket_del() argument
389 list_del(&entry->list); in hash_bucket_del()
392 static unsigned long long phys_addr(struct dma_debug_entry *entry) in phys_addr() argument
394 if (entry->type == dma_debug_resource) in phys_addr()
395 return __pfn_to_phys(entry->pfn) + entry->offset; in phys_addr()
397 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset; in phys_addr()
405 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
406 * the entry already exists at insertion time add a tag as a reference
413 * dma-debug entries in that we need a free dma_debug_entry before
416 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
425 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
426 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
429 static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry) in to_cacheline_number() argument
431 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) + in to_cacheline_number()
432 (entry->offset >> L1_CACHE_SHIFT); in to_cacheline_number()
439 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_read_overlap()
452 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_set_overlap()
468 * leaking dma-mappings. in active_cacheline_inc_overlap()
479 return active_cacheline_set_overlap(cln, --overlap); in active_cacheline_dec_overlap()
482 static int active_cacheline_insert(struct dma_debug_entry *entry) in active_cacheline_insert() argument
484 phys_addr_t cln = to_cacheline_number(entry); in active_cacheline_insert()
492 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_insert()
496 rc = radix_tree_insert(&dma_active_cacheline, cln, entry); in active_cacheline_insert()
497 if (rc == -EEXIST) in active_cacheline_insert()
504 static void active_cacheline_remove(struct dma_debug_entry *entry) in active_cacheline_remove() argument
506 phys_addr_t cln = to_cacheline_number(entry); in active_cacheline_remove()
510 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_remove()
516 * active_cacheline_dec_overlap() returns -1 in that case in active_cacheline_remove()
533 struct dma_debug_entry *entry; in debug_dma_dump_mappings() local
536 spin_lock_irqsave(&bucket->lock, flags); in debug_dma_dump_mappings()
537 list_for_each_entry(entry, &bucket->list, list) { in debug_dma_dump_mappings()
538 if (!dev || dev == entry->dev) { in debug_dma_dump_mappings()
539 cln = to_cacheline_number(entry); in debug_dma_dump_mappings()
540 dev_info(entry->dev, in debug_dma_dump_mappings()
542 type2name[entry->type], idx, in debug_dma_dump_mappings()
543 phys_addr(entry), entry->pfn, in debug_dma_dump_mappings()
544 entry->dev_addr, entry->size, in debug_dma_dump_mappings()
545 &cln, dir2name[entry->direction], in debug_dma_dump_mappings()
546 maperr2str[entry->map_err_type]); in debug_dma_dump_mappings()
549 spin_unlock_irqrestore(&bucket->lock, flags); in debug_dma_dump_mappings()
565 struct dma_debug_entry *entry; in dump_show() local
568 spin_lock_irqsave(&bucket->lock, flags); in dump_show()
569 list_for_each_entry(entry, &bucket->list, list) { in dump_show()
570 cln = to_cacheline_number(entry); in dump_show()
573 dev_driver_string(entry->dev), in dump_show()
574 dev_name(entry->dev), in dump_show()
575 type2name[entry->type], idx, in dump_show()
576 phys_addr(entry), entry->pfn, in dump_show()
577 entry->dev_addr, entry->size, in dump_show()
578 &cln, dir2name[entry->direction], in dump_show()
579 maperr2str[entry->map_err_type]); in dump_show()
581 spin_unlock_irqrestore(&bucket->lock, flags); in dump_show()
588 * Wrapper function for adding an entry to the hash.
591 static void add_dma_entry(struct dma_debug_entry *entry, unsigned long attrs) in add_dma_entry() argument
597 bucket = get_hash_bucket(entry, &flags); in add_dma_entry()
598 hash_bucket_add(bucket, entry); in add_dma_entry()
601 rc = active_cacheline_insert(entry); in add_dma_entry()
602 if (rc == -ENOMEM) { in add_dma_entry()
603 pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n"); in add_dma_entry()
605 } else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) { in add_dma_entry()
606 err_printk(entry->dev, entry, in add_dma_entry()
613 struct dma_debug_entry *entry; in dma_debug_create_entries() local
616 entry = (void *)get_zeroed_page(gfp); in dma_debug_create_entries()
617 if (!entry) in dma_debug_create_entries()
618 return -ENOMEM; in dma_debug_create_entries()
621 list_add_tail(&entry[i].list, &free_entries); in dma_debug_create_entries()
631 struct dma_debug_entry *entry; in __dma_entry_alloc() local
633 entry = list_entry(free_entries.next, struct dma_debug_entry, list); in __dma_entry_alloc()
634 list_del(&entry->list); in __dma_entry_alloc()
635 memset(entry, 0, sizeof(*entry)); in __dma_entry_alloc()
637 num_free_entries -= 1; in __dma_entry_alloc()
641 return entry; in __dma_entry_alloc()
668 struct dma_debug_entry *entry; in dma_entry_alloc() local
677 pr_err("debugging out of memory - disabling\n"); in dma_entry_alloc()
684 entry = __dma_entry_alloc(); in dma_entry_alloc()
692 entry->stack_len = stack_trace_save(entry->stack_entries, in dma_entry_alloc()
693 ARRAY_SIZE(entry->stack_entries), in dma_entry_alloc()
696 return entry; in dma_entry_alloc()
699 static void dma_entry_free(struct dma_debug_entry *entry) in dma_entry_free() argument
703 active_cacheline_remove(entry); in dma_entry_free()
706 * add to beginning of the list - this way the entries are in dma_entry_free()
710 list_add(&entry->list, &free_entries); in dma_entry_free()
716 * DMA-API debugging init code
759 len = min(count, (size_t)(NAME_MAX_LEN - 1)); in filter_write()
761 return -EFAULT; in filter_write()
770 * - only use the first token we got in filter_write()
771 * - token delimiter is everything looking like a space in filter_write()
782 pr_info("switching off dma-debug driver filter\n"); in filter_write()
792 for (i = 0; i < NAME_MAX_LEN - 1; ++i) { in filter_write()
817 struct dentry *dentry = debugfs_create_dir("dma-api", NULL); in dma_debug_fs_init()
835 struct dma_debug_entry *entry; in device_dma_allocations() local
841 list_for_each_entry(entry, &dma_entry_hash[i].list, list) { in device_dma_allocations()
842 if (entry->dev == dev) { in device_dma_allocations()
844 *out_entry = entry; in device_dma_allocations()
856 struct dma_debug_entry *entry; in dma_debug_device_change() local
864 count = device_dma_allocations(dev, &entry); in dma_debug_device_change()
867 err_printk(dev, entry, "device driver has pending " in dma_debug_device_change()
871 "[device address=0x%016llx] [size=%llu bytes] " in dma_debug_device_change()
873 count, entry->dev_addr, entry->size, in dma_debug_device_change()
874 dir2name[entry->direction], type2name[entry->type]); in dma_debug_device_change()
896 nb->notifier_call = dma_debug_device_change; in dma_debug_add_bus()
925 pr_err("debugging out of memory error - disabled\n"); in dma_debug_init()
942 return -EINVAL; in dma_debug_cmdline()
955 return -EINVAL; in dma_debug_entries_cmdline()
966 struct dma_debug_entry *entry; in check_unmap() local
971 entry = bucket_find_exact(bucket, ref); in check_unmap()
973 if (!entry) { in check_unmap()
977 if (dma_mapping_error(ref->dev, ref->dev_addr)) { in check_unmap()
978 err_printk(ref->dev, NULL, in check_unmap()
980 "invalid DMA memory address\n"); in check_unmap()
982 err_printk(ref->dev, NULL, in check_unmap()
985 "address=0x%016llx] [size=%llu bytes]\n", in check_unmap()
986 ref->dev_addr, ref->size); in check_unmap()
991 if (ref->size != entry->size) { in check_unmap()
992 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
994 "[device address=0x%016llx] [map size=%llu bytes] " in check_unmap()
996 ref->dev_addr, entry->size, ref->size); in check_unmap()
999 if (ref->type != entry->type) { in check_unmap()
1000 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1002 "[device address=0x%016llx] [size=%llu bytes] " in check_unmap()
1004 ref->dev_addr, ref->size, in check_unmap()
1005 type2name[entry->type], type2name[ref->type]); in check_unmap()
1006 } else if ((entry->type == dma_debug_coherent) && in check_unmap()
1007 (phys_addr(ref) != phys_addr(entry))) { in check_unmap()
1008 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1009 "DMA memory with different CPU address " in check_unmap()
1010 "[device address=0x%016llx] [size=%llu bytes] " in check_unmap()
1011 "[cpu alloc address=0x%016llx] " in check_unmap()
1012 "[cpu free address=0x%016llx]", in check_unmap()
1013 ref->dev_addr, ref->size, in check_unmap()
1014 phys_addr(entry), in check_unmap()
1018 if (ref->sg_call_ents && ref->type == dma_debug_sg && in check_unmap()
1019 ref->sg_call_ents != entry->sg_call_ents) { in check_unmap()
1020 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1021 "DMA sg list with different entry count " in check_unmap()
1023 entry->sg_call_ents, ref->sg_call_ents); in check_unmap()
1027 * This may be no bug in reality - but most implementations of the in check_unmap()
1030 if (ref->direction != entry->direction) { in check_unmap()
1031 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1033 "[device address=0x%016llx] [size=%llu bytes] " in check_unmap()
1035 ref->dev_addr, ref->size, in check_unmap()
1036 dir2name[entry->direction], in check_unmap()
1037 dir2name[ref->direction]); in check_unmap()
1043 * If not, print this warning message. See Documentation/core-api/dma-api.rst. in check_unmap()
1045 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { in check_unmap()
1046 err_printk(ref->dev, entry, in check_unmap()
1048 "[device address=0x%016llx] [size=%llu bytes] " in check_unmap()
1050 ref->dev_addr, ref->size, in check_unmap()
1051 type2name[entry->type]); in check_unmap()
1054 hash_bucket_del(entry); in check_unmap()
1055 dma_entry_free(entry); in check_unmap()
1067 /* Stack is direct-mapped. */ in check_for_stack()
1077 for (i = 0; i < stack_vm_area->nr_pages; i++) { in check_for_stack()
1078 if (page != stack_vm_area->pages[i]) in check_for_stack()
1081 addr = (u8 *)current->stack + i * PAGE_SIZE + offset; in check_for_stack()
1099 struct dma_debug_entry *entry; in check_sync() local
1105 entry = bucket_find_contain(&bucket, ref, &flags); in check_sync()
1107 if (!entry) { in check_sync()
1110 "[device address=0x%016llx] [size=%llu bytes]\n", in check_sync()
1111 (unsigned long long)ref->dev_addr, ref->size); in check_sync()
1115 if (ref->size > entry->size) { in check_sync()
1116 err_printk(dev, entry, "device driver syncs" in check_sync()
1118 "[device address=0x%016llx] " in check_sync()
1121 entry->dev_addr, entry->size, in check_sync()
1122 ref->size); in check_sync()
1125 if (entry->direction == DMA_BIDIRECTIONAL) in check_sync()
1128 if (ref->direction != entry->direction) { in check_sync()
1129 err_printk(dev, entry, "device driver syncs " in check_sync()
1131 "[device address=0x%016llx] [size=%llu bytes] " in check_sync()
1133 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1134 dir2name[entry->direction], in check_sync()
1135 dir2name[ref->direction]); in check_sync()
1138 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && in check_sync()
1139 !(ref->direction == DMA_TO_DEVICE)) in check_sync()
1140 err_printk(dev, entry, "device driver syncs " in check_sync()
1141 "device read-only DMA memory for cpu " in check_sync()
1142 "[device address=0x%016llx] [size=%llu bytes] " in check_sync()
1144 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1145 dir2name[entry->direction], in check_sync()
1146 dir2name[ref->direction]); in check_sync()
1148 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && in check_sync()
1149 !(ref->direction == DMA_FROM_DEVICE)) in check_sync()
1150 err_printk(dev, entry, "device driver syncs " in check_sync()
1151 "device write-only DMA memory to device " in check_sync()
1152 "[device address=0x%016llx] [size=%llu bytes] " in check_sync()
1154 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1155 dir2name[entry->direction], in check_sync()
1156 dir2name[ref->direction]); in check_sync()
1158 if (ref->sg_call_ents && ref->type == dma_debug_sg && in check_sync()
1159 ref->sg_call_ents != entry->sg_call_ents) { in check_sync()
1160 err_printk(ref->dev, entry, "device driver syncs " in check_sync()
1161 "DMA sg list with different entry count " in check_sync()
1163 entry->sg_call_ents, ref->sg_call_ents); in check_sync()
1180 if (sg->length > max_seg) in check_sg_segment()
1182 sg->length, max_seg); in check_sg_segment()
1189 end = start + sg_dma_len(sg) - 1; in check_sg_segment()
1216 struct dma_debug_entry *entry; in debug_dma_map_page() local
1224 entry = dma_entry_alloc(); in debug_dma_map_page()
1225 if (!entry) in debug_dma_map_page()
1228 entry->dev = dev; in debug_dma_map_page()
1229 entry->type = dma_debug_single; in debug_dma_map_page()
1230 entry->pfn = page_to_pfn(page); in debug_dma_map_page()
1231 entry->offset = offset; in debug_dma_map_page()
1232 entry->dev_addr = dma_addr; in debug_dma_map_page()
1233 entry->size = size; in debug_dma_map_page()
1234 entry->direction = direction; in debug_dma_map_page()
1235 entry->map_err_type = MAP_ERR_NOT_CHECKED; in debug_dma_map_page()
1245 add_dma_entry(entry, attrs); in debug_dma_map_page()
1251 struct dma_debug_entry *entry; in debug_dma_mapping_error() local
1262 list_for_each_entry(entry, &bucket->list, list) { in debug_dma_mapping_error()
1263 if (!exact_match(&ref, entry)) in debug_dma_mapping_error()
1267 * The same physical address can be mapped multiple in debug_dma_mapping_error()
1269 * same device addresses being put into the dma-debug in debug_dma_mapping_error()
1272 * best-fit algorithm here which updates the first entry in debug_dma_mapping_error()
1276 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { in debug_dma_mapping_error()
1277 entry->map_err_type = MAP_ERR_CHECKED; in debug_dma_mapping_error()
1306 struct dma_debug_entry *entry; in debug_dma_map_sg() local
1314 check_for_stack(dev, sg_page(s), s->offset); in debug_dma_map_sg()
1316 check_for_illegal_area(dev, sg_virt(s), s->length); in debug_dma_map_sg()
1320 entry = dma_entry_alloc(); in debug_dma_map_sg()
1321 if (!entry) in debug_dma_map_sg()
1324 entry->type = dma_debug_sg; in debug_dma_map_sg()
1325 entry->dev = dev; in debug_dma_map_sg()
1326 entry->pfn = page_to_pfn(sg_page(s)); in debug_dma_map_sg()
1327 entry->offset = s->offset; in debug_dma_map_sg()
1328 entry->size = sg_dma_len(s); in debug_dma_map_sg()
1329 entry->dev_addr = sg_dma_address(s); in debug_dma_map_sg()
1330 entry->direction = direction; in debug_dma_map_sg()
1331 entry->sg_call_ents = nents; in debug_dma_map_sg()
1332 entry->sg_mapped_ents = mapped_ents; in debug_dma_map_sg()
1336 add_dma_entry(entry, attrs); in debug_dma_map_sg()
1343 struct dma_debug_entry *entry; in get_nr_mapped_entries() local
1349 entry = bucket_find_exact(bucket, ref); in get_nr_mapped_entries()
1352 if (entry) in get_nr_mapped_entries()
1353 mapped_ents = entry->sg_mapped_ents; in get_nr_mapped_entries()
1374 .offset = s->offset, in debug_dma_unmap_sg()
1395 struct dma_debug_entry *entry; in debug_dma_alloc_coherent() local
1407 entry = dma_entry_alloc(); in debug_dma_alloc_coherent()
1408 if (!entry) in debug_dma_alloc_coherent()
1411 entry->type = dma_debug_coherent; in debug_dma_alloc_coherent()
1412 entry->dev = dev; in debug_dma_alloc_coherent()
1413 entry->offset = offset_in_page(virt); in debug_dma_alloc_coherent()
1414 entry->size = size; in debug_dma_alloc_coherent()
1415 entry->dev_addr = dma_addr; in debug_dma_alloc_coherent()
1416 entry->direction = DMA_BIDIRECTIONAL; in debug_dma_alloc_coherent()
1419 entry->pfn = vmalloc_to_pfn(virt); in debug_dma_alloc_coherent()
1421 entry->pfn = page_to_pfn(virt_to_page(virt)); in debug_dma_alloc_coherent()
1423 add_dma_entry(entry, attrs); in debug_dma_alloc_coherent()
1457 struct dma_debug_entry *entry; in debug_dma_map_resource() local
1462 entry = dma_entry_alloc(); in debug_dma_map_resource()
1463 if (!entry) in debug_dma_map_resource()
1466 entry->type = dma_debug_resource; in debug_dma_map_resource()
1467 entry->dev = dev; in debug_dma_map_resource()
1468 entry->pfn = PHYS_PFN(addr); in debug_dma_map_resource()
1469 entry->offset = offset_in_page(addr); in debug_dma_map_resource()
1470 entry->size = size; in debug_dma_map_resource()
1471 entry->dev_addr = dma_addr; in debug_dma_map_resource()
1472 entry->direction = direction; in debug_dma_map_resource()
1473 entry->map_err_type = MAP_ERR_NOT_CHECKED; in debug_dma_map_resource()
1475 add_dma_entry(entry, attrs); in debug_dma_map_resource()
1547 .offset = s->offset, in debug_dma_sync_sg_for_cpu()
1579 .offset = s->offset, in debug_dma_sync_sg_for_device()
1599 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) { in dma_debug_driver_setup()