Skip to contentSkip to conversation

hello-world: some cleanups

PATCH In review Ross Burton 2 participants 4 messages Nov 24, 2025 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hello-world: some cleanups

No need to include linux/init.h as module.h does this.

Set the author to a real name for clarity.

Print the filename of the module to verify that path remapping is working if specified.

Fix a typo in the comments.

---
 hello-world.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hello-world.c +3 -4
index 8ca2dc6..9c22cde 100644
@@ -7,27 +7,26 @@
  *
  *
  */
-#include <linux/init.h>
 #include <linux/module.h>
 
 
 /* See module.h for additional #defines you may want to customize*/
 MODULE_LICENSE("Dual MIT/GPL"); /* See module.h for acceptable options */
-MODULE_AUTHOR("Homer Simpson");
+MODULE_AUTHOR("Mark Asselstine");
 MODULE_DESCRIPTION("A hello-world kernel module");
 MODULE_VERSION("0.1");
 
 /* Initialize the module, called when first loaded */
 static int __init hello_world_init(void) {
   /* print "Hello World" to the dmesg on module load */
-  printk(KERN_INFO "Hello World\n");
+  printk(KERN_INFO "Hello World from " __FILE__ "\n");
 
   return 0;
 }
 
 /* Last call before module unload, complete any cleanup */
 static void __exit hello_world_exit(void) {
-  /* print "Goodby World" to the dmesg on module unload */
+  /* print "Goodbye World" to the dmesg on module unload */
   printk(KERN_INFO "Goodbye World\n");
 }
 
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 Makefile: cleanup, use KERNEL_SRC

Use KERNEL_SRC to specify the location of the kernel source tree, to match module.bbclass in OpenEmbedded Core.

Also add a modules_install target.

---
 Makefile | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
Makefile +8 -7
index e357001..b6fa870 100644
@@ -1,11 +1,12 @@
-KERNELRELEASE	?= `uname -r`
-KERNEL_DIR	?= /lib/modules/$(KERNELRELEASE)/build
-PWD		:= $(shell pwd)
-obj-m		:= hello-world.o
+obj-m      := hello-world.o
+KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build
+SRC        := $(shell pwd)
 
 all:
-	@echo "Building hello-world kernel module..."
-	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
+	$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules
+
+modules_install:
+	$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install
 
 clean:
-	make -C $(KERNEL_DIR) M=$(PWD) clean
+	make -C $(KERNEL_SRC) M=$(PWD) clean
Signature · 1 line
2.43.0
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH kernel-module-hello-world 2/2] Makefile: cleanup, use KERNEL_SRC

Quoted 38 lines · Ross Burton

Use KERNEL_SRC to specify the location of the kernel source tree, to match module.bbclass in OpenEmbedded Core.

Also add a modules_install target.

---
 Makefile | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
Makefile +8 -8
index e357001..b6fa870 100644
@@ -1,11 +1,12 @@
-KERNELRELEASE  ?= `uname -r`
-KERNEL_DIR     ?= /lib/modules/$(KERNELRELEASE)/build
-PWD            := $(shell pwd)
-obj-m          := hello-world.o
+obj-m      := hello-world.o
+KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build
+SRC        := $(shell pwd)
 
 all:
-       @echo "Building hello-world kernel module..."
-       $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
+       $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules
+
+modules_install:
+       $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install
 
 clean:
-       make -C $(KERNEL_DIR) M=$(PWD) clean
+       make -C $(KERNEL_SRC) M=$(PWD) clean
--

2.43.0

Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await thee
at its end
- "Use the force Harry" - Gandalf, Star Trek II
Acked-by on v1 1/2
#3 lore raw
In reply to Ross Burton
Quoted 53 lines · Ross Burton

No need to include linux/init.h as module.h does this.

Set the author to a real name for clarity.

Print the filename of the module to verify that path remapping is working if specified.

Fix a typo in the comments.

---
 hello-world.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hello-world.c +3 -5
index 8ca2dc6..9c22cde 100644
@@ -7,27 +7,26 @@
  *
  *
  */
-#include <linux/init.h>
 #include <linux/module.h>
 
 
 /* See module.h for additional #defines you may want to customize*/
 MODULE_LICENSE("Dual MIT/GPL"); /* See module.h for acceptable options */
-MODULE_AUTHOR("Homer Simpson");
+MODULE_AUTHOR("Mark Asselstine");
 MODULE_DESCRIPTION("A hello-world kernel module");
 MODULE_VERSION("0.1");
 
 /* Initialize the module, called when first loaded */
 static int __init hello_world_init(void) {
   /* print "Hello World" to the dmesg on module load */
-  printk(KERN_INFO "Hello World\n");
+  printk(KERN_INFO "Hello World from " __FILE__ "\n");
 
   return 0;
 }
 
 /* Last call before module unload, complete any cleanup */
 static void __exit hello_world_exit(void) {
-  /* print "Goodby World" to the dmesg on module unload */
+  /* print "Goodbye World" to the dmesg on module unload */
   printk(KERN_INFO "Goodbye World\n");
 }
 
--

2.43.0

Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await thee
at its end
- "Use the force Harry" - Gandalf, Star Trek II