Skip to contentSkip to conversation

xfs_io: Make HAVE_MAP_SYNC more robust

PATCH In review Florian Fainelli 2 participants 4 messages Jul 20, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_io: Make HAVE_MAP_SYNC more robust

MIPS platforms building with recent kernel headers and the musl-libc toolchain will expose the following build failure:

mmap.c: In function 'mmap_f':
mmap.c:196:12: error: 'MAP_SYNC' undeclared (first use in this function); did you mean 'MS_SYNC'?

  196 |    flags = MAP_SYNC | MAP_SHARED_VALIDATE;
      |            ^~~~~~~~
      |            MS_SYNC

mmap.c:196:12: note: each undeclared identifier is reported only once for each function it appears in make[4]: *** [../include/buildrules:81: mmap.o] Error 1

The reason for that is that the linux.h header file which intends to provide a fallback definition for MAP_SYNC and MAP_SHARED_VALIDATE is included too early through:

input.h -> libfrog/projects.h -> xfs.h -> linux.h and this happens *before* sys/mman.h is included.

sys/mman.h -> bits/mman.h which has a:
#undef MAP_SYNC

see: https://git.musl-libc.org/cgit/musl/tree/arch/mips/bits/mman.h#n21

The end result is that sys/mman.h being included for the first time ends-up overriding the HAVE_MAP_SYNC fallbacks.

To remedy that, make sure that linux.h is updated to include sys/mman.h such that its fallbacks are independent of the inclusion order. As a consequence this forces us to ensure that we do not re-define accidentally MAP_SYNC or MAP_SHARED_VALIDATE so we protect against that.

Fixes dad796834cb9 ("xfs_io: add MAP_SYNC support to mmap()")
---
 include/linux.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux.h +5 -0
