[PATCH v2 02/11] um: use tools/include for user files

Benjamin Berg
2025-09-19T15:36:05Z

From: Benjamin Berg <benjamin.berg@intel.com>

Using the kernel headers directly from the userspace parts of UML is
problematic. Switch to use the headers from the tools/include
subdirectory instead. These contain stripped down versions that work
well for UML and only relatively small adjustments are needed to make it
work.

This adds code to create two symlinks so that the userspace code can
still find asm-offsets.h and user_constants.h. Other than that, some
includes are moved into USER_CFLAGS instead of handling them in
Makefile.rules.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>

---
v2:
- Fix out-of-tree building
---
arch/um/Makefile | 18 ++++++++++++------
arch/um/include/shared/init.h | 2 +-
arch/um/include/shared/user.h | 5 -----
arch/um/kernel/skas/stub.c | 1 +
arch/um/kernel/skas/stub_exe.c | 4 ++--
arch/um/os-Linux/skas/process.c | 6 ++----
arch/um/os-Linux/start_up.c | 4 ++--
arch/um/scripts/Makefile.rules | 2 +-
arch/x86/um/Makefile | 6 ++++--
arch/x86/um/user-offsets.c | 1 -
10 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/arch/um/Makefile b/arch/um/Makefile
index 7be0143b5ba3..f7c509262568 100644
--- a/arch/um/Makefile
+++ b/arch/um/Makefile
@@ -42,7 +42,7 @@ include $(srctree)/$(HOST_DIR)/Makefile.um
core-y += $(HOST_DIR)/um/

SHARED_HEADERS := $(ARCH_DIR)/include/shared
-ARCH_INCLUDE := -I$(srctree)/$(SHARED_HEADERS)
+ARCH_INCLUDE := -I$(srctree)/$(SHARED_HEADERS) -I$(objtree)/$(SHARED_HEADERS)
ARCH_INCLUDE += -I$(srctree)/$(HOST_DIR)/um/shared
KBUILD_CPPFLAGS += -I$(srctree)/$(HOST_DIR)/um

@@ -70,10 +70,13 @@ KBUILD_AFLAGS += $(ARCH_INCLUDE)

USER_CFLAGS = $(patsubst $(KERNEL_DEFINES),,$(patsubst -I%,,$(KBUILD_CFLAGS))) \
$(ARCH_INCLUDE) $(MODE_INCLUDE) $(filter -I%,$(CFLAGS)) \
- -D_FILE_OFFSET_BITS=64 -idirafter $(srctree)/include \
- -idirafter $(objtree)/include -D__KERNEL__ -D__UM_HOST__ \
- -include $(srctree)/include/linux/compiler-version.h \
- -include $(srctree)/include/linux/kconfig.h
+ -idirafter $(srctree)/tools/include \
+ -D__UM_HOST__ \
+ -include $(srctree)/tools/include/linux/compiler.h \
+ -include $(srctree)/tools/include/linux/kconfig.h \
+ -include $(objtree)/include/generated/autoconf.h \
+ -include $(srctree)/include/linux/kern_levels.h \
+ -include $(srctree)/$(ARCH_DIR)/include/shared/user.h

#This will adjust *FLAGS accordingly to the platform.
include $(srctree)/$(ARCH_DIR)/Makefile-os-Linux
@@ -116,6 +119,9 @@ archheaders:

archprepare:
$(Q)$(MAKE) $(build)=$(HOST_DIR)/um include/generated/user_constants.h
+ $(Q)mkdir -p $(ARCH_DIR)/include/shared/generated
+ $(Q)ln -fs ../../../../../include/generated/user_constants.h $(ARCH_DIR)/include/shared/generated/
+ $(Q)ln -fs ../../../../../include/generated/asm-offsets.h $(ARCH_DIR)/include/shared/generated/

LINK-$(CONFIG_LD_SCRIPT_STATIC) += -static
LINK-$(CONFIG_LD_SCRIPT_DYN) += -no-pie
@@ -147,7 +153,7 @@ export CFLAGS_vmlinux := $(LINK-y) $(LINK_WRAPS) $(LD_FLAGS_CMDLINE) $(CC_FLAGS_
# When cleaning we don't include .config, so we don't include
# TT or skas makefiles and don't clean skas_ptregs.h.
CLEAN_FILES += linux x.i gmon.out
-MRPROPER_FILES += $(HOST_DIR)/include/generated
+MRPROPER_FILES += $(HOST_DIR)/include/generated $(ARCH_DIR)/include/shared/generated

archclean:
@find . \( -name '*.bb' -o -name '*.bbg' -o -name '*.da' \
diff --git a/arch/um/include/shared/init.h b/arch/um/include/shared/init.h
index 1a659e2e8cc3..d201705bedb3 100644
--- a/arch/um/include/shared/init.h
+++ b/arch/um/include/shared/init.h
@@ -41,7 +41,7 @@
typedef int (*initcall_t)(void);
typedef void (*exitcall_t)(void);

-#include <linux/compiler_types.h>
+#define __section(section) __attribute__((__section__(section)))

/* These are for everybody (although not all archs will actually
discard it in modules) */
diff --git a/arch/um/include/shared/user.h b/arch/um/include/shared/user.h
index 139eb78a4767..c9b853e1282f 100644
--- a/arch/um/include/shared/user.h
+++ b/arch/um/include/shared/user.h
@@ -16,13 +16,8 @@
*/
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

-/* This is to get size_t and NULL */
-#ifndef __UM_HOST__
#include <linux/types.h>
-#else
#include <stddef.h>
-#include <sys/types.h>
-#endif

extern void panic(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2)));
diff --git a/arch/um/kernel/skas/stub.c b/arch/um/kernel/skas/stub.c
index 67cab46a602c..6c9bb1511ea2 100644
--- a/arch/um/kernel/skas/stub.c
+++ b/arch/um/kernel/skas/stub.c
@@ -5,6 +5,7 @@

