Commit bea1bfd5 authored by Robert Richter's avatar Robert Richter Committed by Borislav Petkov

EDAC/mc: Change mci device removal to use put_device()

There are dimm and csrow devices linked to the mci device esp. to show
up in sysfs. It must be granted that children devices are removed before
its mci parent. Thus, the release functions must be called in the
correct order and may not miss any child before releasing its parent. In
the current implementation this is only granted by the correct order of
release functions.

A much better approach is to use put_device() that releases the device
only after all users are gone. It is the recommended way to release a
device and free its memory. The function uses the device's refcount and
only frees it if there are no users of it anymore such as children.

So implement a mci_release() function to remove mci devices, use
put_device() to free them and early initialize the mci device right
after its struct has been allocated.

Change the release function so that it can be universally used no
matter if the device is registered or not. Since subsequent dimm
and csrow sysfs links are implemented as children devices, their
refcounts will keep the parent mci device from being removed as long
as sysfs entries exist and until all users have been unregistered in
edac_remove_sysfs_mci_device().

Remove edac_unregister_sysfs() and merge mci sysfs removal into
edac_remove_sysfs_mci_device(). There is only a single instance now that
removes the sysfs entries. The function can now be used in the error
paths for cleanup.

Also, create device release functions for all involved devices
(dev->release), remove device_type release functions (dev_type->
release) and also use dev->init_name instead of dev_set_name().

 [ bp: Massage commit message and comments. ]
