Skip to contentSkip to conversation

kexec: provide a memfd_create() wrapper if not present in libc

PATCH Applied Julien Olivain 3 participants 4 messages Sep 23, 2023 – Oct 4, 2023

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

PATCH 1/1 kexec: provide a memfd_create() wrapper if not present in libc

Commit 714fa115 "kexec/arm64: Simplify the code for zImage" introduced a use of the memfd_create() system call, included in version kexec-tools v2.0.27.

This system call was introduced in kernel commit [1], first included in kernel v3.17 (released on 2014-10-05).

The memfd_create() glibc wrapper function was added much later in commit [2], first included in glibc version 2.27 (released on 2018-02-01).

This direct use memfd_create() introduced a requirement on Kernel >= 3.17 and glibc >= 2.27.

There is old toolchains like [3] for example (which ships gcc 7.3.1, glibc 2.25 and includes kernel v4.10 headers), that can still be used to build newer kernels. Even if such toolchains can be seen as outdated, they are is still claimed as supported by recent kernel. For example, Kernel v6.5.5 has a requirement on gcc version 5.1 and greater. See [4].

Moreover, kexec-tools <= 2.0.26 could be compiled using recent toolchains with alternative libc (e.g. uclibc-ng, musl) which are not providing the memfd_create() wrapper.

When compiling kexec-tools v2.0.27 with a toolchain not providing the memfd_create() syscall wrapper, the compilation fail with message:

    kexec/kexec.c: In function 'copybuf_memfd':
    kexec/kexec.c:645:7: warning: implicit declaration of function 'memfd_create'; did you mean 'SYS_memfd_create'? [-Wimplicit-function-declaration]
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
           ^~~~~~~~~~~~
           SYS_memfd_create
    kexec/kexec.c:645:30: error: 'MFD_ALLOW_SEALING' undeclared (first use in this function); did you mean '_PC_ALLOC_SIZE_MIN'?
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
                                  ^~~~~~~~~~~~~~~~~
                                  _PC_ALLOC_SIZE_MIN

In order to let kexec-tools compile in a wider range of configurations, this commit adds a memfd_create() function check in autoconf configure script, and adds a system call wrapper which will be used if the function is not available. With this commit, the environment requirement is relaxed to only kernel >= v3.17.

Note: this issue was found in kexec-tools integration in Buildroot [5] using the command "utils/test-pkg -a -p kexec", which tests many toolchain/arch combinations.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=9183df25fe7b194563db3fec6dc3202a5855839c
[2] https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=59d2cbb1fe4b8601d5cbd359c3806973eab6c62d
[3] https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
[4] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/process/changes.rst?h=v6.5.5#n32
[5] https://buildroot.org/

---
 configure.ac  |  3 +++
 kexec/kexec.c | 11 +++++++++++
 2 files changed, 14 insertions(+)
configure.ac +3 -0
index 352eefe..602de79 100644
@@ -208,6 +208,9 @@ if test "$ac_cv_lib_xenctrl_xc_kexec_load" = yes ; then
 		AC_MSG_NOTICE([The kexec_status call is not available]))
 fi
 
+dnl Check if libc has the memfd_create() syscall wrapper
+AC_CHECK_FUNCS([memfd_create])
+
 dnl ---Sanity checks
 if test "$CC"      = "no"; then AC_MSG_ERROR([cc not found]); fi
 if test "$CPP"     = "no"; then AC_MSG_ERROR([cpp not found]); fi
kexec/kexec.c +11 -0
index 1edbd34..946f831 100644
@@ -31,6 +31,10 @@
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifndef HAVE_MEMFD_CREATE
+#include <linux/memfd.h>
+#include <sys/syscall.h>
+#endif
 #include <sys/reboot.h>
 #include <sys/mman.h>
 #include <unistd.h>
@@ -638,6 +642,13 @@ char *slurp_decompress_file(const char *filename, off_t *r_size)
 	return kernel_buf;
 }
 
