Skip to contentSkip to conversation

dmaengine: hsu: use kzalloc_flex()

PATCH Superseded Rosen Penev 3 participants 9 messages Mar 28 – Apr 14

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dmaengine: hsu: use kzalloc_flex()

Simplifies allocations by using a flexible array member in this struct.

Remove hsu_dma_alloc_desc(). It now offers no readability advantages in this single usage.

Add __counted_by to get extra runtime analysis.

Apply the exact same treatment to struct hsu_dma and devm_kzalloc.

---
v2: address review comments.

 drivers/dma/hsu/hsu.c | 45 ++++++++++++-------------------------------
 drivers/dma/hsu/hsu.h |  4 ++--
 2 files changed, 14 insertions(+), 35 deletions(-)
drivers/dma/hsu/hsu.c +12 -33
index f62d60d7bc6b..78a2352ada8c 100644
@@ -241,28 +241,10 @@ int hsu_dma_do_irq(struct hsu_dma_chip *chip, unsigned short nr, u32 status)
 }
 EXPORT_SYMBOL_GPL(hsu_dma_do_irq);
 
-static struct hsu_dma_desc *hsu_dma_alloc_desc(unsigned int nents)
-{
-	struct hsu_dma_desc *desc;
-
-	desc = kzalloc_obj(*desc, GFP_NOWAIT);
-	if (!desc)
-		return NULL;
-
-	desc->sg = kzalloc_objs(*desc->sg, nents, GFP_NOWAIT);
-	if (!desc->sg) {
-		kfree(desc);
-		return NULL;
-	}
-
-	return desc;
-}
-
 static void hsu_dma_desc_free(struct virt_dma_desc *vdesc)
 {
 	struct hsu_dma_desc *desc = to_hsu_dma_desc(vdesc);
 
-	kfree(desc->sg);
 	kfree(desc);
 }
 
