Linux ACPI
 help / color / mirror / Atom feed
* [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths
@ 2026-12-04 16:17 Rafael J. Wysocki
  2026-12-04 16:18 ` [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value Rafael J. Wysocki
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Rafael J. Wysocki @ 2026-12-04 16:17 UTC (permalink / raw)
  To: Linux ACPI; +Cc: Andy Shevchenko, Zhang Rui, Sakari Ailus

Hi All,

This fixes a possible issue with returning an acpi_status value to
sysfs callers on errors which would then take it mistakenly as the
number of bytes written into the buffer (patch [1/2]) and introduces
a __free for ACPI object paths and uses it to simplify code in a few
places (patch [2/2]).

Thanks!




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

* [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value
  2026-12-04 16:17 [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Rafael J. Wysocki
@ 2026-12-04 16:18 ` Rafael J. Wysocki
  2026-12-04 17:37   ` Andy Shevchenko
  2026-12-04 16:20 ` [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path() Rafael J. Wysocki
  2026-12-04 17:41 ` [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Andy Shevchenko
  2 siblings, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2026-12-04 16:18 UTC (permalink / raw)
  To: Linux ACPI; +Cc: Andy Shevchenko, Zhang Rui, Sakari Ailus

From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>

Since acpi_get_name() returns an acpi_status which is a positive
number on failure, its return value should not be returned to the
caller by acpi_object_path().

Make acpi_object_path() return -ENODATA if acpi_get_name() fails.

Fixes: e49bd2dd5a50 ("ACPI: use PNPID:instance_no as bus_id of ACPI device")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/device_sysfs.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

--- a/drivers/acpi/device_sysfs.c
+++ b/drivers/acpi/device_sysfs.c
@@ -23,9 +23,8 @@ static ssize_t acpi_object_path(acpi_han
 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
 	int result;
 
-	result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
-	if (result)
-		return result;
+	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &path)))
+		return -ENODATA;
 
 	result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
 	kfree(path.pointer);




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

* [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path()
  2026-12-04 16:17 [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Rafael J. Wysocki
  2026-12-04 16:18 ` [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value Rafael J. Wysocki
@ 2026-12-04 16:20 ` Rafael J. Wysocki
  2026-12-04 17:40   ` Andy Shevchenko
  2026-12-04 17:41 ` [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Andy Shevchenko
  2 siblings, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2026-12-04 16:20 UTC (permalink / raw)
  To: Linux ACPI; +Cc: Andy Shevchenko, Zhang Rui, Sakari Ailus

From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>

Introduce acpi_path_free for auto-freeing ACPI object paths returned
by acpi_handle_path() and adjust the latter to return a const char
pointer because that's what all of its users assign it to.  While at
it, clean up the acpi_handle_path() function body a bit.

Use acpi_path_free in acpi_handle_printk() and __acpi_handle_debug()
that already call acpi_handle_path() and make acpi_object_path()
call acpi_handle_path() and use acpi_path_free to reduce code
duplication.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/device_sysfs.c |   10 +++-------
 drivers/acpi/utils.c        |   19 +++++++++----------
 include/linux/acpi.h        |    5 ++++-
 3 files changed, 16 insertions(+), 18 deletions(-)

--- a/drivers/acpi/device_sysfs.c
+++ b/drivers/acpi/device_sysfs.c
@@ -20,15 +20,11 @@
 
 static ssize_t acpi_object_path(acpi_handle handle, char *buf)
 {
-	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
-	int result;
-
-	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &path)))
+	const char *path __free(acpi_path_free) = acpi_handle_path(handle);
+	if (!path)
 		return -ENODATA;
 
-	result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
-	kfree(path.pointer);
-	return result;
+	return sysfs_emit(buf, "%s\n", path);
 }
 
 struct acpi_data_node_attr {
--- a/drivers/acpi/utils.c
+++ b/drivers/acpi/utils.c
@@ -599,18 +599,21 @@ EXPORT_SYMBOL(acpi_evaluate_ost);
  * acpi_handle_path: Return the object path of handle
  * @handle: ACPI device handle
  *
- * Caller must free the returned buffer
+ * Caller must free the returned buffer or use acpi_path_free.
  */
-char *acpi_handle_path(acpi_handle handle)
+const char *acpi_handle_path(acpi_handle handle)
 {
 	struct acpi_buffer buffer = {
 		.length = ACPI_ALLOCATE_BUFFER,
 		.pointer = NULL
 	};
 
-	if (in_interrupt() ||
-	    acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
+	if (in_interrupt())
 		return NULL;
+
+	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
+		return NULL;
+
 	return buffer.pointer;
 }
 
@@ -628,19 +631,17 @@ char *acpi_handle_path(acpi_handle handl
 void
 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
 {
+	const char *path __free(acpi_path_free) = acpi_handle_path(handle);
 	struct va_format vaf;
 	va_list args;
-	const char *path;
 
 	va_start(args, fmt);
 	vaf.fmt = fmt;
 	vaf.va = &args;
 
-	path = acpi_handle_path(handle);
 	printk("%sACPI: %s: %pV", level, path ? path : "<n/a>", &vaf);
 
 	va_end(args);
-	kfree(path);
 }
 EXPORT_SYMBOL(acpi_handle_printk);
 
@@ -660,19 +661,17 @@ void
 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
 		    const char *fmt, ...)
 {
+	const char *path __free(acpi_path_free) = acpi_handle_path(handle);
 	struct va_format vaf;
 	va_list args;
-	const char *path;
 
 	va_start(args, fmt);
 	vaf.fmt = fmt;
 	vaf.va = &args;
 
-	path = acpi_handle_path(handle);
 	__dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
 
 	va_end(args);
-	kfree(path);
 }
 EXPORT_SYMBOL(__acpi_handle_debug);
 #endif
--- a/include/linux/acpi.h
+++ b/include/linux/acpi.h
@@ -1280,7 +1280,10 @@ static inline void acpi_ec_set_gpe_wake_
 #endif
 
 #ifdef CONFIG_ACPI
-char *acpi_handle_path(acpi_handle handle);
+DEFINE_FREE(acpi_path_free, const char *, if (_T) ACPI_FREE((void *)_T));
+
+const char *acpi_handle_path(acpi_handle handle);
+
 __printf(3, 4)
 void acpi_handle_printk(const char *level, acpi_handle handle,
 			const char *fmt, ...);




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

* Re: [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value
  2026-12-04 16:18 ` [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value Rafael J. Wysocki
@ 2026-12-04 17:37   ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:37 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux ACPI, Zhang Rui, Sakari Ailus

On Fri, Dec 04, 2026 at 05:18:22PM +0100, Rafael J. Wysocki wrote:
> 
> Since acpi_get_name() returns an acpi_status which is a positive
> number on failure, its return value should not be returned to the
> caller by acpi_object_path().
> 
> Make acpi_object_path() return -ENODATA if acpi_get_name() fails.

Would ENOENT fit better?


-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path()
  2026-12-04 16:20 ` [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path() Rafael J. Wysocki
@ 2026-12-04 17:40   ` Andy Shevchenko
  2026-12-04 18:03     ` Rafael J. Wysocki (Intel)
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:40 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux ACPI, Zhang Rui, Sakari Ailus

On Fri, Dec 04, 2026 at 05:20:13PM +0100, Rafael J. Wysocki wrote:
> 
> Introduce acpi_path_free for auto-freeing ACPI object paths returned
> by acpi_handle_path() and adjust the latter to return a const char
> pointer because that's what all of its users assign it to.  While at
> it, clean up the acpi_handle_path() function body a bit.
> 
> Use acpi_path_free in acpi_handle_printk() and __acpi_handle_debug()
> that already call acpi_handle_path() and make acpi_object_path()
> call acpi_handle_path() and use acpi_path_free to reduce code
> duplication.

...

>  static ssize_t acpi_object_path(acpi_handle handle, char *buf)
>  {
> -	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
> -	int result;
> -
> -	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &path)))
> +	const char *path __free(acpi_path_free) = acpi_handle_path(handle);
> +	if (!path)
>  		return -ENODATA;

Ah, the same error code is already in use. Okay for the first patch then!

> -	result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
> -	kfree(path.pointer);
> -	return result;
> +	return sysfs_emit(buf, "%s\n", path);
>  }

...

> - * Caller must free the returned buffer
> + * Caller must free the returned buffer or use acpi_path_free.

 * Caller must free the returned buffer or use acpi_path_free().

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths
  2026-12-04 16:17 [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Rafael J. Wysocki
  2026-12-04 16:18 ` [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value Rafael J. Wysocki
  2026-12-04 16:20 ` [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path() Rafael J. Wysocki
@ 2026-12-04 17:41 ` Andy Shevchenko
  2 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:41 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux ACPI, Zhang Rui, Sakari Ailus

On Fri, Dec 04, 2026 at 05:17:15PM +0100, Rafael J. Wysocki wrote:
> 
> This fixes a possible issue with returning an acpi_status value to
> sysfs callers on errors which would then take it mistakenly as the
> number of bytes written into the buffer (patch [1/2]) and introduces
> a __free for ACPI object paths and uses it to simplify code in a few
> places (patch [2/2]).
> 
> Thanks!

Good catches!
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path()
  2026-12-04 17:40   ` Andy Shevchenko
@ 2026-12-04 18:03     ` Rafael J. Wysocki (Intel)
  0 siblings, 0 replies; 7+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-12-04 18:03 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Rafael J. Wysocki, Linux ACPI, Zhang Rui, Sakari Ailus

On Fri, Dec 4, 2026 at 6:40 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Fri, Dec 04, 2026 at 05:20:13PM +0100, Rafael J. Wysocki wrote:
> >
> > Introduce acpi_path_free for auto-freeing ACPI object paths returned
> > by acpi_handle_path() and adjust the latter to return a const char
> > pointer because that's what all of its users assign it to.  While at
> > it, clean up the acpi_handle_path() function body a bit.
> >
> > Use acpi_path_free in acpi_handle_printk() and __acpi_handle_debug()
> > that already call acpi_handle_path() and make acpi_object_path()
> > call acpi_handle_path() and use acpi_path_free to reduce code
> > duplication.
>
> ...
>
> >  static ssize_t acpi_object_path(acpi_handle handle, char *buf)
> >  {
> > -     struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
> > -     int result;
> > -
> > -     if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &path)))
> > +     const char *path __free(acpi_path_free) = acpi_handle_path(handle);
> > +     if (!path)
> >               return -ENODATA;
>
> Ah, the same error code is already in use. Okay for the first patch then!
>
> > -     result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
> > -     kfree(path.pointer);
> > -     return result;
> > +     return sysfs_emit(buf, "%s\n", path);
> >  }
>
> ...
>
> > - * Caller must free the returned buffer
> > + * Caller must free the returned buffer or use acpi_path_free.
>
>  * Caller must free the returned buffer or use acpi_path_free().

acpi_path_free is a data type, not a function.

Anyway, I'll change it to "Caller must ensure that the returned buffer
will be freed.".

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

end of thread, other threads:[~2026-12-04 18:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:17 [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Rafael J. Wysocki
2026-12-04 16:18 ` [PATCH v1 1/2] ACPI: sysfs: Fix acpi_object_path() return value Rafael J. Wysocki
2026-12-04 17:37   ` Andy Shevchenko
2026-12-04 16:20 ` [PATCH v1 2/2] ACPI: utils: Add acpi_path_free and adjust acpi_handle_path() Rafael J. Wysocki
2026-12-04 17:40   ` Andy Shevchenko
2026-12-04 18:03     ` Rafael J. Wysocki (Intel)
2026-12-04 17:41 ` [PATCH v1 0/2] ACPI: sysfs/utils: Fix and simplification related to object paths Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox