copy_from_user和细分 [英] copy_from_user and segmentation

查看:116
本文介绍了copy_from_user和细分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读" Linux内核模块编程指南中的一段,我对以下段落有疑问.

I was reading a paragraph from the "The Linux Kernel Module Programming Guide" and I have a couple of doubts related to the following paragraph.

使用copy_from_user或get_user的原因是Linux内存(在 Intel架构,在某些其他处理器下可能有所不同) 被细分.这意味着指针本身并不引用 内存中的唯一位置,只有内存段中的位置,以及 您需要知道要使用哪个内存段. 内核有一个内存段,每个内存段都有一个 程序.

The reason for copy_from_user or get_user is that Linux memory (on Intel architecture, it may be different under some other processors) is segmented. This means that a pointer, by itself, does not reference a unique location in memory, only a location in a memory segment, and you need to know which memory segment it is to be able to use it. There is one memory segment for the kernel, and one for each of the processes.

不过,据我了解,Linux使用分页而不是分段,并且0xc0000000及更高版本的虚拟地址已包含内核映射.

However it is my understanding that Linux uses paging instead of segmentation and that virtual addresses at and above 0xc0000000 have the kernel mapping in.

  1. 我们是否使用copy_from_user来容纳较早的内核?
  2. 当前的Linux内核是否以任何方式使用分段?如果可以,怎么办?
  3. 如果(1)不正确,使用copy_from_user还有其他好处吗?

推荐答案

是的.我也不喜欢这种解释.从技术意义上讲,这些细节基本上是正确的(另请参见copy_from_user等实际上为什么存在,这不是很好的解释.

Yeah. I don't like that explanation either. The details are essentially correct in a technical sense (see also Why does Linux on x86 use different segments for user processes and the kernel?) but as you say, linux typically maps the memory so that kernel code could access it directly, so I don't think it's a good explanation for why copy_from_user, etc. actually exist.

IMO,使用copy_from_user/copy_to_user(和朋友)的主要原因仅仅是因为要检查许多事情(要防范的危险),并且将所有这些支票放在一个地方是很有意义的.您不希望每个需要在用户空间中复制数据和从其中复制数据的地方都必须重新实现所有这些检查.尤其是当细节可能因一种架构而异时.

IMO, the primary reason for using copy_from_user / copy_to_user (and friends) is simply that there are a number of things to be checked (dangers to be guarded against), and it makes sense to put all of those checks in one place. You wouldn't want every place that needs to copy data in and out from user-space to have to re-implement all those checks. Especially when the details may vary from one architecture to the next.

例如,当您需要向该内存复制或从该内存复制时,实际上可能不存在用户空间页面,因此,重要的是要从可以容纳页面错误的上下文进行调用(因此入睡).

For example, it's possible that a user-space page is actually not present when you need to copy to or from that memory and hence it's important that the call be made from a context that can accommodate a page fault (and hence being put to sleep).

此外,还需要仔细检查用户空间数据指针,以确保它们实际指向用户空间并且指向数据区域,并且复制长度不会超出有效区域的范围,等等.

Also, user-space data pointers need to be checked carefully to ensure that they actually point to user-space and that they point to data regions, and that the copy length doesn't wrap beyond the end of the valid regions, and so forth.

最后,用户空间实际上可能与内核共享相同的页面映射.曾经有一个针对32位x86的Linux补丁程序,该补丁程序为用户空间进程提供了完整的4G虚拟地址空间.在那种情况下,内核代码无法假设用户空间指针是可直接访问的,并且这些功能可能需要一次映射一个用户空间页面才能访问它们. (请参见 4GB/4GB内核VM拆分)

Finally, it's possible that user-space actually doesn't share the same page mappings with the kernel. There used to be a linux patch for 32-bit x86 that made the complete 4G of virtual address space available to user-space processes. In that case, kernel code could not make the assumption that a user-space pointer was directly accessible, and those functions might need to map individual user-space pages one at a time in order to access them. (See 4GB/4GB Kernel VM Split)

这篇关于copy_from_user和细分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