Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of memory Management of Linux process

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Linux process memory management example analysis". In the daily operation, I believe that many people have doubts about the memory management example analysis of Linux process. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "memory management example analysis of Linux process". Next, please follow the editor to study!

Several key data structures

The virtual address space of a process is mainly described by two data knots, one is mm_struct and the other is vm_area_structs.

The mm_struct structure describes the entire virtual address space of a process, and vm_area_truct describes an interval of the virtual address space (referred to as the virtual area). The following figure is what we call the distribution of address space for processes from task_struct to mm_struct.

Each process will have its own independent mm_struct, so that each process will have its own independent address space, so that it can not interfere with each other. When the address space between processes is shared, we can understand that this is the time when multiple processes use a single address space, which is the thread.

Struct mm_struct

{

Struct vm_area_struct * mmap; / / points to virtual interval (VMA) linked list

Struct rb_root mm_rb; / / points to the red_ blacktree

Struct vm_area_struct * mmap_cache; / / find the nearest virtual interval

Unsigned long (* get_unmapped_area) (struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoof,unsigned long flags)

Void (* unmap_area) (struct mm_struct * mm,unsigned long addr)

Unsigned long mmap_base

Unsigned long task_size; / / the size of the virtual address space of the process that owns the structure

Unsigned long cached_hole_size

Unsigned long free_area_cache

Pgd_t * pgd; / / points to the page global catalog

How many users are there in atomic_t mm_users; / / user space

Atomic_t mm_count; / / how many references are there to "struct mm_struct"

Number of int map_count; / / virtual intervals

Struct rw_semaphore mmap_sem

Spinlock_t page_table_lock; / / protect task page table and mm- > rss

Struct list_head mmlist; / / linked list of all active mm

Mm_counter_t _ file_rss

Mm_counter_t _ anon_rss

Unsigned long hiwter_rss

Unsigned long hiwater_vm

Unsigned long total_vm,locked_vm,shared_vm,exec_vm

Usingned long stack_vm,reserved_vm,def_flags,nr_ptes

Start start_code of unsingned long start_code,end_code,start_data,end_data; / / code snippet, end end_code, start start_data of data segment, end end_data

Unsigned long start_brk,brk,start_stack; / / start_brk and brk record information about the heap. Start_brk is the initialization of the user's virtual address space, brk is the end address of the current heap, and start_stack is the start address of the stack.

Start arg_start of unsigned long arg_start,arg_end,env_start,env_end; / / parameter segment, end arg_end, start env_start of environment segment, end env_end

Unsigned long saved_ auxv[AT _ VECTOR_SIZE]

Struct linux_binfmt * binfmt

Cpumask_t cpu_vm_mask

Mm_counter_t context

Unsigned int faultstamp

Unsigned int token_priority

Unsigned int last_interval

Unsigned long flags

Struct core_state * core_state

}

Each virtual memory area allocated is managed by a vm_area_struct data structure, including the start and end addresses of the virtual memory, as well as the access rights to the memory, which is usually named vma;vm_area_struct data structure as follows:

Struct vm_area_struct {

/ * The first cache line has the info for VMA tree walking.

The first cache line has information about VMA tree movement * /

Unsigned long vm_start; / * Our start address within vm_mm. , /

Unsigned long vm_end; / * The first byte after our end address within vm_mm. , /

/ * linked list of VM areas per task, sorted by address

List of links in the VM area of each task, sorted by address * /

Struct vm_area_struct * vm_next, * vm_prev

Struct rb_node vm_rb

/ *

The largest available memory gap (in bytes) on the left side of this VMA.

Between this VMA and vma- > vm_prev

Or between the VMA below us and its-> vm_prev in VMA rbtree.

This helps get_unmapped_area find free space of the right size.

, /

Unsigned long rb_subtree_gap

/ * Second cache line starts here.

The second cache line starts here * /

Struct mm_struct * vm_mm; / * the address space*/ to which we belong

Pgprot_t vm_page_prot; / * access to this VMA * /

Unsigned long vm_flags; / * Flags, see mm.h. , /

/ *

For areas with address space (address apace) and backup storage (backing store)

Link to the address_space- > i_mmap interval tree, or to the vma in the address_space- > i_mmap_nonlinear list.

, /

Union {

Struct {

Struct rb_node rb

Unsigned long rb_subtree_last

} linear

Struct list_head nonlinear

} shared

/ *

After the COW of one of the file pages, the MAP_PRIVATE vma of the file can be in the i_mmap tree and the anon_vma list.

MAP_SHARED vma can only be located in an i_mmap tree.

Anonymous MAP_PRIVATE, stack or brk vma (with NULL files) can only be in the anon_vma list.

, /

Struct list_head anon_vma_chain; / * Serialized by mmap_sem & * page_table_lock

Serialized by mmap_sem and * page_table_lock * /

Struct anon_vma * anon_vma; / * Serialized by page_table_lock is serialized by page_table_lock * /

/ * function pointer used to process this structure * /

Const struct vm_operations_struct * vm_ops

/ * backup storage (backing store) information: * /

Unsigned long vm_pgoff; / * offset in PAGE_SIZE (in vm_file), * not * PAGE_CACHE_SIZE*/

Struct file * vm_file; / * We map to a file (which can be NULL) * /

Void * vm_private_data; / * is vm_pte (shared memory) * /

# ifndef CONFIG_MMU

Struct vm_region * vm_region; / * NOMMU mapping area * /

# endif

# ifdef CONFIG_NUMA

Struct mempolicy * vm_policy; / * NUMA Policy for VMA * /

# endif

}

Small experiment insmod test.ko pid_mem=3253 displays each vma area

Cat / proc/3253/maps displays each vma area

Take a look at the comparison between the two ways:

At this point, the study on "Linux process memory management example analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report