linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/harness: Flush stdout before forking
@ 2020-09-17  4:15 Michael Ellerman
  2020-09-17  4:53 ` Max Filippov
  2020-09-17 22:07 ` Kees Cook
  0 siblings, 2 replies; 8+ messages in thread
From: Michael Ellerman @ 2020-09-17  4:15 UTC (permalink / raw)
  To: keescook; +Cc: linux-kselftest, linux-kernel, jcmvbkbc

The test harness forks() a child to run each test. Both the parent and
the child print to stdout using libc functions. That can lead to
duplicated (or more) output if the libc buffers are not flushed before
forking.

It's generally not seen when running programs directly, because stdout
will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

  $ ./seccomp_bpf | tee test.log
  $ grep -c "TAP version 13" test.log
  2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

  $ stdbuf -o 1MB ./seccomp_bpf > test.log
  $ grep -c "TAP version 13" test.log
  13

The fix is simple, we just flush stdout & stderr before fork. Usually
stderr is unbuffered, but that can be changed, so flush it as well
just to be safe.

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
 tools/testing/selftests/kselftest_harness.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 4f78e4805633..f19804df244c 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -971,6 +971,11 @@ void __run_test(struct __fixture_metadata *f,
 
 	ksft_print_msg(" RUN           %s%s%s.%s ...\n",
 	       f->name, variant->name[0] ? "." : "", variant->name, t->name);
+
+	/* Make sure output buffers are flushed before fork */
+	fflush(stdout);
+	fflush(stderr);
+
 	t->pid = fork();
 	if (t->pid < 0) {
 		ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17  4:15 [PATCH] selftests/harness: Flush stdout before forking Michael Ellerman
@ 2020-09-17  4:53 ` Max Filippov
  2020-09-17 12:58   ` Shuah Khan
  2020-09-17 22:07 ` Kees Cook
  1 sibling, 1 reply; 8+ messages in thread
From: Max Filippov @ 2020-09-17  4:53 UTC (permalink / raw)
  To: Michael Ellerman; +Cc: Kees Cook, linux-kselftest, LKML

On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman <mpe@ellerman.id.au> wrote:
>
> The test harness forks() a child to run each test. Both the parent and
> the child print to stdout using libc functions. That can lead to
> duplicated (or more) output if the libc buffers are not flushed before
> forking.
>
> It's generally not seen when running programs directly, because stdout
> will usually be line buffered when it's pointing to a terminal.
>
> This was noticed when running the seccomp_bpf test, eg:
>
>   $ ./seccomp_bpf | tee test.log
>   $ grep -c "TAP version 13" test.log
>   2
>
> But we only expect the TAP header to appear once.
>
> It can be exacerbated using stdbuf to increase the buffer size:
>
>   $ stdbuf -o 1MB ./seccomp_bpf > test.log
>   $ grep -c "TAP version 13" test.log
>   13
>
> The fix is simple, we just flush stdout & stderr before fork. Usually
> stderr is unbuffered, but that can be changed, so flush it as well
> just to be safe.
>
> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
> ---
>  tools/testing/selftests/kselftest_harness.h | 5 +++++
>  1 file changed, 5 insertions(+)

Tested-by: Max Filippov <jcmvbkbc@gmail.com>

-- 
Thanks.
-- Max

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17  4:53 ` Max Filippov
@ 2020-09-17 12:58   ` Shuah Khan
  2020-09-17 16:51     ` Shuah Khan
  2020-09-18  3:37     ` Michael Ellerman
  0 siblings, 2 replies; 8+ messages in thread
From: Shuah Khan @ 2020-09-17 12:58 UTC (permalink / raw)
  To: Max Filippov, Michael Ellerman
  Cc: Kees Cook, linux-kselftest, LKML, Shuah Khan

On 9/16/20 10:53 PM, Max Filippov wrote:
> On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman <mpe@ellerman.id.au> wrote:
>>
>> The test harness forks() a child to run each test. Both the parent and
>> the child print to stdout using libc functions. That can lead to
>> duplicated (or more) output if the libc buffers are not flushed before
>> forking.
>>
>> It's generally not seen when running programs directly, because stdout
>> will usually be line buffered when it's pointing to a terminal.
>>
>> This was noticed when running the seccomp_bpf test, eg:
>>
>>    $ ./seccomp_bpf | tee test.log
>>    $ grep -c "TAP version 13" test.log
>>    2
>>
>> But we only expect the TAP header to appear once.
>>
>> It can be exacerbated using stdbuf to increase the buffer size:
>>
>>    $ stdbuf -o 1MB ./seccomp_bpf > test.log
>>    $ grep -c "TAP version 13" test.log
>>    13
>>
>> The fix is simple, we just flush stdout & stderr before fork. Usually
>> stderr is unbuffered, but that can be changed, so flush it as well
>> just to be safe.
>>
>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>> ---
>>   tools/testing/selftests/kselftest_harness.h | 5 +++++
>>   1 file changed, 5 insertions(+)
> 
> Tested-by: Max Filippov <jcmvbkbc@gmail.com>
> 

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17 12:58   ` Shuah Khan
@ 2020-09-17 16:51     ` Shuah Khan
  2020-09-17 22:06       ` Kees Cook
  2020-09-18  3:37     ` Michael Ellerman
  1 sibling, 1 reply; 8+ messages in thread
From: Shuah Khan @ 2020-09-17 16:51 UTC (permalink / raw)
  To: Kees Cook
  Cc: Max Filippov, Michael Ellerman, linux-kselftest, LKML, Shuah Khan

On 9/17/20 6:58 AM, Shuah Khan wrote:
> On 9/16/20 10:53 PM, Max Filippov wrote:
>> On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman <mpe@ellerman.id.au> 
>> wrote:
>>>
>>> The test harness forks() a child to run each test. Both the parent and
>>> the child print to stdout using libc functions. That can lead to
>>> duplicated (or more) output if the libc buffers are not flushed before
>>> forking.
>>>
>>> It's generally not seen when running programs directly, because stdout
>>> will usually be line buffered when it's pointing to a terminal.
>>>
>>> This was noticed when running the seccomp_bpf test, eg:
>>>
>>>    $ ./seccomp_bpf | tee test.log
>>>    $ grep -c "TAP version 13" test.log
>>>    2
>>>
>>> But we only expect the TAP header to appear once.
>>>
>>> It can be exacerbated using stdbuf to increase the buffer size:
>>>
>>>    $ stdbuf -o 1MB ./seccomp_bpf > test.log
>>>    $ grep -c "TAP version 13" test.log
>>>    13
>>>
>>> The fix is simple, we just flush stdout & stderr before fork. Usually
>>> stderr is unbuffered, but that can be changed, so flush it as well
>>> just to be safe.
>>>
>>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>>> ---
>>>   tools/testing/selftests/kselftest_harness.h | 5 +++++
>>>   1 file changed, 5 insertions(+)
>>
>> Tested-by: Max Filippov <jcmvbkbc@gmail.com>
>>
> 
> Thank you both. Applying to linux-kselftest fixes for 5.9-rc7
> 

Kees,

I haven't pulled this in yet. If you want to take this through seccomp
tree for dependencies:

Acked-by: Shuah Khan <skhan@linuxfoundation.org>

thanks,
-- Shuah


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17 16:51     ` Shuah Khan
@ 2020-09-17 22:06       ` Kees Cook
  0 siblings, 0 replies; 8+ messages in thread
From: Kees Cook @ 2020-09-17 22:06 UTC (permalink / raw)
  To: Shuah Khan; +Cc: Max Filippov, Michael Ellerman, linux-kselftest, LKML

On Thu, Sep 17, 2020 at 10:51:32AM -0600, Shuah Khan wrote:
> On 9/17/20 6:58 AM, Shuah Khan wrote:
> > On 9/16/20 10:53 PM, Max Filippov wrote:
> > > On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman
> > > <mpe@ellerman.id.au> wrote:
> > > > 
> > > > The test harness forks() a child to run each test. Both the parent and
> > > > the child print to stdout using libc functions. That can lead to
> > > > duplicated (or more) output if the libc buffers are not flushed before
> > > > forking.
> > > > 
> > > > It's generally not seen when running programs directly, because stdout
> > > > will usually be line buffered when it's pointing to a terminal.
> > > > 
> > > > This was noticed when running the seccomp_bpf test, eg:
> > > > 
> > > >    $ ./seccomp_bpf | tee test.log
> > > >    $ grep -c "TAP version 13" test.log
> > > >    2
> > > > 
> > > > But we only expect the TAP header to appear once.
> > > > 
> > > > It can be exacerbated using stdbuf to increase the buffer size:
> > > > 
> > > >    $ stdbuf -o 1MB ./seccomp_bpf > test.log
> > > >    $ grep -c "TAP version 13" test.log
> > > >    13
> > > > 
> > > > The fix is simple, we just flush stdout & stderr before fork. Usually
> > > > stderr is unbuffered, but that can be changed, so flush it as well
> > > > just to be safe.
> > > > 
> > > > Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
> > > > ---
> > > >   tools/testing/selftests/kselftest_harness.h | 5 +++++
> > > >   1 file changed, 5 insertions(+)
> > > 
> > > Tested-by: Max Filippov <jcmvbkbc@gmail.com>
> > > 
> > 
> > Thank you both. Applying to linux-kselftest fixes for 5.9-rc7
> > 
> 
> Kees,
> 
> I haven't pulled this in yet. If you want to take this through seccomp
> tree for dependencies:
> 
> Acked-by: Shuah Khan <skhan@linuxfoundation.org>

You can have it -- it's a global fix and more than the seccomp selftest
is likely affected. :)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17  4:15 [PATCH] selftests/harness: Flush stdout before forking Michael Ellerman
  2020-09-17  4:53 ` Max Filippov
@ 2020-09-17 22:07 ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2020-09-17 22:07 UTC (permalink / raw)
  To: Michael Ellerman; +Cc: linux-kselftest, linux-kernel, jcmvbkbc

On Thu, Sep 17, 2020 at 02:15:19PM +1000, Michael Ellerman wrote:
> The test harness forks() a child to run each test. Both the parent and
> the child print to stdout using libc functions. That can lead to
> duplicated (or more) output if the libc buffers are not flushed before
> forking.
> 
> It's generally not seen when running programs directly, because stdout
> will usually be line buffered when it's pointing to a terminal.
> 
> This was noticed when running the seccomp_bpf test, eg:
> 
>   $ ./seccomp_bpf | tee test.log
>   $ grep -c "TAP version 13" test.log
>   2

Oh thank you for tracking this down! I thought seccomp wasn't killing a
child or something, and hadn't found it yet. :)

Acked-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-17 12:58   ` Shuah Khan
  2020-09-17 16:51     ` Shuah Khan
@ 2020-09-18  3:37     ` Michael Ellerman
  2020-09-18 14:32       ` Shuah Khan
  1 sibling, 1 reply; 8+ messages in thread
From: Michael Ellerman @ 2020-09-18  3:37 UTC (permalink / raw)
  To: Shuah Khan, Max Filippov; +Cc: Kees Cook, linux-kselftest, LKML, Shuah Khan

Shuah Khan <skhan@linuxfoundation.org> writes:
> On 9/16/20 10:53 PM, Max Filippov wrote:
>> On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman <mpe@ellerman.id.au> wrote:
>>>
>>> The test harness forks() a child to run each test. Both the parent and
>>> the child print to stdout using libc functions. That can lead to
>>> duplicated (or more) output if the libc buffers are not flushed before
>>> forking.
>>>
>>> It's generally not seen when running programs directly, because stdout
>>> will usually be line buffered when it's pointing to a terminal.
>>>
>>> This was noticed when running the seccomp_bpf test, eg:
>>>
>>>    $ ./seccomp_bpf | tee test.log
>>>    $ grep -c "TAP version 13" test.log
>>>    2
>>>
>>> But we only expect the TAP header to appear once.
>>>
>>> It can be exacerbated using stdbuf to increase the buffer size:
>>>
>>>    $ stdbuf -o 1MB ./seccomp_bpf > test.log
>>>    $ grep -c "TAP version 13" test.log
>>>    13
>>>
>>> The fix is simple, we just flush stdout & stderr before fork. Usually
>>> stderr is unbuffered, but that can be changed, so flush it as well
>>> just to be safe.
>>>
>>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>>> ---
>>>   tools/testing/selftests/kselftest_harness.h | 5 +++++
>>>   1 file changed, 5 insertions(+)
>> 
>> Tested-by: Max Filippov <jcmvbkbc@gmail.com>
>
> Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

It can wait for v5.10 IMHO, but up to you.

cheers

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] selftests/harness: Flush stdout before forking
  2020-09-18  3:37     ` Michael Ellerman
@ 2020-09-18 14:32       ` Shuah Khan
  0 siblings, 0 replies; 8+ messages in thread
From: Shuah Khan @ 2020-09-18 14:32 UTC (permalink / raw)
  To: Michael Ellerman, Max Filippov
  Cc: Kees Cook, linux-kselftest, LKML, Shuah Khan

On 9/17/20 9:37 PM, Michael Ellerman wrote:
> Shuah Khan <skhan@linuxfoundation.org> writes:
>> On 9/16/20 10:53 PM, Max Filippov wrote:
>>> On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman <mpe@ellerman.id.au> wrote:
>>>>
>>>> The test harness forks() a child to run each test. Both the parent and
>>>> the child print to stdout using libc functions. That can lead to
>>>> duplicated (or more) output if the libc buffers are not flushed before
>>>> forking.
>>>>
>>>> It's generally not seen when running programs directly, because stdout
>>>> will usually be line buffered when it's pointing to a terminal.
>>>>
>>>> This was noticed when running the seccomp_bpf test, eg:
>>>>
>>>>     $ ./seccomp_bpf | tee test.log
>>>>     $ grep -c "TAP version 13" test.log
>>>>     2
>>>>
>>>> But we only expect the TAP header to appear once.
>>>>
>>>> It can be exacerbated using stdbuf to increase the buffer size:
>>>>
>>>>     $ stdbuf -o 1MB ./seccomp_bpf > test.log
>>>>     $ grep -c "TAP version 13" test.log
>>>>     13
>>>>
>>>> The fix is simple, we just flush stdout & stderr before fork. Usually
>>>> stderr is unbuffered, but that can be changed, so flush it as well
>>>> just to be safe.
>>>>
>>>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>>>> ---
>>>>    tools/testing/selftests/kselftest_harness.h | 5 +++++
>>>>    1 file changed, 5 insertions(+)
>>>
>>> Tested-by: Max Filippov <jcmvbkbc@gmail.com>
>>
>> Thank you both. Applying to linux-kselftest fixes for 5.9-rc7
> 
> It can wait for v5.10 IMHO, but up to you.
> 

Good to know. I will send this for 5.10-rc1 then.

thanks,
-- Shuah


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-09-18 14:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17  4:15 [PATCH] selftests/harness: Flush stdout before forking Michael Ellerman
2020-09-17  4:53 ` Max Filippov
2020-09-17 12:58   ` Shuah Khan
2020-09-17 16:51     ` Shuah Khan
2020-09-17 22:06       ` Kees Cook
2020-09-18  3:37     ` Michael Ellerman
2020-09-18 14:32       ` Shuah Khan
2020-09-17 22:07 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).