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