Skip to contentSkip to conversation

Update to C11, fix signal undefined behavior

PATCH In review Ian Rogers 2 participants 10 messages Oct 24, 2022 – Nov 3, 2022 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover Update to C11, fix signal undefined behavior

The use of C11 is mainstream in the kernel [1]. There was some confusion on volatile and signal handlers in [2] for which atomics (present in C11) make things clearer. Switch to using volatile sig_atomic_t as per [3]. Thanks to Leo Yan <[email protected]> for the suggestions.

[1] https://lore.kernel.org/lkml/CAHk-=whWbENRz-vLY6vpESDLj6kGUTKO3khGtVfipHqwewh2HQ@mail.gmail.com/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

v2. Remove unneeded stdatomic.h includes for sig_atomic_t as it is
declared in signal.h.

Ian Rogers (8):
perf build: Update to C standard to gnu11 perf record: Use sig_atomic_t for signal handlers perf daemon: Use sig_atomic_t to avoid UB perf ftrace: Use sig_atomic_t to avoid UB perf session: Change type to avoid UB perf stat: Use sig_atomic_t to avoid UB perf top: Use sig_atomic_t to avoid UB perf trace: Use sig_atomic_t to avoid UB

 tools/perf/Makefile.config  | 2 +-
 tools/perf/builtin-daemon.c | 2 +-
 tools/perf/builtin-ftrace.c | 4 ++--
 tools/perf/builtin-record.c | 8 ++++----
 tools/perf/builtin-stat.c   | 8 ++++----
 tools/perf/builtin-top.c    | 4 ++--
 tools/perf/builtin-trace.c  | 4 ++--
 tools/perf/util/session.c   | 3 ++-
 8 files changed, 18 insertions(+), 17 deletions(-)
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#1 lore raw

PATCH v2 1/8 perf build: Update to C standard to gnu11

C11 has become the standard for mainstream kernel development [1], allowing it in the perf build enables libraries like stdatomic.h to be assumed to be present. This came up in the context of [2].

[1] https://lore.kernel.org/lkml/CAHk-=whWbENRz-vLY6vpESDLj6kGUTKO3khGtVfipHqwewh2HQ@mail.gmail.com/
[2] https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/Makefile.config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.config +1 -1
index 6fd4b1384b97..29c49e6e76a1 100644
@@ -307,7 +307,7 @@ CORE_CFLAGS += -ggdb3
 CORE_CFLAGS += -funwind-tables
 CORE_CFLAGS += -Wall
 CORE_CFLAGS += -Wextra
-CORE_CFLAGS += -std=gnu99
+CORE_CFLAGS += -std=gnu11
 
 CXXFLAGS += -std=gnu++14 -fno-exceptions -fno-rtti
 CXXFLAGS += -Wall
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#2 lore raw

PATCH v2 2/8 perf record: Use sig_atomic_t for signal handlers

In reply to Ian Rogers

This removes undefined behavior as described in:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-record.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/builtin-record.c +4 -4
index e128b855ddde..b8438e323002 100644
@@ -646,10 +646,10 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
 	return record__write(rec, map, bf, size);
 }
 
-static volatile int signr = -1;
-static volatile int child_finished;
+static volatile sig_atomic_t signr = -1;
+static volatile sig_atomic_t child_finished;
 #ifdef HAVE_EVENTFD_SUPPORT
-static volatile int done_fd = -1;
+static volatile sig_atomic_t done_fd = -1;
 #endif
 
 static void sig_handler(int sig)
@@ -1926,7 +1926,7 @@ static void record__read_lost_samples(struct record *rec)
 
 }
 