@@ -276,10 +258,15 @@ static struct dma_async_tx_descriptor *hsu_dma_prep_slave_sg(
 	struct scatterlist *sg;
 	unsigned int i;
 
-	desc = hsu_dma_alloc_desc(sg_len);
+	desc = kzalloc_flex(*desc, sg, sg_len, GFP_NOWAIT);
 	if (!desc)
 		return NULL;
 
+	desc->nents = sg_len;
+	desc->direction = direction;
+	/* desc->active = 0 by kzalloc */
+	desc->status = DMA_IN_PROGRESS;
+
 	for_each_sg(sgl, sg, sg_len, i) {
 		desc->sg[i].addr = sg_dma_address(sg);
 		desc->sg[i].len = sg_dma_len(sg);
@@ -287,11 +274,6 @@ static struct dma_async_tx_descriptor *hsu_dma_prep_slave_sg(
 		desc->length += sg_dma_len(sg);
 	}
 
-	desc->nents = sg_len;
-	desc->direction = direction;
-	/* desc->active = 0 by kzalloc */
-	desc->status = DMA_IN_PROGRESS;
-
 	return vchan_tx_prep(&hsuc->vchan, &desc->vdesc, flags);
 }
 
@@ -428,22 +410,19 @@ int hsu_dma_probe(struct hsu_dma_chip *chip)
 {
 	struct hsu_dma *hsu;
 	void __iomem *addr = chip->regs + chip->offset;
+	unsigned short nr_channels;
 	unsigned short i;
 	int ret;
 
-	hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+	/* Calculate nr_channels from the IO space length */
+	nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+	hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
 	if (!hsu)
 		return -ENOMEM;
 
-	chip->hsu = hsu;
-
-	/* Calculate nr_channels from the IO space length */
-	hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+	hsu->nr_channels = nr_channels;
 
-	hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-				 sizeof(*hsu->chan), GFP_KERNEL);
-	if (!hsu->chan)
-		return -ENOMEM;
+	chip->hsu = hsu;
 
 	INIT_LIST_HEAD(&hsu->dma.channels);
 	for (i = 0; i < hsu->nr_channels; i++) {
drivers/dma/hsu/hsu.h +2 -2
index 3bca577b98a1..f6ca1014bccf 100644
@@ -71,11 +71,11 @@ struct hsu_dma_sg {
 struct hsu_dma_desc {
 	struct virt_dma_desc vdesc;
 	enum dma_transfer_direction direction;
-	struct hsu_dma_sg *sg;
 	unsigned int nents;
 	size_t length;
 	unsigned int active;
 	enum dma_status status;
+	struct hsu_dma_sg sg[] __counted_by(nents);
 };
 
 static inline struct hsu_dma_desc *to_hsu_dma_desc(struct virt_dma_desc *vdesc)
@@ -115,8 +115,8 @@ struct hsu_dma {
 	struct dma_device		dma;
 
 	/* channels */
-	struct hsu_dma_chan		*chan;
 	unsigned short			nr_channels;
+	struct hsu_dma_chan		chan[] __counted_by(nr_channels);
 };
 
 static inline struct hsu_dma *to_hsu_dma(struct dma_device *ddev)

--
2.53.0

#1 lore raw
Quoted 9 lines · Rosen Penev

Simplifies allocations by using a flexible array member in this struct.

Remove hsu_dma_alloc_desc(). It now offers no readability advantages in this single usage.

Add __counted_by to get extra runtime analysis.

Apply the exact same treatment to struct hsu_dma and devm_kzalloc.

We refer to the functions as func(): devm_kzalloc().

...

Quoted 8 lines
drivers/dma/hsu/hsu.c +3 -2
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;

drivers/dma/hsu/hsu.c int hsu_dma_probe(struct hsu_dma_chip *chip) · line 425

Quoted 10 lines
drivers/dma/hsu/hsu.c +2 -7
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
 
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 29 lines · Andy Shevchenko
Quoted 9 lines · Rosen Penev

Simplifies allocations by using a flexible array member in this struct.

Remove hsu_dma_alloc_desc(). It now offers no readability advantages in this single usage.

Add __counted_by to get extra runtime analysis.

Apply the exact same treatment to struct hsu_dma and devm_kzalloc.

We refer to the functions as func(): devm_kzalloc().

...

Quoted 13 lines
drivers/dma/hsu/hsu.c +4 -5
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
Quoted 8 lines

drivers/dma/hsu/hsu.c int hsu_dma_probe(struct hsu_dma_chip *chip) · line 425

Quoted 5 lines
drivers/dma/hsu/hsu.c +1 -4
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#3 lore raw
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
drivers/dma/hsu/hsu.c +5 -9
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
 
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;
Quoted 5 lines
Quoted 2 lines

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

--
With Best Regards,
Andy Shevchenko

#4 lore raw
Quoted 29 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
drivers/dma/hsu/hsu.c +3 -2
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;

drivers/dma/hsu/hsu.c int hsu_dma_probe(struct hsu_dma_chip *chip) · line 425

Quoted 10 lines
drivers/dma/hsu/hsu.c +2 -7
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
 
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

Quoted 8 lines
Quoted 3 lines

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

Not sure I follow. This patch changes hsu's chan member to a FAM. Where is VLA coming from?

The current code is devm_kzalloc(x, struct_size()). When it gets introduced, I'm sure there will be a treewide conversion to devm_kzalloc_flex, which would automatically set the counting variable for >=GCC15.

It's best practice to assign right after since kzalloc_flex does it anyways.

Quoted 10 lines

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

--
With Best Regards,
Andy Shevchenko

#5 lore raw
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
drivers/dma/hsu/hsu.c +4 -5
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
Quoted 8 lines

drivers/dma/hsu/hsu.c int hsu_dma_probe(struct hsu_dma_chip *chip) · line 425

Quoted 5 lines
drivers/dma/hsu/hsu.c +1 -4
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Quoted 6 lines
Quoted 4 lines

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

Not sure I follow. This patch changes hsu's chan member to a FAM. Where is VLA coming from?

VLA: variable-length array
FAM: flexible array member
The second one is VLA member + size member.

What your patch is doing is changing a pointer to VLA member.

Quoted 6 lines

The current code is devm_kzalloc(x, struct_size()). When it gets introduced, I'm sure there will be a treewide conversion to devm_kzalloc_flex, which would automatically set the counting variable for >=GCC15.

It's best practice to assign right after since kzalloc_flex does it anyways.

Still, I'm not convinced we should blindly follow this rule. The length needs to be known before accessing the VLA, but it's okay to access other members. Leaving hsu member assignment where it's now is fine, no need to move it around.

Quoted 5 lines
Quoted 5 lines

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 43 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
drivers/dma/hsu/hsu.c +4 -5
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
Quoted 8 lines

drivers/dma/hsu/hsu.c int hsu_dma_probe(struct hsu_dma_chip *chip) · line 425

Quoted 5 lines
drivers/dma/hsu/hsu.c +1 -4
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Quoted 6 lines
Quoted 4 lines

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

Not sure I follow. This patch changes hsu's chan member to a FAM. Where is VLA coming from?

VLA: variable-length array
FAM: flexible array member
The second one is VLA member + size member.

What your patch is doing is changing a pointer to VLA member.

Quoted 5 lines

The current code is devm_kzalloc(x, struct_size()). When it gets introduced, I'm sure there will be a treewide conversion to devm_kzalloc_flex, which would automatically set the counting variable for >=GCC15.

Quoted 12 lines
Quoted 1 line

It's best practice to assign right after since kzalloc_flex does it anyways.

Still, I'm not convinced we should blindly follow this rule. The length needs to be known before accessing the VLA, but it's okay to access other members. Leaving hsu member assignment where it's now is fine, no need to move it around.

Quoted 5 lines
Quoted 5 lines

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

If you agree with my reasoning, please send a v4, I will give you a tag.

Otherwise I really would like to understand the justification why the assignment going first is the best practice and how it may help the developer.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 65 lines · Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 53 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
(quoted diff) +4 -5
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +1 -4
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Quoted 6 lines
Quoted 4 lines

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

Not sure I follow. This patch changes hsu's chan member to a FAM. Where is VLA coming from?

VLA: variable-length array
FAM: flexible array member
The second one is VLA member + size member.

What your patch is doing is changing a pointer to VLA member.

Quoted 6 lines

The current code is devm_kzalloc(x, struct_size()). When it gets introduced, I'm sure there will be a treewide conversion to devm_kzalloc_flex, which would automatically set the counting variable for >=GCC15.

It's best practice to assign right after since kzalloc_flex does it anyways.

Still, I'm not convinced we should blindly follow this rule. The length needs to be known before accessing the VLA, but it's okay to access other members. Leaving hsu member assignment where it's now is fine, no need to move it around.

Quoted 3 lines
Quoted 3 lines

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

If you agree with my reasoning, please send a v4, I will give you a tag.

Otherwise I really would like to understand the justification why the assignment going first is the best practice and how it may help the developer.

Merge window is closed right now AFAIK.

Quoted 6 lines

--
With Best Regards,
Andy Shevchenko

#8 lore raw
Quoted 10 lines · Rosen Penev
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <[email protected]> wrote:

...

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
(quoted diff) +4 -5
-       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
+       /* Calculate nr_channels from the IO space length */
+       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
        if (!hsu)
                return -ENOMEM;
 
-       chip->hsu = hsu;
-
-       /* Calculate nr_channels from the IO space length */
-       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
+       hsu->nr_channels = nr_channels;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +1 -4
-       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
-                                sizeof(*hsu->chan), GFP_KERNEL);
-       if (!hsu->chan)
-               return -ENOMEM;
+       chip->hsu = hsu;

Don't know these _flex() APIs enough, but can we leave the chip->hsu = hsu; in the same place as it's now?

__counted_by requires the first assignment after allocation to be the counting variable. The _flex macros do this automatically for GCC15 and above.

Quoted 6 lines
Quoted 4 lines

Why? The hsu member has nothing to do with VLA, where is this requirement coming from? My understanding is that the check should imply the minimum sizeof of the data structure and the compiler should know that way before doing any allocations.

Not sure I follow. This patch changes hsu's chan member to a FAM. Where is VLA coming from?

VLA: variable-length array
FAM: flexible array member
The second one is VLA member + size member.

What your patch is doing is changing a pointer to VLA member.

Quoted 6 lines

The current code is devm_kzalloc(x, struct_size()). When it gets introduced, I'm sure there will be a treewide conversion to devm_kzalloc_flex, which would automatically set the counting variable for >=GCC15.

It's best practice to assign right after since kzalloc_flex does it anyways.

Still, I'm not convinced we should blindly follow this rule. The length needs to be known before accessing the VLA, but it's okay to access other members. Leaving hsu member assignment where it's now is fine, no need to move it around.

Quoted 3 lines
Quoted 3 lines

My understanding seems in align with what Gustavo blogged:
https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The same is written in the GCC patch description
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

If you agree with my reasoning, please send a v4, I will give you a tag.

Otherwise I really would like to understand the justification why the assignment going first is the best practice and how it may help the developer.

Merge window is closed right now AFAIK.

You mean opened. In any case this kind of patch can still be sent, it's just no time to review by the maintainers, but on the positive side while they are busy with other stuff all kind of CIs, test bots, AI reviews can be performed before the maintainer has a chance to look at this. Which saves their time as well.

Signature · 2 lines
With Best Regards,
Andy Shevchenko