linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* is dma_mapping_error() check necessary for dma_alloc_noncoherent()?
@ 2025-08-26  9:44 ` Baochen Qiang
  2025-08-26 14:09   ` Marek Szyprowski
  2025-08-27  7:31   ` Christoph Hellwig
  0 siblings, 2 replies; 5+ messages in thread
From: Baochen Qiang @ 2025-08-26  9:44 UTC (permalink / raw)
  To: Marek Szyprowski, Robin Murphy, Jeff Johnson
  Cc: iommu, linux-kernel@vger.kernel.org >> linux-kernel,
	ath11k@lists.infradead.org

Hi guys,

I have a driver which allocate noncoherent DMA buffer and get the returned CPU addr tested:

	vaddr_unaligned = dma_alloc_noncoherent(ab->dev, rx_tid->unaligned_size, &paddr,
						DMA_BIDIRECTIONAL, GFP_ATOMIC);
	if (!vaddr_unaligned) {
		spin_unlock_bh(&ab->base_lock);
		return -ENOMEM;
	}

while free the buffer

	dma_free_noncoherent(ab->dev, rx_tid->unaligned_size,
			     rx_tid->vaddr_unaligned,
			     rx_tid->paddr_unaligned, DMA_BIDIRECTIONAL);

I get below warnings:

 DMA-API: ath11k_pci 0000:03:00.0: device driver failed to check map error[device
address=0x00000000f3ad7000] [size=639 bytes] [mapped as single]
 WARNING: CPU: 15 PID: 64303 at kernel/dma/debug.c:1036 check_unmap+0x7e2/0x950
 RIP: 0010:check_unmap+0x7e2/0x950
 Call Trace:
  <TASK>
  ? free_to_partial_list+0x9d/0x350
  debug_dma_unmap_page+0xac/0xc0
  ? debug_smp_processor_id+0x17/0x20
  ? rcu_is_watching+0x13/0x70
  dma_free_pages+0x56/0x180
  [...]
  </TASK>
 ---[ end trace 0000000000000000 ]---
 DMA-API: Mapped at:
  debug_dma_map_page+0x7c/0x140
  dma_alloc_pages+0x74/0x220
  [...]

Checking code gives me the impression that I should do dma_mapping_error() check as well.
And indeed with below diff the warning is gone:

+       dma_mapping_error(ab->dev, paddr);

However this does not make sense to me since IMO testing the CPU address is good enough, I
can not imagine a valid case where DMA alloc/map fails while returning a valid CPU
address, no?

If I was right, should we remove invocation to debug_dma_map_page() in dma_alloc_pages()?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: is dma_mapping_error() check necessary for dma_alloc_noncoherent()?
  2025-08-26  9:44 ` is dma_mapping_error() check necessary for dma_alloc_noncoherent()? Baochen Qiang
@ 2025-08-26 14:09   ` Marek Szyprowski
  2025-08-27  1:55     ` Baochen Qiang
  2025-08-27  7:31   ` Christoph Hellwig
  1 sibling, 1 reply; 5+ messages in thread
From: Marek Szyprowski @ 2025-08-26 14:09 UTC (permalink / raw)
  To: Baochen Qiang, Robin Murphy, Jeff Johnson
  Cc: iommu, linux-kernel@vger.kernel.org >> linux-kernel,
	ath11k@lists.infradead.org

On 26.08.2025 11:44, Baochen Qiang wrote:
> Hi guys,
>
> I have a driver which allocate noncoherent DMA buffer and get the returned CPU addr tested:
>
> 	vaddr_unaligned = dma_alloc_noncoherent(ab->dev, rx_tid->unaligned_size, &paddr,
> 						DMA_BIDIRECTIONAL, GFP_ATOMIC);
> 	if (!vaddr_unaligned) {
> 		spin_unlock_bh(&ab->base_lock);
> 		return -ENOMEM;
> 	}
>
> while free the buffer
>
> 	dma_free_noncoherent(ab->dev, rx_tid->unaligned_size,
> 			     rx_tid->vaddr_unaligned,
> 			     rx_tid->paddr_unaligned, DMA_BIDIRECTIONAL);
>
> I get below warnings:
>
>   DMA-API: ath11k_pci 0000:03:00.0: device driver failed to check map error[device
> address=0x00000000f3ad7000] [size=639 bytes] [mapped as single]
>   WARNING: CPU: 15 PID: 64303 at kernel/dma/debug.c:1036 check_unmap+0x7e2/0x950
>   RIP: 0010:check_unmap+0x7e2/0x950
>   Call Trace:
>    <TASK>
>    ? free_to_partial_list+0x9d/0x350
>    debug_dma_unmap_page+0xac/0xc0
>    ? debug_smp_processor_id+0x17/0x20
>    ? rcu_is_watching+0x13/0x70
>    dma_free_pages+0x56/0x180
>    [...]
>    </TASK>
>   ---[ end trace 0000000000000000 ]---
>   DMA-API: Mapped at:
>    debug_dma_map_page+0x7c/0x140
>    dma_alloc_pages+0x74/0x220
>    [...]
>
> Checking code gives me the impression that I should do dma_mapping_error() check as well.
> And indeed with below diff the warning is gone:
>
> +       dma_mapping_error(ab->dev, paddr);
>
> However this does not make sense to me since IMO testing the CPU address is good enough, I
> can not imagine a valid case where DMA alloc/map fails while returning a valid CPU
> address, no?
>
> If I was right, should we remove invocation to debug_dma_map_page() in dma_alloc_pages()?

Simply replace "if (!vaddr_unaligned)" with "if 
dma_mapping_error(ab->dev, paddr)" and the debug code will be happy.

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: is dma_mapping_error() check necessary for dma_alloc_noncoherent()?
  2025-08-26 14:09   ` Marek Szyprowski
@ 2025-08-27  1:55     ` Baochen Qiang
  0 siblings, 0 replies; 5+ messages in thread
From: Baochen Qiang @ 2025-08-27  1:55 UTC (permalink / raw)
  To: Marek Szyprowski, Baochen Qiang, Robin Murphy, Jeff Johnson
  Cc: iommu, linux-kernel@vger.kernel.org >> linux-kernel,
	ath11k@lists.infradead.org



On 8/26/2025 10:09 PM, Marek Szyprowski wrote:
> On 26.08.2025 11:44, Baochen Qiang wrote:
>> Hi guys,
>>
>> I have a driver which allocate noncoherent DMA buffer and get the returned CPU addr tested:
>>
>> 	vaddr_unaligned = dma_alloc_noncoherent(ab->dev, rx_tid->unaligned_size, &paddr,
>> 						DMA_BIDIRECTIONAL, GFP_ATOMIC);
>> 	if (!vaddr_unaligned) {
>> 		spin_unlock_bh(&ab->base_lock);
>> 		return -ENOMEM;
>> 	}
>>
>> while free the buffer
>>
>> 	dma_free_noncoherent(ab->dev, rx_tid->unaligned_size,
>> 			     rx_tid->vaddr_unaligned,
>> 			     rx_tid->paddr_unaligned, DMA_BIDIRECTIONAL);
>>
>> I get below warnings:
>>
>>   DMA-API: ath11k_pci 0000:03:00.0: device driver failed to check map error[device
>> address=0x00000000f3ad7000] [size=639 bytes] [mapped as single]
>>   WARNING: CPU: 15 PID: 64303 at kernel/dma/debug.c:1036 check_unmap+0x7e2/0x950
>>   RIP: 0010:check_unmap+0x7e2/0x950
>>   Call Trace:
>>    <TASK>
>>    ? free_to_partial_list+0x9d/0x350
>>    debug_dma_unmap_page+0xac/0xc0
>>    ? debug_smp_processor_id+0x17/0x20
>>    ? rcu_is_watching+0x13/0x70
>>    dma_free_pages+0x56/0x180
>>    [...]
>>    </TASK>
>>   ---[ end trace 0000000000000000 ]---
>>   DMA-API: Mapped at:
>>    debug_dma_map_page+0x7c/0x140
>>    dma_alloc_pages+0x74/0x220
>>    [...]
>>
>> Checking code gives me the impression that I should do dma_mapping_error() check as well.
>> And indeed with below diff the warning is gone:
>>
>> +       dma_mapping_error(ab->dev, paddr);
>>
>> However this does not make sense to me since IMO testing the CPU address is good enough, I
>> can not imagine a valid case where DMA alloc/map fails while returning a valid CPU
>> address, no?
>>
>> If I was right, should we remove invocation to debug_dma_map_page() in dma_alloc_pages()?
> 
> Simply replace "if (!vaddr_unaligned)" with "if 
> dma_mapping_error(ab->dev, paddr)" and the debug code will be happy.

Thanks, much better!

But I am wondering if the debug code is worrying too much? isn't the CPU addr test already
good enough?

> 
> Best regards


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: is dma_mapping_error() check necessary for dma_alloc_noncoherent()?
  2025-08-26  9:44 ` is dma_mapping_error() check necessary for dma_alloc_noncoherent()? Baochen Qiang
  2025-08-26 14:09   ` Marek Szyprowski
@ 2025-08-27  7:31   ` Christoph Hellwig
  2025-08-28  6:32     ` Baochen Qiang
  1 sibling, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2025-08-27  7:31 UTC (permalink / raw)
  To: Baochen Qiang
  Cc: Marek Szyprowski, Robin Murphy, Jeff Johnson, iommu,
	linux-kernel@vger.kernel.org >> linux-kernel,
	ath11k@lists.infradead.org

On Tue, Aug 26, 2025 at 05:44:42PM +0800, Baochen Qiang wrote:
> Checking code gives me the impression that I should do dma_mapping_error() check as well.
> And indeed with below diff the warning is gone:
> 
> +       dma_mapping_error(ab->dev, paddr);
> 
> However this does not make sense to me since IMO testing the CPU address is good enough, I
> can not imagine a valid case where DMA alloc/map fails while returning a valid CPU
> address, no?

Yes, this doesn't make sense.  dma_mapping_error exists to provide a
error handling path for dma_map_*, which return the dma address only.

For the dma_alloc_* interfaces that return a pointer and can signal
with a NULL return it should not be needed and dma-debug needs to
be fixed.

> If I was right, should we remove invocation to debug_dma_map_page() in dma_alloc_pages()?
> 

That allocation still needs to be tracked, so it can't just be removed
but needs to be changed to record the kind of allocation.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: is dma_mapping_error() check necessary for dma_alloc_noncoherent()?
  2025-08-27  7:31   ` Christoph Hellwig
