GNU/Linux - Linux kernel memory access

User space memory access from the Linux kernel

An introduction to Linux memory and user space APIs

By M. Jones

10 August 2010

Archive date: 2023-08-31

虽然字节可能是 Linux 中内存的最低可寻址单位,但页面才是内存的可管理抽象。本文首先讨论 Linux 中的内存管理,然后探讨从内核操作用户地址空间的方法。

Although the byte may be the lowest addressable unit of memory within Linux, it's the page that serves as the managed abstraction of memory. This article begins with a discussion of memory management within Linux, and then explores the methods for manipulation of user address space from the kernel.

Linux memory

在 Linux 中,用户内存和内核内存是独立的,并在不同的地址空间中实现。地址空间是虚拟化的,也就是说,地址是从物理内存中抽象出来的(通过即将详述的过程)。由于地址空间是虚拟化的,因此可以存在很多地址空间。事实上,内核本身驻留在一个地址空间中,而每个进程都驻留在自己的地址空间中。这些地址空间由虚拟内存地址组成,允许许多具有独立地址空间的进程引用一个小得多的物理地址空间(机器中的物理内存)。这不仅方便,而且安全,因为每个地址空间都是独立和隔离的,因此是安全的。

In Linux, user memory and kernel memory are independent and implemented in separate address spaces. The address spaces are virtualized, meaning that the addresses are abstracted from physical memory (through a process detailed shortly). Because the address spaces are virtualized, many can exist. In fact, the kernel itself resides in one address space, and each process resides in its own address space. These address spaces consist of virtual memory addresses, permitting many processes with independent address spaces to refer to a considerably smaller physical address space (the physical memory in the machine). Not only is this convenient, but it's also secure, because each address space is independent and isolated and therefore secure.

但这种安全性也是有代价的。因为每个进程(和内核)都可能拥有指向不同物理内存区域的相同地址,所以无法立即共享内存。幸运的是,我们有一些解决方案。用户进程可以通过 UNIX® 便携式操作系统接口(POSIX)共享内存机制(shmem)共享内存,但需要注意的是,每个进程都可能拥有指向同一物理内存区域的不同虚拟地址。

But there's a cost associated with this security. Because each process (and the kernel) can have identical addresses that refer to different regions of physical memory, it's not immediately possible to share memory. Luckily, a few solutions exist. User processes can share memory through the Portable Operating System Interface for UNIX® (POSIX) shared memory mechanism (shmem), with the caveat that each process may have a different virtual address that refers to the same region of physical memory.

虚拟内存与物理内存的映射是通过页表进行的,页表在底层硬件中实现(见图 1)。硬件本身提供映射,但内核管理页表及其配置。请注意,如图所示,一个进程可能有一个很大的地址空间,但它是稀疏的,这意味着地址空间的小区域(页)通过页表指向物理内存。这样,进程就可以拥有一个庞大的地址空间,而这个地址空间只为特定时间所需的页定义。

The mapping of virtual memory to physical memory occurs through page tables, which are implemented in the underlying hardware (see Figure 1). The hardware itself provides the mapping, but the kernel manages the tables and their configuration. Note that as shown here, a process may have a large address space, but it is sparse, meaning that small regions (pages) of the address space refer to physical memory through the page tables. This permits a process to have a massive address space that is defined only for the pages that are needed at any given time.

Figure 1. Page tables provide the mapping from virtual addresses to physical addresses

图 1:页表提供了从虚拟地址到物理地址的映射

为进程稀疏定义内存的能力意味着底层物理内存可能会被过度占用。通过一个称为分页(在 Linux 中通常称为交换)的过程,较少使用的页面会被动态移动到速度较慢的存储设备(如磁盘)上,以容纳其他需要访问的页面(见图 2)。这种行为允许计算机中的物理内存提供应用程序更需要的页面,同时将不太需要的页面迁移到磁盘,以提高物理内存的利用率。需要注意的是,有些页面可能会指向文件,在这种情况下,如果数据较脏,可以(通过页面缓存)刷新,如果页面干净,则直接丢弃。

Having the ability to sparsely define memory for processes means that the underlying physical memory can be overcommitted. Through a process called paging (though in Linux, it's typically called swap), less-used pages are dynamically moved to a slower storage device (such as a disk) to accommodate other pages that need to be accessed (see Figure 2). This behavior allows the physical memory within the computer to serve pages that an application more readily needs while migrating less-needed p

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值