-static volatile int workload_exec_errno;
+static volatile sig_atomic_t workload_exec_errno;
 
 /*
  * evlist__prepare_workload will send a SIGUSR1
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#3 lore raw

PATCH v2 3/8 perf daemon: Use sig_atomic_t to avoid UB

In reply to Ian Rogers

Use sig_atomic_t for a variable written to in a signal handler and read elsewhere. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-daemon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-daemon.c +1 -1
index 6cb3f6cc36d0..aaa47d721ae7 100644
@@ -105,7 +105,7 @@ static const char * const daemon_usage[] = {
 	NULL
 };
 
-static bool done;
+static volatile sig_atomic_t done;
 
 static void sig_handler(int sig __maybe_unused)
 {
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#4 lore raw

PATCH v2 4/8 perf ftrace: Use sig_atomic_t to avoid UB

In reply to Ian Rogers

Use sig_atomic_t for a variable written to in a signal handler and read elsewhere. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-ftrace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +2 -2
index 7de07bb16d23..d7fe00f66b83 100644
@@ -36,8 +36,8 @@
 
 #define DEFAULT_TRACER  "function_graph"
 
-static volatile int workload_exec_errno;
-static bool done;
+static volatile sig_atomic_t workload_exec_errno;
+static volatile sig_atomic_t done;
 
 static void sig_handler(int sig __maybe_unused)
 {
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#5 lore raw

PATCH v2 5/8 perf session: Change type to avoid UB

In reply to Ian Rogers

session_done is written to inside the signal handler of perf report and script. Switch its type to avoid undefined behavior.

---
 tools/perf/util/session.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/session.c +2 -1
index 1a4f10de29ff..0e1a3d6bacb9 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <errno.h>
+#include <signal.h>
 #include <inttypes.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
@@ -2022,7 +2023,7 @@ static int perf_session__flush_thread_stacks(struct perf_session *session)
 					 NULL);
 }
 
-volatile int session_done;
+volatile sig_atomic_t session_done;
 
 static int __perf_session__process_decomp_events(struct perf_session *session);
 
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#6 lore raw

PATCH v2 6/8 perf stat: Use sig_atomic_t to avoid UB

In reply to Ian Rogers

Use sig_atomic_t for variables written/accessed in signal handlers. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-stat.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/builtin-stat.c +4 -4
index 265b05157972..e1a5605adb97 100644
@@ -173,7 +173,7 @@ static struct target target = {
 
 #define METRIC_ONLY_LEN 20
 
-static volatile pid_t		child_pid			= -1;
+static volatile sig_atomic_t	child_pid			= -1;
 static int			detailed_run			=  0;
 static bool			transaction_run;
 static bool			topdown_run			= false;
@@ -208,7 +208,7 @@ struct perf_stat {
 static struct perf_stat		perf_stat;
 #define STAT_RECORD		perf_stat.record
 
-static volatile int done = 0;
+static volatile sig_atomic_t done = 0;
 
 static struct perf_stat_config stat_config = {
 	.aggr_mode		= AGGR_GLOBAL,
@@ -569,7 +569,7 @@ static void disable_counters(void)
 	}
 }
 
-static volatile int workload_exec_errno;
+static volatile sig_atomic_t workload_exec_errno;
 
 /*
  * evlist__prepare_workload will send a SIGUSR1
@@ -1029,7 +1029,7 @@ static void print_counters(struct timespec *ts, int argc, const char **argv)
 	evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv);
 }
 
-static volatile int signr = -1;
+static volatile sig_atomic_t signr = -1;
 
 static void skip_signal(int signo)
 {
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#7 lore raw

PATCH v2 7/8 perf top: Use sig_atomic_t to avoid UB

In reply to Ian Rogers

Use sig_atomic_t for variables written/accessed in signal handlers. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-top.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-top.c +2 -2
index 4b3ff7687236..bb5bd241246b 100644
@@ -87,8 +87,8 @@
 #include <linux/ctype.h>
 #include <perf/mmap.h>
 
-static volatile int done;
-static volatile int resize;
+static volatile sig_atomic_t done;
+static volatile sig_atomic_t resize;
 
 #define HEADER_LINE_NR  5
 
Signature · 1 line
2.38.0.135.g90850a2211-goog
author reviewer
#8 lore raw

PATCH v2 8/8 perf trace: Use sig_atomic_t to avoid UB

In reply to Ian Rogers

Use sig_atomic_t for variables written/accessed in signal handlers. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

---
 tools/perf/builtin-trace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +2 -2
index d3c757769b96..72991528687e 100644
@@ -1535,8 +1535,8 @@ static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
 }
 
 static pid_t workload_pid = -1;
-static bool done = false;
-static bool interrupted = false;
+static volatile sig_atomic_t done = false;
+static volatile sig_atomic_t interrupted = false;
 
 static void sighandler_interrupt(int sig __maybe_unused)
 {
Signature · 1 line
2.38.0.135.g90850a2211-goog
#9 lore raw
In reply to Ian Rogers

Em Mon, Oct 24, 2022 at 11:19:05AM -0700, Ian Rogers escreveu:

Quoted 5 lines

The use of C11 is mainstream in the kernel [1]. There was some confusion on volatile and signal handlers in [2] for which atomics (present in C11) make things clearer. Switch to using volatile sig_atomic_t as per [3]. Thanks to Leo Yan <[email protected]> for the suggestions.

Thanks, applied and testing building it now.

- Arnaldo

Quoted 29 lines

[1] https://lore.kernel.org/lkml/CAHk-=whWbENRz-vLY6vpESDLj6kGUTKO3khGtVfipHqwewh2HQ@mail.gmail.com/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

v2. Remove unneeded stdatomic.h includes for sig_atomic_t as it is
declared in signal.h.

Ian Rogers (8):
perf build: Update to C standard to gnu11 perf record: Use sig_atomic_t for signal handlers perf daemon: Use sig_atomic_t to avoid UB perf ftrace: Use sig_atomic_t to avoid UB perf session: Change type to avoid UB perf stat: Use sig_atomic_t to avoid UB perf top: Use sig_atomic_t to avoid UB perf trace: Use sig_atomic_t to avoid UB

 tools/perf/Makefile.config  | 2 +-
 tools/perf/builtin-daemon.c | 2 +-
 tools/perf/builtin-ftrace.c | 4 ++--
 tools/perf/builtin-record.c | 8 ++++----
 tools/perf/builtin-stat.c   | 8 ++++----
 tools/perf/builtin-top.c    | 4 ++--
 tools/perf/builtin-trace.c  | 4 ++--
 tools/perf/util/session.c   | 3 ++-
 8 files changed, 18 insertions(+), 17 deletions(-)
Signature · 1 line
2.38.0.135.g90850a2211-goog
Signature · 1 line
- Arnaldo