Kernel module is removed successfully although warning message 'not found" - c

Yesterday I installed a very simple kernel module in my embedded Linux system. "hello.ko".
When I install this module to our system, it's normally:
#insmod hello.ko
Initing kernel module
#
#lsmod
hello 638 0 - Live 0xf628f000
But when I remove this kernel module, It can be removed normally but rmmod notice 'hello.ko' not found
# rmmod hello
Cleaning kernel module
rmmod: module 'hello' not found
#lsmod <------------------- already be removed
#
# uname -a
# Linux SWITCH 2.6.32 #8 Thu Jun 23 20:34:48 KST 2016 ppc GNU/Linux
It's very strange. Could anyone explain for me why 'hello.ko' is removed but there's log 'rmmod module not found'?
The following are source code of hello.ko
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/config.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/string.h>
int init_module(void)
{
printk (KERN_EMERG "Initing kernel module\n");
return 0;
}
void cleanup_module(void)
{
printk (KERN_EMERG "Cleaning kernel module\n");
}
MODULE_DESCRIPTION ("Kernel function replacement module");
MODULE_AUTHOR ("DASAN Networks Inc.");
MODULE_LICENSE ("GPL");

You need to add few things in my opinion __init and __exit macros and module_init and module_exit macros, try with:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/string.h>
static int __init init_example(void)
{
printk (KERN_EMERG "Initing kernel module\n");
return 0;
}
static void __exit cleanup_example(void)
{
printk (KERN_EMERG "Cleaning kernel module\n");
}
module_init(init_example);
module_exit(cleanup_example);
MODULE_DESCRIPTION ("Kernel function replacement module");
MODULE_AUTHOR ("DASAN Networks Inc.");
MODULE_LICENSE ("GPL");

Related

local_irq_disable does not work as expected

local_irq_disable()is used to disable all interrupts on the current processor
i wrote a basic code to test local_irq_disable().
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irqflags.h>
static int __init my_init(void)
{
pr_info("module is loaded on processor:%d\n", smp_processor_id());
local_irq_disable();
ssleep(10);
local_irq_enable();
return 0;
}
static void __exit my_exit(void)
{
}
MODULE_LICENSE("GPL");
module_init(my_init);
module_exit(my_exit);
When i load this on my system, it prints the following message:
[10478.930188] module is loaded on processor:5
And when i run "watch -d -n 0.2 'cat /proc/interrupts'", it still shows interrupts on those processor, what i am missing here?

kernel module compilation error of macro for_each_process

I am trying to compile a kernel module program to list tasks linearly, such that if you run the command ps -el should have the same or similar output. my textbook says:
"In the Linux kernel, the for_each_process() macro easily allows iteration over all current tasks in the system:
#include <linux/sched.h>
struct task_struct *task;
for_each_process(task) {
/* on each iteration task points to the next task */
}
so I have the following:
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
int tasks_lister_linear_init(void)
{
printk(KERN_INFO "Loading module...\n");
struct task_struct *task;
for_each_process(task)
{
printk(KERN_INFO "pid: %d | pname: %s | state: %ld\n", task->pid, task-
>comm, task->state);
}
printk(KERN_INFO "Module loaded.\n");
return 0;
}
void tasks_lister_linear_exit(void)
{
printk(KERN_INFO "Module removed.\n");
}
module_init(tasks_lister_linear_init);
module_exit(tasks_lister_linear_exit);
and when I go to make, I get an error saying
implicit declaration of function ‘for_each_process’; did you mean ‘for_each_node’?
expected ‘;’ before ‘{’ token
| for_each_process(task) {
| ^~
| ;
yet every call to this I have seen has not had a semicolon following. any advice on these errors would be greatly appreciated
You are probably using a newer kernel version. In later kernels, the macro for_each_process has been moved to the
#include <linux/sched/signal.h>
instead of
#include <linux/sched.h>

Kernel Module: No printk messages showing. Is init function being called?

I have a simple module, written as follows:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init hellomod_init(void)
{
printk(KERN_DEBUG, "Hello, world!\n");
return 0;
}
static void __exit hellomod_exit(void)
{
printk(KERN_DEBUG, "Goodbye, world!");
}
module_init(hellomod_init);
module_exit(hellomod_exit);
and
$ cat /proc/sys/kernel/printk
7 7 7 7
so that any level messages should output.
The module loads and this can be verified with lsmod, however no output is produced when loading or unloading the module and checking dmesg.
I have tried replacing KERN_DEBUG with lower levels and still no output, so I don't think the log level is the issue.
How else can I verify the hellomod_init() function is called? If it is not being called, what is my error?
I am running and compiling against kernel version 4.6.1-2 on Arch Linux.
There shouldn't be a comma after KERN_DEBUG. So it should look something like this:
static int __init hellomod_init(void)
{
printk(KERN_DEBUG "Hello, world!\n");
return 0;
}

Linux kernel programming: implicit declaration of function 'vmalloc'

I am adding system call to Linux kernel 6.22.
#include <stddef.h>
#incldue <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/lists.h>
#include <asm-i386/uaccess.h>
asmlinkage long sys_mypstree(char* buffer2copy){
char* buffer = (char*)vmalloc(sizeof(buffer2copy));
...
}
Then when i make the kernel.It shows the warning:implicit declaration of function 'vmalloc'.So,what am i gonna do now?
You should definitely:
#include <linux/vmalloc.h> as it will fix your warning.

Error: Unknown symbol in module?

I wrote a simple Linux kernel module:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/reboot.h>
static int my_init(void)
{
machine_power_off();
return 0;
}
static void my_exit(void)
{
return;
}
module_init(my_init);
module_exit(my_exit);
Source compiled successfully but after installing the module (insmod) the following error occurred:
Error: could not insert module my_module.ko: Unknown symbol in module
System log error:
Jun 25 21:50:00 my-virtual-machine kernel: [31625.207827] my_module: Unknown symbol machine_power_off (err 0)
How do I solve this error?
machine_power_off:
http://lxr.free-electrons.com/ident?i=machine_power_off
#Amir, traversed through the files in the linux kernel, w.r.t. all the architecture files where machine_power_off() is used, it is not exported so cannot be used in your module.

Resources