Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'archtopo-cacheinfo-updates-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/sudeep.holla/linux into driver-core-next

Sudeep writes:
"cacheinfo and arch_topology updates for v6.3

The main change is to build the cache topology information for all
the CPUs from the primary CPU. Currently the cacheinfo for secondary CPUs
is created during the early boot on the respective CPU itself. Preemption
and interrupts are disabled at this stage. On PREEMPT_RT kernels, allocating
memory and even parsing the PPTT table for ACPI based systems triggers a:
'BUG: sleeping function called from invalid context'

To prevent this bug, the cacheinfo is now allocated from the primary CPU
when preemption and interrupts are enabled and before booting secondary
CPUs. The cache levels/leaves are computed from DT/ACPI PPTT information
only, without relying on any architecture specific mechanism if done so
early.

The other minor change included here is to handle shared caches at
different levels when not all the CPUs on the system have the same
cache hierarchy."

* tag 'archtopo-cacheinfo-updates-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/sudeep.holla/linux:
cacheinfo: Fix shared_cpu_map to handle shared caches at different levels
arch_topology: Build cacheinfo from primary CPU
ACPI: PPTT: Update acpi_find_last_cache_level() to acpi_get_cache_info()
ACPI: PPTT: Remove acpi_find_cache_levels()
cacheinfo: Check 'cache-unified' property to count cache leaves
cacheinfo: Return error code in init_of_cache_level()
cacheinfo: Use RISC-V's init_cache_level() as generic OF implementation

