diff mbox series

[v4,1/1] power: Emit changed uevent on wakeup_sysfs_add/remove

Message ID 20200707092406.v4.1.I51f5a0be89595b73c4dc17e6cf4cc6f26dc7f2fc@changeid (mailing list archive)
State Superseded
Headers show
Series power: Emit change uevent when updating sysfs | expand

Commit Message

Abhishek Pandit-Subedi July 7, 2020, 4:24 p.m. UTC
Udev rules that depend on the power/wakeup attribute don't get triggered
correctly if device_set_wakeup_capable is called after the device is
created. This can happen for several reasons (driver sets wakeup after
device is created, wakeup is changed on parent device, etc) and it seems
reasonable to emit a changed event when adding or removing attributes on
the device.

Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
---

Changes in v4:
- Fix warning where returning from void and tested on device

Changes in v3:
- Simplified error handling

Changes in v2:
- Add newline at end of bt_dev_err

 drivers/base/power/sysfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Abhishek Pandit-Subedi July 7, 2020, 4:26 p.m. UTC | #1
Built and tested on Chromebook w/ 5.4 kernel.

Sorry about the churn -- will start building with warnings = errors
before I send patches upstream.

Thanks
Abhishek

On Tue, Jul 7, 2020 at 9:24 AM Abhishek Pandit-Subedi
<abhishekpandit@chromium.org> wrote:
>
> Udev rules that depend on the power/wakeup attribute don't get triggered
> correctly if device_set_wakeup_capable is called after the device is
> created. This can happen for several reasons (driver sets wakeup after
> device is created, wakeup is changed on parent device, etc) and it seems
> reasonable to emit a changed event when adding or removing attributes on
> the device.
>
> Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> ---
>
> Changes in v4:
> - Fix warning where returning from void and tested on device
>
> Changes in v3:
> - Simplified error handling
>
> Changes in v2:
> - Add newline at end of bt_dev_err
>
>  drivers/base/power/sysfs.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
> index 24d25cf8ab1487..aeb58d40aac8de 100644
> --- a/drivers/base/power/sysfs.c
> +++ b/drivers/base/power/sysfs.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /* sysfs entries for device PM */
>  #include <linux/device.h>
> +#include <linux/kobject.h>
>  #include <linux/string.h>
>  #include <linux/export.h>
>  #include <linux/pm_qos.h>
> @@ -739,12 +740,18 @@ int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
>
>  int wakeup_sysfs_add(struct device *dev)
>  {
> -       return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> +       int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> +
> +       if (ret)
> +               return ret;
> +
> +       return kobject_uevent(&dev->kobj, KOBJ_CHANGE);
>  }
>
>  void wakeup_sysfs_remove(struct device *dev)
>  {
>         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
> +       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
>  }
>
>  int pm_qos_sysfs_add_resume_latency(struct device *dev)
> --
> 2.27.0.212.ge8ba1cc988-goog
>
Rafael J. Wysocki July 7, 2020, 4:28 p.m. UTC | #2
On Tue, Jul 7, 2020 at 6:24 PM Abhishek Pandit-Subedi
<abhishekpandit@chromium.org> wrote:
>
> Udev rules that depend on the power/wakeup attribute don't get triggered
> correctly if device_set_wakeup_capable is called after the device is
> created. This can happen for several reasons (driver sets wakeup after
> device is created, wakeup is changed on parent device, etc) and it seems
> reasonable to emit a changed event when adding or removing attributes on
> the device.
>
> Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> ---
>
> Changes in v4:
> - Fix warning where returning from void and tested on device
>
> Changes in v3:
> - Simplified error handling
>
> Changes in v2:
> - Add newline at end of bt_dev_err
>
>  drivers/base/power/sysfs.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
> index 24d25cf8ab1487..aeb58d40aac8de 100644
> --- a/drivers/base/power/sysfs.c
> +++ b/drivers/base/power/sysfs.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /* sysfs entries for device PM */
>  #include <linux/device.h>
> +#include <linux/kobject.h>
>  #include <linux/string.h>
>  #include <linux/export.h>
>  #include <linux/pm_qos.h>
> @@ -739,12 +740,18 @@ int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
>
>  int wakeup_sysfs_add(struct device *dev)
>  {
> -       return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> +       int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> +
> +       if (ret)
> +               return ret;
> +
> +       return kobject_uevent(&dev->kobj, KOBJ_CHANGE);

So let me repeat the previous comment:

If you return an error here, it may confuse the caller to think that
the operation has failed completely, whereas the merging of the
attribute group has been successful already.

I don't think that an error can be returned at this point.

>  }
>
>  void wakeup_sysfs_remove(struct device *dev)
>  {
>         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
> +       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
>  }
>
>  int pm_qos_sysfs_add_resume_latency(struct device *dev)
> --
> 2.27.0.212.ge8ba1cc988-goog
>
Abhishek Pandit-Subedi July 7, 2020, 4:47 p.m. UTC | #3
Hi Rafael,

