<html><body><div style="color:#000; background-color:#fff; font-family:lucida console, sans-serif;font-size:10pt"><div><span></span> </div><div><span>Hi All,</span></div><div><span></span> </div><div><span>While building the new kernel their are many option for kernel debugging </span></div><div><span></span> </div><div><span>please refer the below link.</span></div><div><span></span> </div><div><span><a href="http://my.safaribooksonline.com/book/operating-systems-and-server-administration/linux/9780137072446/kernel-hacking-config-options/ch11">http://my.safaribooksonline.com/book/operating-systems-and-server-administration/linux/9780137072446/kernel-hacking-config-options/ch11</a></span></div><div><span></span> </div><div><span>Hope this will help you catch the lock problem.</span></div><div> </div><div>-Anand Moon</div> <div style="font-family: lucida console, sans-serif; font-size: 10pt;"> <div style="font-family: times
new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> <div style="margin: 5px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); height: 0px; line-height: 0; font-size: 0px;" class="hr" contentEditable="false" readonly="true"></div> <b><span style="font-weight: bold;">From:</span></b> Vladimir Murzin <murzin.v@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> Kumar amit mehta <gmate.amit@gmail.com> <br><b><span style="font-weight: bold;">Cc:</span></b> Srivatsa Bhat <bhat.srivatsa@gmail.com>; kernelnewbies@kernelnewbies.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Monday, November 5, 2012 11:21 AM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: Tools for checking incorrect usage of locking techniques in k-space<br> </font> </div> <br>
On Tue, Oct 30, 2012 at 9:41 AM, Kumar amit mehta <<a href="mailto:gmate.amit@gmail.com" ymailto="mailto:gmate.amit@gmail.com">gmate.amit@gmail.com</a>> wrote:<br>> On Mon, Oct 29, 2012 at 12:03:08AM +0530, Srivatsa Bhat wrote:<br>>> You'll need CONFIG_LOCKDEP=y as well. An easy way to configure lock debugging<br>>> checks is to run 'make menuconfig' and enable the required options under the<br>>> "Kernel hacking" section.<br>>><br>>> ><br>>> > If above configuration is all that I need, then should I be seeing warning/error<br>>> > messages in kernel logs(/var/log/kern.log) when there is inconsistency in<br>>> > locking ? To test my hypothesis, I modified my simple kernel module to<br>>> > deliberately induce locking error (After initializing read-write semaphore, I call<br>>> > down_write() and do not free this semaphore lock by commenting out up_write()<br>>>
> invocation). But still I don't see any error or warning message trace in kernel<br>>> > logs, I think, I'm missing something.<br>><br>> Hi Srivatsa,<br>><br>> Thank you for your mail. As per your suggestion, this time I've enabled<br>> CONFIG_LOCKDEP aslo in my running kernel and did the same experiment, but still<br>> I dont't see any warning/error messages in the kernel log. To give you more idea<br>> about what I'm doing, Please see the code below.(This is a simple char driver<br>> based on LDD3 examples)<br>><br>> <echo.c><br>><br>> #include <linux/module.h><br>> #include <linux/init.h><br>> #include <linux/types.h> //MAJOR, MINOR<br>> #include <linux/fs.h> //register_chrdev_region, file_operations<br>> #include <linux/moduleparam.h><br>> #include <linux/kernel.h> //container_of<br>> #include <linux/slab.h> //kmalloc<br>> #include
<linux/cdev.h> //struct cdev<br>> #include <linux/version.h><br>> #include <linux/uaccess.h> //copy_from/to_user()<br>> #include <linux/errno.h> //error code<br>><br>> ssize_t echo_read(struct file *, char __user *, size_t, loff_t *);<br>> ssize_t echo_write(struct file *, const char __user *, size_t, loff_t *);<br>> int echo_open(struct inode *, struct file *);<br>> int echo_release(struct inode *, struct file *);<br>><br>> struct echo_cdev {<br>> char *data;<br>> unsigned long size; //amount of data stored<br>> struct semaphore sem;<br>> struct cdev cdev;<br>> };<br>> MODULE_LICENSE("GPL v2");<br>> MODULE_AUTHOR("amit");<br>><br>> int nr_major;<br>> module_param(nr_major, int, S_IRUGO);<br>> MODULE_PARM_DESC(nr_major, "major number");<br>><br>> int
nr_minor;<br>> char *chrdev_name = "echo";<br>><br>> static dev_t device;<br>> static int echo_dev_count = 1;<br>> struct echo_cdev *echo_dev = NULL;<br>><br>> static struct file_operations echo_fs_ops = {<br>> .open = echo_open,<br>> .release = echo_release,<br>> .read = echo_read,<br>> .write = echo_write,<br>> .owner = THIS_MODULE,<br>> };<br>><br>> int echo_open(struct inode *inode, struct file *filp)<br>> {<br>> struct echo_cdev *dev;<br>> pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags);<br>> //container_of(pointer, container_type, container_field);<br>> dev = container_of(inode->i_cdev, struct echo_cdev, cdev);<br>>
filp->private_data = dev;<br>> if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {<br>> //trim the device size to 0<br>> dev->size = 0;<br>> }<br>> return 0;<br>> }<br>><br>> int echo_release(struct inode *inode, struct file *filp)<br>> {<br>> return 0;<br>> }<br>><br>> ssize_t echo_read(struct file *filp, char __user *ubuff, size_t count, loff_t *poffset)<br>> {<br>> struct echo_cdev *dev = filp->private_data;<br>> pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags);<br>><br>> //user trying to access an offset which is beyond the end of file<br>>
if (down_interruptible(&dev->sem))<br>> return -ERESTARTSYS;<br>> if (*poffset >= dev->size) {<br>> up(&dev->sem);<br>> return 0;<br>> }<br>><br>> //user trying to access more than eof, return bytes read till the eof<br>> if (*poffset + count >= dev->size)<br>> //count = dev->size - *poffset;<br>> count = dev->size;<br>> //kspace --> uspace<br>> if (copy_to_user(ubuff, (dev->data + *poffset), count) < 0)
{<br>> up(&dev->sem);<br>> return -EFAULT;<br>> }<br>> //update the offset<br>> *poffset += count;<br>> up(&dev->sem);<br>> return count;<br>> }<br>><br>> //count is the size of requested data transfer<br>> ssize_t echo_write(struct file *filp, const char __user *ubuff, size_t count, loff_t *poffset)<br>> {<br>> int ret;<br>> struct echo_cdev *dev = filp->private_data;<br>> pr_debug("%s: f_flags: 0x%x\n",__FUNCTION__,filp->f_flags);<br>> if (down_interruptible(&dev->sem))<br>>
return -ERESTARTSYS;<br>> if (dev->data == NULL) {<br>> dev->data = (char *)kmalloc(count, GFP_KERNEL);<br>> if (!dev->data) {<br>> up(&dev->sem);<br>> return -ENOMEM;<br>> } else {<br>> memset(dev->data, 0, sizeof(count));<br>> }<br>> }<br>> dev->size = count;<br>> //uspace --> kspace<br>>
if (copy_from_user(dev->data, ubuff, count) < 0) {<br>> up(&dev->sem);<br>> return -EFAULT;<br>> }<br>><br>> *poffset += count;<br>> ret = count;<br>><br>> if (dev->size < *poffset)<br>> dev->size = *poffset;<br>> //Force lock error<br>> //up(&dev->sem);<br>> return ret;<br>> }<br>><br>> static int __init echo_init(void)<br>> {<br>> int ret;<br>> printk(KERN_EMERG "entering %s\n",__FUNCTION__);<br>> //let
the user provide the major number.<br>> if (nr_major) {<br>> device = MKDEV(nr_major, nr_minor);<br>> if ((ret = register_chrdev_region(device, echo_dev_count, chrdev_name)) < 0) {<br>> pr_debug("%s: failed to register %s\n",__FUNCTION__,<br>> chrdev_name);<br>> return ret;<br>> }<br>> } else {<br>> ret = alloc_chrdev_region(&device, 0,
echo_dev_count, chrdev_name);<br>> if (ret < 0) {<br>> pr_debug("%s: failed to register %s\n",__FUNCTION__,<br>> chrdev_name);<br>> return ret;<br>> }<br>> }<br>> nr_major = MAJOR(device);<br>> nr_minor = MINOR(device);<br>> //print the major and minor numbers<br>> pr_debug("%s: major/minor:: %d/%d\n",__FUNCTION__,<br>>
nr_major, nr_minor);<br>> echo_dev = (struct echo_cdev *)kmalloc(sizeof(struct echo_cdev), GFP_KERNEL);<br>> if (!echo_dev) {<br>> printk(KERN_EMERG "Not enough memory\n");<br>> unregister_chrdev_region(device, echo_dev_count);<br>> return -ENOMEM;<br>> }<br>> memset(echo_dev, 0, sizeof(struct echo_cdev));<br>> echo_dev->cdev.owner = THIS_MODULE;<br>> echo_dev->cdev.ops = &echo_fs_ops;<br>> //initialize the semaphore, before it is presented to the world<br>>
sema_init(&echo_dev->sem,1);<br>> cdev_init(&echo_dev->cdev, &echo_fs_ops);<br>> device = MKDEV(nr_major, nr_minor);<br>> //tell the kernel about this char device<br>> //telling the VFS layer to associate echo driver's fs operation for file r/w etc.<br>> ret = cdev_add(&echo_dev->cdev, device, 1);<br>> if (ret) {<br>> kfree(echo_dev);<br>> unregister_chrdev_region(device, echo_dev_count);<br>> return ret;<br>> }<br>> return 0;<br>> }<br>><br>> static void __exit echo_exit(void)<br>> {<br>>
printk(KERN_EMERG "entering %s\n",__FUNCTION__);<br>> if (echo_dev->data) {<br>> printk("Inside %s: kfree()\n",__FUNCTION__);<br>> kfree(echo_dev->data);<br>> }<br>> cdev_del(&echo_dev->cdev);<br>> kfree(echo_dev);<br>> unregister_chrdev_region(device, echo_dev_count);<br>> }<br>><br>> module_init(echo_init);<br>> module_exit(echo_exit);<br>><br>> <echo.c><br>><br>> <Makefile><br>> obj-m := echo.o<br>> CFLAGS_echo.o := -DDEBUG -Wall -Werror -Wmissing-prototypes \<br>> -Wstrict-prototypes -Wunused-variable -O2 \<br>> -Wunused-function
-g<br>><br>> modules:<br>> make -C /lib/modules/`uname -r`/build M=`pwd`<br>> clean:<br>> make -C /lib/modules/`uname -r`/build M=`pwd` clean<br>> <Makefile><br>><br>> Logs:<br>> root@ubuntu:/boot# egrep -i "debug_kernel|lockdep"<br>> config-3.7.0-rc3-next-20121029<br>> CONFIG_LOCKDEP_SUPPORT=y<br>> CONFIG_DEBUG_KERNEL=y<br>> CONFIG_LOCKDEP=y<br>> # CONFIG_DEBUG_LOCKDEP is not set<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# uname -a<br>> Linux ubuntu 3.7.0-rc3-next-20121029 #1 SMP Mon Oct 29 21:42:20 PDT 2012 i686<br>> i686 i386 GNU/Linux<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# dmesg<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# insmod echo.ko<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# dmesg<br>> [ 1156.704072] entering echo_init<br>> [ 1156.704914] echo_init:
major/minor:: 249/0<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# mknod /dev/echo c 249 0<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# ll /dev/echo<br>> crw-r--r-- 1 root root 249, 0 Oct 29 22:29 /dev/echo<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# echo "kernel newbiee" >/dev/echo<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# dmesg<br>> [ 1156.704072] entering echo_init<var id="yui-ie-cursor"></var><br>> [ 1156.704914] echo_init: major/minor:: 249/0<br>> [ 1237.377205] echo_open: f_flags: 0x8241<br>> [ 1237.377392] echo_write: f_flags: 0x8001<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# cat /dev/echo<br>> --> Here the driver read routine gets stuck as I've not reliquished the<br>> semaphore lock<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# cat /dev/echo<br>> ^C <-- killing the user process<br>><br>>
root@ubuntu:/home/amit/ldd3/misc-modules/echo# rmmod echo<br>><br>> root@ubuntu:/home/amit/ldd3/misc-modules/echo# dmesg<br>> [ 1156.704072] entering echo_init<br>> [ 1156.704914] echo_init: major/minor:: 249/0<br>> [ 1237.377205] echo_open: f_flags: 0x8241<br>> [ 1237.377392] echo_write: f_flags: 0x8001<br>> [ 1270.622337] echo_open: f_flags: 0x8000<br>> [ 1270.622477] echo_read: f_flags: 0x8000<br>> [ 1394.180962] entering echo_exit<br>> [ 1394.180996] Inside echo_exit: kfree()<br>><br>> As you can see, no warning/error messages reported by kernel.<br>> -Amit<br>><br>> _______________________________________________<br>> Kernelnewbies mailing list<br>> <a href="mailto:Kernelnewbies@kernelnewbies.org" ymailto="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>> <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies"
target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br><br>Hi<br><br>I've not tried your module, but I suppose that you should get warning<br>as soon as you try to take write semaphore once again.<br><br>Best wishes<br>Vladimir Murzin<br><br>_______________________________________________<br>Kernelnewbies mailing list<br><a href="mailto:Kernelnewbies@kernelnewbies.org" ymailto="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br><a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br><br><br> </div> </div> </div></body></html>