EDAC: sb_edac: use kzalloc_flex
Subsystems (2)
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
@@ -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
Luck, Tony
maintainer+ 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
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_byfield 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
Luck, Tony
maintainerQuoted 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_byfield of the structure for you.
kzalloc_flexis 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
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_byfield of the structure for you.
kzalloc_flexis 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.hwherekzalloc_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 thecounted_byfield.
/**
* __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
Quoted 6 lines
From: Luck, Tony <[email protected]>
[...]
Subject: RE: [PATCH] EDAC:sb_edac: usekzalloc_flexQuoted 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_byfield of the structure for you.
Quoted 2 lines
kzalloc_flexis 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.
[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.
Quoted 4 lines
From: Rosen Penev <[email protected]>
[...]
Subject: [PATCH] EDAC:sb_edac: usekzalloc_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_byto get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>--- 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..7b282dfd093f100644drivers/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
-7return 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
-1sbridge_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:
Quoted 9 lines · Qiuxu
Quoted 5 lines
From: Rosen Penev <[email protected]>
[...]
Subject: [PATCH] EDAC:sb_edac: usekzalloc_flexCould 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_byto get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>--- 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..7b282dfd093f100644drivers/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
-7return 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
-1sbridge_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:
Reviewed-by Qiuxu Zhuo <[email protected]>