SlideShare a Scribd company logo
1
2
Memory management in
Linux kernel
3
Memory management
tasks
• Physical memory allocator
• Physical memory management
• Virtual memory allocator
• PTE management
• Memory allocator for kernel
needs
4
Memory management
subsystem
• >100K lines
• Buddy allocator
• Page replacement (“LRU” reclaim model)
• PTE management
• Slab/slob/slub kernel allocator
• Pagecache/writeback/readahead/swap
• Cgroup memory controller
• Compaction
5
Hardware
• X86_64
• Paging (MMU, TLB, ...)
• 4KB, 2MB and 1GB pages
• NUMA
• 4-level PTE's
• Hardware referenced bit
6
Physical memory
description
• Node (pg_data_t)
• Zone (struct zone)
• Page (struct page)
$ cat /proc/zoneinfo | grep Node
Node 0, zone DMA
Node 0, zone DMA32
Node 0, zone Normal
Node 1, zone Normal
7
Virtual memory
description• Address space (struct mm_struct)
• VM area (struct vm_area_struct)
$ cat /proc/self/maps
00400000-0040c000 r-xp 00000000 08:03 2359718 /usr/bin/cat
0060b000-0060c000 r--p 0000b000 08:03 2359718 /usr/bin/cat
0060c000-0060d000 rw-p 0000c000 08:03 2359718 /usr/bin/cat
011a7000-011c8000 rw-p 00000000 00:00 0 [heap]
7f4d072e5000-7f4d0d80e000 r--p 00000000 08:03 2369473
/usr/lib/locale/locale-archive
7f4d0d80e000-7f4d0d9c2000 r-xp 00000000 08:03 2366682 /usr/lib64/libc-2.18.so
7f4d0d9c2000-7f4d0dbc2000 ---p 001b4000 08:03 2366682 /usr/lib64/libc-2.18.so
7f4d0dbc2000-7f4d0dbc6000 r--p 001b4000 08:03 2366682 /usr/lib64/libc-2.18.so
...
8
File mappings
• File mappings (struct
address_space)
• Radix tree with all resident pages
• Pagecache
• Major/minor pagefault
9
Kernel API
• __get_free_page()
• kmalloc()/kfree()
• vmalloc()
• ...
10
Userspace API
• pagefault
• mmap()/munmap()
• brk()
• mlock()/munlock()
• fadvise(), madvise()
• ...
11
Memory reclaim
• Normal/direct reclaim (free pool)
• Per-node kswapd
• Working set
• Memory pressure
• File memory vs anonymous memory
• Swap
• OOM
12
“LRU” model
• 5 double linked lists: inactive file,
active file, inactive anon, active
anon, unevictable
• Referenced flag in struct
page_struct flag
13
List transition rules
• mark_page_accessed():
– unreferenced -> referenced
– inactive && referenced -> active
• shrink_inactive_list():
– if (ptes referenced)
• anonymous -> active
• referenced -> active
• (ptes referenced > 1) -> active (3.2)
• (vm_flags & VM_EXEC) -> active (3.2)
• set referenced
• rotate
– else
• reclaim
• shrink_active_list():
– If referenced
• file & VM_EXEC -> rotate
– -> inactive
14
Memory pressure
balancing
• nr_pages_to_scan =
nr_pages/2^priority
• priority = [12..0]
1/4096, 1/2048, 1/1024, ...
• swappiness
• active > inactive
15
Yasearch-specific
problems & solutions
• Working set > 1/2 available
memory
• Memory thrashing
• promote_mapped_pages
• file_inactive_ratio
16
Monitoring & tools
• top
• vmtouch
• /proc/vmstat
• /proc/buddyinfo
• /proc/slabinfo
• perf top
• oom-message in dmesg
17
Demonstration
18
Cgroups
• Each cgroup has own LRU lists.
• No common LRU (since 3.3)!
• Common free pool(s)
• Common kswapd thread(s)
• Global reclaim vs target reclaim
19
Memory controller
• memory.limit_in_bytes
• memory.soft_limit_in_bytes (will
be deprecated)
• memory.use_hierarchy
• ...
20
Monitoring
• memory.usage_in_bytes
• memory.max_usage_in_bytes
• memory.stat
21
Accounting
• Each page belongs to one cgroup
• First accessed - owner
• memory.move_charge_at_immigr
ate
22
Yasearch-specific
problems & solutions
• memory.low_limit_in_bytes
• First accessed – owner? mlock()?
low_limit?
• memory.recharge_on_pgfault
23
Compaction
• Physical pages migration to zone's
top
• https://lwn.net/Articles/368869
• Broken in 3.3-3.7
• Replacement for lumpy reclaim
• Use perf top for problem diagnostics
24
Спасибо за
внимание!

