mm/hugetlb_cgroup.c
Source file repositories/reference/linux-study-clean/mm/hugetlb_cgroup.c
File Facts
- System
- Linux kernel
- Corpus path
mm/hugetlb_cgroup.c- Extension
.c- Size
- 23874 bytes
- Lines
- 921
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/cgroup.hlinux/page_counter.hlinux/slab.hlinux/hugetlb.hlinux/hugetlb_cgroup.h
Detected Declarations
function __hugetlb_cgroup_counter_from_cgroupfunction hugetlb_cgroup_counter_from_cgroupfunction hugetlb_cgroup_counter_from_cgroup_rsvdfunction hugetlb_cgroup_is_rootfunction parent_hugetlb_cgroupfunction hugetlb_cgroup_have_usagefunction for_each_hstatefunction hugetlb_cgroup_initfunction hugetlb_cgroup_freefunction hugetlb_cgroup_css_allocfunction hugetlb_cgroup_css_freefunction hugetlb_cgroup_move_parentfunction hugetlb_cgroup_css_offlinefunction for_each_hstatefunction hugetlb_eventfunction __hugetlb_cgroup_charge_cgroupfunction hugetlb_cgroup_charge_cgroupfunction hugetlb_cgroup_charge_cgroup_rsvdfunction __hugetlb_cgroup_commit_chargefunction hugetlb_cgroup_commit_chargefunction hugetlb_cgroup_commit_charge_rsvdfunction __hugetlb_cgroup_uncharge_foliofunction hugetlb_cgroup_uncharge_foliofunction hugetlb_cgroup_uncharge_folio_rsvdfunction __hugetlb_cgroup_uncharge_cgroupfunction hugetlb_cgroup_uncharge_cgroupfunction hugetlb_cgroup_uncharge_cgroup_rsvdfunction hugetlb_cgroup_uncharge_counterfunction hugetlb_cgroup_uncharge_file_regionfunction hugetlb_cgroup_read_numa_statfunction css_for_each_descendant_prefunction hugetlb_cgroup_read_u64function hugetlb_cgroup_read_u64_maxfunction hugetlb_cgroup_writefunction hugetlb_cgroup_write_legacyfunction hugetlb_cgroup_write_dflfunction hugetlb_cgroup_resetfunction __hugetlb_events_showfunction hugetlb_events_showfunction hugetlb_events_local_showfunction hugetlb_cgroup_cfttypes_initfunction __hugetlb_cgroup_file_dfl_initfunction __hugetlb_cgroup_file_legacy_initfunction __hugetlb_cgroup_file_initfunction __hugetlb_cgroup_file_pre_initfunction __hugetlb_cgroup_file_post_initfunction hugetlb_cgroup_file_initfunction hugetlb_cgroup_migrate
Annotated Snippet
if (parent_h_cgroup) {
fault_parent = hugetlb_cgroup_counter_from_cgroup(
parent_h_cgroup, idx);
rsvd_parent = hugetlb_cgroup_counter_from_cgroup_rsvd(
parent_h_cgroup, idx);
}
fault = hugetlb_cgroup_counter_from_cgroup(h_cgroup, idx);
rsvd = hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx);
page_counter_init(fault, fault_parent, false);
page_counter_init(rsvd, rsvd_parent, false);
if (!cgroup_subsys_on_dfl(hugetlb_cgrp_subsys)) {
fault->track_failcnt = true;
rsvd->track_failcnt = true;
}
limit = round_down(PAGE_COUNTER_MAX,
pages_per_huge_page(&hstates[idx]));
VM_BUG_ON(page_counter_set_max(fault, limit));
VM_BUG_ON(page_counter_set_max(rsvd, limit));
}
}
static void hugetlb_cgroup_free(struct hugetlb_cgroup *h_cgroup)
{
int node;
for_each_node(node)
kfree(h_cgroup->nodeinfo[node]);
kfree(h_cgroup);
}
static struct cgroup_subsys_state *
hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
{
struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css);
struct hugetlb_cgroup *h_cgroup;
int node;
h_cgroup = kzalloc_flex(*h_cgroup, nodeinfo, nr_node_ids);
if (!h_cgroup)
return ERR_PTR(-ENOMEM);
if (!parent_h_cgroup)
root_h_cgroup = h_cgroup;
/*
* TODO: this routine can waste much memory for nodes which will
* never be onlined. It's better to use memory hotplug callback
* function.
*/
for_each_node(node) {
/* Set node_to_alloc to NUMA_NO_NODE for offline nodes. */
int node_to_alloc =
node_state(node, N_NORMAL_MEMORY) ? node : NUMA_NO_NODE;
h_cgroup->nodeinfo[node] =
kzalloc_node(sizeof(struct hugetlb_cgroup_per_node),
GFP_KERNEL, node_to_alloc);
if (!h_cgroup->nodeinfo[node])
goto fail_alloc_nodeinfo;
}
hugetlb_cgroup_init(h_cgroup, parent_h_cgroup);
return &h_cgroup->css;
fail_alloc_nodeinfo:
hugetlb_cgroup_free(h_cgroup);
return ERR_PTR(-ENOMEM);
}
static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)
{
hugetlb_cgroup_free(hugetlb_cgroup_from_css(css));
}
/*
* Should be called with hugetlb_lock held.
* Since we are holding hugetlb_lock, pages cannot get moved from
* active list or uncharged from the cgroup, So no need to get
* page reference and test for page active here. This function
* cannot fail.
*/
static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
struct folio *folio)
{
unsigned int nr_pages;
struct page_counter *counter;
Annotation
- Immediate include surface: `linux/cgroup.h`, `linux/page_counter.h`, `linux/slab.h`, `linux/hugetlb.h`, `linux/hugetlb_cgroup.h`.
- Detected declarations: `function __hugetlb_cgroup_counter_from_cgroup`, `function hugetlb_cgroup_counter_from_cgroup`, `function hugetlb_cgroup_counter_from_cgroup_rsvd`, `function hugetlb_cgroup_is_root`, `function parent_hugetlb_cgroup`, `function hugetlb_cgroup_have_usage`, `function for_each_hstate`, `function hugetlb_cgroup_init`, `function hugetlb_cgroup_free`, `function hugetlb_cgroup_css_alloc`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.