(resent in plain text)

On Tue, Jul 7, 2020 at 9:28 AM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Tue, Jul 7, 2020 at 6:24 PM Abhishek Pandit-Subedi
> <abhishekpandit@chromium.org> wrote:
> >
> > Udev rules that depend on the power/wakeup attribute don't get triggered
> > correctly if device_set_wakeup_capable is called after the device is
> > created. This can happen for several reasons (driver sets wakeup after
> > device is created, wakeup is changed on parent device, etc) and it seems
> > reasonable to emit a changed event when adding or removing attributes on
> > the device.
> >
> > Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> > ---
> >
> > Changes in v4:
> > - Fix warning where returning from void and tested on device
> >
> > Changes in v3:
> > - Simplified error handling
> >
> > Changes in v2:
> > - Add newline at end of bt_dev_err
> >
> >  drivers/base/power/sysfs.c | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
> > index 24d25cf8ab1487..aeb58d40aac8de 100644
> > --- a/drivers/base/power/sysfs.c
> > +++ b/drivers/base/power/sysfs.c
> > @@ -1,6 +1,7 @@
> >  // SPDX-License-Identifier: GPL-2.0
> >  /* sysfs entries for device PM */
> >  #include <linux/device.h>
> > +#include <linux/kobject.h>
> >  #include <linux/string.h>
> >  #include <linux/export.h>
> >  #include <linux/pm_qos.h>
> > @@ -739,12 +740,18 @@ int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
> >
> >  int wakeup_sysfs_add(struct device *dev)
> >  {
> > -       return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > +       int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > +
> > +       if (ret)
> > +               return ret;
> > +
> > +       return kobject_uevent(&dev->kobj, KOBJ_CHANGE);
>
> So let me repeat the previous comment:
>
> If you return an error here, it may confuse the caller to think that
> the operation has failed completely, whereas the merging of the
> attribute group has been successful already.
>
> I don't think that an error can be returned at this point.
>

The caller looks at the return code and just logs that an error
occurred (no other action). It's also unlikely for kobject_uevent to
fail (I saw mostly -ENOMEM and an -ENOENT when the kobj wasn't in the
correct set).

Call site:
    int ret = wakeup_sysfs_add(dev);

    if (ret)
        dev_info(dev, "Wakeup sysfs attributes not added\n");

So I'm ok with either keeping this as-is (caller isn't getting
confused, just logging) or swallowing the return of kobject_uevent.

> >  }
> >
> >  void wakeup_sysfs_remove(struct device *dev)
> >  {
> >         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
> > +       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
> >  }
> >
> >  int pm_qos_sysfs_add_resume_latency(struct device *dev)
> > --
> > 2.27.0.212.ge8ba1cc988-goog
> >
Rafael J. Wysocki July 7, 2020, 5:16 p.m. UTC | #4
On Tue, Jul 7, 2020 at 6:48 PM Abhishek Pandit-Subedi
<abhishekpandit@chromium.org> wrote:
>
> Hi Rafael,
>
> (resent in plain text)
>
> On Tue, Jul 7, 2020 at 9:28 AM Rafael J. Wysocki <rafael@kernel.org> wrote:
> >
> > On Tue, Jul 7, 2020 at 6:24 PM Abhishek Pandit-Subedi
> > <abhishekpandit@chromium.org> wrote:
> > >
> > > Udev rules that depend on the power/wakeup attribute don't get triggered
> > > correctly if device_set_wakeup_capable is called after the device is
> > > created. This can happen for several reasons (driver sets wakeup after
> > > device is created, wakeup is changed on parent device, etc) and it seems
> > > reasonable to emit a changed event when adding or removing attributes on
> > > the device.
> > >
> > > Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> > > ---
> > >
> > > Changes in v4:
> > > - Fix warning where returning from void and tested on device
> > >
> > > Changes in v3:
> > > - Simplified error handling
> > >
> > > Changes in v2:
> > > - Add newline at end of bt_dev_err
> > >
> > >  drivers/base/power/sysfs.c | 9 ++++++++-
> > >  1 file changed, 8 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
> > > index 24d25cf8ab1487..aeb58d40aac8de 100644
> > > --- a/drivers/base/power/sysfs.c
> > > +++ b/drivers/base/power/sysfs.c
> > > @@ -1,6 +1,7 @@
> > >  // SPDX-License-Identifier: GPL-2.0
> > >  /* sysfs entries for device PM */
> > >  #include <linux/device.h>
> > > +#include <linux/kobject.h>
> > >  #include <linux/string.h>
> > >  #include <linux/export.h>
> > >  #include <linux/pm_qos.h>
> > > @@ -739,12 +740,18 @@ int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
> > >
> > >  int wakeup_sysfs_add(struct device *dev)
> > >  {
> > > -       return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > +       int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > +
> > > +       if (ret)
> > > +               return ret;
> > > +
> > > +       return kobject_uevent(&dev->kobj, KOBJ_CHANGE);
> >
> > So let me repeat the previous comment:
> >
> > If you return an error here, it may confuse the caller to think that
> > the operation has failed completely, whereas the merging of the
> > attribute group has been successful already.
> >
> > I don't think that an error can be returned at this point.
> >
>
> The caller looks at the return code and just logs that an error
> occurred (no other action). It's also unlikely for kobject_uevent to
> fail (I saw mostly -ENOMEM and an -ENOENT when the kobj wasn't in the
> correct set).
>
> Call site:
>     int ret = wakeup_sysfs_add(dev);
>
>     if (ret)
>         dev_info(dev, "Wakeup sysfs attributes not added\n");

Yes, which is confusing, because the wakeup attributes may in fact
have been added.  Which is my point.

>
> So I'm ok with either keeping this as-is (caller isn't getting
> confused, just logging) or swallowing the return of kobject_uevent.

I would just ignore the return value of kobject_uevent() along the
lines of wakeup_sysfs_remove() below.

Thanks!

> > >  }
> > >
> > >  void wakeup_sysfs_remove(struct device *dev)
> > >  {
> > >         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > +       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
> > >  }
> > >
> > >  int pm_qos_sysfs_add_resume_latency(struct device *dev)
> > > --
> > > 2.27.0.212.ge8ba1cc988-goog
> > >
Abhishek Pandit-Subedi July 7, 2020, 5:18 p.m. UTC | #5
Sounds good to me. Patch v5 incoming after compile-test.

Thanks
Abhishek

