Skip to contentSkip to conversation

KVM: selftests: Include linux version of mman.h

PATCH In review Rick Edgecombe 3 participants 6 messages Apr 27 – Apr 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: Include linux version of mman.h

Fix a selftest build error by switching a mman.h header include from the "sys" version to the "linux" one.

Recently a reference to MADV_COLLAPSE was added to guest_memfd_test.c. This works fine if the system libc has this definition, but against older libcs, it leads to build errors like:
In file included from include/kvm_util.h:8,

                   from guest_memfd_test.c:21:
  guest_memfd_test.c: In function ‘test_collapse’:
  guest_memfd_test.c:219:47: error: ‘MADV_COLLAPSE’ undeclared (first use in this function); did you mean ‘MADV_COLD’?
      219 |         TEST_ASSERT_EQ(madvise(mem, pmd_size, MADV_COLLAPSE), -1);
          |                                               ^~~~~~~~~~~~~
    include/test_util.h:62:16: note: in definition of macro ‘TEST_ASSERT_EQ’
       62 |         typeof(a) __a = (a);                                            \
          |                ^
    guest_memfd_test.c:219:47: note: each undeclared identifier is reported only once for each function it appears in
      219 |         TEST_ASSERT_EQ(madvise(mem, pmd_size, MADV_COLLAPSE), -1);
          |                                               ^~~~~~~~~~~~~
    include/test_util.h:62:16: note: in definition of macro ‘TEST_ASSERT_EQ’
       62 |         typeof(a) __a = (a);                                            \
          |                ^

The problem is the test is pulling in the libc version of mman.h instead of the Linux one. Switch it to the Linux one so MADV_COLLAPSE will be guaranteed to be present (if the user installs the headers).

Fixes 9830209b4ae8 ("KVM: selftests: Test MADV_COLLAPSE on guest_memfd")

---

Hi,

I'm not sure if there is standard official version of the header to include for the selftests, but the KVM selftests no longer build for me. This fixes it.

 tools/testing/selftests/kvm/guest_memfd_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/guest_memfd_test.c +1 -1
index ec7644aae999..7c113b8c4f75 100644
@@ -14,7 +14,7 @@
 #include <linux/bitmap.h>
 #include <linux/falloc.h>
 #include <linux/sizes.h>
-#include <sys/mman.h>
+#include <linux/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
Signature · 1 line
2.54.0
#1 lore raw
Quoted 28 lines · Rick Edgecombe

Fix a selftest build error by switching a mman.h header include from the "sys" version to the "linux" one.

Recently a reference to MADV_COLLAPSE was added to guest_memfd_test.c. This works fine if the system libc has this definition, but against older libcs, it leads to build errors like:
In file included from include/kvm_util.h:8,

                   from guest_memfd_test.c:21:
  guest_memfd_test.c: In function ‘test_collapse’:
  guest_memfd_test.c:219:47: error: ‘MADV_COLLAPSE’ undeclared (first use in this function); did you mean ‘MADV_COLD’?
      219 |         TEST_ASSERT_EQ(madvise(mem, pmd_size, MADV_COLLAPSE), -1);
          |                                               ^~~~~~~~~~~~~
    include/test_util.h:62:16: note: in definition of macro ‘TEST_ASSERT_EQ’
       62 |         typeof(a) __a = (a);                                            \
          |                ^
    guest_memfd_test.c:219:47: note: each undeclared identifier is reported only once for each function it appears in
      219 |         TEST_ASSERT_EQ(madvise(mem, pmd_size, MADV_COLLAPSE), -1);
          |                                               ^~~~~~~~~~~~~
    include/test_util.h:62:16: note: in definition of macro ‘TEST_ASSERT_EQ’
       62 |         typeof(a) __a = (a);                                            \
          |                ^

The problem is the test is pulling in the libc version of mman.h instead of the Linux one. Switch it to the Linux one so MADV_COLLAPSE will be guaranteed to be present (if the user installs the headers).

Fixes 9830209b4ae8 ("KVM: selftests: Test MADV_COLLAPSE on guest_memfd")
Quoted 7 lines

---

Hi,

