devres: provide API devm_kstrndup
PATCH 1/1 devres: provide API devm_kstrndup
From: Peng Fan <[email protected]>
This patch introduces devm_kstrndup API so that the device's driver can allocate memory and copy string.
--- drivers/base/devres.c | 28 ++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 29 insertions(+)
drivers/base/devres.c +28 -0
@@ -963,6 +963,34 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) }EXPORT_SYMBOL_GPL (devm_kstrdup); +/** +* devm_kstrndup - Allocate resource managed space and +* copy an existing string into that. +* @dev: Device to allocate memory for +* @s: the string to duplicate +* @max: max length to duplicate +* @gfp: the GFP mask used in the devm_kmalloc() call when allocating memory +* RETURNS: +* Pointer to allocated string on success, NULL on failure. +*/ +char *devm_kstrndup (struct device *dev,const char *s,size_t max,gfp_t gfp) +{ +size_t len; +char *buf; + +if (!s) +return NULL ; + + len =strnlen (s, max); + buf =devm_kmalloc (dev, len +1 , gfp); +if (buf) { +memcpy (buf, s, len); + buf[len] ='\0' ; + } +return buf; +} +EXPORT_SYMBOL_GPL (devm_kstrndup); +/** * devm_kstrdup_const - resource managed conditional string duplication * @dev: device for which to duplicate the string
include/linux/device.h +1 -0
@@ -226,6 +226,7 @@ static inline void *devm_kcalloc(struct device *dev,void devm_kfree (struct device *dev,const void *p);char *devm_kstrdup (struct device *dev,const char *s,gfp_t gfp) __malloc;const char *devm_kstrdup_const (struct device *dev,const char *s,gfp_t gfp); +char *devm_kstrndup (struct device *dev,const char *s,size_t max,gfp_t gfp) __malloc;void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp)__realloc_size (3 );
Signature · 1 line
2.37.1
Greg KH
maintainerQuoted 10 lines · Peng Fan (OSS)
From: Peng Fan <[email protected]>
This patch introduces
devm_kstrndupAPI so that the device's driver can allocate memory and copy string.Signed-off-by Peng Fan <[email protected]>--- drivers/base/devres.c | 28 ++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 29 insertions(+)
We can not add apis with no users, please submit this at the same time a driver needs it.
And what driver would need to copy a string?
Quoted 22 lines
drivers/base/devres.c +13
-0@@ -963,6 +963,34 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) }EXPORT_SYMBOL_GPL (devm_kstrdup); +/** +* devm_kstrndup - Allocate resource managed space and +* copy an existing string into that. +* @dev: Device to allocate memory for +* @s: the string to duplicate +* @max: max length to duplicate +* @gfp: the GFP mask used in the devm_kmalloc() call when allocating memory +* RETURNS: +* Pointer to allocated string on success, NULL on failure. +*/ +char *devm_kstrndup (struct device *dev,const char *s,size_t max,gfp_t gfp) +{ +size_t len;
drivers/base/devres.c char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) · line 988
Quoted 10 lines
drivers/base/devres.c +10
-0+char *buf; + +if (!s) +return NULL ; + + len =strnlen (s, max); + buf =devm_kmalloc (dev, len +1 , gfp); +if (buf) { +memcpy (buf, s, len); + buf[len] ='\0' ;
Why not use kstrndup() instead of copying the same logic here?
thanks,
greg k-h
Hi Greg,
Quoted 23 lines
-----Original Message-----
From: Greg KH <[email protected]>
Sent: 2023年5月4日 16:58
To: Peng Fan (OSS) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Peng Fan <[email protected]>
Subject: Re: [PATCH] devres: provide APIdevm_kstrndupQuoted 10 lines · Peng Fan (OSS)
From: Peng Fan <[email protected]>
This patch introduces
devm_kstrndupAPI so that the device's driver can allocate memory and copy string.Signed-off-by Peng Fan <[email protected]>--- drivers/base/devres.c | 28 ++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 29 insertions(+)
Quoted 4 lines
We can not add apis with no users, please submit this at the same time a driver needs it.
And what driver would need to copy a string?
https://lore.kernel.org/all/[email protected]/
Should I submit V2 with the upper patch in a patchset?
Thanks,
Peng.
Quoted 41 lines
drivers/base/devres.c char *devm_kstrdup(struct device *dev, const
Quoted 8 lines
drivers/base/devres.c index renamed from drivers/base/devres.c +0
-0
5c998cfac335..87e2965e5bab100644drivers/base/devres.c +0
-0@@ -963,6 +963,34 @@ char *devm_kstrdup(struct device *dev, constchar
Quoted 16 lines
drivers/base/devres.c +14
-0*s, gfp_t gfp) } EXPORT_SYMBOL_GPL(devm_kstrdup); +/** +* devm_kstrndup - Allocate resource managed space and +* copy an existing string into that. +* @dev: Device to allocate memory for +* @s: the string to duplicate +* @max: max length to duplicate +* @gfp: the GFP mask used in the devm_kmalloc() call when allocating +memory +* RETURNS: +* Pointer to allocated string on success, NULL on failure. +*/ +char *devm_kstrndup (struct device *dev,const char *s,size_t max, +gfp_t gfp) { +size_t len;drivers/base/devres.c char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) · line 988
Quoted 10 lines
drivers/base/devres.c +10
-0+char *buf; + +if (!s) +return NULL ; + + len =strnlen (s, max); + buf =devm_kmalloc (dev, len +1 , gfp); +if (buf) { +memcpy (buf, s, len); + buf[len] ='\0' ;Why not use
kstrndup()instead of copying the same logic here?thanks,
greg k-h
Greg KH
maintainerQuoted 25 lines · Peng Fan
Hi Greg,
Quoted 23 lines
-----Original Message-----
From: Greg KH <[email protected]>
Sent: 2023年5月4日 16:58
To: Peng Fan (OSS) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Peng Fan <[email protected]>
Subject: Re: [PATCH] devres: provide APIdevm_kstrndupQuoted 10 lines · Peng Fan (OSS)
From: Peng Fan <[email protected]>
This patch introduces
devm_kstrndupAPI so that the device's driver can allocate memory and copy string.Signed-off-by Peng Fan <[email protected]>--- drivers/base/devres.c | 28 ++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 29 insertions(+)
Quoted 6 lines
Quoted 4 lines
We can not add apis with no users, please submit this at the same time a driver needs it.
And what driver would need to copy a string?
Please never add new module parameters, ESPECIALLY for a driver. This is not the 1990's anymore, please use the correct apis instead (hint, sysfs, configfs, DT, etc.)
Quoted 1 line
Should I submit V2 with the upper patch in a patchset?
I can't take this as-is, for the reason I said, so yes. And again, your dependant patch is not ok either, so I don't think this is needed anymore.
thanks,
greg k-h
Quoted 4 lines · Peng Fan (OSS)
From: Peng Fan <[email protected]>
This patch introduces
devm_kstrndupAPI so that the device's driver can allocate memory and copy string.
...
drivers/base/devres.c char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) · line 987
Quoted 13 lines
drivers/base/devres.c +12
-0+char *devm_kstrndup (struct device *dev,const char *s,size_t max,gfp_t gfp) +{ +size_t len; +char *buf; + +if (!s) +return NULL ; + + len =strnlen (s, max); + buf =devm_kmalloc (dev, len +1 , gfp); +if (buf) { +memcpy (buf, s, len);
besides this to be open coded devm_kmemdup()
drivers/base/devres.c char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) · line 991
Quoted 4 lines
drivers/base/devres.c +4
-0+ buf[len] ='\0' ; + } +return buf; +}
you can always use devm_add_action_or_reset() for a single user before we introduce a new wide API.
Signature · 2 lines
With Best Regards, Andy Shevchenko