@ 2025-08-28  6:32     ` Baochen Qiang
  0 siblings, 0 replies; 5+ messages in thread
From: Baochen Qiang @ 2025-08-28  6:32 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Marek Szyprowski, Robin Murphy, Jeff Johnson, iommu,
	linux-kernel@vger.kernel.org >> linux-kernel,
	ath11k@lists.infradead.org



On 8/27/2025 3:31 PM, Christoph Hellwig wrote:
> On Tue, Aug 26, 2025 at 05:44:42PM +0800, Baochen Qiang wrote:
>> Checking code gives me the impression that I should do dma_mapping_error() check as well.
>> And indeed with below diff the warning is gone:
>>
>> +       dma_mapping_error(ab->dev, paddr);
>>
>> However this does not make sense to me since IMO testing the CPU address is good enough, I
>> can not imagine a valid case where DMA alloc/map fails while returning a valid CPU
>> address, no?
> 
> Yes, this doesn't make sense.  dma_mapping_error exists to provide a
> error handling path for dma_map_*, which return the dma address only.
> 
> For the dma_alloc_* interfaces that return a pointer and can signal
> with a NULL return it should not be needed and dma-debug needs to
> be fixed.
> 
>> If I was right, should we remove invocation to debug_dma_map_page() in dma_alloc_pages()?
>>
> 
> That allocation still needs to be tracked, so it can't just be removed
> but needs to be changed to record the kind of allocation.

Thanks, I will submit a patch to fix this.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-28  6:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20250826094453eucas1p178ff4a39db0c655f3505128a5cfb0a6a@eucas1p1.samsung.com>
2025-08-26  9:44 ` is dma_mapping_error() check necessary for dma_alloc_noncoherent()? Baochen Qiang
2025-08-26 14:09   ` Marek Szyprowski
2025-08-27  1:55     ` Baochen Qiang
2025-08-27  7:31   ` Christoph Hellwig
2025-08-28  6:32     ` Baochen Qiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).