【Linux内核笔记】Linux进程地址空间:内存描述符,虚拟内存区域

本文详细介绍了Linux内核中进程的地址空间,包括内存描述符mm_struct及其分配与撤销,虚拟内存区域VMA的管理,以及页表的工作原理。内存描述符用于表示进程地址空间,而VMA描述了进程中的独立内存范围。通过mmap和munmap操作,进程可以动态地映射和取消映射内存区域。页表是将虚拟地址转换为物理地址的关键,Linux采用三级页表实现这一转换,确保高效地处理内存访问。
摘要由CSDN通过智能技术生成

进程地址空间

  • 进程地址空间——进程可寻址的虚拟内存,内核允许进程使用该虚拟内存中的地址
  • 内存区域(memory areas)——进程允许访问的虚拟内存的地址区间
  • 每个进程有32/64位的flat(独立的连续)地址空间。进程访问了不在有效范围内的内存区域,或不正确的访问方式访问了有效地址,内核会终止该进程,返回“段错误”信息 segment fault

内存区域可以包含各种内存对象

映射的内存对象 进程地址空间的任何有效地址都只能位于唯一的区域,以下区域不能相互覆盖
代码段 text section 可执行文件代码的内存映射
数据段 data section 可执行文件的已初始化全局变量的内存映射
bss段 未初始化全局变量。C标准规定未初始化的全局变量需要被赋予特殊默认值(0)内核需要将变量从可执行代码载入到内存中,将零页映射到该片内存中
进程用户空间栈的零页内存映射 进程内核栈独立存在并由内核维护
每一个C库或动态链接程序等共享库的代码段、数据段和bss段
任何内存映射文件
任何共享内存段
malloc()分配的内存

内存描述符mm_struct

  • 内核使用内存描述符结构体表示进程的地址空间,进程描述符task_struct中的mm域存放该进程使用的内存描述符
<linux/mm_types.h>
struct mm_struct {
   
	struct vm_area_struct *mmap;		/* list of VMAs */
	struct rb_root mm_rb;
	u32 vmacache_seqnum;                   /* per-thread vmacache */
#ifdef CONFIG_MMU
	unsigned long (*get_unmapped_area) (struct file *filp,
				unsigned long addr, unsigned long len,
				unsigned long pgoff, unsigned long flags);
#endif
	unsigned long mmap_base;		/* base of mmap area */
	unsigned long mmap_legacy_base;         /* base of mmap area in bottom-up allocations */
	unsigned long task_size;		/* size of task vm space */
	unsigned long highest_vm_end;		/* highest vma end address */
	pgd_t * pgd;
	atomic_t mm_users;			/* How many users with user space? */
	atomic_t mm_count;			/* How many references to "struct mm_struct" (users count as 1) */
	atomic_long_t nr_ptes;			/* Page table pages */
	int map_count;				/* number of VMAs */

	spinlock_t page_table_lock;		/* Protects page tables and some counters */
	struct rw_semaphore mmap_sem;

	struct list_head mmlist;		/* List of maybe swapped mm's.	These are globally strung
						 * together off init_mm.mmlist, and are protected
						 * by mmlist_lock
						 */


	unsigned long hiwater_rss;	/* High-watermark of RSS usage */
	unsigned long hiwater_vm;	/* High-water virtual memory usage */

	unsigned long total_vm;		/* Total pages mapped */
	unsigned long locked_vm;	/* Pages that have PG_mlocked set */
	unsigned long pinned_vm;	/* Refcount permanently increased */
	unsigned long shared_vm;	/* Shared pages (files) */
	unsigned long exec_vm;		/* VM_EXEC & ~VM_WRITE */
	unsigned long stack_vm;		/* VM_GROWSUP/DOWN */
	unsigned long def_flags;
	unsigned long start_code, end_code, start_data, end_data;
	unsigned long start_brk, brk, start_stack;
	unsigned long arg_start, arg_end, env_start, env_end;

	unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */

	/*
	 * Special counters, in some configurations protected by the
	 * page_table_lock, in other configurations by being atomic.
	 */
	struct mm_rss_stat rss_stat;

	struct linux_binfmt *binfmt;

	cpumask_var_t cpu_vm_mask_var;

	/* Architecture-specific MM context */
	mm_context_t context;

	unsigned long flags; /* Must use atomic bitops to access the bits */

	struct core_state *core_state; /* coredumping support */
#ifdef CONFIG_AIO
	spinlock_t			ioctx_lock;
	struct kioctx_table __rcu	*ioctx_table;
#endif
#ifdef CONFIG_MEMCG
	/*
	 * "owner" points to a task that is regarded as the canonical
	 * user/owner of this mm. All of the following must be true in
	 * order for it to be changed:
	 *
	 * current == mm->owner
	 * current->mm != mm
	 * new_owner->mm == mm
	 * new_owner->alloc_lock is held
	 */
	struct task_struct __rcu *owner;
#endif

	/* store ref to file /proc/<pid>/exe symlink points to */
	struct file *exe_file;
#ifdef CONFIG_MMU_NOTIFIER
	struct mmu_notifier_mm *mmu_notifier_mm;
#endif
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
	pgtable_t pmd_huge_pte; /* protected by page_table_lock */
#endif
#ifdef CONFIG_CPUMASK_OFFSTACK
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值