From: Pasha Tatashin <[email protected]>

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 <[email protected]>
[Rebased, also skipped intermediary helper variable nr_pages]
Signed-off-by: Linus Walleij <[email protected]>
Signed-off-by: David Stevens <[email protected]>
[Update commit message]
Signed-off-by: Mostafa Saleh <[email protected]>
---
 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


Reply via email to