On Tue, Jul 7, 2020 at 10:16 AM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Tue, Jul 7, 2020 at 6:48 PM Abhishek Pandit-Subedi
> <abhishekpandit@chromium.org> wrote:
> >
> > Hi Rafael,
> >
> > (resent in plain text)
> >
> > On Tue, Jul 7, 2020 at 9:28 AM Rafael J. Wysocki <rafael@kernel.org> wrote:
> > >
> > > On Tue, Jul 7, 2020 at 6:24 PM Abhishek Pandit-Subedi
> > > <abhishekpandit@chromium.org> wrote:
> > > >
> > > > Udev rules that depend on the power/wakeup attribute don't get triggered
> > > > correctly if device_set_wakeup_capable is called after the device is
> > > > created. This can happen for several reasons (driver sets wakeup after
> > > > device is created, wakeup is changed on parent device, etc) and it seems
> > > > reasonable to emit a changed event when adding or removing attributes on
> > > > the device.
> > > >
> > > > Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> > > > ---
> > > >
> > > > Changes in v4:
> > > > - Fix warning where returning from void and tested on device
> > > >
> > > > Changes in v3:
> > > > - Simplified error handling
> > > >
> > > > Changes in v2:
> > > > - Add newline at end of bt_dev_err
> > > >
> > > >  drivers/base/power/sysfs.c | 9 ++++++++-
> > > >  1 file changed, 8 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
> > > > index 24d25cf8ab1487..aeb58d40aac8de 100644
> > > > --- a/drivers/base/power/sysfs.c
> > > > +++ b/drivers/base/power/sysfs.c
> > > > @@ -1,6 +1,7 @@
> > > >  // SPDX-License-Identifier: GPL-2.0
> > > >  /* sysfs entries for device PM */
> > > >  #include <linux/device.h>
> > > > +#include <linux/kobject.h>
> > > >  #include <linux/string.h>
> > > >  #include <linux/export.h>
> > > >  #include <linux/pm_qos.h>
> > > > @@ -739,12 +740,18 @@ int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
> > > >
> > > >  int wakeup_sysfs_add(struct device *dev)
> > > >  {
> > > > -       return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > > +       int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > > +
> > > > +       if (ret)
> > > > +               return ret;
> > > > +
> > > > +       return kobject_uevent(&dev->kobj, KOBJ_CHANGE);
> > >
> > > So let me repeat the previous comment:
> > >
> > > If you return an error here, it may confuse the caller to think that
> > > the operation has failed completely, whereas the merging of the
> > > attribute group has been successful already.
> > >
> > > I don't think that an error can be returned at this point.
> > >
> >
> > The caller looks at the return code and just logs that an error
> > occurred (no other action). It's also unlikely for kobject_uevent to
> > fail (I saw mostly -ENOMEM and an -ENOENT when the kobj wasn't in the
> > correct set).
> >
> > Call site:
> >     int ret = wakeup_sysfs_add(dev);
> >
> >     if (ret)
> >         dev_info(dev, "Wakeup sysfs attributes not added\n");
>
> Yes, which is confusing, because the wakeup attributes may in fact
> have been added.  Which is my point.
>
> >
> > So I'm ok with either keeping this as-is (caller isn't getting
> > confused, just logging) or swallowing the return of kobject_uevent.
>
> I would just ignore the return value of kobject_uevent() along the
> lines of wakeup_sysfs_remove() below.
>
> Thanks!
>
> > > >  }
> > > >
> > > >  void wakeup_sysfs_remove(struct device *dev)
> > > >  {
> > > >         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
> > > > +       kobject_uevent(&dev->kobj, KOBJ_CHANGE);
> > > >  }
> > > >
> > > >  int pm_qos_sysfs_add_resume_latency(struct device *dev)
> > > > --
> > > > 2.27.0.212.ge8ba1cc988-goog
> > > >
diff mbox series

Patch

diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
index 24d25cf8ab1487..aeb58d40aac8de 100644
--- a/drivers/base/power/sysfs.c
+++ b/drivers/base/power/sysfs.c
@@ -1,6 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0
 /* sysfs entries for device PM */
 #include <linux/device.h>
+#include <linux/kobject.h>
 #include <linux/string.h>
 #include <linux/export.h>
 #include <linux/pm_qos.h>
@@ -739,12 +740,18 @@  int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
 
 int wakeup_sysfs_add(struct device *dev)
 {
-	return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
+	int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
+
+	if (ret)
+		return ret;
+
+	return kobject_uevent(&dev->kobj, KOBJ_CHANGE);
 }
 
 void wakeup_sysfs_remove(struct device *dev)
 {
 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
+	kobject_uevent(&dev->kobj, KOBJ_CHANGE);
 }
 
 int pm_qos_sysfs_add_resume_latency(struct device *dev)