From: Florian Weimer <fweimer@redhat.com>
To: "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 15:08:35 +0200 [thread overview]
Message-ID: <87imby6obw.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <20200918160709.949608-2-hjl.tools@gmail.com> (H. J. Lu via Libc-alpha's message of "Fri, 18 Sep 2020 09:07:06 -0700")
* H. J. Lu via Libc-alpha:
> X86 CPU features in ld.so are initialized by init_cpu_features, which is
> invoked by DL_PLATFORM_INIT from _dl_sysdep_start. But when ld.so is
> loaded by static executable, DL_PLATFORM_INIT is never called. Also
> x86 cache info in libc.o and libc.a is initialized by a constructor
> which may be called too late. Since _rtld_global_ro in ld.so is
> initialized by dynamic relocation, we can also initialize x86 CPU
> features in _rtld_global_ro in ld.so and cache info in libc.so by
> initializing dummy function pointers in ld.so and libc.so via IFUNC
> relocation.
_rtld_global_ro is *partially* initialized by relocation. Most of it is
not initialized (see the need for rtld_active).
Please make this a little bit clearer in the commit message.
> diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
> index 0f08079e48..5e22e795cc 100644
> --- a/sysdeps/i386/dl-machine.h
> +++ b/sysdeps/i386/dl-machine.h
> @@ -25,7 +25,6 @@
> #include <sysdep.h>
> #include <tls.h>
> #include <dl-tlsdesc.h>
> -#include <cpu-features.c>
>
> /* Return nonzero iff ELF header is compatible with the running host. */
> static inline int __attribute__ ((unused))
> @@ -250,7 +249,7 @@ dl_platform_init (void)
> #if IS_IN (rtld)
> /* init_cpu_features has been called early from __libc_start_main in
> static executable. */
> - init_cpu_features (&GLRO(dl_x86_cpu_features));
> + _dl_x86_init_cpu_features ();
Is the commented outdated?
> 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
relocation processing is correctly ordered?
> +/* NB: Call init_cacheinfo by initializing a dummy function pointer via
> + IFUNC relocation. */
> +extern void __x86_cacheinfo (void) attribute_hidden;
> +const void (*__x86_cacheinfo_p) (void) attribute_hidden
> + = __x86_cacheinfo;
> +
> +__ifunc (__x86_cacheinfo, __x86_cacheinfo, NULL, void, init_cacheinfo);
> #endif
Please expand the comment and mention that is used to initialize the
dormant copy of ld.so after static dlopen. The comment in
sysdeps/x86/dl-get-cpu-features.c is good.
> 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?
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.
Thanks,
Florian
--
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill
next prev parent reply other threads:[~2020-09-28 13:08 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 [this message]
2020-09-28 13:48 ` H.J. Lu
2020-09-28 14:05 ` Florian Weimer
2020-09-28 14:20 ` H.J. Lu
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=87imby6obw.fsf@oldenburg2.str.redhat.com \
--to=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).