Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Fix stdout race condition in traffic monitor

Fix a race condition between the main test_progs thread and the traffic
monitoring thread. The traffic monitor thread tries to print a line
using multiple printf and use flockfile() to prevent the line from being
torn apart. Meanwhile, the main thread doing io redirection can reassign
or close stdout when going through tests. A deadlock as shown below can
happen.

main traffic_monitor_thread
==== ======================
show_transport()
-> flockfile(stdout)

stdio_hijack_init()
-> stdout = open_memstream(log_buf, log_cnt);
...
env.subtest_state->stdout_saved = stdout;

...
funlockfile(stdout)
stdio_restore_cleanup()
-> fclose(env.subtest_state->stdout_saved);

After the traffic monitor thread lock stdout, A new memstream can be
assigned to stdout by the main thread. Therefore, the traffic monitor
thread later will not be able to unlock the original stdout. As the
main thread tries to access the old stdout, it will hang indefinitely
as it is still locked by the traffic monitor thread.

The deadlock 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 this by only calling printf once and remove flockfile()/funlockfile().

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250213233217.553258-1-ameryhung@gmail.com

authored by

Amery Hung and committed by
Martin KaFai Lau
b99f27e9 c83e2d97

+13 -20
+13 -20
tools/testing/selftests/bpf/network_helpers.c
··· 788 788 return "Unknown"; 789 789 } 790 790 791 + #define MAX_FLAGS_STRLEN 21 791 792 /* Show the information of the transport layer in the packet */ 792 793 static void show_transport(const u_char *packet, u16 len, u32 ifindex, 793 794 const char *src_addr, const char *dst_addr, 794 795 u16 proto, bool ipv6, u8 pkt_type) 795 796 { 796 - char *ifname, _ifname[IF_NAMESIZE]; 797 + char *ifname, _ifname[IF_NAMESIZE], flags[MAX_FLAGS_STRLEN] = ""; 797 798 const char *transport_str; 798 799 u16 src_port, dst_port; 799 800 struct udphdr *udp; ··· 835 834 836 835 /* TCP or UDP*/ 837 836 838 - flockfile(stdout); 837 + if (proto == IPPROTO_TCP) 838 + snprintf(flags, MAX_FLAGS_STRLEN, "%s%s%s%s", 839 + tcp->fin ? ", FIN" : "", 840 + tcp->syn ? ", SYN" : "", 841 + tcp->rst ? ", RST" : "", 842 + tcp->ack ? ", ACK" : ""); 843 + 839 844 if (ipv6) 840 - printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d", 845 + printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n", 841 846 ifname, pkt_type_str(pkt_type), src_addr, src_port, 842 - dst_addr, dst_port, transport_str, len); 847 + dst_addr, dst_port, transport_str, len, flags); 843 848 else 844 - printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d", 849 + printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n", 845 850 ifname, pkt_type_str(pkt_type), src_addr, src_port, 846 - dst_addr, dst_port, transport_str, len); 847 - 848 - if (proto == IPPROTO_TCP) { 849 - if (tcp->fin) 850 - printf(", FIN"); 851 - if (tcp->syn) 852 - printf(", SYN"); 853 - if (tcp->rst) 854 - printf(", RST"); 855 - if (tcp->ack) 856 - printf(", ACK"); 857 - } 858 - 859 - printf("\n"); 860 - funlockfile(stdout); 851 + dst_addr, dst_port, transport_str, len, flags); 861 852 } 862 853 863 854 static void show_ipv6_packet(const u_char *packet, u32 ifindex, u8 pkt_type)