title: How to use copy_to_user
body: <p>I'm trying to add a custom system call into the linux kernel. Here is a simple code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;linux/mysyscall.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;asm/uaccess.h&gt;
  #include &lt;asm/system.h&gt;
  
  asmlinkage int sys_mysyscall(int *data){
  
      int a = 3;
  
      cli();
      copy_to_user(data, &amp;a, 1);
      sti();
  
      printk(KERN_EMERG "Called with %d\n", a);
  
      return a;
  }
  </code></pre>
  
  <p>I can compile a kernel with mysyscall added and when I try to access it with a user program like:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;linux/mysyscall.h&gt;
  
  int main(void){
  
      int *data;
      int r;
      int a = 0;
      data = &amp;a;
  
      r = mysyscall(data);
  
      printf("r is %d and data is %d", r, *data);
  }
  </code></pre>
  
  <p>*data does not equal to 3 it equals to 0.</p>
  
  <p>How should I use copy_to_user to fix it?</p>