Kernel Module with multiple source files not initializing
Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used *nm* to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output. U __alloc_skb 00000000000001c0 T create_socket 0000000000000030 T data_update U __fentry__ U fortify_panic 0000000000000000 r __func__.5058 U init_net 0000000000000000 t kernel_space_receiver 0000000000000080 T kernel_space_sender U memcpy 0000000000000038 r __module_depends 0000000000000008 D msg_size U msleep U __netlink_kernel_create U netlink_kernel_release U netlink_unicast 0000000000000078 B nlh U __nlmsg_put 0000000000000080 B nl_sk 0000000000000000 r _note_6 0000000000000004 D pid U printk 0000000000000000 D res 0000000000000000 B s_data 0000000000000070 B skb_out U __stack_chk_fail U strlen 0000000000000000 D __this_module 0000000000000000 r __UNIQUE_ID_license381 000000000000004d r __UNIQUE_ID_name240 0000000000000041 r __UNIQUE_ID_retpoline241 0000000000000010 r __UNIQUE_ID_srcversion242 0000000000000068 r __UNIQUE_ID_vermagic239 -------------------- *Following is the output of some useful commands:* ------------------------------------------------- *ssd@ssd:~/Desktop/Kernel_User_comm$ ls* Makefile netlink_kernel_module.c netlink_kernel_space.c netlink_kernel_space.h netlink_user_module.c --------------------------------------------------- *ssd@ssd:~/Kernel_User_comm$ make --version* GNU Make 4.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. ------------------------------------------------------- *ssd@ssd:~/Kernel_User_comm$ uname -r* 5.2.0-rc6 -------------------------------------------------------- Can you please help me: what's the problem/mistake I am doing? Thank you very much. -- *Best Regards,* *Mr. Irfan Ullah* PhD Candidate Data and Knowledge Engineering(DKE) Lab Department of Computer Science and Engineering Kyung Hee University, South Korea. +82-010-591-51651 <+82%2010-3877-8867> sahibzada.iu@gmail.com sahibzada_irfanullah
On Fri, Oct 25, 2019 at 2:16 AM Irfan Ullah (울라 이르판) <irfan@dke.khu.ac.kr> wrote:
Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used *nm* to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output.
I assume you have verified that your module is loaded by something like lsmod | grep your_module_name and log level allows KERN_ALERT printing.
Thanks you. Yes, I have tested it. " smod | grep your_module_name" command shows that the module is loaded but the "dmesg" shows nothing. Following is the output of the commands. *ssd@ssd:~/Desktop/Kernel_User_comm$ lsmod | grep netlink_kernel_module* netlink_kernel_module 16384 0 *ssd@ssd:~/Desktop/Kernel_User_comm$ dmesgssd@ssd:~/Desktop/Kernel_User_comm$ * On Sat, Oct 26, 2019 at 1:16 AM Abu Rasheda <rcpilot2010@gmail.com> wrote:
On Fri, Oct 25, 2019 at 2:16 AM Irfan Ullah (울라 이르판) <irfan@dke.khu.ac.kr> wrote:
Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used *nm* to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output.
I assume you have verified that your module is loaded by something like
lsmod | grep your_module_name
and log level allows KERN_ALERT printing.
-- *Best Regards,* *Mr. Irfan Ullah* PhD Candidate Data and Knowledge Engineering(DKE) Lab Department of Computer Science and Engineering Kyung Hee University, South Korea. +82-010-591-51651 <+82%2010-3877-8867> sahibzada.iu@gmail.com sahibzada_irfanullah
Hi all! On 25/10/2019 11:14, Irfan Ullah (울라 이르판) wrote:
Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>;, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used /nm/ to inspect the module, but, as expected, there was no> signs of "__init and __exit" functions can in the output.
To be exact, you are looking for the hello_init function. Since "nm" doesn't show it, it's either not there or "static". [ for the nm-output see in the OPs mail ]
Can you please help me: what's the problem/mistake I am doing? Thank you very much.
Get rid of the "static" for the hello_init and hello_exit function. MfG, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
Thanks for the help. I have removed the "static" from the hello_init, hello_exti but nothing worked. I am not sure, but I think the problem is with the make file. On Sun, Oct 27, 2019 at 5:44 PM Bernd Petrovitsch <bernd@petrovitsch.priv.at> wrote:
Hi all!
On 25/10/2019 11:14, Irfan Ullah (울라 이르판) wrote:
Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 < https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-... ;, and link3 < https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc.
but nothings worked out. I also used /nm/ to inspect the module, but, as expected, there was no> signs of "__init and __exit" functions can in the output.
To be exact, you are looking for the hello_init function. Since "nm" doesn't show it, it's either not there or "static".
[ for the nm-output see in the OPs mail ]
Can you please help me: what's the problem/mistake I am doing? Thank you very much.
Get rid of the "static" for the hello_init and hello_exit function.
MfG, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
-- *Best Regards,* *Mr. Irfan Ullah* PhD Candidate Data and Knowledge Engineering(DKE) Lab Department of Computer Science and Engineering Kyung Hee University, South Korea. +82-010-591-51651 <+82%2010-3877-8867> sahibzada.iu@gmail.com sahibzada_irfanullah
On 28/10/2019 10:43, Irfan Ullah (울라 이르판) wrote:
Thanks for the help. I have removed the "static" from the hello_init, hello_exti but nothing worked. I am not sure, but I think the problem is with the make file.
OK, when I add "static" to my driver, it works too. Sry for the confusion. I have also __init with the init function and __exit with the exit function. MfG, Bernd -- "I dislike type abstraction if it has no real reason. And saving on typing is not a good reason - if your typing speed is the main issue when you're coding, you're doing something seriously wrong." - Linus Torvalds
my problem still not solved. Thanks for your help. On Mon, Oct 28, 2019 at 7:04 PM Bernd Petrovitsch <bernd@petrovitsch.priv.at> wrote:
On 28/10/2019 10:43, Irfan Ullah (울라 이르판) wrote:
Thanks for the help. I have removed the "static" from the hello_init, hello_exti but nothing worked. I am not sure, but I think the problem is with the make file.
OK, when I add "static" to my driver, it works too. Sry for the confusion.
I have also __init with the init function and __exit with the exit function.
MfG, Bernd -- "I dislike type abstraction if it has no real reason. And saving on typing is not a good reason - if your typing speed is the main issue when you're coding, you're doing something seriously wrong." - Linus Torvalds
-- *Best Regards,* *Mr. Irfan Ullah* PhD Candidate Data and Knowledge Engineering(DKE) Lab Department of Computer Science and Engineering Kyung Hee University, South Korea. +82-010-591-51651 <+82%2010-3877-8867> sahibzada.iu@gmail.com sahibzada_irfanullah
On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) <irfan@dke.khu.ac.kr> wrote: Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling >> and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used *nm* to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output.
When you take a careful look at make's output we see that netlink_kernel_module.c is not being compiled. See below: aruna@debian:~/Downloads/kmod6$ make make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/Downloads/kmod6 modules make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64' make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64' CC [M] /home/aruna/Downloads/kmod6/netlink_kernel_space.o LD [M] /home/aruna/Downloads/kmod6/netlink_kernel_module.o Building modules, stage 2. MODPOST 1 modules CC /home/aruna/Downloads/kmod6/netlink_kernel_module.mod.o LD [M] /home/aruna/Downloads/kmod6/netlink_kernel_module.ko make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64' This is why nm does not show hello_init or hello_exit.
Can you please help me: what's the problem/mistake I am doing?
Change your Makefile so the module name is not the same as the C source file. Let's say we want the module to be named aruna.ko ( make up any name different to the C source file) obj-m := aruna.o aruna-objs := netlink_kernel_module.o netlink_kernel_space.o and now make shows: make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/kmod6/Kernel_User_comm modules make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64' make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64' CC [M] /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_module.o // <-- THIS TIME IT COMPILES ! CC [M] /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_space.o LD [M] /home/aruna/kmod6/Kernel_User_comm/aruna.o Building modules, stage 2. MODPOST 1 modules CC /home/aruna/kmod6/Kernel_User_comm/aruna.mod.o LD [M] /home/aruna/kmod6/Kernel_User_comm/aruna.ko make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64' and now nm aruna.ko shows: aruna@debian:~/kmod6/Kernel_User_comm$ nm aruna.ko 0000000000000020 T cleanup_module 0000000000000080 T create_socket 0000000000000040 T data_update U __fentry__ 0000000000000020 t hello_exit // WE HAVE hello_exit 0000000000000000 t hello_init // WE HAVE hello_init 0000000000000000 T init_module 0000000000000070 T kernel_space_receiver 0000000000000050 T kernel_space_sender 0000000000000053 r __module_depends 0000000000000004 D pid U printk 0000000000000000 D res 0000000000000000 D __this_module 0000000000000000 r __UNIQUE_ID_author2 0000000000000013 r __UNIQUE_ID_description1 0000000000000047 r __UNIQUE_ID_license0 000000000000005c r __UNIQUE_ID_vermagic0 0000000000000000 r ____versions To get make to do this smoothly you will have to fix the multiple definition and other errors I encountered along the way. And something's in your header file really should belong in a C file :) heed Valdis's advice. As a learning experience try this Makefile: ------------------------------------------------------------------------------------------------------ obj-m := aruna.o aruna-objs := netlink_kernel_module.o netlink_kernel_space.o SHELL += -x all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ------------------------------------------------------------------------------------------------------ One simple way to enhance the output of GNU Make is to redefine SHELL. SHELL is a GNU Make built-in variable that contains the name of the shell to use when GNU Make executes commands. Reference: https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make Since most shells have a -x option that causes them to print out each command they are about to execute modifying SHELL in a Makefile by appendin -x causes every command to be printed (usually preceded by +) as the Makefile is run. Try: make clean the make and go through the output can be an enlightening experience. Ah the joys of building a kernel module that has more than one source file :-)-- Good luck - Aruna
Thanks a lot. it is working now. On Wed, Nov 6, 2019 at 1:52 AM Aruna Hewapathirane < aruna.hewapathirane@gmail.com> wrote:
On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) < irfan@dke.khu.ac.kr> wrote: Dear All, I have developed a kernel module consists of one source file that sends and receives message to the user space. I have spitted the source code in two files, and now I am trying to develop kernel module from these source files. After compiling >> and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file. I tried many things , e.g., link <https://paguilar.org/?p=7>1, link2 <https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-...>, and link3 <https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-w...> etc. but nothings worked out. I also used *nm* to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output.
When you take a careful look at make's output we see that netlink_kernel_module.c is not being compiled. See below:
aruna@debian:~/Downloads/kmod6$ make make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/Downloads/kmod6 modules make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64' make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64' CC [M] /home/aruna/Downloads/kmod6/netlink_kernel_space.o LD [M] /home/aruna/Downloads/kmod6/netlink_kernel_module.o Building modules, stage 2. MODPOST 1 modules CC /home/aruna/Downloads/kmod6/netlink_kernel_module.mod.o LD [M] /home/aruna/Downloads/kmod6/netlink_kernel_module.ko make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'
This is why nm does not show hello_init or hello_exit.
Can you please help me: what's the problem/mistake I am doing?
Change your Makefile so the module name is not the same as the C source file. Let's say we want the module to be named aruna.ko ( make up any name different to the C source file)
obj-m := aruna.o aruna-objs := netlink_kernel_module.o netlink_kernel_space.o
and now make shows:
make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/kmod6/Kernel_User_comm modules make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64' make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64' CC [M] /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_module.o // <-- THIS TIME IT COMPILES ! CC [M] /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_space.o LD [M] /home/aruna/kmod6/Kernel_User_comm/aruna.o Building modules, stage 2. MODPOST 1 modules CC /home/aruna/kmod6/Kernel_User_comm/aruna.mod.o LD [M] /home/aruna/kmod6/Kernel_User_comm/aruna.ko make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'
and now nm aruna.ko shows: aruna@debian:~/kmod6/Kernel_User_comm$ nm aruna.ko 0000000000000020 T cleanup_module 0000000000000080 T create_socket 0000000000000040 T data_update U __fentry__ 0000000000000020 t hello_exit // WE HAVE hello_exit 0000000000000000 t hello_init // WE HAVE hello_init 0000000000000000 T init_module 0000000000000070 T kernel_space_receiver 0000000000000050 T kernel_space_sender 0000000000000053 r __module_depends 0000000000000004 D pid U printk 0000000000000000 D res 0000000000000000 D __this_module 0000000000000000 r __UNIQUE_ID_author2 0000000000000013 r __UNIQUE_ID_description1 0000000000000047 r __UNIQUE_ID_license0 000000000000005c r __UNIQUE_ID_vermagic0 0000000000000000 r ____versions
To get make to do this smoothly you will have to fix the multiple definition and other errors I encountered along the way. And something's in your header file really should belong in a C file :) heed Valdis's advice.
As a learning experience try this Makefile:
------------------------------------------------------------------------------------------------------ obj-m := aruna.o aruna-objs := netlink_kernel_module.o netlink_kernel_space.o
SHELL += -x
all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
------------------------------------------------------------------------------------------------------
One simple way to enhance the output of GNU Make is to redefine SHELL. SHELL is a GNU Make built-in variable that contains the name of the shell to use when GNU Make executes commands.
Reference: https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make
Since most shells have a -x option that causes them to print out each command they are about to execute modifying SHELL in a Makefile by appendin -x causes every command to be printed (usually preceded by +) as the Makefile is run.
Try: make clean the make and go through the output can be an enlightening experience.
Ah the joys of building a kernel module that has more than one source file :-)--
Good luck - Aruna
-- *Best Regards,* *Mr. Irfan Ullah* PhD Candidate Data and Knowledge Engineering(DKE) Lab Department of Computer Science and Engineering Kyung Hee University, South Korea. +82-010-591-51651 <+82%2010-3877-8867> sahibzada.iu@gmail.com sahibzada_irfanullah
participants (4)
-
Abu Rasheda -
Aruna Hewapathirane -
Bernd Petrovitsch -
Irfan Ullah (울라 이르판)