Skip to contentSkip to conversation

net: thunderbolt: Clear finished Tx frame bus address in tbnet_tx_callback()

PATCH Applied Mika Westerberg 3 participants 5 messages Nov 9, 2017 – Nov 20, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: thunderbolt: Clear finished Tx frame bus address in tbnet_tx_callback()

When Thunderbolt network interface is disabled or when the cable is unplugged the driver releases all allocated buffers by calling tbnet_free_buffers() for each ring. This function then calls dma_unmap_page() for each buffer it finds where bus address is non-zero. Now, we only clear this bus address when the Tx buffer is sent to the hardware so it is possible that the function finds an entry that has already been unmapped.

Enabling DMA-API debugging catches this as well:

thunderbolt 0000:06:00.0: DMA-API: device driver tries to free DMA

    memory it has not allocated [device address=0x0000000068321000] [size=4096 bytes]

Fix this by clearing the bus address of a Tx frame right after we have unmapped the buffer.

---
 drivers/net/thunderbolt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/thunderbolt.c +1 -0
index 435854688a7a..228d4aa6d9ae 100644
@@ -536,6 +536,7 @@ static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
 
 	dma_unmap_page(dma_dev, tf->frame.buffer_phy, tbnet_frame_size(tf),
 		       DMA_TO_DEVICE);
+	tf->frame.buffer_phy = 0;
 
 	/* Return buffer to the ring */
 	net->tx_ring.prod++;
Signature · 1 line
2.14.2
maintainer Applied
#1 lore raw

From: Mika Westerberg <[email protected]>
Date: Thu, 9 Nov 2017 13:46:28 +0300

Quoted 10 lines

When Thunderbolt network interface is disabled or when the cable is unplugged the driver releases all allocated buffers by calling tbnet_free_buffers() for each ring. This function then calls dma_unmap_page() for each buffer it finds where bus address is non-zero. Now, we only clear this bus address when the Tx buffer is sent to the hardware so it is possible that the function finds an entry that has already been unmapped.

Enabling DMA-API debugging catches this as well:

Quoted 7 lines

thunderbolt 0000:06:00.0: DMA-API: device driver tries to free DMA

    memory it has not allocated [device address=0x0000000068321000] [size=4096 bytes]

Fix this by clearing the bus address of a Tx frame right after we have unmapped the buffer.

Applied, but assuming zero is a non-valid DMA address is never a good idea. That's why we have the DMA error code signaling abstracted.

#2 lore raw
Quoted 16 lines · David Miller

From: Mika Westerberg <[email protected]>
Date: Thu, 9 Nov 2017 13:46:28 +0300

Quoted 13 lines

When Thunderbolt network interface is disabled or when the cable is unplugged the driver releases all allocated buffers by calling tbnet_free_buffers() for each ring. This function then calls dma_unmap_page() for each buffer it finds where bus address is non-zero. Now, we only clear this bus address when the Tx buffer is sent to the hardware so it is possible that the function finds an entry that has already been unmapped.

Enabling DMA-API debugging catches this as well:

thunderbolt 0000:06:00.0: DMA-API: device driver tries to free DMA

    memory it has not allocated [device address=0x0000000068321000] [size=4096 bytes]
Quoted 7 lines
Quoted 4 lines

Fix this by clearing the bus address of a Tx frame right after we have unmapped the buffer.

Applied, but assuming zero is a non-valid DMA address is never a good idea. That's why we have the DMA error code signaling abstracted.

There does not seem to be a way to mark DMA address invalid in a driver so we probably need to add a flag to struct tbnet_frame instead.

#3 lore raw

From: Mika Westerberg

Quoted 26 lines

Sent: 13 November 2017 10:22
To: David Miller
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net-next] net: thunderbolt: Clear finished Tx frame bus address in tbnet_tx_callback()

Quoted 19 lines · David Miller

From: Mika Westerberg <[email protected]>
Date: Thu, 9 Nov 2017 13:46:28 +0300

Quoted 16 lines

When Thunderbolt network interface is disabled or when the cable is unplugged the driver releases all allocated buffers by calling tbnet_free_buffers() for each ring. This function then calls dma_unmap_page() for each buffer it finds where bus address is non-zero. Now, we only clear this bus address when the Tx buffer is sent to the hardware so it is possible that the function finds an entry that has already been unmapped.

Enabling DMA-API debugging catches this as well:

thunderbolt 0000:06:00.0: DMA-API: device driver tries to free DMA

    memory it has not allocated [device address=0x0000000068321000] [size=4096 bytes]

Fix this by clearing the bus address of a Tx frame right after we have unmapped the buffer.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Applied, but assuming zero is a non-valid DMA address is never a good idea. That's why we have the DMA error code signaling abstracted.

There does not seem to be a way to mark DMA address invalid in a driver so we probably need to add a flag to struct tbnet_frame instead.

Can you use the length?

David

#4 lore raw
Quoted 4 lines · David Laight
Quoted 2 lines

There does not seem to be a way to mark DMA address invalid in a driver so we probably need to add a flag to struct tbnet_frame instead.

Can you use the length?

Unfortunately no because size is 12-bit field in struct ring_frame and 0 means 4096 bytes.