2024-02-27 07:42:58

by Meng, Li (Jassmine)

[permalink] [raw]
Subject: [RESEND PATCH] selftests/overlayfs: fix compilation error in overlayfs

make -C tools/testing/selftests, compiling dev_in_maps fail.
In file included from dev_in_maps.c:10:
/usr/include/x86_64-linux-gnu/sys/mount.h:35:3: error: expected identifier before numeric constant
35 | MS_RDONLY = 1, /* Mount read-only. */
| ^~~~~~~~~

That sys/mount.h has to be included before linux/mount.h.

Signed-off-by: Meng Li <[email protected]>
---
tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
index e19ab0e85709..871a0923c06e 100644
--- a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
+++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
@@ -7,11 +7,11 @@

#include <linux/unistd.h>
#include <linux/types.h>
-#include <linux/mount.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/mman.h>
+#include <linux/mount.h>
#include <sched.h>
#include <fcntl.h>

--
2.34.1



2024-02-27 16:43:36

by Shuah Khan

[permalink] [raw]
Subject: Re: [RESEND PATCH] selftests/overlayfs: fix compilation error in overlayfs

On 2/27/24 00:42, Meng Li wrote:
> make -C tools/testing/selftests, compiling dev_in_maps fail.
> In file included from dev_in_maps.c:10:
> /usr/include/x86_64-linux-gnu/sys/mount.h:35:3: error: expected identifier before numeric constant
> 35 | MS_RDONLY = 1, /* Mount read-only. */
> | ^~~~~~~~~
>
> That sys/mount.h has to be included before linux/mount.h.
>
> Signed-off-by: Meng Li <[email protected]>
> ---
> tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>

I don't see this problem when I build it on my system when
I run:

make -C tools/testing/selftests
or
make -C tools/testing/selftests/filesystems/overlayfs

Are you running this after doing headers_install?

thanks,
-- Shuah






2024-02-27 21:30:54

by Shuah Khan

[permalink] [raw]
Subject: Re: [RESEND PATCH] selftests/overlayfs: fix compilation error in overlayfs

On 2/27/24 14:20, Andrei Vagin wrote:
> On Tue, Feb 27, 2024 at 8:41 AM Shuah Khan <[email protected]> wrote:
>>
>> On 2/27/24 00:42, Meng Li wrote:
>>> make -C tools/testing/selftests, compiling dev_in_maps fail.
>>> In file included from dev_in_maps.c:10:
>>> /usr/include/x86_64-linux-gnu/sys/mount.h:35:3: error: expected identifier before numeric constant
>>> 35 | MS_RDONLY = 1, /* Mount read-only. */
>>> | ^~~~~~~~~
>>>
>>> That sys/mount.h has to be included before linux/mount.h.
>>>
>>> Signed-off-by: Meng Li <[email protected]>
>>> ---
>>> tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>
>> I don't see this problem when I build it on my system when
>> I run:
>>
>> make -C tools/testing/selftests
>> or
>> make -C tools/testing/selftests/filesystems/overlayfs
>>
>> Are you running this after doing headers_install?
>
> It depends on libc headers. It can work with one libc and doesn't work
> with another one. I have seen many times when linux headers conflicted
> with libc headers. The only reliable way to avoid this sort of issues is
> to include just one linux or libc header.
>
> In this case, we can do something like this:
>
> diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
> b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
> index e19ab0e85709..f1ba82e52192 100644
> --- a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
> +++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
> @@ -10,7 +10,6 @@
> #include <linux/mount.h>
> #include <sys/syscall.h>
> #include <sys/stat.h>
> -#include <sys/mount.h>
> #include <sys/mman.h>
> #include <sched.h>
> #include <fcntl.h>
> @@ -40,6 +39,14 @@ static int sys_move_mount(int from_dfd, const char
> *from_pathname,
> return syscall(__NR_move_mount, from_dfd, from_pathname,
> to_dfd, to_pathname, flags);
> }
>
> +static int sys_mount(const char *source, const char *target,
> + const char *filesystemtype, unsigned long mountflags,
> + const void *data)
> +{
> + return syscall(__NR_mount, source, target, filesystemtype,
> mountflags, data);
> +}
> +
> +
> static long get_file_dev_and_inode(void *addr, struct statx *stx)
> {
> char buf[4096];
> @@ -167,7 +174,7 @@ int main(int argc, char **argv)
> return 1;
> }
>
> - if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
> + if (sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
> pr_perror("mount");
> return 1;
> }
>


This is definitely better solution to this problem than reordering
the includes only find another problem down the road.

thanks,
-- Shuah


2024-02-27 21:48:40

by Andrei Vagin

[permalink] [raw]
Subject: Re: [RESEND PATCH] selftests/overlayfs: fix compilation error in overlayfs

On Tue, Feb 27, 2024 at 8:41 AM Shuah Khan <[email protected]> wrote:
>
> On 2/27/24 00:42, Meng Li wrote:
> > make -C tools/testing/selftests, compiling dev_in_maps fail.
> > In file included from dev_in_maps.c:10:
> > /usr/include/x86_64-linux-gnu/sys/mount.h:35:3: error: expected identifier before numeric constant
> > 35 | MS_RDONLY = 1, /* Mount read-only. */
> > | ^~~~~~~~~
> >
> > That sys/mount.h has to be included before linux/mount.h.
> >
> > Signed-off-by: Meng Li <[email protected]>
> > ---
> > tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
>
> I don't see this problem when I build it on my system when
> I run:
>
> make -C tools/testing/selftests
> or
> make -C tools/testing/selftests/filesystems/overlayfs
>
> Are you running this after doing headers_install?

It depends on libc headers. It can work with one libc and doesn't work
with another one. I have seen many times when linux headers conflicted
with libc headers. The only reliable way to avoid this sort of issues is
to include just one linux or libc header.

In this case, we can do something like this:

diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
index e19ab0e85709..f1ba82e52192 100644
--- a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
+++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
@@ -10,7 +10,6 @@
#include <linux/mount.h>
#include <sys/syscall.h>
#include <sys/stat.h>
-#include <sys/mount.h>
#include <sys/mman.h>
#include <sched.h>
#include <fcntl.h>
@@ -40,6 +39,14 @@ static int sys_move_mount(int from_dfd, const char
*from_pathname,
return syscall(__NR_move_mount, from_dfd, from_pathname,
to_dfd, to_pathname, flags);
}

+static int sys_mount(const char *source, const char *target,
+ const char *filesystemtype, unsigned long mountflags,
+ const void *data)
+{
+ return syscall(__NR_mount, source, target, filesystemtype,
mountflags, data);
+}
+
+
static long get_file_dev_and_inode(void *addr, struct statx *stx)
{
char buf[4096];
@@ -167,7 +174,7 @@ int main(int argc, char **argv)
return 1;
}

- if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
+ if (sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
pr_perror("mount");
return 1;
}

Thanks,
Andrei