linux内核中的address_space 结构解析

144 篇文章 6 订阅

在阅读Linux2.6的内核内存管理这一部分时,我看到page结构中的一个mapping成员,我感到很迷惑,这个成员的属性太复杂了,我们来看看:

struct address_space *mapping;表示该页所在地址空间描述结构指针,用于内容为文件的页帧

(1)       如果page->mapping等于0,说明该页属于交换告诉缓存swap cache

(2)       如果page->mapping不等于0,但第0位为0,说明该页为匿名也,此时mapping指向一个struct anon_vma结构变量;

(3)       如果page->mapping不等于0,但第0位不为0,则apping指向一个struct address_space地址空间结构变量;

这成员也太麻烦了吧,下面我先看看struct address_space是啥东东:

1、定义:

看linux内核很容易被struct address_space 这个结构迷惑,它是代表某个地址空间吗?实际上不是的,它是用于管理文件(struct inode)映射到内存的页面(struct page)的,其实就是每个file都有这么一个结构,将文件系统中这个file对应的数据与这个file对应的内存绑定到一起;与之对应,address_space_operations 就是用来操作该文件映射到内存的页面,比如把内存中的修改写回文件、从文件中读入数据到页面缓冲等。file结构体和inode结构体中都有一个address_space结构体指针,实际上,file->f_mapping是从对应inode->i_mapping而来,inode->i_mapping->a_ops是由对应的文件系统类型在生成这个inode时赋予的。

也就是说address_space结构与文件的对应:一个具体的文件在打开后,内核会在内存中为之建立一个struct inode结构(该inode结构也会在对应的file结构体中引用),其中的i_mapping域指向一个address_space结构。这样,一个文件就对应一个address_space结构,一个 address_space与一个偏移量能够确定一个page cache 或swap cache中的一个页面。因此,当要寻址某个数据时,很容易根据给定的文件及数据在文件内的偏移量而找到相应的页面。看下这张图我们就很了解了:

 

下面先看下address_space结构定义:

struct address_space {

       struct inode   *host;  /* owner: inode, block_device拥有它的节点 */
       struct radix_tree_root    page_tree;/* radix tree of all pages包含全部页面的radix树 */
       rwlock_t        tree_lock;  /* and rwlock protecting it保护page_tree的自旋锁  */
       unsigned int   i_mmap_writable;/* count VM_SHARED mappings共享映射数 VM_SHARED记数*/
       struct prio_tree_root      i_mmap;         /* tree of private and shared mappings 优先搜索树的树根*/
       struct list_head       i_mmap_nonlinear;/*list VM_NONLINEAR mappings 非线性映射的链表头*/
       spinlock_t              i_mmap_lock; /* protect tree, count, list 保护i_mmap的自旋锁*/
       unsigned int           truncate_count;      /* Cover race condition with truncate 将文件截断的记数*/
       unsigned long         nrpages;  /* number of total pages 页总数*/
       pgoff_t                  writeback_index;/* writeback starts here 回写的起始偏移*/
       struct address_space_operations *a_ops;     /* methods  操作函数表*/
       unsigned long         flags;             /* error bits/gfp mask ,gfp_mask掩码与错误标识 */
       struct backing_dev_info *backing_dev_info; /* device readahead, etc预读信息 */
       spinlock_t              private_lock;   /* for use by the address_space  私有address_space锁*/
       struct list_head       private_list;     /* ditto 私有address_space链表*/
       struct address_space     *assoc_mapping;    /* ditto 相关的缓冲*/
} __attribute__((aligned(sizeof(long))));

2、page cache和swap cache

了解了struct address_space这个东东,我们就知道“一个 address_space与一个偏移量能够确定一个page cache 或swap cache中的一个页面”,那么page cache和swap cache又是什么东东呢?

page cache是与文件映射对应的,而swap cache是与匿名页对应的。如果一个内存页面不是文件映射,则在换入换出的时候加入到swap cache,如果是文件映射,则不需要交换缓冲。 

