[PATCH] thunderbolt: dma_port: kmalloc_array + kzalloc to flex

Rosen Penev posted 1 patch 3 weeks, 3 days ago
drivers/thunderbolt/dma_port.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
[PATCH] thunderbolt: dma_port: kmalloc_array + kzalloc to flex
Posted by Rosen Penev 3 weeks, 3 days ago
Use a single allocation with a flexible array member. Simplifies
allocation and freeing.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/thunderbolt/dma_port.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/drivers/thunderbolt/dma_port.c b/drivers/thunderbolt/dma_port.c
index 334fefe21255..c7c2942fa7be 100644
--- a/drivers/thunderbolt/dma_port.c
+++ b/drivers/thunderbolt/dma_port.c
@@ -55,7 +55,7 @@ struct tb_dma_port {
 	struct tb_switch *sw;
 	u8 port;
 	u32 base;
-	u8 *buf;
+	u8 buf[];
 };
 
 /*
@@ -209,16 +209,10 @@ struct tb_dma_port *dma_port_alloc(struct tb_switch *sw)
 	if (port < 0)
 		return NULL;
 
-	dma = kzalloc_obj(*dma);
+	dma = kzalloc_flex(*dma, buf, MAIL_DATA_DWORDS);
 	if (!dma)
 		return NULL;
 
-	dma->buf = kmalloc_array(MAIL_DATA_DWORDS, sizeof(u32), GFP_KERNEL);
-	if (!dma->buf) {
-		kfree(dma);
-		return NULL;
-	}
-
 	dma->sw = sw;
 	dma->port = port;
 	dma->base = DMA_PORT_CAP;
@@ -232,10 +226,7 @@ struct tb_dma_port *dma_port_alloc(struct tb_switch *sw)
  */
 void dma_port_free(struct tb_dma_port *dma)
 {
-	if (dma) {
-		kfree(dma->buf);
-		kfree(dma);
-	}
+	kfree(dma);
 }
 
 static int dma_port_wait_for_completion(struct tb_dma_port *dma,
-- 
2.53.0
Re: [PATCH] thunderbolt: dma_port: kmalloc_array + kzalloc to flex
Posted by Mika Westerberg 2 weeks, 5 days ago
On Fri, Mar 13, 2026 at 02:41:37PM -0700, Rosen Penev wrote:
> Use a single allocation with a flexible array member. Simplifies
> allocation and freeing.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Applied to thunderbolt.git/next, thanks!