요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
=======================
Kernel Samepage Merging
=======================
KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
added to the Linux kernel in 2.6.32. See ``mm/ksm.c`` for its implementation,
and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/
The userspace interface of KSM is described in Documentation/admin-guide/mm/ksm.rst
Design
======
Overview
--------
.. kernel-doc:: mm/ksm.c
:DOC: Overview
Reverse mapping
---------------
KSM maintains reverse mapping information for KSM pages in the stable
tree.
If a KSM page is shared between less than ``max_page_sharing`` VMAs,
the node of the stable tree that represents such KSM page points to a
list of struct ksm_rmap_item and the ``page->mapping`` of the
KSM page points to the stable tree node.
When the sharing passes this threshold, KSM adds a second dimension to
the stable tree. The tree node becomes a "chain" that links one or
more "dups". Each "dup" keeps reverse mapping information for a KSM
page with ``page->mapping`` pointing to that "dup".
Every "chain" and all "dups" linked into a "chain" enforce the
invariant that they represent the same write protected memory content,
even if each "dup" will be pointed by a different KSM page copy of
that content.
This way the stable tree lookup computational complexity is unaffected
if compared to an unlimited list of reverse mappings. It is still
enforced that there cannot be KSM page content duplicates in the
stable tree itself.
The deduplication limit enforced by ``max_page_sharing`` is required
to avoid the virtual memory rmap lists to grow too large. The rmap
walk has O(N) complexity where N is the number of rmap_items
(i.e. virtual mappings) that are sharing the page, which is in turn
capped by ``max_page_sharing``. So this effectively spreads the linear
O(N) computational complexity from rmap walk context over different
KSM pages. The ksmd walk over the stable_node "chains" is also O(N),
but N is the number of stable_node "dups", not the number of
rmap_items, so it has not a significant impact on ksmd performance. In
practice the best stable_node "dup" candidate will be kept and found
at the head of the "dups" list.
High values of ``max_page_sharing`` result in faster memory merging
(because there will be fewer stable_node dups queued into the
stable_node chain->hlist to check for pruning) and higher
deduplication factor at the expense of slower worst case for rmap
walks for any KSM page which can happen during swapping, compaction,
NUMA balancing and page migration.
The ``stable_node_dups/stable_node_chains`` ratio is also affected by the
``max_page_sharing`` tunable, and an high ratio may indicate fragmentation
in the stable_node dups, which could be solved by introducing
fragmentation algorithms in ksmd which would refile rmap_items from
one stable_node dup to another stable_node dup, in order to free up
stable_node "dups" with few rmap_items in them, but that may increase
the ksmd CPU usage and possibly slowdown the readonly computations on
the KSM pages of the applications.
The whole list of stable_node "dups" linked in the stable_node
"chains" is scanned periodically in order to prune stale stable_nodes.
The frequency of such scans is defined by
``stable_node_chains_prune_millisecs`` sysfs tunable.
Reference
---------
.. kernel-doc:: mm/ksm.c
:functions: mm_slot ksm_scan stable_node rmap_item
--
Izik Eidus,
Hugh Dickins, 17 Nov 2009
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
KSM 소개
1-10KSM은 `CONFIG_KSM=y`로 활성화하는 memory-saving deduplication 기능이며 Linux kernel 2.6.32에 추가됐습니다. 구현은 `mm/ksm.c`에 있고 배경 설명은 `http://lwn.net/Articles/306704/`와 `https://lwn.net/Articles/330589/`에서 볼 수 있습니다.
KSM의 userspace interface는 `Documentation/admin-guide/mm/ksm.rst`에 설명돼 있습니다.
=======================
Kernel Samepage Merging
=======================
KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
added to the Linux kernel in 2.6.32. See ``mm/ksm.c`` for its implementation,
and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/
The userspace interface of KSM is described in Documentation/admin-guide/mm/ksm.rst
설계 개요
11-19KSM 설계 개요는 `mm/ksm.c`의 `Overview` kernel-doc에서 직접 가져옵니다.
Design
======
Overview
--------
.. kernel-doc:: mm/ksm.c
:DOC: Overview
Reverse mapping과 sharing 상한
20-77KSM은 stable tree에서 KSM page의 reverse-mapping 정보를 유지합니다.
KSM page를 공유하는 VMA 수가 `max_page_sharing`보다 적으면 그 page를 나타내는 stable-tree node가 `struct ksm_rmap_item` list를 가리키고, KSM page의 `page->mapping`은 stable-tree node를 가리킵니다.
Sharing이 이 threshold를 넘으면 KSM은 stable tree에 두 번째 차원을 추가합니다. Tree node는 하나 이상의 `dup`을 연결하는 `chain`이 됩니다. 각 `dup`은 KSM page 하나의 reverse-mapping 정보를 보관하며 그 page의 `page->mapping`이 해당 `dup`을 가리킵니다.
각 `chain`과 거기에 연결된 모든 `dup`은 서로 다른 KSM page copy가 각 `dup`을 가리키더라도 모두 같은 write-protected memory content를 나타낸다는 invariant를 강제합니다.
이 구조 덕분에 무제한 reverse-mapping list와 비교해도 stable-tree lookup의 computational complexity가 증가하지 않습니다. Stable tree 자체에는 같은 content를 가진 KSM page duplicate가 존재할 수 없다는 조건도 유지합니다.
`max_page_sharing`이 강제하는 deduplication limit는 virtual-memory rmap list가 지나치게 커지는 것을 막기 위해 필요합니다. Rmap walk는 page를 공유하는 `rmap_item`, 즉 virtual mapping 수 N에 대해 O(N)이며 N은 `max_page_sharing`으로 제한됩니다. 따라서 rmap-walk context의 선형 O(N) 비용을 여러 KSM page로 분산합니다. Stable-node `chain`을 도는 `ksmd` walk도 O(N)이지만 여기서 N은 `rmap_item` 수가 아니라 stable-node `dup` 수이므로 `ksmd` 성능에 미치는 영향은 크지 않습니다. 실제로는 가장 좋은 stable-node `dup` candidate를 `dups` list의 head에 보관해 먼저 찾습니다.
`max_page_sharing` 값이 크면 pruning 대상으로 검사할 stable-node `dup` 수가 줄어 memory merging이 빨라지고 deduplication factor가 높아집니다. 대신 swapping, compaction, NUMA balancing과 page migration 중 발생할 수 있는 KSM page의 최악 rmap-walk 시간이 길어집니다.
`stable_node_dups/stable_node_chains` 비율도 `max_page_sharing` tunable의 영향을 받습니다. 비율이 높으면 stable-node `dup` fragmentation을 뜻할 수 있습니다. `ksmd`에 fragmentation algorithm을 넣어 `rmap_item`을 한 stable-node `dup`에서 다른 `dup`으로 옮기면 `rmap_item`이 적은 `dup`을 해제할 수 있습니다. 다만 `ksmd` CPU 사용량이 늘고 application KSM page의 read-only computation이 느려질 수 있습니다.
Stable-node `chain`에 연결된 `dup` 전체 list를 주기적으로 scan해 stale stable node를 제거합니다. Scan 주기는 `stable_node_chains_prune_millisecs` sysfs tunable로 정합니다.
Reverse mapping
---------------
KSM maintains reverse mapping information for KSM pages in the stable
tree.
If a KSM page is shared between less than ``max_page_sharing`` VMAs,
the node of the stable tree that represents such KSM page points to a
list of struct ksm_rmap_item and the ``page->mapping`` of the
KSM page points to the stable tree node.
When the sharing passes this threshold, KSM adds a second dimension to
the stable tree. The tree node becomes a "chain" that links one or
more "dups". Each "dup" keeps reverse mapping information for a KSM
page with ``page->mapping`` pointing to that "dup".
Every "chain" and all "dups" linked into a "chain" enforce the
invariant that they represent the same write protected memory content,
even if each "dup" will be pointed by a different KSM page copy of
that content.
This way the stable tree lookup computational complexity is unaffected
if compared to an unlimited list of reverse mappings. It is still
enforced that there cannot be KSM page content duplicates in the
stable tree itself.
The deduplication limit enforced by ``max_page_sharing`` is required
to avoid the virtual memory rmap lists to grow too large. The rmap
walk has O(N) complexity where N is the number of rmap_items
(i.e. virtual mappings) that are sharing the page, which is in turn
capped by ``max_page_sharing``. So this effectively spreads the linear
O(N) computational complexity from rmap walk context over different
KSM pages. The ksmd walk over the stable_node "chains" is also O(N),
but N is the number of stable_node "dups", not the number of
rmap_items, so it has not a significant impact on ksmd performance. In
practice the best stable_node "dup" candidate will be kept and found
at the head of the "dups" list.
High values of ``max_page_sharing`` result in faster memory merging
(because there will be fewer stable_node dups queued into the
stable_node chain->hlist to check for pruning) and higher
deduplication factor at the expense of slower worst case for rmap
walks for any KSM page which can happen during swapping, compaction,
NUMA balancing and page migration.
The ``stable_node_dups/stable_node_chains`` ratio is also affected by the
``max_page_sharing`` tunable, and an high ratio may indicate fragmentation
in the stable_node dups, which could be solved by introducing
fragmentation algorithms in ksmd which would refile rmap_items from
one stable_node dup to another stable_node dup, in order to free up
stable_node "dups" with few rmap_items in them, but that may increase
the ksmd CPU usage and possibly slowdown the readonly computations on
the KSM pages of the applications.
The whole list of stable_node "dups" linked in the stable_node
"chains" is scanned periodically in order to prune stale stable_nodes.
The frequency of such scans is defined by
``stable_node_chains_prune_millisecs`` sysfs tunable.
구조체 참고
78-85Reference kernel-doc은 `mm/ksm.c`의 `mm_slot`, `ksm_scan`, `stable_node`, `rmap_item`을 다룹니다.
Izik Eidus, Hugh Dickins, 2009년 11월 17일
Reference
---------
.. kernel-doc:: mm/ksm.c
:functions: mm_slot ksm_scan stable_node rmap_item
--
Izik Eidus,
Hugh Dickins, 17 Nov 2009
요약·해설
ksm.rst:1-85KSM은 내용이 같은 anonymous page를 write-protect한 공유 page로 합칩니다. Stable tree는 content 중복을 막고 reverse mapping을 추적하며, 한 page에 mapping이 너무 많이 몰리면 `chain` 아래 여러 `dup`으로 나눠 rmap-walk 비용을 `max_page_sharing` 이하로 제한합니다.
Sharing 수가 threshold를 넘을 때 reverse mapping을 두 번째 차원으로 분산합니다.
큰 값은 merge 효율을 높이지만 최악의 rmap walk를 길게 만듭니다.