从内核空间将地址传递给用户空间

时间:2022-04-23 17:50:08

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.

我正在使用linux-3.7.6 / kernel / sched / core.c,在schedule()函数中,我必须记录进程的pid和tgid,并且必须向用户空间显示记录的值。我在内核空间中获取了全局数组结构,我正在存储tgid和pid,我想是否可以将数组的地址传递给用户空间,然后在用户空间访问tgid和pid的值。

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!

有没有办法可以一次性将存储在结构数组中的所有数据发送到用户空间?我之前使用过copy_to_user,但只是因为copy_to_user以块的形式复制数据而如何发送这些整个值?如果有人能给我指示如何继续,我真的很感激。谢谢!

1 个解决方案

#1


1  

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));

#1


1  

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));