Skip to contentSkip to conversation

mm: Fix kernel stack tagging for certain configs

PATCH In review Vishal Moola (Oracle) 4 participants 9 messages Sep 2, 2025 – Sep 4, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Fix kernel stack tagging for certain configs

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

---
 kernel/fork.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/fork.c +8 -6
index 1b394426ab4a..9b13cb83e1c6 100644
@@ -448,14 +448,15 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
 	} else {
 		void *stack = task_stack_page(tsk);
 		struct page *page = virt_to_head_page(stack);
-		int i;
+		int i = 0;
 
 		/* All stack pages are in the same node. */
 		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
 				      account * (THREAD_SIZE / 1024));
 
-		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++, page++)
-			__SetPageStack(page);
+		do {
+			__SetPageStack(page++);
+		} while (++i < THREAD_SIZE / PAGE_SIZE);
 	}
 }
 
@@ -474,10 +475,11 @@ void exit_task_stack_account(struct task_struct *tsk)
 		}
 	} else {
 		struct page *page = virt_to_head_page(task_stack_page(tsk));
-		int i;
+		int i = 0;
 
-		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++, page++)
-			__ClearPageStack(page);
+		do {
+			__ClearPageStack(page++);
+		} while (++i < THREAD_SIZE / PAGE_SIZE);
 	}
 }
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 17 lines · Vishal Moola (Oracle)

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

Quoted 6 lines

---

You sent the patch on August 20 and I replied on August 21.

I did not receive any reply so far.

And now I realize that this patch is not upstream yet and the commit id not stable. So the Fixes/Closes etc. do not really apply.

My current opinion is that we don't want this. (see vmalloc reasoning and unclear use)

I'm happy to be convinced otherwise.

Signature · 3 lines
Cheers

David / dhildenb
#2 lore raw
Quoted 25 lines · David Hildenbrand
Quoted 17 lines · Vishal Moola (Oracle)

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

Quoted 6 lines

---

Quoted 3 lines

You sent the patch on August 20 and I replied on August 21.

I did not receive any reply so far.

Ah sorry, I didn't mean to miss your reply.

I can't find your reply in my inboxes so I definitely missed it somehow. I'll go find it and respond.

Quoted 2 lines

And now I realize that this patch is not upstream yet and the commit id not stable. So the Fixes/Closes etc. do not really apply.

Gotcha.

Quoted 10 lines

My current opinion is that we don't want this. (see vmalloc reasoning and unclear use)

I'm happy to be convinced otherwise.

Signature · 3 lines
Cheers

David / dhildenb
#3 lore raw
Quoted 26 lines · Vishal Moola (Oracle)
Quoted 25 lines · David Hildenbrand
Quoted 23 lines · Vishal Moola (Oracle)

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

---

Quoted 8 lines
Quoted 3 lines

You sent the patch on August 20 and I replied on August 21.

I did not receive any reply so far.

Ah sorry, I didn't mean to miss your reply.

I can't find your reply in my inboxes so I definitely missed it somehow. I'll go find it and respond.

I had a mail server config issue on one day last month (sending @redhat.com through kernel.org :) ), let me check if that was on that problematic day and it might have went straight into your spam folder due to dkim mismatch.

So the mailing list did not reject it:

https://lore.kernel.org/all/[email protected]/

And yes, indeed, it was on that problemtic day, and there is:

Received: from smtp.kernel.org

So, problem on my side. Willy already replied, but let me resend that mail.

Quoted 5 lines
Quoted 3 lines

And now I realize that this patch is not upstream yet and the commit id not stable. So the Fixes/Closes etc. do not really apply.

Gotcha.

If there are bigger changes it usually makes sense to send a v2, or a simple fixup as reply to the original patch (I prefer as inline reply).

Of course, once it's in mm-stable or upstream, things get more tricky :)

Signature · 3 lines
Cheers

David / dhildenb
#4 lore raw
Quoted 51 lines · David Hildenbrand
Quoted 26 lines · Vishal Moola (Oracle)
Quoted 25 lines · David Hildenbrand
Quoted 23 lines · Vishal Moola (Oracle)

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

---

Quoted 8 lines
Quoted 3 lines

You sent the patch on August 20 and I replied on August 21.

I did not receive any reply so far.

Ah sorry, I didn't mean to miss your reply.

I can't find your reply in my inboxes so I definitely missed it somehow. I'll go find it and respond.

I had a mail server config issue on one day last month (sending @redhat.com through kernel.org :) ), let me check if that was on that problematic day and it might have went straight into your spam folder due to dkim mismatch.

So the mailing list did not reject it:

https://lore.kernel.org/all/[email protected]/

And yes, indeed, it was on that problemtic day, and there is:

Received: from smtp.kernel.org

So, problem on my side. Willy already replied, but let me resend that mail.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

And now I realize that this patch is not upstream yet and the commit id not stable. So the Fixes/Closes etc. do not really apply.

Gotcha.

If there are bigger changes it usually makes sense to send a v2, or a simple fixup as reply to the original patch (I prefer as inline reply).

