mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <linux@weissschuh.net>
To: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Len Brown <lenb@kernel.org>,
	Robert Moore <robert.moore@intel.com>,
	 linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org,
	acpica-devel@lists.linux.dev
Subject: Re: [PATCH 1/5] ACPI: sysfs: convert utf-16 from _STR to utf-8 only once
Date: Mon, 17 Jun 2024 20:57:10 +0200	[thread overview]
Message-ID: <7c0d3358-eb1f-4c71-8a09-52b5e7668e36@t-8ch.de> (raw)
In-Reply-To: <CAJZ5v0gmHPBS3LE+Eo8yJkvpuavBvip-1AEEEf9nxAp2gi_adQ@mail.gmail.com>

On 2024-06-17 20:43:03+0000, Rafael J. Wysocki wrote:
> On Mon, Jun 17, 2024 at 8:37 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
> >
> > On Thu, Jun 13, 2024 at 10:15 PM Thomas Weißschuh <linux@weissschuh.net> wrote:
> > >
> > > The ACPI _STR method returns an UTF-16 string that is converted to utf-8
> > > before printing it in sysfs.
> > > Currently this conversion is performed every time the "description"
> > > sysfs attribute is read, which is not necessary.
> >
> > Why is it a problem, though?

It's not a real problem, mostly it made the following changes simpler.

> > How many devices have _STR and how much of the time is it used?
> >
> > Hint: On the system I'm sitting in front of, the answer is 0 and never.
> 
> This was actually factually incorrect, sorry.
> 
> The correct answer is 12 out of 247 and very rarely (if at all).
> Which doesn't really change the point IMO.
> 
> > So Is it really worth adding an _STR string pointer to every struct acpi_device?

The string pointer replaces a 'union acpi_object *str_obj', so no new
space is used.
Also in case the device _STR is present the new code uses less memory, as
it doesn't need the full union and stores utf-8 instead of utf-16.
(Plus a few more cycles for the preemptive conversion)

In case no _STR is present both CPU and memory costs are identical.

Anyways, I don't really care about this and can also try to drop this
patch if you prefer.
Or drop the 'union acpi_device *' from the struct completely at your
preference.

