[PATCH v2 09/13] fork: Dynamic Kernel Stack accounting

From: David Stevens

Date: Fri Apr 24 2026 - 15:22:08 EST


From: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>

Add an accounting of the amount of stack pages that have been faulted in
and are currently in use.

Example use case:
$ cat /proc/vmstat | grep stack
nr_kernel_stack 18684
nr_dynamic_stacks_faults 156

The above shows that the kernel stacks use total 18684KiB, out of which
156KiB were faulted in.

Given that the pre-allocated stacks are 4KiB, we can determine the total
number of tasks:

tasks = (nr_kernel_stack - nr_dynamic_stacks_faults) / 4 = 4632.

The amount of kernel stack memory without dynamic stack on this machine
would be:

4632 * 16 KiB = 74,112 KiB

Therefore, in this example dynamic stacks save: 55,428 KiB

Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
[Rebased]
Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
[add to memcg stats, fix typos]
Signed-off-by: David Stevens <stevensd@xxxxxxxxxx>
---
include/linux/mmzone.h | 3 +++
kernel/fork.c | 12 +++++++++++-
mm/memcontrol.c | 10 ++++++++++
mm/vmstat.c | 3 +++
4 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 3e51190a55e4..4458fa7016a1 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -221,6 +221,9 @@ enum node_stat_item {
NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */
NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */
NR_KERNEL_STACK_KB, /* measured in KiB */
+#ifdef CONFIG_DYNAMIC_STACK
+ NR_DYNAMIC_STACKS_FAULTS_KB, /* KiB of faulted kernel stack memory */
+#endif
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
NR_KERNEL_SCS_KB, /* measured in KiB */
#endif
diff --git a/kernel/fork.c b/kernel/fork.c
index e615ef736dc0..9ac9d23f5f4b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -463,6 +463,8 @@ unsigned long dynamic_stack_accounting(struct task_struct *tsk, bool finalize)

mod_lruvec_page_state(page, NR_KERNEL_STACK_KB,
PAGE_SIZE / 1024);
+ mod_lruvec_page_state(page, NR_DYNAMIC_STACKS_FAULTS_KB,
+ PAGE_SIZE / 1024);
}

if (finalize) {
@@ -811,9 +813,17 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
nr_accounted = vm_area->nr_pages;
#endif

- for (i = 0; i < nr_accounted; i++)
+ for (i = 0; i < nr_accounted; i++) {
mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB,
account * (PAGE_SIZE / 1024));
+#ifdef CONFIG_DYNAMIC_STACK
+ if (i >= THREAD_PREALLOC_PAGES) {
+ mod_lruvec_page_state(vm_area->pages[i],
+ NR_DYNAMIC_STACKS_FAULTS_KB,
+ account * (PAGE_SIZE / 1024));
+ }
+#endif
+ }
} else {
void *stack = task_stack_page(tsk);

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 772bac21d155..cd2195a735ab 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -318,6 +318,9 @@ static const unsigned int memcg_node_stat_items[] = {
NR_FILE_THPS,
NR_ANON_THPS,
NR_KERNEL_STACK_KB,
+#ifdef CONFIG_DYNAMIC_STACK
+ NR_DYNAMIC_STACKS_FAULTS_KB,
+#endif
NR_PAGETABLE,
NR_SECONDARY_PAGETABLE,
#ifdef CONFIG_SWAP
@@ -1403,6 +1406,10 @@ static const struct memory_stat memory_stats[] = {
#ifdef CONFIG_NUMA_BALANCING
{ "pgpromote_success", PGPROMOTE_SUCCESS },
#endif
+
+#ifdef CONFIG_DYNAMIC_STACK
+ { "dynamic_stack_faults", NR_DYNAMIC_STACKS_FAULTS_KB },
+#endif
};

/* The actual unit of the state item, not the same as the output unit */
@@ -1415,6 +1422,9 @@ static int memcg_page_state_unit(int item)
case NR_SLAB_UNRECLAIMABLE_B:
return 1;
case NR_KERNEL_STACK_KB:
+#ifdef CONFIG_DYNAMIC_STACK
+ case NR_DYNAMIC_STACKS_FAULTS_KB:
+#endif
return SZ_1K;
default:
return PAGE_SIZE;
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 86b14b0f77b5..8fa1c7bcbaea 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1256,6 +1256,9 @@ const char * const vmstat_text[] = {
[I(NR_FOLL_PIN_ACQUIRED)] = "nr_foll_pin_acquired",
[I(NR_FOLL_PIN_RELEASED)] = "nr_foll_pin_released",
[I(NR_KERNEL_STACK_KB)] = "nr_kernel_stack",
+#ifdef CONFIG_DYNAMIC_STACK
+ [I(NR_DYNAMIC_STACKS_FAULTS_KB)] = "nr_dynamic_stacks_faults",
+#endif
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
[I(NR_KERNEL_SCS_KB)] = "nr_shadow_call_stack",
#endif
--
2.54.0.rc2.544.gc7ae2d5bb8-goog