From: Tom Rix <trix@redhat.com>
To: Michael Walle <michael@walle.cc>, Xu Yilun <yilun.xu@intel.com>,
Jean Delvare <jdelvare@suse.com>,
Guenter Roeck <linux@roeck-us.net>, Andrew Lunn <andrew@lunn.ch>,
Heiner Kallweit <hkallweit1@gmail.com>,
Russell King <linux@armlinux.org.uk>,
"David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
netdev@vger.kernel.org, David Laight <David.Laight@ACULAB.COM>
Subject: Re: [PATCH v3 1/2] hwmon: introduce hwmon_sanitize_name()
Date: Mon, 4 Apr 2022 16:14:55 -0700 [thread overview]
Message-ID: <428c28e4-87cc-50a4-ef13-41ae36702a84@redhat.com> (raw)
In-Reply-To: <20220404184340.3973329-2-michael@walle.cc>
On 4/4/22 11:43 AM, Michael Walle wrote:
> More and more drivers will check for bad characters in the hwmon name
> and all are using the same code snippet. Consolidate that code by adding
> a new hwmon_sanitize_name() function.
>
> Signed-off-by: Michael Walle <michael@walle.cc>
> ---
> Documentation/hwmon/hwmon-kernel-api.rst | 16 ++++++++
> drivers/hwmon/hwmon.c | 50 ++++++++++++++++++++++++
> include/linux/hwmon.h | 3 ++
> 3 files changed, 69 insertions(+)
>
> diff --git a/Documentation/hwmon/hwmon-kernel-api.rst b/Documentation/hwmon/hwmon-kernel-api.rst
> index c41eb6108103..e2975d5caf34 100644
> --- a/Documentation/hwmon/hwmon-kernel-api.rst
> +++ b/Documentation/hwmon/hwmon-kernel-api.rst
> @@ -50,6 +50,10 @@ register/unregister functions::
>
> void devm_hwmon_device_unregister(struct device *dev);
>
> + char *hwmon_sanitize_name(const char *name);
> +
> + char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
> +
> hwmon_device_register_with_groups registers a hardware monitoring device.
> The first parameter of this function is a pointer to the parent device.
> The name parameter is a pointer to the hwmon device name. The registration
> @@ -95,6 +99,18 @@ All supported hwmon device registration functions only accept valid device
> names. Device names including invalid characters (whitespace, '*', or '-')
> will be rejected. The 'name' parameter is mandatory.
>
> +If the driver doesn't use a static device name (for example it uses
> +dev_name()), and therefore cannot make sure the name only contains valid
> +characters, hwmon_sanitize_name can be used. This convenience function
> +will duplicate the string and replace any invalid characters with an
> +underscore. It will allocate memory for the new string and it is the
> +responsibility of the caller to release the memory when the device is
> +removed.
> +
> +devm_hwmon_sanitize_name is the resource managed version of
> +hwmon_sanitize_name; the memory will be freed automatically on device
> +removal.
> +
> Using devm_hwmon_device_register_with_info()
> --------------------------------------------
>
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 989e2c8496dd..cc4a16a466a0 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -1057,6 +1057,56 @@ void devm_hwmon_device_unregister(struct device *dev)
> }
> EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
>
> +static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
> +{
> + char *name, *p;
> +
> + if (dev)
> + name = devm_kstrdup(dev, old_name, GFP_KERNEL);
> + else
> + name = kstrdup(old_name, GFP_KERNEL);
> + if (!name)
> + return NULL;
should return ERR_PTR(-ENOMEM)
> +
> + for (p = name; *p; p++)
> + if (hwmon_is_bad_char(*p))
> + *p = '_';
> +
> + return name;
> +}
> +
> +/**
> + * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
> + * @name: NUL-terminated name
> + *
> + * Allocates a new string where any invalid characters will be replaced
> + * by an underscore. It is the responsibility of the caller to release
> + * the memory.
> + *
> + * Returns newly allocated name or %NULL in case of error.
> + */
> +char *hwmon_sanitize_name(const char *name)
> +{
> + return __hwmon_sanitize_name(NULL, name);
> +}
> +EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
> +
> +/**
> + * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
> + * @dev: device to allocate memory for
> + * @name: NUL-terminated name
> + *
> + * Allocates a new string where any invalid characters will be replaced
> + * by an underscore.
> + *
> + * Returns newly allocated name or %NULL in case of error.
> + */
> +char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
> +{
> + return __hwmon_sanitize_name(dev, name);
Should have a (!dev) check.
> +}
> +EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
> +
> static void __init hwmon_pci_quirks(void)
> {
> #if defined CONFIG_X86 && defined CONFIG_PCI
> diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> index eba380b76d15..4efaf06fd2b8 100644
> --- a/include/linux/hwmon.h
> +++ b/include/linux/hwmon.h
> @@ -461,6 +461,9 @@ void devm_hwmon_device_unregister(struct device *dev);
> int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
> u32 attr, int channel);
>
> +char *hwmon_sanitize_name(const char *name);
> +char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
> +
> /**
> * hwmon_is_bad_char - Is the char invalid in a hwmon name
This still needed in *.h ?
Tom
> * @ch: the char to be considered
next prev parent reply other threads:[~2022-04-04 23:17 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-04 18:43 [PATCH v3 0/2] hwmon: introduce hwmon_sanitize() Michael Walle
2022-04-04 18:43 ` [PATCH v3 1/2] hwmon: introduce hwmon_sanitize_name() Michael Walle
2022-04-04 23:14 ` Tom Rix [this message]
2022-04-04 23:31 ` Guenter Roeck
2022-04-05 8:20 ` David Laight
2022-04-04 18:43 ` [PATCH v3 2/2] hwmon: intel-m10-bmc-hwmon: use devm_hwmon_sanitize_name() Michael Walle
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=428c28e4-87cc-50a4-ef13-41ae36702a84@redhat.com \
--to=trix@redhat.com \
--cc=David.Laight@ACULAB.COM \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=hkallweit1@gmail.com \
--cc=jdelvare@suse.com \
--cc=kuba@kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linux@roeck-us.net \
--cc=michael@walle.cc \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=yilun.xu@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®