Re: Tools for checking incorrect usage of locking techniques in k-space
On Mon, Nov 05, 2012 at 09:51:19AM +0400, Vladimir Murzin wrote: > I've not tried your module, but I suppose that you should get warning > as soon as you try to take write semaphore once again. > Thank you for your mail. I wanted to try what you've suggested, but now I'm facing a different problem. For kernel-janitorial purpose I sync my local repo to the linux-next tree almost everyday and once in a while I build and boot from the latest kernel from the linux-next tree. But most of the time, After syncing to the latest linux-next tree, I perform only following steps to build some staging drivers under linux-next tree: >From the top of the latest linux-next tree: # make oldconfig # make prepare # make modules_prepare # make M=drivers/staging/ The above steps work fine, except that I get some warning message. WARNING: Symbol version dump /home/amit/lkernel/Module.symvers is missing; modules will have no dependencies and modversions. So my understanding is that I've booted from version-X and the linux-next kernel version is version-Y and because of this version mismatch, I see the above WARNING message. Now, because of this kernel version mismatch I am not able to insert the module. An attempt to do so returns following error message: root@ubuntu:/home/amit/ldd3/misc-modules/echo# insmod echo.ko insmod: error inserting 'echo.ko': -1 Invalid module format Some information regarding my setup: amit@ubuntu:~/ldd3/misc-modules/echo$ uname -r 3.7.0-rc3-next-20121029 amit@ubuntu:~/ldd3/misc-modules/echo$ ll /lib/modules/`uname -r`/build lrwxrwxrwx 1 root root 18 Oct 29 22:02 /lib/modules/3.7.0-rc3-next-20121029/build -> /home/amit/lkernel/ Here the /lib/modules/3.7.0-rc3-next-20121029/build is a softlink to the latest kernel sources. If I try to install the header files for my current kernel(returned by 'uname -r'), I get following error message: amit@ubuntu:~$ sudo apt-get install linux-headers-$(uname -r) Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package linux-headers-3.7.0-rc3-next-20121029 E: Couldn't find any package by regex 'linux-headers-3.7.0-rc3-next-20121029' How do I ovecome this ? Do I need to build the latest kernel(linux-next) and boot from it, whenever I sync my local linux-next repository with the remote? I'd like to be able to check my local changes by building and loading the module/subsystem. -Amit ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Tools for checking incorrect usage of locking techniques in k-space
Hi All, While building the new kernel their are many option for kernel debugging please refer the below link. http://my.safaribooksonline.com/book/operating-systems-and-server-administration/linux/9780137072446/kernel-hacking-config-options/ch11 Hope this will help you catch the lock problem. -Anand Moon From: Vladimir Murzin To: Kumar amit mehta Cc: Srivatsa Bhat ; kernelnewbies@kernelnewbies.org Sent: Monday, November 5, 2012 11:21 AM Subject: Re: Tools for checking incorrect usage of locking techniques in k-space On Tue, Oct 30, 2012 at 9:41 AM, Kumar amit mehta wrote: > On Mon, Oct 29, 2012 at 12:03:08AM +0530, Srivatsa Bhat wrote: >> You'll need CONFIG_LOCKDEP=y as well. An easy way to configure lock debugging >> checks is to run 'make menuconfig' and enable the required options under the >> "Kernel hacking" section. >> >> > >> > If above configuration is all that I need, then should I be seeing >> > warning/error >> > messages in kernel logs(/var/log/kern.log) when there is inconsistency in >> > locking ? To test my hypothesis, I modified my simple kernel module to >> > deliberately induce locking error (After initializing read-write >> > semaphore, I call >> > down_write() and do not free this semaphore lock by commenting out >> > up_write() >> > invocation). But still I don't see any error or warning message trace in >> > kernel >> > logs, I think, I'm missing something. > > Hi Srivatsa, > > Thank you for your mail. As per your suggestion, this time I've enabled > CONFIG_LOCKDEP aslo in my running kernel and did the same experiment, but > still > I dont't see any warning/error messages in the kernel log. To give you more > idea > about what I'm doing, Please see the code below.(This is a simple char driver > based on LDD3 examples) > > > > #include > #include > #include //MAJOR, MINOR > #include //register_chrdev_region, file_operations > #include > #include //container_of > #include //kmalloc > #include //struct cdev > #include > #include //copy_from/to_user() > #include //error code > > ssize_t echo_read(struct file *, char __user *, size_t, loff_t *); > ssize_t echo_write(struct file *, const char __user *, size_t, loff_t *); > int echo_open(struct inode *, struct file *); > int echo_release(struct inode *, struct file *); > > struct echo_cdev { > char *data; > unsigned long size; //amount of data stored > struct semaphore sem; > struct cdev cdev; > }; > MODULE_LICENSE("GPL v2"); > MODULE_AUTHOR("amit"); > > int nr_major; > module_param(nr_major, int, S_IRUGO); > MODULE_PARM_DESC(nr_major, "major number"); > > int nr_minor; > char *chrdev_name = "echo"; > > static dev_t device; > static int echo_dev_count = 1; > struct echo_cdev *echo_dev = NULL; > > static struct file_operations echo_fs_ops = { > .open = echo_open, > .release = echo_release, > .read = echo_read, > .write = echo_write, > .owner = THIS_MODULE, > }; > > int echo_open(struct inode *inode, struct file *filp) > { > struct echo_cdev *dev; > pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); > //container_of(pointer, container_type, container_field); > dev = container_of(inode->i_cdev, struct echo_cdev, cdev); > filp->private_data = dev; > if ((filp->f_flags & O_ACCMODE) == O_WRONLY) { > //trim the device size to 0 > dev->size = 0; > } > return 0; > } > > int echo_release(struct inode *inode, struct file *filp) > { > return 0; > } > > ssize_t echo_read(struct file *filp, char __user *ubuff, size_t count, loff_t > *poffset) > { > struct echo_cdev *dev = filp->private_data; > pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); > > //user trying to access an offset which is beyond the end of file > if (down_interruptible(&dev->sem)) > return -ERESTARTSYS; > if (*poffset >= dev->size) { > up(&dev->sem); > return 0; > } > > //user trying to access more than eof, return bytes read till the eof > if (*poffset + count >= dev->size) > //count = dev->size - *poffset; > count = dev->size; > //kspace --> uspace > if (copy_to_user(ubuff,
Re: Tools for checking incorrect usage of locking techniques in k-space
On Tue, Oct 30, 2012 at 9:41 AM, Kumar amit mehta wrote: > On Mon, Oct 29, 2012 at 12:03:08AM +0530, Srivatsa Bhat wrote: >> You'll need CONFIG_LOCKDEP=y as well. An easy way to configure lock debugging >> checks is to run 'make menuconfig' and enable the required options under the >> "Kernel hacking" section. >> >> > >> > If above configuration is all that I need, then should I be seeing >> > warning/error >> > messages in kernel logs(/var/log/kern.log) when there is inconsistency in >> > locking ? To test my hypothesis, I modified my simple kernel module to >> > deliberately induce locking error (After initializing read-write >> > semaphore, I call >> > down_write() and do not free this semaphore lock by commenting out >> > up_write() >> > invocation). But still I don't see any error or warning message trace in >> > kernel >> > logs, I think, I'm missing something. > > Hi Srivatsa, > > Thank you for your mail. As per your suggestion, this time I've enabled > CONFIG_LOCKDEP aslo in my running kernel and did the same experiment, but > still > I dont't see any warning/error messages in the kernel log. To give you more > idea > about what I'm doing, Please see the code below.(This is a simple char driver > based on LDD3 examples) > > > > #include > #include > #include //MAJOR, MINOR > #include //register_chrdev_region, file_operations > #include > #include //container_of > #include //kmalloc > #include //struct cdev > #include > #include //copy_from/to_user() > #include //error code > > ssize_t echo_read(struct file *, char __user *, size_t, loff_t *); > ssize_t echo_write(struct file *, const char __user *, size_t, loff_t *); > int echo_open(struct inode *, struct file *); > int echo_release(struct inode *, struct file *); > > struct echo_cdev { > char *data; > unsigned long size; //amount of data stored > struct semaphore sem; > struct cdev cdev; > }; > MODULE_LICENSE("GPL v2"); > MODULE_AUTHOR("amit"); > > int nr_major; > module_param(nr_major, int, S_IRUGO); > MODULE_PARM_DESC(nr_major, "major number"); > > int nr_minor; > char *chrdev_name = "echo"; > > static dev_t device; > static int echo_dev_count = 1; > struct echo_cdev *echo_dev = NULL; > > static struct file_operations echo_fs_ops = { > .open = echo_open, > .release = echo_release, > .read = echo_read, > .write = echo_write, > .owner = THIS_MODULE, > }; > > int echo_open(struct inode *inode, struct file *filp) > { > struct echo_cdev *dev; > pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); > //container_of(pointer, container_type, container_field); > dev = container_of(inode->i_cdev, struct echo_cdev, cdev); > filp->private_data = dev; > if ((filp->f_flags & O_ACCMODE) == O_WRONLY) { > //trim the device size to 0 > dev->size = 0; > } > return 0; > } > > int echo_release(struct inode *inode, struct file *filp) > { > return 0; > } > > ssize_t echo_read(struct file *filp, char __user *ubuff, size_t count, loff_t > *poffset) > { > struct echo_cdev *dev = filp->private_data; > pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); > > //user trying to access an offset which is beyond the end of file > if (down_interruptible(&dev->sem)) > return -ERESTARTSYS; > if (*poffset >= dev->size) { > up(&dev->sem); > return 0; > } > > //user trying to access more than eof, return bytes read till the eof > if (*poffset + count >= dev->size) > //count = dev->size - *poffset; > count = dev->size; > //kspace --> uspace > if (copy_to_user(ubuff, (dev->data + *poffset), count) < 0) { > up(&dev->sem); > return -EFAULT; > } > //update the offset > *poffset += count; > up(&dev->sem); > return count; > } > > //count is the size of requested data transfer > ssize_t echo_write(struct file *filp, const char __user *ubuff, size_t count, > loff_t *poffset) > { > int ret; > struct echo_cdev *dev = filp->private_data; > pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); > if (down_interruptible(&dev->sem)) > return -ERESTARTSYS; > if (dev->data == NULL) { > dev->data = (char *)kmalloc(count, GFP_KERNEL); > if (!dev->data) { > up(&dev->sem); > return -ENOMEM; > } else { > memset(dev->data, 0, sizeof(count)); > } > } > dev->size = count; > //uspace --> kspace > if (copy_from_user(dev->data, ubuff, count) < 0) { > up(&dev->sem); > return -EFAULT; > } > > *poffset += cou
Re: Tools for checking incorrect usage of locking techniques in k-space
On Mon, Oct 29, 2012 at 12:03:08AM +0530, Srivatsa Bhat wrote: > You'll need CONFIG_LOCKDEP=y as well. An easy way to configure lock debugging > checks is to run 'make menuconfig' and enable the required options under the > "Kernel hacking" section. > > > > > If above configuration is all that I need, then should I be seeing > > warning/error > > messages in kernel logs(/var/log/kern.log) when there is inconsistency in > > locking ? To test my hypothesis, I modified my simple kernel module to > > deliberately induce locking error (After initializing read-write semaphore, > > I call > > down_write() and do not free this semaphore lock by commenting out > > up_write() > > invocation). But still I don't see any error or warning message trace in > > kernel > > logs, I think, I'm missing something. Hi Srivatsa, Thank you for your mail. As per your suggestion, this time I've enabled CONFIG_LOCKDEP aslo in my running kernel and did the same experiment, but still I dont't see any warning/error messages in the kernel log. To give you more idea about what I'm doing, Please see the code below.(This is a simple char driver based on LDD3 examples) #include #include #include //MAJOR, MINOR #include //register_chrdev_region, file_operations #include #include //container_of #include //kmalloc #include //struct cdev #include #include //copy_from/to_user() #include //error code ssize_t echo_read(struct file *, char __user *, size_t, loff_t *); ssize_t echo_write(struct file *, const char __user *, size_t, loff_t *); int echo_open(struct inode *, struct file *); int echo_release(struct inode *, struct file *); struct echo_cdev { char *data; unsigned long size; //amount of data stored struct semaphore sem; struct cdev cdev; }; MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("amit"); int nr_major; module_param(nr_major, int, S_IRUGO); MODULE_PARM_DESC(nr_major, "major number"); int nr_minor; char *chrdev_name = "echo"; static dev_t device; static int echo_dev_count = 1; struct echo_cdev *echo_dev = NULL; static struct file_operations echo_fs_ops = { .open = echo_open, .release = echo_release, .read = echo_read, .write = echo_write, .owner = THIS_MODULE, }; int echo_open(struct inode *inode, struct file *filp) { struct echo_cdev *dev; pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); //container_of(pointer, container_type, container_field); dev = container_of(inode->i_cdev, struct echo_cdev, cdev); filp->private_data = dev; if ((filp->f_flags & O_ACCMODE) == O_WRONLY) { //trim the device size to 0 dev->size = 0; } return 0; } int echo_release(struct inode *inode, struct file *filp) { return 0; } ssize_t echo_read(struct file *filp, char __user *ubuff, size_t count, loff_t *poffset) { struct echo_cdev *dev = filp->private_data; pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); //user trying to access an offset which is beyond the end of file if (down_interruptible(&dev->sem)) return -ERESTARTSYS; if (*poffset >= dev->size) { up(&dev->sem); return 0; } //user trying to access more than eof, return bytes read till the eof if (*poffset + count >= dev->size) //count = dev->size - *poffset; count = dev->size; //kspace --> uspace if (copy_to_user(ubuff, (dev->data + *poffset), count) < 0) { up(&dev->sem); return -EFAULT; } //update the offset *poffset += count; up(&dev->sem); return count; } //count is the size of requested data transfer ssize_t echo_write(struct file *filp, const char __user *ubuff, size_t count, loff_t *poffset) { int ret; struct echo_cdev *dev = filp->private_data; pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags); if (down_interruptible(&dev->sem)) return -ERESTARTSYS; if (dev->data == NULL) { dev->data = (char *)kmalloc(count, GFP_KERNEL); if (!dev->data) { up(&dev->sem); return -ENOMEM; } else { memset(dev->data, 0, sizeof(count)); } } dev->size = count; //uspace --> kspace if (copy_from_user(dev->data, ubuff, count) < 0) { up(&dev->sem); return -EFAULT; } *poffset += count; ret = count; if (dev->size < *poffset) dev->size = *poffset; //Force lock error //up(&dev->sem); return ret; } static int __init echo_init(void) { int ret; printk(KERN_EMERG "entering %s\n",__FUNCTION__); //le
Re: Tools for checking incorrect usage of locking techniques in k-space
Hi, On Sun, Oct 28, 2012 at 12:36 AM, Kumar amit mehta wrote: > Thank you Srivatsa. It seems that lockdep framework is enabled on my running > kernel. > > > amit@ubuntu:/boot$ egrep -i "debug_kernel|lockdep" config-3.2.0-29-generic-pae > CONFIG_LOCKDEP_SUPPORT=y > CONFIG_DEBUG_KERNEL=y You'll need CONFIG_LOCKDEP=y as well. An easy way to configure lock debugging checks is to run 'make menuconfig' and enable the required options under the "Kernel hacking" section. Regards, Srivatsa S. Bhat > > > If above configuration is all that I need, then should I be seeing > warning/error > messages in kernel logs(/var/log/kern.log) when there is inconsistency in > locking ? To test my hypothesis, I modified my simple kernel module to > deliberately induce locking error (After initializing read-write semaphore, I > call > down_write() and do not free this semaphore lock by commenting out up_write() > invocation). But still I don't see any error or warning message trace in > kernel > logs, I think, I'm missing something. ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Tools for checking incorrect usage of locking techniques in k-space
On Sat, Oct 27, 2012 at 09:26:51PM +0530, Srivatsa Bhat wrote: > On Sat, Oct 27, 2012 at 10:11 AM, Kumar amit mehta > wrote: > > Hi, > > > > I recently came across this tool called KEDR[1] for checking memory leak in > > kernel modules. I'm using it to check If my trivial kernel modules (based on > > LDD3 examples) are leaking memory. I was wondering if there exist a similar > > tool for checking incorrect usage of locking techniques implemented by > > kernel > > module. > > > > You can make use of the in-kernel "lockdep" framework to check your locking. > Just enable it in your .config, and build and boot your kernel. Lockdep > detects > a variety of locking problems at run time and warns you if there is a > possibility > of deadlock etc. > > Regards, > Srivatsa S. Bhat Thank you Srivatsa. It seems that lockdep framework is enabled on my running kernel. amit@ubuntu:/boot$ egrep -i "debug_kernel|lockdep" config-3.2.0-29-generic-pae CONFIG_LOCKDEP_SUPPORT=y CONFIG_DEBUG_KERNEL=y If above configuration is all that I need, then should I be seeing warning/error messages in kernel logs(/var/log/kern.log) when there is inconsistency in locking ? To test my hypothesis, I modified my simple kernel module to deliberately induce locking error (After initializing read-write semaphore, I call down_write() and do not free this semaphore lock by commenting out up_write() invocation). But still I don't see any error or warning message trace in kernel logs, I think, I'm missing something. I've just now found out that there is a tutorial on runtime locking correctness validator[1] under Documentation. I'll go through it. On the other hand, I found this KEDR utility very simple and accurate in finding out the erroneous memory handling. For example, this is how the KEDR reports memory leakage in my kernel module: 2670043 Oct 27 06:32:38 ubuntu kernel: [16963.930621] Inside echo_exit: kfree() 2670044 Oct 27 06:32:38 ubuntu kernel: [16963.930886] [leak_check] LeakCheck has detected possible memory leaks: 2670045 Oct 27 06:32:38 ubuntu kernel: [16963.930897] [leak_check] Address: 0xeeafbb70, size: 9; stack trace of the allocation: 2670046 Oct 27 06:32:38 ubuntu kernel: [16963.930907] [leak_check] [] echo_write+0x42/0xd0 [echo] 2670047 Oct 27 06:32:38 ubuntu kernel: [16964.001413] [leak_check] [] vfs_write+0x8f/0x160 2670048 Oct 27 06:32:38 ubuntu kernel: [16964.001421] [leak_check] [] sys_write+0x3d/0x70 2670049 Oct 27 06:32:38 ubuntu kernel: [16964.002214] [leak_check] [] sysenter_do_call+0x12/0x28 2670050 Oct 27 06:32:38 ubuntu kernel: [16964.002238] [leak_check] [] 0x 2670051 Oct 27 06:32:38 ubuntu kernel: [16964.002242] [leak_check] 2670052 Oct 27 06:32:38 ubuntu kernel: [16964.002248] [leak_check] Totals: 2670053 Oct 27 06:32:38 ubuntu kernel: [16964.002251] [leak_check] Allocations: 3 2670054 Oct 27 06:32:38 ubuntu kernel: [16964.002255] [leak_check] Possible leaks: 1 2670055 Oct 27 06:32:38 ubuntu kernel: [16964.002257] [leak_check] Unallocated frees: 0 2670056 Oct 27 06:32:38 ubuntu kernel: [16964.002260] [leak_check] end of LeakCheck report As you can see that with reports generated by KEDR like above, it becomes easy to detect the memory leak and therefore I'm looking at something similar like KEDR for detecting locking errors in kernel module. -Amit [1] http://lxr.linux.no/#linux+v3.6.3/Documentation/lockdep-design.txt ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Tools for checking incorrect usage of locking techniques in k-space
Hi, On Sat, Oct 27, 2012 at 10:11 AM, Kumar amit mehta wrote: > Hi, > > I recently came across this tool called KEDR[1] for checking memory leak in > kernel modules. I'm using it to check If my trivial kernel modules (based on > LDD3 examples) are leaking memory. I was wondering if there exist a similar > tool for checking incorrect usage of locking techniques implemented by kernel > module. > You can make use of the in-kernel "lockdep" framework to check your locking. Just enable it in your .config, and build and boot your kernel. Lockdep detects a variety of locking problems at run time and warns you if there is a possibility of deadlock etc. Regards, Srivatsa S. Bhat ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies