Skip to content

Commit b9792ab

Browse files
Gavin Shangregkh
authored andcommitted
drivers/base/memory: Avoid overhead from for_each_present_section_nr()
for_each_present_section_nr() was introduced to add_boot_memory_block() by commit 61659ef ("drivers/base/memory: improve add_boot_memory_block()"). It causes unnecessary overhead when the present sections are really sparse. next_present_section_nr() called by the macro to find the next present section, which is far away from the spanning sections in the specified block. Too much time consumed by next_present_section_nr() in this case, which can lead to softlockup as observed by Aditya Gupta on IBM Power10 machine. watchdog: BUG: soft lockup - CPU#248 stuck for 22s! [swapper/248:1] Modules linked in: CPU: 248 UID: 0 PID: 1 Comm: swapper/248 Not tainted 6.15.0-rc1-next-20250408 #1 VOLUNTARY Hardware name: 9105-22A POWER10 (raw) 0x800200 opal:v7.1-107-gfda75d121942 PowerNV NIP: c00000000209218c LR: c000000002092204 CTR: 0000000000000000 REGS: c00040000418fa30 TRAP: 0900 Not tainted (6.15.0-rc1-next-20250408) MSR: 9000000002009033 <SF,HV,VEC,EE,ME,IR,DR,RI,LE> CR: 28000428 XER: 00000000 CFAR: 0000000000000000 IRQMASK: 0 GPR00: c000000002092204 c00040000418fcd0 c000000001b08100 0000000000000040 GPR04: 0000000000013e00 c000c03ffebabb00 0000000000c03fff c000400fff587f80 GPR08: 0000000000000000 00000000001196f7 0000000000000000 0000000028000428 GPR12: 0000000000000000 c000000002e80000 c00000000001007c 0000000000000000 GPR16: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR20: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR24: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR28: c000000002df7f70 0000000000013dc0 c0000000011dd898 0000000008000000 NIP [c00000000209218c] memory_dev_init+0x114/0x1e0 LR [c000000002092204] memory_dev_init+0x18c/0x1e0 Call Trace: [c00040000418fcd0] [c000000002092204] memory_dev_init+0x18c/0x1e0 (unreliable) [c00040000418fd50] [c000000002091348] driver_init+0x78/0xa4 [c00040000418fd70] [c0000000020063ac] kernel_init_freeable+0x22c/0x370 [c00040000418fde0] [c0000000000100a8] kernel_init+0x34/0x25c [c00040000418fe50] [c00000000000cd94] ret_from_kernel_user_thread+0x14/0x1c Avoid the overhead by folding for_each_present_section_nr() to the outer loop. add_boot_memory_block() is dropped after that. Fixes: 61659ef ("drivers/base/memory: improve add_boot_memory_block()") Closes: https://lore.kernel.org/linux-mm/20250409180344.477916-1-adityag@linux.ibm.com Reported-by: Aditya Gupta <adityag@linux.ibm.com> Signed-off-by: Gavin Shan <gshan@redhat.com> Acked-by: Oscar Salvador <osalvador@suse.de> Tested-by: Aditya Gupta <adityag@linux.ibm.com> Acked-by: David Hildenbrand <david@redhat.com> Link: https://lore.kernel.org/r/20250410125110.1232329-1-gshan@redhat.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bc2c464 commit b9792ab

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

drivers/base/memory.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -816,21 +816,6 @@ static int add_memory_block(unsigned long block_id, unsigned long state,
816816
return 0;
817817
}
818818

819-
static int __init add_boot_memory_block(unsigned long base_section_nr)
820-
{
821-
unsigned long nr;
822-
823-
for_each_present_section_nr(base_section_nr, nr) {
824-
if (nr >= (base_section_nr + sections_per_block))
825-
break;
826-
827-
return add_memory_block(memory_block_id(base_section_nr),
828-
MEM_ONLINE, NULL, NULL);
829-
}
830-
831-
return 0;
832-
}
833-
834819
static int add_hotplug_memory_block(unsigned long block_id,
835820
struct vmem_altmap *altmap,
836821
struct memory_group *group)
@@ -957,7 +942,7 @@ static const struct attribute_group *memory_root_attr_groups[] = {
957942
void __init memory_dev_init(void)
958943
{
959944
int ret;
960-
unsigned long block_sz, nr;
945+
unsigned long block_sz, block_id, nr;
961946

962947
/* Validate the configured memory block size */
963948
block_sz = memory_block_size_bytes();
@@ -970,15 +955,23 @@ void __init memory_dev_init(void)
970955
panic("%s() failed to register subsystem: %d\n", __func__, ret);
971956

972957
/*
973-
* Create entries for memory sections that were found
974-
* during boot and have been initialized
958+
* Create entries for memory sections that were found during boot
959+
* and have been initialized. Use @block_id to track the last
960+
* handled block and initialize it to an invalid value (ULONG_MAX)
961+
* to bypass the block ID matching check for the first present
962+
* block so that it can be covered.
975963
*/
976-
for (nr = 0; nr <= __highest_present_section_nr;
977-
nr += sections_per_block) {
978-
ret = add_boot_memory_block(nr);
979-
if (ret)
980-
panic("%s() failed to add memory block: %d\n", __func__,
981-
ret);
964+
block_id = ULONG_MAX;
965+
for_each_present_section_nr(0, nr) {
966+
if (block_id != ULONG_MAX && memory_block_id(nr) == block_id)
967+
continue;
968+
969+
block_id = memory_block_id(nr);
970+
ret = add_memory_block(block_id, MEM_ONLINE, NULL, NULL);
971+
if (ret) {
972+
panic("%s() failed to add memory block: %d\n",
973+
__func__, ret);
974+
}
982975
}
983976
}
984977

0 commit comments

Comments
 (0)