[RFC 04/14] fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE

From: Pasha Tatashin
Date: Mon Mar 11 2024 - 12:47:32 EST


In many places number of pages in the stack is detremined 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.

However, with dynamic stacks, the number of pages in vm_area will grow
with stack, therefore, use vm_area->nr_pages to determine the actual
number of pages allocated in stack.

Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
---
kernel/fork.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index 60e812825a7a..a35f4008afa0 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -243,13 +243,11 @@ static int free_vm_stack_cache(unsigned int cpu)

static int memcg_charge_kernel_stack(struct vm_struct *vm)
{
- int i;
- int ret;
+ int i, ret, nr_pages;
int nr_charged = 0;

- BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
-
- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
+ nr_pages = vm->nr_pages;
+ for (i = 0; i < nr_pages; i++) {
ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 0);
if (ret)
goto err;
@@ -531,9 +529,10 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
{
if (IS_ENABLED(CONFIG_VMAP_STACK)) {
struct vm_struct *vm = task_stack_vm_area(tsk);
- int i;
+ int i, nr_pages;

- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+ nr_pages = vm->nr_pages;
+ for (i = 0; i < nr_pages; i++)
mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB,
account * (PAGE_SIZE / 1024));
} else {
@@ -551,10 +550,11 @@ void exit_task_stack_account(struct task_struct *tsk)

if (IS_ENABLED(CONFIG_VMAP_STACK)) {
struct vm_struct *vm;
- int i;
+ int i, nr_pages;

vm = task_stack_vm_area(tsk);
- for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+ nr_pages = vm->nr_pages;
+ for (i = 0; i < nr_pages; i++)
memcg_kmem_uncharge_page(vm->pages[i], 0);
}
}
--
2.44.0.278.ge034bb2e1d-goog