TEST(check_file_mmap) will fail when we run the test on tmpfs and report:
mincore_selftest.c:261:check_file_mmap:Expected ra_pages (0) > 0 (0)
mincore_selftest.c:262:check_file_mmap:No read-ahead pages found in memory
For some embaded system, maybe there is only tmpfs file system exist,
run the test will fail, or we install the test on a filesystem that
has no backend, also it will fail as unepected.
So add a checking of block dev for the filesystem at first.
Signed-off-by: Zhao Gongyi <zhaogongyi(a)huawei.com>
---
.../selftests/mincore/mincore_selftest.c | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
index 4c88238fc8f0..287351a599a2 100644
--- a/tools/testing/selftests/mincore/mincore_selftest.c
+++ b/tools/testing/selftests/mincore/mincore_selftest.c
@@ -14,6 +14,9 @@
#include <sys/mman.h>
#include <string.h>
#include <fcntl.h>
+#include <mntent.h>
+#include <sys/stat.h>
+#include <linux/fs.h>
#include "../kselftest.h"
#include "../kselftest_harness.h"
@@ -173,6 +176,43 @@ TEST(check_huge_pages)
munmap(addr, page_size);
}
+static struct mntent* find_mount_point(const char *name)
+{
+ struct stat s;
+ FILE *mtab_fp;
+ struct mntent *mountEntry;
+ dev_t devno_of_name;
+
+ if (stat(name, &s) != 0)
+ return NULL;
+
+ devno_of_name = s.st_dev;
+
+ mtab_fp = setmntent("/proc/mounts", "r");
+ if (!mtab_fp)
+ return NULL;
+
+ while ((mountEntry = getmntent(mtab_fp)) != NULL) {
+ if (strcmp(name, mountEntry->mnt_dir) == 0
+ || strcmp(name, mountEntry->mnt_fsname) == 0) {
+ break;
+ }
+
+ if (mountEntry->mnt_fsname[0] == '/'
+ && stat(mountEntry->mnt_fsname, &s) == 0
+ && s.st_rdev == devno_of_name) {
+ break;
+ }
+
+ if (stat(mountEntry->mnt_dir, &s) == 0
+ && s.st_dev == devno_of_name) {
+ break;
+ }
+ }
+ endmntent(mtab_fp);
+
+ return mountEntry;
+}
/*
* Test mincore() behavior on a file-backed page.
@@ -194,6 +234,19 @@ TEST(check_file_mmap)
int fd;
int i;
int ra_pages = 0;
+ struct stat s;
+ struct mntent *mount_entry;
+
+ mount_entry = find_mount_point(".");
+ ASSERT_NE(NULL, mount_entry) {
+ TH_LOG("Find mount point of '.' failed");
+ }
+
+ ASSERT_EQ(0, (stat(mount_entry->mnt_fsname, &s) != 0
+ || !S_ISBLK(s.st_mode))) {
+ TH_LOG("There is no a block dev on mount point, "
+ "test is not supported");
+ }
page_size = sysconf(_SC_PAGESIZE);
vec_size = FILE_SIZE / page_size;
--
2.17.1
When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.
One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.
It turns out that -fno-tree-loop-distribute-patterns disables replacement
of strlen-like loops with calls to strlen and that this option is accepted
in the optimize() function attribute. Thus at least it allows us to make
sure our local definition is not replaced with a self jump. The function
only needs to be renamed back to strlen() so that the symbol exists, which
implies that nolibc_strlen() which is used on variable strings has to be
declared as a macro that points back to it before the strlen() macro is
redifined.
It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.
Reported-by: kernel test robot <yujie.liu(a)intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a21 ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck(a)kernel.org>
Signed-off-by: Willy Tarreau <w(a)1wt.eu>
---
tools/include/nolibc/string.h | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index bef35bee9c44..5ef8778cd16f 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -125,10 +125,16 @@ char *strcpy(char *dst, const char *src)
}
/* this function is only used with arguments that are not constants or when
- * it's not known because optimizations are disabled.
+ * it's not known because optimizations are disabled. Note that gcc 12
+ * recognizes an strlen() pattern and replaces it with a jump to strlen(),
+ * thus itself, hence the optimize() attribute below that's meant to disable
+ * this confusing practice.
*/
+#if defined(__GNUC__) && (__GNUC__ >= 12)
+__attribute__((optimize("no-tree-loop-distribute-patterns")))
+#endif
static __attribute__((unused))
-size_t nolibc_strlen(const char *str)
+size_t strlen(const char *str)
{
size_t len;
@@ -140,13 +146,12 @@ size_t nolibc_strlen(const char *str)
* the two branches, then will rely on an external definition of strlen().
*/
#if defined(__OPTIMIZE__)
+#define nolibc_strlen(x) strlen(x)
#define strlen(str) ({ \
__builtin_constant_p((str)) ? \
__builtin_strlen((str)) : \
nolibc_strlen((str)); \
})
-#else
-#define strlen(str) nolibc_strlen((str))
#endif
static __attribute__((unused))
--
2.35.3
This change enables to extend CFLAGS and LDFLAGS from command line, e.g.
to extend compiler checks: make USERCFLAGS=-Werror USERLDFLAGS=-static
USERCFLAGS and USERLDFLAGS are documented in
Documentation/kbuild/makefiles.rst and Documentation/kbuild/kbuild.rst
This should be backported (down to 5.10) to improve previous kernel
versions testing as well.
Cc: Shuah Khan <skhan(a)linuxfoundation.org>
Cc: stable(a)vger.kernel.org
Signed-off-by: Mickaël Salaün <mic(a)digikod.net>
Link: https://lore.kernel.org/r/20220909103901.1503436-1-mic@digikod.net
---
tools/testing/selftests/lib.mk | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk
index d44c72b3abe3..da47a0257165 100644
--- a/tools/testing/selftests/lib.mk
+++ b/tools/testing/selftests/lib.mk
@@ -119,6 +119,11 @@ endef
clean:
$(CLEAN)
+# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
+# make USERCFLAGS=-Werror USERLDFLAGS=-static
+CFLAGS += $(USERCFLAGS)
+LDFLAGS += $(USERLDFLAGS)
+
# When make O= with kselftest target from main level
# the following aren't defined.
#
base-commit: 7e18e42e4b280c85b76967a9106a13ca61c16179
--
2.37.2