+#ifndef HAVE_MEMFD_CREATE
+static int memfd_create(const char *name, unsigned int flags)
+{
+	return syscall(SYS_memfd_create, name, flags);
+}
+#endif
+
 static int copybuf_memfd(const char *kernel_buf, size_t size)
 {
 	int fd, count;
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw
Quoted 45 lines · Julien Olivain

Commit 714fa115 "kexec/arm64: Simplify the code for zImage" introduced a use of the memfd_create() system call, included in version kexec-tools v2.0.27.

This system call was introduced in kernel commit [1], first included in kernel v3.17 (released on 2014-10-05).

The memfd_create() glibc wrapper function was added much later in commit [2], first included in glibc version 2.27 (released on 2018-02-01).

This direct use memfd_create() introduced a requirement on Kernel >= 3.17 and glibc >= 2.27.

There is old toolchains like [3] for example (which ships gcc 7.3.1, glibc 2.25 and includes kernel v4.10 headers), that can still be used to build newer kernels. Even if such toolchains can be seen as outdated, they are is still claimed as supported by recent kernel. For example, Kernel v6.5.5 has a requirement on gcc version 5.1 and greater. See [4].

Moreover, kexec-tools <= 2.0.26 could be compiled using recent toolchains with alternative libc (e.g. uclibc-ng, musl) which are not providing the memfd_create() wrapper.

When compiling kexec-tools v2.0.27 with a toolchain not providing the memfd_create() syscall wrapper, the compilation fail with message:

    kexec/kexec.c: In function 'copybuf_memfd':
    kexec/kexec.c:645:7: warning: implicit declaration of function 'memfd_create'; did you mean 'SYS_memfd_create'? [-Wimplicit-function-declaration]
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
           ^~~~~~~~~~~~
           SYS_memfd_create
    kexec/kexec.c:645:30: error: 'MFD_ALLOW_SEALING' undeclared (first use in this function); did you mean '_PC_ALLOC_SIZE_MIN'?
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
                                  ^~~~~~~~~~~~~~~~~
                                  _PC_ALLOC_SIZE_MIN

In order to let kexec-tools compile in a wider range of configurations, this commit adds a memfd_create() function check in autoconf configure script, and adds a system call wrapper which will be used if the function is not available. With this commit, the environment requirement is relaxed to only kernel >= v3.17.

Quoted 3 lines

Note: this issue was found in kexec-tools integration in Buildroot [5] using the command "utils/test-pkg -a -p kexec", which tests many toolchain/arch combinations.

I guess maybe the test was done on non x86 arch, when I tried to build on old versions
I got another failure of lacking "getrandom". Only quickly did a
build test with commenting out
the getrandom code, the build passed with your patch.

Thanks
Dave

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw

Hi Dave,

Quoted 52 lines · Dave Young
Quoted 47 lines · Julien Olivain

Commit 714fa115 "kexec/arm64: Simplify the code for zImage" introduced a use of the memfd_create() system call, included in version kexec-tools v2.0.27.

This system call was introduced in kernel commit [1], first included in kernel v3.17 (released on 2014-10-05).

The memfd_create() glibc wrapper function was added much later in commit [2], first included in glibc version 2.27 (released on 2018-02-01).

This direct use memfd_create() introduced a requirement on Kernel >= 3.17 and glibc >= 2.27.

There is old toolchains like [3] for example (which ships gcc 7.3.1, glibc 2.25 and includes kernel v4.10 headers), that can still be used to build newer kernels. Even if such toolchains can be seen as outdated, they are is still claimed as supported by recent kernel. For example, Kernel v6.5.5 has a requirement on gcc version 5.1 and greater. See [4].

Moreover, kexec-tools <= 2.0.26 could be compiled using recent toolchains with alternative libc (e.g. uclibc-ng, musl) which are not providing the memfd_create() wrapper.

When compiling kexec-tools v2.0.27 with a toolchain not providing the memfd_create() syscall wrapper, the compilation fail with message:

kexec/kexec.c: In function 'copybuf_memfd':
kexec/kexec.c:645:7: warning: implicit declaration of function
'memfd_create'; did you mean 'SYS_memfd_create'? [-Wimplicit-function-declaration]

      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
           ^~~~~~~~~~~~
           SYS_memfd_create
    kexec/kexec.c:645:30: error: 'MFD_ALLOW_SEALING' undeclared (first 
use in this function); did you mean '_PC_ALLOC_SIZE_MIN'?
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
                                  ^~~~~~~~~~~~~~~~~
                                  _PC_ALLOC_SIZE_MIN

In order to let kexec-tools compile in a wider range of configurations, this commit adds a memfd_create() function check in autoconf configure script, and adds a system call wrapper which will be used if the function is not available. With this commit, the environment requirement is relaxed to only kernel >= v3.17.

Quoted 3 lines

Note: this issue was found in kexec-tools integration in Buildroot [5] using the command "utils/test-pkg -a -p kexec", which tests many toolchain/arch combinations.

Quoted 5 lines

I guess maybe the test was done on non x86 arch, when I tried to build on old versions
I got another failure of lacking "getrandom". Only quickly did a
build test with commenting out
the getrandom code, the build passed with your patch.

Thanks for testing!

Regarding the getrandom() issue: according to [1], it requires Kernel 3.17 and glibc 2.25. It is also implemented in uclibc-ng and musl which is why I didn't see the issue.

getrandom() being a GNU extension, it needs '_GNU_SOURCE' to be defined in uclibc-ng. Patch [2] was proposed a while back to fix builds on m68k and uclibc-ng.

Quoted 2 lines

Thanks
Dave

Best regards,

Julien.

[1] https://man7.org/linux/man-pages/man2/getrandom.2.html#HISTORY
[2] https://lore.kernel.org/kexec/[email protected]/

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Applied
#3 lore raw
In reply to Julien Olivain
Quoted 48 lines · Julien Olivain

Commit 714fa115 "kexec/arm64: Simplify the code for zImage" introduced a use of the memfd_create() system call, included in version kexec-tools v2.0.27.

This system call was introduced in kernel commit [1], first included in kernel v3.17 (released on 2014-10-05).

The memfd_create() glibc wrapper function was added much later in commit [2], first included in glibc version 2.27 (released on 2018-02-01).

This direct use memfd_create() introduced a requirement on Kernel >= 3.17 and glibc >= 2.27.

There is old toolchains like [3] for example (which ships gcc 7.3.1, glibc 2.25 and includes kernel v4.10 headers), that can still be used to build newer kernels. Even if such toolchains can be seen as outdated, they are is still claimed as supported by recent kernel. For example, Kernel v6.5.5 has a requirement on gcc version 5.1 and greater. See [4].

Moreover, kexec-tools <= 2.0.26 could be compiled using recent toolchains with alternative libc (e.g. uclibc-ng, musl) which are not providing the memfd_create() wrapper.

When compiling kexec-tools v2.0.27 with a toolchain not providing the memfd_create() syscall wrapper, the compilation fail with message:

    kexec/kexec.c: In function 'copybuf_memfd':
    kexec/kexec.c:645:7: warning: implicit declaration of function 'memfd_create'; did you mean 'SYS_memfd_create'? [-Wimplicit-function-declaration]
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
           ^~~~~~~~~~~~
           SYS_memfd_create
    kexec/kexec.c:645:30: error: 'MFD_ALLOW_SEALING' undeclared (first use in this function); did you mean '_PC_ALLOC_SIZE_MIN'?
      fd = memfd_create("kernel", MFD_ALLOW_SEALING);
                                  ^~~~~~~~~~~~~~~~~
                                  _PC_ALLOC_SIZE_MIN

In order to let kexec-tools compile in a wider range of configurations, this commit adds a memfd_create() function check in autoconf configure script, and adds a system call wrapper which will be used if the function is not available. With this commit, the environment requirement is relaxed to only kernel >= v3.17.

Note: this issue was found in kexec-tools integration in Buildroot [5] using the command "utils/test-pkg -a -p kexec", which tests many toolchain/arch combinations.

Quoted 7 lines

[1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=9183df25fe7b194563db3fec6dc3202a5855839c
[2] https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=59d2cbb1fe4b8601d5cbd359c3806973eab6c62d
[3] https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
[4] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/process/changes.rst?h=v6.5.5#n32
[5] https://buildroot.org/

Thanks Julien,

applied.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec