Skip to contentSkip to conversation

linux-user: Fix pipe() vs. pipe2() usage for ALPHA, MIPS, SH4 and SPARC

PATCH In review Helge Deller 2 participants 8 messages Jul 17, 2022 – Jul 18, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix pipe() vs. pipe2() usage for ALPHA, MIPS, SH4 and SPARC

In 2010, the commit b41a66edd0c added a thrird parameter "is_pipe2" to the internal do_pipe() function, but missed to actually use this parameter to decide if the pipe() or pipe2() syscall should be used. Instead it just continued to check the flags parameter and used pipe2() unconditionally if flags is non-zero.

This change should make a difference for the ALPHA, MIPS, SH4 and SPARC targets if the emulated code calls pipe2() with a flags value of 0.

Fixes fb41a66edd0c ("alpha-linux-user: Fix pipe return mechanism.")
Cc Richard Henderson <[email protected]>
Cc Aurelien Jarno <[email protected]>
linux-user/syscall.c +1 -1
index 991b85e6b4..1e6e814871 100644
@@ -1600,7 +1600,7 @@ static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
 {
     int host_pipe[2];
     abi_long ret;
-    ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
+    ret = is_pipe2 ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
 
     if (is_error(ret))
         return get_errno(ret);
#1 lore raw
Quoted 10 lines · Helge Deller

In 2010, the commit b41a66edd0c added a thrird parameter "is_pipe2" to the internal do_pipe() function, but missed to actually use this parameter to decide if the pipe() or pipe2() syscall should be used. Instead it just continued to check the flags parameter and used pipe2() unconditionally if flags is non-zero.

This change should make a difference for the ALPHA, MIPS, SH4 and SPARC targets if the emulated code calls pipe2() with a flags value of 0.

Fixes fb41a66edd0c ("alpha-linux-user: Fix pipe return mechanism.")
Quoted 16 lines
Cc Richard Henderson <[email protected]>
Cc Aurelien Jarno <[email protected]>
linux-user/syscall.c +1 -1
index 991b85e6b4..1e6e814871 100644
@@ -1600,7 +1600,7 @@ static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
 {
     int host_pipe[2];
     abi_long ret;
-    ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
+    ret = is_pipe2 ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
 
     if (is_error(ret))
         return get_errno(ret);
#2 lore raw
In reply to Helge Deller
Quoted 2 lines · Helge Deller

In 2010, the commit b41a66edd0c added a thrird parameter "is_pipe2" to the

typo in commit hash (lost the first letter). Should be fb41a66edd0c7bd6 ("alpha-linux-user: Fix pipe return mechanism." I think ?

Quoted 17 lines

internal do_pipe() function, but missed to actually use this parameter to decide if the pipe() or pipe2() syscall should be used. Instead it just continued to check the flags parameter and used pipe2() unconditionally if flags is non-zero.

This change should make a difference for the ALPHA, MIPS, SH4 and SPARC targets if the emulated code calls pipe2() with a flags value of 0.

Fixes fb41a66edd0c ("alpha-linux-user: Fix pipe return mechanism.")
Cc Richard Henderson <[email protected]>
Cc Aurelien Jarno <[email protected]>
linux-user/syscall.c +0 -0
index 991b85e6b4..1e6e814871 100644

linux-user/syscall.c static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes, · line 1603

Quoted 5 lines
linux-user/syscall.c +1 -1
@@ -1600,7 +1600,7 @@ static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
 {
     int host_pipe[2];
     abi_long ret;
-    ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
+    ret = is_pipe2 ? do_pipe2(host_pipe, flags) : pipe(host_pipe);

Why do we need to do this? If the flags argument is 0, then pipe2() is the same as pipe(), so we can safely emulate it with the host pipe() call. It is, or at least was, worth doing that, so that guests that use pipe2(fds, 0) can still run on hosts that don't implement the pipe2 syscall.

There's probably a reasonable argument to be made that we don't care any more about hosts so old they don't have pipe2 and that we could just dump do_pipe2() and the CONFIG_PIPE2 check, and have do_pipe() unconditionally call pipe2(). Would just need somebody to check what kernel/glibc versions pipe2() came in.

The architecture specific bit of target behaviour that we need the is_pipe2 flag for is purely for handling the weird return code that only the pipe syscall has, and for that we are correctly looking at the is_pipe2 argument. (The bug that fb41a66edd0c7bd6 fixes is that we used to incorrectly give the pipe syscall return argument behaviour for pipe2-with-flags-zero.)

thanks
-- PMM

#3 lore raw
Quoted 7 lines · Peter Maydell
Quoted 2 lines · Helge Deller

In 2010, the commit b41a66edd0c added a thrird parameter "is_pipe2" to the

typo in commit hash (lost the first letter). Should be fb41a66edd0c7bd6 ("alpha-linux-user: Fix pipe return mechanism." I think ?

Yes. I missed that "f" although I had it in the Fixes: tag.

Quoted 12 lines
Quoted 12 lines

internal do_pipe() function, but missed to actually use this parameter to decide if the pipe() or pipe2() syscall should be used. Instead it just continued to check the flags parameter and used pipe2() unconditionally if flags is non-zero.

This change should make a difference for the ALPHA, MIPS, SH4 and SPARC targets if the emulated code calls pipe2() with a flags value of 0.

Fixes fb41a66edd0c ("alpha-linux-user: Fix pipe return mechanism.")
Cc Richard Henderson <[email protected]>
Cc Aurelien Jarno <[email protected]>
Quoted 12 lines

linux-user/syscall.c static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes, · line 1603

Quoted 10 lines
linux-user/syscall.c +1 -1
index 991b85e6b4..1e6e814871 100644
@@ -1600,7 +1600,7 @@ static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
 {
     int host_pipe[2];
     abi_long ret;
-    ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
+    ret = is_pipe2 ? do_pipe2(host_pipe, flags) : pipe(host_pipe);

Why do we need to do this?

Yep, we don't *need* to...

Quoted 6 lines

If the flags argument is 0,
then pipe2() is the same as pipe(), so we can safely emulate it with the host pipe() call. It is, or at least was, worth doing that, so that guests that use pipe2(fds, 0) can still run on hosts that don't implement the pipe2 syscall.

True, but only for pipe2(fds,0), not e.g. for pipe2(fds,1). On the other side if a guest explicitly calls pipe2()
and if it isn't available, then IMHO -ENOSYS should be returned. Let's assume userspace checks in configure/make scripts if pipe2() is available and succeeds due to pipe2(fds,0), it will assume pipe2() is generally available which isn't necessarily true.

Quoted 5 lines

There's probably a reasonable argument to be made that we don't care any more about hosts so old they don't have pipe2 and that we could just dump do_pipe2() and the CONFIG_PIPE2 check, and have do_pipe() unconditionally call pipe2(). Would just need somebody to check what kernel/glibc versions pipe2() came in.

Yes.

Quoted 7 lines

The architecture specific bit of target behaviour that we need the is_pipe2 flag for is purely for handling the weird return code that only the pipe syscall has, and for that we are correctly looking at the is_pipe2 argument. (The bug that fb41a66edd0c7bd6 fixes is that we used to incorrectly give the pipe syscall return argument behaviour for pipe2-with-flags-zero.)

Yes, that part is ok.

In summary, IMHO the patch isn't necessary, but probably more correct.

Helge

#4 lore raw
Quoted 12 lines · Helge Deller
Quoted 1 line · Peter Maydell

Why do we need to do this?

Yep, we don't *need* to...

Quoted 6 lines

If the flags argument is 0,
then pipe2() is the same as pipe(), so we can safely emulate it with the host pipe() call. It is, or at least was, worth doing that, so that guests that use pipe2(fds, 0) can still run on hosts that don't implement the pipe2 syscall.

Quoted 6 lines

True, but only for pipe2(fds,0), not e.g. for pipe2(fds,1). On the other side if a guest explicitly calls pipe2()
and if it isn't available, then IMHO -ENOSYS should be returned. Let's assume userspace checks in configure/make scripts if pipe2() is available and succeeds due to pipe2(fds,0), it will assume pipe2() is generally available which isn't necessarily true.

Fair point. Did you run into this in practice, or is it just a theoretical concern ?

NB that any probing code that does that will also get the wrong answer on musl libc, though, because musl's pipe2() implementation always calls pipe() for a zero-flags call:
https://git.musl-libc.org/cgit/musl/tree/src/unistd/pipe2.c

Quoted 7 lines
Quoted 5 lines

There's probably a reasonable argument to be made that we don't care any more about hosts so old they don't have pipe2 and that we could just dump do_pipe2() and the CONFIG_PIPE2 check, and have do_pipe() unconditionally call pipe2(). Would just need somebody to check what kernel/glibc versions pipe2() came in.

Yes.

I just had a look, and the pipe2 syscall came in in Linux 2.6.27. musl has implemented pipe2() since at least 2013, and glibc must have had it for at least that long.

In fact current glibc always implements pipe() in terms of pipe2():
https://sourceware.org/git/?p=glibc.git;a=commit;h=efc6b2dbc47231dee7a7ac39beec808deb4e4d1f

So my preference would be that we should just say "we can assume that pipe2 is always available and implemented on linux hosts" and remove the code that handles the possibility that it isn't.

thanks
-- PMM

#5 lore raw
Quoted 40 lines · Peter Maydell
Quoted 12 lines · Helge Deller
Quoted 1 line · Peter Maydell

Why do we need to do this?

Yep, we don't *need* to...

Quoted 6 lines

If the flags argument is 0,
then pipe2() is the same as pipe(), so we can safely emulate it with the host pipe() call. It is, or at least was, worth doing that, so that guests that use pipe2(fds, 0) can still run on hosts that don't implement the pipe2 syscall.

Quoted 6 lines

True, but only for pipe2(fds,0), not e.g. for pipe2(fds,1). On the other side if a guest explicitly calls pipe2()
and if it isn't available, then IMHO -ENOSYS should be returned. Let's assume userspace checks in configure/make scripts if pipe2() is available and succeeds due to pipe2(fds,0), it will assume pipe2() is generally available which isn't necessarily true.

Fair point. Did you run into this in practice, or is it just a theoretical concern ?

NB that any probing code that does that will also get the wrong answer on musl libc, though, because musl's pipe2() implementation always calls pipe() for a zero-flags call:
https://git.musl-libc.org/cgit/musl/tree/src/unistd/pipe2.c

Quoted 7 lines
Quoted 5 lines

There's probably a reasonable argument to be made that we don't care any more about hosts so old they don't have pipe2 and that we could just dump do_pipe2() and the CONFIG_PIPE2 check, and have do_pipe() unconditionally call pipe2(). Would just need somebody to check what kernel/glibc versions pipe2() came in.

Yes.

I just had a look, and the pipe2 syscall came in in Linux 2.6.27. musl has implemented pipe2() since at least 2013, and glibc must have had it for at least that long.

Quoted 6 lines

In fact current glibc always implements pipe() in terms of pipe2():
https://sourceware.org/git/?p=glibc.git;a=commit;h=efc6b2dbc47231dee7a7ac39beec808deb4e4d1f

So my preference would be that we should just say "we can assume that pipe2 is always available and implemented on linux hosts" and remove the code that handles the possibility that it isn't.

Ok for me.
Do you want me to send a patch or will you do?

Helge

#6 lore raw
Quoted 7 lines · Helge Deller
Quoted 3 lines · Peter Maydell

So my preference would be that we should just say "we can assume that pipe2 is always available and implemented on linux hosts" and remove the code that handles the possibility that it isn't.

Ok for me.
Do you want me to send a patch or will you do?

If you'd like to write the patch that would be great. You can remove the meson.build line that sets CONFIG_PIPE2 as well, since we have no other places that check it.

-- PMM

#7 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Helge Deller
Quoted 3 lines · Peter Maydell

So my preference would be that we should just say "we can assume that pipe2 is always available and implemented on linux hosts" and remove the code that handles the possibility that it isn't.

Quoted 6 lines
Quoted 2 lines

Ok for me.
Do you want me to send a patch or will you do?

If you'd like to write the patch that would be great. You can remove the meson.build line that sets CONFIG_PIPE2 as well, since we have no other places that check it.

Ok, I'll do.

Btw, you asked:

Quoted 2 lines

Did you run into this in practice, or is it just a theoretical concern ?

I didn't faced the problem in practice. I'm trying to solve another issue and while debugging I stumbled over that code.

Helge