This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.
Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories: - copying a string and converting the case while specifying a maximum length to mimic strlcpy() - copying a string and converting the case without specifying a length to mimic strcpy() - converting the case of a string in-place (i.e. modifying the string that was passed in)
Consequently, I am proposing these new functions: void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
Several drivers are being modified to make use of the functions above. Another driver that also makes use of this functionality will be submitted upstream shortly, which prompted this whole exercise.
The changes made here have been compile-tested, but not tried out, due to lack of required hardware.
Changes since v2: - use strlcpy() semantics not strncpy() semantics, i.e. guarantee NULL termination - as a result strncpyto<upper|lower> are now called strlcpyto<upper|lower> - make functions void - use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0 - change PATCH 2/7 to match strlcpy() semantics - change PATCH 4/7 to match strlcpy() semantics
Changes since v1: - expanded strtolower() into a family of functions that cover use cases when a length argument is or isn't required and that support copying the string into a new buffer or changing it in-place - changed the function semantics to return a pointer to the terminating '\0' character of the modified string - added strtoupper() functionality mirroring the above - dropped the ACPICA patch, since that code is OS independent and can't rely on a Linux library function (see [2]) - Added two new patches replacing strtoupper() implementations
[1] https://lkml.org/lkml/2016/6/30/727 [2] https://lkml.org/lkml/2016/7/1/9
Markus Mayer (7): lib: string: add functions to case-convert strings drm/nouveau/core: make use of new strlcpytolower() function ACPI / device_sysfs: make use of new strtolower() function staging: speakup: replace spk_strlwr() with strlcpytolower() iscsi-target: replace iscsi_initiatorname_tolower() with strtolower() drm/nouveau/fifo/gk104: make use of new strcpytoupper() function power_supply: make use of new strcpytoupper() function
drivers/acpi/device_sysfs.c | 4 +-- drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +----- drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +-- drivers/power/power_supply_sysfs.c | 13 +++----- drivers/staging/speakup/kobjects.c | 3 +- drivers/staging/speakup/main.c | 3 +- drivers/staging/speakup/speakup.h | 1 - drivers/staging/speakup/varhandlers.c | 12 ------- drivers/target/iscsi/iscsi_target_nego.c | 17 +--------- include/linux/string.h | 40 ++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++ 11 files changed, 90 insertions(+), 55 deletions(-)
Add a collection of generic functions to convert strings to lowercase or uppercase.
Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.
The new functions are void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.
Signed-off-by: Markus Mayer mmayer@broadcom.com --- include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern void strlcpytoupper(char *dst, const char *src, size_t len); +extern void strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; }
+/** + * strcpytoupper - Copy string and convert to uppercase. + * @dst: The buffer to store the result. + * @src: The string to convert to uppercase. + */ +static inline void strcpytoupper(char *dst, const char *src) +{ + strlcpytoupper(dst, src, -1); +} + +/** + * strcpytolower - Copy string and convert to lowercase. + * @dst: The buffer to store the result. + * @src: The string to convert to lowercase. + */ +static inline void strcpytolower(char *dst, const char *src) +{ + strlcpytolower(dst, src, -1); +} + +/** + * strtoupper - Convert string to uppercase. + * @s: The string to operate on. + */ +static inline void strtoupper(char *s) +{ + strlcpytoupper(s, s, -1); +} + +/** + * strtolower - Convert string to lowercase. + * @s: The string to operate on. + */ +static inline void strtolower(char *s) +{ + strlcpytolower(s, s, -1); +} + #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace); + +/** + * strlcpytoupper - Copy a length-limited string and convert to uppercase. + * @dst: The buffer to store the result. + * @src: The string to convert to uppercase. + * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit. + */ +void strlcpytoupper(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = toupper(src[i]); + dst[i < len ? i : i - 1] = '\0'; +} +EXPORT_SYMBOL(strlcpytoupper); + +/** + * strlcpytolower - Copy a length-limited string and convert to lowercase. + * @dst: The buffer to store the result. + * @src: The string to convert to lowercase. + * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit. + */ +void strlcpytolower(char *dst, const char *src, size_t len) +{ + size_t i; + + if (!len) + return; + + for (i = 0; i < len && src[i]; ++i) + dst[i] = tolower(src[i]); + dst[i < len ? i : i - 1] = '\0'; +} +EXPORT_SYMBOL(strlcpytolower);
On 08/07/16 23:43, Markus Mayer wrote:
Add a collection of generic functions to convert strings to lowercase or uppercase.
Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.
The new functions are void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.
Signed-off-by: Markus Mayer mmayer@broadcom.com
include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern void strlcpytoupper(char *dst, const char *src, size_t len); +extern void strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; }
+/**
- strcpytoupper - Copy string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- */
+static inline void strcpytoupper(char *dst, const char *src) +{
- strlcpytoupper(dst, src, -1);
+}
Why not use SIZE_MAX instead of -1?
+/**
- strcpytolower - Copy string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- */
+static inline void strcpytolower(char *dst, const char *src) +{
- strlcpytolower(dst, src, -1);
+}
Same here, and the 2 below :)
Thanks Markus, Luis
+/**
- strtoupper - Convert string to uppercase.
- @s: The string to operate on.
- */
+static inline void strtoupper(char *s) +{
- strlcpytoupper(s, s, -1);
+}
+/**
- strtolower - Convert string to lowercase.
- @s: The string to operate on.
- */
+static inline void strtolower(char *s) +{
- strlcpytolower(s, s, -1);
+}
#endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace);
+/**
- strlcpytoupper - Copy a length-limited string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytoupper(char *dst, const char *src, size_t len) +{
- size_t i;
- if (!len)
return;
- for (i = 0; i < len && src[i]; ++i)
dst[i] = toupper(src[i]);
- dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytoupper);
+/**
- strlcpytolower - Copy a length-limited string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytolower(char *dst, const char *src, size_t len) +{
- size_t i;
- if (!len)
return;
- for (i = 0; i < len && src[i]; ++i)
dst[i] = tolower(src[i]);
- dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytolower);
On 9 July 2016 at 05:04, Luis de Bethencourt luisbg@osg.samsung.com wrote:
On 08/07/16 23:43, Markus Mayer wrote:
Add a collection of generic functions to convert strings to lowercase or uppercase.
Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.
The new functions are void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.
Signed-off-by: Markus Mayer mmayer@broadcom.com
include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern void strlcpytoupper(char *dst, const char *src, size_t len); +extern void strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; }
+/**
- strcpytoupper - Copy string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- */
+static inline void strcpytoupper(char *dst, const char *src) +{
strlcpytoupper(dst, src, -1);
+}
Why not use SIZE_MAX instead of -1?
Sure. I'll change all four of them. Thanks.
+/**
- strcpytolower - Copy string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- */
+static inline void strcpytolower(char *dst, const char *src) +{
strlcpytolower(dst, src, -1);
+}
Same here, and the 2 below :)
Thanks Markus, Luis
+/**
- strtoupper - Convert string to uppercase.
- @s: The string to operate on.
- */
+static inline void strtoupper(char *s) +{
strlcpytoupper(s, s, -1);
+}
+/**
- strtolower - Convert string to lowercase.
- @s: The string to operate on.
- */
+static inline void strtolower(char *s) +{
strlcpytolower(s, s, -1);
+}
#endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace);
+/**
- strlcpytoupper - Copy a length-limited string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytoupper(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = toupper(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytoupper);
+/**
- strlcpytolower - Copy a length-limited string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytolower(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = tolower(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytolower);
On 9 July 2016 at 08:30, Markus Mayer markus.mayer@broadcom.com wrote:
On 9 July 2016 at 05:04, Luis de Bethencourt luisbg@osg.samsung.com wrote:
On 08/07/16 23:43, Markus Mayer wrote:
Add a collection of generic functions to convert strings to lowercase or uppercase.
Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.
The new functions are void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.
Signed-off-by: Markus Mayer mmayer@broadcom.com
include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern void strlcpytoupper(char *dst, const char *src, size_t len); +extern void strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; }
+/**
- strcpytoupper - Copy string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- */
+static inline void strcpytoupper(char *dst, const char *src) +{
strlcpytoupper(dst, src, -1);
+}
Why not use SIZE_MAX instead of -1?
Sure. I'll change all four of them. Thanks.
Turns out there's actually a circular dependency here. SIZE_MAX is defined in linux/kernel.h. So, string.h would need to include kernel.h. But kernel.h, by way of several other headers, includes string.h.
Attempting to include kernel.h in string.h then leads to something like this:
CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CC scripts/mod/devicetable-offsets.s CHK include/generated/timeconst.h In file included from include/linux/printk.h:289:0, from include/linux/kernel.h:13, from include/linux/string.h:11, from include/uapi/linux/uuid.h:21, from include/linux/uuid.h:19, from include/linux/mod_devicetable.h:12, from scripts/mod/devicetable-offsets.c:2: include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’: include/linux/dynamic_debug.h:122:2: error: implicit declaration of function ‘strstr’ [-Werror=implicit-function-declaration] if (strstr(param, "dyndbg")) { ^ include/linux/dynamic_debug.h:122:6: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default] if (strstr(param, "dyndbg")) { ^ Since kernel.h is referencing string.h (which is needed, but not included a second time due to the include guards), this leads to undeclared string functions, because we are still in the early stages of including string.h itself and haven't gotten to the function declarations yet.
+/**
- strcpytolower - Copy string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- */
+static inline void strcpytolower(char *dst, const char *src) +{
strlcpytolower(dst, src, -1);
+}
Same here, and the 2 below :)
Thanks Markus, Luis
+/**
- strtoupper - Convert string to uppercase.
- @s: The string to operate on.
- */
+static inline void strtoupper(char *s) +{
strlcpytoupper(s, s, -1);
+}
+/**
- strtolower - Convert string to lowercase.
- @s: The string to operate on.
- */
+static inline void strtolower(char *s) +{
strlcpytolower(s, s, -1);
+}
#endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace);
+/**
- strlcpytoupper - Copy a length-limited string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytoupper(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = toupper(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytoupper);
+/**
- strlcpytolower - Copy a length-limited string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytolower(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = tolower(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytolower);
On 11/07/16 23:46, Markus Mayer wrote:
On 9 July 2016 at 08:30, Markus Mayer markus.mayer@broadcom.com wrote:
On 9 July 2016 at 05:04, Luis de Bethencourt luisbg@osg.samsung.com wrote:
On 08/07/16 23:43, Markus Mayer wrote:
Add a collection of generic functions to convert strings to lowercase or uppercase.
Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.
The new functions are void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.
Signed-off-by: Markus Mayer mmayer@broadcom.com
include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a..36c9d14 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); +extern void strlcpytoupper(char *dst, const char *src, size_t len); +extern void strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path) return tail ? tail + 1 : path; }
+/**
- strcpytoupper - Copy string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- */
+static inline void strcpytoupper(char *dst, const char *src) +{
strlcpytoupper(dst, src, -1);
+}
Why not use SIZE_MAX instead of -1?
Sure. I'll change all four of them. Thanks.
Turns out there's actually a circular dependency here. SIZE_MAX is defined in linux/kernel.h. So, string.h would need to include kernel.h. But kernel.h, by way of several other headers, includes string.h.
Attempting to include kernel.h in string.h then leads to something like this:
CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CC scripts/mod/devicetable-offsets.s CHK include/generated/timeconst.h In file included from include/linux/printk.h:289:0, from include/linux/kernel.h:13, from include/linux/string.h:11, from include/uapi/linux/uuid.h:21, from include/linux/uuid.h:19, from include/linux/mod_devicetable.h:12, from scripts/mod/devicetable-offsets.c:2: include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’: include/linux/dynamic_debug.h:122:2: error: implicit declaration of function ‘strstr’ [-Werror=implicit-function-declaration] if (strstr(param, "dyndbg")) { ^ include/linux/dynamic_debug.h:122:6: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default] if (strstr(param, "dyndbg")) { ^ Since kernel.h is referencing string.h (which is needed, but not included a second time due to the include guards), this leads to undeclared string functions, because we are still in the early stages of including string.h itself and haven't gotten to the function declarations yet.
Hi Markus,
Amazing. I see this happening as well, but I know it shouldn't.
The reason the #ifndef guards in headers are there is precisely to allow circular dependencies.
The problem in your output reads as: strstr() is in string.h #include string.h -> that includes kernel.h -> that includes string.h
The third should do nothing based on _LINUX_STRING_H_ being defined already and all code inside the #ifndef in string.h not being executed. Yet it shouldn't block the first include above since that macro isn't defined, which is what the error suggests since it doesn't have strstr() If _LINUX_STRING_H is defined, strstr() should be available.
Investigating this issue, it only happens when CONFIG_DYNAMIC_DEBUG is not set and line 170 of dynamic_debug.h runs, but just above we have an include of string.h.
Very strange that #include <linux/string.h> isn't doing its job.
The first thing I tried is to understand where dynamic_debug.h is used and removed the unneeded ones: -------------------------------------------------------------------------- diff --git a/include/linux/kernel.h b/include/linux/kernel.h --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -11,7 +11,6 @@ #include <linux/log2.h> #include <linux/typecheck.h> #include <linux/printk.h> -#include <linux/dynamic_debug.h> #include <asm/byteorder.h> #include <uapi/linux/kernel.h>
diff --git a/include/linux/printk.h b/include/linux/printk.h --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -307,10 +307,11 @@ asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...); no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) #endif
-#include <linux/dynamic_debug.h>
/* If you are writing a driver, please use dev_dbg instead */ #if defined(CONFIG_DYNAMIC_DEBUG) +#include <linux/dynamic_debug.h> + /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */ #define pr_debug(fmt, ...) \ dynamic_pr_debug(fmt, ##__VA_ARGS__) diff --git a/kernel/module.c b/kernel/module.c index beaebea..e70a2fa 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -60,6 +60,7 @@ #include <linux/jump_label.h> #include <linux/pfn.h> #include <linux/bsearch.h> +#include <linux/dynamic_debug.h> #include <uapi/linux/module.h> #include "module-internal.h" --------------------------------------------------------------------------
This diff [0] fixes the issue but it is a workaround for the original issue about string.h not being properly included in dynamic_debug.h
Puzzled by this and can't figure out what is happening wrong.
The second thing I tried was adding #warning "Linking to string header" in include/linux/string.h, and I don't see any include path mentioning kernel.h, where do you see the circular dependency? I might be missing something.
Thanks, Luis
[0] Sent for comments: https://lkml.org/lkml/2016/7/13/686
+/**
- strcpytolower - Copy string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- */
+static inline void strcpytolower(char *dst, const char *src) +{
strlcpytolower(dst, src, -1);
+}
Same here, and the 2 below :)
Thanks Markus, Luis
+/**
- strtoupper - Convert string to uppercase.
- @s: The string to operate on.
- */
+static inline void strtoupper(char *s) +{
strlcpytoupper(s, s, -1);
+}
+/**
- strtolower - Convert string to lowercase.
- @s: The string to operate on.
- */
+static inline void strtolower(char *s) +{
strlcpytolower(s, s, -1);
+}
#endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ed83562..fd8c427 100644 --- a/lib/string.c +++ b/lib/string.c @@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new) return s; } EXPORT_SYMBOL(strreplace);
+/**
- strlcpytoupper - Copy a length-limited string and convert to uppercase.
- @dst: The buffer to store the result.
- @src: The string to convert to uppercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytoupper(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = toupper(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytoupper);
+/**
- strlcpytolower - Copy a length-limited string and convert to lowercase.
- @dst: The buffer to store the result.
- @src: The string to convert to lowercase.
- @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
- */
+void strlcpytolower(char *dst, const char *src, size_t len) +{
size_t i;
if (!len)
return;
for (i = 0; i < len && src[i]; ++i)
dst[i] = tolower(src[i]);
dst[i < len ? i : i - 1] = '\0';
+} +EXPORT_SYMBOL(strlcpytolower);
Call strlcpytolower() rather than copying the string explicitly and then walking it to convert it to lowercase.
Signed-off-by: Markus Mayer mmayer@broadcom.com ---
The semantics of the new function has changed, so this patch has been updated since v2 to match the new strlcpy() semantics (no explicit NULL terminating is required).
See https://patchwork.kernel.org/patch/9215207/ for the previous version.
drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c index 34ecd4a..982601e 100644 --- a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c +++ b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c @@ -36,16 +36,9 @@ nvkm_firmware_get(struct nvkm_device *device, const char *fwname, { char f[64]; char cname[16]; - int i;
/* Convert device name to lowercase */ - strncpy(cname, device->chip->name, sizeof(cname)); - cname[sizeof(cname) - 1] = '\0'; - i = strlen(cname); - while (i) { - --i; - cname[i] = tolower(cname[i]); - } + strlcpytolower(cname, device->chip->name, sizeof(cname));
snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname); return request_firmware(fw, f, device->dev);
Call strcpytoupper() rather than copying the string explicitly and then walking it to convert it to uppercase.
Signed-off-by: Markus Mayer mmayer@broadcom.com --- drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c index 743f3a1..8d01032 100644 --- a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c +++ b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c @@ -332,10 +332,7 @@ gk104_fifo_intr_fault(struct gk104_fifo *fifo, int unit) enum nvkm_devidx engidx = nvkm_top_fault(device->top, unit); if (engidx < NVKM_SUBDEV_NR) { const char *src = nvkm_subdev_name[engidx]; - char *dst = en; - do { - *dst++ = toupper(*src++); - } while(*src); + strcpytoupper(en, src); engine = nvkm_device_engine(device, engidx); } } else {
On 7/8/2016 6:43 PM, Markus Mayer wrote:
This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.
Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories: - copying a string and converting the case while specifying a maximum length to mimic strlcpy() - copying a string and converting the case without specifying a length to mimic strcpy() - converting the case of a string in-place (i.e. modifying the string that was passed in)
Consequently, I am proposing these new functions: void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
You may want to read the article here:
https://lwn.net/Articles/659214/
and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.
On 9 July 2016 at 20:13, Chris Metcalf cmetcalf@mellanox.com wrote:
On 7/8/2016 6:43 PM, Markus Mayer wrote:
This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.
Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories: - copying a string and converting the case while specifying a maximum length to mimic strlcpy() - copying a string and converting the case without specifying a length to mimic strcpy() - converting the case of a string in-place (i.e. modifying the string that was passed in)
Consequently, I am proposing these new functions: void strlcpytoupper(char *dst, const char *src, size_t len); void strlcpytolower(char *dst, const char *src, size_t len); void strcpytoupper(char *dst, const char *src); void strcpytolower(char *dst, const char *src); void strtoupper(char *s); void strtolower(char *s);
You may want to read the article here:
I'll read that. Thanks.
and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.
I am inclined to agree. However, everybody has been telling me that these functions should be void. Originally they weren't.
Regards, -Markus
dri-devel@lists.freedesktop.org