+217 -121
+7 -4
arch/arm64/kernel/cacheinfo.c
··· 46 46 int init_cache_level(unsigned int cpu) 47 47 { 48 48 unsigned int ctype, level, leaves; 49 - int fw_level; 49 + int fw_level, ret; 50 50 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); 51 51 52 52 for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) { ··· 59 59 leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1; 60 60 } 61 61 62 - if (acpi_disabled) 62 + if (acpi_disabled) { 63 63 fw_level = of_find_last_cache_level(cpu); 64 - else 65 - fw_level = acpi_find_last_cache_level(cpu); 64 + } else { 65 + ret = acpi_get_cache_info(cpu, &fw_level, NULL); 66 + if (ret < 0) 67 + return ret; 68 + } 66 69 67 70 if (fw_level < 0) 68 71 return fw_level;
-42
arch/riscv/kernel/cacheinfo.c
··· 113 113 } 114 114 } 115 115 116 - int init_cache_level(unsigned int cpu) 117 - { 118 - struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); 119 - struct device_node *np = of_cpu_device_node_get(cpu); 120 - struct device_node *prev = NULL; 121 - int levels = 0, leaves = 0, level; 122 - 123 - if (of_property_read_bool(np, "cache-size")) 124 - ++leaves; 125 - if (of_property_read_bool(np, "i-cache-size")) 126 - ++leaves; 127 - if (of_property_read_bool(np, "d-cache-size")) 128 - ++leaves; 129 - if (leaves > 0) 130 - levels = 1; 131 - 132 - prev = np; 133 - while ((np = of_find_next_cache_node(np))) { 134 - of_node_put(prev); 135 - prev = np; 136 - if (!of_device_is_compatible(np, "cache")) 137 - break; 138 - if (of_property_read_u32(np, "cache-level", &level)) 139 - break; 140 - if (level <= levels) 141 - break; 142 - if (of_property_read_bool(np, "cache-size")) 143 - ++leaves; 144 - if (of_property_read_bool(np, "i-cache-size")) 145 - ++leaves; 146 - if (of_property_read_bool(np, "d-cache-size")) 147 - ++leaves; 148 - levels = level; 149 - } 150 - 151 - of_node_put(np); 152 - this_cpu_ci->num_levels = levels; 153 - this_cpu_ci->num_leaves = leaves; 154 - 155 - return 0; 156 - } 157 - 158 116 int populate_cache_leaves(unsigned int cpu) 159 117 { 160 118 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
+54 -39
drivers/acpi/pptt.c
··· 81 81 * acpi_pptt_walk_cache() - Attempt to find the requested acpi_pptt_cache 82 82 * @table_hdr: Pointer to the head of the PPTT table 83 83 * @local_level: passed res reflects this cache level 84 + * @split_levels: Number of split cache levels (data/instruction). 84 85 * @res: cache resource in the PPTT we want to walk 85 86 * @found: returns a pointer to the requested level if found 86 87 * @level: the requested cache level ··· 101 100 */ 102 101 static unsigned int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr, 103 102 unsigned int local_level, 103 + unsigned int *split_levels, 104 104 struct acpi_subtable_header *res, 105 105 struct acpi_pptt_cache **found, 106 106 unsigned int level, int type) ··· 115 113 while (cache) { 116 114 local_level++; 117 115 116 + if (!(cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)) { 117 + cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache); 118 + continue; 119 + } 120 + 121 + if (split_levels && 122 + (acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_DATA) || 123 + acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_INSTR))) 124 + *split_levels = local_level; 125 + 118 126 if (local_level == level && 119 - cache->flags & ACPI_PPTT_CACHE_TYPE_VALID && 120 127 acpi_pptt_match_type(cache->attributes, type)) { 121 128 if (*found != NULL && cache != *found) 122 129 pr_warn("Found duplicate cache level/type unable to determine uniqueness\n"); ··· 146 135 static struct acpi_pptt_cache * 147 136 acpi_find_cache_level(struct acpi_table_header *table_hdr, 148 137 struct acpi_pptt_processor *cpu_node, 149 - unsigned int *starting_level, unsigned int level, 150 - int type) 138 + unsigned int *starting_level, unsigned int *split_levels, 139 + unsigned int level, int type) 151 140 { 152 141 struct acpi_subtable_header *res; 153 142 unsigned int number_of_levels = *starting_level; ··· 160 149 resource++; 161 150 162 151 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level, 163 - res, &ret, level, type); 152 + split_levels, res, &ret, 153 + level, type); 164 154 /* 165 155 * we are looking for the max depth. Since its potentially 166 156 * possible for a given node to have resources with differing ··· 177 165 } 178 166 179 167 /** 180 - * acpi_count_levels() - Given a PPTT table, and a CPU node, count the caches 168 + * acpi_count_levels() - Given a PPTT table, and a CPU node, count the cache 169 + * levels and split cache levels (data/instruction). 181 170 * @table_hdr: Pointer to the head of the PPTT table 182 171 * @cpu_node: processor node we wish to count caches for 172 + * @levels: Number of levels if success. 173 + * @split_levels: Number of split cache levels (data/instruction) if 174 + * success. Can by NULL. 183 175 * 184 176 * Given a processor node containing a processing unit, walk into it and count 185 177 * how many levels exist solely for it, and then walk up each level until we hit 186 178 * the root node (ignore the package level because it may be possible to have 187 - * caches that exist across packages). Count the number of cache levels that 188 - * exist at each level on the way up. 189 - * 190 - * Return: Total number of levels found. 179 + * caches that exist across packages). Count the number of cache levels and 180 + * split cache levels (data/instruction) that exist at each level on the way 181 + * up. 191 182 */ 192 - static int acpi_count_levels(struct acpi_table_header *table_hdr, 193 - struct acpi_pptt_processor *cpu_node) 183 + static void acpi_count_levels(struct acpi_table_header *table_hdr, 184 + struct acpi_pptt_processor *cpu_node, 185 + unsigned int *levels, unsigned int *split_levels) 194 186 { 195 - int total_levels = 0; 196 - 197 187 do { 198 - acpi_find_cache_level(table_hdr, cpu_node, &total_levels, 0, 0); 188 + acpi_find_cache_level(table_hdr, cpu_node, levels, split_levels, 0, 0); 199 189 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent); 200 190 } while (cpu_node); 201 - 202 - return total_levels; 203 191 } 204 192 205 193 /** ··· 293 281 return NULL; 294 282 } 295 283 296 - static int acpi_find_cache_levels(struct acpi_table_header *table_hdr, 297 - u32 acpi_cpu_id) 298 - { 299 - int number_of_levels = 0; 300 - struct acpi_pptt_processor *cpu; 301 - 302 - cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id); 303 - if (cpu) 304 - number_of_levels = acpi_count_levels(table_hdr, cpu); 305 - 306 - return number_of_levels; 307 - } 308 - 309 284 static u8 acpi_cache_type(enum cache_type type) 310 285 { 311 286 switch (type) { ··· 333 334 334 335 while (cpu_node && !found) { 335 336 found = acpi_find_cache_level(table_hdr, cpu_node, 336 - &total_levels, level, acpi_type); 337 + &total_levels, NULL, level, acpi_type); 337 338 *node = cpu_node; 338 339 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent); 339 340 } ··· 601 602 } 602 603 603 604 /** 604 - * acpi_find_last_cache_level() - Determines the number of cache levels for a PE 605 + * acpi_get_cache_info() - Determine the number of cache levels and 606 + * split cache levels (data/instruction) and for a PE. 605 607 * @cpu: Kernel logical CPU number 608 + * @levels: Number of levels if success. 609 + * @split_levels: Number of levels being split (i.e. data/instruction) 610 + * if success. Can by NULL. 606 611 * 607 612 * Given a logical CPU number, returns the number of levels of cache represented 608 613 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0 609 614 * indicating we didn't find any cache levels. 610 615 * 611 - * Return: Cache levels visible to this core. 616 + * Return: -ENOENT if no PPTT table or no PPTT processor struct found. 617 + * 0 on success. 612 618 */ 613 - int acpi_find_last_cache_level(unsigned int cpu) 619 + int acpi_get_cache_info(unsigned int cpu, unsigned int *levels, 620 + unsigned int *split_levels) 614 621 { 615 - u32 acpi_cpu_id; 622 + struct acpi_pptt_processor *cpu_node; 616 623 struct acpi_table_header *table; 617 - int number_of_levels = 0; 624 + u32 acpi_cpu_id; 625 + 626 + *levels = 0; 627 + if (split_levels) 628 + *split_levels = 0; 618 629 619 630 table = acpi_get_pptt(); 620 631 if (!table) 621 632 return -ENOENT; 622 633 623 - pr_debug("Cache Setup find last level CPU=%d\n", cpu); 634 + pr_debug("Cache Setup: find cache levels for CPU=%d\n", cpu); 624 635 625 636 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 626 - number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id); 627 - pr_debug("Cache Setup find last level level=%d\n", number_of_levels); 637 + cpu_node = acpi_find_processor_node(table, acpi_cpu_id); 638 + if (!cpu_node) 639 + return -ENOENT; 628 640 629 - return number_of_levels; 641 + acpi_count_levels(table, cpu_node, levels, split_levels); 642 + 643 + pr_debug("Cache Setup: last_level=%d split_levels=%d\n", 644 + *levels, split_levels ? *split_levels : -1); 645 + 646 + return 0; 630 647 } 631 648 632 649 /**
+10 -2
drivers/base/arch_topology.c
··· 736 736 737 737 ret = detect_cache_attributes(cpuid); 738 738 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); 740 740 741 741 /* update core and thread sibling masks */ 742 742 for_each_online_cpu(cpu) { ··· 825 825 #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV) 826 826 void __init init_cpu_topology(void) 827 827 { 828 - int ret; 828 + int cpu, ret; 829 829 830 830 reset_cpu_topology(); 831 831 ret = parse_acpi_topology(); ··· 839 839 */ 840 840 reset_cpu_topology(); 841 841 return; 842 + } 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 + } 842 850 } 843 851 } 844 852
+138 -31
drivers/base/cacheinfo.c
··· 229 229 230 230 return 0; 231 231 } 232 + 233 + static int of_count_cache_leaves(struct device_node *np) 234 + { 235 + unsigned int leaves = 0; 236 + 237 + if (of_property_read_bool(np, "cache-size")) 238 + ++leaves; 239 + if (of_property_read_bool(np, "i-cache-size")) 240 + ++leaves; 241 + if (of_property_read_bool(np, "d-cache-size")) 242 + ++leaves; 243 + 244 + if (!leaves) { 245 + /* The '[i-|d-|]cache-size' property is required, but 246 + * if absent, fallback on the 'cache-unified' property. 247 + */ 248 + if (of_property_read_bool(np, "cache-unified")) 249 + return 1; 250 + else 251 + return 2; 252 + } 253 + 254 + return leaves; 255 + } 256 + 257 + int init_of_cache_level(unsigned int cpu) 258 + { 259 + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); 260 + struct device_node *np = of_cpu_device_node_get(cpu); 261 + struct device_node *prev = NULL; 262 + unsigned int levels = 0, leaves, level; 263 + 264 + leaves = of_count_cache_leaves(np); 265 + if (leaves > 0) 266 + levels = 1; 267 + 268 + prev = np; 269 + while ((np = of_find_next_cache_node(np))) { 270 + of_node_put(prev); 271 + prev = np; 272 + if (!of_device_is_compatible(np, "cache")) 273 + goto err_out; 274 + if (of_property_read_u32(np, "cache-level", &level)) 275 + goto err_out; 276 + if (level <= levels) 277 + goto err_out; 278 + 279 + leaves += of_count_cache_leaves(np); 280 + levels = level; 281 + } 282 + 283 + of_node_put(np); 284 + this_cpu_ci->num_levels = levels; 285 + this_cpu_ci->num_leaves = leaves; 286 + 287 + return 0; 288 + 289 + err_out: 290 + of_node_put(np); 291 + return -EINVAL; 292 + } 293 + 232 294 #else 233 295 static inline int cache_setup_of_node(unsigned int cpu) { return 0; } 296 + int init_of_cache_level(unsigned int cpu) { return 0; } 234 297 #endif 235 298 236 299 int __weak cache_setup_acpi(unsigned int cpu) ··· 319 256 { 320 257 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); 321 258 struct cacheinfo *this_leaf, *sib_leaf; 322 - unsigned int index; 259 + unsigned int index, sib_index; 323 260 int ret = 0; 324 261 325 262 if (this_cpu_ci->cpu_map_populated) ··· 347 284 348 285 if (i == cpu || !sib_cpu_ci->info_list) 349 286 continue;/* skip if itself or no cacheinfo */ 350 - 351 - sib_leaf = per_cpu_cacheinfo_idx(i, index); 352 - if (cache_leaves_are_shared(this_leaf, sib_leaf)) { 353 - cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map); 354 - cpumask_set_cpu(i, &this_leaf->shared_cpu_map); 287 + for (sib_index = 0; sib_index < cache_leaves(i); sib_index++) { 288 + sib_leaf = per_cpu_cacheinfo_idx(i, sib_index); 289 + if (cache_leaves_are_shared(this_leaf, sib_leaf)) { 290 + cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map); 291 + cpumask_set_cpu(i, &this_leaf->shared_cpu_map); 292 + break; 293 + } 355 294 } 356 295 } 357 296 /* record the maximum cache line size */ ··· 367 302 static void cache_shared_cpu_map_remove(unsigned int cpu) 368 303 { 369 304 struct cacheinfo *this_leaf, *sib_leaf; 370 - unsigned int sibling, index; 305 + unsigned int sibling, index, sib_index; 371 306 372 307 for (index = 0; index < cache_leaves(cpu); index++) { 373 308 this_leaf = per_cpu_cacheinfo_idx(cpu, index); ··· 378 313 if (sibling == cpu || !sib_cpu_ci->info_list) 379 314 continue;/* skip if itself or no cacheinfo */ 380 315 381 - sib_leaf = per_cpu_cacheinfo_idx(sibling, index); 382 - cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map); 383 - cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map); 316 + for (sib_index = 0; sib_index < cache_leaves(sibling); sib_index++) { 317 + sib_leaf = per_cpu_cacheinfo_idx(sibling, sib_index); 318 + if (cache_leaves_are_shared(this_leaf, sib_leaf)) { 319 + cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map); 320 + cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map); 321 + break; 322 + } 323 + } 384 324 } 385 325 } 386 326 } ··· 396 326 return; 397 327 398 328 cache_shared_cpu_map_remove(cpu); 399 - 400 - kfree(per_cpu_cacheinfo(cpu)); 401 - per_cpu_cacheinfo(cpu) = NULL; 402 - cache_leaves(cpu) = 0; 403 329 } 404 330 405 331 int __weak init_cache_level(unsigned int cpu) ··· 408 342 return -ENOENT; 409 343 } 410 344 411 - int detect_cache_attributes(unsigned int cpu) 345 + static inline 346 + int allocate_cache_info(int cpu) 412 347 { 413 - int ret; 414 - 415 - /* Since early detection of the cacheinfo is allowed via this 416 - * function and this also gets called as CPU hotplug callbacks via 417 - * cacheinfo_cpu_online, the initialisation can be skipped and only 418 - * CPU maps can be updated as the CPU online status would be update 419 - * if called via cacheinfo_cpu_online path. 420 - */ 421 - if (per_cpu_cacheinfo(cpu)) 422 - goto update_cpu_map; 423 - 424 - if (init_cache_level(cpu) || !cache_leaves(cpu)) 425 - return -ENOENT; 426 - 427 348 per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), 428 349 sizeof(struct cacheinfo), GFP_ATOMIC); 429 - if (per_cpu_cacheinfo(cpu) == NULL) { 350 + if (!per_cpu_cacheinfo(cpu)) { 430 351 cache_leaves(cpu) = 0; 431 352 return -ENOMEM; 432 353 } 433 354 355 + return 0; 356 + } 357 + 358 + int fetch_cache_info(unsigned int cpu) 359 + { 360 + struct cpu_cacheinfo *this_cpu_ci; 361 + unsigned int levels, split_levels; 362 + int ret; 363 + 364 + if (acpi_disabled) { 365 + ret = init_of_cache_level(cpu); 366 + if (ret < 0) 367 + return ret; 368 + } else { 369 + ret = acpi_get_cache_info(cpu, &levels, &split_levels); 370 + if (ret < 0) 371 + return ret; 372 + 373 + this_cpu_ci = get_cpu_cacheinfo(cpu); 374 + this_cpu_ci->num_levels = levels; 375 + /* 376 + * This assumes that: 377 + * - there cannot be any split caches (data/instruction) 378 + * above a unified cache 379 + * - data/instruction caches come by pair 380 + */ 381 + this_cpu_ci->num_leaves = levels + split_levels; 382 + } 383 + if (!cache_leaves(cpu)) 384 + return -ENOENT; 385 + 386 + return allocate_cache_info(cpu); 387 + } 388 + 389 + int detect_cache_attributes(unsigned int cpu) 390 + { 391 + int ret; 392 + 393 + /* Since early initialization/allocation of the cacheinfo is allowed 394 + * via fetch_cache_info() and this also gets called as CPU hotplug 395 + * callbacks via cacheinfo_cpu_online, the init/alloc can be skipped 396 + * as it will happen only once (the cacheinfo memory is never freed). 397 + * Just populate the cacheinfo. 398 + */ 399 + if (per_cpu_cacheinfo(cpu)) 400 + goto populate_leaves; 401 + 402 + if (init_cache_level(cpu) || !cache_leaves(cpu)) 403 + return -ENOENT; 404 + 405 + ret = allocate_cache_info(cpu); 406 + if (ret) 407 + return ret; 408 + 409 + populate_leaves: 434 410 /* 435 411 * populate_cache_leaves() may completely setup the cache leaves and 436 412 * shared_cpu_map or it may leave it partially setup. ··· 481 373 if (ret) 482 374 goto free_ci; 483 375 484 - update_cpu_map: 485 376 /* 486 377 * For systems using DT for cache hierarchy, fw_token 487 378 * and shared_cpu_map will be set up here only if they are
+8 -3
include/linux/cacheinfo.h
··· 80 80 81 81 struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu); 82 82 int init_cache_level(unsigned int cpu); 83 + int init_of_cache_level(unsigned int cpu); 83 84 int populate_cache_leaves(unsigned int cpu); 84 85 int cache_setup_acpi(unsigned int cpu); 85 86 bool last_level_cache_is_valid(unsigned int cpu); 86 87 bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y); 88 + int fetch_cache_info(unsigned int cpu); 87 89 int detect_cache_attributes(unsigned int cpu); 88 90 #ifndef CONFIG_ACPI_PPTT 89 91 /* 90 - * acpi_find_last_cache_level is only called on ACPI enabled 92 + * acpi_get_cache_info() is only called on ACPI enabled 91 93 * platforms using the PPTT for topology. This means that if 92 94 * the platform supports other firmware configuration methods 93 95 * we need to stub out the call when ACPI is disabled. 94 96 * ACPI enabled platforms not using PPTT won't be making calls 95 97 * to this function so we need not worry about them. 96 98 */ 97 - static inline int acpi_find_last_cache_level(unsigned int cpu) 99 + static inline 100 + int acpi_get_cache_info(unsigned int cpu, 101 + unsigned int *levels, unsigned int *split_levels) 98 102 { 99 103 return 0; 100 104 } 101 105 #else 102 - int acpi_find_last_cache_level(unsigned int cpu); 106 + int acpi_get_cache_info(unsigned int cpu, 107 + unsigned int *levels, unsigned int *split_levels); 103 108 #endif 104 109 105 110 const struct attribute_group *cache_get_priv_group(struct cacheinfo *this_leaf);