[英]Allocating memory for user space from kernel thread
My question is about passing data from kernel to a user space program.我的问题是关于将数据从内核传递到用户空间程序。 I want to implement a system call "get_data(size, char *buff, char **meta_buf)".
我想实现一个系统调用“get_data(size, char *buff, char **meta_buf)”。 In this call, buff is allocated by user space program and its length is passed in the size argument.
在这个调用中, buff 由用户空间程序分配,其长度在 size 参数中传递。 However, meta_buf is a variable length buffer that is allocated (in the user space program's vm pages) and filled by kernel.
然而,meta_buf 是一个可变长度的缓冲区,由内核分配(在用户空间程序的 vm 页中)并填充。 User space program will free this region.
用户空间程序将释放该区域。
(I cant allocate data in user space as user space program does not know size of the meta_buff. Also, user space program cannot allocate a fixed amount of memory and call the system call again and again to read the entire meta data. meta_data has to be returned in one single system call) (我不能在用户空间分配数据,因为用户空间程序不知道 meta_buff 的大小。另外,用户空间程序不能分配固定数量的内存并一遍又一遍地调用系统调用来读取整个元数据。meta_data 必须在一个系统调用中返回)
Don't attempt to allocate memory for userspace from the kernel - this is a huge violation of the kernel's abstraction layering. 不要试图从内核为用户空间分配内存 - 这严重违反了内核的抽象分层。 Instead, consider a few other options:
相反,请考虑其他一些选项:
It's hard to say more without knowing why this has to be atomic. 如果不知道为什么这必须是原子的,就很难说更多。 Actually allocating memory's going to need to be interruptible anyway (or you're unlikely to succeed), so it's unlikely that going out of and back into the kernel is going to hurt much.
实际上,分配内存无论如何都需要中断(或者你不太可能成功),因此不太可能走出内核并重新受到伤害。 In fact, any write to userspace memory must be interruptible, as there's the potential for page faults requiring IO.
事实上, 任何对用户空间内存的写入都必须是可中断的,因为存在需要IO的页面错误的可能性。
There is a point to take care, that is, the mapped address should be aligned to one page size.有一点需要注意,就是映射的地址应该与一个页面大小对齐。 If not, you should ajust it, and add the offset after you get the user space address.
如果不是,你应该调整它,并在获得用户空间地址后添加偏移量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.