More Related Content

What's hot

What's hot (20)

Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
Memory Compaction in Linux Kernel.pdf
Memory Compaction in Linux Kernel.pdfMemory Compaction in Linux Kernel.pdf
Memory Compaction in Linux Kernel.pdf
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
 
Memory Management with Page Folios
Memory Management with Page FoliosMemory Management with Page Folios
Memory Management with Page Folios
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 
Linux memory
Linux memoryLinux memory
Linux memory
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
 
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven RostedtKernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
 

Viewers also liked

AXON ivy Pte Ltd Profile
AXON ivy Pte Ltd ProfileAXON ivy Pte Ltd Profile
AXON ivy Pte Ltd Profile
Aashish Mehra
 
Ririn islami (fisika)
Ririn islami (fisika)Ririn islami (fisika)
Ririn islami (fisika)
ririnislami9
 

Viewers also liked (20)

Technology 101
Technology 101Technology 101
Technology 101
 
Memorial ISTEP/IREAD3 parent information 2014
Memorial ISTEP/IREAD3 parent information 2014 Memorial ISTEP/IREAD3 parent information 2014
Memorial ISTEP/IREAD3 parent information 2014
 
Building a Recommendation System for EverQuest Landmark's Marketplace
Building a Recommendation System for EverQuest Landmark's MarketplaceBuilding a Recommendation System for EverQuest Landmark's Marketplace
Building a Recommendation System for EverQuest Landmark's Marketplace
 
Bartkowiak balcerza prezentacja
Bartkowiak balcerza prezentacjaBartkowiak balcerza prezentacja
Bartkowiak balcerza prezentacja
 
Mermaid Telecommunication
Mermaid Telecommunication Mermaid Telecommunication
Mermaid Telecommunication
 
R&D Initial Presentation
R&D Initial PresentationR&D Initial Presentation
R&D Initial Presentation
 
FBC, Belmont Announcements 2 23-14
FBC, Belmont Announcements 2 23-14FBC, Belmont Announcements 2 23-14
FBC, Belmont Announcements 2 23-14
 
First Baptist Church, Belmont, MS Weekly Announcements for July 6, 2014
First Baptist Church, Belmont, MS Weekly Announcements for July 6, 2014First Baptist Church, Belmont, MS Weekly Announcements for July 6, 2014
First Baptist Church, Belmont, MS Weekly Announcements for July 6, 2014
 
FLUIDA
FLUIDAFLUIDA
FLUIDA
 
Technical Analysis
Technical AnalysisTechnical Analysis
Technical Analysis
 
FBC, Belmont, MS Weekly Announcements for Sunday, March 16, 2014
FBC, Belmont, MS Weekly Announcements for Sunday, March 16, 2014FBC, Belmont, MS Weekly Announcements for Sunday, March 16, 2014
FBC, Belmont, MS Weekly Announcements for Sunday, March 16, 2014
 
AXON ivy Pte Ltd Profile
AXON ivy Pte Ltd ProfileAXON ivy Pte Ltd Profile
AXON ivy Pte Ltd Profile
 
CHAD App Store Icon
CHAD App Store IconCHAD App Store Icon
CHAD App Store Icon
 
Global Initiative on Out-of-school children: Central and Eastern Europe/ CIS
Global Initiative on Out-of-school children: Central and Eastern Europe/ CISGlobal Initiative on Out-of-school children: Central and Eastern Europe/ CIS
Global Initiative on Out-of-school children: Central and Eastern Europe/ CIS
 