Signed-off-by: default avatarRobert Richter <rrichter@marvell.com>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Acked-by: default avatarAristeu Rozanski <aris@redhat.com>
Link: https://lkml.kernel.org/r/20200212120340.4764-5-rrichter@marvell.com
parent 11a48a5a
...@@ -278,6 +278,12 @@ void *edac_align_ptr(void **p, unsigned int size, int n_elems) ...@@ -278,6 +278,12 @@ void *edac_align_ptr(void **p, unsigned int size, int n_elems)
static void _edac_mc_free(struct mem_ctl_info *mci) static void _edac_mc_free(struct mem_ctl_info *mci)
{ {
put_device(&mci->dev);
}
static void mci_release(struct device *dev)
{
struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);
struct csrow_info *csr; struct csrow_info *csr;
int i, chn, row; int i, chn, row;
...@@ -371,6 +377,9 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num, ...@@ -371,6 +377,9 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
if (mci == NULL) if (mci == NULL)
return NULL; return NULL;
mci->dev.release = mci_release;
device_initialize(&mci->dev);
/* Adjust pointers so they point within the memory we just allocated /* Adjust pointers so they point within the memory we just allocated
* rather than an imaginary chunk of memory located at address 0. * rather than an imaginary chunk of memory located at address 0.
*/ */
...@@ -505,9 +514,6 @@ void edac_mc_free(struct mem_ctl_info *mci) ...@@ -505,9 +514,6 @@ void edac_mc_free(struct mem_ctl_info *mci)
{ {
edac_dbg(1, "\n"); edac_dbg(1, "\n");
if (device_is_registered(&mci->dev))
edac_unregister_sysfs(mci);
_edac_mc_free(mci); _edac_mc_free(mci);
} }
EXPORT_SYMBOL_GPL(edac_mc_free); EXPORT_SYMBOL_GPL(edac_mc_free);
......
...@@ -274,14 +274,8 @@ static const struct attribute_group *csrow_attr_groups[] = { ...@@ -274,14 +274,8 @@ static const struct attribute_group *csrow_attr_groups[] = {
NULL NULL
}; };
static void csrow_attr_release(struct device *dev)
{
/* release device with _edac_mc_free() */
}
static const struct device_type csrow_attr_type = { static const struct device_type csrow_attr_type = {
.groups = csrow_attr_groups, .groups = csrow_attr_groups,
.release = csrow_attr_release,
}; };
/* /*
...@@ -387,6 +381,14 @@ static const struct attribute_group *csrow_dev_groups[] = { ...@@ -387,6 +381,14 @@ static const struct attribute_group *csrow_dev_groups[] = {
NULL NULL
}; };
static void csrow_release(struct device *dev)
{
/*
* Nothing to do, just unregister sysfs here. The mci
* device owns the data and will also release it.
*/
}
static inline int nr_pages_per_csrow(struct csrow_info *csrow) static inline int nr_pages_per_csrow(struct csrow_info *csrow)
{ {
int chan, nr_pages = 0; int chan, nr_pages = 0;
...@@ -405,6 +407,7 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci, ...@@ -405,6 +407,7 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci,
csrow->dev.type = &csrow_attr_type; csrow->dev.type = &csrow_attr_type;
csrow->dev.groups = csrow_dev_groups; csrow->dev.groups = csrow_dev_groups;
csrow->dev.release = csrow_release;
device_initialize(&csrow->dev); device_initialize(&csrow->dev);
csrow->dev.parent = &mci->dev; csrow->dev.parent = &mci->dev;
csrow->mci = mci; csrow->mci = mci;
...@@ -441,9 +444,7 @@ static int edac_create_csrow_objects(struct mem_ctl_info *mci) ...@@ -441,9 +444,7 @@ static int edac_create_csrow_objects(struct mem_ctl_info *mci)
error: error:
for (--i; i >= 0; i--) { for (--i; i >= 0; i--) {
csrow = mci->csrows[i]; if (device_is_registered(&mci->csrows[i]->dev))
if (!nr_pages_per_csrow(csrow))
continue;
device_unregister(&mci->csrows[i]->dev); device_unregister(&mci->csrows[i]->dev);
} }
...@@ -453,15 +454,13 @@ static int edac_create_csrow_objects(struct mem_ctl_info *mci) ...@@ -453,15 +454,13 @@ static int edac_create_csrow_objects(struct mem_ctl_info *mci)
static void edac_delete_csrow_objects(struct mem_ctl_info *mci) static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
{ {
int i; int i;
struct csrow_info *csrow;
for (i = mci->nr_csrows - 1; i >= 0; i--) { for (i = 0; i < mci->nr_csrows; i++) {
csrow = mci->csrows[i]; if (device_is_registered(&mci->csrows[i]->dev))
if (!nr_pages_per_csrow(csrow))
continue;
device_unregister(&mci->csrows[i]->dev); device_unregister(&mci->csrows[i]->dev);
} }
} }
#endif #endif
/* /*
...@@ -602,16 +601,18 @@ static const struct attribute_group *dimm_attr_groups[] = { ...@@ -602,16 +601,18 @@ static const struct attribute_group *dimm_attr_groups[] = {
NULL NULL
}; };
static void dimm_attr_release(struct device *dev)
{
/* release device with _edac_mc_free() */
}
static const struct device_type dimm_attr_type = { static const struct device_type dimm_attr_type = {
.groups = dimm_attr_groups, .groups = dimm_attr_groups,
.release = dimm_attr_release,
}; };
static void dimm_release(struct device *dev)
{
/*
* Nothing to do, just unregister sysfs here. The mci
* device owns the data and will also release it.
*/
}
/* Create a DIMM object under specifed memory controller device */ /* Create a DIMM object under specifed memory controller device */
static int edac_create_dimm_object(struct mem_ctl_info *mci, static int edac_create_dimm_object(struct mem_ctl_info *mci,
struct dimm_info *dimm) struct dimm_info *dimm)
...@@ -620,6 +621,7 @@ static int edac_create_dimm_object(struct mem_ctl_info *mci, ...@@ -620,6 +621,7 @@ static int edac_create_dimm_object(struct mem_ctl_info *mci,
dimm->mci = mci; dimm->mci = mci;
dimm->dev.type = &dimm_attr_type; dimm->dev.type = &dimm_attr_type;
dimm->dev.release = dimm_release;
device_initialize(&dimm->dev); device_initialize(&dimm->dev);
dimm->dev.parent = &mci->dev; dimm->dev.parent = &mci->dev;
...@@ -884,14 +886,8 @@ static const struct attribute_group *mci_attr_groups[] = { ...@@ -884,14 +886,8 @@ static const struct attribute_group *mci_attr_groups[] = {
NULL NULL
}; };
static void mci_attr_release(struct device *dev)
{
/* release device with _edac_mc_free() */
}
static const struct device_type mci_attr_type = { static const struct device_type mci_attr_type = {
.groups = mci_attr_groups, .groups = mci_attr_groups,
.release = mci_attr_release,
}; };
/* /*
...@@ -910,8 +906,6 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci, ...@@ -910,8 +906,6 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
/* get the /sys/devices/system/edac subsys reference */ /* get the /sys/devices/system/edac subsys reference */
mci->dev.type = &mci_attr_type; mci->dev.type = &mci_attr_type;
device_initialize(&mci->dev);
mci->dev.parent = mci_pdev; mci->dev.parent = mci_pdev;
mci->dev.groups = groups; mci->dev.groups = groups;
dev_set_name(&mci->dev, "mc%d", mci->mc_idx); dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
...@@ -921,7 +915,7 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci, ...@@ -921,7 +915,7 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
err = device_add(&mci->dev); err = device_add(&mci->dev);
if (err < 0) { if (err < 0) {
edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev)); edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
put_device(&mci->dev); /* no put_device() here, free mci with _edac_mc_free() */
return err; return err;
} }
...@@ -937,24 +931,20 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci, ...@@ -937,24 +931,20 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
err = edac_create_dimm_object(mci, dimm); err = edac_create_dimm_object(mci, dimm);
if (err) if (err)
goto fail_unregister_dimm; goto fail;
} }
#ifdef CONFIG_EDAC_LEGACY_SYSFS #ifdef CONFIG_EDAC_LEGACY_SYSFS
err = edac_create_csrow_objects(mci); err = edac_create_csrow_objects(mci);
if (err < 0) if (err < 0)
goto fail_unregister_dimm; goto fail;
#endif #endif
edac_create_debugfs_nodes(mci); edac_create_debugfs_nodes(mci);
return 0; return 0;
fail_unregister_dimm: fail:
mci_for_each_dimm(mci, dimm) { edac_remove_sysfs_mci_device(mci);
if (device_is_registered(&dimm->dev))
device_unregister(&dimm->dev);
}
device_unregister(&mci->dev);
return err; return err;
} }
...@@ -966,6 +956,9 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci) ...@@ -966,6 +956,9 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
{ {
struct dimm_info *dimm; struct dimm_info *dimm;
if (!device_is_registered(&mci->dev))
return;
edac_dbg(0, "\n"); edac_dbg(0, "\n");
#ifdef CONFIG_EDAC_DEBUG #ifdef CONFIG_EDAC_DEBUG
...@@ -976,17 +969,14 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci) ...@@ -976,17 +969,14 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
#endif #endif
mci_for_each_dimm(mci, dimm) { mci_for_each_dimm(mci, dimm) {
if (dimm->nr_pages == 0) if (!device_is_registered(&dimm->dev))
continue; continue;
edac_dbg(1, "unregistering device %s\n", dev_name(&dimm->dev)); edac_dbg(1, "unregistering device %s\n", dev_name(&dimm->dev));
device_unregister(&dimm->dev); device_unregister(&dimm->dev);
} }
}
void edac_unregister_sysfs(struct mem_ctl_info *mci) /* only remove the device, but keep mci */
{ device_del(&mci->dev);
edac_dbg(1, "unregistering device %s\n", dev_name(&mci->dev));
device_unregister(&mci->dev);
} }
static void mc_attr_release(struct device *dev) static void mc_attr_release(struct device *dev)
...@@ -1000,9 +990,6 @@ static void mc_attr_release(struct device *dev) ...@@ -1000,9 +990,6 @@ static void mc_attr_release(struct device *dev)
kfree(dev); kfree(dev);
} }
static const struct device_type mc_attr_type = {
.release = mc_attr_release,
};
/* /*
* Init/exit code for the module. Basically, creates/removes /sys/class/rc * Init/exit code for the module. Basically, creates/removes /sys/class/rc
*/ */
...@@ -1015,11 +1002,10 @@ int __init edac_mc_sysfs_init(void) ...@@ -1015,11 +1002,10 @@ int __init edac_mc_sysfs_init(void)
return -ENOMEM; return -ENOMEM;
mci_pdev->bus = edac_get_sysfs_subsys(); mci_pdev->bus = edac_get_sysfs_subsys();
mci_pdev->type = &mc_attr_type; mci_pdev->release = mc_attr_release;
device_initialize(mci_pdev); mci_pdev->init_name = "mc";
dev_set_name(mci_pdev, "mc");
err = device_add(mci_pdev); err = device_register(mci_pdev);
if (err < 0) { if (err < 0) {
edac_dbg(1, "failure: create device %s\n", dev_name(mci_pdev)); edac_dbg(1, "failure: create device %s\n", dev_name(mci_pdev));
put_device(mci_pdev); put_device(mci_pdev);
......
...@@ -28,7 +28,6 @@ void edac_mc_sysfs_exit(void); ...@@ -28,7 +28,6 @@ void edac_mc_sysfs_exit(void);
extern int edac_create_sysfs_mci_device(struct mem_ctl_info *mci, extern int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
const struct attribute_group **groups); const struct attribute_group **groups);
extern void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci); extern void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci);
void edac_unregister_sysfs(struct mem_ctl_info *mci);
extern int edac_get_log_ue(void); extern int edac_get_log_ue(void);
extern int edac_get_log_ce(void); extern int edac_get_log_ce(void);
extern int edac_get_panic_on_ue(void); extern int edac_get_panic_on_ue(void);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment