Segmentation Paging Logical address Segment selector Segment Descriptor

  • Slides: 40
Download presentation

セグメンテーションと ページング Segmentation Paging Logical address Segment selector Segment Descriptor Global Descriptor Table Linear

セグメンテーションと ページング Segmentation Paging Logical address Segment selector Segment Descriptor Global Descriptor Table Linear address Directory Table offset Linear Address Space Page Directory Entry Page Table Entry Page Directory Page Table CR 3 offset Physical address Page

Linuxのメモリマップ(32 bit) プロセス毎の 仮想アドレス空間 0 x 0000 text data bss bss heap 0 x

Linuxのメモリマップ(32 bit) プロセス毎の 仮想アドレス空間 0 x 0000 text data bss bss heap 0 x 40000000 ディスク File System Swap 0 xc 0000000 kernel stack *. so mapped vmalloc space 0 xc 0000000 stack High mem 物理メモリ 0 xffff

2レベルページング(x 86) Linear Address 31 22 21 Directory 12 11 Table Page Directory CR

2レベルページング(x 86) Linear Address 31 22 21 Directory 12 11 Table Page Directory CR 3 0 Offset Page

3レベルページング(Linux) Linear Address Global Dir Middle Dir Table Offset Page Global Directory CR 3

3レベルページング(Linux) Linear Address Global Dir Middle Dir Table Offset Page Global Directory CR 3 Page Middle Directory Page Table ※x 86 Linuxでは PMDはメモリ上に存在しない (PGDからのアクセスはスルーしてPTにいく)

アドレス空間モデル task_struct mmap pgd vm_area_struct vm_next vm_file vm_start vm_end NULL mm PTE PGD virtual

アドレス空間モデル task_struct mmap pgd vm_area_struct vm_next vm_file vm_start vm_end NULL mm PTE PGD virtual address space PTE PMD Page cache File system Physical memory Swap cache Swap device

ページングの流れ Page faultのハンドリング YES Does the address belong to the process address space? Does

ページングの流れ Page faultのハンドリング YES Does the address belong to the process address space? Does the access type match the memory region access right? Legal access: Allocate a new Page frame NO YES Illegal access: Send a SIGSEGV signal handle_mm_fault() NO Did the exception occur in User Mode? Kernel bug: Kill the process NO

Page fault In interrupt or kernel thread NO YES Address in Memory region NO

Page fault In interrupt or kernel thread NO YES Address in Memory region NO Address could Belong to user Mode stack YES Write access NO NO YES Region is writable YES Copy on write NO Page is present Region is readable or executable Demand Paging YES NO Send SIGSEGV YES NO Kernel process And kernel “Oops” NO In user mode Address is a wrong system call parameter “Fixup code” (typically send SIGSEGV) NO YES

ページングの流れ ページ割り当て (1) NO Is the PTE present? YES Demand paging invoke NO do_no_page()

ページングの流れ ページ割り当て (1) NO Is the PTE present? YES Demand paging invoke NO do_no_page() NO Is the PTE empty? Have a file already mapped? invoke do_anonymous_page() YES NO Write access? Allocate a page & 0 clear & Set PTE as writable invoke do_swap_page() YES ※ 2 invoke vma->vm_ops->no_page() ※ 1 Mapped ZERO_PAGE & Set PTE as Read. Only ※ 3

ページングの流れ ページ割り当て(2) Demand paging ※ 1 NO invoke page_cache_read() Allocate a page & Set

ページングの流れ ページ割り当て(2) Demand paging ※ 1 NO invoke page_cache_read() Allocate a page & Set PTE & load the file Found the page in page cache? NO load the file YES Is the cache valid? NO YES Is the page to be YES shared? Allocate a page & Set PTE & load the file Set PTE as shared page

ページングの流れ ページ割り当て(3) ※ 2 Demand paging NO invoke swap_in() NO Is read access or

ページングの流れ ページ割り当て(3) ※ 2 Demand paging NO invoke swap_in() NO Is read access or shared? Set PTE with Writable&dirty Is present vm_ops->swapin? YES Set PTE YES Is swap cache present? NO Make a swap cache