这两个的相同点就是它们都是address_space,都有相对应的文件操作:一个被访问的文件的物理页面都驻留在page cache或swap cache中,一个页面的所有信息由struct page来描述。struct page中有一个域为指针mapping ,它指向一个struct address_space类型结构。page cache或swap cache中的所有页面就是根据address_space结构以及一个偏移量来区分的。而在这里我可以负责任的告诉大家这个偏移量就是进程线性空间中某个线性地址对应的二级描述符(注意:我这里说的是以SEP4020这个arm结构为例的,它只有二级映射,另外这个二级描述符也是Linux版的,而不是硬件版的描述符)。

一般情况下用户进程调用mmap()时,只是在进程空间内新增了一块相应大小的缓冲区,并设置了相应的访问标识,但并没有建立进程空间到物理页面的映射。因此,第一次访问该空间时,会引发一个缺页异常。对于共享内存映射情况,缺页异常处理程序首先在swap cache中寻找目标页(符合address_space以及偏移量的物理页),如果找到,则直接返回地址;如果没有找到,则判断该页是否在交换区 (swap area),如果在,则执行一个换入操作;如果上述两种情况都不满足,处理程序将分配新的物理页面,并把它插入到page cache中。进程最终将更新进程页表。注:对于映射普通文件情况(非共享映射),缺页异常处理程序首先会在page cache中根据address_space以及数据偏移量寻找相应的页面。如果没有找到,则说明文件数据还没有读入内存,处理程序会从磁盘读入相应的页面,并返回相应地址,同时,进程页表也会更新。

3、swap cache的补充知识;

当将页面交换到交换文件中时,Linux总是避免页面写,除非必须这样做。当页面已经被交换出内存但是当有进程再次访问时又要将它重新调入内存。只要页面在内存中没有被写过,则交换文件中的拷贝是有效的。

Linux使用swap cache来跟踪这些页面。这个swap cache是一个页表入口链表,每个对应于系统中的物理页面。这是一个对应于交换出页面的页表入口并且描叙页面放置在哪个交换文件中以及在交换文件中的位置。如果swap cache入口为非0值,则表示在交换文件中的这一页没有被修改。如果此页被修改(或者写入)。 则其入口从swap cache中删除。

当Linux需要将一个物理页面交换到交换文件(文件类型之一)时,它将检查swap cache,如果对应此页面存在有效入口,则不必将这个页面写到交换文件中。这是因为自从上次从交换文件中将其读出来,内存中的这个页面还没有被修改。

swap cache中的入口是已换出页面的页表入口。它们虽被标记为无效但是为Linux提供了页面在哪个交换文件中以及文件中的位置等信息。

保存在交换文件中的dirty页 面可能被再次使用到,例如,当应用程序向包含在已交换出物理页面上的虚拟内存区域写入时。对不在物理内存中的虚拟内存页面的访问将引发页面错误。由于处理 器不能将此虚拟地址转换成物理地址,处理器将通知操作系统。由于已被交换出去,此时描叙此页面的页表入口被标记成无效。处理器不能处理这种虚拟地址到物理 地址的转换,所以它将控制传递给操作系统,同时通知操作系统页面错误的地址与原因。这些信息的格式以及处理器如何将控制传递给操作系统与具体硬件有关。

处理器相关页面错误处理代码将定位描叙包含出错虚拟地址对应的虚拟内存区域的vm_area_struct数据结构。它通过在此进程的vm_area_struct中查找包含出错虚拟地址的位置直到找到为止。这些代码与时间关系重大,进程的vm_area_struct数据结构特意安排成使查找操作时间更少。

执行完这些处理器相关操作并且找到出错虚拟地址的有效内存区域后,页面错处理过程其余部分和前面类似。

通用页面错处理代码为出错虚拟地址寻找页表入口。如果找到的页表入口是一个已换出页面,Linux必须将其交换进入物理内存。已换出页面的页表入口的格式与处理器类型有关,但是所有的处理器将这些页面标记成无效并把定位此页面的必要信息放入页表入口中。Linux利用这些信息以便将页面交换进物理入内存。

此时Linux知道出错虚拟内存地址并且拥有一个包含页面位置信息的页表入口。vm_area_struct数据结构可能包含将此虚拟内存区域交换到物理内存中的子程序:swapin。如果对此虚拟内存区域存在swapin则Linux会使用它。这是已换出系统V共享内存页面的处理过程-因为已换出系统V共享页面和普通的已换出页面有少许不同。如果没有swapin操作,这可能是Linux假定普通页面无须特殊处理。

