Skip to content

Commit 5944ce0

Browse files
pierregondoissudeep-holla
authored andcommitted
arch_topology: Build cacheinfo from primary CPU
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 <[email protected]> Reviewed-by: Sudeep Holla <[email protected]> Acked-by: Palmer Dabbelt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sudeep Holla <[email protected]>
1 parent bd50036 commit 5944ce0

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

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
@@ -389,10 +389,6 @@ static void free_cache_attributes(unsigned int cpu)
389389
return;
390390

391391
cache_shared_cpu_map_remove(cpu);
392-
393-
kfree(per_cpu_cacheinfo(cpu));
394-
per_cpu_cacheinfo(cpu) = NULL;
395-
cache_leaves(cpu) = 0;
396392
}
397393

398394
int __weak init_cache_level(unsigned int cpu)
@@ -405,29 +401,71 @@ int __weak populate_cache_leaves(unsigned int cpu)
405401
return -ENOENT;
406402
}
407403

404+
static inline
405+
int allocate_cache_info(int cpu)
406+
{
407+
per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
408+
sizeof(struct cacheinfo), GFP_ATOMIC);
409+
if (!per_cpu_cacheinfo(cpu)) {
410+
cache_leaves(cpu) = 0;
411+
return -ENOMEM;
412+
}
413+
414+
return 0;
415+
}
416+
417+
int fetch_cache_info(unsigned int cpu)
418+
{
419+
struct cpu_cacheinfo *this_cpu_ci;
420+
unsigned int levels, split_levels;
421+
int ret;
422+
423+
if (acpi_disabled) {
424+
ret = init_of_cache_level(cpu);
425+
if (ret < 0)
426+
return ret;
427+
} else {
428+
ret = acpi_get_cache_info(cpu, &levels, &split_levels);
429+
if (ret < 0)
430+
return ret;
431+
432+
this_cpu_ci = get_cpu_cacheinfo(cpu);
433+
this_cpu_ci->num_levels = levels;
434+
/*
435+
* This assumes that:
436+
* - there cannot be any split caches (data/instruction)
437+
* above a unified cache
438+
* - data/instruction caches come by pair
439+
*/
440+
this_cpu_ci->num_leaves = levels + split_levels;
441+
}
442+
if (!cache_leaves(cpu))
443+
return -ENOENT;
444+
445+
return allocate_cache_info(cpu);
446+
}
447+
408448
int detect_cache_attributes(unsigned int cpu)
409449
{
410450
int ret;
411451

412-
/* Since early detection of the cacheinfo is allowed via this
413-
* function and this also gets called as CPU hotplug callbacks via
414-
* cacheinfo_cpu_online, the initialisation can be skipped and only
415-
* CPU maps can be updated as the CPU online status would be update
416-
* if called via cacheinfo_cpu_online path.
452+
/* Since early initialization/allocation of the cacheinfo is allowed
453+
* via fetch_cache_info() and this also gets called as CPU hotplug
454+
* callbacks via cacheinfo_cpu_online, the init/alloc can be skipped
455+
* as it will happen only once (the cacheinfo memory is never freed).
456+
* Just populate the cacheinfo.
417457
*/
418458
if (per_cpu_cacheinfo(cpu))
419-
goto update_cpu_map;
459+
goto populate_leaves;
420460

421461
if (init_cache_level(cpu) || !cache_leaves(cpu))
422462
return -ENOENT;
423463

424-
per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
425-
sizeof(struct cacheinfo), GFP_ATOMIC);
426-
if (per_cpu_cacheinfo(cpu) == NULL) {
427-
cache_leaves(cpu) = 0;
428-
return -ENOMEM;
429-
}
464+
ret = allocate_cache_info(cpu);
465+
if (ret)
466+
return ret;
430467

468+
populate_leaves:
431469
/*
432470
* populate_cache_leaves() may completely setup the cache leaves and
433471
* shared_cpu_map or it may leave it partially setup.
@@ -436,7 +474,6 @@ int detect_cache_attributes(unsigned int cpu)
436474
if (ret)
437475
goto free_ci;
438476

439-
update_cpu_map:
440477
/*
441478
* For systems using DT for cache hierarchy, fw_token
442479
* 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)