v1 2026-09-18

cgroup v2 实现 memsw 限额 · v1

在 cgroup v2 上实现类似 v1 memsw.limit_in_bytes 的内存加 swap 合计限额,避免仅靠 memory.max 时通过换页绕过总量限制。

原始补丁链接

子 Patch(6)

1/6
mm: page_counter: add page_counter_protection struct and init API
新增 page_counter_protection 并用 page_counter->prot 挂接,使 swap/kmem/hugetlb 等计数器不再内嵌保护状态;调整 page_counter_init 和缓存布局,仅支持保护的计数器挂结构。
2/6
mm: page_counter: track protection state in page_counter_protection
把 min/low、usage/children 等层次保护状态及 propagate_protected_usage、set_min/low、calculate_protection 迁移到 page_counter_protection,memcg/dmem 改读新结构,暂无功能变化。
3/6
mm: page_counter: drop protection fields from struct page_counter
从 struct page_counter 删除 min/low、usage/children 和 protection_support 等字段及填充,使非 memcg/dmem 计数器不再携带无用保护状态,64 位下缩减一个 cache line。
4/6
mm: memcontrol: give swap and memsw their own page counters
拆分 mem_cgroup 中 union 的 swap/memsw 计数器并让 v2 也维护 memsw;do_memsw_account 不再 gate try_charge/uncharge/swap charge 路径,为组合内存+swap 限制铺路,默认行为暂不变。
5/6
mm: memcontrol: add memory.memsw.max to the default hierarchy
在 cgroup v2 暴露 memory.memsw.current/max,基于已拆分的 memsw 计数器实现 memory+swap 总和硬限;写 max 时禁用 MAY_SWAP 回收并校验 memory.max<=memsw.max,OOM 兜底。
6/6
mm: memcontrol: clamp mem_cgroup_get_max() to the combined limit
mem_cgroup_get_max 默认层次分支改为受 memory.memsw.max 约束,避免把 memory.max 与 swap.max 相加高估 totalpages,导致 oom_badness 过度放大 oom_score_adj。