All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pm / wakeup: simplify the output logic of pm_show_wakelocks()
@ 2022-01-13 18:44 Greg Kroah-Hartman
  2022-01-14 10:10 ` Lee Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2022-01-13 18:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Pavel Machek, Len Brown,
	linux-pm, Lee Jones

The buffer handling in pm_show_wakelocks() is tricky, and hopefully
correct.  Ensure it really is correct by using sysfs_emit_at() which
handles all of the tricky string handling logic in a PAGE_SIZE buffer
for us automatically as this is a sysfs file being read from.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Len Brown <len.brown@intel.com>
Cc: linux-pm@vger.kernel.org
Cc: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 kernel/power/wakelock.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/kernel/power/wakelock.c b/kernel/power/wakelock.c
index 105df4dfc783..52571dcad768 100644
--- a/kernel/power/wakelock.c
+++ b/kernel/power/wakelock.c
@@ -39,23 +39,20 @@ ssize_t pm_show_wakelocks(char *buf, bool show_active)
 {
 	struct rb_node *node;
 	struct wakelock *wl;
-	char *str = buf;
-	char *end = buf + PAGE_SIZE;
+	int len = 0;
 
 	mutex_lock(&wakelocks_lock);
 
 	for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
 		wl = rb_entry(node, struct wakelock, node);
 		if (wl->ws->active == show_active)
-			str += scnprintf(str, end - str, "%s ", wl->name);
+			len += sysfs_emit_at(buf, len, "%s ", wl->name);
 	}
-	if (str > buf)
-		str--;
 
-	str += scnprintf(str, end - str, "\n");
+	len += sysfs_emit_at(buf, len, "\n");
 
 	mutex_unlock(&wakelocks_lock);
-	return (str - buf);
+	return len;
 }
 
 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] pm / wakeup: simplify the output logic of pm_show_wakelocks()
  2022-01-13 18:44 [PATCH] pm / wakeup: simplify the output logic of pm_show_wakelocks() Greg Kroah-Hartman
@ 2022-01-14 10:10 ` Lee Jones
  2022-01-25 17:28   ` Rafael J. Wysocki
  0 siblings, 1 reply; 3+ messages in thread
From: Lee Jones @ 2022-01-14 10:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-kernel, Rafael J. Wysocki, Pavel Machek, Len Brown, linux-pm

On Thu, 13 Jan 2022, Greg Kroah-Hartman wrote:

> The buffer handling in pm_show_wakelocks() is tricky, and hopefully
> correct.  Ensure it really is correct by using sysfs_emit_at() which
> handles all of the tricky string handling logic in a PAGE_SIZE buffer
> for us automatically as this is a sysfs file being read from.
> 
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Pavel Machek <pavel@ucw.cz>
> Cc: Len Brown <len.brown@intel.com>
> Cc: linux-pm@vger.kernel.org
> Cc: Lee Jones <lee.jones@linaro.org>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
>  kernel/power/wakelock.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)

Reviewed-by: Lee Jones <lee.jones@linaro.org>

> diff --git a/kernel/power/wakelock.c b/kernel/power/wakelock.c
> index 105df4dfc783..52571dcad768 100644
> --- a/kernel/power/wakelock.c
> +++ b/kernel/power/wakelock.c
> @@ -39,23 +39,20 @@ ssize_t pm_show_wakelocks(char *buf, bool show_active)
>  {
>  	struct rb_node *node;
>  	struct wakelock *wl;
> -	char *str = buf;
> -	char *end = buf + PAGE_SIZE;
> +	int len = 0;
>  
>  	mutex_lock(&wakelocks_lock);
>  
>  	for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
>  		wl = rb_entry(node, struct wakelock, node);
>  		if (wl->ws->active == show_active)
> -			str += scnprintf(str, end - str, "%s ", wl->name);
> +			len += sysfs_emit_at(buf, len, "%s ", wl->name);
>  	}
> -	if (str > buf)
> -		str--;
>  
> -	str += scnprintf(str, end - str, "\n");
> +	len += sysfs_emit_at(buf, len, "\n");
>  
>  	mutex_unlock(&wakelocks_lock);
> -	return (str - buf);
> +	return len;
>  }
>  
>  #if CONFIG_PM_WAKELOCKS_LIMIT > 0

-- 
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] pm / wakeup: simplify the output logic of pm_show_wakelocks()
  2022-01-14 10:10 ` Lee Jones
@ 2022-01-25 17:28   ` Rafael J. Wysocki
  0 siblings, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2022-01-25 17:28 UTC (permalink / raw)
  To: Lee Jones, Greg Kroah-Hartman
  Cc: Linux Kernel Mailing List, Rafael J. Wysocki, Pavel Machek,
	Len Brown, Linux PM

On Fri, Jan 14, 2022 at 11:10 AM Lee Jones <lee.jones@linaro.org> wrote:
>
> On Thu, 13 Jan 2022, Greg Kroah-Hartman wrote:
>
> > The buffer handling in pm_show_wakelocks() is tricky, and hopefully
> > correct.  Ensure it really is correct by using sysfs_emit_at() which
> > handles all of the tricky string handling logic in a PAGE_SIZE buffer
> > for us automatically as this is a sysfs file being read from.
> >
> > Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> > Cc: Pavel Machek <pavel@ucw.cz>
> > Cc: Len Brown <len.brown@intel.com>
> > Cc: linux-pm@vger.kernel.org
> > Cc: Lee Jones <lee.jones@linaro.org>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > ---
> >  kernel/power/wakelock.c | 11 ++++-------
> >  1 file changed, 4 insertions(+), 7 deletions(-)
>
> Reviewed-by: Lee Jones <lee.jones@linaro.org>

Applied as 5.18 material, thanks!

>
> > diff --git a/kernel/power/wakelock.c b/kernel/power/wakelock.c
> > index 105df4dfc783..52571dcad768 100644
> > --- a/kernel/power/wakelock.c
> > +++ b/kernel/power/wakelock.c
> > @@ -39,23 +39,20 @@ ssize_t pm_show_wakelocks(char *buf, bool show_active)
> >  {
> >       struct rb_node *node;
> >       struct wakelock *wl;
> > -     char *str = buf;
> > -     char *end = buf + PAGE_SIZE;
> > +     int len = 0;
> >
> >       mutex_lock(&wakelocks_lock);
> >
> >       for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
> >               wl = rb_entry(node, struct wakelock, node);
> >               if (wl->ws->active == show_active)
> > -                     str += scnprintf(str, end - str, "%s ", wl->name);
> > +                     len += sysfs_emit_at(buf, len, "%s ", wl->name);
> >       }
> > -     if (str > buf)
> > -             str--;
> >
> > -     str += scnprintf(str, end - str, "\n");
> > +     len += sysfs_emit_at(buf, len, "\n");
> >
> >       mutex_unlock(&wakelocks_lock);
> > -     return (str - buf);
> > +     return len;
> >  }
> >
> >  #if CONFIG_PM_WAKELOCKS_LIMIT > 0
>
> --
> Lee Jones [李琼斯]
> Principal Technical Lead - Developer Services
> Linaro.org │ Open source software for Arm SoCs
> Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-25 17:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13 18:44 [PATCH] pm / wakeup: simplify the output logic of pm_show_wakelocks() Greg Kroah-Hartman
2022-01-14 10:10 ` Lee Jones
2022-01-25 17:28   ` Rafael J. Wysocki

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.