From: "H.J. Lu" <hjl.tools@gmail.com>
To: libc-alpha@sourceware.org
Cc: Florian Weimer <fweimer@redhat.com>, Carlos O'Donell <carlos@redhat.com>
Subject: [PATCH 1/3] x86: Initialize CPU info via IFUNC relocation [BZ 26203]
Date: Sat, 12 Sep 2020 06:44:39 -0700 [thread overview]
Message-ID: <20200912134441.2407884-2-hjl.tools@gmail.com> (raw)
In-Reply-To: <20200912134441.2407884-1-hjl.tools@gmail.com>
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. Instead, we should initialize x86 CPU
features and cache info by initializing dummy function pointers via
IFUNC relocation.
Note: _dl_x86_init_cpu_features can be called more than once from
DL_PLATFORM_INIT and during relocation in ld.so.
---
sysdeps/i386/dl-machine.h | 3 +--
sysdeps/x86/cacheinfo.c | 10 +++++++++-
sysdeps/x86/dl-get-cpu-features.c | 25 ++++++++++++++++++++++++-
sysdeps/x86/include/cpu-features.h | 1 +
sysdeps/x86_64/dl-machine.h | 3 +--
5 files changed, 36 insertions(+), 6 deletions(-)
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 ();
#else
if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
/* Avoid an empty string which would disturb us. */
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
@@ -756,7 +756,6 @@ intel_bug_no_cache_info:
static void
-__attribute__((constructor))
init_cacheinfo (void)
{
/* Find out what brand of processor. */
@@ -770,6 +769,8 @@ init_cacheinfo (void)
unsigned int threads = 0;
const struct cpu_features *cpu_features = __get_cpu_features ();
+ assert (cpu_features->basic.kind != arch_kind_unknown);
+
if (cpu_features->basic.kind == arch_kind_intel)
{
data = handle_intel (_SC_LEVEL1_DCACHE_SIZE, cpu_features);
@@ -894,4 +895,11 @@ init_cacheinfo (void)
# endif
}
+/* 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
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
const struct cpu_features *
diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h
index dcf29b6fe8..f62be0b9b3 100644
--- a/sysdeps/x86/include/cpu-features.h
+++ b/sysdeps/x86/include/cpu-features.h
@@ -159,6 +159,7 @@ struct cpu_features
/* Unused for x86. */
# define INIT_ARCH()
# define __x86_get_cpu_features(max) (&GLRO(dl_x86_cpu_features))
+extern void _dl_x86_init_cpu_features (void) attribute_hidden;
# endif
# ifdef __x86_64__
diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
index ca73d8fef9..773e94c8bb 100644
--- a/sysdeps/x86_64/dl-machine.h
+++ b/sysdeps/x86_64/dl-machine.h
@@ -26,7 +26,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))
@@ -225,7 +224,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 ();
#else
if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
/* Avoid an empty string which would disturb us. */
--
2.26.2
next prev parent reply other threads:[~2020-09-12 13:44 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-12 13:44 [PATCH 0/3] ld.so: Add --list-tunables to print tunable values H.J. Lu
2020-09-12 13:44 ` H.J. Lu [this message]
2020-09-18 8:06 ` [PATCH 1/3] x86: Initialize CPU info via IFUNC relocation [BZ 26203] Florian Weimer
2020-09-18 13:55 ` H.J. Lu
2020-09-18 13:59 ` Florian Weimer
2020-09-18 14:18 ` H.J. Lu
2020-09-18 14:22 ` Florian Weimer
2020-09-18 14:30 ` H.J. Lu
2020-09-21 8:21 ` Florian Weimer
2020-09-21 11:31 ` H.J. Lu
2020-09-25 12:55 ` Florian Weimer
2020-09-12 13:44 ` [PATCH 2/3] Set tunable value as well as min/max values H.J. Lu
2020-09-18 8:29 ` Florian Weimer
2020-09-18 15:11 ` H.J. Lu
2020-09-12 13:44 ` [PATCH 3/3] ld.so: Add --list-tunables to print tunable values H.J. Lu
2020-09-18 8:13 ` Florian Weimer
2020-09-18 15:24 ` H.J. Lu
-- strict thread matches above, loose matches on Subject: below --
2020-07-12 14:24 [PATCH 0/3] " H.J. Lu
2020-07-12 14:24 ` [PATCH 1/3] x86: Initialize CPU info via IFUNC relocation [BZ 26203] H.J. Lu
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=20200912134441.2407884-2-hjl.tools@gmail.com \
--to=hjl.tools@gmail.com \
--cc=carlos@redhat.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).