Skip to contentSkip to conversation

kernel/fork.c:457 account_kernel_stack() warn: we never enter this loop

PATCH Dan Carpenter 2 participants 2 messages Aug 30, 2025 – Sep 2, 2025 Memory management

lore
Share & tools
lore

Dan Carpenter

author build bot
#0 lore raw

BCC: [email protected]
CC: [email protected]
TO: "Vishal Moola (Oracle)" <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Linux Memory Management List <[email protected]>

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
head:   3cace99d63192a7250461b058279a42d91075d0c
commit: e0aa7237ef4323a66ed06953225d9b07cf039530 [4756/5283] mm: tag kernel stack pages

:::::: branch date: 20 hours ago
:::::: commit date: 2 days ago
config: hexagon-randconfig-r072-20250829 (https://download.01.org/0day-ci/archive/20250830/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
kernel/fork.c:457 account_kernel_stack() warn: we never enter this loop kernel/fork.c:457 account_kernel_stack() warn: unsigned 'i' is never less than zero. kernel/fork.c:457 account_kernel_stack() warn: unsigned 'i' is never less than zero. kernel/fork.c:479 exit_task_stack_account() warn: we never enter this loop kernel/fork.c:479 exit_task_stack_account() warn: unsigned 'i' is never less than zero. kernel/fork.c:479 exit_task_stack_account() warn: unsigned 'i' is never less than zero.

vim +457 kernel/fork.c

^1da177e4c3f41 Linus Torvalds            2005-04-16  436  
ba14a194a434cc Andy Lutomirski           2016-08-11  437  static void account_kernel_stack(struct task_struct *tsk, int account)
c6a7f5728a1db4 KOSAKI Motohiro           2009-09-21  438  {
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  439  	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  440  		struct vm_struct *vm_area = task_stack_vm_area(tsk);
27faca83a7e955 Muchun Song               2021-04-29  441  		int i;
efdc94907977d2 Andy Lutomirski           2016-07-28  442  
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  443) 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  444  			mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB,
27faca83a7e955 Muchun Song               2021-04-29  445  					      account * (PAGE_SIZE / 1024));
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  446) 			__SetPageStack(vm_area->pages[i]);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  447) 		}
27faca83a7e955 Muchun Song               2021-04-29  448  	} else {
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  449  		void *stack = task_stack_page(tsk);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  450) 		struct page *page = virt_to_head_page(stack);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  451) 		int i;
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  452  
991e7673859ed4 Shakeel Butt              2020-08-06  453  		/* All stack pages are in the same node. */
da3ceeff923e3b Muchun Song               2020-12-14  454  		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
efdc94907977d2 Andy Lutomirski           2016-07-28  455  				      account * (THREAD_SIZE / 1024));
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  456) 
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20 @457) 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++, page++)
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  458) 			__SetPageStack(page);
ba14a194a434cc Andy Lutomirski           2016-08-11  459  	}
27faca83a7e955 Muchun Song               2021-04-29  460  }
c6a7f5728a1db4 KOSAKI Motohiro           2009-09-21  461  
1a03d3f13ffe5d Sebastian Andrzej Siewior 2022-02-17  462  void exit_task_stack_account(struct task_struct *tsk)
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  463  {
1a03d3f13ffe5d Sebastian Andrzej Siewior 2022-02-17  464  	account_kernel_stack(tsk, -1);
991e7673859ed4 Shakeel Butt              2020-08-06  465  
1a03d3f13ffe5d Sebastian Andrzej Siewior 2022-02-17  466  	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  467  		struct vm_struct *vm_area;
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  468  		int i;
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  469  
449e0b4ed5a16c Pasha Tatashin            2025-05-09  470  		vm_area = task_stack_vm_area(tsk);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  471) 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  472  			memcg_kmem_uncharge_page(vm_area->pages[i], 0);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  473) 			__ClearPageStack(vm_area->pages[i]);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  474) 		}
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  475) 	} else {
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  476) 		struct page *page = virt_to_head_page(task_stack_page(tsk));
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  477) 		int i;
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  478) 
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20 @479) 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++, page++)
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  480) 			__ClearPageStack(page);
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  481  	}
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  482  }
9b6f7e163cd0f4 Roman Gushchin            2018-10-26  483  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 12 lines · Dan Carpenter
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
head:   3cace99d63192a7250461b058279a42d91075d0c
commit: e0aa7237ef4323a66ed06953225d9b07cf039530 [4756/5283] mm: tag kernel stack pages
config: hexagon-randconfig-r072-20250829 (https://download.01.org/0day-ci/archive/20250830/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
Quoted 5 lines

smatch warnings:
kernel/fork.c:457 account_kernel_stack() warn: we never enter this loop kernel/fork.c:457 account_kernel_stack() warn: unsigned 'i' is never less than zero. kernel/fork.c:479 exit_task_stack_account() warn: we never enter this loop kernel/fork.c:479 exit_task_stack_account() warn: unsigned 'i' is never less than zero.

Thanks for catching this!

Quoted 22 lines

vim +457 kernel/fork.c

ba14a194a434cc Andy Lutomirski           2016-08-11  437  static void account_kernel_stack(struct task_struct *tsk, int account)
c6a7f5728a1db4 KOSAKI Motohiro           2009-09-21  438  {
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  439  	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  440  		struct vm_struct *vm_area = task_stack_vm_area(tsk);
27faca83a7e955 Muchun Song               2021-04-29  441  		int i;
efdc94907977d2 Andy Lutomirski           2016-07-28  442  
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  443) 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
449e0b4ed5a16c Pasha Tatashin            2025-05-09  444  			mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB,
27faca83a7e955 Muchun Song               2021-04-29  445  					      account * (PAGE_SIZE / 1024));
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  446) 			__SetPageStack(vm_area->pages[i]);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  447) 		}
27faca83a7e955 Muchun Song               2021-04-29  448  	} else {
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  449  		void *stack = task_stack_page(tsk);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  450) 		struct page *page = virt_to_head_page(stack);
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  451) 		int i;
0ce055f85335e4 Sebastian Andrzej Siewior 2022-02-17  452  
991e7673859ed4 Shakeel Butt              2020-08-06  453  		/* All stack pages are in the same node. */
da3ceeff923e3b Muchun Song               2020-12-14  454  		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
efdc94907977d2 Andy Lutomirski           2016-07-28  455  				      account * (THREAD_SIZE / 1024));
e0aa7237ef4323 Vishal Moola (Oracle      2025-08-20  456) 
Quoted 6 lines

e0aa7237ef4323 Vishal Moola (Oracle 2025-08-20 @457) for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++, page++)
e0aa7237ef4323 Vishal Moola (Oracle 2025-08-20 458) __SetPageStack(page);

Apparently in the linked config the THREAD_SIZE / PAGE_SIZE is zero. Is this the expected behavior? I don't know mm enough to say the answer...

THREAD_SIZE and PAGE_SIZE are very architecture dependent. This behavior is expected in some configs. This can't happen in the VMAP_STACK case, only !VMAP_STACK, so looks like my patch is wrong in handling that case. I'll send a fix shortly.