From: Mateusz Guzik <mjguzik@gmail.com>
To: Linus Walleij <linus.walleij@linaro.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, Pasha Tatashin <pasha.tatashin@soleen.com>
Subject: Re: [PATCH v2 3/5] fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE
Date: Wed, 7 May 2025 18:56:27 +0200 [thread overview]
Message-ID: <sqssqrn2gcisl3hwz7br7qhpnaklfu7ck6zuhlpv5ewwqfge3j@mlbmvvgf73bo> (raw)
In-Reply-To: <20250507-fork-fixes-v2-3-82ab1e42cde3@linaro.org>
On Wed, May 07, 2025 at 02:46:29PM +0200, Linus Walleij wrote:
> From: Pasha Tatashin <pasha.tatashin@soleen.com>
>
> 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 equal to vm_area->nr_pages.
>
> Consistently use vm_area->nr_pages to determine the actual number
> of pages allocated in the stack instead, so it is clear what is
> going on here.
>
> The assignment of a local variable nr_pages in
> memcg_charge_kernel_stack() takes roughly the same amount of time
> as the BUG() assertion, and the two other sites arguably should
> have had a BUG() assertion as well.
>
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Link: https://lore.kernel.org/20240311164638.2015063-5-pasha.tatashin@soleen.com
> [linus.walleij@linaro.orh: Rebased, initialized helpers variables in declaration]
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> kernel/fork.c | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 8b8457562740c114c640a8cc230876f6a286b246..c60a0ec61a421324e3733b506d99531c5965cbc6 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -253,10 +253,9 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm_area)
> int i;
> int ret;
> int nr_charged = 0;
> + int nr_pages = vm_area->nr_pages;
>
> - BUG_ON(vm_area->nr_pages != THREAD_SIZE / PAGE_SIZE);
> -
> - for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
> + for (i = 0; i < nr_pages; i++) {
> ret = memcg_kmem_charge_page(vm_area->pages[i], GFP_KERNEL, 0);
> if (ret)
> goto err;
> @@ -516,9 +515,10 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
> {
> if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> struct vm_struct *vm_area = task_stack_vm_area(tsk);
> + int nr_pages = vm_area->nr_pages;
> int i;
>
> - for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
> + for (i = 0; i < nr_pages; i++)
> mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB,
> account * (PAGE_SIZE / 1024));
> } else {
> @@ -535,11 +535,11 @@ void exit_task_stack_account(struct task_struct *tsk)
> account_kernel_stack(tsk, -1);
>
> if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> - struct vm_struct *vm_area;
> + struct vm_struct *vm_area = task_stack_vm_area(tsk);
> + int nr_pages = vm_area->nr_pages;
> int i;
>
> - vm_area = task_stack_vm_area(tsk);
> - for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
> + for (i = 0; i < nr_pages; i++)
> memcg_kmem_uncharge_page(vm_area->pages[i], 0);
> }
> }
>
> --
> 2.49.0
>
>
I think the change makes it very much less clear what's going on here as it
suggests the page count for stacks is variable instead of fixed.
I can agree "THREAD_SIZE / PAGE_SIZE" open-coded is not the best way to
express it though.
How about THREAD_STACK_PAGE_COUNT or similar to hide the above division?
next prev parent reply other threads:[~2025-05-07 16:56 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-07 12:46 [PATCH v2 0/5] fork: Page operation cleanups in the fork code Linus Walleij
2025-05-07 12:46 ` [PATCH v2 1/5] fork: Clean-up ifdef logic around stack allocation Linus Walleij
2025-05-13 6:29 ` Mike Rapoport
2025-05-07 12:46 ` [PATCH v2 2/5] fork: Clean-up naming of vm_stack/vm_struct variables in vmap stacks code Linus Walleij
2025-05-13 6:33 ` Mike Rapoport
2025-05-07 12:46 ` [PATCH v2 3/5] fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE Linus Walleij
2025-05-07 16:56 ` Mateusz Guzik [this message]
2025-05-09 5:44 ` Linus Walleij
2025-05-07 12:46 ` [PATCH v2 4/5] fork: check charging success before zeroing stack Linus Walleij
2025-05-13 7:38 ` Mike Rapoport
2025-05-07 12:46 ` [PATCH v2 5/5] fork: zero vmap stack using clear_page() instead of memset() Linus Walleij
2025-05-07 16:51 ` Mateusz Guzik
2025-05-09 5:49 ` Linus Walleij
2025-05-07 17:03 ` [PATCH v2 0/5] fork: Page operation cleanups in the fork code Mateusz Guzik
2025-05-09 6:57 ` Linus Walleij
2025-05-09 11:16 ` Mateusz Guzik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sqssqrn2gcisl3hwz7br7qhpnaklfu7ck6zuhlpv5ewwqfge3j@mlbmvvgf73bo \
--to=mjguzik@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linus.walleij@linaro.org \
--cc=linux-mm@kvack.org \
--cc=pasha.tatashin@soleen.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox