public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <maskray@google.com>
To: fweimer@redhat.com, Palmer Dabbelt <palmer@dabbelt.com>
Cc: l.stelmach@samsung.com, libc-alpha@sourceware.org,
	schwab@suse.de, adhemerval.zanella@linaro.org,
	joseph@codesourcery.com, binutils@sourceware.org,
	m.pikula@partner.samsung.com, m.szyprowski@samsung.com,
	k.lewandowsk@samsung.com, Jeff Law <jeffreyalaw@gmail.com>
Subject: Re: global pointer gets overwritten with dlopen(3) on RISC-V
Date: Fri, 12 May 2023 21:09:08 +0000	[thread overview]
Message-ID: <20230512210908.osbbhn4hl5pjldof@google.com> (raw)
In-Reply-To: <mhng-82428a70-5ed4-4a50-a032-d5dc7fcaf347@palmer-ri-x1c9a>

On 2023-05-12, Palmer Dabbelt wrote:
>On Fri, 12 May 2023 13:11:43 PDT (-0700), fweimer@redhat.com wrote:
>>* Fangrui Song:
>>
>>>>1. Make -mno-relax the default for ld(1) (on Linux?). We have no
>>>>benchmarks whatsoever, but global variables aren't very popular in
>>>>application code these days and the gp register allows access to a
>>>>single memory page (4kB) only. No big deal really.
>>>
>>>I do agree that --no-relax-gp is a sensible default choice for GNU ld.
>>>https://maskray.me/blog/2021-03-14-the-dark-side-of-riscv-linker-relaxation#global-pointer-relaxation
>>>
>>>Perhaps you can start a separate topic on binutils? :)
>>>
>>>According to a doc from SiFive about -static -mcpu=sifive-u74 builds,
>>>https://docs.google.com/spreadsheets/d/14V7cPbyc80AcGHzsMaw9hYb232dzRbGCmTApnxj-SpU/edit#gid=0
>>>global pointer relaxation saves at best 0.5% size (I guess that refers
>>>to .text. If we count all allocable sections, the percentage is likely
>>>even smaller.)
>>
>>For a mature toolchain, 0.5% in code size reduction would be *a lot*,
>>so I wouldn't dismiss that.
>
>That's broadly speaking why it sticks around.  We've got a bunch of 
>headaches related to relaxation, GP or otherwise, but they improve 
>performance and nobody's figured out how to replace that yet.
>
>>Do we have a reproducer?  Is the issue actually about gp relaxation for
>>the main executable?
>
>In general we don't reference GP from shared libraries as we don't 
>have a GP save/restore scheme.  There may be a bug floating around 
>here somewhere, in which case we should fix it, but the original post 
>sounds like it wasn't a supported use case.
>
>>Thanks,
>>Florian

Global pointer relaxation only applies to +-2KiB data relative to __global_pointer$ (= .sdata + 0x800).
The area that potentially benefits global pointer relaxation is very small.

0.5% code size reduction (relative to .text?) is the best case. I
suspect the program somehow has a lot global variable accesses and
placing these variables in .sdata helps.

I've got results from Yingwei Zheng at PLCT lab using many
configurations. The saving is like 0.1%.
https://docs.google.com/spreadsheets/d/1Gz0h-C4U0toa9qELFtRaEWT_CzauE5JD9xMsLR8RyK8/edit#gid=1721258109

On the binutils side, we occasionally see patches to fix global pointer
relaxation bugs, e.g. the patch just sent a few hours ago:
https://sourceware.org/pipermail/binutils/2023-May/127413.html

I do not know the embedded toolchain well, but for Linux desktop/server,
disabling global pointer relaxation seems like a sensible choice. If we
discover a better way to utilize GP (x3) in the future, disabling global
pointer relaxation today will result in fewer compatibility issues.

Haiku
(https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/298#issuecomment-1344724796), Android, and Fuchsia
have mentioned that they don't use global pointer relaxation.

  reply	other threads:[~2023-05-12 21:09 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20230512142114eucas1p112d969a89ad2480a0c10a532bd6d8440@eucas1p1.samsung.com>
2023-05-12 14:21 ` Lukasz Stelmach
2023-05-12 15:13   ` Florian Weimer
2023-05-15 13:47     ` Palmer Dabbelt
     [not found]       ` <CGME20230516065316eucas1p17bffcd25209bb441b9a9f4d263aa8b3c@eucas1p1.samsung.com>
2023-05-16  6:53         ` Lukasz Stelmach
2023-05-16  7:59           ` Szabolcs Nagy
2023-05-17  0:11             ` Palmer Dabbelt
2023-05-12 19:50   ` Fangrui Song
2023-05-12 20:11     ` Florian Weimer
2023-05-12 20:33       ` Palmer Dabbelt
2023-05-12 21:09         ` Fangrui Song [this message]
2023-05-12 21:57           ` Palmer Dabbelt
2023-05-12 22:34             ` Fangrui Song
2023-05-12 22:47               ` Palmer Dabbelt
2023-05-13  0:05                 ` Fangrui Song
2023-05-13  0:35                   ` Palmer Dabbelt
2023-05-16  3:56                     ` Fangrui Song
2023-05-16 22:51                       ` Palmer Dabbelt
2023-05-16 23:21                         ` Palmer Dabbelt
2023-05-12 20:35       ` Jeff Law

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=20230512210908.osbbhn4hl5pjldof@google.com \
    --to=maskray@google.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=binutils@sourceware.org \
    --cc=fweimer@redhat.com \
    --cc=jeffreyalaw@gmail.com \
    --cc=joseph@codesourcery.com \
    --cc=k.lewandowsk@samsung.com \
    --cc=l.stelmach@samsung.com \
    --cc=libc-alpha@sourceware.org \
    --cc=m.pikula@partner.samsung.com \
    --cc=m.szyprowski@samsung.com \
    --cc=palmer@dabbelt.com \
    --cc=schwab@suse.de \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).