Skip to contentSkip to conversation

External kernel modules

RFC In review Andreas Gruenbacher 2 participants 2 messages Feb 2, 2004 Kbuild

lore
Share & tools
lore
#0 lore raw

RFC 1/1 External kernel modules

Hello,

the kernel build system (kbuild) allows to compile modules whose sources live outside the kernel source tree by setting SUBDIRS as follows (from the module sources's directory):

  $ make -C /lib/modules/$(uname -r)/build modules SUBDIRS=$(pwd)

people use this mechanism to compile kernel modules that are missing in their kernel sources, or have been updated/fixed in the meantime.

Currently there are a number of deficiencies for building such modules:

(1) symbol versioning (MODVERSIONS) is not properly supported, and

(2) only the `modules' make target is supported.

Problem (1) did not exist in the 2.4 kernel series. The current situation has lead authors of several external modules to implement some of the kbuild functionality in their own makefiles, which can only lead to problems later on.

During the kernel compile, a list of all files that export symbols is created in .tmp_versions/ of the kernel source tree. After everything is compiled, the module versions of all these object files are resolved with modpost. When an external module is compiled, it ends up being the only file in .tmp_versions/, and so symbols in other modules would not be found.

An additional problem is that resolving symbol versions requires all module files plus vmlinux to be in the kernel source tree, so modules cannot be build against a clean kernel source tree.

I propose the attached patch: It adds symbol version dump/load functionality to modpost: When compiling the kernel, the module versions are dumped to a file. When compiling external modules, the dumped symbols are first loaded. Also, to allow a read-only kernel source tree and not interfere with the kernel sources, the .tmp_versions/ directory is placed in the external module's directory.

Furthermore, the patch adds clean/distclean/mrproper make targets with reasonable semantics for external modules, and also adds a modules_add target that installs the external modules into /lib/modules/$(KERNELRELEASE). (The modules_install target could be used, but it has different semantics, so I have used a different name instead.)

There also is one fix for using tempfiles instead of writing into the kernel source tree. A few problems with compiling against a read-only kernel source tree seem to remain.

Regards,

Signature · 2 lines
Andreas Gruenbacher <[email protected]>
SUSE Labs, SUSE LINUX AG
Makefile +34 -12
Index: linux-2.6.1/Makefile
===================================================================
@@ -289,7 +289,10 @@ export CPPFLAGS NOSTDINC_FLAGS OBJCOPYFL
 export CFLAGS CFLAGS_KERNEL CFLAGS_MODULE 
 export AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 
-export MODVERDIR := .tmp_versions
+# When compiling out-of-tree modules, put MODVERDIR in the module source
+# tree rather than in the kernel source tree. The kernel source tree might
+# even be read-only.
+export MODVERDIR := $(patsubst %,%/,$(firstword $(filter ../% /%,$(SUBDIRS)))).tmp_versions
 
 # The temporary file to save gcc -MD generated dependencies must not
 # contain a comma
@@ -748,6 +751,9 @@ endif
 _modinst_post: _modinst_
 	if [ -r System.map ]; then $(DEPMOD) -ae -F System.map $(depmod_opts) $(KERNELRELEASE); fi
 
+modules_add:
+	$(Q)$(MAKE) -rR -f $(srctree)/scripts/Makefile.modinst
+
 else # CONFIG_MODULES
 
 # Modules not configured
@@ -819,11 +825,28 @@ clean-dirs += $(addprefix _clean_,$(ALL_
 $(clean-dirs):
 	$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
 
+.PHONY: _clean _mrproper
+ifeq ($(filter ../% /%,$(SUBDIRS)),)
+
+clean_subdirs := .
+clean: _clean archclean $(clean-dirs)
+	$(call cmd,rmclean)
+mrproper distclean: clean _mrproper archmrproper
+	$(call cmd,mrproper)
+
+else
+
+clean_subdirs := $(filter ../% /%,$(SUBDIRS))
+clean: _clean
+mrproper distclean: _mrproper
+	$(Q)rm -rf $(MODVERDIR)
+
+endif
+
 quiet_cmd_rmclean = RM  $$(CLEAN_FILES)
 cmd_rmclean	  = rm -f $(CLEAN_FILES)
-clean: archclean $(clean-dirs)
-	$(call cmd,rmclean)
-	@find . $(RCS_FIND_IGNORE) \
+_clean:
+	@find $(clean_subdirs) $(RCS_FIND_IGNORE) \
 	 	\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \) \
 		-type f -print | xargs rm -f
@@ -832,15 +855,13 @@ clean: archclean $(clean-dirs)
 #
 quiet_cmd_mrproper = RM  $$(MRPROPER_DIRS) + $$(MRPROPER_FILES)
 cmd_mrproper = rm -rf $(MRPROPER_DIRS) && rm -f $(MRPROPER_FILES)
-mrproper distclean: clean archmrproper
-	@echo '  Making $@ in the srctree'
-	@find . $(RCS_FIND_IGNORE) \
+_mrproper:
+	@find $(clean_subdirs) $(RCS_FIND_IGNORE) \
 	 	\( -name '*.orig' -o -name '*.rej' -o -name '*~' \
 		-o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
 	 	-o -name '.*.rej' -o -size 0 \
 		-o -name '*%' -o -name '.*.cmd' -o -name 'core' \) \
 		-type f -print | xargs rm -f
-	$(call cmd,mrproper)
 
 # Generate tags for editors
 # ---------------------------------------------------------------------------
@@ -1045,12 +1066,13 @@ define filechk
 	@set -e;				\
 	echo '  CHK     $@';			\
 	mkdir -p $(dir $@);			\
-	$(filechk_$(1)) < $< > [email protected];		\
-	if [ -r $@ ] && cmp -s $@ [email protected]; then	\
-		rm -f [email protected];			\
+	tmp=$$(/bin/mktemp /tmp/kbuild.XXXXXX);	\
+	$(filechk_$(1)) < $< > $$tmp;		\
+	if [ -r $@ ] && cmp -s $@ $$tmp; then	\
+		rm -f $$tmp;			\
 	else					\
 		echo '  UPD     $@';		\
-		mv -f [email protected] $@;		\
+		mv -f $$tmp $@;		\
 	fi
 endef
 
scripts/modpost.c +109 -9

168 lines not on this page · raw message

scripts/modpost.h +1 -0
Index: linux-2.6.1/scripts/modpost.h
===================================================================
@@ -70,6 +70,7 @@ struct module {
 	const char *name;
 	struct symbol *unres;
 	int seen;
+	int skip;
 	struct buffer dev_table_buf;
 };
 
scripts/Makefile.modinst +6 -4
Index: linux-2.6.1/scripts/Makefile.modinst
===================================================================
@@ -29,11 +29,13 @@ quiet_cmd_modules_install = INSTALL $@
 $(filter-out ../% /%,$(modules)):
 	$(call cmd,modules_install)
 
-# Modules built outside just go into extra
+# Modules built outside go into extra by default
 
-quiet_cmd_modules_install_extra = INSTALL $(obj-m:.o=.ko)
-      cmd_modules_install_extra = mkdir -p $(MODLIB)/extra; \
-			    	  cp $@ $(MODLIB)/extra
+MOD_DIR ?= extra
+
+quiet_cmd_modules_install_extra = INSTALL $(@F)
+      cmd_modules_install_extra = mkdir -p $(MODLIB)/$(MOD_DIR); \
+			    	  cp $@ $(MODLIB)/$(MOD_DIR)
 
 $(filter     ../% /%,$(modules)):
 	$(call cmd,modules_install_extra)
scripts/Makefile.modpost +12 -2
Index: linux-2.6.1/scripts/Makefile.modpost
===================================================================
@@ -51,11 +51,21 @@ $(modules:.ko=.mod.c): __modpost ;
 
 # Extract all checksums for all exported symbols
 
-quiet_cmd_modpost = MODPOST
-      cmd_modpost = scripts/modpost $(filter-out FORCE,$^)
+# Are we building out-of-tree modules?
 
+ifeq ($(filter ../% /%,$(SUBDIRS)),)
+modver_file := modversions
+quiet_cmd_modpost = MODPOST
+      cmd_modpost = scripts/modpost $(filter-out FORCE,$^) -o $(modver_file)
 __modpost: $(wildcard vmlinux) $(modules:.ko=.o) FORCE
 	$(call if_changed,modpost)
+else
+modver_file := $(wildcard modversions)
+quiet_cmd_modpost = MODPOST
+      cmd_modpost = scripts/modpost $^ $(if $(modver_file),-i $(modver_file))
+__modpost: $(modules:.ko=.o)
+	$(call cmd,modpost)
+endif
 
 targets += __modpost
 
#1 lore raw
Quoted 7 lines · Andreas Gruenbacher

I propose the attached patch: It adds symbol version dump/load functionality to modpost: When compiling the kernel, the module versions are dumped to a file. When compiling external modules, the dumped symbols are first loaded. Also, to allow a read-only kernel source tree and not interfere with the kernel sources, the .tmp_versions/ directory is placed in the external module's directory.

Quoted 6 lines

Furthermore, the patch adds clean/distclean/mrproper make targets with reasonable semantics for external modules, and also adds a modules_add target that installs the external modules into /lib/modules/$(KERNELRELEASE). (The modules_install target could be used, but it has different semantics, so I have used a different name instead.)

Thanks, I will take a closer look during the week.
Did you consider using the make O=dir support, so .tmp_versions were created in the output directory?

Sam