1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2025 Arm Ltd. 3 4 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__ 5 6 #include <linux/arm_mpam.h> 7 #include <linux/cacheinfo.h> 8 #include <linux/cpu.h> 9 #include <linux/cpumask.h> 10 #include <linux/errno.h> 11 #include <linux/limits.h> 12 #include <linux/list.h> 13 #include <linux/math.h> 14 #include <linux/printk.h> 15 #include <linux/rculist.h> 16 #include <linux/resctrl.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 #include <linux/wait.h> 20 21 #include <asm/mpam.h> 22 23 #include "mpam_internal.h" 24 25 static DECLARE_WAIT_QUEUE_HEAD(resctrl_mon_ctx_waiters); 26 27 /* 28 * The classes we've picked to map to resctrl resources, wrapped 29 * in with their resctrl structure. 30 * Class pointer may be NULL. 31 */ 32 static struct mpam_resctrl_res mpam_resctrl_controls[RDT_NUM_RESOURCES]; 33 34 #define for_each_mpam_resctrl_control(res, rid) \ 35 for (rid = 0, res = &mpam_resctrl_controls[rid]; \ 36 rid < RDT_NUM_RESOURCES; \ 37 rid++, res = &mpam_resctrl_controls[rid]) 38 39 /* 40 * The classes we've picked to map to resctrl events. 41 * Resctrl believes all the worlds a Xeon, and these are all on the L3. This 42 * array lets us find the actual class backing the event counters. e.g. 43 * the only memory bandwidth counters may be on the memory controller, but to 44 * make use of them, we pretend they are on L3. Restrict the events considered 45 * to those supported by MPAM. 46 * Class pointer may be NULL. 47 */ 48 #define MPAM_MAX_EVENT QOS_L3_MBM_TOTAL_EVENT_ID 49 static struct mpam_resctrl_mon mpam_resctrl_counters[MPAM_MAX_EVENT + 1]; 50 51 #define for_each_mpam_resctrl_mon(mon, eventid) \ 52 for (eventid = QOS_FIRST_EVENT, mon = &mpam_resctrl_counters[eventid]; \ 53 eventid <= MPAM_MAX_EVENT; \ 54 eventid++, mon = &mpam_resctrl_counters[eventid]) 55 56 /* The lock for modifying resctrl's domain lists from cpuhp callbacks. */ 57 static DEFINE_MUTEX(domain_list_lock); 58 59 /* 60 * MPAM emulates CDP by setting different PARTID in the I/D fields of MPAM0_EL1. 61 * This applies globally to all traffic the CPU generates. 62 */ 63 static bool cdp_enabled; 64 65 /* 66 * We use cacheinfo to discover the size of the caches and their id. cacheinfo 67 * populates this from a device_initcall(). mpam_resctrl_setup() must wait. 68 */ 69 static bool cacheinfo_ready; 70 static DECLARE_WAIT_QUEUE_HEAD(wait_cacheinfo_ready); 71 72 /* 73 * If resctrl_init() succeeded, resctrl_exit() can be used to remove support 74 * for the filesystem in the event of an error. 75 */ 76 static bool resctrl_enabled; 77 78 bool resctrl_arch_alloc_capable(void) 79 { 80 struct mpam_resctrl_res *res; 81 enum resctrl_res_level rid; 82 83 for_each_mpam_resctrl_control(res, rid) { 84 if (res->resctrl_res.alloc_capable) 85 return true; 86 } 87 88 return false; 89 } 90 91 bool resctrl_arch_mon_capable(void) 92 { 93 struct mpam_resctrl_res *res = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 94 struct rdt_resource *l3 = &res->resctrl_res; 95 96 /* All monitors are presented as being on the L3 cache */ 97 return l3->mon_capable; 98 } 99 100 bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt) 101 { 102 return false; 103 } 104 105 void resctrl_arch_mon_event_config_read(void *info) 106 { 107 } 108 109 void resctrl_arch_mon_event_config_write(void *info) 110 { 111 } 112 113 void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_l3_mon_domain *d) 114 { 115 } 116 117 void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_l3_mon_domain *d, 118 u32 closid, u32 rmid, enum resctrl_event_id eventid) 119 { 120 } 121 122 void resctrl_arch_reset_cntr(struct rdt_resource *r, struct rdt_l3_mon_domain *d, 123 u32 closid, u32 rmid, int cntr_id, 124 enum resctrl_event_id eventid) 125 { 126 } 127 128 int resctrl_arch_cntr_read(struct rdt_resource *r, struct rdt_l3_mon_domain *d, 129 u32 unused, u32 rmid, int cntr_id, 130 enum resctrl_event_id eventid, u64 *val) 131 { 132 return -EOPNOTSUPP; 133 } 134 135 bool resctrl_arch_mbm_cntr_assign_enabled(struct rdt_resource *r) 136 { 137 return (r == &mpam_resctrl_controls[RDT_RESOURCE_L3].resctrl_res); 138 } 139 140 int resctrl_arch_mbm_cntr_assign_set(struct rdt_resource *r, bool enable) 141 { 142 return -EINVAL; 143 } 144 145 int resctrl_arch_io_alloc_enable(struct rdt_resource *r, bool enable) 146 { 147 return -EOPNOTSUPP; 148 } 149 150 bool resctrl_arch_get_io_alloc_enabled(struct rdt_resource *r) 151 { 152 return false; 153 } 154 155 void resctrl_arch_pre_mount(void) 156 { 157 } 158 159 bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level rid) 160 { 161 return mpam_resctrl_controls[rid].cdp_enabled; 162 } 163 164 /** 165 * resctrl_reset_task_closids() - Reset the PARTID/PMG values for all tasks. 166 * 167 * At boot, all existing tasks use partid zero for D and I. 168 * To enable/disable CDP emulation, all these tasks need relabelling. 169 */ 170 static void resctrl_reset_task_closids(void) 171 { 172 struct task_struct *p, *t; 173 174 read_lock(&tasklist_lock); 175 for_each_process_thread(p, t) { 176 resctrl_arch_set_closid_rmid(t, RESCTRL_RESERVED_CLOSID, 177 RESCTRL_RESERVED_RMID); 178 } 179 read_unlock(&tasklist_lock); 180 } 181 182 static void mpam_resctrl_monitor_sync_abmc_vals(struct rdt_resource *l3) 183 { 184 struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[QOS_L3_MBM_TOTAL_EVENT_ID]; 185 186 if (!mon->class) 187 return; 188 189 if (!mon->assigned_counters) 190 return; 191 192 l3->mon.num_mbm_cntrs = mon->class->props.num_mbwu_mon; 193 if (cdp_enabled) 194 l3->mon.num_mbm_cntrs /= 2; 195 196 /* 197 * Continue as normal even if enabling cdp causes there to be 198 * zero counters. This avoids giving resctrl mixed messages. 199 */ 200 } 201 202 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable) 203 { 204 u32 partid_i = RESCTRL_RESERVED_CLOSID, partid_d = RESCTRL_RESERVED_CLOSID; 205 struct mpam_resctrl_res *res = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 206 struct rdt_resource *l3 = &res->resctrl_res; 207 int cpu; 208 209 if (!IS_ENABLED(CONFIG_EXPERT) && enable) { 210 /* 211 * If the resctrl fs is mounted more than once, sequentially, 212 * then CDP can lead to the use of out of range PARTIDs. 213 */ 214 pr_warn("CDP not supported\n"); 215 return -EOPNOTSUPP; 216 } 217 218 if (enable) 219 pr_warn("CDP is an expert feature and may cause MPAM to malfunction.\n"); 220 221 /* 222 * resctrl_arch_set_cdp_enabled() is only called with enable set to 223 * false on error and unmount. 224 */ 225 cdp_enabled = enable; 226 mpam_resctrl_controls[rid].cdp_enabled = enable; 227 228 if (enable) 229 l3->mon.num_rmid = resctrl_arch_system_num_rmid_idx() / 2; 230 else 231 l3->mon.num_rmid = resctrl_arch_system_num_rmid_idx(); 232 233 /* The mbw_max feature can't hide cdp as it's a per-partid maximum. */ 234 if (cdp_enabled && !mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled) 235 mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = false; 236 237 /* 238 * If resctrl has attempted to enable CDP on MBA, re-enable MBA as two 239 * configurations will be provided so there is no aliasing problem. 240 */ 241 if (mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled && 242 mpam_resctrl_controls[RDT_RESOURCE_MBA].class) 243 mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = true; 244 245 /* On unmount when CDP is disabled, re-enable MBA */ 246 if (!cdp_enabled && mpam_resctrl_controls[RDT_RESOURCE_MBA].class) 247 mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = true; 248 249 if (enable) { 250 if (mpam_partid_max < 1) 251 return -EINVAL; 252 253 partid_d = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_DATA); 254 partid_i = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_CODE); 255 } 256 257 mpam_set_task_partid_pmg(current, partid_d, partid_i, 0, 0); 258 WRITE_ONCE(arm64_mpam_global_default, mpam_get_regval(current)); 259 260 resctrl_reset_task_closids(); 261 mpam_resctrl_monitor_sync_abmc_vals(l3); 262 263 for_each_possible_cpu(cpu) 264 mpam_set_cpu_defaults(cpu, partid_d, partid_i, 0, 0); 265 on_each_cpu(resctrl_arch_sync_cpu_closid_rmid, NULL, 1); 266 267 return 0; 268 } 269 270 static bool mpam_resctrl_hide_cdp(enum resctrl_res_level rid) 271 { 272 return cdp_enabled && !resctrl_arch_get_cdp_enabled(rid); 273 } 274 275 /* 276 * MSC may raise an error interrupt if it sees an out or range partid/pmg, 277 * and go on to truncate the value. Regardless of what the hardware supports, 278 * only the system wide safe value is safe to use. 279 */ 280 u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored) 281 { 282 return mpam_partid_max + 1; 283 } 284 285 u32 resctrl_arch_system_num_rmid_idx(void) 286 { 287 return (mpam_pmg_max + 1) * (mpam_partid_max + 1); 288 } 289 290 u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid) 291 { 292 return closid * (mpam_pmg_max + 1) + rmid; 293 } 294 295 void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid) 296 { 297 *closid = idx / (mpam_pmg_max + 1); 298 *rmid = idx % (mpam_pmg_max + 1); 299 } 300 301 void resctrl_arch_sched_in(struct task_struct *tsk) 302 { 303 lockdep_assert_preemption_disabled(); 304 305 mpam_thread_switch(tsk); 306 } 307 308 void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, u32 rmid) 309 { 310 WARN_ON_ONCE(closid > U16_MAX); 311 WARN_ON_ONCE(rmid > U8_MAX); 312 313 if (!cdp_enabled) { 314 mpam_set_cpu_defaults(cpu, closid, closid, rmid, rmid); 315 } else { 316 /* 317 * When CDP is enabled, resctrl halves the closid range and we 318 * use odd/even partid for one closid. 319 */ 320 u32 partid_d = resctrl_get_config_index(closid, CDP_DATA); 321 u32 partid_i = resctrl_get_config_index(closid, CDP_CODE); 322 323 mpam_set_cpu_defaults(cpu, partid_d, partid_i, rmid, rmid); 324 } 325 } 326 327 void resctrl_arch_sync_cpu_closid_rmid(void *info) 328 { 329 struct resctrl_cpu_defaults *r = info; 330 331 lockdep_assert_preemption_disabled(); 332 333 if (r) { 334 resctrl_arch_set_cpu_default_closid_rmid(smp_processor_id(), 335 r->closid, r->rmid); 336 } 337 338 resctrl_arch_sched_in(current); 339 } 340 341 void resctrl_arch_set_closid_rmid(struct task_struct *tsk, u32 closid, u32 rmid) 342 { 343 WARN_ON_ONCE(closid > U16_MAX); 344 WARN_ON_ONCE(rmid > U8_MAX); 345 346 if (!cdp_enabled) { 347 mpam_set_task_partid_pmg(tsk, closid, closid, rmid, rmid); 348 } else { 349 u32 partid_d = resctrl_get_config_index(closid, CDP_DATA); 350 u32 partid_i = resctrl_get_config_index(closid, CDP_CODE); 351 352 mpam_set_task_partid_pmg(tsk, partid_d, partid_i, rmid, rmid); 353 } 354 } 355 356 bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid) 357 { 358 u64 regval = mpam_get_regval(tsk); 359 u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval); 360 361 if (cdp_enabled) 362 tsk_closid >>= 1; 363 364 return tsk_closid == closid; 365 } 366 367 /* The task's pmg is not unique, the partid must be considered too */ 368 bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid) 369 { 370 u64 regval = mpam_get_regval(tsk); 371 u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval); 372 u32 tsk_rmid = FIELD_GET(MPAM0_EL1_PMG_D, regval); 373 374 if (cdp_enabled) 375 tsk_closid >>= 1; 376 377 return (tsk_closid == closid) && (tsk_rmid == rmid); 378 } 379 380 struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l) 381 { 382 if (l >= RDT_NUM_RESOURCES) 383 return NULL; 384 385 return &mpam_resctrl_controls[l].resctrl_res; 386 } 387 388 static int resctrl_arch_mon_ctx_alloc_no_wait(enum resctrl_event_id evtid) 389 { 390 struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[evtid]; 391 392 if (!mpam_is_enabled()) 393 return -EINVAL; 394 395 if (!mon->class) 396 return -EINVAL; 397 398 switch (evtid) { 399 case QOS_L3_OCCUP_EVENT_ID: 400 /* With CDP, one monitor gets used for both code/data reads */ 401 return mpam_alloc_csu_mon(mon->class); 402 case QOS_L3_MBM_LOCAL_EVENT_ID: 403 case QOS_L3_MBM_TOTAL_EVENT_ID: 404 return USE_PRE_ALLOCATED; 405 default: 406 return -EOPNOTSUPP; 407 } 408 } 409 410 void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, 411 enum resctrl_event_id evtid) 412 { 413 DEFINE_WAIT(wait); 414 int *ret; 415 416 ret = kmalloc_obj(*ret); 417 if (!ret) 418 return ERR_PTR(-ENOMEM); 419 420 do { 421 prepare_to_wait(&resctrl_mon_ctx_waiters, &wait, 422 TASK_INTERRUPTIBLE); 423 *ret = resctrl_arch_mon_ctx_alloc_no_wait(evtid); 424 if (*ret == -ENOSPC) 425 schedule(); 426 } while (*ret == -ENOSPC && !signal_pending(current)); 427 finish_wait(&resctrl_mon_ctx_waiters, &wait); 428 429 return ret; 430 } 431 432 static void resctrl_arch_mon_ctx_free_no_wait(enum resctrl_event_id evtid, 433 u32 mon_idx) 434 { 435 struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[evtid]; 436 437 if (!mpam_is_enabled()) 438 return; 439 440 if (!mon->class) 441 return; 442 443 if (evtid == QOS_L3_OCCUP_EVENT_ID) 444 mpam_free_csu_mon(mon->class, mon_idx); 445 446 wake_up(&resctrl_mon_ctx_waiters); 447 } 448 449 void resctrl_arch_mon_ctx_free(struct rdt_resource *r, 450 enum resctrl_event_id evtid, void *arch_mon_ctx) 451 { 452 u32 mon_idx = *(u32 *)arch_mon_ctx; 453 454 kfree(arch_mon_ctx); 455 456 resctrl_arch_mon_ctx_free_no_wait(evtid, mon_idx); 457 } 458 459 static int __read_mon(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, 460 enum mpam_device_features mon_type, 461 int mon_idx, 462 enum resctrl_conf_type cdp_type, u32 closid, u32 rmid, u64 *val) 463 { 464 struct mon_cfg cfg; 465 466 if (!mpam_is_enabled()) 467 return -EINVAL; 468 469 /* Shift closid to account for CDP */ 470 closid = resctrl_get_config_index(closid, cdp_type); 471 472 if (irqs_disabled()) { 473 /* Check if we can access this domain without an IPI */ 474 return -EIO; 475 } 476 477 cfg = (struct mon_cfg) { 478 .mon = mon_idx, 479 .match_pmg = true, 480 .partid = closid, 481 .pmg = rmid, 482 }; 483 484 return mpam_msmon_read(mon_comp, &cfg, mon_type, val); 485 } 486 487 static int read_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, 488 enum mpam_device_features mon_type, 489 int mon_idx, u32 closid, u32 rmid, u64 *val) 490 { 491 if (cdp_enabled) { 492 u64 code_val = 0, data_val = 0; 493 int err; 494 495 err = __read_mon(mon, mon_comp, mon_type, mon_idx, 496 CDP_CODE, closid, rmid, &code_val); 497 if (err) 498 return err; 499 500 err = __read_mon(mon, mon_comp, mon_type, mon_idx, 501 CDP_DATA, closid, rmid, &data_val); 502 if (err) 503 return err; 504 505 *val += code_val + data_val; 506 return 0; 507 } 508 509 return __read_mon(mon, mon_comp, mon_type, mon_idx, 510 CDP_NONE, closid, rmid, val); 511 } 512 513 /* MBWU when not in ABMC mode (not supported), and CSU counters. */ 514 int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain_hdr *hdr, 515 u32 closid, u32 rmid, enum resctrl_event_id eventid, 516 void *arch_priv, u64 *val, void *arch_mon_ctx) 517 { 518 struct mpam_resctrl_dom *l3_dom; 519 struct mpam_component *mon_comp; 520 u32 mon_idx = *(u32 *)arch_mon_ctx; 521 enum mpam_device_features mon_type; 522 struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[eventid]; 523 524 resctrl_arch_rmid_read_context_check(); 525 526 if (!mpam_is_enabled()) 527 return -EINVAL; 528 529 if (eventid >= QOS_NUM_EVENTS || !mon->class) 530 return -EINVAL; 531 532 l3_dom = container_of(hdr, struct mpam_resctrl_dom, resctrl_mon_dom.hdr); 533 mon_comp = l3_dom->mon_comp[eventid]; 534 535 if (eventid != QOS_L3_OCCUP_EVENT_ID) 536 return -EINVAL; 537 538 mon_type = mpam_feat_msmon_csu; 539 540 return read_mon_cdp_safe(mon, mon_comp, mon_type, mon_idx, 541 closid, rmid, val); 542 } 543 544 /* 545 * The rmid realloc threshold should be for the smallest cache exposed to 546 * resctrl. 547 */ 548 static int update_rmid_limits(struct mpam_class *class) 549 { 550 u32 num_unique_pmg = resctrl_arch_system_num_rmid_idx(); 551 struct mpam_props *cprops = &class->props; 552 struct cacheinfo *ci; 553 554 lockdep_assert_cpus_held(); 555 556 if (!mpam_has_feature(mpam_feat_msmon_csu, cprops)) 557 return 0; 558 559 /* 560 * Assume cache levels are the same size for all CPUs... 561 * The check just requires any online CPU and it can't go offline as we 562 * hold the cpu lock. 563 */ 564 ci = get_cpu_cacheinfo_level(raw_smp_processor_id(), class->level); 565 if (!ci || ci->size == 0) { 566 pr_debug("Could not read cache size for class %u\n", 567 class->level); 568 return -EINVAL; 569 } 570 571 if (!resctrl_rmid_realloc_limit || 572 ci->size < resctrl_rmid_realloc_limit) { 573 resctrl_rmid_realloc_limit = ci->size; 574 resctrl_rmid_realloc_threshold = ci->size / num_unique_pmg; 575 } 576 577 return 0; 578 } 579 580 static bool cache_has_usable_cpor(struct mpam_class *class) 581 { 582 struct mpam_props *cprops = &class->props; 583 584 if (!mpam_has_feature(mpam_feat_cpor_part, cprops)) 585 return false; 586 587 /* resctrl uses u32 for all bitmap configurations */ 588 return class->props.cpbm_wd <= 32; 589 } 590 591 static bool mba_class_use_mbw_max(struct mpam_props *cprops) 592 { 593 return (mpam_has_feature(mpam_feat_mbw_max, cprops) && 594 cprops->bwa_wd); 595 } 596 597 static bool class_has_usable_mba(struct mpam_props *cprops) 598 { 599 return mba_class_use_mbw_max(cprops); 600 } 601 602 static bool cache_has_usable_csu(struct mpam_class *class) 603 { 604 struct mpam_props *cprops; 605 606 if (!class) 607 return false; 608 609 cprops = &class->props; 610 611 if (!mpam_has_feature(mpam_feat_msmon_csu, cprops)) 612 return false; 613 614 /* 615 * CSU counters settle on the value, so we can get away with 616 * having only one. 617 */ 618 if (!cprops->num_csu_mon) 619 return false; 620 621 return true; 622 } 623 624 static bool class_has_usable_mbwu(struct mpam_class *class) 625 { 626 struct mpam_props *cprops = &class->props; 627 628 if (!mpam_has_feature(mpam_feat_msmon_mbwu, cprops)) 629 return false; 630 631 if (!cprops->num_mbwu_mon) 632 return false; 633 634 return true; 635 } 636 637 /* 638 * Calculate the worst-case percentage change from each implemented step 639 * in the control. 640 */ 641 static u32 get_mba_granularity(struct mpam_props *cprops) 642 { 643 if (!mba_class_use_mbw_max(cprops)) 644 return 0; 645 646 /* 647 * bwa_wd is the number of bits implemented in the 0.xxx 648 * fixed point fraction. 1 bit is 50%, 2 is 25% etc. 649 */ 650 return DIV_ROUND_UP(MAX_MBA_BW, 1 << cprops->bwa_wd); 651 } 652 653 /* 654 * Each fixed-point hardware value architecturally represents a range 655 * of values: the full range 0% - 100% is split contiguously into 656 * (1 << cprops->bwa_wd) equal bands. 657 * 658 * Although the bwa_bwd fields have 6 bits the maximum valid value is 16 659 * as it reports the width of fields that are at most 16 bits. When 660 * fewer than 16 bits are valid the least significant bits are 661 * ignored. The implied binary point is kept between bits 15 and 16 and 662 * so the valid bits are leftmost. 663 * 664 * See ARM IHI0099B.a "MPAM system component specification", Section 9.3, 665 * "The fixed-point fractional format" for more information. 666 * 667 * Find the nearest percentage value to the upper bound of the selected band: 668 */ 669 static u32 mbw_max_to_percent(u16 mbw_max, struct mpam_props *cprops) 670 { 671 u32 val = mbw_max; 672 673 val >>= 16 - cprops->bwa_wd; 674 val += 1; 675 val *= MAX_MBA_BW; 676 val = DIV_ROUND_CLOSEST(val, 1 << cprops->bwa_wd); 677 678 return val; 679 } 680 681 /* 682 * Find the band whose upper bound is closest to the specified percentage. 683 * 684 * A round-to-nearest policy is followed here as a balanced compromise 685 * between unexpected under-commit of the resource (where the total of 686 * a set of resource allocations after conversion is less than the 687 * expected total, due to rounding of the individual converted 688 * percentages) and over-commit (where the total of the converted 689 * allocations is greater than expected). 690 */ 691 static u16 percent_to_mbw_max(u8 pc, struct mpam_props *cprops) 692 { 693 u32 val = pc; 694 695 val <<= cprops->bwa_wd; 696 val = DIV_ROUND_CLOSEST(val, MAX_MBA_BW); 697 val = max(val, 1) - 1; 698 val <<= 16 - cprops->bwa_wd; 699 700 return val; 701 } 702 703 static u32 get_mba_min(struct mpam_props *cprops) 704 { 705 if (!mba_class_use_mbw_max(cprops)) { 706 WARN_ON_ONCE(1); 707 return 0; 708 } 709 710 return mbw_max_to_percent(0, cprops); 711 } 712 713 /* Find the L3 cache that has affinity with this CPU */ 714 static int find_l3_equivalent_bitmask(int cpu, cpumask_var_t tmp_cpumask) 715 { 716 u32 cache_id = get_cpu_cacheinfo_id(cpu, 3); 717 718 lockdep_assert_cpus_held(); 719 720 return mpam_get_cpumask_from_cache_id(cache_id, 3, tmp_cpumask); 721 } 722 723 /* 724 * topology_matches_l3() - Is the provided class the same shape as L3 725 * @victim: The class we'd like to pretend is L3. 726 * 727 * resctrl expects all the world's a Xeon, and all counters are on the 728 * L3. We allow some mapping counters on other classes. This requires 729 * that the CPU->domain mapping is the same kind of shape. 730 * 731 * Using cacheinfo directly would make this work even if resctrl can't 732 * use the L3 - but cacheinfo can't tell us anything about offline CPUs. 733 * Using the L3 resctrl domain list also depends on CPUs being online. 734 * Using the mpam_class we picked for L3 so we can use its domain list 735 * assumes that there are MPAM controls on the L3. 736 * Instead, this path eventually uses the mpam_get_cpumask_from_cache_id() 737 * helper which can tell us about offline CPUs ... but getting the cache_id 738 * to start with relies on at least one CPU per L3 cache being online at 739 * boot. 740 * 741 * Walk the victim component list and compare the affinity mask with the 742 * corresponding L3. The topology matches if each victim:component's affinity 743 * mask is the same as the CPU's corresponding L3's. These lists/masks are 744 * computed from firmware tables so don't change at runtime. 745 */ 746 static bool topology_matches_l3(struct mpam_class *victim) 747 { 748 int cpu, err; 749 struct mpam_component *victim_iter; 750 751 lockdep_assert_cpus_held(); 752 753 cpumask_var_t __free(free_cpumask_var) tmp_cpumask = CPUMASK_VAR_NULL; 754 if (!alloc_cpumask_var(&tmp_cpumask, GFP_KERNEL)) 755 return false; 756 757 guard(srcu)(&mpam_srcu); 758 list_for_each_entry_srcu(victim_iter, &victim->components, class_list, 759 srcu_read_lock_held(&mpam_srcu)) { 760 if (cpumask_empty(&victim_iter->affinity)) { 761 pr_debug("class %u has CPU-less component %u - can't match L3!\n", 762 victim->level, victim_iter->comp_id); 763 return false; 764 } 765 766 cpu = cpumask_any_and(&victim_iter->affinity, cpu_online_mask); 767 if (WARN_ON_ONCE(cpu >= nr_cpu_ids)) 768 return false; 769 770 cpumask_clear(tmp_cpumask); 771 err = find_l3_equivalent_bitmask(cpu, tmp_cpumask); 772 if (err) { 773 pr_debug("Failed to find L3's equivalent component to class %u component %u\n", 774 victim->level, victim_iter->comp_id); 775 return false; 776 } 777 778 /* Any differing bits in the affinity mask? */ 779 if (!cpumask_equal(tmp_cpumask, &victim_iter->affinity)) { 780 pr_debug("class %u component %u has Mismatched CPU mask with L3 equivalent\n" 781 "L3:%*pbl != victim:%*pbl\n", 782 victim->level, victim_iter->comp_id, 783 cpumask_pr_args(tmp_cpumask), 784 cpumask_pr_args(&victim_iter->affinity)); 785 786 return false; 787 } 788 } 789 790 return true; 791 } 792 793 /* 794 * Test if the traffic for a class matches that at egress from the L3. For 795 * MSC at memory controllers this is only possible if there is a single L3 796 * as otherwise the counters at the memory can include bandwidth from the 797 * non-local L3. 798 */ 799 static bool traffic_matches_l3(struct mpam_class *class) 800 { 801 int err, cpu; 802 803 lockdep_assert_cpus_held(); 804 805 if (class->type == MPAM_CLASS_CACHE && class->level == 3) 806 return true; 807 808 if (class->type == MPAM_CLASS_CACHE && class->level != 3) { 809 pr_debug("class %u is a different cache from L3\n", class->level); 810 return false; 811 } 812 813 if (class->type != MPAM_CLASS_MEMORY) { 814 pr_debug("class %u is neither of type cache or memory\n", class->level); 815 return false; 816 } 817 818 cpumask_var_t __free(free_cpumask_var) tmp_cpumask = CPUMASK_VAR_NULL; 819 if (!alloc_cpumask_var(&tmp_cpumask, GFP_KERNEL)) { 820 pr_debug("cpumask allocation failed\n"); 821 return false; 822 } 823 824 cpu = cpumask_any_and(&class->affinity, cpu_online_mask); 825 err = find_l3_equivalent_bitmask(cpu, tmp_cpumask); 826 if (err) { 827 pr_debug("Failed to find L3 downstream to cpu %d\n", cpu); 828 return false; 829 } 830 831 if (!cpumask_equal(tmp_cpumask, cpu_possible_mask)) { 832 pr_debug("There is more than one L3\n"); 833 return false; 834 } 835 836 /* Be strict; the traffic might stop in the intermediate cache. */ 837 if (get_cpu_cacheinfo_id(cpu, 4) != -1) { 838 pr_debug("L3 isn't the last level of cache\n"); 839 return false; 840 } 841 842 if (num_possible_nodes() > 1) { 843 pr_debug("There is more than one numa node\n"); 844 return false; 845 } 846 847 #ifdef CONFIG_HMEM_REPORTING 848 if (node_devices[cpu_to_node(cpu)]->cache_dev) { 849 pr_debug("There is a memory side cache\n"); 850 return false; 851 } 852 #endif 853 854 return true; 855 } 856 857 /* Test whether we can export MPAM_CLASS_CACHE:{2,3}? */ 858 static void mpam_resctrl_pick_caches(void) 859 { 860 struct mpam_class *class; 861 struct mpam_resctrl_res *res; 862 863 lockdep_assert_cpus_held(); 864 865 guard(srcu)(&mpam_srcu); 866 list_for_each_entry_srcu(class, &mpam_classes, classes_list, 867 srcu_read_lock_held(&mpam_srcu)) { 868 if (class->type != MPAM_CLASS_CACHE) { 869 pr_debug("class %u is not a cache\n", class->level); 870 continue; 871 } 872 873 if (class->level != 2 && class->level != 3) { 874 pr_debug("class %u is not L2 or L3\n", class->level); 875 continue; 876 } 877 878 if (!cache_has_usable_cpor(class)) { 879 pr_debug("class %u cache misses CPOR\n", class->level); 880 continue; 881 } 882 883 if (!cpumask_equal(&class->affinity, cpu_possible_mask)) { 884 pr_debug("class %u has missing CPUs, mask %*pb != %*pb\n", class->level, 885 cpumask_pr_args(&class->affinity), 886 cpumask_pr_args(cpu_possible_mask)); 887 continue; 888 } 889 890 if (class->level == 2) 891 res = &mpam_resctrl_controls[RDT_RESOURCE_L2]; 892 else 893 res = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 894 res->class = class; 895 } 896 } 897 898 static void mpam_resctrl_pick_mba(void) 899 { 900 struct mpam_class *class, *candidate_class = NULL; 901 struct mpam_resctrl_res *res; 902 903 lockdep_assert_cpus_held(); 904 905 guard(srcu)(&mpam_srcu); 906 list_for_each_entry_srcu(class, &mpam_classes, classes_list, 907 srcu_read_lock_held(&mpam_srcu)) { 908 struct mpam_props *cprops = &class->props; 909 910 if (class->level != 3 && class->type == MPAM_CLASS_CACHE) { 911 pr_debug("class %u is a cache but not the L3\n", class->level); 912 continue; 913 } 914 915 if (!class_has_usable_mba(cprops)) { 916 pr_debug("class %u has no bandwidth control\n", 917 class->level); 918 continue; 919 } 920 921 if (!cpumask_equal(&class->affinity, cpu_possible_mask)) { 922 pr_debug("class %u has missing CPUs\n", class->level); 923 continue; 924 } 925 926 if (!topology_matches_l3(class)) { 927 pr_debug("class %u topology doesn't match L3\n", 928 class->level); 929 continue; 930 } 931 932 if (!traffic_matches_l3(class)) { 933 pr_debug("class %u traffic doesn't match L3 egress\n", 934 class->level); 935 continue; 936 } 937 938 /* 939 * Pick a resource to be MBA that as close as possible to 940 * the L3. mbm_total counts the bandwidth leaving the L3 941 * cache and MBA should correspond as closely as possible 942 * for proper operation of mba_sc. 943 */ 944 if (!candidate_class || class->level < candidate_class->level) 945 candidate_class = class; 946 } 947 948 if (candidate_class) { 949 pr_debug("selected class %u to back MBA\n", 950 candidate_class->level); 951 res = &mpam_resctrl_controls[RDT_RESOURCE_MBA]; 952 res->class = candidate_class; 953 } 954 } 955 956 static void __free_mbwu_mon(struct mpam_class *class, int *array, 957 u16 num_mbwu_mon) 958 { 959 for (int i = 0; i < num_mbwu_mon; i++) { 960 if (array[i] < 0) 961 continue; 962 963 mpam_free_mbwu_mon(class, array[i]); 964 array[i] = -1; 965 } 966 } 967 968 static int __alloc_mbwu_mon(struct mpam_class *class, int *array, 969 u16 num_mbwu_mon) 970 { 971 for (int i = 0; i < num_mbwu_mon; i++) { 972 int mbwu_mon = mpam_alloc_mbwu_mon(class); 973 974 if (mbwu_mon < 0) { 975 __free_mbwu_mon(class, array, num_mbwu_mon); 976 return mbwu_mon; 977 } 978 array[i] = mbwu_mon; 979 } 980 981 return 0; 982 } 983 984 static int *__alloc_mbwu_array(struct mpam_class *class, u16 num_mbwu_mon) 985 { 986 int err; 987 988 int *array __free(kvfree) = kvmalloc_objs(*array, num_mbwu_mon); 989 if (!array) 990 return ERR_PTR(-ENOMEM); 991 992 memset(array, -1, num_mbwu_mon * sizeof(*array)); 993 994 err = __alloc_mbwu_mon(class, array, num_mbwu_mon); 995 if (err) 996 return ERR_PTR(err); 997 return_ptr(array); 998 } 999 1000 static void counter_update_class(enum resctrl_event_id evt_id, 1001 struct mpam_class *class) 1002 { 1003 struct mpam_class *existing_class = mpam_resctrl_counters[evt_id].class; 1004 1005 if (existing_class) { 1006 if (class->level == 3) { 1007 pr_debug("Existing class is L3 - L3 wins\n"); 1008 return; 1009 } 1010 1011 if (existing_class->level < class->level) { 1012 pr_debug("Existing class is closer to L3, %u versus %u - closer is better\n", 1013 existing_class->level, class->level); 1014 return; 1015 } 1016 } 1017 1018 mpam_resctrl_counters[evt_id].class = class; 1019 } 1020 1021 static void mpam_resctrl_pick_counters(void) 1022 { 1023 struct mpam_class *class; 1024 1025 lockdep_assert_cpus_held(); 1026 1027 guard(srcu)(&mpam_srcu); 1028 list_for_each_entry_srcu(class, &mpam_classes, classes_list, 1029 srcu_read_lock_held(&mpam_srcu)) { 1030 /* The name of the resource is L3... */ 1031 if (class->type == MPAM_CLASS_CACHE && class->level != 3) { 1032 pr_debug("class %u is a cache but not the L3", class->level); 1033 continue; 1034 } 1035 1036 if (!cpumask_equal(&class->affinity, cpu_possible_mask)) { 1037 pr_debug("class %u does not cover all CPUs", 1038 class->level); 1039 continue; 1040 } 1041 1042 if (cache_has_usable_csu(class)) { 1043 pr_debug("class %u has usable CSU", 1044 class->level); 1045 1046 /* CSU counters only make sense on a cache. */ 1047 switch (class->type) { 1048 case MPAM_CLASS_CACHE: 1049 if (update_rmid_limits(class)) 1050 break; 1051 1052 counter_update_class(QOS_L3_OCCUP_EVENT_ID, class); 1053 break; 1054 default: 1055 break; 1056 } 1057 } 1058 1059 if (class_has_usable_mbwu(class) && 1060 topology_matches_l3(class) && 1061 traffic_matches_l3(class)) { 1062 pr_debug("class %u has usable MBWU, and matches L3 topology and traffic\n", 1063 class->level); 1064 1065 /* 1066 * An MSC measures bandwidth for a path determined by 1067 * its location in hardware. We can't distinguish 1068 * traffic by destination so we don't know if it's 1069 * staying on the same NUMA node. Hence, we can't 1070 * calculate mbm_local except when we only have one L3 1071 * and it's equivalent to mbm_total and so always use 1072 * mbm_total. 1073 */ 1074 counter_update_class(QOS_L3_MBM_TOTAL_EVENT_ID, class); 1075 } 1076 } 1077 } 1078 1079 static void __config_cntr(struct mpam_resctrl_mon *mon, u32 cntr_id, 1080 enum resctrl_conf_type cdp_type, u32 closid, u32 rmid, 1081 bool assign) 1082 { 1083 /* Same CDP index remap as closid; maps cntr_id to assigned_counters[]. */ 1084 u32 mbwu_idx, mon_idx = resctrl_get_config_index(cntr_id, cdp_type); 1085 1086 closid = resctrl_get_config_index(closid, cdp_type); 1087 mbwu_idx = resctrl_arch_rmid_idx_encode(closid, rmid); 1088 1089 if (assign) 1090 mon->mbwu_idx_to_mon[mbwu_idx] = mon->assigned_counters[mon_idx]; 1091 else 1092 mon->mbwu_idx_to_mon[mbwu_idx] = -1; 1093 } 1094 1095 void resctrl_arch_config_cntr(struct rdt_resource *r, struct rdt_l3_mon_domain *d, 1096 enum resctrl_event_id evtid, u32 rmid, u32 closid, 1097 u32 cntr_id, bool assign) 1098 { 1099 struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[evtid]; 1100 1101 if (evtid != QOS_L3_MBM_TOTAL_EVENT_ID) { 1102 pr_debug("unexpected event id\n"); 1103 return; 1104 } 1105 1106 if (!mon->mbwu_idx_to_mon || !mon->assigned_counters) { 1107 pr_debug("monitor arrays not allocated\n"); 1108 return; 1109 } 1110 1111 if (cdp_enabled) { 1112 __config_cntr(mon, cntr_id, CDP_CODE, closid, rmid, assign); 1113 __config_cntr(mon, cntr_id, CDP_DATA, closid, rmid, assign); 1114 } else { 1115 __config_cntr(mon, cntr_id, CDP_NONE, closid, rmid, assign); 1116 } 1117 1118 resctrl_arch_reset_cntr(r, d, closid, rmid, cntr_id, QOS_L3_MBM_TOTAL_EVENT_ID); 1119 } 1120 1121 static int mpam_resctrl_control_init(struct mpam_resctrl_res *res) 1122 { 1123 struct mpam_class *class = res->class; 1124 struct mpam_props *cprops = &class->props; 1125 struct rdt_resource *r = &res->resctrl_res; 1126 1127 switch (r->rid) { 1128 case RDT_RESOURCE_L2: 1129 case RDT_RESOURCE_L3: 1130 r->schema_fmt = RESCTRL_SCHEMA_BITMAP; 1131 r->cache.arch_has_sparse_bitmasks = true; 1132 1133 r->cache.cbm_len = class->props.cpbm_wd; 1134 /* mpam_devices will reject empty bitmaps */ 1135 r->cache.min_cbm_bits = 1; 1136 1137 if (r->rid == RDT_RESOURCE_L2) { 1138 r->name = "L2"; 1139 r->ctrl_scope = RESCTRL_L2_CACHE; 1140 r->cdp_capable = true; 1141 } else { 1142 r->name = "L3"; 1143 r->ctrl_scope = RESCTRL_L3_CACHE; 1144 r->cdp_capable = true; 1145 } 1146 1147 /* 1148 * Which bits are shared with other ...things... Unknown 1149 * devices use partid-0 which uses all the bitmap fields. Until 1150 * we have configured the SMMU and GIC not to do this 'all the 1151 * bits' is the correct answer here. 1152 */ 1153 r->cache.shareable_bits = resctrl_get_default_ctrl(r); 1154 r->alloc_capable = true; 1155 break; 1156 case RDT_RESOURCE_MBA: 1157 r->schema_fmt = RESCTRL_SCHEMA_RANGE; 1158 r->ctrl_scope = RESCTRL_L3_CACHE; 1159 1160 r->membw.delay_linear = true; 1161 r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED; 1162 r->membw.min_bw = get_mba_min(cprops); 1163 r->membw.max_bw = MAX_MBA_BW; 1164 r->membw.bw_gran = get_mba_granularity(cprops); 1165 1166 r->name = "MB"; 1167 r->alloc_capable = true; 1168 break; 1169 default: 1170 return -EINVAL; 1171 } 1172 1173 return 0; 1174 } 1175 1176 static int mpam_resctrl_pick_domain_id(int cpu, struct mpam_component *comp) 1177 { 1178 struct mpam_class *class = comp->class; 1179 1180 if (class->type == MPAM_CLASS_CACHE) 1181 return comp->comp_id; 1182 1183 if (topology_matches_l3(class)) { 1184 /* Use the corresponding L3 component ID as the domain ID */ 1185 int id = get_cpu_cacheinfo_id(cpu, 3); 1186 1187 /* Implies topology_matches_l3() made a mistake */ 1188 if (WARN_ON_ONCE(id == -1)) 1189 return comp->comp_id; 1190 1191 return id; 1192 } 1193 1194 /* Otherwise, expose the ID used by the firmware table code. */ 1195 return comp->comp_id; 1196 } 1197 1198 /* 1199 * This must run after all event counters have been picked so that any free 1200 * running counters have already been allocated. 1201 */ 1202 static int mpam_resctrl_monitor_init_abmc(struct mpam_resctrl_mon *mon) 1203 { 1204 struct mpam_resctrl_res *res = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 1205 size_t num_rmid = resctrl_arch_system_num_rmid_idx(); 1206 struct rdt_resource *l3 = &res->resctrl_res; 1207 struct mpam_class *class = mon->class; 1208 u16 num_mbwu_mon; 1209 int *cntrs; 1210 1211 int *rmid_array __free(kvfree) = kvmalloc_objs(*rmid_array, num_rmid); 1212 if (!rmid_array) { 1213 pr_debug("Failed to allocate RMID array\n"); 1214 return -ENOMEM; 1215 } 1216 memset(rmid_array, -1, num_rmid * sizeof(*rmid_array)); 1217 1218 num_mbwu_mon = class->props.num_mbwu_mon; 1219 cntrs = __alloc_mbwu_array(mon->class, num_mbwu_mon); 1220 if (IS_ERR(cntrs)) 1221 return PTR_ERR(cntrs); 1222 mon->assigned_counters = cntrs; 1223 mon->mbwu_idx_to_mon = no_free_ptr(rmid_array); 1224 1225 l3->mon.mbm_cntr_assignable = true; 1226 l3->mon.mbm_assign_on_mkdir = true; 1227 l3->mon.mbm_cntr_configurable = false; 1228 l3->mon.mbm_cntr_assign_fixed = true; 1229 1230 mpam_resctrl_monitor_sync_abmc_vals(l3); 1231 1232 return 0; 1233 } 1234 1235 static int mpam_resctrl_monitor_init(struct mpam_resctrl_mon *mon, 1236 enum resctrl_event_id type) 1237 { 1238 struct mpam_resctrl_res *res = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 1239 struct rdt_resource *l3 = &res->resctrl_res; 1240 1241 lockdep_assert_cpus_held(); 1242 1243 /* 1244 * There also needs to be an L3 cache present. 1245 * The check just requires any online CPU and it can't go offline as we 1246 * hold the cpu lock. 1247 */ 1248 if (get_cpu_cacheinfo_id(raw_smp_processor_id(), 3) == -1) 1249 return 0; 1250 1251 /* 1252 * If there are no MPAM resources on L3, force it into existence. 1253 * topology_matches_l3() already ensures this looks like the L3. 1254 * The domain-ids will be fixed up by mpam_resctrl_domain_hdr_init(). 1255 */ 1256 if (!res->class) { 1257 pr_warn_once("Faking L3 MSC to enable counters.\n"); 1258 res->class = mpam_resctrl_counters[type].class; 1259 } 1260 1261 /* 1262 * Called multiple times!, once per event type that has a 1263 * monitoring class. 1264 * Setting name is necessary on monitor only platforms. 1265 */ 1266 l3->name = "L3"; 1267 l3->mon_scope = RESCTRL_L3_CACHE; 1268 1269 /* 1270 * num-rmid is the upper bound for the number of monitoring groups that 1271 * can exist simultaneously, including the default monitoring group for 1272 * each control group. Hence, advertise the whole rmid_idx space even 1273 * though each control group has its own pmg/rmid space. Unfortunately, 1274 * this does mean userspace needs to know the architecture to correctly 1275 * interpret this value. 1276 */ 1277 l3->mon.num_rmid = resctrl_arch_system_num_rmid_idx(); 1278 1279 if (type == QOS_L3_MBM_TOTAL_EVENT_ID) { 1280 int err; 1281 1282 err = mpam_resctrl_monitor_init_abmc(mon); 1283 if (err) 1284 return err; 1285 1286 static_assert(MAX_EVT_CONFIG_BITS == 0x7f); 1287 l3->mon.mbm_cfg_mask = MAX_EVT_CONFIG_BITS; 1288 } 1289 1290 if (!resctrl_enable_mon_event(type, false, 0, NULL)) 1291 return -EINVAL; 1292 1293 l3->mon_capable = true; 1294 1295 return 0; 1296 } 1297 1298 u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d, 1299 u32 closid, enum resctrl_conf_type type) 1300 { 1301 u32 partid; 1302 struct mpam_config *cfg; 1303 struct mpam_props *cprops; 1304 struct mpam_resctrl_res *res; 1305 struct mpam_resctrl_dom *dom; 1306 enum mpam_device_features configured_by; 1307 1308 lockdep_assert_cpus_held(); 1309 1310 if (!mpam_is_enabled()) 1311 return resctrl_get_default_ctrl(r); 1312 1313 res = container_of(r, struct mpam_resctrl_res, resctrl_res); 1314 dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom); 1315 cprops = &res->class->props; 1316 1317 /* 1318 * When CDP is enabled, but the resource doesn't support it, 1319 * the control is cloned across both partids. 1320 * Pick one at random to read: 1321 */ 1322 if (mpam_resctrl_hide_cdp(r->rid)) 1323 type = CDP_DATA; 1324 1325 partid = resctrl_get_config_index(closid, type); 1326 cfg = &dom->ctrl_comp->cfg[partid]; 1327 1328 switch (r->rid) { 1329 case RDT_RESOURCE_L2: 1330 case RDT_RESOURCE_L3: 1331 configured_by = mpam_feat_cpor_part; 1332 break; 1333 case RDT_RESOURCE_MBA: 1334 if (mpam_has_feature(mpam_feat_mbw_max, cprops)) { 1335 configured_by = mpam_feat_mbw_max; 1336 break; 1337 } 1338 fallthrough; 1339 default: 1340 return resctrl_get_default_ctrl(r); 1341 } 1342 1343 if (!r->alloc_capable || partid >= resctrl_arch_get_num_closid(r) || 1344 !mpam_has_feature(configured_by, cfg)) 1345 return resctrl_get_default_ctrl(r); 1346 1347 switch (configured_by) { 1348 case mpam_feat_cpor_part: 1349 return cfg->cpbm; 1350 case mpam_feat_mbw_max: 1351 return mbw_max_to_percent(cfg->mbw_max, cprops); 1352 default: 1353 return resctrl_get_default_ctrl(r); 1354 } 1355 } 1356 1357 int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d, 1358 u32 closid, enum resctrl_conf_type t, u32 cfg_val) 1359 { 1360 int err; 1361 u32 partid; 1362 struct mpam_config cfg; 1363 struct mpam_props *cprops; 1364 struct mpam_resctrl_res *res; 1365 struct mpam_resctrl_dom *dom; 1366 1367 lockdep_assert_cpus_held(); 1368 lockdep_assert_irqs_enabled(); 1369 1370 if (!mpam_is_enabled()) 1371 return -EINVAL; 1372 1373 /* 1374 * No need to check the CPU as mpam_apply_config() doesn't care, and 1375 * resctrl_arch_update_domains() relies on this. 1376 */ 1377 res = container_of(r, struct mpam_resctrl_res, resctrl_res); 1378 dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom); 1379 cprops = &res->class->props; 1380 1381 if (mpam_resctrl_hide_cdp(r->rid)) 1382 t = CDP_DATA; 1383 1384 partid = resctrl_get_config_index(closid, t); 1385 if (!r->alloc_capable || partid >= resctrl_arch_get_num_closid(r)) { 1386 pr_debug("Not alloc capable or computed PARTID out of range\n"); 1387 return -EINVAL; 1388 } 1389 1390 /* 1391 * Copy the current config to avoid clearing other resources when the 1392 * same component is exposed multiple times through resctrl. 1393 */ 1394 cfg = dom->ctrl_comp->cfg[partid]; 1395 1396 switch (r->rid) { 1397 case RDT_RESOURCE_L2: 1398 case RDT_RESOURCE_L3: 1399 cfg.cpbm = cfg_val; 1400 mpam_set_feature(mpam_feat_cpor_part, &cfg); 1401 break; 1402 case RDT_RESOURCE_MBA: 1403 if (mpam_has_feature(mpam_feat_mbw_max, cprops)) { 1404 cfg.mbw_max = percent_to_mbw_max(cfg_val, cprops); 1405 mpam_set_feature(mpam_feat_mbw_max, &cfg); 1406 break; 1407 } 1408 fallthrough; 1409 default: 1410 return -EINVAL; 1411 } 1412 1413 /* 1414 * When CDP is enabled, but the resource doesn't support it, we need to 1415 * apply the same configuration to the other partid. 1416 */ 1417 if (mpam_resctrl_hide_cdp(r->rid)) { 1418 partid = resctrl_get_config_index(closid, CDP_CODE); 1419 err = mpam_apply_config(dom->ctrl_comp, partid, &cfg); 1420 if (err) 1421 return err; 1422 1423 partid = resctrl_get_config_index(closid, CDP_DATA); 1424 return mpam_apply_config(dom->ctrl_comp, partid, &cfg); 1425 } 1426 1427 return mpam_apply_config(dom->ctrl_comp, partid, &cfg); 1428 } 1429 1430 int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid) 1431 { 1432 int err; 1433 struct rdt_ctrl_domain *d; 1434 1435 lockdep_assert_cpus_held(); 1436 lockdep_assert_irqs_enabled(); 1437 1438 if (!mpam_is_enabled()) 1439 return -EINVAL; 1440 1441 list_for_each_entry_rcu(d, &r->ctrl_domains, hdr.list) { 1442 for (enum resctrl_conf_type t = 0; t < CDP_NUM_TYPES; t++) { 1443 struct resctrl_staged_config *cfg = &d->staged_config[t]; 1444 1445 if (!cfg->have_new_ctrl) 1446 continue; 1447 1448 err = resctrl_arch_update_one(r, d, closid, t, 1449 cfg->new_ctrl); 1450 if (err) 1451 return err; 1452 } 1453 } 1454 1455 return 0; 1456 } 1457 1458 void resctrl_arch_reset_all_ctrls(struct rdt_resource *r) 1459 { 1460 struct mpam_resctrl_res *res; 1461 1462 lockdep_assert_cpus_held(); 1463 1464 if (!mpam_is_enabled()) 1465 return; 1466 1467 res = container_of(r, struct mpam_resctrl_res, resctrl_res); 1468 mpam_reset_class_locked(res->class); 1469 } 1470 1471 static void mpam_resctrl_domain_hdr_init(int cpu, struct mpam_component *comp, 1472 enum resctrl_res_level rid, 1473 struct rdt_domain_hdr *hdr) 1474 { 1475 lockdep_assert_cpus_held(); 1476 1477 INIT_LIST_HEAD(&hdr->list); 1478 hdr->id = mpam_resctrl_pick_domain_id(cpu, comp); 1479 hdr->rid = rid; 1480 cpumask_set_cpu(cpu, &hdr->cpu_mask); 1481 } 1482 1483 static void mpam_resctrl_online_domain_hdr(unsigned int cpu, 1484 struct rdt_domain_hdr *hdr) 1485 { 1486 lockdep_assert_cpus_held(); 1487 1488 cpumask_set_cpu(cpu, &hdr->cpu_mask); 1489 } 1490 1491 /** 1492 * mpam_resctrl_offline_domain_hdr() - Update the domain header to remove a CPU. 1493 * @cpu: The CPU to remove from the domain. 1494 * @hdr: The domain's header. 1495 * 1496 * Removes @cpu from the header mask. If this was the last CPU in the domain, 1497 * the domain header is removed from its parent list and true is returned, 1498 * indicating the parent structure can be freed. 1499 * If there are other CPUs in the domain, returns false. 1500 */ 1501 static bool mpam_resctrl_offline_domain_hdr(unsigned int cpu, 1502 struct rdt_domain_hdr *hdr) 1503 { 1504 lockdep_assert_held(&domain_list_lock); 1505 1506 cpumask_clear_cpu(cpu, &hdr->cpu_mask); 1507 if (cpumask_empty(&hdr->cpu_mask)) { 1508 list_del_rcu(&hdr->list); 1509 synchronize_rcu(); 1510 return true; 1511 } 1512 1513 return false; 1514 } 1515 1516 static void mpam_resctrl_domain_insert(struct list_head *list, 1517 struct rdt_domain_hdr *new) 1518 { 1519 struct rdt_domain_hdr *err; 1520 struct list_head *pos = NULL; 1521 1522 lockdep_assert_held(&domain_list_lock); 1523 1524 err = resctrl_find_domain(list, new->id, &pos); 1525 if (WARN_ON_ONCE(err)) 1526 return; 1527 1528 list_add_tail_rcu(&new->list, pos); 1529 } 1530 1531 static struct mpam_component *find_component(struct mpam_class *class, int cpu) 1532 { 1533 struct mpam_component *comp; 1534 1535 guard(srcu)(&mpam_srcu); 1536 list_for_each_entry_srcu(comp, &class->components, class_list, 1537 srcu_read_lock_held(&mpam_srcu)) { 1538 if (cpumask_test_cpu(cpu, &comp->affinity)) 1539 return comp; 1540 } 1541 1542 return NULL; 1543 } 1544 1545 static struct mpam_resctrl_dom * 1546 mpam_resctrl_alloc_domain(unsigned int cpu, struct mpam_resctrl_res *res) 1547 { 1548 int err; 1549 struct mpam_resctrl_dom *dom; 1550 struct rdt_l3_mon_domain *mon_d; 1551 struct rdt_ctrl_domain *ctrl_d; 1552 struct mpam_class *class = res->class; 1553 struct mpam_component *comp_iter, *ctrl_comp; 1554 struct rdt_resource *r = &res->resctrl_res; 1555 1556 lockdep_assert_held(&domain_list_lock); 1557 1558 ctrl_comp = NULL; 1559 guard(srcu)(&mpam_srcu); 1560 list_for_each_entry_srcu(comp_iter, &class->components, class_list, 1561 srcu_read_lock_held(&mpam_srcu)) { 1562 if (cpumask_test_cpu(cpu, &comp_iter->affinity)) { 1563 ctrl_comp = comp_iter; 1564 break; 1565 } 1566 } 1567 1568 /* class has no component for this CPU */ 1569 if (WARN_ON_ONCE(!ctrl_comp)) 1570 return ERR_PTR(-EINVAL); 1571 1572 dom = kzalloc_node(sizeof(*dom), GFP_KERNEL, cpu_to_node(cpu)); 1573 if (!dom) 1574 return ERR_PTR(-ENOMEM); 1575 1576 if (r->alloc_capable) { 1577 dom->ctrl_comp = ctrl_comp; 1578 1579 ctrl_d = &dom->resctrl_ctrl_dom; 1580 mpam_resctrl_domain_hdr_init(cpu, ctrl_comp, r->rid, &ctrl_d->hdr); 1581 ctrl_d->hdr.type = RESCTRL_CTRL_DOMAIN; 1582 err = resctrl_online_ctrl_domain(r, ctrl_d); 1583 if (err) 1584 goto free_domain; 1585 1586 mpam_resctrl_domain_insert(&r->ctrl_domains, &ctrl_d->hdr); 1587 } else { 1588 pr_debug("Skipped control domain online - no controls\n"); 1589 } 1590 1591 if (r->mon_capable) { 1592 struct mpam_component *any_mon_comp = NULL; 1593 struct mpam_resctrl_mon *mon; 1594 enum resctrl_event_id eventid; 1595 1596 /* 1597 * Even if the monitor domain is backed by a different 1598 * component, the L3 component IDs need to be used... only 1599 * there may be no ctrl_comp for the L3. 1600 * Search each event's class list for a component with 1601 * overlapping CPUs and set up the dom->mon_comp array. 1602 */ 1603 1604 for_each_mpam_resctrl_mon(mon, eventid) { 1605 struct mpam_component *mon_comp; 1606 1607 if (!mon->class) 1608 continue; // dummy resource 1609 1610 mon_comp = find_component(mon->class, cpu); 1611 dom->mon_comp[eventid] = mon_comp; 1612 if (mon_comp) 1613 any_mon_comp = mon_comp; 1614 } 1615 if (!any_mon_comp) { 1616 WARN_ON_ONCE(0); 1617 err = -EFAULT; 1618 goto offline_ctrl_domain; 1619 } 1620 1621 mon_d = &dom->resctrl_mon_dom; 1622 mpam_resctrl_domain_hdr_init(cpu, any_mon_comp, r->rid, &mon_d->hdr); 1623 mon_d->hdr.type = RESCTRL_MON_DOMAIN; 1624 err = resctrl_online_mon_domain(r, &mon_d->hdr); 1625 if (err) 1626 goto offline_ctrl_domain; 1627 1628 mpam_resctrl_domain_insert(&r->mon_domains, &mon_d->hdr); 1629 } else { 1630 pr_debug("Skipped monitor domain online - no monitors\n"); 1631 } 1632 1633 return dom; 1634 1635 offline_ctrl_domain: 1636 if (r->alloc_capable) { 1637 mpam_resctrl_offline_domain_hdr(cpu, &ctrl_d->hdr); 1638 resctrl_offline_ctrl_domain(r, ctrl_d); 1639 } 1640 free_domain: 1641 kfree(dom); 1642 dom = ERR_PTR(err); 1643 1644 return dom; 1645 } 1646 1647 /* 1648 * We know all the monitors are associated with the L3, even if there are no 1649 * controls and therefore no control component. Find the cache-id for the CPU 1650 * and use that to search for existing resctrl domains. 1651 * This relies on mpam_resctrl_pick_domain_id() using the L3 cache-id 1652 * for anything that is not a cache. 1653 */ 1654 static struct mpam_resctrl_dom *mpam_resctrl_get_mon_domain_from_cpu(int cpu) 1655 { 1656 int cache_id; 1657 struct mpam_resctrl_dom *dom; 1658 struct mpam_resctrl_res *l3 = &mpam_resctrl_controls[RDT_RESOURCE_L3]; 1659 1660 lockdep_assert_cpus_held(); 1661 1662 if (!l3->class) 1663 return NULL; 1664 cache_id = get_cpu_cacheinfo_id(cpu, 3); 1665 if (cache_id < 0) 1666 return NULL; 1667 1668 list_for_each_entry_rcu(dom, &l3->resctrl_res.mon_domains, resctrl_mon_dom.hdr.list) { 1669 if (dom->resctrl_mon_dom.hdr.id == cache_id) 1670 return dom; 1671 } 1672 1673 return NULL; 1674 } 1675 1676 static struct mpam_resctrl_dom * 1677 mpam_resctrl_get_domain_from_cpu(int cpu, struct mpam_resctrl_res *res) 1678 { 1679 struct mpam_resctrl_dom *dom; 1680 struct rdt_resource *r = &res->resctrl_res; 1681 1682 lockdep_assert_cpus_held(); 1683 1684 list_for_each_entry_rcu(dom, &r->ctrl_domains, resctrl_ctrl_dom.hdr.list) { 1685 if (cpumask_test_cpu(cpu, &dom->ctrl_comp->affinity)) 1686 return dom; 1687 } 1688 1689 if (r->rid != RDT_RESOURCE_L3) 1690 return NULL; 1691 1692 /* Search the mon domain list too - needed on monitor only platforms. */ 1693 return mpam_resctrl_get_mon_domain_from_cpu(cpu); 1694 } 1695 1696 int mpam_resctrl_online_cpu(unsigned int cpu) 1697 { 1698 struct mpam_resctrl_res *res; 1699 enum resctrl_res_level rid; 1700 1701 guard(mutex)(&domain_list_lock); 1702 for_each_mpam_resctrl_control(res, rid) { 1703 struct mpam_resctrl_dom *dom; 1704 struct rdt_resource *r = &res->resctrl_res; 1705 1706 if (!res->class) 1707 continue; // dummy_resource; 1708 1709 dom = mpam_resctrl_get_domain_from_cpu(cpu, res); 1710 if (!dom) { 1711 dom = mpam_resctrl_alloc_domain(cpu, res); 1712 if (IS_ERR(dom)) 1713 return PTR_ERR(dom); 1714 } else { 1715 if (r->alloc_capable) { 1716 struct rdt_ctrl_domain *ctrl_d = &dom->resctrl_ctrl_dom; 1717 1718 mpam_resctrl_online_domain_hdr(cpu, &ctrl_d->hdr); 1719 } 1720 if (r->mon_capable) { 1721 struct rdt_l3_mon_domain *mon_d = &dom->resctrl_mon_dom; 1722 1723 mpam_resctrl_online_domain_hdr(cpu, &mon_d->hdr); 1724 } 1725 } 1726 } 1727 1728 resctrl_online_cpu(cpu); 1729 1730 return 0; 1731 } 1732 1733 void mpam_resctrl_offline_cpu(unsigned int cpu) 1734 { 1735 struct mpam_resctrl_res *res; 1736 enum resctrl_res_level rid; 1737 1738 resctrl_offline_cpu(cpu); 1739 1740 guard(mutex)(&domain_list_lock); 1741 for_each_mpam_resctrl_control(res, rid) { 1742 struct mpam_resctrl_dom *dom; 1743 struct rdt_l3_mon_domain *mon_d; 1744 struct rdt_ctrl_domain *ctrl_d; 1745 bool ctrl_dom_empty, mon_dom_empty; 1746 struct rdt_resource *r = &res->resctrl_res; 1747 1748 if (!res->class) 1749 continue; // dummy resource 1750 1751 dom = mpam_resctrl_get_domain_from_cpu(cpu, res); 1752 if (WARN_ON_ONCE(!dom)) 1753 continue; 1754 1755 if (r->alloc_capable) { 1756 ctrl_d = &dom->resctrl_ctrl_dom; 1757 ctrl_dom_empty = mpam_resctrl_offline_domain_hdr(cpu, &ctrl_d->hdr); 1758 if (ctrl_dom_empty) 1759 resctrl_offline_ctrl_domain(&res->resctrl_res, ctrl_d); 1760 } else { 1761 ctrl_dom_empty = true; 1762 } 1763 1764 if (r->mon_capable) { 1765 mon_d = &dom->resctrl_mon_dom; 1766 mon_dom_empty = mpam_resctrl_offline_domain_hdr(cpu, &mon_d->hdr); 1767 if (mon_dom_empty) 1768 resctrl_offline_mon_domain(&res->resctrl_res, &mon_d->hdr); 1769 } else { 1770 mon_dom_empty = true; 1771 } 1772 1773 if (ctrl_dom_empty && mon_dom_empty) 1774 kfree(dom); 1775 } 1776 } 1777 1778 int mpam_resctrl_setup(void) 1779 { 1780 int err = 0; 1781 struct mpam_resctrl_res *res; 1782 enum resctrl_res_level rid; 1783 struct mpam_resctrl_mon *mon; 1784 enum resctrl_event_id eventid; 1785 1786 wait_event(wait_cacheinfo_ready, cacheinfo_ready); 1787 1788 cpus_read_lock(); 1789 for_each_mpam_resctrl_control(res, rid) { 1790 INIT_LIST_HEAD_RCU(&res->resctrl_res.ctrl_domains); 1791 INIT_LIST_HEAD_RCU(&res->resctrl_res.mon_domains); 1792 res->resctrl_res.rid = rid; 1793 } 1794 1795 /* Find some classes to use for controls */ 1796 mpam_resctrl_pick_caches(); 1797 mpam_resctrl_pick_mba(); 1798 1799 /* Initialise the resctrl structures from the classes */ 1800 for_each_mpam_resctrl_control(res, rid) { 1801 if (!res->class) 1802 continue; // dummy resource 1803 1804 err = mpam_resctrl_control_init(res); 1805 if (err) { 1806 pr_debug("Failed to initialise rid %u\n", rid); 1807 goto internal_error; 1808 } 1809 } 1810 1811 /* Find some classes to use for monitors */ 1812 mpam_resctrl_pick_counters(); 1813 1814 for_each_mpam_resctrl_mon(mon, eventid) { 1815 if (!mon->class) 1816 continue; // dummy resource 1817 1818 err = mpam_resctrl_monitor_init(mon, eventid); 1819 if (err) { 1820 pr_debug("Failed to initialise event %u\n", eventid); 1821 goto internal_error; 1822 } 1823 } 1824 1825 cpus_read_unlock(); 1826 1827 if (!resctrl_arch_alloc_capable() && !resctrl_arch_mon_capable()) { 1828 pr_debug("No alloc(%u) or monitor(%u) found - resctrl not supported\n", 1829 resctrl_arch_alloc_capable(), resctrl_arch_mon_capable()); 1830 return -EOPNOTSUPP; 1831 } 1832 1833 err = resctrl_init(); 1834 if (err) 1835 return err; 1836 1837 WRITE_ONCE(resctrl_enabled, true); 1838 1839 return 0; 1840 1841 internal_error: 1842 cpus_read_unlock(); 1843 pr_debug("Internal error %d - resctrl not supported\n", err); 1844 return err; 1845 } 1846 1847 void mpam_resctrl_exit(void) 1848 { 1849 if (!READ_ONCE(resctrl_enabled)) 1850 return; 1851 1852 WRITE_ONCE(resctrl_enabled, false); 1853 resctrl_exit(); 1854 } 1855 1856 static void mpam_resctrl_teardown_mon(struct mpam_resctrl_mon *mon, struct mpam_class *class) 1857 { 1858 u32 num_mbwu_mon = class->props.num_mbwu_mon; 1859 1860 if (!mon->mbwu_idx_to_mon) 1861 return; 1862 1863 if (mon->assigned_counters) { 1864 __free_mbwu_mon(class, mon->assigned_counters, num_mbwu_mon); 1865 kvfree(mon->assigned_counters); 1866 mon->assigned_counters = NULL; 1867 } 1868 1869 kvfree(mon->mbwu_idx_to_mon); 1870 mon->mbwu_idx_to_mon = NULL; 1871 } 1872 1873 /* 1874 * The driver is detaching an MSC from this class, if resctrl was using it, 1875 * pull on resctrl_exit(). 1876 */ 1877 void mpam_resctrl_teardown_class(struct mpam_class *class) 1878 { 1879 struct mpam_resctrl_res *res; 1880 enum resctrl_res_level rid; 1881 struct mpam_resctrl_mon *mon; 1882 enum resctrl_event_id eventid; 1883 1884 might_sleep(); 1885 1886 for_each_mpam_resctrl_control(res, rid) { 1887 if (res->class == class) { 1888 res->class = NULL; 1889 break; 1890 } 1891 } 1892 for_each_mpam_resctrl_mon(mon, eventid) { 1893 if (mon->class == class) { 1894 mon->class = NULL; 1895 1896 mpam_resctrl_teardown_mon(mon, class); 1897 break; 1898 } 1899 } 1900 } 1901 1902 static int __init __cacheinfo_ready(void) 1903 { 1904 cacheinfo_ready = true; 1905 wake_up(&wait_cacheinfo_ready); 1906 1907 return 0; 1908 } 1909 device_initcall_sync(__cacheinfo_ready); 1910 1911 #ifdef CONFIG_MPAM_KUNIT_TEST 1912 #include "test_mpam_resctrl.c" 1913 #endif 1914