[RFC PATCH 01/15] fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE
From: Mostafa Saleh
Date: Mon Sep 28 2026 - 13:55:41 EST
From: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
In many places number of pages in the stack is determined via
(THREAD_SIZE / PAGE_SIZE). There is also a BUG_ON() that ensures that
(THREAD_SIZE / PAGE_SIZE) is indeed equals to vm_area->nr_pages.
In the next patches the kernel stack size will not be a compile
time constant and would be less than or equal THREAD_SIZE based
on a command line arg. Therefore, use vm_area->nr_pages to
determine the actual number of pages allocated in stack.
Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
[Rebased, also skipped intermediary helper variable nr_pages]
Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
Signed-off-by: David Stevens <stevensd@xxxxxxxxxx>
[Update commit message]
Signed-off-by: Mostafa Saleh <smostafa@xxxxxxxxxx>
---
kernel/fork.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index 5ef413368912..2065693e762c 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -315,9 +315,7 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm_area)
int ret;
int nr_charged = 0;
- BUG_ON(vm_area->nr_pages != THREAD_SIZE / PAGE_SIZE);
-
- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
+ for (i = 0; i < vm_area->nr_pages; i++) {
ret = memcg_kmem_charge_page(vm_area->pages[i], GFP_KERNEL, 0);
if (ret)
goto err;
@@ -488,7 +486,7 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
struct vm_struct *vm_area = task_stack_vm_area(tsk);
int i;
- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+ for (i = 0; i < vm_area->nr_pages; i++)
mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB,
account * (PAGE_SIZE / 1024));
} else {
@@ -509,7 +507,7 @@ void exit_task_stack_account(struct task_struct *tsk)
int i;
vm_area = task_stack_vm_area(tsk);
- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+ for (i = 0; i < vm_area->nr_pages; i++)
memcg_kmem_uncharge_page(vm_area->pages[i], 0);
}
}
--
2.56.0.rc1.315.gc6ed9934b7-goog