Callback function from kernel module
Hello All, Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module. -- Regards Jeshwanth Kumar N K +91-7411483498
You can use kobject_uevent() http://www.mjmwired.net/kernel/Documentation/kobject.txt#169 Also check out the kobject example under samples folder. BR Subbu On Sat, Nov 3, 2012 at 8:44 AM, Jeshwanth Kumar N K Jeshu < jeshkumar555@gmail.com> wrote:
Hello All,
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
-- Regards Jeshwanth Kumar N K +91-7411483498
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
HI I would like to reserve some of the system memory for our kernel modules. After some googling there seems to be there are different ways of doing this but still I am not clear. My question is if I want to put aside some portion of the our platform memory (about 300 Mbytes) for couple of our kernel modules, how could I achieve this and how to access the dedicated memory for those modules at the run time. Many thanks in advance, B
Hi...... On Sat, Nov 3, 2012 at 9:02 PM, Bizhan Gholikhamseh (bgholikh) <bgholikh@cisco.com> wrote:
My question is if I want to put aside some portion of the our platform memory (about 300 Mbytes) for couple of our kernel modules, how could I achieve this and how to access the dedicated memory for those modules at the run time.
after some googling, I think you need to use memory pool for page reservation. Someone is kind enough to summarize how to use memory pool here: http://venkateshpnv.blogspot.com/2011/05/memory-allocation-rough-notes-by-ch... (maybe he's also in kernelnewbies). Good luck... -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Hi, On Sat, Nov 3, 2012 at 9:41 PM, Mulyadi Santosa <mulyadi.santosa@gmail.com> wrote:
Hi...... Someone is kind enough to summarize how to use memory pool here: http://venkateshpnv.blogspot.com/2011/05/memory-allocation-rough-notes-by-ch...
What do you think about using vmalloc() ? Regards.
Hi On Sat, Nov 3, 2012 at 11:56 PM, Prashant Shah <pshah.mumbai@gmail.com> wrote:
Hi,
On Sat, Nov 3, 2012 at 9:41 PM, Mulyadi Santosa <mulyadi.santosa@gmail.com> wrote:
Hi...... Someone is kind enough to summarize how to use memory pool here: http://venkateshpnv.blogspot.com/2011/05/memory-allocation-rough-notes-by-ch...
What do you think about using vmalloc() ?
Actually, it depends on how the original poster define "reserve". I try my best to help with the ideas of 'reserving' in the sense that to put aside some amount of memory right from kernel boot stage, so they can't be used for other purpose. -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
On Sat, Nov 3, 2012 at 6:02 PM, Bizhan Gholikhamseh (bgholikh) <bgholikh@cisco.com> wrote:
HI
I would like to reserve some of the system memory for our kernel modules.
After some googling there seems to be there are different ways of doing this but still I am not clear.
My question is if I want to put aside some portion of the our platform memory (about 300 Mbytes) for couple of our kernel modules, how could I achieve this and how to access the dedicated memory for those modules at the run time.
Many thanks in advance,
B
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hi Have a look at CMA[ 1], ION [2] and dmabuf [3]. I think something of it will suite your needs... at least you can use reserve_bootmem [4] [1] http://lwn.net/Articles/486301/ [2] http://lwn.net/Articles/480055/ [3] http://lwn.net/Articles/474819/ [4] http://lxr.free-electrons.com/ident?i=reserve_bootmem Best wishes Vladimir Murzin
THanks Subramaniam , Get back to you once understand KObject :) On Sat, Nov 3, 2012 at 7:25 PM, Subramaniam Appadodharana < c.a.subramaniam@gmail.com> wrote:
You can use kobject_uevent()
http://www.mjmwired.net/kernel/Documentation/kobject.txt#169
Also check out the kobject example under samples folder.
BR Subbu
On Sat, Nov 3, 2012 at 8:44 AM, Jeshwanth Kumar N K Jeshu < jeshkumar555@gmail.com> wrote:
Hello All,
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
-- Regards Jeshwanth Kumar N K +91-7411483498
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards Jeshwanth Kumar N K +91-7411483498
Hi.. On Sat, Nov 3, 2012 at 8:44 PM, Jeshwanth Kumar N K Jeshu <jeshkumar555@gmail.com> wrote:
Hello All,
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
user space function or user space program? for later, IIRC you can use usermode_helper... -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Hi! On Sam, 2012-11-03 at 19:14 +0530, Jeshwanth Kumar N K Jeshu wrote: [...]
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
The usual way to implement this with a character device. The userspace application opens the character device. It then read()s the events from it, handles it and write()s results back (if needed). The kernel part handles to IRQs, puts that into a buffer where it waits for the read() from user space. You can use netlink sockets for this which may save some code though or implement a character device directly. Kind regards, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
I think you are looking for Upcall Functionality upcall functionality allows a kernel module to invoke a function in user space. It is possible to start a program in user space, and give it some command line arguments, as well as setting environment variables. ** int call_usermodehelper (char * *path*, char ** *argv*, char ** *envp*, int *wait*); Runs a user-space application. The application is started asynchronously if wait is not set, and runs as a child of keventd. (ie. it runs with full root capabilities). Must be called from process context. Returns a negative error code if program was not execed successfully, or 0. **man pages Regards Santosh On Wed, Nov 7, 2012 at 8:30 PM, Bernd Petrovitsch <bernd@petrovitsch.priv.at
wrote:
Hi!
On Sam, 2012-11-03 at 19:14 +0530, Jeshwanth Kumar N K Jeshu wrote: [...]
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
The usual way to implement this with a character device. The userspace application opens the character device. It then read()s the events from it, handles it and write()s results back (if needed). The kernel part handles to IRQs, puts that into a buffer where it waits for the read() from user space.
You can use netlink sockets for this which may save some code though or implement a character device directly.
Kind regards, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- *Regards, Santosh*
Hello Santosh and Anand, Thanks for the reply and very good suggestions. As Anand Moon suggestedhttp://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.htm... link in http://lists.kernelnewbies.org/pipermail/kernelnewbies/2012-November/006489.... previous thread, I read all the kernel to user space communication mechanism ( Except Upcall <http://people.ee.ethz.ch/%7Earkeller/linux/multi/kernel_user_space_howto.htm...>, ll read it). As of now for my application, the signals passing from kernel to user space is enough. I was trying the sample code provided in signals section of above link. 1. In the code he used *find_task_by_pid_type, *but *find_task_by_pid_type *not available in sched.h. So I have changed it to *find_task_by_vpid(pid)*, is this right way ? 2. I tried to install the module but it is not finding the symbol, but the symbol is present in */proc/kallsyms. * Anything I am missing while compiling ? as I am getting warning. *Compile:* jeshwanth@jeshwanth:~/linux/kernel_user_space/code/signals$ make make -C /lib/modules/3.0.0-26-generic-pae/build M=/home/jeshwanth/linux/kernel_user_space/code/signals modules make[1]: Entering directory `/usr/src/linux-headers-3.0.0-26-generic-pae' Building modules, stage 2. MODPOST 1 modules *WARNING:* "find_task_by_vpid" [/home/jeshwanth/linux/kernel_user_space/code/signals/signal_kernel.ko] undefined! make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-26-generic-pae' [1]+ Done gedit signal_kernel.c *install Command:* root@jeshwanth:/home/jeshwanth/linux/kernel_user_space/code/signals# insmod signal_kernel.ko insmod: error inserting 'signal_kernel.ko': -1 Unknown symbol in module */proc/kallsyms:* 00000000 T find_task_by_pid_ns 00000000 T find_task_by_vpid *dmesg:* [15012.556740] signal_kernel: Unknown symbol find_task_by_vpid (err 0) *Code:* #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <asm/siginfo.h> //siginfo #include <linux/rcupdate.h> //rcu_read_lock #include <linux/sched.h> //find_task_by_pid_type #include <linux/debugfs.h> #include <linux/uaccess.h> #define SIG_TEST 44 // we choose 44 as our signal number (real-time signals are in the range of 33 to 64) struct dentry *file; static ssize_t write_pid(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char mybuf[10]; int pid = 0; int ret; struct siginfo info; struct task_struct *t; /* read the value from user space */ if(count > 10) return -EINVAL; copy_from_user(mybuf, buf, count); sscanf(mybuf, "%d", &pid); printk("pid = %d\n", pid); /* send the signal */ memset(&info, 0, sizeof(struct siginfo)); info.si_signo = SIG_TEST; info.si_code = SI_QUEUE; // this is bit of a trickery: SI_QUEUE is normally used by sigqueue from user space, // and kernel space should use SI_KERNEL. But if SI_KERNEL is used the real_time data // is not delivered to the user space signal handler function. info.si_int = 1234; //real time signals may have 32 bits of data. rcu_read_lock(); //t = find_task_by_pid_type(PIDTYPE_PID, pid); //find the task_struct associated with this pid t = find_task_by_vpid(pid); if(t == NULL){ printk("no such pid\n"); rcu_read_unlock(); return -ENODEV; } rcu_read_unlock(); ret = send_sig_info(SIG_TEST, &info, t); //send the signal if (ret < 0) { printk("error sending signal\n"); return ret; } return count; } static const struct file_operations my_fops = { .write = write_pid, }; static int __init signalexample_module_init(void) { /* we need to know the pid of the user space process * -> we use debugfs for this. As soon as a pid is written to * this file, a signal is sent to that pid */ /* only root can write to this file (no read) */ file = debugfs_create_file("signalconfpid", 0200, NULL, NULL, &my_fops); return 0; } static void __exit signalexample_module_exit(void) { debugfs_remove(file); } module_init(signalexample_module_init); module_exit(signalexample_module_exit); MODULE_LICENSE("GPL"); On Thu, Nov 8, 2012 at 11:33 AM, SaNtosh kuLkarni < santosh.yesoptus@gmail.com> wrote:
I think you are looking for Upcall Functionality
upcall functionality allows a kernel module to invoke a function in user space. It is possible to start a program in user space, and give it some command line arguments, as well as setting environment variables.
** int call_usermodehelper (char * *path*, char ** *argv*, char ** *envp*, int *wait*);
Runs a user-space application. The application is started asynchronously if wait is not set, and runs as a child of keventd. (ie. it runs with full root capabilities).
Must be called from process context. Returns a negative error code if program was not execed successfully, or 0.
**man pages
Regards
Santosh
On Wed, Nov 7, 2012 at 8:30 PM, Bernd Petrovitsch < bernd@petrovitsch.priv.at> wrote:
Hi!
On Sam, 2012-11-03 at 19:14 +0530, Jeshwanth Kumar N K Jeshu wrote: [...]
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
The usual way to implement this with a character device. The userspace application opens the character device. It then read()s the events from it, handles it and write()s results back (if needed). The kernel part handles to IRQs, puts that into a buffer where it waits for the read() from user space.
You can use netlink sockets for this which may save some code though or implement a character device directly.
Kind regards, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- *Regards, Santosh*
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards Jeshwanth Kumar N K +91-7411483498
Hi all, You cand find number of example on how to use send_sig_info api. please follow this link : http://lxr.linux.no/linux+v3.6.6/kernel/pid_namespace.c#L179 Regarding this warning -------------------------------------------------------------------------------- jeshwanth@jeshwanth:~/linux/kernel_user_space/code/signals$ make make -C /lib/modules/3.0.0-26-generic-pae/build M=/home/jeshwanth/linux/kernel_user_space/code/signals modules make[1]: Entering directory `/usr/src/linux-headers-3.0.0-26-generic-pae' Building modules, stage 2. MODPOST 1 modules WARNING: "find_task_by_vpid" [/home/jeshwanth/linux/kernel_user_space/code/signals/signal_kernel.ko] undefined! make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-26-generic-pae' [1]+ Done gedit signal_kernel.c install Command: root@jeshwanth:/home/jeshwanth/linux/kernel_user_space/code/signals# insmod signal_kernel.ko insmod: error inserting 'signal_kernel.ko': -1 Unknown symbol in module /proc/kallsyms: 00000000 T find_task_by_pid_ns 00000000 T find_task_by_vpid -------------------------------------------------------------------------------- you need the export these functions so that you can use then in your module. -Anand Moon ________________________________ From: Jeshwanth Kumar N K Jeshu <jeshkumar555@gmail.com> To: SaNtosh kuLkarni <santosh.yesoptus@gmail.com>; moon.linux@yahoo.com Cc: Bernd Petrovitsch <bernd@petrovitsch.priv.at>; kernelnewbies@kernelnewbies.org Sent: Saturday, November 10, 2012 1:47 PM Subject: Re: Callback function from kernel module Hello Santosh and Anand, Thanks for the reply and very good suggestions. As Anand Moon suggestedhttp://people.ee.ethz.ch/%7Earkeller/linux/multi/kernel_user_space_howto-3.h... link in http://lists.kernelnewbies.org/pipermail/kernelnewbies/2012-November/006489.... previous thread, I read all the kernel to user space communication mechanism ( ExceptUpcall , ll read it). As of now for my application, the signals passing from kernel to user space is enough. I was trying the sample code provided in signals section of above link. 1. In the code he used find_task_by_pid_type, but find_task_by_pid_type not available in sched.h. So I have changed it to find_task_by_vpid(pid), is this right way ? 2. I tried to install the module but it is not finding the symbol, but the symbol is present in /proc/kallsyms. Anything I am missing while compiling ? as I am getting warning. Compile: jeshwanth@jeshwanth:~/linux/kernel_user_space/code/signals$ make make -C /lib/modules/3.0.0-26-generic-pae/build M=/home/jeshwanth/linux/kernel_user_space/code/signals modules make[1]: Entering directory `/usr/src/linux-headers-3.0.0-26-generic-pae' Building modules, stage 2. MODPOST 1 modules WARNING: "find_task_by_vpid" [/home/jeshwanth/linux/kernel_user_space/code/signals/signal_kernel.ko] undefined! make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-26-generic-pae' [1]+ Done gedit signal_kernel.c install Command: root@jeshwanth:/home/jeshwanth/linux/kernel_user_space/code/signals# insmod signal_kernel.ko insmod: error inserting 'signal_kernel.ko': -1 Unknown symbol in module /proc/kallsyms: 00000000 T find_task_by_pid_ns 00000000 T find_task_by_vpid dmesg: [15012.556740] signal_kernel: Unknown symbol find_task_by_vpid (err 0) Code: #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <asm/siginfo.h> //siginfo #include <linux/rcupdate.h> //rcu_read_lock #include <linux/sched.h> //find_task_by_pid_type #include <linux/debugfs.h> #include <linux/uaccess.h> #define SIG_TEST 44 // we choose 44 as our signal number (real-time signals are in the range of 33 to 64) struct dentry *file; static ssize_t write_pid(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char mybuf[10]; int pid = 0; int ret; struct siginfo info; struct task_struct *t; /* read the value from user space */ if(count > 10) return -EINVAL; copy_from_user(mybuf, buf, count); sscanf(mybuf, "%d", &pid); printk("pid = %d\n", pid); /* send the signal */ memset(&info, 0, sizeof(struct siginfo)); info.si_signo = SIG_TEST; info.si_code = SI_QUEUE; // this is bit of a trickery: SI_QUEUE is normally used by sigqueue from user space, // and kernel space should use SI_KERNEL. But if SI_KERNEL is used the real_time data // is not delivered to the user space signal handler function. info.si_int = 1234; //real time signals may have 32 bits of data. rcu_read_lock(); //t = find_task_by_pid_type(PIDTYPE_PID, pid); //find the task_struct associated with this pid t = find_task_by_vpid(pid); if(t == NULL){ printk("no such pid\n"); rcu_read_unlock(); return -ENODEV; } rcu_read_unlock(); ret = send_sig_info(SIG_TEST, &info, t); //send the signal if (ret < 0) { printk("error sending signal\n"); return ret; } return count; } static const struct file_operations my_fops = { .write = write_pid, }; static int __init signalexample_module_init(void) { /* we need to know the pid of the user space process * -> we use debugfs for this. As soon as a pid is written to * this file, a signal is sent to that pid */ /* only root can write to this file (no read) */ file = debugfs_create_file("signalconfpid", 0200, NULL, NULL, &my_fops); return 0; } static void __exit signalexample_module_exit(void) { debugfs_remove(file); } module_init(signalexample_module_init); module_exit(signalexample_module_exit); MODULE_LICENSE("GPL"); On Thu, Nov 8, 2012 at 11:33 AM, SaNtosh kuLkarni <santosh.yesoptus@gmail.com> wrote: I think you are looking for Upcall Functionality
upcall functionality allows a kernel module to invoke a function in user space. It is possible to start a program in user space, and give it some command line arguments, as well as setting environment variables.
** int call_usermodehelper (char * path, char ** argv, char ** envp, int wait);
Runs a user-space application. The application is started asynchronously if wait is not set, and runs as a child of keventd. (ie. it runs with full root capabilities). Must be called from process context. Returns a negative error code if program was not execed successfully, or 0. **man pages Regards Santosh
On Wed, Nov 7, 2012 at 8:30 PM, Bernd Petrovitsch <bernd@petrovitsch.priv.at> wrote:
Hi!
On Sam, 2012-11-03 at 19:14 +0530, Jeshwanth Kumar N K Jeshu wrote: [...]
Can I call userspace function from kernel module ? Actually I need to process some data in user space for every event occured in kernel module.
The usual way to implement this with a character device. The userspace application opens the character device. It then read()s the events from it, handles it and write()s results back (if needed). The kernel part handles to IRQs, puts that into a buffer where it waits for the read() from user space.
You can use netlink sockets for this which may save some code though or implement a character device directly.
Kind regards, Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at/
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards, Santosh
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards Jeshwanth Kumar N K +91-7411483498 _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
participants (9)
-
Anand Moon -
Bernd Petrovitsch -
Bizhan Gholikhamseh (bgholikh) -
Jeshwanth Kumar N K Jeshu -
Mulyadi Santosa -
Prashant Shah -
SaNtosh kuLkarni -
Subramaniam Appadodharana -
Vladimir Murzin