Skip to contentSkip to conversation

EDAC: sb_edac: use kzalloc_flex

PATCH In review Rosen Penev 3 participants 8 messages Mar 11 – Mar 13

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

PATCH 1/1 EDAC: sb_edac: use kzalloc_flex

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

Add __counted_by to get extra runtime analysis.

---
 drivers/edac/sb_edac.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/edac/sb_edac.c +3 -11
index 09d4e816404b..7b282dfd093f 100644
@@ -364,11 +364,11 @@ struct sbridge_dev {
 	int			seg;
 	u8			bus, mc;
 	u8			node_id, source_id;
-	struct pci_dev		**pdev;
 	enum domain		dom;
 	int			n_devs;
 	int			i_devs;
 	struct mem_ctl_info	*mci;
+	struct pci_dev		*pdev[] __counted_by(n_devs);
 };
 
 struct knl_pvt {
@@ -771,21 +771,14 @@ static struct sbridge_dev *alloc_sbridge_dev(int seg, u8 bus, enum domain dom,
 {
 	struct sbridge_dev *sbridge_dev;
 
-	sbridge_dev = kzalloc_obj(*sbridge_dev);
+	sbridge_dev = kzalloc_flex(*sbridge_dev, pdev, table->n_devs_per_imc);
 	if (!sbridge_dev)
 		return NULL;
 
-	sbridge_dev->pdev = kzalloc_objs(*sbridge_dev->pdev,
-					 table->n_devs_per_imc);
-	if (!sbridge_dev->pdev) {
-		kfree(sbridge_dev);
-		return NULL;
-	}
-
+	sbridge_dev->n_devs = table->n_devs_per_imc;
 	sbridge_dev->seg = seg;
 	sbridge_dev->bus = bus;
 	sbridge_dev->dom = dom;
-	sbridge_dev->n_devs = table->n_devs_per_imc;
 	list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
 
 	return sbridge_dev;
@@ -794,7 +787,6 @@ static struct sbridge_dev *alloc_sbridge_dev(int seg, u8 bus, enum domain dom,
 static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
 {
 	list_del(&sbridge_dev->list);
-	kfree(sbridge_dev->pdev);
 	kfree(sbridge_dev);
 }
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw

+ sbridge_dev->n_devs = table->n_devs_per_imc;

Do you need this? I thought that kzalloc_flex() filled in the __counted_by field of the structure for you.

-Tony

#2 lore raw
Quoted 4 lines · Tony

+ sbridge_dev->n_devs = table->n_devs_per_imc;

Do you need this? I thought that kzalloc_flex() filled in the __counted_by field of the structure for you.

kzalloc_flex is just a macro over kzalloc(struct_size()). It does not do automatic __counted_by, which makes no sense.

Quoted 1 line

-Tony

maintainer
#3 lore raw
Quoted 5 lines
Quoted 3 lines

+ sbridge_dev->n_devs = table->n_devs_per_imc;

Do you need this? I thought that kzalloc_flex() filled in the __counted_by field of the structure for you.

kzalloc_flex is just a macro over kzalloc(struct_size()). It does not do automatic __counted_by, which makes no sense.

It is a fancy macro. But may be complier version dependent whether the count is filled in.

See include/linux/slab.h where kzalloc_flex() uses __alloc_flex() which looks like this:

#define __alloc_flex(KMALLOC, GFP, TYPE, FAM, COUNT)                    \
({                                                                      \
        const size_t __count = (COUNT);                                 \
        const size_t __obj_size = struct_size_t(TYPE, FAM, __count);    \
        TYPE *__obj_ptr = KMALLOC(__obj_size, GFP);                     \
        if (__obj_ptr)                                                  \
                __set_flex_counter(__obj_ptr->FAM, __count);            \
        __obj_ptr;                                                      \

})

See the __set_flex_counter() ... looks like it is trying to set the counted_by field.

-Tony

#4 lore raw
Quoted 20 lines · Tony
Quoted 6 lines
Quoted 4 lines

+ sbridge_dev->n_devs = table->n_devs_per_imc;

Do you need this? I thought that kzalloc_flex() filled in the __counted_by field of the structure for you.

kzalloc_flex is just a macro over kzalloc(struct_size()). It does not do automatic __counted_by, which makes no sense.

It is a fancy macro. But may be complier version dependent whether the count is filled in.

See include/linux/slab.h where kzalloc_flex() uses __alloc_flex() which looks like this:

#define __alloc_flex(KMALLOC, GFP, TYPE, FAM, COUNT)                    \
({                                                                      \
        const size_t __count = (COUNT);                                 \
        const size_t __obj_size = struct_size_t(TYPE, FAM, __count);    \
        TYPE *__obj_ptr = KMALLOC(__obj_size, GFP);                     \
        if (__obj_ptr)                                                  \
                __set_flex_counter(__obj_ptr->FAM, __count);            \
        __obj_ptr;                                                      \
Quoted 4 lines

})

See the __set_flex_counter() ... looks like it is trying to set the counted_by field.

/**
* __set_flex_counter() - Set the counter associated with the given flexible
* array member that has been annoated by __counted_by().
* @FAM: Instance of flexible array member within a given struct.
* @COUNT: Value to store to the __counted_by annotated @FAM_PTR's counter. *
* This is a no-op if no annotation exists. Count needs to be checked with
* overflows_flex_counter_type() before using this function.
*/

Quoted 4 lines

-Tony

#5 lore raw
In reply to Luck, Tony
Quoted 6 lines

From: Luck, Tony <[email protected]>
[...]
Subject: RE: [PATCH] EDAC: sb_edac: use kzalloc_flex

Quoted 2 lines
Quoted 2 lines

+ sbridge_dev->n_devs = table->n_devs_per_imc;

Quoted 7 lines
Quoted 1 line
Quoted 1 line

Do you need this? I thought that kzalloc_flex() filled in the __counted_by

field of the structure for you.

Quoted 2 lines

kzalloc_flex is just a macro over kzalloc(struct_size()). It does not do automatic __counted_by, which makes no sense.

It is a fancy macro. But may be complier version dependent whether the count is filled in.

Yes, " __builtin_counted_by_ref" needs gcc >= 15. See comments [1].
My gcc is v13.3 on Ubuntu 24.04, and I verified that "sbridge_dev->n_devs" was NOT filled in automatically [2].

For safety, we still need to fill in the count field explicitly.

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/compiler_types.h#n550

[2] --- a/drivers/edac/sb_edac.c
+++ b/drivers/edac/sb_edac.c
@@ -778,7 +778,8 @@ static struct sbridge_dev *alloc_sbridge_dev(int seg, u8 bus, enum domain dom,

        sbridge_dev->seg = seg;
        sbridge_dev->bus = bus;
        sbridge_dev->dom = dom;
-       sbridge_dev->n_devs = table->n_devs_per_imc;
+       //sbridge_dev->n_devs = table->n_devs_per_imc;
+       pr_info("sbridge_dev->n_devs = %d, table->n_devs_per_imc = %d\n", sbridge_dev->n_devs, table->n_devs_per_imc);

Testing result log:

[ 1640.072460] EDAC DEBUG: sbridge_init:
[ 1640.072474] EDAC sbridge: Seeking for: PCI ID 8086:3ca0
[ 1640.072505] sbridge_dev->n_devs = 0, table->n_devs_per_imc = 11     <------------------
[ 1640.072511] EDAC DEBUG: sbridge_get_onedevice: Detected 8086:3ca0
[ 1640.072515] EDAC sbridge: Seeking for: PCI ID 8086:3ca0
[ 1640.072530] sbridge_dev->n_devs = 0, table->n_devs_per_imc = 11     <------------------

...
Failed to load the sb_edac driver.

Reviewed-by on v1 1/1
#6 lore raw
In reply to Rosen Penev
Quoted 4 lines

From: Rosen Penev <[email protected]>
[...]
Subject: [PATCH] EDAC: sb_edac: use kzalloc_flex

Could you use this style of subject:

EDAC/sb: Use kzalloc_flex()

Quoted 35 lines

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

Add __counted_by to get extra runtime analysis.

---
 drivers/edac/sb_edac.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/edac/sb_edac.c index renamed from drivers/edac/sb_edac.c +0 -0

09d4e816404b..7b282dfd093f 100644

drivers/edac/sb_edac.c +2 -2
@@ -364,11 +364,11 @@ struct sbridge_dev {
 	int			seg;
 	u8			bus, mc;
 	u8			node_id, source_id;
-	struct pci_dev		**pdev;
 	enum domain		dom;
 	int			n_devs;
 	int			i_devs;
 	struct mem_ctl_info	*mci;
+	struct pci_dev		*pdev[] __counted_by(n_devs);
 };
 
 struct knl_pvt {
@@ -771,21 +771,14 @@ static struct sbridge_dev *alloc_sbridge_dev(int
 seg, u8 bus, enum domain dom,  {
 	struct sbridge_dev *sbridge_dev;
 
-	sbridge_dev = kzalloc_obj(*sbridge_dev);
+	sbridge_dev = kzalloc_flex(*sbridge_dev, pdev, table-
 >n_devs_per_imc);
 	if (!sbridge_dev)

drivers/edac/sb_edac.c static struct sbridge_dev *alloc_sbridge_dev(int · line 779

Quoted 10 lines
drivers/edac/sb_edac.c +1 -7
 		return NULL;
 
-	sbridge_dev->pdev = kzalloc_objs(*sbridge_dev->pdev,
-					 table->n_devs_per_imc);
-	if (!sbridge_dev->pdev) {
-		kfree(sbridge_dev);
-		return NULL;
-	}
-
+	sbridge_dev->n_devs = table->n_devs_per_imc;

What's the reason for moving this line of code up here?

drivers/edac/sb_edac.c static struct sbridge_dev *alloc_sbridge_dev(int seg, u8 bus, enum domain dom, · line 782

Quoted 6 lines
drivers/edac/sb_edac.c +0 -1
 	sbridge_dev->seg = seg;
 	sbridge_dev->bus = bus;
 	sbridge_dev->dom = dom;
-	sbridge_dev->n_devs = table->n_devs_per_imc;
 	list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
[...]

Other than the comments above:

#7 lore raw
Quoted 9 lines · Qiuxu
Quoted 5 lines

From: Rosen Penev <[email protected]>
[...]
Subject: [PATCH] EDAC: sb_edac: use kzalloc_flex

Could you use this style of subject:

EDAC/sb: Use kzalloc_flex()

Will do.

Quoted 47 lines
Quoted 36 lines

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

Add __counted_by to get extra runtime analysis.

---
 drivers/edac/sb_edac.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/edac/sb_edac.c index renamed from drivers/edac/sb_edac.c +0 -0

09d4e816404b..7b282dfd093f 100644

drivers/edac/sb_edac.c +2 -2
@@ -364,11 +364,11 @@ struct sbridge_dev {
      int                     seg;
      u8                      bus, mc;
      u8                      node_id, source_id;
-     struct pci_dev          **pdev;
      enum domain             dom;
      int                     n_devs;
      int                     i_devs;
      struct mem_ctl_info     *mci;
+     struct pci_dev          *pdev[] __counted_by(n_devs);
 };
 
 struct knl_pvt {
@@ -771,21 +771,14 @@ static struct sbridge_dev *alloc_sbridge_dev(int
 seg, u8 bus, enum domain dom,  {
      struct sbridge_dev *sbridge_dev;
 
-     sbridge_dev = kzalloc_obj(*sbridge_dev);
+     sbridge_dev = kzalloc_flex(*sbridge_dev, pdev, table-
 >n_devs_per_imc);
      if (!sbridge_dev)

drivers/edac/sb_edac.c static struct sbridge_dev *alloc_sbridge_dev(int · line 779

Quoted 10 lines
drivers/edac/sb_edac.c +1 -7
              return NULL;
 
-     sbridge_dev->pdev = kzalloc_objs(*sbridge_dev->pdev,
-                                      table->n_devs_per_imc);
-     if (!sbridge_dev->pdev) {
-             kfree(sbridge_dev);
-             return NULL;
-     }
-
+     sbridge_dev->n_devs = table->n_devs_per_imc;
Quoted 1 line

What's the reason for moving this line of code up here?

__counted_by requires setting the counting variable first.

Quoted 11 lines

drivers/edac/sb_edac.c static struct sbridge_dev *alloc_sbridge_dev(int seg, u8 bus, enum domain dom, · line 782

Quoted 7 lines
drivers/edac/sb_edac.c +0 -1
      sbridge_dev->seg = seg;
      sbridge_dev->bus = bus;
      sbridge_dev->dom = dom;
-     sbridge_dev->n_devs = table->n_devs_per_imc;
      list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
[...]

Other than the comments above: