From: Martin KaFai Lau <martin.lau@linux.dev>
To: Amery Hung <ameryhung@gmail.com>
Cc: bpf@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org,
alexei.starovoitov@gmail.com, martin.lau@kernel.org,
kernel-team@meta.com
Subject: Re: [PATCH bpf-next v1] selftests/bpf: Fix stdout race condition in traffic monitor
Date: Thu, 13 Feb 2025 15:55:17 -0800 [thread overview]
Message-ID: <cfe7a83f-a2de-4157-8a43-abbe95968b05@linux.dev> (raw)
In-Reply-To: <20250213233217.553258-1-ameryhung@gmail.com>
On 2/13/25 3:32 PM, Amery Hung wrote:
> 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);
Great debugging.
Does it mean that the main thread will start the next test before the
traffic_monitor_thread has finished? Meaning the traffic_monitor_stop() does not
wait for the traffic_monitor_thread somehow?
>
> 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().
Yep. I agree this patch should be the better way to print the one-liner regardless.
next prev parent reply other threads:[~2025-02-13 23:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-13 23:32 [PATCH bpf-next v1] selftests/bpf: Fix stdout race condition in traffic monitor Amery Hung
2025-02-13 23:55 ` Martin KaFai Lau [this message]
2025-02-14 0:19 ` Amery Hung
2025-02-14 1:08 ` Martin KaFai Lau
2025-02-14 1:10 ` patchwork-bot+netdevbpf
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=cfe7a83f-a2de-4157-8a43-abbe95968b05@linux.dev \
--to=martin.lau@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.