5

Trying the simplest kernel module from LDD3 without any modification on custom built kernel v4.1.0-rc6 for Beagle Bone board with BusyBox v1.23.0. The code for the module is as follows:

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

The Makefile is:

ARCH := arm
CROSS_COMPILE := arm-cortex_a8-linux-gnueabi-
obj-m := hello.o
all:
        make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C /path/to/linux/source/tree M=$(PWD) modules
clean:
        make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C /path/to/linux/source/tree M=$(PWD) clean

The module is compiling and installing on the rootfs just fine. also it is loading:

$ insmod hello.ko 
[   30.692404] Hello, world

But when trying to remove it, I am getting:

$ rmmod hello.ko 
Segmentation fault
$modprobe -r hello.ko 
Segmentation fault
$ lsmod
hello 813 0 - Live 0xbf000000 (O)

The kernel is compiled with module unloading (both regular and forced) support enabled.

What can be the possible cause of this issue? What is the way to approach the investigation of it?

Update:

As suggested in the comments I have tried including linux/kernel.h, defining the MODULE,LINUX and __KERNEL__ symbols. Added the__init and __exit prefixes to the functions. Removed the static modifiers. Removed the printk lines. The result the same. dmesg shows only the initial greeting. Loading and unloading of the kernel's modules such a gpio_keys or crypto/ccm is working, surprisingly. So the only thing remaining to suspect is the way the module is compiled..

Update 2
Updating the kernel to the freshest snapshot didn't help. Compiled the module with different optimization settings didn't help. Next step, I guess, I am going to modify the BusyBox's rmmod to have some indication of the problem location..

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • The code looks fine. Do you see anymore info in /var/log/messages (or dmesg) when removing it? Does it happen with your module only and can you load/unload any system modules successfully? – P.P Jun 11 '15 at 12:45
  • @BlueMoon Unfortunately, I don't even have the `/var` directory right now on my rootfs :) I am building it from scratch, and I am not sure which additional service should be installed in order to write the messages.. Maybe I should start by doing so. – Eugene Sh. Jun 11 '15 at 12:50
  • Can you load/unload others, for example `modprobe nfs` and `modprobe -r nfs` successfully? `dmesg` says anything useful? – P.P Jun 11 '15 at 12:54
  • @Blue Moon I will take a look at these later today, have to go to work now. I know the modules are loading. Not sure about unloading. Is `dmesg` available on bare bones system with busybox only? Anyway, thanks for the direction. – Eugene Sh. Jun 11 '15 at 13:02
  • Just run `dmesg` on the commandline to find out. – P.P Jun 11 '15 at 13:04
  • I will once back to my lab. Thanks – Eugene Sh. Jun 11 '15 at 13:05
  • for grins, add #include – Les Jun 11 '15 at 13:05
  • @BlueMoon Please see the update. It is really weird. – Eugene Sh. Jun 12 '15 at 01:46

2 Answers2

0

Have a look at these tutorials:

http://www.tldp.org/HOWTO/Module-HOWTO/x839.html http://www.tldp.org/LDP/lkmpg/2.4/html/x281.htm

Try adding:

#define MODULE
#define LINUX
#define __KERNEL__

#include <linux/kernel.h>      /* Needed for KERN_ALERT */
corro
  • 343
  • 3
  • 15
0

I have managed to work around this problem. Using strace I've found out that the segfault is occurring somewhere when reading the BusyBox specific modules.dep.bb file. This file is used by BusyBox when it is compiled with the "Simplified modutils" option (CONFIG_MODPROBE_SMALL). By disabling the option, selecting the utils to be installed and rebuilding BusybBox I've got the module unloading work. I believe the problem root is near the fact the test module is compiled and stored outside the /lib/..../modules directory, so busybox with the simplified modutils is just getting confused.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61