index 3d9f4e3dca80..c3cc8e30c677 100644
@@ -252,8 +252,13 @@ struct fsxattr {
 #endif
 
 #ifndef HAVE_MAP_SYNC
+#include <sys/mman.h>
+#ifndef MAP_SYNC
 #define MAP_SYNC 0
+#endif
+#ifndef MAP_SHARED_VALIDATE
 #define MAP_SHARED_VALIDATE 0
+#endif
 #else
 #include <asm-generic/mman.h>
 #include <asm-generic/mman-common.h>
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · Florian Fainelli

MIPS platforms building with recent kernel headers and the musl-libc toolchain will expose the following build failure:

mmap.c: In function 'mmap_f':
mmap.c:196:12: error: 'MAP_SYNC' undeclared (first use in this function); did you mean 'MS_SYNC'?

  196 |    flags = MAP_SYNC | MAP_SHARED_VALIDATE;
      |            ^~~~~~~~
      |            MS_SYNC

mmap.c:196:12: note: each undeclared identifier is reported only once for each function it appears in make[4]: *** [../include/buildrules:81: mmap.o] Error 1

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

Oh, I guess the maintainer didn't apply either of these patches, so this has been broken for years.

Well... MAP_SYNC has been with us for a while now, perhaps it makes more sense to remove all the override cruft and make xfs_io not export -S if if neither kernel headers nor libc define it?

--D

Quoted 46 lines

The reason for that is that the linux.h header file which intends to provide a fallback definition for MAP_SYNC and MAP_SHARED_VALIDATE is included too early through:

input.h -> libfrog/projects.h -> xfs.h -> linux.h and this happens *before* sys/mman.h is included.

sys/mman.h -> bits/mman.h which has a:
#undef MAP_SYNC

see: https://git.musl-libc.org/cgit/musl/tree/arch/mips/bits/mman.h#n21

The end result is that sys/mman.h being included for the first time ends-up overriding the HAVE_MAP_SYNC fallbacks.

To remedy that, make sure that linux.h is updated to include sys/mman.h such that its fallbacks are independent of the inclusion order. As a consequence this forces us to ensure that we do not re-define accidentally MAP_SYNC or MAP_SHARED_VALIDATE so we protect against that.

Fixes dad796834cb9 ("xfs_io: add MAP_SYNC support to mmap()")
---
 include/linux.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux.h +5 -0
index 3d9f4e3dca80..c3cc8e30c677 100644
@@ -252,8 +252,13 @@ struct fsxattr {
 #endif
 
 #ifndef HAVE_MAP_SYNC
+#include <sys/mman.h>
+#ifndef MAP_SYNC
 #define MAP_SYNC 0
+#endif
+#ifndef MAP_SHARED_VALIDATE
 #define MAP_SHARED_VALIDATE 0
+#endif
 #else
 #include <asm-generic/mman.h>
 #include <asm-generic/mman-common.h>
Signature · 1 line
2.25.1
#2 lore raw
Quoted 12 lines · Darrick J. Wong
Quoted 10 lines · Florian Fainelli

MIPS platforms building with recent kernel headers and the musl-libc toolchain will expose the following build failure:

mmap.c: In function 'mmap_f':
mmap.c:196:12: error: 'MAP_SYNC' undeclared (first use in this function); did you mean 'MS_SYNC'?

  196 |    flags = MAP_SYNC | MAP_SHARED_VALIDATE;
      |            ^~~~~~~~
      |            MS_SYNC

mmap.c:196:12: note: each undeclared identifier is reported only once for each function it appears in make[4]: *** [../include/buildrules:81: mmap.o] Error 1

Quoted 5 lines

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

First off, I was not aware of these two proposed solutions so apologies for submitting a third (are there more) one.

The common problem I see with both proposed patches is that they specifically tackle io/mmap.c when really any inclusion order of linux.h and sys/mman.h could and would lead to the the same problem to re-surface somewhere else in a different file. So sure enough there is only mmap.c now, but it has been identified that this specific include order is problematic so we ought to address it in a general way.

Quoted 3 lines

Oh, I guess the maintainer didn't apply either of these patches, so this has been broken for years.

Fabrice's patch is only a few months old, and but "[email protected]"'s is nearly 4 years old...

Quoted 4 lines

Well... MAP_SYNC has been with us for a while now, perhaps it makes more sense to remove all the override cruft and make xfs_io not export -S if if neither kernel headers nor libc define it?

Sure that would work too, but we will still end up with some MAP_SYNC conditional code, so the original intent of always defining it seemed laudable.

Quoted 49 lines

--D

Quoted 46 lines

The reason for that is that the linux.h header file which intends to provide a fallback definition for MAP_SYNC and MAP_SHARED_VALIDATE is included too early through:

input.h -> libfrog/projects.h -> xfs.h -> linux.h and this happens *before* sys/mman.h is included.

sys/mman.h -> bits/mman.h which has a:
#undef MAP_SYNC

see: https://git.musl-libc.org/cgit/musl/tree/arch/mips/bits/mman.h#n21

The end result is that sys/mman.h being included for the first time ends-up overriding the HAVE_MAP_SYNC fallbacks.

To remedy that, make sure that linux.h is updated to include sys/mman.h such that its fallbacks are independent of the inclusion order. As a consequence this forces us to ensure that we do not re-define accidentally MAP_SYNC or MAP_SHARED_VALIDATE so we protect against that.

Fixes dad796834cb9 ("xfs_io: add MAP_SYNC support to mmap()")
---
 include/linux.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux.h +5 -0
index 3d9f4e3dca80..c3cc8e30c677 100644
@@ -252,8 +252,13 @@ struct fsxattr {
 #endif
 
 #ifndef HAVE_MAP_SYNC
+#include <sys/mman.h>
+#ifndef MAP_SYNC
 #define MAP_SYNC 0
+#endif
+#ifndef MAP_SHARED_VALIDATE
 #define MAP_SHARED_VALIDATE 0
+#endif
 #else
 #include <asm-generic/mman.h>
 #include <asm-generic/mman-common.h>
Signature · 1 line
2.25.1
Signature · 1 line
Florian
#3 lore raw
Quoted 13 lines · Florian Fainelli
Quoted 12 lines · Darrick J. Wong
Quoted 10 lines · Florian Fainelli

MIPS platforms building with recent kernel headers and the musl-libc toolchain will expose the following build failure:

mmap.c: In function 'mmap_f':
mmap.c:196:12: error: 'MAP_SYNC' undeclared (first use in this function); did you mean 'MS_SYNC'?

  196 |    flags = MAP_SYNC | MAP_SHARED_VALIDATE;
      |            ^~~~~~~~
      |            MS_SYNC

mmap.c:196:12: note: each undeclared identifier is reported only once for each function it appears in make[4]: *** [../include/buildrules:81: mmap.o] Error 1

Quoted 8 lines
Quoted 5 lines

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

Didn't we already fix this?
https://lore.kernel.org/linux-xfs/[email protected]/

First off, I was not aware of these two proposed solutions so apologies for submitting a third (are there more) one.

No worries :)

Quoted 3 lines

The common problem I see with both proposed patches is that they specifically tackle io/mmap.c when really any inclusion order of linux.h

OH, yikes, this is what goes into include/linux.h (which is also exported as /usr/include/xfs/linux.h):

#ifndef HAVE_MAP_SYNC
#define MAP_SYNC 0
#define MAP_SHARED_VALIDATE 0
#else
#include <asm-generic/mman.h>
#include <asm-generic/mman-common.h>
#endif /* HAVE_MAP_SYNC */

xfsprogs has no business exporting redefinitions of kernel symbols to userspace. This innocent looking program on an x64 system:

#include <stdio.h>
#include <sys/mman.h>
#include <xfs/xfs.h>

int main(int argc, char *argv[]) {
	printf("MAP_SYNC 0x%x\n", MAP_SYNC);
}

prints "MAP_SYNC 0x0", not the "MAP_SYNC 0x80000" that you'd expect.

Ok, all this override stuff needs to die.

Quoted 4 lines

and sys/mman.h could and would lead to the the same problem to re-surface somewhere else in a different file. So sure enough there is only mmap.c now, but it has been identified that this specific include order is problematic so we ought to address it in a general way.

Exactly.

Quoted 6 lines
Quoted 3 lines

Oh, I guess the maintainer didn't apply either of these patches, so this has been broken for years.

Fabrice's patch is only a few months old, and but "[email protected]"'s is nearly 4 years old...

Yep.

Quoted 8 lines
Quoted 4 lines

Well... MAP_SYNC has been with us for a while now, perhaps it makes more sense to remove all the override cruft and make xfs_io not export -S if if neither kernel headers nor libc define it?

Sure that would work too, but we will still end up with some MAP_SYNC conditional code, so the original intent of always defining it seemed laudable.

Oh no, if I cleaned up xfs_io mmap command, I'd also get rid of the override stuff too. Working on it...

--D

Quoted 54 lines
Quoted 50 lines

--D

Quoted 46 lines

The reason for that is that the linux.h header file which intends to provide a fallback definition for MAP_SYNC and MAP_SHARED_VALIDATE is included too early through:

input.h -> libfrog/projects.h -> xfs.h -> linux.h and this happens *before* sys/mman.h is included.

sys/mman.h -> bits/mman.h which has a:
#undef MAP_SYNC

see: https://git.musl-libc.org/cgit/musl/tree/arch/mips/bits/mman.h#n21

The end result is that sys/mman.h being included for the first time ends-up overriding the HAVE_MAP_SYNC fallbacks.

To remedy that, make sure that linux.h is updated to include sys/mman.h such that its fallbacks are independent of the inclusion order. As a consequence this forces us to ensure that we do not re-define accidentally MAP_SYNC or MAP_SHARED_VALIDATE so we protect against that.

Fixes dad796834cb9 ("xfs_io: add MAP_SYNC support to mmap()")
---
 include/linux.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux.h +5 -0
index 3d9f4e3dca80..c3cc8e30c677 100644
@@ -252,8 +252,13 @@ struct fsxattr {
 #endif
 
 #ifndef HAVE_MAP_SYNC
+#include <sys/mman.h>
+#ifndef MAP_SYNC
 #define MAP_SYNC 0
+#endif
+#ifndef MAP_SHARED_VALIDATE
 #define MAP_SHARED_VALIDATE 0
+#endif
 #else
 #include <asm-generic/mman.h>
 #include <asm-generic/mman-common.h>
Signature · 1 line
2.25.1
Signature · 1 line
Florian