lkml.org 
[lkml]   [2021]   [Mar]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH 2/2] lib/vsprintf: reduce space taken by no_hash_pointers warning
On Mon, 8 Mar 2021 at 18:23, Petr Mladek <pmladek@suse.com> wrote:
[...]
> > I'm actually concerned about both. Platforms (and boot loaders) may
> > have limitations for kernel image size, too.
> > Static memory consumption is also more easily measured, so I tend
> > to run bloat-o-meter, and dive into anything that adds more than 1 KiB.
> > And yes, this message is a low-hanging fruit...
>
> OK, I wondered how big trick does the __initconst on its own.
>
> 1. I compiled kernel without this patchset:
>
> $# ll /boot/vmlinux-5.12.0-rc2-default+.bz2
> -rwxr-xr-x 1 root root 18911364 Mar 8 15:58 /boot/vmlinux-5.12.0-rc2-default+.bz2
>
> 2. With this patchset:
>
> $# ll /boot/vmlinux-5.12.0-rc2-default+.bz2
> -rwxr-xr-x 1 root root 18910767 Mar 8 16:16 /boot/vmlinux-5.12.0-rc2-default+.bz2
> $# echo $((18910767 - 18911364))
> -597
>
> 3. With the patch below:
>
> $# ll /boot/vmlinux-5.12.0-rc2-default+.bz2
> -rwxr-xr-x 1 root root 18910906 Mar 8 16:58 /boot/vmlinux-5.12.0-rc2-default+.bz2
> $# echo $((18910906 - 18911364))
> -458
>
> This patchset saves 139B more than a simple array.
>
>
> Well, I am a bit confused. I have tried to keep the strings as a
> static variable outside the function:
>
> static const char *no_hash_pointers_warning[] __initconst = {
> ...
>
> and I got the following build error:
>
> CC lib/vsprintf.o
> lib/vsprintf.c:2097:20: error: no_hash_pointers_warning causes a section type conflict with __setup_str_no_hash_pointers_enable
> static const char *no_hash_pointers_warning[] __initconst = {

This does not place the strings themselves into the initconst section,
but only the array of pointers to them. So, with 13 lines, we're
merely saving 13*sizeof(char*) after init, which does not resolve
Geert's problem of runtime overhead.

To dealloc the string text itself (remove the section), each line must
be placed into a 'char[N] __initconst' (or 'char [M][N] __initconst'
if we split the lines).

> ^~~~~~~~~~~~~~~~~~~~~~~~
> In file included from ./include/linux/printk.h:6:0,
> from ./include/linux/kernel.h:16,
> from ./include/linux/clk.h:13,
> from lib/vsprintf.c:22:
> ./include/linux/init.h:315:20: note: ‘__setup_str_no_hash_pointers_enable’ was declared here
> static const char __setup_str_##unique_id[] __initconst \
> ^
> ./include/linux/init.h:330:2: note: in expansion of macro ‘__setup_param’
> __setup_param(str, fn, fn, 1)
> ^~~~~~~~~~~~~
> lib/vsprintf.c:2127:1: note: in expansion of macro ‘early_param’
> early_param("no_hash_pointers", no_hash_pointers_enable);
> ^~~~~~~~~~~
>
>
> I solved this be defining the array inside the function that is marked
> __init. But I am not sure if it is the correct solution. And I wonder
> why the original patch did not have this problem.
>
> Also I am curious why the array reduced the size of the binary so
> significantly in compare with the const strings used as pr_warn()
> arguments. It might depend on the compression method or???
>
>
> Anyway, here is the patch that works for me and reduced the size of
> the binary considerably:
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 4a14889ccb35..af01edae0d86 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -2096,24 +2096,30 @@ EXPORT_SYMBOL_GPL(no_hash_pointers);
>
> static int __init no_hash_pointers_enable(char *str)
> {
> + int i;
> + const char *no_hash_pointers_warning[] = {
> + "**********************************************************",
> + "** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **",
> + "** **",
> + "** This system shows unhashed kernel memory addresses **",
> + "** via the console, logs, and other interfaces. This **",
> + "** might reduce the security of your system. **",
> + "** **",
> + "** If you see this message and you are not debugging **",
> + "** the kernel, report this immediately to your system **",
> + "** administrator! **",
> + "** **",
> + "** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **",
> + "**********************************************************",
> + };
> +

This has no __initconst optimization (no runtime savings), and the
compiler places these strings into the data section and the above
array is just an array of pointers to them.

> if (no_hash_pointers)
> return 0;
>
> no_hash_pointers = true;
>
> - pr_warn("**********************************************************\n");
> - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
> - pr_warn("** **\n");
> - pr_warn("** This system shows unhashed kernel memory addresses **\n");
> - pr_warn("** via the console, logs, and other interfaces. This **\n");
> - pr_warn("** might reduce the security of your system. **\n");
> - pr_warn("** **\n");
> - pr_warn("** If you see this message and you are not debugging **\n");
> - pr_warn("** the kernel, report this immediately to your system **\n");

While we're here: This paragraph can be shortened by saying what
kernel/trace/trace.c says ("..., report this immediately to your
vendor!") which avoids the "administrator! <lots of wasted spaces>".

> - pr_warn("** administrator! **\n");
> - pr_warn("** **\n");
> - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
> - pr_warn("**********************************************************\n");
> + for (i = 0; i < ARRAY_SIZE(no_hash_pointers_warning); i++)
> + pr_warn("%s\n", no_hash_pointers_warning[i]);

My guess is that the savings came from repeated calls to pr_warn() and
reduction in code-size and compression working better.

> return 0;
> }
>
>
> Honestly, I do not want to spend much more time on this. I made the
> test out of curiosity.
>
> Feel free to provide the patch using the array, ideally with some
> numbers how it helps. But please _avoid_ the indirection via
>
> const int lines[] = { 0, 1, -1, 2, 3, 4, -1, 5, 6, 7, -1, 1, 0 };

We can probably do without this, but we'll have duplicated lines
stored in the initconst section.

> and also _avoid_ all the hardcoded constants, like:
>
> no_hash_pointers_warning[8][55]

We'll need this if we want __initconst. But perhaps we do not have to
split it by lines, so we can get away with a char[].

> and
>
> pr_warn("**%54s**\n"
>
> They are error prone and hard to maintain. Such tricks are not
> worth it from my POV.

I can send the version with a single 'static char[] __initconst', but
that version doesn't dedup lines and requires more init memory. I
don't know if we can make everybody happy here, we have to sacrifice
something: readability or space.

\
 
 \ /
  Last update: 2021-03-08 19:25    [W:0.115 / U:0.024 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site