lib/vsprintf: Rework header inclusions
Subsystems (4)
Andy Shevchenko
reviewercover lib/vsprintf: Rework header inclusions
Some patches that reduce the mess with the header inclusions related to vsprintf.c module. Each patch has its own description, and has no dependencies to each other, except the collisions over modifications of the same places. Hence the series.
Andy Shevchenko (4):
lib/vsprintf: Declare no_hash_pointers in a local header lib/vsprintf: Sort headers alphabetically lib/vsprintf: Remove implied inclusions lib/vsprintf: Split out sprintf() and friends
include/linux/kernel.h | 30 +----------------------------- include/linux/sprintf.h | 24 ++++++++++++++++++++++++ lib/test_printf.c | 4 ++-- lib/vsprintf.c | 38 ++++++++++++++++++++------------------ lib/vsprintf.h | 7 +++++++ mm/kfence/report.c | 3 +-- 6 files changed, 55 insertions(+), 51 deletions(-) create mode 100644 include/linux/sprintf.h create mode 100644 lib/vsprintf.h
Signature · 1 line
2.40.0.1.gaa8946217a0b
Andy Shevchenko
reviewerPATCH 1/4 lib/vsprintf: Declare no_hash_pointers in a local header
Sparse is not happy to see non-static variable without declaration:lib/vsprintf.c:61:6: warning: symbol 'no_hash_pointers' was not declared. Should it be static?
Declare respective variable in the local header.
--- lib/test_printf.c | 4 ++-- lib/vsprintf.c | 1 + lib/vsprintf.h | 7 +++++++ mm/kfence/report.c | 3 +-- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 lib/vsprintf.h
lib/test_printf.c +2 -2
@@ -24,6 +24,8 @@#include <linux/property.h> +#include "vsprintf.h" +#include "../tools/testing/selftests/kselftest_module.h" #define BUF_SIZE 256
@@ -41,8 +43,6 @@ KSTM_MODULE_GLOBALS();static char *test_buffer__initdata ;static char *alloced_buffer__initdata ; -extern bool no_hash_pointers; - static int __printf(4, 0) __init do_test(int bufsize, const char *expect, int elen, const char *fmt, va_list ap)
lib/vsprintf.c +1 -0
@@ -54,6 +54,7 @@#include <linux/string_helpers.h> #include "kstrtox.h" +#include "vsprintf.h" /* Disable pointer hashing if requested */ bool no_hash_pointers__ro_after_init ;
lib/vsprintf.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LIB_VSPRINTF_H +#define _LIB_VSPRINTF_H + +extern bool no_hash_pointers; + +#endif
mm/kfence/report.c +1 -2
@@ -19,6 +19,7 @@#include <asm/kfence.h> +#include "../../lib/vsprintf.h" #include "kfence.h" /* May be overridden by <asm/kfence.h>. */
@@ -26,8 +27,6 @@#define ARCH_FUNC_PREFIX ""#endif -extern bool no_hash_pointers; -/* Helper function to either print to a seq_file or to console. */ __printf(2, 3)static void seq_con_printf (struct seq_file *seq,const char *fmt, ...)
Signature · 1 line
2.40.0.1.gaa8946217a0b
Marco Elver
maintainerRe: [PATCH v1 1/4] lib/vsprintf: Declare no_hash_pointers in a local header
Quoted 55 lines · Andy Shevchenko
Sparse is not happy to see non-static variable without declaration:
lib/vsprintf.c:61:6: warning: symbol 'no_hash_pointers' was not declared. Should it be static?Declare respective variable in the local header.
Signed-off-by Andy Shevchenko <[email protected]>--- lib/test_printf.c | 4 ++-- lib/vsprintf.c | 1 + lib/vsprintf.h | 7 +++++++ mm/kfence/report.c | 3 +-- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 lib/vsprintf.hlib/test_printf.c +2
-2@@ -24,6 +24,8 @@#include <linux/property.h> +#include "vsprintf.h" +#include "../tools/testing/selftests/kselftest_module.h" #define BUF_SIZE 256@@ -41,8 +43,6 @@ KSTM_MODULE_GLOBALS();static char *test_buffer__initdata ;static char *alloced_buffer__initdata ; -extern bool no_hash_pointers; - static int __printf(4, 0) __init do_test(int bufsize, const char *expect, int elen, const char *fmt, va_list ap)lib/vsprintf.c +1
-0@@ -54,6 +54,7 @@#include <linux/string_helpers.h> #include "kstrtox.h" +#include "vsprintf.h" /* Disable pointer hashing if requested */ bool no_hash_pointers__ro_after_init ;lib/vsprintf.h new file +0
-0
lib/vsprintf.h line 7
Quoted 7 lines
lib/vsprintf.h new file +7
-0@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LIB_VSPRINTF_H +#define _LIB_VSPRINTF_H + +extern bool no_hash_pointers; + +#endif
It seems odd to create such a local header and then refer to it from other subsystems.
What's the downside of just putting this into the new include/linux/sprintf.h? If someone wants to access this variable, they will one way or another.
Steven Rostedt
maintainerRe: [PATCH v1 1/4] lib/vsprintf: Declare no_hash_pointers in a local header
Quoted 13 lines · On Fri, 4 Aug 2023 10:38:22 +0200 Marco Elver <[email protected]> wrote:
lib/vsprintf.h line 7
Quoted 9 lines
+++
b/lib/vsprintf.hlib/vsprintf.h new file +7
-0@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LIB_VSPRINTF_H +#define _LIB_VSPRINTF_H + +extern bool no_hash_pointers; + +#endif It seems odd to create such a local header and then refer to it from other subsystems.
Quoted 3 lines
What's the downside of just putting this into the new
include/linux/sprintf.h? If someone wants to access this variable, they will one way or another.
+1
-- Steve
Steven Rostedt
maintainerRe: [PATCH v1 1/4] lib/vsprintf: Declare no_hash_pointers in a local header
In reply to Andy Shevchenkomm/kfence/report.c line 24
Quoted 11 lines · On Fri, 4 Aug 2023 11:26:16 +0300 Andy Shevchenko…
mm/kfence/report.c +1
-0@@ -19,6 +19,7 @@#include <asm/kfence.h> +#include "../../lib/vsprintf.h" #include "kfence.h"
Ug, I hate "../../" includes. Can we have this in one of the main header files?
-- Steve
Andy Shevchenko
reviewerPATCH 2/4 lib/vsprintf: Sort headers alphabetically
In reply to Andy ShevchenkoSorting headers alphabetically helps locating duplicates, and make it easier to figure out where to insert new headers.
--- lib/vsprintf.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-)
lib/vsprintf.c +20 -18
@@ -17,42 +17,44 @@* - scnprintf and vscnprintf */ -#include <linux/stdarg.h> #include <linux/build_bug.h> #include <linux/clk.h> #include <linux/clk-provider.h> -#include <linux/errname.h> -#include <linux/module.h> /* for KSYM_SYMBOL_LEN */ -#include <linux/types.h> -#include <linux/string.h> +#include <linux/compiler.h> +#include <linux/cred.h> #include <linux/ctype.h> -#include <linux/kernel.h> +#include <linux/dcache.h> +#include <linux/errname.h> +#include <linux/ioport.h> #include <linux/kallsyms.h> +#include <linux/kernel.h> #include <linux/math64.h> -#include <linux/uaccess.h> -#include <linux/ioport.h> -#include <linux/dcache.h> -#include <linux/cred.h> +#include <linux/module.h> /* for KSYM_SYMBOL_LEN */ +#include <linux/notifier.h> +#include <linux/of.h> +#include <linux/property.h> #include <linux/rtc.h> +#include <linux/siphash.h> +#include <linux/stdarg.h> +#include <linux/string.h> +#include <linux/string_helpers.h> #include <linux/time.h> +#include <linux/types.h> +#include <linux/uaccess.h> #include <linux/uuid.h> -#include <linux/of.h> -#include <net/addrconf.h> -#include <linux/siphash.h> -#include <linux/compiler.h> -#include <linux/property.h> -#include <linux/notifier.h> +#ifdef CONFIG_BLOCK #include <linux/blkdev.h> #endif +#include <net/addrconf.h> +#include "../mm/internal.h" /* For the trace_print_flags arrays */ -#include <asm/page.h> /* for PAGE_SIZE */ #include <asm/byteorder.h> /* cpu_to_le16 */ +#include <asm/page.h> /* for PAGE_SIZE */ #include <asm/unaligned.h> -#include <linux/string_helpers.h> #include "kstrtox.h" #include "vsprintf.h"
Signature · 1 line
2.40.0.1.gaa8946217a0b
Andy Shevchenko
reviewerPATCH 3/4 lib/vsprintf: Remove implied inclusions
In reply to Andy ShevchenkoRemove inclusions that are implied and guaranteed to be provided by others:
compiler.h by types.h
string.hi by string_helpers.h
--- lib/vsprintf.c | 2 -- 1 file changed, 2 deletions(-)
lib/vsprintf.c +0 -2
@@ -20,7 +20,6 @@#include <linux/build_bug.h> #include <linux/clk.h> #include <linux/clk-provider.h> -#include <linux/compiler.h> #include <linux/cred.h> #include <linux/ctype.h> #include <linux/dcache.h>
@@ -36,7 +35,6 @@#include <linux/rtc.h> #include <linux/siphash.h> #include <linux/stdarg.h> -#include <linux/string.h> #include <linux/string_helpers.h> #include <linux/time.h> #include <linux/types.h>
Signature · 1 line
2.40.0.1.gaa8946217a0b
Rasmus Villemoes
reviewerRe: [PATCH v1 3/4] lib/vsprintf: Remove implied inclusions
Quoted 4 lines · Andy Shevchenko
Remove inclusions that are implied and guaranteed to be provided by others:
compiler.hbytypes.h
string.hi bystring_helpers.h
What? No. That's not what we want. Each .c and each .h file should include the headers that declare the stuff they're using. So if string_helpers.h magically stops referring to anything from string.h, one should be allowed to stop including string.h from string_helpers.h.
Sure, those two may forever be so intertwined that it never happens, but one really can't maintain some matrix of "X always includes Y so if you include X you don't have to include Y" in one's head.
Rasmus
Andy Shevchenko
reviewerRe: [PATCH v1 3/4] lib/vsprintf: Remove implied inclusions
Quoted 8 lines · Rasmus Villemoes
Quoted 4 lines · Andy Shevchenko
Remove inclusions that are implied and guaranteed to be provided by others:
compiler.hbytypes.h
string.hi bystring_helpers.hWhat? No. That's not what we want. Each .c and each .h file should include the headers that declare the stuff they're using.
99.99% of kernel if not more doesn't follow this rule pedantically. We have to have a compromise between what is guaranteed and what is not.
For example, I'm pretty sure the types.h will always include compiler*.h.
Quoted 2 lines
So if
string_helpers.hmagically stops referring to anything fromstring.h, one should be allowed to stop includingstring.hfromstring_helpers.h.
That's how agreements work. We may agree to guarantee such inclusion or not. The kernel headers as of today is a complete mess (refer to the Ingo's 2k+ patch series). But still, some order / agreement is good to have.
Quoted 3 lines
Sure, those two may forever be so intertwined that it never happens, but one really can't maintain some matrix of "X always includes Y so if you include X you don't have to include Y" in one's head.
Somebody should do that at some point, otherwise it becomes even more mess.
If you want your way, iwyu should be part of the kernel build. And be prepared for dozens of headers to be added to the every single C file in the kernel.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerPATCH 4/4 lib/vsprintf: Split out sprintf() and friends
In reply to Andy Shevchenkokernel.h is being used as a dump for all kinds of stuff for a long time. sprintf() and friends are used in many drivers without need of the full kernel.h dependency train with it.
Here is the attempt on cleaning it up by splitting out sprintf() and friends.
--- include/linux/kernel.h | 30 +----------------------------- include/linux/sprintf.h | 24 ++++++++++++++++++++++++ lib/vsprintf.c | 1 + 3 files changed, 26 insertions(+), 29 deletions(-) create mode 100644 include/linux/sprintf.h
include/linux/kernel.h +1 -29
@@ -29,6 +29,7 @@#include <linux/panic.h> #include <linux/printk.h> #include <linux/build_bug.h> +#include <linux/sprintf.h> #include <linux/static_call_types.h> #include <linux/instruction_pointer.h> #include <asm/byteorder.h>
@@ -203,35 +204,6 @@ static inline void might_fault(void) { }void do_exit (long error_code)__noreturn ; -extern int num_to_str(char *buf, int size, - unsigned long long num, unsigned int width); - -/* lib/printf utilities */ - -extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); -extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); -extern __printf(3, 4) -int snprintf(char *buf, size_t size, const char *fmt, ...); -extern __printf(3, 0) -int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); -extern __printf(3, 4) -int scnprintf(char *buf, size_t size, const char *fmt, ...); -extern __printf(3, 0) -int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); -extern __printf(2, 3) __malloc -char *kasprintf(gfp_t gfp, const char *fmt, ...); -extern __printf(2, 0) __malloc -char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); -extern __printf(2, 0) -const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); - -extern __scanf(2, 3) -int sscanf(const char *, const char *, ...); -extern __scanf(2, 0) -int vsscanf(const char *, const char *, va_list); - -extern int no_hash_pointers_enable(char *str); -extern int get_option (char **str,int *pint);extern char *get_options (const char *str,int nints,int *ints);extern unsigned long long memparse (const char *ptr,char **retptr);
include/linux/sprintf.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KERNEL_SPRINTF_H_ +#define _LINUX_KERNEL_SPRINTF_H_ + +#include <linux/types.h> + +int num_to_str (char *buf,int size,unsigned long long num,unsigned int width); + +__printf(2, 3) int sprintf(char *buf, const char * fmt, ...); +__printf(2, 0) int vsprintf(char *buf, const char *, va_list); +__printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...); +__printf(3, 0) int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); +__printf(3, 4) int scnprintf(char *buf, size_t size, const char *fmt, ...); +__printf(3, 0) int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); +__printf(2, 3) __malloc char *kasprintf(gfp_t gfp, const char *fmt, ...); +__printf(2, 0) __malloc char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); +__printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); + +__scanf(2, 3) int sscanf(const char *, const char *, ...); +__scanf(2, 0) int vsscanf(const char *, const char *, va_list); + +int no_hash_pointers_enable (char *str); + +#endif /* _LINUX_KERNEL_SPRINTF_H */
lib/vsprintf.c +1 -0
@@ -34,6 +34,7 @@#include <linux/property.h> #include <linux/rtc.h> #include <linux/siphash.h> +#include <linux/sprintf.h> #include <linux/stdarg.h> #include <linux/string_helpers.h> #include <linux/time.h>
Signature · 1 line
2.40.0.1.gaa8946217a0b
Rasmus Villemoes
reviewerRe: [PATCH v1 4/4] lib/vsprintf: Split out sprintf() and friends
Quoted 69 lines · Andy Shevchenko
kernel.his being used as a dump for all kinds of stuff for a long time.sprintf()and friends are used in many drivers without need of the fullkernel.hdependency train with it.Here is the attempt on cleaning it up by splitting out
sprintf()and friends.Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/kernel.h | 30 +----------------------------- include/linux/sprintf.h | 24 ++++++++++++++++++++++++ lib/vsprintf.c | 1 + 3 files changed, 26 insertions(+), 29 deletions(-) create mode 100644 include/linux/sprintf.hinclude/linux/kernel.h +1
-29@@ -29,6 +29,7 @@#include <linux/panic.h> #include <linux/printk.h> #include <linux/build_bug.h> +#include <linux/sprintf.h> #include <linux/static_call_types.h> #include <linux/instruction_pointer.h> #include <asm/byteorder.h> @@ -203,35 +204,6 @@ static inline void might_fault(void) { }void do_exit (long error_code)__noreturn ; -extern int num_to_str(char *buf, int size, - unsigned long long num, unsigned int width); - -/* lib/printf utilities */ - -extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); -extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); -extern __printf(3, 4) -int snprintf(char *buf, size_t size, const char *fmt, ...); -extern __printf(3, 0) -int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); -extern __printf(3, 4) -int scnprintf(char *buf, size_t size, const char *fmt, ...); -extern __printf(3, 0) -int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); -extern __printf(2, 3) __malloc -char *kasprintf(gfp_t gfp, const char *fmt, ...); -extern __printf(2, 0) __malloc -char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); -extern __printf(2, 0) -const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); - -extern __scanf(2, 3) -int sscanf(const char *, const char *, ...); -extern __scanf(2, 0) -int vsscanf(const char *, const char *, va_list); - -extern int no_hash_pointers_enable(char *str); -extern int get_option (char **str,int *pint);extern char *get_options (const char *str,int nints,int *ints);extern unsigned long long memparse (const char *ptr,char **retptr);include/linux/sprintf.h new file +0
-0
include/linux/sprintf.h line 6
Quoted 6 lines
include/linux/sprintf.h new file +6
-0@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KERNEL_SPRINTF_H_ +#define _LINUX_KERNEL_SPRINTF_H_ + +#include <linux/types.h> +
Shouldn't this at least also include compiler_attributes.h, to make it self-contained?
As Marco said, please just declare no_hash_pointers in this file as well. Perhaps with a comment about not accessing it unless one has good reason, but I suppose that's true in general for all kernel global variables, so maybe not worth it for this one.
Rasmus
Andy Shevchenko
reviewerRe: [PATCH v1 4/4] lib/vsprintf: Split out sprintf() and friends
Quoted 1 line · Rasmus Villemoes
On 04/08/2023 10.26, Andy Shevchenko wrote:
...
Quoted 4 lines
Quoted 1 line
+#include <
linux/types.h>Shouldn't this at least also include
compiler_attributes.h, to make it self-contained?
As I replied in the other email, this is guaranteed by types.h. But if you insist, I can add it.
Quoted 4 lines
As Marco said, please just declare
no_hash_pointersin this file as well. Perhaps with a comment about not accessing it unless one has good reason, but I suppose that's true in general for all kernel global variables, so maybe not worth it for this one.
Sure, thank you for the review!
Signature · 2 lines
With Best Regards, Andy Shevchenko