All of lore.kernel.org
 help / color / mirror / Atom feed
* copy_to_user
@ 2010-12-23  0:59 Hemanth Kumar
  2010-12-23  1:18 ` copy_to_user Dexter Haslem
  0 siblings, 1 reply; 24+ messages in thread
From: Hemanth Kumar @ 2010-12-23  0:59 UTC (permalink / raw)
  To: kernelnewbies

Hi All,

           I have small problem with copy_to_user in read function,below is my code,when I try to read from userspace I get segmentation fault,
Can any please point me where I went wrong,


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/jiffies.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/mutex.h>

struct mutex timer;
static struct cdev my_cdev;
dev_t devn;
int maj = 300;
int min = 0;
int count = 1;
char modname[] = "mytimer";
short x[10] = {1,2,3,4,5,6,7,8,9,10};




ssize_t my_read(struct file *file,char *buf,size_t count,loff_t *pos){
     unsigned long res;
     void *k = (void *)&x;
          void *l = (void *)&x+1;
          void *j = (void *)&x+2;

                     mutex_lock(&timer);
                             res =    copy_to_user(buf,k,sizeof(short));
                             res =    copy_to_user(buf,l,sizeof(short));
                             res =    copy_to_user(buf,j,sizeof(short));
                    
                       /*    res =  copy_to_user(buf,&x+4,sizeof(short));
                             res =    copy_to_user(buf,&x+5,sizeof(short));
                             res =    copy_to_user(buf,&x+6,sizeof(short));
                             res =    copy_to_user(buf,&x+7,sizeof(short));
                             res =    copy_to_user(buf,&x+8,sizeof(short));
                             res =    copy_to_user(buf,&x+9,sizeof(short));
                        */
                 mutex_unlock(&timer);

   return 20;

}


static struct file_operations my_fops = {
                 .owner = THIS_MODULE,
                 .read = my_read,

};


static int __init my_init(void){
        int ret;
   devn = MKDEV(maj,min);

     ret = register_chrdev_region(devn,count,modname);

      cdev_init(&my_cdev,&my_fops);
      cdev_add(&my_cdev,devn,count);

      printk("<1> Register timer maj = %d\n",maj);




 return 0;
}



static void __exit my_exit(void){

      cdev_del(&my_cdev);
       unregister_chrdev_region(devn,count);
        printk("<1> Bye Bye \n");

}


module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("Dual BSD/GPL");




my userspace App:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
         int nbytes ;      
         char n[20];
         short a = *((short *)&n[0]);
         short b = *((short *)&n[2]);
         short c = *((short *)&n[4]);
         
        int     fd = open( "/dev/mytimer", O_RDONLY );
        if ( fd < 0 ) { perror( "/dev/mytimer" ); exit(1); }

while ( 1 )
                {

                 nbytes = read( fd, n, 40 );
                if ( nbytes < 0 ) break;

                printf( "\r a = %d \n ", a);
                printf("\r b = %d \n",b);
                printf("\r c = %d \n",c);
                    
                sleep(1);
                fflush( stdout );
                }
return 0;
}




Best regards,

^ permalink raw reply	[flat|nested] 24+ messages in thread
[parent not found: <589679.60204.qm@web94707.mail.in2.yahoo.com>]
* Re: copy to user
@ 2001-11-25 21:58 Marco C. Mason
  0 siblings, 0 replies; 24+ messages in thread
From: Marco C. Mason @ 2001-11-25 21:58 UTC (permalink / raw)
  To: umiguel, linux-kernel

Luis Henriques--

Before making my suggestion: Apologies to the list if this has already
been settled.  I'm trying to catch up on my LKML reading, and I'm only
up to Nov 20 so far...

Anyway:  Here's what I'd do, if I had to do such a apalling thing  8^)

Drop a function in your code something like:

_xyzzy:
    db 0x18, 0xfe    ; jr $

Then, when you detect the condition where you want to waste time, then
put the address of this function on top of the user stack (along with
whatever else in the stack frame is required) so that the code just sits
there burning CPU.  To clean it up, you'd simply restore the original
stack frame to the process.

It's hideous & gross, but if you need it.....

--marco



^ permalink raw reply	[flat|nested] 24+ messages in thread
* copy to user
@ 2001-11-20 20:54 Luis Miguel Correia Henriques
  2001-11-20 21:28 ` John Alvord
                   ` (4 more replies)
  0 siblings, 5 replies; 24+ messages in thread
From: Luis Miguel Correia Henriques @ 2001-11-20 20:54 UTC (permalink / raw)
  To: linux-kernel

The reason that I need it to spend CPU time is that I'm developing a fault
injector. The purpose of a fault injection tool is, as you could imagine,
to test some critical systems and it's capacity to recover from fails. The
reason for changing the code of a process is that process must be delayed
but without leaving the CPU - everything must look like nothing wrong is
happening, except for other processes that are waiting for something from
the delayed process...

Maybe I should have explained this before... sorry.

I suppose now you can understand why SIGSTOP won't work. Hope you can help
me :)

About using udelay... this soluction seemed fine to me at first but if I
hang the CPU with udelay the scheduler will no be doing it's job (isn't
it?). This would give me even more intrusiveness (another requirement: the
less intrusiveness as possible).

Isn't there any doubt that copy_to_user can handle my problem? When I use
it to change CS, this function returns the correct number of bytes (and no
error) but, when I try to read... the old data is still there. I suppose
there is a page/segment protection against writing to CS, isn't it?

Luis Henriques


^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2010-12-26  1:33 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-23  0:59 copy_to_user Hemanth Kumar
2010-12-23  1:18 ` copy_to_user Dexter Haslem
2010-12-23  2:15   ` copy_to_user Hemanth Kumar
2010-12-23  4:35   ` copy_to_user Srinivas G.
2010-12-23  5:18     ` copy_to_user Hemanth Kumar
2010-12-23  5:37     ` copy_to_user mukti jain
2010-12-23 12:21       ` copy_to_user Hemanth Kumar
2010-12-24  6:07       ` copy_to_user Hemanth Kumar
2010-12-24  7:59         ` copy_to_user Nilesh Tayade
2010-12-24  9:00           ` copy_to_user Dave Hylands
2010-12-24  9:20             ` copy_to_user Nilesh Tayade
2010-12-24 10:00               ` copy_to_user Hemanth Kumar
2010-12-24  9:26           ` copy_to_user Hemanth Kumar
     [not found] <589679.60204.qm@web94707.mail.in2.yahoo.com>
2010-12-24 18:31 ` copy_to_user Mulyadi Santosa
2010-12-26  1:33   ` copy_to_user Hemanth Kumar
  -- strict thread matches above, loose matches on Subject: below --
2001-11-25 21:58 copy to user Marco C. Mason
2001-11-20 20:54 Luis Miguel Correia Henriques
2001-11-20 21:28 ` John Alvord
2001-11-20 21:44 ` Andreas Dilger
2001-11-21 11:02   ` Luís Henriques
2001-11-20 22:02 ` n0ano
2001-11-20 22:05 ` Chris Wright
2001-11-21  8:43 ` Mathijs Mohlmann
2001-11-21 10:12   ` Jan Hudec

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.