[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAMRc=MewoxcijL_OYi=LwWMJmYCSsYFQ2j+koOF5b2_w8VyGsg@mail.gmail.com>
Date: Fri, 2 Jan 2026 07:25:54 -0600
From: Bartosz Golaszewski <brgl@...nel.org>
To: Shivendra Pratap <shivendra.pratap@....qualcomm.com>
Cc: linux-kernel@...r.kernel.org, linux-arm-msm@...r.kernel.org,
linux-pm@...r.kernel.org, Bartosz Golaszewski <brgl@...ev.pl>,
Sebastian Reichel <sre@...nel.org>, Bartosz Golaszewski <bgolasze@...cinc.com>,
Bjorn Andersson <andersson@...nel.org>
Subject: Re: [PATCH v22 2/2] power: reset: reboot-mode: Expose sysfs for
registered reboot_modes
On Fri, 26 Dec 2025 19:56:34 +0100, Shivendra Pratap
<shivendra.pratap@....qualcomm.com> said:
> Currently, there is no standardized mechanism for userspace to discover
> which reboot-modes are supported on a given platform. This limitation
> forces tools and scripts to rely on hardcoded assumptions about the
> supported reboot-modes.
>
> Create a class 'reboot-mode' and a device under it to expose a sysfs
> interface to show the available reboot mode arguments to userspace. Use
> the driver_name field of the struct reboot_mode_driver to create the
> device. For device-based drivers, configure the device driver name as
> driver_name.
>
> This results in the creation of:
> /sys/class/reboot-mode/<driver>/reboot_modes
>
> This read-only sysfs file will exposes the list of supported reboot
> modes arguments provided by the driver, enabling userspace to query the
> list of arguments.
>
> Signed-off-by: Shivendra Pratap <shivendra.pratap@....qualcomm.com>
> ---
> drivers/power/reset/reboot-mode.c | 139 +++++++++++++++++++++++++++++++++++++-
> include/linux/reboot-mode.h | 1 +
> 2 files changed, 137 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/power/reset/reboot-mode.c b/drivers/power/reset/reboot-mode.c
> index fba53f638da04655e756b5f8b7d2d666d1379535..96d0201697a539c6d048dac021db97e4e3063366 100644
> --- a/drivers/power/reset/reboot-mode.c
> +++ b/drivers/power/reset/reboot-mode.c
> @@ -6,10 +6,12 @@
> #include <linux/device.h>
> #include <linux/init.h>
> #include <linux/kernel.h>
> +#include <linux/list.h>
> #include <linux/module.h>
> #include <linux/of.h>
> #include <linux/reboot.h>
> #include <linux/reboot-mode.h>
> +#include <linux/slab.h>
>
> #define PREFIX "mode-"
>
> @@ -19,6 +21,42 @@ struct mode_info {
> struct list_head list;
> };
>
> +struct sysfs_data {
Let's make this more descriptive? struct reboot_mode_sysfs_data?
> + const char *mode;
> + struct list_head list;
> +};
> +
> +static ssize_t reboot_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
> +{
> + struct sysfs_data *sysfs_info;
> + struct list_head *head;
> + ssize_t size = 0;
> +
> + head = dev_get_drvdata(dev);
> + if (!head)
> + return -ENODATA;
> +
> + list_for_each_entry(sysfs_info, head, list)
> + size += sysfs_emit_at(buf, size, "%s ", sysfs_info->mode);
> +
> + if (!size)
> + return -ENODATA;
> +
> + return size + sysfs_emit_at(buf, size - 1, "\n");
> +}
> +static DEVICE_ATTR_RO(reboot_modes);
> +
> +static struct attribute *reboot_mode_attrs[] = {
> + &dev_attr_reboot_modes.attr,
> + NULL,
> +};
> +ATTRIBUTE_GROUPS(reboot_mode);
> +
> +static const struct class reboot_mode_class = {
> + .name = "reboot-mode",
> + .dev_groups = reboot_mode_groups,
> +};
> +
> static unsigned int get_reboot_mode_magic(struct reboot_mode_driver *reboot,
> const char *cmd)
> {
> @@ -62,6 +100,61 @@ static int reboot_mode_notify(struct notifier_block *this,
> return NOTIFY_DONE;
> }
>
> +static int reboot_mode_create_device(struct reboot_mode_driver *reboot)
> +{
> + struct sysfs_data *sysfs_info;
> + struct sysfs_data *next;
> + struct list_head *head;
> + struct mode_info *info;
> + int ret;
> +
> + head = kzalloc(sizeof(*head), GFP_KERNEL);
> + if (!head) {
> + ret = -ENOMEM;
> + goto error;
> + }
> +
> + INIT_LIST_HEAD(head);
> +
> + list_for_each_entry(info, &reboot->head, list) {
> + sysfs_info = kzalloc(sizeof(*sysfs_info), GFP_KERNEL);
> + if (!sysfs_info) {
> + ret = -ENOMEM;
> + goto error;
> + }
> +
> + sysfs_info->mode = kstrdup_const(info->mode, GFP_KERNEL);
> + if (!sysfs_info->mode) {
> + kfree(sysfs_info);
> + ret = -ENOMEM;
> + goto error;
> + }
> +
> + list_add_tail(&sysfs_info->list, head);
> + }
> +
> + reboot->reboot_mode_device = device_create(&reboot_mode_class, NULL, 0,
> + (void *)head, reboot->dev->driver->name);
No, why pass the list? You should create an instance of struct sysfs_data per
device_create(). If it needs to contain a list, then let it contain a list but
don't allocate the list_head, that's really unusual.
> +
> + if (IS_ERR(reboot->reboot_mode_device)) {
> + ret = PTR_ERR(reboot->reboot_mode_device);
> + goto error;
> + }
> +
> + return 0;
> +
> +error:
> + list_for_each_entry_safe(sysfs_info, next, head, list) {
> + list_del(&sysfs_info->list);
> + kfree_const(sysfs_info->mode);
> + kfree(sysfs_info);
> + }
> +
> + kfree(head);
> + reboot->reboot_mode_device = NULL;
> + return ret;
> +}
> +
> /**
> * reboot_mode_register - register a reboot mode driver
> * @reboot: reboot mode driver
> @@ -113,16 +206,39 @@ int reboot_mode_register(struct reboot_mode_driver *reboot)
> reboot->reboot_notifier.notifier_call = reboot_mode_notify;
> register_reboot_notifier(&reboot->reboot_notifier);
>
> + ret = reboot_mode_create_device(reboot);
> + if (ret)
> + goto error;
> +
> return 0;
>
> error:
> - list_for_each_entry(info, &reboot->head, list)
> - kfree_const(info->mode);
> -
> + reboot_mode_unregister(reboot);
> return ret;
> }
> EXPORT_SYMBOL_GPL(reboot_mode_register);
>
> +static inline void reboot_mode_unregister_device(struct reboot_mode_driver *reboot)
> +{
> + struct sysfs_data *sysfs_info;
> + struct sysfs_data *next;
> + struct list_head *head;
> +
> + head = dev_get_drvdata(reboot->reboot_mode_device);
> + device_unregister(reboot->reboot_mode_device);
> + reboot->reboot_mode_device = NULL;
> +
> + if (head) {
> + list_for_each_entry_safe(sysfs_info, next, head, list) {
> + list_del(&sysfs_info->list);
> + kfree_const(sysfs_info->mode);
> + kfree(sysfs_info);
> + }
This loop is duplicated, can you please factor it out into a dedicated
function?
> + }
> +
> + kfree(head);
> +}
> +
> /**
> * reboot_mode_unregister - unregister a reboot mode driver
> * @reboot: reboot mode driver
> @@ -131,7 +247,11 @@ int reboot_mode_unregister(struct reboot_mode_driver *reboot)
> {
> struct mode_info *info;
>
> + if (!reboot->reboot_mode_device)
> + return -ENODEV;
> +
> unregister_reboot_notifier(&reboot->reboot_notifier);
> + reboot_mode_unregister_device(reboot);
>
> list_for_each_entry(info, &reboot->head, list)
> kfree_const(info->mode);
> @@ -199,6 +319,19 @@ void devm_reboot_mode_unregister(struct device *dev,
> }
> EXPORT_SYMBOL_GPL(devm_reboot_mode_unregister);
>
> +static int __init reboot_mode_init(void)
> +{
> + return class_register(&reboot_mode_class);
> +}
> +
> +static void __exit reboot_mode_exit(void)
> +{
> + class_unregister(&reboot_mode_class);
> +}
> +
> +subsys_initcall(reboot_mode_init);
> +module_exit(reboot_mode_exit);
> +
> MODULE_AUTHOR("Andy Yan <andy.yan@...k-chips.com>");
> MODULE_DESCRIPTION("System reboot mode core library");
> MODULE_LICENSE("GPL v2");
> diff --git a/include/linux/reboot-mode.h b/include/linux/reboot-mode.h
> index 4a2abb38d1d612ec0fdf05eb18c98b210f631b7f..b56783c32068096325f92445b9530d1856c4826c 100644
> --- a/include/linux/reboot-mode.h
> +++ b/include/linux/reboot-mode.h
> @@ -5,6 +5,7 @@
> struct reboot_mode_driver {
> struct device *dev;
> struct list_head head;
> + struct device *reboot_mode_device;
Why can't this be part of struct (reboot_mode_)sysfs_data?
Bart
> int (*write)(struct reboot_mode_driver *reboot, unsigned int magic);
> struct notifier_block reboot_notifier;
> };
>
> --
> 2.34.1
>
>
Powered by blists - more mailing lists