简体   繁体   English

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

[英]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. 我正在linux-3.7.6 / kernel / sched / core.c上工作,在schedule()函数中,我必须记录进程的pid和tgid并向用户空间显示记录的值。 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. 我在存储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? 我之前使用过copy_to_user,但是由于将copy_to_user以块形式复制数据时如何发送这些整个值集停留在这里? 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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