The third stage is to establish a buddy
Buddy's lifespan
mm_init->mem_init returns - no end point
Buddy managed memory size
The memory managed by buddy is determined by memblock
The portion of memblock.memory that does not include memblock.reserved
memblock can reserve memory through memblock_alloc or memblock_reserve
How to use buddy
alloc
alloc_pages/alloc_page // Returns struct page
get_zeroed_page // Returns the virtual address
__get_free_pages/__get_free_page // Returns the virtual address
get_dma_pages
__get_free_pages
struct page *page = alloc_pages
alloc_pages_node
__alloc_pages_node
__alloc_pages
__alloc_pages_nodemask
struct page * page = get_page_from_freelist
return (unsigned long) page_address(page);
__alloc_pages_nodemask details
__alloc_pages_nodemask
prepare_alloc_context //1. Prepare parameters
get_page_from_freelist //2. According to the parameters obtained by prepare_alloc_context, the fast path attempts to allocate memory
__alloc_pages_slowpath //3. If the fast path fails to allocate memory, the slow path attempts to allocate memory
For details, please visit https://zhuanlan.zhihu.com/p/258921453
highly recommended
prepare_alloc_context // Set three parameters of get_page_from_freelist function
1. struct alloc_context *ac
ac->migratetype = gfp_migratetype(gfp_mask); // Set the required migration type, related to zone->free_area[order].free_list[migratetype]
2. gfp_t *alloc_mask
*alloc_mask |= __GFP_HARDWALL;
3. unsigned int *alloc_flags
*alloc_flags = current_alloc_flags(gfp_mask, *alloc_flags);
get_page_from_freelist
for_next_zone_zonelist_nodemask(zone, z, ac->highest_zoneidx, ac->nodemask) { // Traverse the zones according to the priority of ZONE_HIGHMEM ZONE_NORMAL ZONE_DMA
mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); // Get the current watermark
if (!zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, alloc_flags, gfp_mask)) {
// Compare with the watermark FLAG set by alloc_pages
// Entering the if function body at this time indicates that the current watermark <= the watermark FLAG set by alloc_pages, indicating that no more physical pages can be allocated
if (alloc_flags & ALLOC_NO_WATERMARKS) goto try_this_zone;
// Indicates the watermark set by alloc_pages FLAG means ignoring the watermark, and directly allocates pages to the try_this_zone label
}
try_this_zone:
page = rmqueue(ac->preferred_zoneref->zone, zone, order, gfp_mask, alloc_flags, ac->migratetype);
if (likely(order == 0)) rmqueue_pcplist(preferred_zone, zone, gfp_flags, migratetype, alloc_flags);
// Check whether the linked list maintained by per_cpu_pages (pcp) can satisfy the memory request
else __rmqueue(zone, order, migratetype, alloc_flags);
// Apply from the partner system, the query list is
// zone->free_area[order].free_list[migratetype]
__rmqueue_smallest(zone, order, migratetype);
area = &(zone->free_area[current_order]);
page = get_page_from_free_area(area, migratetype);
del_page_from_free_list(page, zone, current_order);
}
__alloc_pages_slowpath
Various mechanisms for invoking memory shortage
...relaim
... compact
... oom killer
... kswapd
get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
free
free_pages/free_page
__free_pages/__free_page
free_pages(unsigned long addr, unsigned int order)
__free_pages(virt_to_page((void *)addr), order);
free_the_page(page, order)
__free_pages_ok
free_one_page
__free_one_page
add_to_free_list(page, zone, order, migratetype);
other
The essence of buddy
The buddy system only maintains free blocks, and the allocated blocks do not belong to the buddy system.
After calling free_pages on some blocks, they will be put into the buddy system.
---
Disassembly & Merge
The essence of buddy is to split and merge according to 2^n
Buddy Problems and Solutions
// Is there any virtual memory problem?
1. Physical memory fragmentation problem
Buddy Merge
Timing: free_pages
method:
Threads: None
Compact
Timing: alloc
method:
Threads: 1 thread kcompactd0
2. Insufficient physical memory
Reclaim
Timing: when alloc memory is scarce/cycle/system sleep
method:
Threads: 2 threads kswapd0 oom_reaper
3. Unable to apply for consecutive physical pages
CMA contiguous memory allocator
Timing: when allocating continuous memory
method:
A section of memory is reserved for the driver to use, but when the driver is not in use, the CMA area can be allocated to the user process for use as anonymous memory or page cache.
When the driver needs to use it, the memory occupied by the process is recycled or migrated to free up the reserved memory previously occupied for use by the driver.
Threads: None
miscellaneous
---
5. If __GFP_DMA is set, pages can only be obtained from the ZONE_DMA memory management area
6. If __GFP_HIGH is not set, get pages from the ZONE_NORMAL ZONE_DMA memory management area in turn
7. If __GFP_HIGH is set, get pages from ZONE_HIGHMEM ZONE_NORMAL ZONE_DMA memory management areas in turn
---
Page frames are unlimited and can store any data
x86 hardware has restrictions on page frames, resulting in the addition of ZONE (ZONE page frame classification)
There is no zone, or there is only one zone : ZONE_NORMAL 16MB - 896MB
1.DMA can only address the first 16MB of RAM : Added ZONE_DMA 0MB - 16MB
2.32bit cannot address all physical memory : Added ZONE_HIGHMEM 896M -
What about arm32???
Page frames are unlimited and can store any data
The arm32 hardware has restrictions on page frames, resulting in the addition of ZONE (ZONE page frame classification)
There is no zone, or there is only one zone : ZONE_NORMAL 0MB - 760MB
1.32bit cannot address all physical memory : Added ZONE_HIGHMEM 760MB -
---
status quo
CONFIG_ZONE_DMA CONFIG_ZONE_DMA32 CONFIG_HIGHMEM is not configured
There are only two zones: ZONE_NORMAL ZONE_MOVABLE
---
For arm32, when do you need to configure CONFIG_HIGHMEM?
200MB does not require CONFIG_HIGHMEM
1GB needs to be configured because the kernel space is only 1G, and the memblock reservation is less than 1G, so it cannot be fully mapped.
---
S3C6410 DMA
6410 has DMA
Source and destination are both available in system bus/peripheral bus (4 cases)
It uses ARM's IP PL080
4 DMAs, 8 channels per DMA
No limit on memory
Previous article:OK6410A Development Board (VIII) 41 linux-5.11 OK6410A slab alloc and free
Next article:OK6410A Development Board (VIII) 39 linux-5.11 OK6410A memblock alloc and free
Recommended ReadingLatest update time:2024-11-16 19:48
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Some new changes that 5G brings to PA
- Experience in using Ti's C28x series DSP (28069) (28377D)
- EETalk: What pitfalls have you encountered during PCB design?
- Harbin Institute of Technology and Harbin Engineering University are banned from using genuine MATLAB software. What do you think about this?
- How to design an RC oscillator circuit with an output frequency of 1k to 3.2MHz
- Will power management integrated circuits (PMIC) be the mainstream direction in the future?
- Some experience on DSP parallel FLASH booting
- Where can I find the meaning of TI chip suffixes?
- What is the minimum power consumption of the Zigbee module?
- Summary of EMC principles for PCB design