Why this kernel module doesn't do anything when i load it?
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#define DEVICE_NAME "hello-1.00.a"
#define DRIVER_NAME "hello"
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(struct platform_device *pdev){
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static int hello_exit(struct platform_device *pdev){
printk(KERN_ALERT "Goodbye, cruel world\n");
return 0;
}
static const struct of_device_id myled_of_match[] =
{
{.compatible = DEVICE_NAME},
{},
};
MODULE_DEVICE_TABLE(of, myled_of_match);
static struct platform_driver hello_driver =
{
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = myled_of_match
},
.probe = hello_init,
.remove = hello_exit
};
module_platform_driver(hello_driver);
It musts print Hello, world\n, if i do lsmod the module appear to be loaded:
lsmod
hello_world 1538 0 - Live 0xbf000000 (O)
but nothing is printed neither in the console nor in dmesg.
If i use module_init and module_exit all works, but i need the pointer platform_device *pdev to the device, what can i do?
EDIT:
the original module looks like this:
#include <linux/init.h>
#include <linux/module.h>
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);
In my device tree blob is present this entry:
hello {
compatible = "dglnt,hello-1.00.a";
reg = <0x41220000 0x10000>;
};
If i use module_init and module_exit all works
That short "original" code only consists of the module framework. The init routine is guaranteed to be called when the module is loaded, and the exit routine is called prior to unloading. That "original" code is not a driver.
The longer kernel module is a driver and getting loaded, but since it has the default init and exit code that does nothing (as generated by the expansion of the module_platform_driver() macro), there are no messages. The driver code in the loadable module is not guaranteed to be called when the kernel uses a Device Tree.
Why this kernel module doesn't do anything when i load it?
The probe function of the driver (which would output messages) is probably not getting called because there is nothing in your Device Tree that indicates that this device driver is needed.
The snippet of the board's Device Tree has
compatible = "dglnt,hello-1.00.a";
but the driver declares that it should specified as
#define DEVICE_NAME "hello-1.00.a"
...
{.compatible = DEVICE_NAME},
These strings should match so that the driver can bind with this referenced device in the Device Tree node.
Also the device node should be declared as
status = "okay";
to override any default status that could disable the device.
A properly configured node in the Device Tree should get the driver's probe function to be executed as expected.
Related
Disclaimer - I have to admit that it's the 1'st time I'm using this kernel interface (socket).
I'm currently working on a design of a kernel module that is based on a netlink socket .
I'm using Ubuntu14.04 and linux kernel 4.
As a starter, I wanted to make sure that I can use the netlink socket in both directions.
I've written an application that does the following:
1) User send a message to kernel via the netlink socket.
2) Kernel, upon receiving the message – sends "ABCD" string message to a workqueue.
3) When the "ABCD" message is received by the workqueue, it calls a function (named - my_wq_function) which send it back to the user space via netlink socket.
4) In the user space I'm using a recvmsg function (blocking until a message is received) and displays the "ABCD" message.
My problem is that the return value from the recvmsg function is 20 (instead of 4), and the data itself (i.e. NLMSG_DATA) is empty.
During the debug I tried to change the message to "ABCD1234" and got a return value of 24 bytes, however the data is still empty.
I also verified that my entire path until the point of sending the "ABCD" from kernel to the socket is OK.
Not sure what I'm doing wrong here & will highly appreciate your help.
Thanks in advance, MotiC.
my code example can be found below:
User space code:
printf("netlink receiver thread started...\n");
nlh_rcv = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
while(true) //endless loop on netlink socket
{
memset(nlh_rcv, 0, NLMSG_SPACE(MAX_PAYLOAD));
iov_rcv.iov_base = (void *)nlh_rcv;
iov_rcv.iov_len = nlh_rcv->nlmsg_len;
msg_rcv.msg_name = (void *)&dest_addr;
msg_rcv.msg_namelen = sizeof(dest_addr);
msg_rcv.msg_iov = &iov;
msg_rcv.msg_iovlen = 1;
ret=recvmsg(sock_fd, &msg_rcv, 0);
printf("errno=%i bytes=%i message from kernel: %s\n",errno, ret, (char*)NLMSG_DATA(nlh_rcv));
uint8_t mymsg[100];
memcpy(mymsg, NLMSG_DATA(nlh_rcv), 100);
printf("message from kernel: %s\n",mymsg);
}
Kernel space code:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <linux/workqueue.h>
MODULE_LICENSE("GPL");
#include "rf_Kdriver_main.h"
//------ definitions ------------------------------------------------------------------------------------------------------------
#define NETLINK_USER 31
#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sock *nl_sk = NULL;
struct nlmsghdr *nlh;
struct nlmsghdr *nlh_out;
struct sk_buff *skb_out;
char buf_to_user[100];
int pid;
//------------------------------------------------------------------------------------------------------------------------------
struct workqueue_struct *my_wq;
typedef struct {
struct work_struct my_work;
uint8_t msg_to_pc[128];
uint8_t msg_len;
} my_work_t;
my_work_t *work, *work2;
//-----------------------------------------------------------------------------------------------------------------------------
static void my_wq_function( struct work_struct *work)
{
int res;
my_work_t *my_work = (my_work_t *)work;
skb_out = nlmsg_new(my_work->msg_len,0);
if (!skb_out)
{
printk("Failed to allocate new skb\n");
return;
}
nlh_out = nlmsg_put(skb_out, 0, 0, NLMSG_DONE,my_work->msg_len, 0);
NETLINK_CB(skb_out).dst_group = 0;
memcpy((char*)NLMSG_DATA(nlh_out), my_work->msg_to_pc , my_work->msg_len);
printk( "dequeue message to pc=%s len=%i\n", (char*)NLMSG_DATA(nlh_out), (int)strlen((char*)NLMSG_DATA(nlh_out)));
res = nlmsg_unicast(nl_sk, skb_out, pid);
if (res<0)
printk("Failed to send message from kernel to user\n");
kfree( (void *)work );
return;
}
//-----------------------------------------------------------------------------------------------------------------------------
int send_up_msg_to_workque(uint8_t msg_to_pc[], uint8_t msg_len)
{
int ret=0;
work = (my_work_t *)kmalloc(sizeof(my_work_t), GFP_KERNEL);
if (work) {
INIT_WORK( (struct work_struct *)work, my_wq_function );
memcpy(work->msg_to_pc, msg_to_pc, msg_len);
work->msg_len = msg_len;
ret = queue_work( my_wq, /*(struct work_struct *)RR*/work );
printk("kuku ret=%i msg=%s\n",ret,work->msg_to_pc);
}
return ret;
}
//------------------------------------------------------------------------------------------------------------------------------
static void netlink_recv_msg(struct sk_buff *skb)
{
char *msg = "ABCD1234";
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
nlh=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink at kernel received msg payload: %s\n",(char*)NLMSG_DATA(nlh));
//rr
pid = nlh->nlmsg_pid;
send_up_msg_to_workque((uint8_t*) msg, strlen(msg));
}
//-------------------------------------------------------------------------------------------------------------------------------------
struct netlink_kernel_cfg cfg = {
.input = netlink_recv_msg,
};
static int __init rf_driver_start(void)
{
printk(KERN_INFO "Loading RF Driver module1...\n");
my_wq = create_workqueue("my_queue");
if (!my_wq)
{
printk("Failed to create work queue\n");
}
printk("Entering: %s\n",__FUNCTION__);
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if(!nl_sk)
{
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}
//--------------------------------------------------------------------------------------------------------------
static void __exit rf_driver_end(void)
{
netlink_kernel_release(nl_sk);
flush_workqueue(my_wq);
destroy_workqueue(my_wq);
printk(KERN_INFO "RF Driver exit...\n");
}
module_init(rf_driver_start);
module_exit(rf_driver_end);
Update,
I changed my user space function to:
char buf[100];
ret=recv(sock_fd, buf, 100, 0);
instead of:
ret=recvmsg(sock_fd, &msg_rcv, 0);
and it works...
does anyone have an idea regarding this strange behavior ?
Thanks.
Can you please paste complete userspace code.
I guess 'len' int this code is the issue:
memset(nlh_rcv, 0, NLMSG_SPACE(MAX_PAYLOAD));
iov_rcv.iov_len = nlh_rcv->nlmsg_len; << check to what value is it getting initialized.
how to real time display the current code running in kernel with kernel module in ubuntu and moreover to use keyboard key to control stop and continue running ubuntu kernel
when compile a custom linux kernel, it takes so long to run. for just a small changes takes a long time to see the result, is there a fast way?
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lakshmanan");
MODULE_DESCRIPTION("A Simple Hello World module");
static int __init hello_init(void)
{
printk(KERN_INFO "Hello world!\n");
return 0; // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);
I am attempting some Linux kernel development.
I have a problem in including a header file sched.h (which exists in the path /usr/src/linux-3.12.26/kernel/sched, not in /usr/src/linux-3.12.26/include/linux ).
But when I "sudo make -C /usr/src/linux-3.12.26/ M=$(pwd) modules" I got the error "fatal error: kernel/sched/sched.h: doesn't exist"
Here is my code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/percpu.h>
#include </usr/src/linux-3.12.26/kernel/sched/sched.h>
int __init init_current(void){
int cpu_num=0;
struct task_struct *p;
for(cpu_num=0;cpu_num<8;cpu_num++)
{
p=curr_task(cpu_num);
printk(KERN_ALERT "current task on cpu %d is %d\n", cpu_num, p->pid);
}
return 0;
}
void __exit exit_current(void)
{
printk(KERN_ALERT "FINISHED!\n");
}
MODULE_LICENSE("GPL");
module_init(init_current);
module_exit(exit_current);
There may be some errors in the code. My intention is to get the current running process in different cores.
#Santosh A .Thanks for continuing concerning my puzzle.After I changed the Makefile in /usr/src/linux-3.12.26/(see my answer above,it may not be normative,but work ),the headers I include can be found.Then I get another problem. Here is my code:
#include <linux/init.h>
#include <linux/module.h>
//#include <linux/sched.h>
#include <linux/percpu.h>
#include <sched/sched.h>
static int __init init_current(void){
int cpu_num=0;
extern struct task_struct *p;
struct rq *q;
extern struct rq * cpu_rq(int);
for(cpu_num=0;cpu_num<8;cpu_num++)
{
q=cpu_rq(cpu_num);
p=q->curr;
printk(KERN_ALERT "current task on cpu %d is %d\n", cpu_num, p->pid);
}
return 0;
}
static void __exit exit_current(void)
{
printk(KERN_ALERT "FINISHED!\n");
}
MODULE_LICENSE("GPL");
module_init(init_current);
module_exit(exit_current);
Here is error info:
/home/wison/code/current/current.c: In function ‘init_current’:
kernel/sched/sched.h:539:23: error: expected identifier or ‘(’ before ‘&’ token
#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
^
/home/wison/code/current/current.c:11:28: note: in expansion of macro ‘cpu_rq’
extern struct rq * cpu_rq(int);
I wrote a simple code to capture the netdevice notifications and simply print their value out to the messages log file ... here's the code :
#include <linux/notifier.h>
#include <asm/kdebug.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
int my_dev_event_handler (struct notifier_block *self,unsigned long val, void *data)
{
printk (KERN_INFO "my_dev_event: Val=%ld, Interface=%s\n", val,((struct net_device *) data)->name);
return 0;
}
static struct notifier_block my_dev_notifier = {
.notifier_call = my_dev_event_handler,
};
static int __init
my_init (void)
{
printk(KERN_ALERT "***Module Loaded***\n");
register_netdevice_notifier (&my_dev_notifier);
return 0;
}
static void __exit my_end(void)
{
printk(KERN_ALERT "***Module Unloaded***\n");
}
module_init(my_init);
module_exit(my_end);
this code compiles and runs correctly, it prints out the "my_dev_event:..." line every time a device goes up/off ... but sometimes (not always) the entire system freezes when a device goes up\down ... now I have two questions here:
1- why is the system freezing? anything wrong with this code?
2- if there's a better way to notify my kernel module when a device goes connected/disconnected ...
The only problem I see is that my_end doesn't unregister the notifier.
This can cause crashes or freezes after you've unloaded your module. This is because a pointer to your code remains in Linux data structures, but your code is no longer there.
Regarding an alternative way - I think you're using the correct way to get these notifications.
I copy and paste code from this URL for creating and reading/writing a proc file using a kernel module and get the error that proc_root is undeclared. This same example is on a few sites so I assume it works. Any ideas why I'd get this error? Does my makefile need something different. Below is my makefile as well:
Example code for a basic proc file creation (direct copy and paste to get initial test done):
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN769
Makefile I'm using:
obj-m := counter.o
KDIR := /MY/LINUX/SRC
PWD := $(shell pwd)
default:
$(MAKE) ARCH=um -C $(KDIR) SUBDIRS=$(PWD) modules
That example is out of date. Under the current kernel API, pass NULL for the root of procfs.
Also, instead of create_proc_entry, you should use proc_create() with a proper const struct file_operations *.
There has been a change in the interface to create an entry in the proc file system. You can have a look at http://pointer-overloading.blogspot.in/2013/09/linux-creating-entry-in-proc-file.html for details
Here is a 'hello_proc' example with the new interface:
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int hello_proc_show(struct seq_file *m, void *v) {
seq_printf(m, "Hello proc!\n");
return 0;
}
static int hello_proc_open(struct inode *inode, struct file *file) {
return single_open(file, hello_proc_show, NULL);
}
static const struct file_operations hello_proc_fops = {
.owner = THIS_MODULE,
.open = hello_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init hello_proc_init(void) {
proc_create("hello_proc", 0, NULL, &hello_proc_fops);
return 0;
}
static void __exit hello_proc_exit(void) {
remove_proc_entry("hello_proc", NULL);
}
MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);
Update:
The above accepted answer might have worked for you. It no longer works in GNU/Linux 5.6.y and above! Since 5.6, proc_create() will accept proc_ops as argument instead of file_operations. Fields are prepended with proc_ and there's no owner field in proc_ops (check here).
As a side note, a programmer would wish for a portable code. In this, same code shall work across different versions of GNU/Linux. So, you may also need to use LINUX_VERSION_CODE, KERNEL_VERSION(5,6,0) macros which are in linux/version.h. For example,
#include <linux/version.h>
...
...
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5,6,0))
static struct file_operations
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
static struct proc_ops
#endif
proc_file_ops = {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5,6,0))
owner : THIS_MODULE,
read : proc_file_read,
write : proc_file_write
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
proc_read : proc_file_read,
proc_write : proc_file_write
#endif
};
...
...
AFAIK apart from these, I couldn't note any other major changes :)