From: Julia Lawall <julia.lawall@inria.fr>
To: Joe Perches <joe@perches.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
kernelnewbies <kernelnewbies@kernelnewbies.org>,
kernel-janitors <kernel-janitors@vger.kernel.org>,
cocci <cocci@systeme.lip6.fr>,
Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Subject: Re: [Cocci] linux-kernel janitorial RFP: Mark static arrays as const
Date: Mon, 8 Mar 2021 07:54:51 +0100 (CET) [thread overview]
Message-ID: <alpine.DEB.2.22.394.2103080752250.3383@hadrien> (raw)
In-Reply-To: <dd39a6974e6cef241ecfd1014d683a42cc722366.camel@perches.com>
[-- Attachment #1: Type: text/plain, Size: 3333 bytes --]
On Sun, 7 Mar 2021, Joe Perches wrote:
> On Sun, 2021-03-07 at 20:14 +0100, Julia Lawall wrote:
> >
> > On Wed, 3 Mar 2021, Joe Perches wrote:
> >
> > > On Wed, 2021-03-03 at 10:41 +0100, Rasmus Villemoes wrote:
> > > > On 02/03/2021 18.42, Joe Perches wrote:
> > > > > Here is a possible opportunity to reduce data usage in the kernel.
> > > > >
> > > > > $ git grep -P -n '^static\s+(?!const|struct)(?:\w+\s+){1,3}\w+\s*\[\s*\]' drivers/ | \
> > > > > grep -v __initdata | \
> > > > > wc -l
> > > > > 3250
> > > > >
> > > > > Meaning there are ~3000 declarations of arrays with what appears to be
> > > > > file static const content that are not marked const.
> > > > >
> > > > > So there are many static arrays that could be marked const to move the
> > > > > compiled object code from data to text minimizing the total amount of
> > > > > exposed r/w data.
> > > >
> > > > You can add const if you like, but it will rarely change the generated
> > > > code. gcc is already smart enough to take a static array whose contents
> > > > are provably never modified within the TU and put it in .rodata:
> > >
> > > At least some or perhaps even most of the time, true, but the gcc compiler
> > > from v5 through at least v10 seems inconsistent about when it does the
> > > appropriate conversion.
> > >
> > > See the example I posted:
> > > https://lore.kernel.org/lkml/6b8b250a06a98ce42120a14824531a8641f5e8aa.camel@perches.com/
> > >
> > > It was a randomly chosen source file conversion btw, I had no prior
> > > knowledge of whether the text/data use would change.
> > >
> > > I'm unsure about clang consistently moving static but provably const arrays
> > > from data to text. I rarely use clang. At least for v11 it seems to be
> > > better though. I didn't try 10.1.
> >
> > I tried the relevnt drivers in drivers/input/joystick. I got only one
> > driver that changed with gcc 9.3, which was
> > drivers/input/joystick/analog.c. It actually got larger:
> >
> > original:
> >
> > text data bss dec hex filename
> > 22607 10560 320 33487 82cf drivers/input/joystick/analog.o
> >
> > after adding const:
> >
> > text data bss dec hex filename
> > 22728 10816 320 33864 8448 drivers/input/joystick/analog.o
> >
> > This was the only case where bss was not 0, but I don't know if there is a
> > connection.
>
> You really need consider using defconfig so whatever object code
> does not have tracing/debugging support.
>
> For instance, this code with defconfig and analog joystick:
>
> Original:
>
> $ size drivers/input/joystick/analog.o
> text data bss dec hex filename
> 8115 261 224 8600 2198 drivers/input/joystick/analog.o
>
> with const:
>
> $ size drivers/input/joystick/analog.o
> text data bss dec hex filename
> 8179 201 224 8604 219c drivers/input/joystick/analog.o
Thanks for the suggestion. It occurred to me that in one case my
transformation was wrong, because the array was multi-level, and a sub
array was being stored in a structure field that was not declared as
const. So the argument that the compiler would figure out to put the
array in .rodata didn't make sense. But I still go the same sizes for
that file. So I'll try the whole thing again.
thanks,
julia
[-- Attachment #2: Type: text/plain, Size: 136 bytes --]
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
next prev parent reply other threads:[~2021-03-08 6:55 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-02 17:42 [Cocci] linux-kernel janitorial RFP: Mark static arrays as const Joe Perches
2021-03-02 21:41 ` Julia Lawall
2021-03-03 2:47 ` Joe Perches
2021-03-02 22:18 ` Bernd Petrovitsch
2021-03-03 8:36 ` Julia Lawall
2021-03-04 8:34 ` Andy Shevchenko
2021-03-03 9:41 ` Rasmus Villemoes
2021-03-03 14:51 ` Joe Perches
2021-03-07 19:14 ` Julia Lawall
2021-03-08 5:38 ` Joe Perches
2021-03-08 6:54 ` Julia Lawall [this message]
2021-03-03 17:06 ` Mansour Moufid
2021-03-03 17:21 ` Julia Lawall
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=alpine.DEB.2.22.394.2103080752250.3383@hadrien \
--to=julia.lawall@inria.fr \
--cc=cocci@systeme.lip6.fr \
--cc=joe@perches.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=kernelnewbies@kernelnewbies.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rasmus.villemoes@prevas.dk \
/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;
as well as URLs for NNTP newsgroup(s).