Skip to contentSkip to conversation

mfd: viperboard: remove redundant NULL check before kfree()

PATCH Applied [email protected] 3 participants 3 messages Jul 3 – Jul 9 Multifunction devices (MFD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: viperboard: remove redundant NULL check before kfree()

From: Mohammad Shahid <[email protected]>

kfree() safely handles NULL pointers, so the explicit NULL check before calling kfree() is unnecessary.

This issue was reported by ifnullfree.cocci.

---
 drivers/mfd/viperboard.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/mfd/viperboard.c +1 -2
index 888737b8e7be..36be8e69f45c 100644
@@ -96,8 +96,7 @@ static int vprbrd_probe(struct usb_interface *interface,
 	return 0;
 
 error:
-	if (vb)
-		kfree(vb);
+	kfree(vb);
 
 	return ret;
 }
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] DMA cacheline sharing memory corruption on struct vprbrd --

commit 226dd61211b7aeb126296ed3bc673a04067d3fee
Author: Mohammad Shahid <[email protected]>

mfd: viperboard: remove redundant NULL check before kfree()

This patch cleans up the error path in vprbrd_probe() by removing an unnecessary NULL check before calling kfree(). Since kfree() safely handles NULL pointers, the explicit check can be safely removed.

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 99

Quoted 11 lines
drivers/mfd/viperboard.c +1 -2
index 888737b8e7be8..36be8e69f45c3 100644
@@ -96,8 +96,7 @@ static int vprbrd_probe(struct usb_interface *interface,
 	return 0;
 
 error:
-	if (vb)
-		kfree(vb);
+	kfree(vb);

[Severity: High]
This is a pre-existing issue and was not introduced by the patch under review, but while reviewing this cleanup, I noticed a potential DMA cacheline sharing issue with the vb structure being freed here.

In include/linux/mfd/viperboard.h, struct vprbrd embeds a buffer directly between a mutex and a platform_device:

struct vprbrd {
	...
	struct mutex lock;
	u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
	struct platform_device pdev;
	...
};

During vprbrd_probe() in drivers/mfd/viperboard.c, this unaligned buffer is passed directly to usb_control_msg() for DMA transfers:

	ret = usb_control_msg(vb->usb_dev, pipe, VPRBRD_USB_REQUEST_MAJOR,
		VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, vb->buf, 1,
		VPRBRD_USB_TIMEOUT_MS);

Because buf is not explicitly cacheline-aligned (for example, using ____cacheline_aligned), it shares a cacheline with the preceding lock and the following pdev.

On non-coherent DMA architectures, when the DMA API invalidates the CPU cache lines covering buf during a receive transfer, it could discard concurrent modifications to the lock or pdev.

Can this memory layout cause silent memory corruption or deadlocks if another thread modifies vb->lock while a DMA receive operation like this one, or the runtime transfers in drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_receive(), are in flight?

Quoted 3 lines
drivers/mfd/viperboard.c +0 -0
 	return ret;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw

Re: (subset) [PATCH] mfd: viperboard: remove redundant NULL check before kfree()

In reply to [email protected]
Quoted 4 lines · On Fri, 03 Jul 2026 16:29:37 +0530, [email protected] wrote:

kfree() safely handles NULL pointers, so the explicit NULL check before calling kfree() is unnecessary.

This issue was reported by ifnullfree.cocci.

Applied, thanks!

[1/1] mfd: viperboard: remove redundant NULL check before kfree()

      commit: cf9775233b4d1618dfac5e84009f36eba1e7fddc

--
Lee Jones [李琼斯]