<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><br><br>--- On <b>Thu, 23/12/10, mukti jain <i>&lt;muktijn@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: mukti jain &lt;muktijn@gmail.com&gt;<br>Subject: Re: copy_to_user<br>To: "Srinivas G." &lt;srinivasg@esntechnologies.co.in&gt;<br>Cc: "Dexter Haslem" &lt;dexter.haslem@gmail.com&gt;, "Hemanth Kumar" &lt;hemwire@yahoo.co.in&gt;, Kernelnewbies@kernelnewbies.org<br>Date: Thursday, 23 December, 2010, 11:07 AM<br><br><div id="yiv811986852"><br><br><div class="yiv811986852gmail_quote">On Thu, Dec 23, 2010 at 10:05 AM, Srinivas G. <span dir="ltr">&lt;<a rel="nofollow" ymailto="mailto:srinivasg@esntechnologies.co.in" target="_blank" href="/mc/compose?to=srinivasg@esntechnologies.co.in">srinivasg@esntechnologies.co.in</a>&gt;</span> wrote:<br><blockquote
 class="yiv811986852gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="yiv811986852im">&gt; On 12/22/2010 5:59 PM, Hemanth Kumar wrote:<br>
&gt; &gt; Hi All,<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I have small problem with copy_to_user in read<br>
&gt; function,below is my code,when I try to read from userspace I get<br>
&gt; segmentation fault,<br>
&gt; &gt; Can any please point me where I went wrong,<br>
<br>
</div>I guess, you need to also implement open method in your driver. Because,<br>
you are opening the device in your application. Could you try this?<br>
<br>
Regards,<br>
<font color="#888888">Srinivas G<br>
</font><div><div></div><div class="yiv811986852h5"><br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; #include&lt;linux/kernel.h&gt;<br>
&gt; &gt; #include&lt;linux/module.h&gt;<br>
&gt; &gt; #include&lt;linux/init.h&gt;<br>
&gt; &gt; #include&lt;linux/types.h&gt;<br>
&gt; &gt; #include&lt;linux/proc_fs.h&gt;<br>
&gt; &gt; #include&lt;linux/fs.h&gt;<br>
&gt; &gt; #include&lt;linux/kdev_t.h&gt;<br>
&gt; &gt; #include&lt;linux/jiffies.h&gt;<br>
&gt; &gt; #include&lt;linux/cdev.h&gt;<br>
&gt; &gt; #include&lt;asm/uaccess.h&gt;<br>
&gt; &gt; #include&lt;linux/mutex.h&gt;<br>
&gt; &gt;<br>
&gt; &gt; struct mutex timer;<br>
&gt; &gt; static struct cdev my_cdev;<br>
&gt; &gt; dev_t devn;<br>
&gt; &gt; int maj = 300;<br>
&gt; &gt; int min = 0;<br>
&gt; &gt; int count = 1;<br>
&gt; &gt; char modname[] = "mytimer";<br>
&gt; &gt; short x[10] = {1,2,3,4,5,6,7,8,9,10};<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; ssize_t my_read(struct file *file,char *buf,size_t count,loff_t<br>
&gt; *pos){<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; unsigned long res;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; void *k = (void *)&amp;x;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;void *l = (void *)&amp;x+1;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;void *j = (void *)&amp;x+2;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mutex_lock(&amp;timer);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,k,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,l,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,j,sizeof(short));<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; &nbsp;res =<br>
&gt; copy_to_user(buf,&amp;x+4,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,&amp;x+5,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,&amp;x+6,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,&amp;x+7,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,&amp;x+8,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res =<br>
&gt; copy_to_user(buf,&amp;x+9,sizeof(short));<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mutex_unlock(&amp;timer);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; return 20;<br>
&gt; &gt;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; static struct file_operations my_fops = {<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .owner = THIS_MODULE,<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .read = my_read,<br>
&gt; &gt;<br>
&gt; &gt; };<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; static int __init my_init(void){<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int ret;<br>
&gt; &gt; &nbsp; &nbsp; devn = MKDEV(maj,min);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; ret = register_chrdev_region(devn,count,modname);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;cdev_init(&amp;my_cdev,&amp;my_fops);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;cdev_add(&amp;my_cdev,devn,count);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;printk("&lt;1&gt; &nbsp;Register timer maj = %d\n",maj);<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; return 0;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; static void __exit my_exit(void){<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;cdev_del(&amp;my_cdev);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; unregister_chrdev_region(devn,count);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printk("&lt;1&gt; &nbsp;Bye Bye \n");<br>
&gt; &gt;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; module_init(my_init);<br>
&gt; &gt; module_exit(my_exit);<br>
&gt; &gt; MODULE_LICENSE("Dual BSD/GPL");<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; my userspace App:<br>
&gt; &gt;<br>
&gt; &gt; #include&lt;stdio.h&gt;<br>
&gt; &gt; #include&lt;fcntl.h&gt;<br>
&gt; &gt; #include&lt;stdlib.h&gt;<br>
&gt; &gt; #include&lt;unistd.h&gt;<br>
&gt; &gt;<br>
&gt; &gt; int main()<br>
&gt; &gt; {<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nbytes ;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char n[20];<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short a = *((short *)&amp;n[0]);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short b = *((short *)&amp;n[2]);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short c = *((short *)&amp;n[4]);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int &nbsp; &nbsp; fd = open( "/dev/mytimer", O_RDONLY );<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( fd&lt; &nbsp;0 ) { perror( "/dev/mytimer" ); exit(1); }<br>
&gt; &gt;<br>
&gt; &gt; while ( 1 )<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nbytes = read( fd, n, 40 );<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( nbytes&lt; &nbsp;0 ) break;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf( "\r a = %d \n ", a);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\r b = %d \n",b);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\r c = %d \n",c);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(1);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fflush( stdout );<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt; return 0;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Best regards,<br>
&gt; &gt;<br>
&gt; &gt;<br></div></div></blockquote><div>The mutex initialization is missing. <br>Adding&nbsp; mutex_init(&amp;timer); in the driver init will make it work.<br><br>Thanks,<br>Mukti<br></div><br>Hi All,<br><br>&nbsp;&nbsp;&nbsp; Can anybody please share some idea ,why I am getting segmentation fault &amp; kernel oops,<br><br>Regards,<br></div><br>
</div></blockquote></td></tr></table><br>