Skip to content

Commit 22def0b

Browse files
pierregondoisgregkh
authored andcommitted
arch_topology: Build cacheinfo from primary CPU
[ Upstream commit 5944ce0 ] commit 3fcbf1c ("arch_topology: Fix cache attributes detection in the CPU hotplug path") adds a call to detect_cache_attributes() to populate the cacheinfo before updating the siblings mask. detect_cache_attributes() allocates memory and can take the PPTT mutex (on ACPI platforms). On PREEMPT_RT kernels, on secondary CPUs, this triggers a: 'BUG: sleeping function called from invalid context' [1] as the code is executed with preemption and interrupts disabled. The primary CPU was previously storing the cache information using the now removed (struct cpu_topology).llc_id: commit 5b8dc78 ("arch_topology: Drop LLC identifier stash from the CPU topology") allocate_cache_info() tries to build the cacheinfo from the primary CPU prior secondary CPUs boot, if the DT/ACPI description contains cache information. If allocate_cache_info() fails, then fallback to the current state for the cacheinfo allocation. [1] will be triggered in such case. When unplugging a CPU, the cacheinfo memory cannot be freed. If it was, then the memory would be allocated early by the re-plugged CPU and would trigger [1]. Note that populate_cache_leaves() might be called multiple times due to populate_leaves being moved up. This is required since detect_cache_attributes() might be called with per_cpu_cacheinfo(cpu) being allocated but not populated. [1]: | BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:46 | in_atomic(): 1, irqs_disabled(): 128, non_block: 0, pid: 0, name: swapper/111 | preempt_count: 1, expected: 0 | RCU nest depth: 1, expected: 1 | 3 locks held by swapper/111/0: | #0: (&pcp->lock){+.+.}-{3:3}, at: get_page_from_freelist+0x218/0x12c8 | #1: (rcu_read_lock){....}-{1:3}, at: rt_spin_trylock+0x48/0xf0 | #2: (&zone->lock){+.+.}-{3:3}, at: rmqueue_bulk+0x64/0xa80 | irq event stamp: 0 | hardirqs last enabled at (0): 0x0 | hardirqs last disabled at (0): copy_process+0x5dc/0x1ab8 | softirqs last enabled at (0): copy_process+0x5dc/0x1ab8 | softirqs last disabled at (0): 0x0 | Preemption disabled at: | migrate_enable+0x30/0x130 | CPU: 111 PID: 0 Comm: swapper/111 Tainted: G W 6.0.0-rc4-rt6-[...] | Call trace: | __kmalloc+0xbc/0x1e8 | detect_cache_attributes+0x2d4/0x5f0 | update_siblings_masks+0x30/0x368 | store_cpu_topology+0x78/0xb8 | secondary_start_kernel+0xd0/0x198 | __secondary_switched+0xb0/0xb4 Signed-off-by: Pierre Gondois <pierre.gondois@arm.com> Reviewed-by: Sudeep Holla <sudeep.holla@arm.com> Acked-by: Palmer Dabbelt <palmer@rivosinc.com> Link: https://lore.kernel.org/r/20230104183033.755668-7-pierre.gondois@arm.com Signed-off-by: Sudeep Holla <sudeep.holla@arm.com> Signed-off-by: Wen Yang <wen.yang@linux.dev> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 57ab709 commit 22def0b

4 files changed

Lines changed: 65 additions & 24 deletions

File tree

arch/riscv/kernel/cacheinfo.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ static void fill_cacheinfo(struct cacheinfo **this_leaf,
113113
}
114114
}
115115

116-
int init_cache_level(unsigned int cpu)
117-
{
118-
return init_of_cache_level(cpu);
119-
}
120-
121116
int populate_cache_leaves(unsigned int cpu)
122117
{
123118
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);

drivers/base/arch_topology.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ void update_siblings_masks(unsigned int cpuid)
736736

737737
ret = detect_cache_attributes(cpuid);
738738
if (ret && ret != -ENOENT)
739-
pr_info("Early cacheinfo failed, ret = %d\n", ret);
739+
pr_info("Early cacheinfo allocation failed, ret = %d\n", ret);
740740

741741
/* update core and thread sibling masks */
742742
for_each_online_cpu(cpu) {
@@ -825,7 +825,7 @@ __weak int __init parse_acpi_topology(void)
825825
#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
826826
void __init init_cpu_topology(void)
827827
{
828-
int ret;
828+
int cpu, ret;
829829

830830
reset_cpu_topology();
831831
ret = parse_acpi_topology();
@@ -840,6 +840,14 @@ void __init init_cpu_topology(void)
840840
reset_cpu_topology();
841841
return;
842842
}
843+
844+
for_each_possible_cpu(cpu) {
845+
ret = fetch_cache_info(cpu);
846+
if (ret) {
847+
pr_err("Early cacheinfo failed, ret = %d\n", ret);
848+
break;
849+
}
850+
}
843851
}
844852

845853
void store_cpu_topology(unsigned int cpuid)

drivers/base/cacheinfo.c

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,6 @@ static void free_cache_attributes(unsigned int cpu)
412412
return;
413413

414414
cache_shared_cpu_map_remove(cpu);
415-
416-
kfree(per_cpu_cacheinfo(cpu));
417-
per_cpu_cacheinfo(cpu) = NULL;
418-
cache_leaves(cpu) = 0;
419415
}
420416

421417
int __weak init_cache_level(unsigned int cpu)
@@ -428,29 +424,71 @@ int __weak populate_cache_leaves(unsigned int cpu)
428424
return -ENOENT;
429425
}
430426

427+
static inline
428+
int allocate_cache_info(int cpu)
429+
{
430+
per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
431+
sizeof(struct cacheinfo), GFP_ATOMIC);
432+
if (!per_cpu_cacheinfo(cpu)) {
433+
cache_leaves(cpu) = 0;
434+
return -ENOMEM;
435+
}
436+
437+
return 0;
438+
}
439+
440+
int fetch_cache_info(unsigned int cpu)
441+
{
442+
struct cpu_cacheinfo *this_cpu_ci;
443+
unsigned int levels, split_levels;
444+
int ret;
445+
446+
if (acpi_disabled) {
447+
ret = init_of_cache_level(cpu);
448+
if (ret < 0)
449+
return ret;
450+
} else {
451+
ret = acpi_get_cache_info(cpu, &levels, &split_levels);
452+
if (ret < 0)
453+
return ret;
454+
455+
this_cpu_ci = get_cpu_cacheinfo(cpu);
456+
this_cpu_ci->num_levels = levels;
457+
/*
458+
* This assumes that:
459+
* - there cannot be any split caches (data/instruction)
460+
* above a unified cache
461+
* - data/instruction caches come by pair
462+
*/
463+
this_cpu_ci->num_leaves = levels + split_levels;
464+
}
465+
if (!cache_leaves(cpu))
466+
return -ENOENT;
467+
468+
return allocate_cache_info(cpu);
469+
}
470+
431471
int detect_cache_attributes(unsigned int cpu)
432472
{
433473
int ret;
434474

435-
/* Since early detection of the cacheinfo is allowed via this
436-
* function and this also gets called as CPU hotplug callbacks via
437-
* cacheinfo_cpu_online, the initialisation can be skipped and only
438-
* CPU maps can be updated as the CPU online status would be update
439-
* if called via cacheinfo_cpu_online path.
475+
/* Since early initialization/allocation of the cacheinfo is allowed
476+
* via fetch_cache_info() and this also gets called as CPU hotplug
477+
* callbacks via cacheinfo_cpu_online, the init/alloc can be skipped
478+
* as it will happen only once (the cacheinfo memory is never freed).
479+
* Just populate the cacheinfo.
440480
*/
441481
if (per_cpu_cacheinfo(cpu))
442-
goto update_cpu_map;
482+
goto populate_leaves;
443483

444484
if (init_cache_level(cpu) || !cache_leaves(cpu))
445485
return -ENOENT;
446486

447-
per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
448-
sizeof(struct cacheinfo), GFP_ATOMIC);
449-
if (per_cpu_cacheinfo(cpu) == NULL) {
450-
cache_leaves(cpu) = 0;
451-
return -ENOMEM;
452-
}
487+
ret = allocate_cache_info(cpu);
488+
if (ret)
489+
return ret;
453490

491+
populate_leaves:
454492
/*
455493
* populate_cache_leaves() may completely setup the cache leaves and
456494
* shared_cpu_map or it may leave it partially setup.
@@ -459,7 +497,6 @@ int detect_cache_attributes(unsigned int cpu)
459497
if (ret)
460498
goto free_ci;
461499

462-
update_cpu_map:
463500
/*
464501
* For systems using DT for cache hierarchy, fw_token
465502
* and shared_cpu_map will be set up here only if they are

include/linux/cacheinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int populate_cache_leaves(unsigned int cpu);
8585
int cache_setup_acpi(unsigned int cpu);
8686
bool last_level_cache_is_valid(unsigned int cpu);
8787
bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y);
88+
int fetch_cache_info(unsigned int cpu);
8889
int detect_cache_attributes(unsigned int cpu);
8990
#ifndef CONFIG_ACPI_PPTT
9091
/*

0 commit comments

Comments
 (0)