perf tools: Avoid 'sample_reg_masks' being const + weak
authorIan Rogers <irogers@google.com>
Tue, 1 Oct 2019 00:36:23 +0000 (17:36 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 10 Oct 2019 12:29:33 +0000 (09:29 -0300)
Being const + weak breaks with some compilers that constant-propagate
from the weak symbol. This behavior is outside of the specification, but
in LLVM is chosen to match GCC's behavior.

LLVM's implementation was set in this patch:

  https://github.com/llvm/llvm-project/commit/f49573d1eedcf1e44893d5a062ac1b72c8419646

A const + weak symbol is set to be weak_odr:

  https://llvm.org/docs/LangRef.html

ODR is one definition rule, and given there is one constant definition
constant-propagation is possible. It is possible to get this code to
miscompile with LLVM when applying link time optimization. As compilers
become more aggressive, this is likely to break in more instances.

Move the definition of sample_reg_masks to the conditional part of
perf_regs.h and guard usage with HAVE_PERF_REGS_SUPPORT. This avoids the
weak symbol.

Fix an issue when HAVE_PERF_REGS_SUPPORT isn't defined from patch v1.
In v3, add perf_regs.c for architectures that HAVE_PERF_REGS_SUPPORT but
don't declare sample_regs_masks.

Further notes:

Jiri asked:

  "Is this just a precaution or you actualy saw some breakage?"

Ian answered:

  "We saw a breakage with clang with thinlto enabled for linking. Our
   compiler team had recently seen, and were surprised by, a similar issue
   and were able to dig out the weak ODR issue."

Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: clang-built-linux@googlegroups.com
Cc: Guo Ren <guoren@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: linux-riscv@lists.infradead.org
Cc: Mao Han <han_mao@c-sky.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Palmer Dabbelt <palmer@sifive.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20191001003623.255186-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
13 files changed:
tools/perf/arch/arm/util/Build
tools/perf/arch/arm/util/perf_regs.c [new file with mode: 0644]
tools/perf/arch/arm64/util/Build
tools/perf/arch/arm64/util/perf_regs.c [new file with mode: 0644]
tools/perf/arch/csky/util/Build
tools/perf/arch/csky/util/perf_regs.c [new file with mode: 0644]
tools/perf/arch/riscv/util/Build
tools/perf/arch/riscv/util/perf_regs.c [new file with mode: 0644]
tools/perf/arch/s390/util/Build
tools/perf/arch/s390/util/perf_regs.c [new file with mode: 0644]
tools/perf/util/parse-regs-options.c
tools/perf/util/perf_regs.c
tools/perf/util/perf_regs.h

index 296f0eac5e18ca5f26105c0b55de9e0bdde49fa2..37fc63708966d49cc12885649f0511c6a1aeabaf 100644 (file)
@@ -1,3 +1,5 @@
+perf-y += perf_regs.o
+
 perf-$(CONFIG_DWARF) += dwarf-regs.o
 
 perf-$(CONFIG_LOCAL_LIBUNWIND)    += unwind-libunwind.o
diff --git a/tools/perf/arch/arm/util/perf_regs.c b/tools/perf/arch/arm/util/perf_regs.c
new file mode 100644 (file)
index 0000000..2864e2e
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../util/perf_regs.h"
+
+const struct sample_reg sample_reg_masks[] = {
+       SMPL_REG_END
+};
index 3cde540d2fcf3c99be3f936e4efef02a10547a4d..0a7782c61209e74024e70c36655fca2c316e9831 100644 (file)
@@ -1,4 +1,5 @@
 perf-y += header.o
+perf-y += perf_regs.o
 perf-y += sym-handling.o
 perf-$(CONFIG_DWARF)     += dwarf-regs.o
 perf-$(CONFIG_LOCAL_LIBUNWIND) += unwind-libunwind.o
diff --git a/tools/perf/arch/arm64/util/perf_regs.c b/tools/perf/arch/arm64/util/perf_regs.c
new file mode 100644 (file)
index 0000000..2864e2e
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../util/perf_regs.h"
+
+const struct sample_reg sample_reg_masks[] = {
+       SMPL_REG_END
+};
index 1160bb2332bad22be319c9802feff2cdd4111c8c..7d3050134ae0fd4b1bacabb41a95fa51903beff3 100644 (file)
@@ -1,2 +1,4 @@
+perf-y += perf_regs.o
+
 perf-$(CONFIG_DWARF) += dwarf-regs.o
 perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
diff --git a/tools/perf/arch/csky/util/perf_regs.c b/tools/perf/arch/csky/util/perf_regs.c
new file mode 100644 (file)
index 0000000..2864e2e
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../util/perf_regs.h"
+
+const struct sample_reg sample_reg_masks[] = {
+       SMPL_REG_END
+};
index 1160bb2332bad22be319c9802feff2cdd4111c8c..7d3050134ae0fd4b1bacabb41a95fa51903beff3 100644 (file)
@@ -1,2 +1,4 @@
+perf-y += perf_regs.o
+
 perf-$(CONFIG_DWARF) += dwarf-regs.o
 perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
diff --git a/tools/perf/arch/riscv/util/perf_regs.c b/tools/perf/arch/riscv/util/perf_regs.c
new file mode 100644 (file)
index 0000000..2864e2e
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../util/perf_regs.h"
+
+const struct sample_reg sample_reg_masks[] = {
+       SMPL_REG_END
+};
index 22797f043b844378d7889d6243a38b079e7336d1..3d9d0f4f72ca1f50f70bfb0e57e82a1d62bbebdc 100644 (file)
@@ -1,5 +1,6 @@
 perf-y += header.o
 perf-y += kvm-stat.o
+perf-y += perf_regs.o
 
 perf-$(CONFIG_DWARF) += dwarf-regs.o
 perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
diff --git a/tools/perf/arch/s390/util/perf_regs.c b/tools/perf/arch/s390/util/perf_regs.c
new file mode 100644 (file)
index 0000000..2864e2e
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../util/perf_regs.h"
+
+const struct sample_reg sample_reg_masks[] = {
+       SMPL_REG_END
+};
index ef46c284880854de3574120e37a99b638242c998..e687497b3aac0c935182e234a7630bf86785e7a1 100644 (file)
@@ -13,7 +13,7 @@ static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
        uint64_t *mode = (uint64_t *)opt->value;
-       const struct sample_reg *r;
+       const struct sample_reg *r = NULL;
        char *s, *os = NULL, *p;
        int ret = -1;
        uint64_t mask;
@@ -46,19 +46,23 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 
                        if (!strcmp(s, "?")) {
                                fprintf(stderr, "available registers: ");
+#ifdef HAVE_PERF_REGS_SUPPORT
                                for (r = sample_reg_masks; r->name; r++) {
                                        if (r->mask & mask)
                                                fprintf(stderr, "%s ", r->name);
                                }
+#endif
                                fputc('\n', stderr);
                                /* just printing available regs */
                                return -1;
                        }
+#ifdef HAVE_PERF_REGS_SUPPORT
                        for (r = sample_reg_masks; r->name; r++) {
                                if ((r->mask & mask) && !strcasecmp(s, r->name))
                                        break;
                        }
-                       if (!r->name) {
+#endif
+                       if (!r || !r->name) {
                                ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
                                            s, intr ? "-I" : "--user-regs=");
                                goto error;
index 2774cec1f15fa0561f8e68ae01cb03c3757a8c14..5ee47ae1509c67fcf015ae5e52637d27272af423 100644 (file)
@@ -3,10 +3,6 @@
 #include "perf_regs.h"
 #include "event.h"
 
-const struct sample_reg __weak sample_reg_masks[] = {
-       SMPL_REG_END
-};
-
 int __weak arch_sdt_arg_parse_op(char *old_op __maybe_unused,
                                 char **new_op __maybe_unused)
 {
index 47fe34e5f7d5bed647e37537b39c583879abcb14..e014c2c038f46eb9cece0fb3e9c46eef228973be 100644 (file)
@@ -15,8 +15,6 @@ struct sample_reg {
 #define SMPL_REG2(n, b) { .name = #n, .mask = 3ULL << (b) }
 #define SMPL_REG_END { .name = NULL }
 
-extern const struct sample_reg sample_reg_masks[];
-
 enum {
        SDT_ARG_VALID = 0,
        SDT_ARG_SKIP,
@@ -27,6 +25,8 @@ uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
 
 #ifdef HAVE_PERF_REGS_SUPPORT
+extern const struct sample_reg sample_reg_masks[];
+
 #include <perf_regs.h>
 
 #define DWARF_MINIMAL_REGS ((1ULL << PERF_REG_IP) | (1ULL << PERF_REG_SP))