> > > Only perform the conversion once and cache the result.
> > >
> > > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > > ---
> > >  drivers/acpi/device_sysfs.c | 63 ++++++++++++++++++++++++++++-----------------
> > >  include/acpi/acpi_bus.h     |  2 +-
> > >  2 files changed, 40 insertions(+), 25 deletions(-)
> >
> > And it's more lines of code even.
> >
> > >
> > > diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c
> > > index 23373faa35ec..4bedbe8f57ed 100644
> > > --- a/drivers/acpi/device_sysfs.c
> > > +++ b/drivers/acpi/device_sysfs.c
> > > @@ -439,24 +439,11 @@ static ssize_t description_show(struct device *dev,
> > >                                 char *buf)
> > >  {
> > >         struct acpi_device *acpi_dev = to_acpi_device(dev);
> > > -       int result;
> > >
> > > -       if (acpi_dev->pnp.str_obj == NULL)
> > > +       if (acpi_dev->pnp.str == NULL)
> > >                 return 0;
> > >
> > > -       /*
> > > -        * The _STR object contains a Unicode identifier for a device.
> > > -        * We need to convert to utf-8 so it can be displayed.
> > > -        */
> > > -       result = utf16s_to_utf8s(
> > > -               (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
> > > -               acpi_dev->pnp.str_obj->buffer.length,
> > > -               UTF16_LITTLE_ENDIAN, buf,
> > > -               PAGE_SIZE - 1);
> > > -
> > > -       buf[result++] = '\n';
> > > -
> > > -       return result;
> > > +       return sysfs_emit("%s\n", acpi_dev->pnp.str);
> > >  }
> > >  static DEVICE_ATTR_RO(description);
> > >
> > > @@ -507,14 +494,46 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr,
> > >  }
> > >  static DEVICE_ATTR_RO(status);
> > >
> > > +static const char *acpi_device_str(struct acpi_device *dev)
> > > +{
> > > +       struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
> > > +       union acpi_object *str_obj;
> > > +       acpi_status status;
> > > +       const char *ret;
> > > +       char buf[512];
> > > +       int result;
> > > +
> > > +       if (!acpi_has_method(dev->handle, "_STR"))
> > > +               return NULL;
> > > +
> > > +       status = acpi_evaluate_object(dev->handle, "_STR",
> > > +                                     NULL, &buffer);
> > > +       if (ACPI_FAILURE(status))
> > > +               return NULL;
> > > +
> > > +       str_obj = buffer.pointer;
> > > +       /*
> > > +        * The _STR object contains a Unicode identifier for a device.
> > > +        * We need to convert to utf-8 so it can be displayed.
> > > +        */
> > > +       result = utf16s_to_utf8s((wchar_t *)str_obj->buffer.pointer,
> > > +                                str_obj->buffer.length,
> > > +                                UTF16_LITTLE_ENDIAN,
> > > +                                buf, sizeof(buf) - 1);
> > > +       buf[result++] = '\0';
> > > +
> > > +       ret = kstrdup(buf, GFP_KERNEL);
> > > +       kfree(buffer.pointer);
> > > +
> > > +       return ret;
> > > +}
> > > +
> > >  /**
> > >   * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
> > >   * @dev: ACPI device object.
> > >   */
> > >  int acpi_device_setup_files(struct acpi_device *dev)
> > >  {
> > > -       struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
> > > -       acpi_status status;
> > >         int result = 0;
> > >
> > >         /*
> > > @@ -539,12 +558,8 @@ int acpi_device_setup_files(struct acpi_device *dev)
> > >         /*
> > >          * If device has _STR, 'description' file is created
> > >          */
> > > -       if (acpi_has_method(dev->handle, "_STR")) {
> > > -               status = acpi_evaluate_object(dev->handle, "_STR",
> > > -                                       NULL, &buffer);
> > > -               if (ACPI_FAILURE(status))
> > > -                       buffer.pointer = NULL;
> > > -               dev->pnp.str_obj = buffer.pointer;
> > > +       dev->pnp.str = acpi_device_str(dev);
> > > +       if (dev->pnp.str) {
> > >                 result = device_create_file(&dev->dev, &dev_attr_description);
> > >                 if (result)
> > >                         goto end;
> > > @@ -618,7 +633,7 @@ void acpi_device_remove_files(struct acpi_device *dev)
> > >          * If device has _STR, remove 'description' file
> > >          */
> > >         if (acpi_has_method(dev->handle, "_STR")) {
> > > -               kfree(dev->pnp.str_obj);
> > > +               kfree(dev->pnp.str);
> > >                 device_remove_file(&dev->dev, &dev_attr_description);
> > >         }
> > >         /*
> > > diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h
> > > index 1a4dfd7a1c4a..32e3105c9ece 100644
> > > --- a/include/acpi/acpi_bus.h
> > > +++ b/include/acpi/acpi_bus.h
> > > @@ -254,7 +254,7 @@ struct acpi_device_pnp {
> > >         struct list_head ids;           /* _HID and _CIDs */
> > >         acpi_device_name device_name;   /* Driver-determined */
> > >         acpi_device_class device_class; /*        "          */
> > > -       union acpi_object *str_obj;     /* unicode string for _STR method */
> > > +       const char *str;                /* _STR */
> > >  };
> > >
> > >  #define acpi_device_bid(d)     ((d)->pnp.bus_id)
> > >
> > > --
> > > 2.45.2
> > >
> > >

  reply	other threads:[~2024-06-17 18:57 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-13 20:15 [PATCH 0/5] ACPI: sysfs: manage sysfs attributes through device core Thomas Weißschuh
2024-06-13 20:15 ` [PATCH 1/5] ACPI: sysfs: convert utf-16 from _STR to utf-8 only once Thomas Weißschuh
2024-06-17 18:37   ` Rafael J. Wysocki
2024-06-17 18:43     ` Rafael J. Wysocki
2024-06-17 18:57       ` Thomas Weißschuh [this message]
2024-06-17 19:20         ` Rafael J. Wysocki
2024-06-13 20:15 ` [PATCH 2/5] ACPI: sysfs: use device lifecycle for _STR result Thomas Weißschuh
2024-06-25 20:57   ` Mark Brown
2024-06-25 21:56     ` Thomas Weißschuh
2024-06-26 15:39       ` Mark Brown
2024-06-27 17:17         ` Thomas Weißschuh
2024-06-28 17:26           ` Rafael J. Wysocki
2024-06-13 20:15 ` [PATCH 3/5] ACPI: sysfs: manage attributes as attribute_group Thomas Weißschuh
2024-06-13 20:15 ` [PATCH 4/5] ACPI: sysfs: manage sysfs attributes through device core Thomas Weißschuh
2024-06-13 20:15 ` [PATCH 5/5] ACPI: sysfs: remove return value of acpi_device_setup_files() Thomas Weißschuh
2024-06-19 16:51 ` [PATCH 0/5] ACPI: sysfs: manage sysfs attributes through device core Rafael J. Wysocki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c0d3358-eb1f-4c71-8a09-52b5e7668e36@t-8ch.de \
    --to=linux@weissschuh.net \
    --cc=acpica-devel@lists.linux.dev \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=robert.moore@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®