[PATCH] power: supply: core: make power_supply_class constant
From: Ricardo B. Marliere
Date: Tue Feb 20 2024 - 17:41:05 EST
Since commit 43a7206b0963 ("driver core: class: make class_register() take
a const *"), the driver core allows for struct class to be in read-only
memory, so move the power_supply_class structure to be declared at build
time placing it into read-only memory, instead of having to be dynamically
allocated at boot time.
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Ricardo B. Marliere <ricardo@xxxxxxxxxxxx>
---
drivers/power/supply/power_supply_core.c | 28 ++++++++++++++++------------
include/linux/power_supply.h | 2 +-
2 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index ecef35ac3b7e..f13372e88589 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -26,7 +26,10 @@
#include "samsung-sdi-battery.h"
/* exported for the APM Power driver, APM emulation */
-struct class *power_supply_class;
+const struct class power_supply_class = {
+ .name = "power_supply",
+ .dev_uevent = power_supply_uevent,
+};
EXPORT_SYMBOL_GPL(power_supply_class);
static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);
@@ -93,7 +96,7 @@ static void power_supply_changed_work(struct work_struct *work)
if (likely(psy->changed)) {
psy->changed = false;
spin_unlock_irqrestore(&psy->changed_lock, flags);
- class_for_each_device(power_supply_class, NULL, psy,
+ class_for_each_device(&power_supply_class, NULL, psy,
__power_supply_changed_work);
power_supply_update_leds(psy);
blocking_notifier_call_chain(&power_supply_notifier,
@@ -329,7 +332,7 @@ int power_supply_am_i_supplied(struct power_supply *psy)
struct psy_am_i_supplied_data data = { psy, 0 };
int error;
- error = class_for_each_device(power_supply_class, NULL, &data,
+ error = class_for_each_device(&power_supply_class, NULL, &data,
__power_supply_am_i_supplied);
dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
@@ -365,7 +368,7 @@ int power_supply_is_system_supplied(void)
int error;
unsigned int count = 0;
- error = class_for_each_device(power_supply_class, NULL, &count,
+ error = class_for_each_device(&power_supply_class, NULL, &count,
__power_supply_is_system_supplied);
/*
@@ -412,7 +415,7 @@ int power_supply_get_property_from_supplier(struct power_supply *psy,
* This function is not intended for use with a supply with multiple
* suppliers, we simply pick the first supply to report the psp.
*/
- ret = class_for_each_device(power_supply_class, NULL, &data,
+ ret = class_for_each_device(&power_supply_class, NULL, &data,
__power_supply_get_supplier_property);
if (ret < 0)
return ret;
@@ -458,7 +461,7 @@ static int power_supply_match_device_by_name(struct device *dev, const void *dat
struct power_supply *power_supply_get_by_name(const char *name)
{
struct power_supply *psy = NULL;
- struct device *dev = class_find_device(power_supply_class, NULL, name,
+ struct device *dev = class_find_device(&power_supply_class, NULL, name,
power_supply_match_device_by_name);
if (dev) {
@@ -1369,7 +1372,7 @@ __power_supply_register(struct device *parent,
device_initialize(dev);
- dev->class = power_supply_class;
+ dev->class = &power_supply_class;
dev->type = &power_supply_dev_type;
dev->parent = parent;
dev->release = power_supply_dev_release;
@@ -1617,12 +1620,13 @@ EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
static int __init power_supply_class_init(void)
{
- power_supply_class = class_create("power_supply");
+ int err;
+
+ err = class_register(&power_supply_class);
- if (IS_ERR(power_supply_class))
- return PTR_ERR(power_supply_class);
+ if (err)
+ return err;
- power_supply_class->dev_uevent = power_supply_uevent;
power_supply_init_attrs(&power_supply_dev_type);
return 0;
@@ -1630,7 +1634,7 @@ static int __init power_supply_class_init(void)
static void __exit power_supply_class_exit(void)
{
- class_destroy(power_supply_class);
+ class_unregister(&power_supply_class);
}
subsys_initcall(power_supply_class_init);
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index c0992a77feea..514f652de64d 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -895,7 +895,7 @@ extern int power_supply_powers(struct power_supply *psy, struct device *dev);
extern void *power_supply_get_drvdata(struct power_supply *psy);
/* For APM emulation, think legacy userspace. */
-extern struct class *power_supply_class;
+extern const struct class power_supply_class;
static inline bool power_supply_is_amp_property(enum power_supply_property psp)
{
---
base-commit: a9b254892ce1a447b06c5019cbf0e9caeb48c138
change-id: 20240220-class_cleanup-power-14680219753d
Best regards,
--
Ricardo B. Marliere <ricardo@xxxxxxxxxxxx>