1610 lines · 41.7 KB · C
RawDownload
	    ref->sg_call_ents != entry->sg_call_ents) {		err_printk(ref->dev, entry, "device driver frees "			   "DMA sg list with different entry count "			   "[map count=%d] [unmap count=%d]\n",			   entry->sg_call_ents, ref->sg_call_ents);	} 	/*	 * This may be no bug in reality - but most implementations of the	 * DMA API don't handle this properly, so check for it here	 */	if (ref->direction != entry->direction) {		err_printk(ref->dev, entry, "device driver frees "			   "DMA memory with different direction "			   "[device address=0x%016llx] [size=%llu bytes] "			   "[mapped with %s] [unmapped with %s]\n",			   ref->dev_addr, ref->size,			   dir2name[entry->direction],			   dir2name[ref->direction]);	} 	/*	 * Drivers should use dma_mapping_error() to check the returned	 * addresses of dma_map_single() and dma_map_page().	 * If not, print this warning message. See Documentation/core-api/dma-api.rst.	 */	if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {		err_printk(ref->dev, entry,			   "device driver failed to check map error"			   "[device address=0x%016llx] [size=%llu bytes] "			   "[mapped as %s]",			   ref->dev_addr, ref->size,			   type2name[entry->type]);	} 	hash_bucket_del(entry);	dma_entry_free(entry); 	put_hash_bucket(bucket, flags);} static void check_for_stack(struct device *dev,			    struct page *page, size_t offset){	void *addr;	struct vm_struct *stack_vm_area = task_stack_vm_area(current); 	if (!stack_vm_area) {		/* Stack is direct-mapped. */		if (PageHighMem(page))			return;		addr = page_address(page) + offset;		if (object_is_on_stack(addr))			err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);	} else {		/* Stack is vmalloced. */		int i; 		for (i = 0; i < stack_vm_area->nr_pages; i++) {			if (page != stack_vm_area->pages[i])				continue; 			addr = (u8 *)current->stack + i * PAGE_SIZE + offset;			err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);			break;		}	}} static inline bool overlap(void *addr, unsigned long len, void *start, void *end){	unsigned long a1 = (unsigned long)addr;	unsigned long b1 = a1 + len;	unsigned long a2 = (unsigned long)start;	unsigned long b2 = (unsigned long)end; 	return !(b1 <= a2 || a1 >= b2);} static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len){	if (overlap(addr, len, _stext, _etext) ||	    overlap(addr, len, __start_rodata, __end_rodata))		err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);} static void check_sync(struct device *dev,		       struct dma_debug_entry *ref,		       bool to_cpu){	struct dma_debug_entry *entry;	struct hash_bucket *bucket;	unsigned long flags; 	bucket = get_hash_bucket(ref, &flags); 	entry = bucket_find_contain(&bucket, ref, &flags); 	if (!entry) {		err_printk(dev, NULL, "device driver tries "				"to sync DMA memory it has not allocated "				"[device address=0x%016llx] [size=%llu bytes]\n",				(unsigned long long)ref->dev_addr, ref->size);		goto out;	} 	if (ref->size > entry->size) {		err_printk(dev, entry, "device driver syncs"				" DMA memory outside allocated range "				"[device address=0x%016llx] "				"[allocation size=%llu bytes] "				"[sync offset+size=%llu]\n",				entry->dev_addr, entry->size,				ref->size);	} 	if (entry->direction == DMA_BIDIRECTIONAL)		goto out; 	if (ref->direction != entry->direction) {		err_printk(dev, entry, "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)ref->dev_addr, entry->size,				dir2name[entry->direction],				dir2name[ref->direction]);	} 	if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&		      !(ref->direction == DMA_TO_DEVICE))		err_printk(dev, entry, "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)ref->dev_addr, entry->size,				dir2name[entry->direction],				dir2name[ref->direction]); 	if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&		       !(ref->direction == DMA_FROM_DEVICE))		err_printk(dev, entry, "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)ref->dev_addr, entry->size,				dir2name[entry->direction],				dir2name[ref->direction]); 	if (ref->sg_call_ents && ref->type == dma_debug_sg &&	    ref->sg_call_ents != entry->sg_call_ents) {		err_printk(ref->dev, entry, "device driver syncs "			   "DMA sg list with different entry count "			   "[map count=%d] [sync count=%d]\n",			   entry->sg_call_ents, ref->sg_call_ents);	} out:	put_hash_bucket(bucket, flags);} static void check_sg_segment(struct device *dev, struct scatterlist *sg){#ifdef CONFIG_DMA_API_DEBUG_SG	unsigned int max_seg = dma_get_max_seg_size(dev);	u64 start, end, boundary = dma_get_seg_boundary(dev); 	/*	 * Either the driver forgot to set dma_parms appropriately, or	 * whoever generated the list forgot to check them.	 */	if (sg->length > max_seg)		err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",			   sg->length, max_seg);	/*	 * In some cases this could potentially be the DMA API	 * implementation's fault, but it would usually imply that	 * the scatterlist was built inappropriately to begin with.	 */	start = sg_dma_address(sg);	end = start + sg_dma_len(sg) - 1;	if ((start ^ end) & ~boundary)		err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",			   start, end, boundary);#endif} void debug_dma_map_single(struct device *dev, const void *addr,			    unsigned long len){	if (unlikely(dma_debug_disabled()))		return; 	if (!virt_addr_valid(addr))		err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",			   addr, len); 	if (is_vmalloc_addr(addr))		err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",			   addr, len);}EXPORT_SYMBOL(debug_dma_map_single); void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,			size_t size, int direction, dma_addr_t dma_addr){	struct dma_debug_entry *entry; 	if (unlikely(dma_debug_disabled()))		return; 	if (dma_mapping_error(dev, dma_addr))		return; 	entry = dma_entry_alloc();	if (!entry)		return; 	entry->dev       = dev;	entry->type      = dma_debug_single;	entry->pfn	 = page_to_pfn(page);	entry->offset	 = offset,	entry->dev_addr  = dma_addr;	entry->size      = size;	entry->direction = direction;	entry->map_err_type = MAP_ERR_NOT_CHECKED; 	check_for_stack(dev, page, offset); 	if (!PageHighMem(page)) {		void *addr = page_address(page) + offset; 		check_for_illegal_area(dev, addr, size);	} 	add_dma_entry(entry);}EXPORT_SYMBOL(debug_dma_map_page); void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr){	struct dma_debug_entry ref;	struct dma_debug_entry *entry;	struct hash_bucket *bucket;	unsigned long flags; 	if (unlikely(dma_debug_disabled()))		return; 	ref.dev = dev;	ref.dev_addr = dma_addr;	bucket = get_hash_bucket(&ref, &flags); 	list_for_each_entry(entry, &bucket->list, list) {		if (!exact_match(&ref, entry))			continue; 		/*		 * The same physical address can be mapped 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 updates the first entry		 * from the hash which fits the reference value and is		 * not currently listed as being checked.		 */		if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {			entry->map_err_type = MAP_ERR_CHECKED;			break;		}	} 	put_hash_bucket(bucket, flags);}EXPORT_SYMBOL(debug_dma_mapping_error); void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,			  size_t size, int direction){	struct dma_debug_entry ref = {		.type           = dma_debug_single,		.dev            = dev,		.dev_addr       = addr,		.size           = size,		.direction      = direction,	}; 	if (unlikely(dma_debug_disabled()))		return;	check_unmap(&ref);}EXPORT_SYMBOL(debug_dma_unmap_page); void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,		      int nents, int mapped_ents, int direction){	struct dma_debug_entry *entry;	struct scatterlist *s;	int i; 	if (unlikely(dma_debug_disabled()))		return; 	for_each_sg(sg, s, mapped_ents, i) {		entry = dma_entry_alloc();		if (!entry)			return; 		entry->type           = dma_debug_sg;		entry->dev            = dev;		entry->pfn	      = page_to_pfn(sg_page(s));		entry->offset	      = s->offset,		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; 		check_for_stack(dev, sg_page(s), s->offset); 		if (!PageHighMem(sg_page(s))) {			check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));		} 		check_sg_segment(dev, s); 		add_dma_entry(entry);	}}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        = bucket_find_exact(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 scatterlist *s;	int mapped_ents = 0, i; 	if (unlikely(dma_debug_disabled()))		return; 	for_each_sg(sglist, s, nelems, i) { 		struct dma_debug_entry ref = {			.type           = dma_debug_sg,			.dev            = dev,			.pfn		= page_to_pfn(sg_page(s)),			.offset		= s->offset,			.dev_addr       = sg_dma_address(s),			.size           = sg_dma_len(s),			.direction      = dir,			.sg_call_ents   = nelems,		}; 		if (mapped_ents && i >= mapped_ents)			break; 		if (!i)			mapped_ents = get_nr_mapped_entries(dev, &ref); 		check_unmap(&ref);	}}EXPORT_SYMBOL(debug_dma_unmap_sg); void debug_dma_alloc_coherent(struct device *dev, size_t size,			      dma_addr_t dma_addr, void *virt){	struct dma_debug_entry *entry; 	if (unlikely(dma_debug_disabled()))		return; 	if (unlikely(virt == NULL))		return; 	/* handle vmalloc and linear addresses */	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))		return; 	entry = dma_entry_alloc();	if (!entry)		return; 	entry->type      = dma_debug_coherent;	entry->dev       = dev;	entry->offset	 = offset_in_page(virt);	entry->size      = size;	entry->dev_addr  = dma_addr;	entry->direction = DMA_BIDIRECTIONAL; 	if (is_vmalloc_addr(virt))		entry->pfn = vmalloc_to_pfn(virt);	else		entry->pfn = page_to_pfn(virt_to_page(virt)); 	add_dma_entry(entry);} void debug_dma_free_coherent(struct device *dev, size_t size,			 void *virt, dma_addr_t addr){	struct dma_debug_entry ref = {		.type           = dma_debug_coherent,		.dev            = dev,		.offset		= offset_in_page(virt),		.dev_addr       = addr,		.size           = size,		.direction      = DMA_BIDIRECTIONAL,	}; 	/* handle vmalloc and linear addresses */	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))		return; 	if (is_vmalloc_addr(virt))		ref.pfn = vmalloc_to_pfn(virt);	else		ref.pfn = page_to_pfn(virt_to_page(virt)); 	if (unlikely(dma_debug_disabled()))		return; 	check_unmap(&ref);} void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,			    int direction, dma_addr_t dma_addr){	struct dma_debug_entry *entry; 	if (unlikely(dma_debug_disabled()))		return; 	entry = dma_entry_alloc();	if (!entry)		return; 	entry->type		= dma_debug_resource;	entry->dev		= dev;	entry->pfn		= PHYS_PFN(addr);	entry->offset		= offset_in_page(addr);	entry->size		= size;	entry->dev_addr		= dma_addr;	entry->direction	= direction;	entry->map_err_type	= MAP_ERR_NOT_CHECKED; 	add_dma_entry(entry);}EXPORT_SYMBOL(debug_dma_map_resource); void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,			      size_t size, int direction){	struct dma_debug_entry ref = {		.type           = dma_debug_resource,		.dev            = dev,		.dev_addr       = dma_addr,		.size           = size,		.direction      = direction,	}; 	if (unlikely(dma_debug_disabled()))		return; 	check_unmap(&ref);}EXPORT_SYMBOL(debug_dma_unmap_resource); 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(dma_debug_disabled()))		return; 	ref.type         = dma_debug_single;	ref.dev          = dev;	ref.dev_addr     = dma_handle;	ref.size         = size;

Lines 1001–1500 of 1610. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t