系统将分配物理页面并将已换出页面读入。关于页面在交换文件中位置信息从页表入口中取出。如果引起页面错误的访问不是写操作则页面被保留在swap cache中并且它的页表入口不再标记为可写。如果页面随后被写入,则将产生另一个页面错误,这时页面被标记为dirty,同时其入口从swap cache中删除。如果页面没有被写并且被要求重新换出,Linux可以免除这次写,因为页面已经存在于交换文件中。

如果引起页面从交换文件中读出的操作是写操作,这个页面将被从swap cache中删除并且其页表入口被标记成dirty且可写。

4、page cache的补充:

说到page cache我们很容易就与buffer cache混淆,

在这里我需要说的是page cache是VFS的一部分,buffer cache是块设备驱动的一部分,或者说page cache是面向用户IO的cache,buffer cache是面向块设备IO的cache,page cache按照文件的逻辑页进行缓冲,buffer cache按照文件的物理块进行缓冲。page cache与buffer cache并不相互独立而是相互融合的,同一文件的cache页即可存在于page cache中,又可存在于buffer cache中,它们在物理内存中只有一份拷贝。文件系统接口就处于page cache和buffer cache之间,它完成page cache的逻辑页与buffer cache的物理块之间的相互转换,再交给统一的块设备IO进行调度处理,文件的逻辑块与物理块的关系就表现为page cache与buffer cache的关系。

Page cache实际上是针对文件系统的,是文件的缓存,在文件层面上的数据会缓存到page cache。文件的逻辑层需要映射到实际的物理磁盘,这种映射关系由文件系统来完成。当page cache的数据需要刷新时,page cache中的数据交给buffer cache,但是这种处理在2.6版本的内核之后就变的很简单了,没有真正意义上的cache操作。

Buffer cache是针对磁盘块的缓存,也就是在没有文件系统的情况下,直接对磁盘进行操作的数据会缓存到buffer cache中,例如,文件系统的元数据都会缓存到buffer cache中。

简单说来,page cache用来缓存文件数据,buffer cache用来缓存磁盘数据。在有文件系统的情况下,对文件操作,那么数据会缓存到page cache,如果直接采用dd等工具对磁盘进行读写,那么数据会缓存到buffer cache。

  • 13
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Design and Implementation of Network Protocols in the Linux Kernel Part I: The Linux Kernel 1 Chapter 1. Motivation 3 Section 1.1. The Linux Operating System 4 Section 1.2. What is Linux? 5 Section 1.3. Reasons for Using Linux 6 Chapter 2. The Kernel Structure 9 Section 2.1. Monolithic Architectures and Microkernels 11 Section 2.2. Activities in the Linux Kernel 12 Section 2.3. Locking—Atomic Operations 17 Section 2.4. Kernel Modules 23 Section 2.5. Device Drivers 29 Section 2.6. Memory Management in the Kernel 31 Section 2.7. Timing in the Linux Kernel 35 Section 2.8. The Proc File System 40 Section 2.9. Versioning 43 Part II: Architecture of Network Implementation 45 Chapter 3. The Architecture of Communication Systems 47 Section 3.1. Layer-Based Communication Models 47 Section 3.2. Services and Protocols 52 Chapter 4. Managing Network Packets in the Kernel 55 Section 4.1. Socket Buffers 55 Section 4.2. Socket-Buffer Queues 66 Chapter 5. Network Devices 71 Section 5.1. The net_device Interface 73 Section 5.2. Managing Network Devices 82 Section 5.3. Network Drivers 92 Part III: Layer I + II—Medium Access and Logical Link Layer 115 Chapter 6. Introduction to the Data-Link Layer 117 Section 6.1. Structure of the Data-Link Layer 117 Section 6.2. Processes on the Data-Link Layer 119 Section 6.3. Managing Layer-3 Protocols 127 Chapter 7. The Serial-Line Internet Protocol (SLIP) 132 Section 7.1. Introduction 132 Section 7.2. Slip Implementation in the Linux Kernel 134 Chapter 8. The Point-to-Point Protocol (PPP) 145 Section 8.1. Introduction 145 Section 8.2. PPP Configuration in Linux 148 Section 8.3. PPP Implementation in the Linux Kernel 150 Section 8.4. Implementing the PPP Daemon 158 Chapter 9. PPP over Ethernet 161 Section 9.1. Introduction 161 Section 9.2. PPPOE Specification in RFC 2516 161 Section 9.3. Implementation in the User Space 163 Section 9.4. Implementation in the Linux Kernel 164 Chapter 10. Asynchronous Transfer Mode—ATM 168 Section 10.1. Introduction 168 Section 10.2. Implementing ATM in Linux 169 Section 10.3. Configuration 177 Chapter 11. Bluetooth in Linux 179 Section 11.1. Host Controller Interface (HCI) 181 Section 11.2. L2CAP 185 Section 11.3. Other Protocols 188 Chapter 12. Transparent Bridges 189 Section 12.1. Introduction 189 Section 12.2. Basics 190 Section 12.3. Configuring a Bridge in Linux 199 Section 12.4. Implementation 202 Part IV: Network Layer 221 Chapter 13. The TCP/IP Protocols 223 Section 13.1. The Internet Protocol Suite 224 Chapter 14. The Internet Protocol V4 227 Section 14.1. Properties of the Internet Protocol 228 Section 14.2. Implementing the Internet Protocol 233 Section 14.3. IP Options 250 Section 14.4. Internet Control Message Protocol (ICMP) 262 Chapter 15. Address Resolution Protocol (ARP) 273 Section 15.1. Using the Address Resolution Protocol 274 Section 15.2. The ARP Command 276 Section 15.3. Implementing the ARP Instance in the Linux Kernel 277 Chapter 16. IP Routing 293 Section 16.1. Introduction 293 Section 16.2. Configuration 301 Section 16.3. Implementation 309 Chapter 17. IP Multicast for Group Communication 330 Section 17.1. Group Communication 331 Section 17.2. IP Multicast 333 Section 17.3. Internet Group Management Protocol (IGMP) 339 Section 17.4. Multicast Data Path in the Linux Kernel 345 Section 17.5. Multicasting in Today's Internet 355 Section 17.6. Multicast Transport Protocols 364 Chapter 18. Using Traffic Control to Support Quality of Service (QoS) 366 Section 18.1. Introduction 366 Section 18.2. Basic Structure of Traffic Control in Linux 367 Section 18.3. Traffic Control in the Outgoing Direction 367 Section 18.4. Kernel Structures and Interfaces 369 Section 18.5. Ingress Policing 378 Section 18.6. Implementing a Queuing Discipline 378 Section 18.7. Configuration 381 Chapter 19. Packet Filters and Firewalls 383 Section 19.1. Introduction 383 Section 19.2. The Ipchains Architecture of Linux 2.2 386 Section 19.3. The Netfilter Architecture of Linux 2.4 391 Chapter 20. Connection Tracking 399 Section 20.1. Introduction 399 Section 20.2. Implementation 400 Chapter 21. Network Address Translation (NAT) 410 Section 21.1. Introduction 410 Section 21.2. Configuring NAT in Linux 414 Section 21.3. Implementing the NAT Module 416 Section 21.4. Interfaces to Extend the NAT Module 422 Chapter 22. Extending the Linux Network Architecture Functionality—KIDS 426 Section 22.1. Managing Dynamically Extendable Functionalities 426 Section 22.2. Structure of the KIDS Construction System 428 Section 22.3. Using the KIDS Example to Extend the Linux Network Architecture 431 Chapter 23. IPv6—Internet Protocol Version 6 443 Section 23.1. Introduction 443 Section 23.2. IPv6 Features 443 Section 23.3. IPv6 Implementation 450 Part V: Layer IV—Transport Layer 455 Chapter 24. Transmission Control Protocol (TCP) 457 Section 24.1. Overview 457 Section 24.2. Implementing The TCP Protocol Instance 460 Section 24.3. Connection Management 476 Section 24.4. Protocol Mechanisms For Data Exchange 486 Section 24.5. Timer Management In TCP 508 Chapter 25. User Datagram Protocol (UDP) 513 Section 25.1. Introduction 513 Section 25.2. Data Structures 514 Section 25.3. Sending and Receiving UDP Datagrams 519 Chapter 26. The Concept of Sockets 522 Section 26.1. Introduction 522 Section 26.2. BSD Sockets 522 Section 26.3. Protocol-Specific Sockets

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值