6

I am developing a kernel application which involves kthreads. I create an array of structure and allocate memory using malloc in user-space. Then I call a system call (which I implemented) and pass the address of array to kernel-space. In the handler of system-call I create I create 2 kthreads which will monitor the array. kthread can change some value and user-space threads can also change some values. The idea is to use the array as a shared memory. But some when I access the memory in kernel space (using copy_from_user) the data are somehow changed. I can verify that the address are same when it was assigned and in kernel. But when using copy_from_user it is giving various values like garbage values.

Also is the following statement ok?

int kthread_run_function(void* data){
    struct entry tmp;
    copy_from_user(&tmp, data, sizeof(struct entry));
}

2 Answers 2

9

This is not OK because copy_from_user() copies from the current user process (which should be obvious, since there's no way to tell it which user process to copy from).

In a syscall invoked by your userspace process this is OK, because the current process is your userspace process. However, within the kernel thread the current process could be any other process on the system - so you're copying from a random process's memory, which is why you get garbage.

If you want to share memory between the kernel and a userspace process, the right way to do this is to have the kernel allocate it, then allow the userspace process to map it into its address space with mmap(). The kernel thread and the userspace process will use different pointers to refer to the memory region - the kernel thread will use a pointer to the memory allocated within the kernel address space, and the userspace process will use a pointer to the memory region returned by mmap().

Sign up to request clarification or add additional context in comments.

2 Comments

You are exactly right. I was thinking of saving context from the syscall and using this context for copy_from_user but I guess it might be not appropriate. I was also thinking of mmap. Can you provide me an example which works in Linux kernel 2.6.
@max Please did you find an example for that ? I am stuck in the same problem and did not find anything how allocate memory from kernel space and map it from user space using mmap
1

No, generally it's not OK since data is kernel virtual address, not a user virtual address.

However, IFF you called kthread_create with the data argument equal to an __user pointer, this should be ok.

2 Comments

I tried this but, not working. I got into trouble when accessing the user space pointer from kernel space kthread. I can access the memory in system call function but in the kthread function I can not access it. My simple query is how to get shared memory between kthreads and user-space.
You can't just stuff a user pointer into a kernel thread. It means nothing in the context of another task than the user process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.