selftests/bpf: Clean up call sites of stdio_restore()
Subsystems (3)
PATCH v4 1/3 selftests/bpf: Clean up call sites of stdio_restore()
There is no need to call a bunch of stdio_restore() in test_progs if the scope of stdio redirection is reduced to what it needs to be: only hijacking tests/subtests' stdio.
Also remove an unnecessary check of env.stdout_saved in the crash handler.
--- tools/testing/selftests/bpf/test_progs.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_progs.c +6 -11
@@ -474,8 +474,6 @@ static void dump_test_log(const struct prog_test_def *test,print_test_result (test, test_state); } -static void stdio_restore (void ); -/* A bunch of tests set custom affinity per-thread and/or per-process. Reset * it after each test/sub-test. */
@@ -490,13 +488,11 @@ static void reset_affinity(void) err =sched_setaffinity (0 ,sizeof (cpuset), &cpuset);if (err <0 ) { -stdio_restore ();fprintf (stderr,"Failed to reset process affinity: %d!\n" , err);exit (EXIT_ERR_SETUP_INFRA ); } err =pthread_setaffinity_np (pthread_self (),sizeof (cpuset), &cpuset);if (err <0 ) { -stdio_restore ();fprintf (stderr,"Failed to reset thread affinity: %d!\n" , err);exit (EXIT_ERR_SETUP_INFRA ); }
@@ -514,7 +510,6 @@ static void save_netns(void)static void restore_netns (void ) {if (setns (env.saved_netns_fd,CLONE_NEWNET ) ==-1 ) { -stdio_restore ();perror ("setns(CLONE_NEWNS)" );exit (EXIT_ERR_SETUP_INFRA ); }
@@ -1270,8 +1265,7 @@ void crash_handler(int signum) sz =backtrace (bt,ARRAY_SIZE (bt)); -if (env.stdout_saved) -stdio_restore (); +stdio_restore ();if (env.test) { env.test_state->error_cnt++;dump_test_log (env.test, env.test_state, true, false,NULL );
@@ -1400,6 +1394,8 @@ static void run_one_test(int test_num) state->tested = true; +stdio_restore (); +if (verbose () && env.worker_id ==-1 )print_test_result (test, state);
@@ -1408,7 +1404,6 @@ static void run_one_test(int test_num)if (test->need_cgroup_cleanup)cleanup_cgroup_environment (); -stdio_restore ();free (stop_libbpf_log_capture ());dump_test_log (test, state, false, false,NULL );
@@ -1943,6 +1938,9 @@ int main(int argc, char **argv)sigaction (SIGSEGV , &sigact,NULL ); + env.stdout_saved = stdout; + env.stderr_saved = stderr; + env.secs_till_notify =10 ; env.secs_till_kill =120 ; err =argp_parse (&argp, argc, argv,0 ,NULL , &env);
@@ -1969,9 +1967,6 @@ int main(int argc, char **argv)return -1 ; } - env.stdout_saved = stdout; - env.stderr_saved = stderr; - env.has_testmod = true;if (!env.list_test_names) {/* ensure previous instance of the module is unloaded */
Signature · 1 line
2.47.1
PATCH v4 2/3 selftests/bpf: Allow assigning traffic monitor print function
Allow users to change traffic monitor's print function. If not provided, traffic monitor will print to stdout by default.
--- tools/testing/selftests/bpf/network_helpers.c | 69 ++++++++++++++----- tools/testing/selftests/bpf/network_helpers.h | 8 +++ 2 files changed, 58 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +50 -19
@@ -750,6 +750,36 @@ struct tmonitor_ctx {int pcap_fd; }; +static int __base_pr (const char *format,va_list args) +{ +return vfprintf (stdout, format, args); +} + +static tm_print_fn_t __tm_pr = __base_pr; + +tm_print_fn_t traffic_monitor_set_print (tm_print_fn_t fn) +{ +tm_print_fn_t old_print_fn; + + old_print_fn =__atomic_exchange_n (&__tm_pr, fn, __ATOMIC_RELAXED); + +return old_print_fn; +} + +void tm_print (const char *format, ...) +{ +tm_print_fn_t print_fn; +va_list args; + + print_fn =__atomic_load_n (&__tm_pr, __ATOMIC_RELAXED); +if (!print_fn) +return ; + +va_start (args, format); +__tm_pr (format, args); +va_end (args); +} +/* Is this packet captured with a Ethernet protocol type? */ static bool is_ethernet (const u_char *packet) {
@@ -767,7 +797,7 @@ static bool is_ethernet(const u_char *packet)case 770 :/* ARPHRD_FRAD */ case 778 :/* ARPHDR_IPGRE */ case 803 :/* ARPHRD_IEEE80211_RADIOTAP */ -printf ("Packet captured: arphdr_type=%d\n" , arphdr_type); +tm_print ("Packet captured: arphdr_type=%d\n" , arphdr_type);return false; }return true;
@@ -817,19 +847,19 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex, dst_port =ntohs (tcp->dest); transport_str ="TCP" ; }else if (proto ==IPPROTO_ICMP ) { -printf ("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n" , - ifname,pkt_type_str (pkt_type), src_addr, dst_addr, len, - packet[0 ], packet[1 ]); +tm_print ("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n" , + ifname,pkt_type_str (pkt_type), src_addr, dst_addr, len, + packet[0 ], packet[1 ]);return ; }else if (proto ==IPPROTO_ICMPV6 ) { -printf ("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n" , - ifname,pkt_type_str (pkt_type), src_addr, dst_addr, len, - packet[0 ], packet[1 ]); +tm_print ("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n" , + ifname,pkt_type_str (pkt_type), src_addr, dst_addr, len, + packet[0 ], packet[1 ]);return ; }else { -printf ("%-7s %-3s %s %s > %s: protocol %d\n" , - ifname,pkt_type_str (pkt_type), ipv6 ?"IPv6" :"IPv4" , - src_addr, dst_addr, proto); +tm_print ("%-7s %-3s %s %s > %s: protocol %d\n" , + ifname,pkt_type_str (pkt_type), ipv6 ?"IPv6" :"IPv4" , + src_addr, dst_addr, proto);return ; }
@@ -843,13 +873,13 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex, tcp->ack ? ", ACK" : "");if (ipv6) -printf ("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n" , - ifname,pkt_type_str (pkt_type), src_addr, src_port, - dst_addr, dst_port, transport_str, len, flags); +tm_print ("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n" , + ifname,pkt_type_str (pkt_type), src_addr, src_port, + dst_addr, dst_port, transport_str, len, flags);else -printf ("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n" , - ifname,pkt_type_str (pkt_type), src_addr, src_port, - dst_addr, dst_port, transport_str, len, flags); +tm_print ("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n" , + ifname,pkt_type_str (pkt_type), src_addr, src_port, + dst_addr, dst_port, transport_str, len, flags); }static void show_ipv6_packet (const u_char *packet,u32 ifindex,u8 pkt_type)
@@ -964,8 +994,8 @@ static void *traffic_monitor_thread(void *arg) ifname = _ifname; } -printf ("%-7s %-3s Unknown network protocol type 0x%x\n" , - ifname,pkt_type_str (ptype), proto); +tm_print ("%-7s %-3s Unknown network protocol type 0x%x\n" , + ifname,pkt_type_str (ptype), proto); } }
@@ -1165,8 +1195,9 @@ void traffic_monitor_stop(struct tmonitor_ctx *ctx)write (ctx->wake_fd, &w,sizeof (w));pthread_join (ctx->thread,NULL ); -printf ("Packet file: %s\n" ,strrchr (ctx->pkt_fname,'/' ) +1 ); +tm_print ("Packet file: %s\n" ,strrchr (ctx->pkt_fname,'/' ) +1 ); traffic_monitor_release(ctx); } +#endif /* TRAFFIC_MONITOR */
tools/testing/selftests/bpf/network_helpers.h +8 -0
@@ -249,10 +249,13 @@ static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,struct tmonitor_ctx ; +typedef int (*tm_print_fn_t )(const char *format,va_list args); +#ifdef TRAFFIC_MONITOR struct tmonitor_ctx *traffic_monitor_start (const char *netns,const char *test_name,const char *subtest_name);void traffic_monitor_stop (struct tmonitor_ctx *ctx); +tm_print_fn_t traffic_monitor_set_print (tm_print_fn_t fn);#else static inline struct tmonitor_ctx *traffic_monitor_start (const char *netns,const char *test_name,const char *subtest_name)
@@ -263,6 +266,11 @@ static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, consstatic inline void traffic_monitor_stop (struct tmonitor_ctx *ctx) { } + +static inline tm_print_fn_t traffic_monitor_set_print (tm_print_fn_t fn) +{ +return NULL ; +}#endif #endif
Signature · 1 line
2.47.1
PATCH v4 3/3 selftests/bpf: Fix dangling stdout seen by traffic monitor thread
In reply to Amery HungTraffic monitor thread may see dangling stdout as the main thread closes and reassigns stdout without protection. This happens when the main thread finishes one subtest and moves to another one in the same netns_new() scope.
The issue can be reproduced by running test_progs repeatedly with traffic monitor enabled:
for ((i=1;i<=100;i++)); do
./test_progs -a flow_dissector_skb* -m '*'
done
Fix it by first consolidating stdout assignment into stdio_restore(). stdout will be restored to env.stdout_saved when a test ends or running in the crash handler and to test_state.stdout_saved otherwise. Then, protect use/close/reassignment of stdout with a lock. The locking in the main thread is always performed regradless of whether traffic monitor is running or not for simplicity. It won't have any side-effect. stdio_restore() is kept in the crash handler instead of making all print functions in the crash handler use env.stdout_saved to make it less error-prone.
--- tools/testing/selftests/bpf/test_progs.c | 59 ++++++++++++++++-------- 1 file changed, 39 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/test_progs.c +39 -20
@@ -88,7 +88,11 @@ static void stdio_hijack(char **log_buf, size_t *log_cnt)#endif } -static void stdio_restore_cleanup (void ) +static pthread_mutex_t stdout_lock =PTHREAD_MUTEX_INITIALIZER ; + +static bool in_crash_handler (void ); + +static void stdio_restore (void ) {#ifdef __GLIBC__if (verbose () && env.worker_id ==-1 ) {
@@ -98,34 +102,34 @@ static void stdio_restore_cleanup(void)fflush (stdout); -if (env.subtest_state) { +pthread_mutex_lock (&stdout_lock); + +if (!env.subtest_state ||in_crash_handler ()) { +if (stdout == env.stdout_saved) + goto out; + +fclose (env.test_state->stdout_saved); + env.test_state->stdout_saved =NULL ; + stdout = env.stdout_saved; + stderr = env.stderr_saved; + }else {fclose (env.subtest_state->stdout_saved); env.subtest_state->stdout_saved =NULL ; stdout = env.test_state->stdout_saved; stderr = env.test_state->stdout_saved; - }else { -fclose (env.test_state->stdout_saved); - env.test_state->stdout_saved =NULL ; } +out: +pthread_mutex_unlock (&stdout_lock);#endif } -static void stdio_restore (void ) +static int traffic_monitor_print_fn (const char *format,va_list args) { -#ifdef __GLIBC__ -if (verbose () && env.worker_id ==-1 ) { -/* nothing to do, output to stdout by default */ -return ; - } - -if (stdout == env.stdout_saved) -return ; - -stdio_restore_cleanup (); +pthread_mutex_lock (&stdout_lock); +vfprintf (stdout, format, args); +pthread_mutex_unlock (&stdout_lock); - stdout = env.stdout_saved; - stderr = env.stderr_saved; -#endif +return 0 ; }/* Adapted from perf/util/string.c */
@@ -536,7 +540,8 @@ void test__end_subtest(void)test_result (subtest_state->error_cnt, subtest_state->skipped)); -stdio_restore_cleanup (); +stdio_restore (); + env.subtest_state =NULL ; }
@@ -1276,6 +1281,18 @@ void crash_handler(int signum)backtrace_symbols_fd (bt, sz,STDERR_FILENO ); } +static bool in_crash_handler (void ) +{ +struct sigaction sigact; + +/* sa_handler will be cleared if invoked since crash_handler is +* registered with SA_RESETHAND +*/ +sigaction (SIGSEGV ,NULL , &sigact); + +return sigact.sa_handler != crash_handler; +} +void hexdump (const char *prefix,const void *buf,size_t len) {for (int i =0 ; i < len; i++) {
@@ -1957,6 +1974,8 @@ int main(int argc, char **argv)libbpf_set_strict_mode (LIBBPF_STRICT_ALL );libbpf_set_print (libbpf_print_fn); +traffic_monitor_set_print (traffic_monitor_print_fn); +srand (time (NULL )); env.jit_enabled =is_jit_enabled ();
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v4 3/3] selftests/bpf: Fix dangling stdout seen by traffic monitor thread
Quoted 21 lines · Amery Hung
Traffic monitor thread may see dangling stdout as the main thread closes and reassigns stdout without protection. This happens when the main thread finishes one subtest and moves to another one in the same
netns_new()scope.
The issue can be reproduced by runningtest_progsrepeatedly with traffic monitor enabled:for ((i=1;i<=100;i++)); do
./test_progs -a flow_dissector_skb* -m '*'
doneFix it by first consolidating stdout assignment into
stdio_restore(). stdout will be restored to env.stdout_saved when a test ends or running in the crash handler and to test_state.stdout_saved otherwise. Then, protect use/close/reassignment of stdout with a lock. The locking in the main thread is always performed regradless of whether traffic monitor is running or not for simplicity. It won't have any side-effect.stdio_restore()is kept in the crash handler instead of making all print functions in the crash handler use env.stdout_saved to make it less error-prone.
Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
This patch fixes the error for me.
Quoted 38 lines
tools/testing/selftests/bpf/test_progs.c | 59 ++++++++++++++++-------- 1 file changed, 39 insertions(+), 20 deletions(-)tools/testing/selftests/bpf/test_progs.c +16
-2@@ -88,7 +88,11 @@ static void stdio_hijack(char **log_buf, size_t *log_cnt)#endif } -static void stdio_restore_cleanup (void ) +static pthread_mutex_t stdout_lock =PTHREAD_MUTEX_INITIALIZER ; + +static bool in_crash_handler (void ); + +static void stdio_restore (void ) {#ifdef __GLIBC__if (verbose () && env.worker_id ==-1 ) {@@ -98,34 +102,34 @@ static void stdio_restore_cleanup(void)fflush (stdout); -if (env.subtest_state) { +pthread_mutex_lock (&stdout_lock); + +if (!env.subtest_state ||in_crash_handler ()) { +if (stdout == env.stdout_saved) + goto out; + +fclose (env.test_state->stdout_saved); + env.test_state->stdout_saved =NULL ; + stdout = env.stdout_saved; + stderr = env.stderr_saved; + }else {fclose (env.subtest_state->stdout_saved); env.subtest_state->stdout_saved =NULL ;
tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 124
Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +2
-3stdout = env.test_state->stdout_saved; stderr = env.test_state->stdout_saved; - }else { -fclose (env.test_state->stdout_saved); - env.test_state->stdout_saved =NULL ; } +out: +pthread_mutex_unlock (&stdout_lock);#endif }
stdio_restore_cleanup() did not reset stderr/stdout when env.subtest_state was NULL, but this difference does not seem to matter, stdio_restore_cleanup() was called from:
- test__start_subtest(), where stdio_hijack_init() would override stderr/stdout anyways.
- run_one_test(), where it is followed by call to stdio_restore().
I think this change is Ok.
[...]
Quoted 6 lines
tools/testing/selftests/bpf/test_progs.c +2
-0@@ -1276,6 +1281,18 @@ void crash_handler(int signum)backtrace_symbols_fd (bt, sz,STDERR_FILENO ); } +static bool in_crash_handler (void ) +{
tools/testing/selftests/bpf/test_progs.c void crash_handler(int signum) · line 1295
Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +10
-0+struct sigaction sigact; + +/* sa_handler will be cleared if invoked since crash_handler is +* registered with SA_RESETHAND +*/ +sigaction (SIGSEGV ,NULL , &sigact); + +return sigact.sa_handler != crash_handler; +} +
The patch would be simpler w/o this function. I double checked functions called from crash_handler() and two 'fprintf(stderr, ...)' there are the only places where stderr/stdout is used instead of *_saved versions. It is already a prevalent pattern to do 'fprintf(env.stderr_saved, ...)' in this file.
Or pass a flag as in v3?
Quoted 4 lines
tools/testing/selftests/bpf/test_progs.c +0
-0void hexdump (const char *prefix,const void *buf,size_t len) {for (int i =0 ; i < len; i++) {@@ -1957,6 +1974,8 @@ int main(int argc, char **argv)
[...]
Re: [PATCH bpf-next v4 3/3] selftests/bpf: Fix dangling stdout seen by traffic monitor thread
Quoted 105 lines · Eduard Zingerman
Quoted 21 lines · Amery Hung
Traffic monitor thread may see dangling stdout as the main thread closes and reassigns stdout without protection. This happens when the main thread finishes one subtest and moves to another one in the same
netns_new()scope.
The issue can be reproduced by runningtest_progsrepeatedly with traffic monitor enabled:for ((i=1;i<=100;i++)); do
./test_progs -a flow_dissector_skb* -m '*'
doneFix it by first consolidating stdout assignment into
stdio_restore(). stdout will be restored to env.stdout_saved when a test ends or running in the crash handler and to test_state.stdout_saved otherwise. Then, protect use/close/reassignment of stdout with a lock. The locking in the main thread is always performed regradless of whether traffic monitor is running or not for simplicity. It won't have any side-effect.stdio_restore()is kept in the crash handler instead of making all print functions in the crash handler use env.stdout_saved to make it less error-prone.Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
This patch fixes the error for me.
Tested-by Eduard Zingerman <[email protected]>Quoted 38 lines
tools/testing/selftests/bpf/test_progs.c | 59 ++++++++++++++++-------- 1 file changed, 39 insertions(+), 20 deletions(-)tools/testing/selftests/bpf/test_progs.c +16
-2@@ -88,7 +88,11 @@ static void stdio_hijack(char **log_buf, size_t *log_cnt)#endif } -static void stdio_restore_cleanup (void ) +static pthread_mutex_t stdout_lock =PTHREAD_MUTEX_INITIALIZER ; + +static bool in_crash_handler (void ); + +static void stdio_restore (void ) {#ifdef __GLIBC__if (verbose () && env.worker_id ==-1 ) {@@ -98,34 +102,34 @@ static void stdio_restore_cleanup(void)fflush (stdout); -if (env.subtest_state) { +pthread_mutex_lock (&stdout_lock); + +if (!env.subtest_state ||in_crash_handler ()) { +if (stdout == env.stdout_saved) + goto out; + +fclose (env.test_state->stdout_saved); + env.test_state->stdout_saved =NULL ; + stdout = env.stdout_saved; + stderr = env.stderr_saved; + }else {fclose (env.subtest_state->stdout_saved); env.subtest_state->stdout_saved =NULL ;tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 124
Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +2
-3stdout = env.test_state->stdout_saved; stderr = env.test_state->stdout_saved; - }else { -fclose (env.test_state->stdout_saved); - env.test_state->stdout_saved =NULL ; } +out: +pthread_mutex_unlock (&stdout_lock);#endif }
stdio_restore_cleanup()did not reset stderr/stdout when env.subtest_state was NULL, but this difference does not seem to matter,stdio_restore_cleanup()was called from:
-test__start_subtest(), wherestdio_hijack_init()would override stderr/stdout anyways.
-run_one_test(), where it is followed by call tostdio_restore().I think this change is Ok.
[...]
Quoted 15 lines
tools/testing/selftests/bpf/test_progs.c +11
-0@@ -1276,6 +1281,18 @@ void crash_handler(int signum)backtrace_symbols_fd (bt, sz,STDERR_FILENO ); } +static bool in_crash_handler (void ) +{ +struct sigaction sigact; + +/* sa_handler will be cleared if invoked since crash_handler is +* registered with SA_RESETHAND +*/ +sigaction (SIGSEGV ,NULL , &sigact); + +return sigact.sa_handler != crash_handler; +}
Quoted 8 lines
tools/testing/selftests/bpf/test_progs.c void crash_handler(int signum) · line 1295
Quoted 1 line
tools/testing/selftests/bpf/test_progs.c +1
-0+The patch would be simpler w/o this function. I double checked functions called from
crash_handler()and two 'fprintf(stderr, ...)' there are the only places where stderr/stdout is used instead of*_savedversions. It is already a prevalent pattern to do 'fprintf(env.stderr_saved, ...)' in this file.
Or pass a flag as in v3?
While that is a common pattern, I think restoring stdio there can make sure people don't accidentally use stdout in the crash handler and find that it does not work occasionally.
I will remove the in_crash_handler special case handling per Martin's suggestion.
Quoted 8 lines
Quoted 5 lines
tools/testing/selftests/bpf/test_progs.c +0
-0void hexdump (const char *prefix,const void *buf,size_t len) {for (int i =0 ; i < len; i++) {@@ -1957,6 +1974,8 @@ int main(int argc, char **argv)[...]
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v4 3/3] selftests/bpf: Fix dangling stdout seen by traffic monitor thread
In reply to Amery HungQuoted 18 lines · Amery Hung
tools/testing/selftests/bpf/test_progs.c +5
-1@@ -88,7 +88,11 @@ static void stdio_hijack(char **log_buf, size_t *log_cnt)#endif } -static void stdio_restore_cleanup (void ) +static pthread_mutex_t stdout_lock =PTHREAD_MUTEX_INITIALIZER ; + +static bool in_crash_handler (void ); + +static void stdio_restore (void ) {#ifdef __GLIBC__if (verbose () && env.worker_id ==-1 ) {
tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 107
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3
-1@@ -98,34 +102,34 @@ static void stdio_restore_cleanup(void)fflush (stdout); -if (env.subtest_state) { +pthread_mutex_lock (&stdout_lock); + +if (!env.subtest_state ||in_crash_handler ()) {
Can the stdio restore be done in the crash_handler() itself instead of having a special case here and adding another in_crash_handler()?
Theoretically, the crash_handler() only needs to fflush(stdout /* whatever the current stdout is */) and...
tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 114
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +7
-0+if (stdout == env.stdout_saved) + goto out; + +fclose (env.test_state->stdout_saved); + env.test_state->stdout_saved =NULL ; + stdout = env.stdout_saved; + stderr = env.stderr_saved;
... restore std{out,err} = env.std{out,err}_saved.
At the crash point, it does not make a big difference to fclose(evn.test_state->stdout_saved) or not?
If the crash_handler() does not close the stdout that the traffic monitor might potentially be using, then crash_handler() does not need to take mutex, right?
Quoted 14 lines
tools/testing/selftests/bpf/test_progs.c +3
-3+ } else {fclose (env.subtest_state->stdout_saved); env.subtest_state->stdout_saved =NULL ; stdout = env.test_state->stdout_saved; stderr = env.test_state->stdout_saved; - }else { -fclose (env.test_state->stdout_saved); - env.test_state->stdout_saved =NULL ; } +out: +pthread_mutex_unlock (&stdout_lock);#endif }
[ ... ]
Quoted 12 lines
tools/testing/selftests/bpf/test_progs.c +12
-0+static bool in_crash_handler (void ) +{ +struct sigaction sigact; + +/* sa_handler will be cleared if invoked since crash_handler is +* registered with SA_RESETHAND +*/ +sigaction (SIGSEGV ,NULL , &sigact); + +return sigact.sa_handler != crash_handler; +} +
Re: [PATCH bpf-next v4 3/3] selftests/bpf: Fix dangling stdout seen by traffic monitor thread
Quoted 42 lines · Martin KaFai Lau
Quoted 18 lines · Amery Hung
tools/testing/selftests/bpf/test_progs.c +5
-1@@ -88,7 +88,11 @@ static void stdio_hijack(char **log_buf, size_t *log_cnt)#endif } -static void stdio_restore_cleanup (void ) +static pthread_mutex_t stdout_lock =PTHREAD_MUTEX_INITIALIZER ; + +static bool in_crash_handler (void ); + +static void stdio_restore (void ) {#ifdef __GLIBC__if (verbose () && env.worker_id ==-1 ) {tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 107
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3
-1@@ -98,34 +102,34 @@ static void stdio_restore_cleanup(void)fflush (stdout); -if (env.subtest_state) { +pthread_mutex_lock (&stdout_lock); + +if (!env.subtest_state ||in_crash_handler ()) {Can the stdio restore be done in the
crash_handler()itself instead of having a special case here and adding anotherin_crash_handler()?Theoretically, the
crash_handler()only needs tofflush(stdout /* whatever the current stdout is */)and...tools/testing/selftests/bpf/test_progs.c static void stdio_restore_cleanup(void) · line 114
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +7
-0+if (stdout == env.stdout_saved) + goto out; + +fclose (env.test_state->stdout_saved); + env.test_state->stdout_saved =NULL ; + stdout = env.stdout_saved; + stderr = env.stderr_saved;
Quoted 8 lines
... restore std{out,err} = env.std{out,err}_saved.
At the crash point, it does not make a big difference to
fclose(evn.test_state->stdout_saved)or not?If the
crash_handler()does not close the stdout that the traffic monitor might potentially be using, thencrash_handler()does not need to take mutex, right?
You are right. I think it is simpler to not let stdio_restore() handle the crash case, and just do the following in the crash handler.
fflush(stdout);
stdout = env.stdout_saved;
stderr = env.stderr_saved;
Quoted 29 lines
Quoted 14 lines
tools/testing/selftests/bpf/test_progs.c +3
-3+ } else {fclose (env.subtest_state->stdout_saved); env.subtest_state->stdout_saved =NULL ; stdout = env.test_state->stdout_saved; stderr = env.test_state->stdout_saved; - }else { -fclose (env.test_state->stdout_saved); - env.test_state->stdout_saved =NULL ; } +out: +pthread_mutex_unlock (&stdout_lock);#endif }[ ... ]
Quoted 12 lines
tools/testing/selftests/bpf/test_progs.c +12
-0+static bool in_crash_handler (void ) +{ +struct sigaction sigact; + +/* sa_handler will be cleared if invoked since crash_handler is +* registered with SA_RESETHAND +*/ +sigaction (SIGSEGV ,NULL , &sigact); + +return sigact.sa_handler != crash_handler; +} +
Quoted 8 lines · Amery Hung
There is no need to call a bunch of
stdio_restore()intest_progsif the scope of stdio redirection is reduced to what it needs to be: only hijacking tests/subtests' stdio.Also remove an unnecessary check of env.stdout_saved in the crash handler.
Signed-off-by Amery Hung <[email protected]>---
If anyone else would look at this commit, here is an alternative description:
- functions reset_affinity() and restore_netns() are only called from run_one_test();
- beside other places stdio_restore() is called from reset_affinity(), restore_netns() and run_one_test() itself;
- this commit moves stdio_restore() call in run_one_test() so that it executes before reset_affinity() and restore_netns().
[...]
tools/testing/selftests/bpf/test_progs.c int main(int argc, char **argv) · line 1943
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3
-0@@ -1943,6 +1938,9 @@ int main(int argc, char **argv)sigaction (SIGSEGV , &sigact,NULL ); + env.stdout_saved = stdout; + env.stderr_saved = stderr; +
Nit: why moving these?
Quoted 13 lines
tools/testing/selftests/bpf/test_progs.c +0
-3env.secs_till_notify =10 ; env.secs_till_kill =120 ; err =argp_parse (&argp, argc, argv,0 ,NULL , &env);@@ -1969,9 +1967,6 @@ int main(int argc, char **argv)return -1 ; } - env.stdout_saved = stdout; - env.stderr_saved = stderr; - env.has_testmod = true;if (!env.list_test_names) {/* ensure previous instance of the module is unloaded */
Quoted 20 lines · Eduard Zingerman
Quoted 8 lines · Amery Hung
There is no need to call a bunch of
stdio_restore()intest_progsif the scope of stdio redirection is reduced to what it needs to be: only hijacking tests/subtests' stdio.Also remove an unnecessary check of env.stdout_saved in the crash handler.
Signed-off-by Amery Hung <[email protected]>---
If anyone else would look at this commit, here is an alternative description:
- functionsreset_affinity()andrestore_netns()are only called fromrun_one_test();
- beside other placesstdio_restore()is called fromreset_affinity(),restore_netns()andrun_one_test()itself;
- this commit movesstdio_restore()call inrun_one_test()so that it executes beforereset_affinity()andrestore_netns().
Quoted 2 lines
Acked-by Eduard Zingerman <[email protected]>
I can improve the commit message in the next respin.
Thanks,
Amery
Quoted 11 lines
[...]
tools/testing/selftests/bpf/test_progs.c int main(int argc, char **argv) · line 1943
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3
-0@@ -1943,6 +1938,9 @@ int main(int argc, char **argv)sigaction (SIGSEGV , &sigact,NULL ); + env.stdout_saved = stdout; + env.stderr_saved = stderr; +Nit: why moving these?
If we assign env.stdout_saved at the very beginning, crash_handler() can just call stdio_restore() without checking if env.stdout_saved is set or not.
Quoted 16 lines
Quoted 14 lines
tools/testing/selftests/bpf/test_progs.c +0
-3env.secs_till_notify =10 ; env.secs_till_kill =120 ; err =argp_parse (&argp, argc, argv,0 ,NULL , &env);@@ -1969,9 +1967,6 @@ int main(int argc, char **argv)return -1 ; } - env.stdout_saved = stdout; - env.stderr_saved = stderr; - env.has_testmod = true;if (!env.list_test_names) {/* ensure previous instance of the module is unloaded */
Eduard Zingerman
maintainerQuoted 21 lines · Amery Hung
Quoted 20 lines · Eduard Zingerman
Quoted 8 lines · Amery Hung
There is no need to call a bunch of
stdio_restore()intest_progsif the scope of stdio redirection is reduced to what it needs to be: only hijacking tests/subtests' stdio.Also remove an unnecessary check of env.stdout_saved in the crash handler.
Signed-off-by Amery Hung <[email protected]>---
If anyone else would look at this commit, here is an alternative description:
- functionsreset_affinity()andrestore_netns()are only called fromrun_one_test();
- beside other placesstdio_restore()is called fromreset_affinity(),restore_netns()andrun_one_test()itself;
- this commit movesstdio_restore()call inrun_one_test()so that it executes beforereset_affinity()andrestore_netns().
Quoted 4 lines
Quoted 2 lines
Acked-by Eduard Zingerman <[email protected]>I can improve the commit message in the next respin.
If there would be a respin, otherwise no need to worry.
[...]
Quoted 10 lines
Quoted 9 lines
tools/testing/selftests/bpf/test_progs.c int main(int argc, char **argv) · line 1943
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3
-0@@ -1943,6 +1938,9 @@ int main(int argc, char **argv)sigaction (SIGSEGV , &sigact,NULL ); + env.stdout_saved = stdout; + env.stderr_saved = stderr; +Nit: why moving these?
Quoted 3 lines
If we assign env.stdout_saved at the very beginning,
crash_handler()can just callstdio_restore()without checking if env.stdout_saved is set or not.
Understood, thank you.
[...]