I'm not sure if there is standard official version of the header to include for the selftests, but the KVM selftests no longer build for me. This fixes it.

KVM selftests (well, selftests in general, actually) are intended to use whatever kernel headers have have been installed for selftests. E.g. for my setups, that means the headers from the git tree I'm building.

However, for mman.h, we actually need both. linux/mman.h provides the bleeding header flags and definitions, but sys/mman.h proves the syscall wrappers that selftests expect/use. The below builds because sys/mman.h is getting included elsewhere, more than likely by tools/testing/selftests/kvm/include/test_util.h.

Rather than updating individual tests on an ad hoc basis, what if we try our best to handle this once?

---
 .../testing/selftests/kvm/access_tracking_perf_test.c  |  2 +-
 tools/testing/selftests/kvm/guest_memfd_test.c         |  2 +-
 tools/testing/selftests/kvm/include/kvm_syscalls.h     | 10 ++++++++++
 tools/testing/selftests/kvm/include/test_util.h        |  2 +-
 tools/testing/selftests/kvm/lib/kvm_util.c             |  2 +-
 tools/testing/selftests/kvm/memslot_perf_test.c        |  2 +-
 .../testing/selftests/kvm/s390/shared_zeropage_test.c  |  3 +--
 tools/testing/selftests/kvm/s390/tprot.c               |  2 +-
 tools/testing/selftests/kvm/set_memory_region_test.c   |  2 +-
 9 files changed, 18 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +1 -1
index e5bbdb5bbdc3..4415c94b2866 100644
@@ -41,10 +41,10 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <pthread.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "test_util.h"
 #include "memstress.h"
tools/testing/selftests/kvm/guest_memfd_test.c +1 -1
index b099b09cbae3..79dd3f91f1ae 100644
@@ -14,10 +14,10 @@
 #include <linux/bitmap.h>
 #include <linux/falloc.h>
 #include <linux/sizes.h>
-#include <linux/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "numaif.h"
 #include "test_util.h"
tools/testing/selftests/kvm/include/kvm_syscalls.h +10 -0
index 843c9904c46f..067a4c9cf452 100644
@@ -2,8 +2,18 @@
 #ifndef SELFTEST_KVM_SYSCALLS_H
 #define SELFTEST_KVM_SYSCALLS_H
 
+/*
+ * Include both the kernel and libc versions of mman.h.  The kernel provides
+ * the most up-to-date flags and definitions, while libc provides the syscall
+ * wrappers tests expect.
+ */
+#include <linux/mman.h>
+
+#include <sys/mman.h>
 #include <sys/syscall.h>
 
+#include <test_util.h>
+
 #define MAP_ARGS0(m,...)
 #define MAP_ARGS1(m,t,a,...) m(t,a)
 #define MAP_ARGS2(m,t,a,...) m(t,a), MAP_ARGS1(m,__VA_ARGS__)
tools/testing/selftests/kvm/include/test_util.h +1 -1
index d9b433b834f1..a56271c237ae 100644
@@ -19,9 +19,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include "kselftest.h"
 
+#include <linux/mman.h>
 #include <linux/types.h>
 
 #define msecs_to_usecs(msec)    ((msec) * 1000ULL)
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
index 2a76eca7029d..e08967ef7b7b 100644
@@ -5,13 +5,13 @@
  * Copyright (C) 2018, Google LLC.
  */
 #include "test_util.h"
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "processor.h"
 #include "ucall_common.h"
 
 #include <assert.h>
 #include <sched.h>
-#include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/stat.h>
tools/testing/selftests/kvm/memslot_perf_test.c +1 -1
index 3d02db371422..e977e979470f 100644
@@ -15,7 +15,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/mman.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -23,6 +22,7 @@
 #include <linux/sizes.h>
 
 #include <test_util.h>
+#include <kvm_syscalls.h>
 #include <kvm_util.h>
 #include <processor.h>
 #include <ucall_common.h>
tools/testing/selftests/kvm/s390/shared_zeropage_test.c +1 -2
index a9e5a01200b8..478381e6f84e 100644
@@ -4,11 +4,10 @@
  *
  * Copyright (C) 2024, Red Hat, Inc.
  */