#include <sysdep/stub.h>

+#include <linux/init.h>
#include <linux/futex.h>
#include <sys/socket.h>
#include <errno.h>
diff --git a/arch/um/kernel/skas/stub_exe.c b/arch/um/kernel/skas/stub_exe.c
index cbafaa684e66..0563838c01d1 100644
--- a/arch/um/kernel/skas/stub_exe.c
+++ b/arch/um/kernel/skas/stub_exe.c
@@ -4,8 +4,8 @@
#include <asm/unistd.h>
#include <sysdep/stub.h>
#include <stub-data.h>
-#include <linux/filter.h>
-#include <linux/seccomp.h>
+#include <uapi/linux/filter.h>
+#include <uapi/linux/seccomp.h>
#include <generated/asm-offsets.h>

void _start(void);
diff --git a/arch/um/os-Linux/skas/process.c b/arch/um/os-Linux/skas/process.c
index 78f48fa9db8b..8ad7e863af97 100644
--- a/arch/um/os-Linux/skas/process.c
+++ b/arch/um/os-Linux/skas/process.c
@@ -29,9 +29,7 @@
#include <sysdep/stub.h>
#include <sysdep/mcontext.h>
#include <linux/futex.h>
-#include <linux/threads.h>
#include <timetravel.h>
-#include <asm-generic/rwonce.h>
#include "../internal.h"

int is_skas_winch(int pid, int fd, void *data)
@@ -204,7 +202,7 @@ void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys)
* Either way, if PID is negative, then we have no
* choice but to kill the task.
*/
- if (__READ_ONCE(mm_idp->pid) < 0)
+ if (READ_ONCE(mm_idp->pid) < 0)
goto out_kill;

ret = syscall(__NR_futex, &data->futex,
@@ -217,7 +215,7 @@ void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys)
}
} while (data->futex == FUTEX_IN_CHILD);

- if (__READ_ONCE(mm_idp->pid) < 0)
+ if (READ_ONCE(mm_idp->pid) < 0)
goto out_kill;

running = 0;
diff --git a/arch/um/os-Linux/start_up.c b/arch/um/os-Linux/start_up.c
index a827c2e01aa5..8971f4fdddab 100644
--- a/arch/um/os-Linux/start_up.c
+++ b/arch/um/os-Linux/start_up.c
@@ -28,8 +28,8 @@
#include <stdbool.h>
#include <stub-data.h>
#include <sys/prctl.h>
-#include <linux/seccomp.h>
-#include <linux/filter.h>
+#include <uapi/linux/seccomp.h>
+#include <uapi/linux/filter.h>
#include <sysdep/mcontext.h>
#include <sysdep/stub.h>
#include <registers.h>
diff --git a/arch/um/scripts/Makefile.rules b/arch/um/scripts/Makefile.rules
index a8b7d9dab0a6..b4a2e0058503 100644
--- a/arch/um/scripts/Makefile.rules
+++ b/arch/um/scripts/Makefile.rules
@@ -9,7 +9,7 @@ USER_OBJS += $(filter %_user.o,$(obj-y) $(USER_SINGLE_OBJS))
USER_OBJS := $(foreach file,$(USER_OBJS),$(obj)/$(file))

$(USER_OBJS:.o=.%): \
- c_flags = -Wp,-MD,$(depfile) $(USER_CFLAGS) -include $(srctree)/include/linux/kern_levels.h -include user.h $(CFLAGS_$(basetarget).o)
+ c_flags = -Wp,-MD,$(depfile) $(USER_CFLAGS) $(CFLAGS_$(basetarget).o)

# These are like USER_OBJS but filter USER_CFLAGS through unprofile instead of
# using it directly.
diff --git a/arch/x86/um/Makefile b/arch/x86/um/Makefile
index b42c31cd2390..d8a120bace25 100644
--- a/arch/x86/um/Makefile
+++ b/arch/x86/um/Makefile
@@ -38,8 +38,10 @@ subarch-$(CONFIG_MODULES) += ../kernel/module.o

USER_OBJS := bugs_$(BITS).o ptrace_user.o fault.o

-$(obj)/user-offsets.s: c_flags = -Wp,-MD,$(depfile) $(USER_CFLAGS) \
- -Iarch/x86/include/generated
+$(obj)/user-offsets.s: c_flags = -Wp,-MD,$(depfile) $(USER_CFLAGS) \
+ -Iarch/x86/include/generated \
+ -include $(srctree)/include/linux/kbuild.h
+
targets += user-offsets.s

include/generated/user_constants.h: $(obj)/user-offsets.s FORCE
diff --git a/arch/x86/um/user-offsets.c b/arch/x86/um/user-offsets.c
index d6e1cd9956bf..74fc9763b76e 100644
--- a/arch/x86/um/user-offsets.c
+++ b/arch/x86/um/user-offsets.c
@@ -8,7 +8,6 @@
#define __FRAME_OFFSETS
#include <linux/ptrace.h>
#include <asm/types.h>
-#include <linux/kbuild.h>

#define DEFINE_LONGS(sym, val) \
COMMENT(#val " / sizeof(unsigned long)"); \
--
2.51.0