简体   繁体   中英

Passing Address to user space from Kernel space

I am working on linux-3.7.6/kernel/sched/core.c where in schedule() function I have to record the pid's and tgid's for processes and have to show the recorded values to user space. I took global array of structs in kernel space where I am storing tgid and pid's and I was thinking if I could just pass the address of the array to user space and then access the values of tgid and pid at user space.

typedef struct process{
int pid;
int tgid;
}p;

p proc[100];

Is there a way I can send all the data stored in array of structs to user space in one shot? I have used copy_to_user before but just stuck here as how to send these entire set of values as copy_to_user copies data in form of blocks? I would really appreciate if somebody could give me directions as how to go ahead. Thanks!

I assume you want keep atomicity while copying your array to user-level.

A easy way is:

 p local_array[100];

preemption_disable();   //disable preemption so you array content will not change,
                        //because no schedule() can be executed at this time.

memcpy(local_array, array, sizeof(array));   //then we get the consistent snapshot of
                                             //array.
preemption_enable();

copy_to_user(user_buff_ptr, local_array, sizeof(array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM