diff mbox series

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

Message ID 20240311164638.2015063-5-pasha.tatashin@soleen.com (mailing list archive)
State New
Headers show
Series Dynamic Kernel Stacks | expand

Commit Message

Pasha Tatashin March 11, 2024, 4:46 p.m. UTC
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@soleen.com>
---
 kernel/fork.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

Comments

Christophe JAILLET March 17, 2024, 2:45 p.m. UTC | #1
Le 11/03/2024 à 17:46, Pasha Tatashin a écrit :
> 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@soleen.com>
> ---
>   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)

Maybe s/vm/vm_area/ as done in 03/14?

CJ

>   {
> -	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);
>   	}
>   }
Pasha Tatashin March 17, 2024, 3:14 p.m. UTC | #2
On Sun, Mar 17, 2024 at 10:45 AM Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> Le 11/03/2024 à 17:46, Pasha Tatashin a écrit :
> > 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@soleen.com>
> > ---
> >   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)
>
> Maybe s/vm/vm_area/ as done in 03/14?

Yes, I will add it to 03/14.

Thank you,
Pasha

>
> CJ
>
> >   {
> > -     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);
> >       }
> >   }
>
diff mbox series

Patch

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);
 	}
 }