-#include <sys/mman.h>
-
 #include <linux/fs.h>
 
 #include "test_util.h"
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "kselftest.h"
 #include "ucall_common.h"
tools/testing/selftests/kvm/s390/tprot.c +1 -1
index 8054d2b178f0..d86179827a18 100644
@@ -4,8 +4,8 @@
  *
  * Copyright IBM Corp. 2021
  */
-#include <sys/mman.h>
 #include "test_util.h"
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "kselftest.h"
 #include "ucall_common.h"
tools/testing/selftests/kvm/set_memory_region_test.c +1 -1
index 9b919a231c93..e639a9db51ee 100644
@@ -8,11 +8,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
-#include <sys/mman.h>
 
 #include <linux/compiler.h>
 
 #include <test_util.h>
+#include <kvm_syscalls.h>
 #include <kvm_util.h>
 #include <processor.h>
 

base-commit: 5c96e184e344065813e652e1ca0571e448e160fb

Quoted 19 lines
 tools/testing/selftests/kvm/guest_memfd_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/guest_memfd_test.c +1 -1
index ec7644aae999..7c113b8c4f75 100644
@@ -14,7 +14,7 @@
 #include <linux/bitmap.h>
 #include <linux/falloc.h>
 #include <linux/sizes.h>
-#include <sys/mman.h>
+#include <linux/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
Signature · 1 line
2.54.0
#2 lore raw
Quoted 7 lines · Sean Christopherson

However, for mman.h, we actually need both.  linux/mman.h provides the bleeding header flags and definitions, but sys/mman.h proves the syscall wrappers that selftests expect/use.  The below builds because sys/mman.h is getting included elsewhere, more than likely by tools/testing/selftests/kvm/include/test_util.h.

Rather than updating individual tests on an ad hoc basis, what if we try our best to handle this once?

Sounds reasonable to me. But I couldn't find the base commit that let me apply this in order to test it. Do you want me to turn it into a patch?

#3 lore raw
Quoted 11 lines · Rick P Edgecombe
Quoted 7 lines · Sean Christopherson

However, for mman.h, we actually need both.  linux/mman.h provides the bleeding header flags and definitions, but sys/mman.h proves the syscall wrappers that selftests expect/use.  The below builds because sys/mman.h is getting included elsewhere, more than likely by tools/testing/selftests/kvm/include/test_util.h.

Rather than updating individual tests on an ad hoc basis, what if we try our best to handle this once?

Sounds reasonable to me. But I couldn't find the base commit that let me apply this in order to test it.

LOL, it's probably a good thing you can't, because it'd be a bit embarrassing on my end. The shortlogs I have locally are a mix of "tmp" and NSFW exclamations of frustration :-)

Quoted 1 line

Do you want me to turn it into a patch?

I can just send it as a patch? It'll probably be more work for you get it applied.

#4 lore raw
Quoted 4 lines · Sean Christopherson
Quoted 1 line

Do you want me to turn it into a patch?

I can just send it as a patch?  It'll probably be more work for you get it applied.

Works for me!

Tested-by on v1 1/1
#5 lore raw
In reply to Rick Edgecombe
Quoted 11 lines · Rick Edgecombe

Fix a selftest build error by switching a mman.h header include from the "sys" version to the "linux" one.

[...snip...]

tools/testing/selftests/kvm/guest_memfd_test.c +0 -0
index ec7644aae999..7c113b8c4f75 100644

tools/testing/selftests/kvm/guest_memfd_test.c line 17

Quoted 5 lines
tools/testing/selftests/kvm/guest_memfd_test.c +1 -1
@@ -14,7 +14,7 @@
 #include <linux/bitmap.h>
 #include <linux/falloc.h>
 #include <linux/sizes.h>
-#include <sys/mman.h>
+#include <linux/mman.h>

Thanks for reporting this! I'll take note for next time. This works fine with my compiler too.

Quoted 5 lines
tools/testing/selftests/kvm/guest_memfd_test.c +0 -1
 #include <sys/types.h>
 #include <sys/stat.h>
 
--

2.54.0