From: "H.J. Lu" <hjl.tools@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: "H.J. Lu via Libc-alpha" <libc-alpha@sourceware.org>
Subject: Re: [PATCH 1/4] x86: Initialize CPU info via IFUNC relocation [BZ 26203]
Date: Mon, 28 Sep 2020 07:20:01 -0700 [thread overview]
Message-ID: <CAMe9rOqjzDHL+O04FSCzRdnANJK0_3o5pDCd2UGyFsce+mU8Kg@mail.gmail.com> (raw)
In-Reply-To: <87y2ku574k.fsf@oldenburg2.str.redhat.com>
On Mon, Sep 28, 2020 at 7:05 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> >> > diff --git a/sysdeps/x86/cacheinfo.c b/sysdeps/x86/cacheinfo.c
> >> > index 217c21c34f..7a325ab70e 100644
> >> > --- a/sysdeps/x86/cacheinfo.c
> >> > +++ b/sysdeps/x86/cacheinfo.c
> >>
> >> > + assert (cpu_features->basic.kind != arch_kind_unknown);
> >>
> >> Why doesn't this assert fire occasionally? How do you ensure that
> >
> > See
> >
> > https://sourceware.org/bugzilla/show_bug.cgi?id=26203
> >
> > It only happens in dlopen from a static executable.
>
> Sorry, I don't understand how this answers my question.
>
> Do you mean that for the non-static case, initialization has already
> happened.
Yes, it is initialized by DL_PLATFORM_INIT in a dynamic executable.
> >> relocation processing is correctly ordered?
> >
> > cpu_features is also initialized by IFUNC relocation in ld.so which
> > is relocated before libc.so.
>
> Is that really true in all cases? Even if libc.so is preloaded?
Not if init_cacheinfo is a constructor function.
> (Static dlopen probably ignores LD_PRELOAD.)
Correct.
> Maybe put this information as a comment next to the assert?
Will do.
> But since cacheinfo.os is linked into libc.so, I don't really think the
> assert is correct.
When init_cacheinfo is called, cpu_features must be initialized.
> >> > diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
> >> > index 5f9e46b0c6..da4697b895 100644
> >> > --- a/sysdeps/x86/dl-get-cpu-features.c
> >> > +++ b/sysdeps/x86/dl-get-cpu-features.c
> >> > @@ -1,4 +1,4 @@
> >> > -/* This file is part of the GNU C Library.
> >> > +/* Initialize CPU feature data via IFUNC relocation.
> >> > Copyright (C) 2015-2020 Free Software Foundation, Inc.
> >> >
> >> > The GNU C Library is free software; you can redistribute it and/or
> >> > @@ -18,6 +18,29 @@
> >> >
> >> > #include <ldsodefs.h>
> >> >
> >> > +#ifdef SHARED
> >> > +# include <cpu-features.c>
> >> > +
> >> > +/* NB: Normally, DL_PLATFORM_INIT calls init_cpu_features to initialize
> >> > + CPU features. But when loading ld.so inside of static executable,
> >> > + DL_PLATFORM_INIT isn't called. Call init_cpu_features by initializing
> >> > + a dummy function pointer via IFUNC relocation for ld.so. */
> >> > +extern void __x86_cpu_features (void) attribute_hidden;
> >> > +const void (*__x86_cpu_features_p) (void) attribute_hidden
> >> > + = __x86_cpu_features;
> >> > +
> >> > +void
> >> > +_dl_x86_init_cpu_features (void)
> >> > +{
> >> > + struct cpu_features *cpu_features = __get_cpu_features ();
> >> > + if (cpu_features->basic.kind == arch_kind_unknown)
> >> > + init_cpu_features (cpu_features);
> >> > +}
> >> > +
> >> > +__ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
> >> > + _dl_x86_init_cpu_features);
> >> > +#endif
> >> > +
> >> > #undef __x86_get_cpu_features
> >>
> >> Why do we need both the conditional check and the function pointer hack?
> >
> > Because _dl_x86_init_cpu_features is called both indirectly and by IFUNC
> > reloc in dynamic executable, but it is only called by IFUNC reloc when
> > dlopen in static executable.
>
> I think we always need to call it eventually, as a dependency of filling
> in the cacheinfo data?
Yes. _dl_x86_init_cpu_features is called twice by DL_PLATFORM_INIT
and IFUNC reloc in dynamic executable. It is called once by IFUNC reloc
via dlopen in static executable.
> >> I expect that one of the function pointers can go, probably the one
> >> here. The cache hierarchy data might be used by a string function that
> >> has not been selected by IFUNC.
> >>
> >
> > There are one IFUNC reloc in ld.so and the other in libc.so. We need
> > both.
>
> libc.so should not need the relocation hack because we have
> __libc_early_init, which is also called after static dlopen and before
> constructors.
>
We want to call init_cacheinfo as early as possible. __libc_early_init is
still too late.
--
H.J.
next prev parent reply other threads:[~2020-09-28 14:20 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-18 16:07 V2 [PATCH 0/4] ld.so: Add --list-tunables to print tunable values H.J. Lu
2020-09-18 16:07 ` [PATCH 1/4] x86: Initialize CPU info via IFUNC relocation [BZ 26203] H.J. Lu
2020-09-28 13:08 ` Florian Weimer
2020-09-28 13:48 ` H.J. Lu
2020-09-28 14:05 ` Florian Weimer
2020-09-28 14:20 ` H.J. Lu [this message]
2020-09-28 14:22 ` Florian Weimer
2020-09-28 14:39 ` H.J. Lu
2020-09-28 14:47 ` Florian Weimer
2020-09-28 17:54 ` V3 [PATCH] " H.J. Lu
2020-09-29 7:53 ` Florian Weimer
2020-09-29 11:44 ` H.J. Lu
2020-10-01 8:46 ` Florian Weimer
2020-10-01 19:50 ` V4 " H.J. Lu
2020-10-08 13:22 ` PING: " H.J. Lu
2020-10-15 12:53 ` PING^2: " H.J. Lu
2022-05-02 13:59 ` Sunil Pandey
2022-05-03 18:51 ` Sunil Pandey
2020-09-18 16:07 ` [PATCH 2/4] Set tunable value as well as min/max values H.J. Lu
2020-09-28 13:35 ` Florian Weimer
2020-09-28 13:53 ` H.J. Lu
2020-09-28 14:03 ` Florian Weimer
2020-09-28 17:30 ` Siddhesh Poyarekar
2020-09-29 4:00 ` V3 [PATCH] " H.J. Lu
2020-09-29 4:45 ` Siddhesh Poyarekar
2020-09-29 4:47 ` Siddhesh Poyarekar
2020-09-29 12:30 ` V4 " H.J. Lu
2020-09-29 13:50 ` Siddhesh Poyarekar
2020-09-29 14:54 ` V5 " H.J. Lu
2020-09-29 15:58 ` Siddhesh Poyarekar
2020-09-18 16:07 ` [PATCH 3/4] x86: Move x86 processor cache info to cpu_features H.J. Lu
2020-09-18 16:07 ` [PATCH 4/4] ld.so: Add --list-tunables to print tunable values H.J. Lu
2020-09-21 8:25 ` Florian Weimer
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=CAMe9rOqjzDHL+O04FSCzRdnANJK0_3o5pDCd2UGyFsce+mU8Kg@mail.gmail.com \
--to=hjl.tools@gmail.com \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/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).