ページングの流れ ページ割り当て(4) ※ 3 Set PTE & aging NO Do nothing YES Is write

ページングの流れ ページ割り当て(4) ※ 3 Set PTE & aging NO Do nothing YES Is write access? NO YES invoke Is writable page? do_wp_page() Set PTE with dirty flag ※ 4 Copy on write

ページングの流れ ページ割り当て(5) Copy on write ※ 4 Does multiple Processes refer YES the page?

ページングの流れ ページ割り当て(5) Copy on write ※ 4 Does multiple Processes refer YES the page? Set PTE as writable Allocate a page & &dirty (copy no page) Set PTE as writable & Copy data from old page NO

スワップ処理の流れ The page fault handler must swap in a page A page must be

スワップ処理の流れ The page fault handler must swap in a page A page must be swapped out swap_out() do_swap_page() swap_out_process() swap_in() swap_out_vma() swapin_readahead() swap_out_pgd() read_swap_cache_async() swap_out_pmd() try_to_swap_out() rw_swap_page() Low-level swapping function brw_page() Block device driver function

フリーページの取得処理 (2) • swap_out() for (counter=nr_tasks/(priority+1); counter--) { int max_cnt=0; struct task_struct *pbest; for

フリーページの取得処理 (2) • swap_out() for (counter=nr_tasks/(priority+1); counter--) { int max_cnt=0; struct task_struct *pbest; for (init以外のすべてのプロセス) { if (最近スワップアウトされていない && 使用物理ページ数 > max_cnt) { max_cnt = 使用物理ページ数; pbest = 当該プロセス; } } swap_out_process(pbest, gfp_mask); }

メモリ領域管理機構 (1) Buddy system 10 kbytes の メモリの要求 pager 2^2 page*1 の メモリの割り当て Free

メモリ領域管理機構 (1) Buddy system 10 kbytes の メモリの要求 pager 2^2 page*1 の メモリの割り当て Free area list (n *page/block) n= 2^0 1 page … n= 2^1 2 page merge n= 2^2 4 page split n= 2^3 … 8 page

メモリ領域管理機構 (2) Slab allocator Page-level allocator (buddy system) back end vnode cache active vnodes

メモリ領域管理機構 (2) Slab allocator Page-level allocator (buddy system) back end vnode cache active vnodes coloring area N page free active proc cache active procs file … cache Slab allocator front end active files unused free active NULL Slab data Linked list

メモリ領域管理機構 (2) Slab allocator • Linux で使われている slab の種類 – カーネル内で利用される各structure用のブロック • slabinfo, kmem_cache,

メモリ領域管理機構 (2) Slab allocator • Linux で使われている slab の種類 – カーネル内で利用される各structure用のブロック • slabinfo, kmem_cache, tcp_tw_bucket, tcp_bind_bucket, tcp_open_request, inet_peer_cache, ip_fib_hash, ip_dst_cache, arp_cache, uhci_urb_priv, blkdev_requests, nfs_read_data, nfs_inode_cache, nfs_write_data, nfs_page, journal_head, revoke_table, revoke_record, dnotify, file, fasync, uid_cache, skbuff_head_cache, sock, sigqueue, kiobuf, cdev_cache, bdev_cache, mnt_cache, inode_cache, dentry_cache, filp, names_cache, buffer_head, mm_struct, vm_area_struct, fs_cache, files_cache – 2^{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}bytesのブロック • 通常/DMA用 – ※ /proc/slabinfo で確認可能

参考文献 • Understanding Linux Kernel – Daniel P. Bovet & Marco Cesati, O’reilly, 2001

参考文献 • Understanding Linux Kernel – Daniel P. Bovet & Marco Cesati, O’reilly, 2001 • UNIX Internals: The New Frontiers – Uresh Vahalia, Prentice Hall, 1996 • Intel Architecture Developers Manual Vol 1, 2, 3 – Intel Corp. , 1999