Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
From: Aruna Hewapathirane <aruna.hewapathirane@gmail.com>
To: "Irfan Ullah (울라 이르판)" <irfan@dke.khu.ac.kr>
Cc: kernelnewbies <kernelnewbies@kernelnewbies.org>
Subject: Re: Kernel Module with multiple source files not initializing
Date: Tue, 5 Nov 2019 11:52:07 -0500
Message-ID: <CAFSeFg_iRUdjUY1cjqVtmJw=TBYKvZcPAC3auSwP9=L9DiX35w@mail.gmail.com> (raw)
In-Reply-To: <CA+mB8Oz1Hu82nk2vbinhpOQW8uasp7_b8CFNf2jNovqP==OH6A@mail.gmail.com>

[-- Attachment #1.1: Type: text/plain, Size: 5189 bytes --]

>> 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-multiple-source-files-not-initializing-886190/>,
and link3
<https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-wear/Documentation/kbuild/modules.txt>
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

[-- Attachment #1.2: Type: text/html, Size: 6284 bytes --]

<div dir="ltr"><div dir="ltr" class="gmail_attr">&gt;&gt; On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) &lt;<a href="mailto:irfan@dke.khu.ac.kr">irfan@dke.khu.ac.kr</a>&gt; wrote:</div><div dir="ltr">&gt;&gt; Dear All,<div>&gt;&gt; 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 &gt;&gt; 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.</div><div>&gt;&gt; I tried many things , e.g.,  <a href="https://paguilar.org/?p=7" target="_blank">link</a>1, <a href="https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-multiple-source-files-not-initializing-886190/" target="_blank">link2</a>, and <a href="https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-wear/Documentation/kbuild/modules.txt" target="_blank">link3</a> etc.  but nothings worked out. </div><div>&gt;&gt; I also used <i>nm</i> to inspect the module, but, as expected, there was no signs of &quot;__init and __exit&quot; functions can in the output. <br></div><div><br></div><div>When you take a careful look at make&#39;s output we see that netlink_kernel_module.c <br>is not being compiled. See below:<br></div><div><br>aruna@debian:~/Downloads/kmod6$ make<br>make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/Downloads/kmod6 modules<br>make[1]: Entering directory &#39;/usr/src/linux-headers-3.16.0-4-amd64&#39;<br>make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64&#39;<br>  CC [M]  /home/aruna/Downloads/kmod6/netlink_kernel_space.o <br>  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.o<br>  Building modules, stage 2.<br>  MODPOST 1 modules<br>  CC      /home/aruna/Downloads/kmod6/netlink_kernel_module.mod.o<br>  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.ko<br>make[1]: Leaving directory &#39;/usr/src/linux-headers-3.16.0-4-amd64&#39;</div><div><br></div><div>This is why nm does not show hello_init or hello_exit.</div><br><div>&gt;&gt; Can you please help me: what&#39;s the problem/mistake I am doing?</div><div><br></div><div>Change your Makefile so the module name is not the same as the C source file. Let&#39;s say</div><div>we want the module to be named aruna.ko ( make up any name different to the C source file)<br></div><div><br>obj-m := aruna.o<br>aruna-objs := netlink_kernel_module.o netlink_kernel_space.o <br></div><div><br></div><div>and now make shows:<br><br></div><div>make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/kmod6/Kernel_User_comm modules<br>make[1]: Entering directory &#39;/usr/src/linux-headers-3.16.0-4-amd64&#39;<br>make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64&#39;<br>  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_module.o  //   &lt;-- THIS TIME IT COMPILES !<br>  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_space.o<br>  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.o<br>  Building modules, stage 2.<br>  MODPOST 1 modules<br>  CC      /home/aruna/kmod6/Kernel_User_comm/aruna.mod.o<br>  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.ko<br>make[1]: Leaving directory &#39;/usr/src/linux-headers-3.16.0-4-amd64&#39;<br></div><div><br></div><div>and now nm aruna.ko shows:</div>aruna@debian:~/kmod6/Kernel_User_comm$ nm aruna.ko<br>0000000000000020 T cleanup_module<br>0000000000000080 T create_socket<br>0000000000000040 T data_update<br>                 U __fentry__<br>0000000000000020 t hello_exit               // WE HAVE hello_exit<br>0000000000000000 t hello_init                // WE HAVE hello_init<br>0000000000000000 T init_module<br>0000000000000070 T kernel_space_receiver<br>0000000000000050 T kernel_space_sender<br>0000000000000053 r __module_depends<br>0000000000000004 D pid<br>                 U printk<br>0000000000000000 D res<br>0000000000000000 D __this_module<br>0000000000000000 r __UNIQUE_ID_author2<br>0000000000000013 r __UNIQUE_ID_description1<br>0000000000000047 r __UNIQUE_ID_license0<br>000000000000005c r __UNIQUE_ID_vermagic0<br>0000000000000000 r ____versions<br><div><br></div><div>To get make to do this smoothly you will have to fix the multiple definition and</div><div>other errors I encountered along the way. And something&#39;s in your header<br></div><div>file really should belong in a C file :) heed Valdis&#39;s advice.<br></div><br><div>As a learning experience try this Makefile:</div><div>------------------------------------------------------------------------------------------------------<br>obj-m := aruna.o<br>aruna-objs := netlink_kernel_module.o netlink_kernel_space.o <br><br>SHELL += -x<br><br>all:<br>        	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules<br>clean:<br>        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean<br></div><div>------------------------------------------------------------------------------------------------------</div><div><br></div><div>One simple way to enhance the output of GNU Make is to redefine SHELL. 
<br>SHELL is a GNU Make built-in variable that contains the name of the 
shell to use when <br></div><div>GNU Make executes commands.<br></div><div><br></div><div>Reference: <a href="https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make">https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make</a><br><br></div><div>Since most shells 
have a -x option that causes them to print out each command they <br></div><div>are 
about to execute modifying SHELL in a Makefile by appendin -x causes 
every command <br></div><div>to be printed (usually preceded by +) as the Makefile is 
run.</div><div><br></div><div>Try:</div><div>make clean the make and go through the output can be an enlightening experience.<br><br></div><div>Ah the joys of building a kernel module that has more than one source file :-)-- <br></div></div><div dir="ltr"><br></div><div>Good luck - Aruna</div><br></div>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  parent reply index

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-25  9:14 Irfan Ullah (울라 이르판)
2019-10-25 16:15 ` Abu Rasheda
2019-10-27  7:11   ` Irfan Ullah (울라 이르판)
2019-10-27  8:43 ` Bernd Petrovitsch
2019-10-28  9:43   ` Irfan Ullah (울라 이르판)
2019-10-28 10:04     ` Bernd Petrovitsch
2019-10-29  8:30       ` Irfan Ullah (울라 이르판)
2019-11-05 16:52 ` Aruna Hewapathirane [this message]
2019-11-07  1:50   ` Irfan Ullah (울라 이르판)

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFSeFg_iRUdjUY1cjqVtmJw=TBYKvZcPAC3auSwP9=L9DiX35w@mail.gmail.com' \
    --to=aruna.hewapathirane@gmail.com \
    --cc=irfan@dke.khu.ac.kr \
    --cc=kernelnewbies@kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git