How Can I Make Money From Home
How Can I Make Money From HomeHow Can I Make Money From Home
How Can I Make Money From Home
 
Ansel Adams Pictures
Ansel Adams PicturesAnsel Adams Pictures
Ansel Adams Pictures
 
Ririn islami (fisika)
Ririn islami (fisika)Ririn islami (fisika)
Ririn islami (fisika)
 
First Baptist Church, Belmont Weekly Announcements for June 8, 2014
First Baptist Church, Belmont Weekly Announcements for June 8, 2014First Baptist Church, Belmont Weekly Announcements for June 8, 2014
First Baptist Church, Belmont Weekly Announcements for June 8, 2014
 
Holding Effective Data Meetings
Holding Effective Data MeetingsHolding Effective Data Meetings
Holding Effective Data Meetings
 
Konflik sosial
Konflik sosialKonflik sosial
Konflik sosial
 

Similar to Memory management in Linux kernel

Monitoring MongoDB’s Engines in the Wild
Monitoring MongoDB’s Engines in the WildMonitoring MongoDB’s Engines in the Wild
Monitoring MongoDB’s Engines in the Wild
Tim Vaillancourt
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
Fuenteovejuna
 
MySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
MySQL Server Backup, Restoration, And Disaster Recovery Planning PresentationMySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
MySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
Colin Charles
 

Similar to Memory management in Linux kernel (20)

Workshop - Linux Memory Analysis with Volatility
Workshop - Linux Memory Analysis with VolatilityWorkshop - Linux Memory Analysis with Volatility
Workshop - Linux Memory Analysis with Volatility
 
Mac Memory Analysis with Volatility
Mac Memory Analysis with VolatilityMac Memory Analysis with Volatility
Mac Memory Analysis with Volatility
 
Collaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAsCollaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAs
 
Investigate SQL Server Memory Like Sherlock Holmes
Investigate SQL Server Memory Like Sherlock HolmesInvestigate SQL Server Memory Like Sherlock Holmes
Investigate SQL Server Memory Like Sherlock Holmes
 
Deployment Strategies (Mongo Austin)
Deployment Strategies (Mongo Austin)Deployment Strategies (Mongo Austin)
Deployment Strategies (Mongo Austin)
 
SQL 2005 Memory Module
SQL 2005 Memory ModuleSQL 2005 Memory Module
SQL 2005 Memory Module
 
Monitoring MongoDB’s Engines in the Wild
Monitoring MongoDB’s Engines in the WildMonitoring MongoDB’s Engines in the Wild
Monitoring MongoDB’s Engines in the Wild
 
An introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP EngineAn introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP Engine
 
Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
 
Linux introduction
Linux introductionLinux introduction
Linux introduction
 
Percona FT / TokuDB
Percona FT / TokuDBPercona FT / TokuDB
Percona FT / TokuDB
 
memory_mapping.ppt
memory_mapping.pptmemory_mapping.ppt
memory_mapping.ppt
 
Oracle DBA
Oracle DBAOracle DBA
Oracle DBA
 
MySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
MySQL Server Backup, Restoration, And Disaster Recovery Planning PresentationMySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
MySQL Server Backup, Restoration, And Disaster Recovery Planning Presentation
 
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar AhmedPGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
 
Innodb 和 XtraDB 结构和性能优化
Innodb 和 XtraDB 结构和性能优化Innodb 和 XtraDB 结构和性能优化
Innodb 和 XtraDB 结构和性能优化
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
 
Optimizing RocksDB for Open-Channel SSDs
Optimizing RocksDB for Open-Channel SSDsOptimizing RocksDB for Open-Channel SSDs
Optimizing RocksDB for Open-Channel SSDs
 
macospptok.pptx
macospptok.pptxmacospptok.pptx
macospptok.pptx
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

The architecture of Generative AI for enterprises.pdf
The architecture of Generative AI for enterprises.pdfThe architecture of Generative AI for enterprises.pdf
The architecture of Generative AI for enterprises.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 

Memory management in Linux kernel