Ok I'll keep that in mind for the future :). My mail client loves attaching the fixup patches as additional files, but I'll find a workflow for inlining them.

Quoted 7 lines

Of course, once it's in mm-stable or upstream, things get more tricky :)

Signature · 3 lines
Cheers

David / dhildenb
#5 lore raw
Quoted 55 lines · Vishal Moola (Oracle)
Quoted 54 lines · David Hildenbrand
Quoted 26 lines · Vishal Moola (Oracle)
Quoted 25 lines · David Hildenbrand
Quoted 23 lines · Vishal Moola (Oracle)

Commit 4ef905bda61f ("mm: tag kernel stack pages") began marking pages that were being used for the kernel stack.

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

---

Quoted 8 lines
Quoted 3 lines

You sent the patch on August 20 and I replied on August 21.

I did not receive any reply so far.

Ah sorry, I didn't mean to miss your reply.

I can't find your reply in my inboxes so I definitely missed it somehow. I'll go find it and respond.

I had a mail server config issue on one day last month (sending @redhat.com through kernel.org :) ), let me check if that was on that problematic day and it might have went straight into your spam folder due to dkim mismatch.

So the mailing list did not reject it:

https://lore.kernel.org/all/[email protected]/

And yes, indeed, it was on that problemtic day, and there is:

Received: from smtp.kernel.org

So, problem on my side. Willy already replied, but let me resend that mail.

Quoted 4 lines
Quoted 3 lines

And now I realize that this patch is not upstream yet and the commit id not stable. So the Fixes/Closes etc. do not really apply.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Gotcha.

If there are bigger changes it usually makes sense to send a v2, or a simple fixup as reply to the original patch (I prefer as inline reply).

Ok I'll keep that in mind for the future :). My mail client loves attaching the fixup patches as additional files, but I'll find a workflow for inlining them.

Oh, I really just format-patch, gedit to then copy and paste.

Just be careful if your mail client wraps at 80 chars as default. I have Thunderbird plugin where I can easily toggle that (Toggle Line Wrap).

Signature · 3 lines
Cheers

David / dhildenb
#6 lore raw
In reply to Vishal Moola (Oracle)
Quoted 4 lines · Vishal Moola (Oracle)

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

I missed that the third case existed ...

Quoted 5 lines

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

I didn't see those smatch warnings. Were they cc'd to the mailing list?

Quoted 2 lines

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

We can't do this. Pages can only have one type at a time. Since they're allocated from slab, they have PGTY_slab. This will lead to a warning at runtime:

        VM_BUG_ON_PAGE(data_race(page->page_type) != UINT_MAX, page);   \

But for our purposes (trying to figure out how fragmented the vmap stack is making the memmap), we don't need to do this accounting. These pages are already being allocated from slab, and slab allocates naturally aligned, so we can skip all of this for these configurations.

(now I'll go back and reply to David's mail from the 21st which I missed)

#7 lore raw
Quoted 8 lines · Matthew Wilcox
Quoted 4 lines · Vishal Moola (Oracle)

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

I missed that the third case existed ...

Quoted 8 lines
Quoted 5 lines

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

I didn't see those smatch warnings. Were they cc'd to the mailing list?

I messed up and accidentally sent an email with two Message-ID headers so maybe it got eaten by your spam filter?

https://lore.kernel.org/all/[email protected]/

regards,
dan carpenter

#8 lore raw
In reply to Matthew Wilcox
Quoted 23 lines · Matthew Wilcox
Quoted 4 lines · Vishal Moola (Oracle)

There are 3 cases where kernel pages are allocated for kernel stacks:
CONFIG_VMAP_STACK, THREAD_SIZE >= PAGE_SIZE, THREAD_SIZE < PAGE_SIZE. These cases use vmalloc(), alloc_pages() and kmem_cache_alloc() respectively.

I missed that the third case existed ...

Quoted 5 lines

In the first 2 cases, THREAD_SIZE / PAGE_SIZE will always be greater than 0, and pages are tagged as expected. In the third case, THREAD_SIZE / PAGE_SIZE evaluates to 0 and doesn't tag any pages at all. This meant that in those configs, the stack tagging was a no-op, and led to smatch build warnings.

I didn't see those smatch warnings. Were they cc'd to the mailing list?

Quoted 2 lines

We definitely have at least 1 page we want tagged at this point, so fix it by using a do {} while loop instead of a for loop.

We can't do this. Pages can only have one type at a time. Since they're allocated from slab, they have PGTY_slab. This will lead to a warning at runtime:

Quoted 6 lines
         VM_BUG_ON_PAGE(data_race(page->page_type) != UINT_MAX, page);   \

But for our purposes (trying to figure out how fragmented the vmap stack is making the memmap), we don't need to do this accounting. These pages are already being allocated from slab, and slab allocates naturally aligned, so we can skip all of this for these configurations.

IIUC what you mean, I am not a fan of having PGTY_stack being properly set or not based on a kernel config.

I much rather prefer this being done cleanly for all cases, or not at all (OOT for research/debugging purposes).

Signature · 3 lines
Cheers

David / dhildenb