[PATCH v3] RISC-V: Enable dead code elimination

Zhangjin Wu falcon at tinylab.org
Sun May 21 05:41:34 PDT 2023


Hi, Jisheng

> On Wed, May 17, 2023 at 04:29:36PM +0800, Zhangjin Wu wrote:
> > Select CONFIG_HAVE_LD_DEAD_CODE_DATA_ELIMINATION for RISC-V, allowing
> > the user to enable dead code elimination. In order for this to work,
> > ensure that we keep the alternative table by annotating them with KEEP.
> > 
> > This boots well on qemu with both rv32_defconfig & rv64 defconfig, but
> > it only shrinks their builds by ~1%, a smaller config is thereforce
> > customized to test this feature:
> 
> OOPS, I didn't noticed that you have sent out the patch and you are now
> in v3. I may read your patch sevral months ago, but I forget it.

Yeah, I have sent this patch several times, but beside the suggestion from
conor, I have gotten no more responses (before v3), I even thought RISC-V
people are not interested in or not require size shrinking ;-)

As I can see from the mailing list, Guoren just sent out a new series of
patchset [0] about size shrink (-16%), and include the one [1] from you, so,
I'm not alone, the left patchsets I'm working on and upstreaming include dead
syscall elimination [2], vdso configuration [3], nolibc for rv32 [4] and even
the self-decompress vmlinuz support (RFC v1 will be sent out next week), all of
these patchsets are result of my tinylinux [5] porting to RISC-V (got 334k
non-MMU rv64 vmlinuz+nolibc hello).

Is it ok to add you to my future CC list of the coming tinylinux
patchsets for RISC-V? welcome your review suggestions ;-)

>From Guoren:
[0]: https://lore.kernel.org/linux-riscv/mhng-24855381-7da8-4c77-bcaf-a3a53c8cb38b@palmer-ri-x1c9/T/

>From Jisheng:
[1]: https://lore.kernel.org/linux-riscv/20230511141211.2418-1-jszhang@kernel.org/

>From Zhangjin:
[2]: https://lore.kernel.org/linux-riscv/cover.1676594211.git.falcon@tinylab.org/
[3]: https://lore.kernel.org/linux-riscv/cover.1684430522.git.falcon@tinylab.org/T/#t
[4]: https://lore.kernel.org/linux-riscv/cover.1684425792.git.falcon@tinylab.org/T/#t
[5]: http://elinux.org/Work_on_Tiny_Linux_Kernel

> But your version missed an important preparation patch which is
> to fix the typo in commom vmlinux.lds.h in asm-generic, thus LD_ORPHAN_WARN
> will warn loudly. See[1] for more details.
> 

ok, it is a great fix, .init.data.* are really critical sections, so, do we
need to select LD_ORPHAN_WARN by default for HAVE_LD_DEAD_CODE_DATA_ELIMINATION
to dig out such potential issues?

> Another missing point is we need to keep the .init.pi* and .init.bss*
> sections. See[1] for more details.
>

sorry, my v3 has forgotten re-checking the new changes of vmlinux.lds.S, just
checked it again, .init.bss* about efi was added from v6.2 and the .init.pi*
was really a new rv kernel feature from v6.3-rc1, just to mention, this may be
an important review part of the future vmlinux.lds.S changes: the sections
required but not explicitly used by any function or data must be KEEP'ed
manually, for more details, my old paper about kernel gc-sections [6] is a good
reference.

[6]: https://lwn.net/images/conf/rtlws-2011/proc/Yong.pdf

Another work I'm trying to do in toolchain side (in progress) is let the kernel
auto keep part of current KEEP'ed sections, this may eliminate more
functions/data and also reduce the maintain cost of manual KEEPing. This idea
derives from the coming RFC v2 of system call elimination patchset, some of the
unused syscalls can not be eliminated automatically because of the "rough"
exception table KEEPing.

> Since you are the first person to send out the patch of
> HAVE_LD_DEAD_CODE_DATA_ELIMINATION for riscv, do you mind if I update
> my series and take your patch in my series v2, make you as the commmit
> author, use your commit msg, and add my co-developped tag etc?
>

I have found that you have fixed up some other issues, It is better to
merge mine in your series, please don't forget the Tested-by line from
Bin Meng [7] and the Reviewed-by line from Guoren [8].

[7]: https://lore.kernel.org/linux-riscv/20230521040949.1411220-1-bmeng@tinylab.org/#t
[8]: https://lore.kernel.org/linux-riscv/20230521022313.3758709-1-guoren@kernel.org/

Thanks very much,
Zhangjin

> Thanks
> 
> > 
> >           | rv32                   | rv64
> >   --------|------------------------|---------------------
> >    No DCE | 4460684                | 4893488
> >       DCE | 3986716                | 4376400
> >    Shrink |  473968 (~10.6%)       |  517088 (~10.5%)
> > 
> > The config used above only reserves necessary options to boot on qemu
> > with serial console, more like the size-critical embedded scenes:
> > 
> >   - rv64 config: https://pastebin.com/crz82T0s
> >   - rv32 config: rv64 config + 32-bit.config
> > 
> > Signed-off-by: Zhangjin Wu <falcon at tinylab.org>
> > ---
> >  arch/riscv/Kconfig              | 1 +
> >  arch/riscv/kernel/vmlinux.lds.S | 2 +-
> >  2 files changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> > index 348c0fa1fc8c..d2eb1a862ea8 100644
> > --- a/arch/riscv/Kconfig
> > +++ b/arch/riscv/Kconfig
> > @@ -111,6 +111,7 @@ config RISCV
> >  	select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL
> >  	select HAVE_KRETPROBES if !XIP_KERNEL
> >  	select HAVE_RETHOOK if !XIP_KERNEL
> > +	select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
> >  	select HAVE_MOVE_PMD
> >  	select HAVE_MOVE_PUD
> >  	select HAVE_PCI
> > diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
> > index e5f9f4677bbf..0f5dfbc113d4 100644
> > --- a/arch/riscv/kernel/vmlinux.lds.S
> > +++ b/arch/riscv/kernel/vmlinux.lds.S
> > @@ -112,7 +112,7 @@ SECTIONS
> >  	. = ALIGN(8);
> >  	.alternative : {
> >  		__alt_start = .;
> > -		*(.alternative)
> > +		KEEP(*(.alternative))
> >  		__alt_end = .;
> >  	}
> >  	__init_end = .;



More information about the linux-riscv mailing list