2023-09-05 16:19:40

by Naresh Solanki

[permalink] [raw]
Subject: [PATCH 2/3] regulator: userspace-consumer: Add regulator event support

Add sysfs attribute to track regulator events received from regulator
notifier block handler.

Signed-off-by: Naresh Solanki <[email protected]>
---
drivers/regulator/userspace-consumer.c | 54 +++++++++++++++++++++++++-
1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
index 97f075ed68c9..a936661d99cd 100644
--- a/drivers/regulator/userspace-consumer.c
+++ b/drivers/regulator/userspace-consumer.c
@@ -29,6 +29,10 @@ struct userspace_consumer_data {

int num_supplies;
struct regulator_bulk_data *supplies;
+
+ struct kobject *kobj;
+ struct notifier_block nb;
+ unsigned long events;
};

static ssize_t name_show(struct device *dev,
@@ -89,12 +93,30 @@ static ssize_t state_store(struct device *dev, struct device_attribute *attr,
return count;
}

+static DEFINE_SPINLOCK(events_lock);
+
+static ssize_t events_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct userspace_consumer_data *data = dev_get_drvdata(dev);
+ unsigned long e;
+
+ spin_lock(&events_lock);
+ e = data->events;
+ data->events = 0;
+ spin_unlock(&events_lock);
+
+ return sprintf(buf, "0x%lx\n", e);
+}
+
static DEVICE_ATTR_RO(name);
static DEVICE_ATTR_RW(state);
+static DEVICE_ATTR_RO(events);

static struct attribute *attributes[] = {
&dev_attr_name.attr,
&dev_attr_state.attr,
+ &dev_attr_events.attr,
NULL,
};

@@ -115,12 +137,30 @@ static const struct attribute_group attr_group = {
.is_visible = attr_visible,
};

+static int regulator_userspace_notify(struct notifier_block *nb,
+ unsigned long event,
+ void *ignored)
+{
+ struct userspace_consumer_data *data =
+ container_of(nb, struct userspace_consumer_data, nb);
+ static const char * const *envp[] = { "NAME=events", NULL };
+
+ spin_lock(&events_lock);
+ data->events |= event;
+ spin_unlock(&events_lock);
+
+ sysfs_notify(data->kobj, NULL, dev_attr_events.attr.name);
+ kobject_uevent_env(data->kobj, KOBJ_CHANGE, envp);
+
+ return NOTIFY_OK;
+}
+
static int regulator_userspace_consumer_probe(struct platform_device *pdev)
{
struct regulator_userspace_consumer_data tmpdata;
struct regulator_userspace_consumer_data *pdata;
struct userspace_consumer_data *drvdata;
- int ret;
+ int i, ret;

pdata = dev_get_platdata(&pdev->dev);
if (!pdata) {
@@ -153,6 +193,7 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
drvdata->num_supplies = pdata->num_supplies;
drvdata->supplies = pdata->supplies;
drvdata->no_autoswitch = pdata->no_autoswitch;
+ drvdata->kobj = &pdev->dev.kobj;

mutex_init(&drvdata->lock);

@@ -186,6 +227,13 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
}
drvdata->enabled = !!ret;

+ drvdata->nb.notifier_call = regulator_userspace_notify;
+ for (i = 0; i < drvdata->num_supplies; i++) {
+ ret = devm_regulator_register_notifier(drvdata->supplies[i].consumer, &drvdata->nb);
+ if (ret)
+ goto err_enable;
+ }
+
return 0;

err_enable:
@@ -197,6 +245,10 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
static int regulator_userspace_consumer_remove(struct platform_device *pdev)
{
struct userspace_consumer_data *data = platform_get_drvdata(pdev);
+ int i;
+
+ for (i = 0; i < data->num_supplies; i++)
+ devm_regulator_unregister_notifier(data->supplies[i].consumer, &data->nb);

sysfs_remove_group(&pdev->dev.kobj, &attr_group);

--
2.41.0


2023-09-05 16:19:44

by Zev Weiss

[permalink] [raw]
Subject: Re: [PATCH 2/3] regulator: userspace-consumer: Add regulator event support

On Thu, Aug 31, 2023 at 05:14:09AM PDT, Naresh Solanki wrote:
>Add sysfs attribute to track regulator events received from regulator
>notifier block handler.
>
>Signed-off-by: Naresh Solanki <[email protected]>
>---
> drivers/regulator/userspace-consumer.c | 54 +++++++++++++++++++++++++-
> 1 file changed, 53 insertions(+), 1 deletion(-)
>
>diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
>index 97f075ed68c9..a936661d99cd 100644
>--- a/drivers/regulator/userspace-consumer.c
>+++ b/drivers/regulator/userspace-consumer.c
>@@ -29,6 +29,10 @@ struct userspace_consumer_data {
>
> int num_supplies;
> struct regulator_bulk_data *supplies;
>+
>+ struct kobject *kobj;
>+ struct notifier_block nb;
>+ unsigned long events;
> };
>
> static ssize_t name_show(struct device *dev,
>@@ -89,12 +93,30 @@ static ssize_t state_store(struct device *dev, struct device_attribute *attr,
> return count;
> }
>
>+static DEFINE_SPINLOCK(events_lock);
>+
>+static ssize_t events_show(struct device *dev,
>+ struct device_attribute *attr, char *buf)
>+{
>+ struct userspace_consumer_data *data = dev_get_drvdata(dev);
>+ unsigned long e;
>+
>+ spin_lock(&events_lock);
>+ e = data->events;
>+ data->events = 0;

I still don't think this is a good solution for the problem.

I for one frequently examine things in sysfs using shell commands like
'cat' and 'grep' and such, and I suspect I'm (very, very) far from alone
in that. With this design a user doing that could cause a monitoring
daemon to miss events that it was expecting to receive via this file.

I don't think we should be creating sysfs files that are secretly land
mines that allow a curious user innocently peeking around in sysfs doing
(they think) read-only operations to break things for other programs
using those files.


Zev