1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * User interface for Resource Allocation in Resource Director Technology(RDT) 4 * 5 * Copyright (C) 2016 Intel Corporation 6 * 7 * Author: Fenghua Yu <fenghua.yu@intel.com> 8 * 9 * More information about RDT be found in the Intel (R) x86 Architecture 10 * Software Developer Manual. 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/cpu.h> 16 #include <linux/debugfs.h> 17 #include <linux/fs.h> 18 #include <linux/fs_parser.h> 19 #include <linux/sysfs.h> 20 #include <linux/kernfs.h> 21 #include <linux/once.h> 22 #include <linux/resctrl.h> 23 #include <linux/seq_buf.h> 24 #include <linux/seq_file.h> 25 #include <linux/sched/task.h> 26 #include <linux/slab.h> 27 #include <linux/user_namespace.h> 28 29 #include <uapi/linux/magic.h> 30 31 #include "internal.h" 32 33 /* Mutex to protect rdtgroup access. */ 34 DEFINE_MUTEX(rdtgroup_mutex); 35 36 static struct kernfs_root *rdt_root; 37 38 struct rdtgroup rdtgroup_default; 39 40 LIST_HEAD(rdt_all_groups); 41 42 /* list of entries for the schemata file */ 43 LIST_HEAD(resctrl_schema_all); 44 45 /* 46 * List of struct mon_data containing private data of event files for use by 47 * rdtgroup_mondata_show(). Protected by rdtgroup_mutex. 48 */ 49 static LIST_HEAD(mon_data_kn_priv_list); 50 51 /* The filesystem can only be mounted once. */ 52 bool resctrl_mounted; 53 54 /* Kernel fs node for "info" directory under root */ 55 static struct kernfs_node *kn_info; 56 57 /* Kernel fs node for "mon_groups" directory under root */ 58 static struct kernfs_node *kn_mongrp; 59 60 /* Kernel fs node for "mon_data" directory under root */ 61 static struct kernfs_node *kn_mondata; 62 63 /* 64 * Used to store the max resource name width to display the schemata names in 65 * a tabular format. 66 */ 67 int max_name_width; 68 69 static struct seq_buf last_cmd_status; 70 71 static char last_cmd_status_buf[512]; 72 73 static int rdtgroup_setup_root(struct rdt_fs_context *ctx); 74 75 static void rdtgroup_destroy_root(void); 76 77 struct dentry *debugfs_resctrl; 78 79 /* 80 * Memory bandwidth monitoring event to use for the default CTRL_MON group 81 * and each new CTRL_MON group created by the user. Only relevant when 82 * the filesystem is mounted with the "mba_MBps" option so it does not 83 * matter that it remains uninitialized on systems that do not support 84 * the "mba_MBps" option. 85 */ 86 enum resctrl_event_id mba_mbps_default_event; 87 88 static bool resctrl_debug; 89 90 void rdt_last_cmd_clear(void) 91 { 92 lockdep_assert_held(&rdtgroup_mutex); 93 seq_buf_clear(&last_cmd_status); 94 } 95 96 void rdt_last_cmd_puts(const char *s) 97 { 98 lockdep_assert_held(&rdtgroup_mutex); 99 seq_buf_puts(&last_cmd_status, s); 100 } 101 102 void rdt_last_cmd_printf(const char *fmt, ...) 103 { 104 va_list ap; 105 106 va_start(ap, fmt); 107 lockdep_assert_held(&rdtgroup_mutex); 108 seq_buf_vprintf(&last_cmd_status, fmt, ap); 109 va_end(ap); 110 } 111 112 void rdt_staged_configs_clear(void) 113 { 114 struct rdt_ctrl_domain *dom; 115 struct rdt_resource *r; 116 117 lockdep_assert_held(&rdtgroup_mutex); 118 119 for_each_alloc_capable_rdt_resource(r) { 120 list_for_each_entry(dom, &r->ctrl_domains, hdr.list) 121 memset(dom->staged_config, 0, sizeof(dom->staged_config)); 122 } 123 } 124 125 static bool resctrl_is_mbm_enabled(void) 126 { 127 return (resctrl_is_mon_event_enabled(QOS_L3_MBM_TOTAL_EVENT_ID) || 128 resctrl_is_mon_event_enabled(QOS_L3_MBM_LOCAL_EVENT_ID)); 129 } 130 131 /* 132 * Trivial allocator for CLOSIDs. Use BITMAP APIs to manipulate a bitmap 133 * of free CLOSIDs. 134 * 135 * Using a global CLOSID across all resources has some advantages and 136 * some drawbacks: 137 * + We can simply set current's closid to assign a task to a resource 138 * group. 139 * + Context switch code can avoid extra memory references deciding which 140 * CLOSID to load into the PQR_ASSOC MSR 141 * - We give up some options in configuring resource groups across multi-socket 142 * systems. 143 * - Our choices on how to configure each resource become progressively more 144 * limited as the number of resources grows. 145 */ 146 static unsigned long *closid_free_map; 147 148 static int closid_free_map_len; 149 150 int closids_supported(void) 151 { 152 return closid_free_map_len; 153 } 154 155 static int closid_init(void) 156 { 157 struct resctrl_schema *s; 158 u32 rdt_min_closid = ~0; 159 160 /* Monitor only platforms still call closid_init() */ 161 if (list_empty(&resctrl_schema_all)) 162 return 0; 163 164 /* Compute rdt_min_closid across all resources */ 165 list_for_each_entry(s, &resctrl_schema_all, list) 166 rdt_min_closid = min(rdt_min_closid, s->num_closid); 167 168 closid_free_map = bitmap_alloc(rdt_min_closid, GFP_KERNEL); 169 if (!closid_free_map) 170 return -ENOMEM; 171 bitmap_fill(closid_free_map, rdt_min_closid); 172 173 /* RESCTRL_RESERVED_CLOSID is always reserved for the default group */ 174 __clear_bit(RESCTRL_RESERVED_CLOSID, closid_free_map); 175 closid_free_map_len = rdt_min_closid; 176 177 return 0; 178 } 179 180 static void closid_exit(void) 181 { 182 bitmap_free(closid_free_map); 183 closid_free_map = NULL; 184 } 185 186 static int closid_alloc(void) 187 { 188 int cleanest_closid; 189 u32 closid; 190 191 lockdep_assert_held(&rdtgroup_mutex); 192 193 if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID) && 194 resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) { 195 cleanest_closid = resctrl_find_cleanest_closid(); 196 if (cleanest_closid < 0) 197 return cleanest_closid; 198 closid = cleanest_closid; 199 } else { 200 closid = find_first_bit(closid_free_map, closid_free_map_len); 201 if (closid == closid_free_map_len) 202 return -ENOSPC; 203 } 204 __clear_bit(closid, closid_free_map); 205 206 return closid; 207 } 208 209 void closid_free(int closid) 210 { 211 lockdep_assert_held(&rdtgroup_mutex); 212 213 __set_bit(closid, closid_free_map); 214 } 215 216 /** 217 * closid_allocated - test if provided closid is in use 218 * @closid: closid to be tested 219 * 220 * Return: true if @closid is currently associated with a resource group, 221 * false if @closid is free 222 */ 223 bool closid_allocated(unsigned int closid) 224 { 225 lockdep_assert_held(&rdtgroup_mutex); 226 227 return !test_bit(closid, closid_free_map); 228 } 229 230 bool closid_alloc_fixed(u32 closid) 231 { 232 return __test_and_clear_bit(closid, closid_free_map); 233 } 234 235 /** 236 * rdtgroup_mode_by_closid - Return mode of resource group with closid 237 * @closid: closid if the resource group 238 * 239 * Each resource group is associated with a @closid. Here the mode 240 * of a resource group can be queried by searching for it using its closid. 241 * 242 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid 243 */ 244 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid) 245 { 246 struct rdtgroup *rdtgrp; 247 248 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 249 if (rdtgrp->closid == closid) 250 return rdtgrp->mode; 251 } 252 253 return RDT_NUM_MODES; 254 } 255 256 static const char * const rdt_mode_str[] = { 257 [RDT_MODE_SHAREABLE] = "shareable", 258 [RDT_MODE_EXCLUSIVE] = "exclusive", 259 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup", 260 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked", 261 }; 262 263 /** 264 * rdtgroup_mode_str - Return the string representation of mode 265 * @mode: the resource group mode as &enum rdtgroup_mode 266 * 267 * Return: string representation of valid mode, "unknown" otherwise 268 */ 269 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode) 270 { 271 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES) 272 return "unknown"; 273 274 return rdt_mode_str[mode]; 275 } 276 277 /* set uid and gid of rdtgroup dirs and files to that of the creator */ 278 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn) 279 { 280 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, 281 .ia_uid = current_fsuid(), 282 .ia_gid = current_fsgid(), }; 283 284 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && 285 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) 286 return 0; 287 288 return kernfs_setattr(kn, &iattr); 289 } 290 291 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft) 292 { 293 struct kernfs_node *kn; 294 int ret; 295 296 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode, 297 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 298 0, rft->kf_ops, rft, NULL, NULL); 299 if (IS_ERR(kn)) 300 return PTR_ERR(kn); 301 302 ret = rdtgroup_kn_set_ugid(kn); 303 if (ret) { 304 kernfs_remove(kn); 305 return ret; 306 } 307 308 return 0; 309 } 310 311 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg) 312 { 313 struct kernfs_open_file *of = m->private; 314 struct rftype *rft = of->kn->priv; 315 316 if (rft->seq_show) 317 return rft->seq_show(of, m, arg); 318 return 0; 319 } 320 321 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf, 322 size_t nbytes, loff_t off) 323 { 324 struct rftype *rft = of->kn->priv; 325 326 if (rft->write) 327 return rft->write(of, buf, nbytes, off); 328 329 return -EINVAL; 330 } 331 332 static const struct kernfs_ops rdtgroup_kf_single_ops = { 333 .atomic_write_len = PAGE_SIZE, 334 .write = rdtgroup_file_write, 335 .seq_show = rdtgroup_seqfile_show, 336 }; 337 338 static const struct kernfs_ops kf_mondata_ops = { 339 .atomic_write_len = PAGE_SIZE, 340 .seq_show = rdtgroup_mondata_show, 341 }; 342 343 static bool is_cpu_list(struct kernfs_open_file *of) 344 { 345 struct rftype *rft = of->kn->priv; 346 347 return rft->flags & RFTYPE_FLAGS_CPUS_LIST; 348 } 349 350 static int rdtgroup_cpus_show(struct kernfs_open_file *of, 351 struct seq_file *s, void *v) 352 { 353 struct rdtgroup *rdtgrp; 354 struct cpumask *mask; 355 int ret = 0; 356 357 rdtgrp = rdtgroup_kn_lock_live(of->kn); 358 359 if (rdtgrp) { 360 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 361 if (!rdtgrp->plr->d) { 362 rdt_last_cmd_clear(); 363 rdt_last_cmd_puts("Cache domain offline\n"); 364 ret = -ENODEV; 365 } else { 366 mask = &rdtgrp->plr->d->hdr.cpu_mask; 367 seq_printf(s, is_cpu_list(of) ? 368 "%*pbl\n" : "%*pb\n", 369 cpumask_pr_args(mask)); 370 } 371 } else { 372 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n", 373 cpumask_pr_args(&rdtgrp->cpu_mask)); 374 } 375 } else { 376 ret = -ENOENT; 377 } 378 rdtgroup_kn_unlock(of->kn); 379 380 return ret; 381 } 382 383 /* 384 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask, 385 * 386 * Per task closids/rmids must have been set up before calling this function. 387 * @r may be NULL. 388 */ 389 static void 390 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r) 391 { 392 struct resctrl_cpu_defaults defaults, *p = NULL; 393 394 if (r) { 395 defaults.closid = r->closid; 396 defaults.rmid = r->mon.rmid; 397 p = &defaults; 398 } 399 400 on_each_cpu_mask(cpu_mask, resctrl_arch_sync_cpu_closid_rmid, p, 1); 401 } 402 403 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 404 cpumask_var_t tmpmask) 405 { 406 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp; 407 struct list_head *head; 408 409 /* Check whether cpus belong to parent ctrl group */ 410 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask); 411 if (!cpumask_empty(tmpmask)) { 412 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n"); 413 return -EINVAL; 414 } 415 416 /* Check whether cpus are dropped from this group */ 417 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 418 if (!cpumask_empty(tmpmask)) { 419 /* Give any dropped cpus to parent rdtgroup */ 420 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask); 421 update_closid_rmid(tmpmask, prgrp); 422 } 423 424 /* 425 * If we added cpus, remove them from previous group that owned them 426 * and update per-cpu rmid 427 */ 428 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 429 if (!cpumask_empty(tmpmask)) { 430 head = &prgrp->mon.crdtgrp_list; 431 list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 432 if (crgrp == rdtgrp) 433 continue; 434 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask, 435 tmpmask); 436 } 437 update_closid_rmid(tmpmask, rdtgrp); 438 } 439 440 /* Done pushing/pulling - update this group with new mask */ 441 cpumask_copy(&rdtgrp->cpu_mask, newmask); 442 443 return 0; 444 } 445 446 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m) 447 { 448 struct rdtgroup *crgrp; 449 450 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m); 451 /* update the child mon group masks as well*/ 452 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list) 453 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask); 454 } 455 456 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 457 cpumask_var_t tmpmask, cpumask_var_t tmpmask1) 458 { 459 struct rdtgroup *r, *crgrp; 460 struct list_head *head; 461 462 /* Check whether cpus are dropped from this group */ 463 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 464 if (!cpumask_empty(tmpmask)) { 465 /* Can't drop from default group */ 466 if (rdtgrp == &rdtgroup_default) { 467 rdt_last_cmd_puts("Can't drop CPUs from default group\n"); 468 return -EINVAL; 469 } 470 471 /* Give any dropped cpus to rdtgroup_default */ 472 cpumask_or(&rdtgroup_default.cpu_mask, 473 &rdtgroup_default.cpu_mask, tmpmask); 474 update_closid_rmid(tmpmask, &rdtgroup_default); 475 } 476 477 /* 478 * If we added cpus, remove them from previous group and 479 * the prev group's child groups that owned them 480 * and update per-cpu closid/rmid. 481 */ 482 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 483 if (!cpumask_empty(tmpmask)) { 484 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) { 485 if (r == rdtgrp) 486 continue; 487 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask); 488 if (!cpumask_empty(tmpmask1)) 489 cpumask_rdtgrp_clear(r, tmpmask1); 490 } 491 update_closid_rmid(tmpmask, rdtgrp); 492 } 493 494 /* Done pushing/pulling - update this group with new mask */ 495 cpumask_copy(&rdtgrp->cpu_mask, newmask); 496 497 /* 498 * Clear child mon group masks since there is a new parent mask 499 * now and update the rmid for the cpus the child lost. 500 */ 501 head = &rdtgrp->mon.crdtgrp_list; 502 list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 503 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask); 504 update_closid_rmid(tmpmask, rdtgrp); 505 cpumask_clear(&crgrp->cpu_mask); 506 } 507 508 return 0; 509 } 510 511 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of, 512 char *buf, size_t nbytes, loff_t off) 513 { 514 cpumask_var_t tmpmask, newmask, tmpmask1; 515 struct rdtgroup *rdtgrp; 516 int ret; 517 518 if (!buf) 519 return -EINVAL; 520 521 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 522 return -ENOMEM; 523 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) { 524 free_cpumask_var(tmpmask); 525 return -ENOMEM; 526 } 527 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) { 528 free_cpumask_var(tmpmask); 529 free_cpumask_var(newmask); 530 return -ENOMEM; 531 } 532 533 rdtgrp = rdtgroup_kn_lock_live(of->kn); 534 if (!rdtgrp) { 535 ret = -ENOENT; 536 goto unlock; 537 } 538 539 rdt_last_cmd_clear(); 540 541 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 542 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 543 ret = -EINVAL; 544 rdt_last_cmd_puts("Pseudo-locking in progress\n"); 545 goto unlock; 546 } 547 548 if (is_cpu_list(of)) 549 ret = cpulist_parse(buf, newmask); 550 else 551 ret = cpumask_parse(buf, newmask); 552 553 if (ret) { 554 rdt_last_cmd_puts("Bad CPU list/mask\n"); 555 goto unlock; 556 } 557 558 /* check that user didn't specify any offline cpus */ 559 cpumask_andnot(tmpmask, newmask, cpu_online_mask); 560 if (!cpumask_empty(tmpmask)) { 561 ret = -EINVAL; 562 rdt_last_cmd_puts("Can only assign online CPUs\n"); 563 goto unlock; 564 } 565 566 if (rdtgrp->type == RDTCTRL_GROUP) 567 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1); 568 else if (rdtgrp->type == RDTMON_GROUP) 569 ret = cpus_mon_write(rdtgrp, newmask, tmpmask); 570 else 571 ret = -EINVAL; 572 573 unlock: 574 rdtgroup_kn_unlock(of->kn); 575 free_cpumask_var(tmpmask); 576 free_cpumask_var(newmask); 577 free_cpumask_var(tmpmask1); 578 579 return ret ?: nbytes; 580 } 581 582 /** 583 * rdtgroup_remove - the helper to remove resource group safely 584 * @rdtgrp: resource group to remove 585 * 586 * On resource group creation via a mkdir, an extra kernfs_node reference is 587 * taken to ensure that the rdtgroup structure remains accessible for the 588 * rdtgroup_kn_unlock() calls where it is removed. 589 * 590 * Drop the extra reference here, then free the rdtgroup structure. 591 * 592 * Return: void 593 */ 594 static void rdtgroup_remove(struct rdtgroup *rdtgrp) 595 { 596 kernfs_put(rdtgrp->kn); 597 kfree(rdtgrp); 598 } 599 600 static void _update_task_closid_rmid(void *task) 601 { 602 /* 603 * If the task is still current on this CPU, update PQR_ASSOC MSR. 604 * Otherwise, the MSR is updated when the task is scheduled in. 605 */ 606 if (task == current) 607 resctrl_arch_sched_in(task); 608 } 609 610 static void update_task_closid_rmid(struct task_struct *t) 611 { 612 if (IS_ENABLED(CONFIG_SMP) && task_curr(t)) 613 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1); 614 else 615 _update_task_closid_rmid(t); 616 } 617 618 static bool task_in_rdtgroup(struct task_struct *tsk, struct rdtgroup *rdtgrp) 619 { 620 u32 closid, rmid = rdtgrp->mon.rmid; 621 622 if (rdtgrp->type == RDTCTRL_GROUP) 623 closid = rdtgrp->closid; 624 else if (rdtgrp->type == RDTMON_GROUP) 625 closid = rdtgrp->mon.parent->closid; 626 else 627 return false; 628 629 return resctrl_arch_match_closid(tsk, closid) && 630 resctrl_arch_match_rmid(tsk, closid, rmid); 631 } 632 633 static int __rdtgroup_move_task(struct task_struct *tsk, 634 struct rdtgroup *rdtgrp) 635 { 636 /* If the task is already in rdtgrp, no need to move the task. */ 637 if (task_in_rdtgroup(tsk, rdtgrp)) 638 return 0; 639 640 /* 641 * Set the task's closid/rmid before the PQR_ASSOC MSR can be 642 * updated by them. 643 * 644 * For ctrl_mon groups, move both closid and rmid. 645 * For monitor groups, can move the tasks only from 646 * their parent CTRL group. 647 */ 648 if (rdtgrp->type == RDTMON_GROUP && 649 !resctrl_arch_match_closid(tsk, rdtgrp->mon.parent->closid)) { 650 rdt_last_cmd_puts("Can't move task to different control group\n"); 651 return -EINVAL; 652 } 653 654 if (rdtgrp->type == RDTMON_GROUP) 655 resctrl_arch_set_closid_rmid(tsk, rdtgrp->mon.parent->closid, 656 rdtgrp->mon.rmid); 657 else 658 resctrl_arch_set_closid_rmid(tsk, rdtgrp->closid, 659 rdtgrp->mon.rmid); 660 661 /* 662 * Ensure the task's closid and rmid are written before determining if 663 * the task is current that will decide if it will be interrupted. 664 * This pairs with the full barrier between the rq->curr update and 665 * resctrl_arch_sched_in() during context switch. 666 */ 667 smp_mb(); 668 669 /* 670 * By now, the task's closid and rmid are set. If the task is current 671 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource 672 * group go into effect. If the task is not current, the MSR will be 673 * updated when the task is scheduled in. 674 */ 675 update_task_closid_rmid(tsk); 676 677 return 0; 678 } 679 680 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r) 681 { 682 return (resctrl_arch_alloc_capable() && (r->type == RDTCTRL_GROUP) && 683 resctrl_arch_match_closid(t, r->closid)); 684 } 685 686 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r) 687 { 688 return (resctrl_arch_mon_capable() && (r->type == RDTMON_GROUP) && 689 resctrl_arch_match_rmid(t, r->mon.parent->closid, 690 r->mon.rmid)); 691 } 692 693 /** 694 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group 695 * @r: Resource group 696 * 697 * Return: 1 if tasks have been assigned to @r, 0 otherwise 698 */ 699 int rdtgroup_tasks_assigned(struct rdtgroup *r) 700 { 701 struct task_struct *p, *t; 702 int ret = 0; 703 704 lockdep_assert_held(&rdtgroup_mutex); 705 706 rcu_read_lock(); 707 for_each_process_thread(p, t) { 708 if (is_closid_match(t, r) || is_rmid_match(t, r)) { 709 ret = 1; 710 break; 711 } 712 } 713 rcu_read_unlock(); 714 715 return ret; 716 } 717 718 static int rdtgroup_task_write_permission(struct task_struct *task, 719 struct kernfs_open_file *of) 720 { 721 const struct cred *tcred = get_task_cred(task); 722 const struct cred *cred = current_cred(); 723 int ret = 0; 724 725 /* 726 * Even if we're attaching all tasks in the thread group, we only 727 * need to check permissions on one of them. 728 */ 729 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && 730 !uid_eq(cred->euid, tcred->uid) && 731 !uid_eq(cred->euid, tcred->suid)) { 732 rdt_last_cmd_printf("No permission to move task %d\n", task->pid); 733 ret = -EPERM; 734 } 735 736 put_cred(tcred); 737 return ret; 738 } 739 740 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp, 741 struct kernfs_open_file *of) 742 { 743 struct task_struct *tsk; 744 int ret; 745 746 rcu_read_lock(); 747 if (pid) { 748 tsk = find_task_by_vpid(pid); 749 if (!tsk) { 750 rcu_read_unlock(); 751 rdt_last_cmd_printf("No task %d\n", pid); 752 return -ESRCH; 753 } 754 } else { 755 tsk = current; 756 } 757 758 get_task_struct(tsk); 759 rcu_read_unlock(); 760 761 ret = rdtgroup_task_write_permission(tsk, of); 762 if (!ret) 763 ret = __rdtgroup_move_task(tsk, rdtgrp); 764 765 put_task_struct(tsk); 766 return ret; 767 } 768 769 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of, 770 char *buf, size_t nbytes, loff_t off) 771 { 772 struct rdtgroup *rdtgrp; 773 char *pid_str; 774 int ret = 0; 775 pid_t pid; 776 777 rdtgrp = rdtgroup_kn_lock_live(of->kn); 778 if (!rdtgrp) { 779 rdtgroup_kn_unlock(of->kn); 780 return -ENOENT; 781 } 782 rdt_last_cmd_clear(); 783 784 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 785 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 786 ret = -EINVAL; 787 rdt_last_cmd_puts("Pseudo-locking in progress\n"); 788 goto unlock; 789 } 790 791 while (buf && buf[0] != '\0' && buf[0] != '\n') { 792 pid_str = strim(strsep(&buf, ",")); 793 794 if (kstrtoint(pid_str, 0, &pid)) { 795 rdt_last_cmd_printf("Task list parsing error pid %s\n", pid_str); 796 ret = -EINVAL; 797 break; 798 } 799 800 if (pid < 0) { 801 rdt_last_cmd_printf("Invalid pid %d\n", pid); 802 ret = -EINVAL; 803 break; 804 } 805 806 ret = rdtgroup_move_task(pid, rdtgrp, of); 807 if (ret) { 808 rdt_last_cmd_printf("Error while processing task %d\n", pid); 809 break; 810 } 811 } 812 813 unlock: 814 rdtgroup_kn_unlock(of->kn); 815 816 return ret ?: nbytes; 817 } 818 819 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s) 820 { 821 struct task_struct *p, *t; 822 pid_t pid; 823 824 rcu_read_lock(); 825 for_each_process_thread(p, t) { 826 if (is_closid_match(t, r) || is_rmid_match(t, r)) { 827 pid = task_pid_vnr(t); 828 if (pid) 829 seq_printf(s, "%d\n", pid); 830 } 831 } 832 rcu_read_unlock(); 833 } 834 835 static int rdtgroup_tasks_show(struct kernfs_open_file *of, 836 struct seq_file *s, void *v) 837 { 838 struct rdtgroup *rdtgrp; 839 int ret = 0; 840 841 rdtgrp = rdtgroup_kn_lock_live(of->kn); 842 if (rdtgrp) 843 show_rdt_tasks(rdtgrp, s); 844 else 845 ret = -ENOENT; 846 rdtgroup_kn_unlock(of->kn); 847 848 return ret; 849 } 850 851 static int rdtgroup_closid_show(struct kernfs_open_file *of, 852 struct seq_file *s, void *v) 853 { 854 struct rdtgroup *rdtgrp; 855 int ret = 0; 856 857 rdtgrp = rdtgroup_kn_lock_live(of->kn); 858 if (rdtgrp) 859 seq_printf(s, "%u\n", rdtgrp->closid); 860 else 861 ret = -ENOENT; 862 rdtgroup_kn_unlock(of->kn); 863 864 return ret; 865 } 866 867 static int rdtgroup_rmid_show(struct kernfs_open_file *of, 868 struct seq_file *s, void *v) 869 { 870 struct rdtgroup *rdtgrp; 871 int ret = 0; 872 873 rdtgrp = rdtgroup_kn_lock_live(of->kn); 874 if (rdtgrp) 875 seq_printf(s, "%u\n", rdtgrp->mon.rmid); 876 else 877 ret = -ENOENT; 878 rdtgroup_kn_unlock(of->kn); 879 880 return ret; 881 } 882 883 #ifdef CONFIG_PROC_CPU_RESCTRL 884 /* 885 * A task can only be part of one resctrl control group and of one monitor 886 * group which is associated to that control group. 887 * 888 * 1) res: 889 * mon: 890 * 891 * resctrl is not available. 892 * 893 * 2) res:/ 894 * mon: 895 * 896 * Task is part of the root resctrl control group, and it is not associated 897 * to any monitor group. 898 * 899 * 3) res:/ 900 * mon:mon0 901 * 902 * Task is part of the root resctrl control group and monitor group mon0. 903 * 904 * 4) res:group0 905 * mon: 906 * 907 * Task is part of resctrl control group group0, and it is not associated 908 * to any monitor group. 909 * 910 * 5) res:group0 911 * mon:mon1 912 * 913 * Task is part of resctrl control group group0 and monitor group mon1. 914 */ 915 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns, 916 struct pid *pid, struct task_struct *tsk) 917 { 918 struct rdtgroup *rdtg; 919 int ret = 0; 920 921 mutex_lock(&rdtgroup_mutex); 922 923 /* Return empty if resctrl has not been mounted. */ 924 if (!resctrl_mounted) { 925 seq_puts(s, "res:\nmon:\n"); 926 goto unlock; 927 } 928 929 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) { 930 struct rdtgroup *crg; 931 932 /* 933 * Task information is only relevant for shareable 934 * and exclusive groups. 935 */ 936 if (rdtg->mode != RDT_MODE_SHAREABLE && 937 rdtg->mode != RDT_MODE_EXCLUSIVE) 938 continue; 939 940 if (!resctrl_arch_match_closid(tsk, rdtg->closid)) 941 continue; 942 943 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "", 944 rdt_kn_name(rdtg->kn)); 945 seq_puts(s, "mon:"); 946 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list, 947 mon.crdtgrp_list) { 948 if (!resctrl_arch_match_rmid(tsk, crg->mon.parent->closid, 949 crg->mon.rmid)) 950 continue; 951 seq_printf(s, "%s", rdt_kn_name(crg->kn)); 952 break; 953 } 954 seq_putc(s, '\n'); 955 goto unlock; 956 } 957 /* 958 * The above search should succeed. Otherwise return 959 * with an error. 960 */ 961 ret = -ENOENT; 962 unlock: 963 mutex_unlock(&rdtgroup_mutex); 964 965 return ret; 966 } 967 #endif 968 969 static int rdt_last_cmd_status_show(struct kernfs_open_file *of, 970 struct seq_file *seq, void *v) 971 { 972 int len; 973 974 mutex_lock(&rdtgroup_mutex); 975 len = seq_buf_used(&last_cmd_status); 976 if (len) 977 seq_printf(seq, "%.*s", len, last_cmd_status_buf); 978 else 979 seq_puts(seq, "ok\n"); 980 mutex_unlock(&rdtgroup_mutex); 981 return 0; 982 } 983 984 void *rdt_kn_parent_priv(struct kernfs_node *kn) 985 { 986 /* 987 * The parent pointer is only valid within RCU section since it can be 988 * replaced. 989 */ 990 guard(rcu)(); 991 return rcu_dereference(kn->__parent)->priv; 992 } 993 994 static int rdt_num_closids_show(struct kernfs_open_file *of, 995 struct seq_file *seq, void *v) 996 { 997 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 998 999 seq_printf(seq, "%u\n", s->num_closid); 1000 return 0; 1001 } 1002 1003 static int rdt_default_ctrl_show(struct kernfs_open_file *of, 1004 struct seq_file *seq, void *v) 1005 { 1006 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1007 struct rdt_resource *r = s->res; 1008 1009 seq_printf(seq, "%x\n", resctrl_get_default_ctrl(r)); 1010 return 0; 1011 } 1012 1013 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, 1014 struct seq_file *seq, void *v) 1015 { 1016 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1017 struct rdt_resource *r = s->res; 1018 1019 seq_printf(seq, "%u\n", r->cache.min_cbm_bits); 1020 return 0; 1021 } 1022 1023 static int rdt_shareable_bits_show(struct kernfs_open_file *of, 1024 struct seq_file *seq, void *v) 1025 { 1026 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1027 struct rdt_resource *r = s->res; 1028 1029 seq_printf(seq, "%x\n", r->cache.shareable_bits); 1030 return 0; 1031 } 1032 1033 /* 1034 * rdt_bit_usage_show - Display current usage of resources 1035 * 1036 * A domain is a shared resource that can now be allocated differently. Here 1037 * we display the current regions of the domain as an annotated bitmask. 1038 * For each domain of this resource its allocation bitmask 1039 * is annotated as below to indicate the current usage of the corresponding bit: 1040 * 0 - currently unused 1041 * X - currently available for sharing and used by software and hardware 1042 * H - currently used by hardware only but available for software use 1043 * S - currently used and shareable by software only 1044 * E - currently used exclusively by one resource group 1045 * P - currently pseudo-locked by one resource group 1046 */ 1047 static int rdt_bit_usage_show(struct kernfs_open_file *of, 1048 struct seq_file *seq, void *v) 1049 { 1050 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1051 /* 1052 * Use unsigned long even though only 32 bits are used to ensure 1053 * test_bit() is used safely. 1054 */ 1055 unsigned long sw_shareable = 0, hw_shareable = 0; 1056 unsigned long exclusive = 0, pseudo_locked = 0; 1057 struct rdt_resource *r = s->res; 1058 struct rdt_ctrl_domain *dom; 1059 int i, hwb, swb, excl, psl; 1060 enum rdtgrp_mode mode; 1061 bool sep = false; 1062 u32 ctrl_val; 1063 1064 cpus_read_lock(); 1065 mutex_lock(&rdtgroup_mutex); 1066 list_for_each_entry(dom, &r->ctrl_domains, hdr.list) { 1067 if (sep) 1068 seq_putc(seq, ';'); 1069 hw_shareable = r->cache.shareable_bits; 1070 sw_shareable = 0; 1071 exclusive = 0; 1072 seq_printf(seq, "%d=", dom->hdr.id); 1073 for (i = 0; i < closids_supported(); i++) { 1074 if (!closid_allocated(i) || 1075 (resctrl_arch_get_io_alloc_enabled(r) && 1076 i == resctrl_io_alloc_closid(r))) 1077 continue; 1078 ctrl_val = resctrl_arch_get_config(r, dom, i, 1079 s->conf_type); 1080 mode = rdtgroup_mode_by_closid(i); 1081 switch (mode) { 1082 case RDT_MODE_SHAREABLE: 1083 sw_shareable |= ctrl_val; 1084 break; 1085 case RDT_MODE_EXCLUSIVE: 1086 exclusive |= ctrl_val; 1087 break; 1088 case RDT_MODE_PSEUDO_LOCKSETUP: 1089 /* 1090 * RDT_MODE_PSEUDO_LOCKSETUP is possible 1091 * here but not included since the CBM 1092 * associated with this CLOSID in this mode 1093 * is not initialized and no task or cpu can be 1094 * assigned this CLOSID. 1095 */ 1096 break; 1097 case RDT_MODE_PSEUDO_LOCKED: 1098 case RDT_NUM_MODES: 1099 WARN(1, 1100 "invalid mode for closid %d\n", i); 1101 break; 1102 } 1103 } 1104 1105 /* 1106 * When the "io_alloc" feature is enabled, a portion of the cache 1107 * is configured for shared use between hardware and software. 1108 * Also, when CDP is enabled the CBMs of CDP_CODE and CDP_DATA 1109 * resources are kept in sync. So, the CBMs for "io_alloc" can 1110 * be accessed through either resource. 1111 */ 1112 if (resctrl_arch_get_io_alloc_enabled(r)) { 1113 ctrl_val = resctrl_arch_get_config(r, dom, 1114 resctrl_io_alloc_closid(r), 1115 s->conf_type); 1116 hw_shareable |= ctrl_val; 1117 } 1118 1119 for (i = r->cache.cbm_len - 1; i >= 0; i--) { 1120 pseudo_locked = dom->plr ? dom->plr->cbm : 0; 1121 hwb = test_bit(i, &hw_shareable); 1122 swb = test_bit(i, &sw_shareable); 1123 excl = test_bit(i, &exclusive); 1124 psl = test_bit(i, &pseudo_locked); 1125 if (hwb && swb) 1126 seq_putc(seq, 'X'); 1127 else if (hwb && !swb) 1128 seq_putc(seq, 'H'); 1129 else if (!hwb && swb) 1130 seq_putc(seq, 'S'); 1131 else if (excl) 1132 seq_putc(seq, 'E'); 1133 else if (psl) 1134 seq_putc(seq, 'P'); 1135 else /* Unused bits remain */ 1136 seq_putc(seq, '0'); 1137 } 1138 sep = true; 1139 } 1140 seq_putc(seq, '\n'); 1141 mutex_unlock(&rdtgroup_mutex); 1142 cpus_read_unlock(); 1143 return 0; 1144 } 1145 1146 static int rdt_min_bw_show(struct kernfs_open_file *of, 1147 struct seq_file *seq, void *v) 1148 { 1149 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1150 struct rdt_resource *r = s->res; 1151 1152 seq_printf(seq, "%u\n", r->membw.min_bw); 1153 return 0; 1154 } 1155 1156 static int rdt_num_rmids_show(struct kernfs_open_file *of, 1157 struct seq_file *seq, void *v) 1158 { 1159 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1160 1161 seq_printf(seq, "%u\n", r->mon.num_rmid); 1162 1163 return 0; 1164 } 1165 1166 static int rdt_mon_features_show(struct kernfs_open_file *of, 1167 struct seq_file *seq, void *v) 1168 { 1169 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1170 struct mon_evt *mevt; 1171 1172 for_each_mon_event(mevt) { 1173 if (mevt->rid != r->rid || !mevt->enabled) 1174 continue; 1175 seq_printf(seq, "%s\n", mevt->name); 1176 if (mevt->configurable && 1177 !resctrl_arch_mbm_cntr_assign_enabled(r)) 1178 seq_printf(seq, "%s_config\n", mevt->name); 1179 } 1180 1181 return 0; 1182 } 1183 1184 static int rdt_bw_gran_show(struct kernfs_open_file *of, 1185 struct seq_file *seq, void *v) 1186 { 1187 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1188 struct rdt_resource *r = s->res; 1189 1190 seq_printf(seq, "%u\n", r->membw.bw_gran); 1191 return 0; 1192 } 1193 1194 static int rdt_delay_linear_show(struct kernfs_open_file *of, 1195 struct seq_file *seq, void *v) 1196 { 1197 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1198 struct rdt_resource *r = s->res; 1199 1200 seq_printf(seq, "%u\n", r->membw.delay_linear); 1201 return 0; 1202 } 1203 1204 static int max_threshold_occ_show(struct kernfs_open_file *of, 1205 struct seq_file *seq, void *v) 1206 { 1207 seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold); 1208 1209 return 0; 1210 } 1211 1212 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of, 1213 struct seq_file *seq, void *v) 1214 { 1215 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1216 struct rdt_resource *r = s->res; 1217 1218 switch (r->membw.throttle_mode) { 1219 case THREAD_THROTTLE_PER_THREAD: 1220 seq_puts(seq, "per-thread\n"); 1221 return 0; 1222 case THREAD_THROTTLE_MAX: 1223 seq_puts(seq, "max\n"); 1224 return 0; 1225 case THREAD_THROTTLE_UNDEFINED: 1226 seq_puts(seq, "undefined\n"); 1227 return 0; 1228 } 1229 1230 WARN_ON_ONCE(1); 1231 1232 return 0; 1233 } 1234 1235 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, 1236 char *buf, size_t nbytes, loff_t off) 1237 { 1238 unsigned int bytes; 1239 int ret; 1240 1241 ret = kstrtouint(buf, 0, &bytes); 1242 if (ret) 1243 return ret; 1244 1245 if (bytes > resctrl_rmid_realloc_limit) 1246 return -EINVAL; 1247 1248 resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes); 1249 1250 return nbytes; 1251 } 1252 1253 /* 1254 * rdtgroup_mode_show - Display mode of this resource group 1255 */ 1256 static int rdtgroup_mode_show(struct kernfs_open_file *of, 1257 struct seq_file *s, void *v) 1258 { 1259 struct rdtgroup *rdtgrp; 1260 1261 rdtgrp = rdtgroup_kn_lock_live(of->kn); 1262 if (!rdtgrp) { 1263 rdtgroup_kn_unlock(of->kn); 1264 return -ENOENT; 1265 } 1266 1267 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode)); 1268 1269 rdtgroup_kn_unlock(of->kn); 1270 return 0; 1271 } 1272 1273 enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type) 1274 { 1275 switch (my_type) { 1276 case CDP_CODE: 1277 return CDP_DATA; 1278 case CDP_DATA: 1279 return CDP_CODE; 1280 default: 1281 case CDP_NONE: 1282 return CDP_NONE; 1283 } 1284 } 1285 1286 static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of, 1287 struct seq_file *seq, void *v) 1288 { 1289 struct resctrl_schema *s = rdt_kn_parent_priv(of->kn); 1290 struct rdt_resource *r = s->res; 1291 1292 seq_printf(seq, "%u\n", r->cache.arch_has_sparse_bitmasks); 1293 1294 return 0; 1295 } 1296 1297 /** 1298 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other 1299 * @r: Resource to which domain instance @d belongs. 1300 * @d: The domain instance for which @closid is being tested. 1301 * @cbm: Capacity bitmask being tested. 1302 * @closid: Intended closid for @cbm. 1303 * @type: CDP type of @r. 1304 * @exclusive: Only check if overlaps with exclusive resource groups 1305 * 1306 * Checks if provided @cbm intended to be used for @closid on domain 1307 * @d overlaps with any other closids or other hardware usage associated 1308 * with this domain. If @exclusive is true then only overlaps with 1309 * resource groups in exclusive mode will be considered. If @exclusive 1310 * is false then overlaps with any resource group or hardware entities 1311 * will be considered. 1312 * 1313 * @cbm is unsigned long, even if only 32 bits are used, to make the 1314 * bitmap functions work correctly. 1315 * 1316 * Return: false if CBM does not overlap, true if it does. 1317 */ 1318 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_ctrl_domain *d, 1319 unsigned long cbm, int closid, 1320 enum resctrl_conf_type type, bool exclusive) 1321 { 1322 enum rdtgrp_mode mode; 1323 unsigned long ctrl_b; 1324 int i; 1325 1326 /* Check for any overlap with regions used by hardware directly */ 1327 if (!exclusive) { 1328 ctrl_b = r->cache.shareable_bits; 1329 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) 1330 return true; 1331 } 1332 1333 /* Check for overlap with other resource groups */ 1334 for (i = 0; i < closids_supported(); i++) { 1335 ctrl_b = resctrl_arch_get_config(r, d, i, type); 1336 mode = rdtgroup_mode_by_closid(i); 1337 if (closid_allocated(i) && i != closid && 1338 mode != RDT_MODE_PSEUDO_LOCKSETUP) { 1339 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) { 1340 if (exclusive) { 1341 if (mode == RDT_MODE_EXCLUSIVE) 1342 return true; 1343 continue; 1344 } 1345 return true; 1346 } 1347 } 1348 } 1349 1350 return false; 1351 } 1352 1353 /** 1354 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware 1355 * @s: Schema for the resource to which domain instance @d belongs. 1356 * @d: The domain instance for which @closid is being tested. 1357 * @cbm: Capacity bitmask being tested. 1358 * @closid: Intended closid for @cbm. 1359 * @exclusive: Only check if overlaps with exclusive resource groups 1360 * 1361 * Resources that can be allocated using a CBM can use the CBM to control 1362 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test 1363 * for overlap. Overlap test is not limited to the specific resource for 1364 * which the CBM is intended though - when dealing with CDP resources that 1365 * share the underlying hardware the overlap check should be performed on 1366 * the CDP resource sharing the hardware also. 1367 * 1368 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the 1369 * overlap test. 1370 * 1371 * Return: true if CBM overlap detected, false if there is no overlap 1372 */ 1373 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_ctrl_domain *d, 1374 unsigned long cbm, int closid, bool exclusive) 1375 { 1376 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type); 1377 struct rdt_resource *r = s->res; 1378 1379 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type, 1380 exclusive)) 1381 return true; 1382 1383 if (!resctrl_arch_get_cdp_enabled(r->rid)) 1384 return false; 1385 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive); 1386 } 1387 1388 /** 1389 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive 1390 * @rdtgrp: Resource group identified through its closid. 1391 * 1392 * An exclusive resource group implies that there should be no sharing of 1393 * its allocated resources. At the time this group is considered to be 1394 * exclusive this test can determine if its current schemata supports this 1395 * setting by testing for overlap with all other resource groups. 1396 * 1397 * Return: true if resource group can be exclusive, false if there is overlap 1398 * with allocations of other resource groups and thus this resource group 1399 * cannot be exclusive. 1400 */ 1401 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp) 1402 { 1403 int closid = rdtgrp->closid; 1404 struct rdt_ctrl_domain *d; 1405 struct resctrl_schema *s; 1406 struct rdt_resource *r; 1407 bool has_cache = false; 1408 u32 ctrl; 1409 1410 /* Walking r->domains, ensure it can't race with cpuhp */ 1411 lockdep_assert_cpus_held(); 1412 1413 list_for_each_entry(s, &resctrl_schema_all, list) { 1414 r = s->res; 1415 if (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA) 1416 continue; 1417 has_cache = true; 1418 list_for_each_entry(d, &r->ctrl_domains, hdr.list) { 1419 ctrl = resctrl_arch_get_config(r, d, closid, 1420 s->conf_type); 1421 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) { 1422 rdt_last_cmd_puts("Schemata overlaps\n"); 1423 return false; 1424 } 1425 } 1426 } 1427 1428 if (!has_cache) { 1429 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n"); 1430 return false; 1431 } 1432 1433 return true; 1434 } 1435 1436 /* 1437 * rdtgroup_mode_write - Modify the resource group's mode 1438 */ 1439 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, 1440 char *buf, size_t nbytes, loff_t off) 1441 { 1442 struct rdtgroup *rdtgrp; 1443 enum rdtgrp_mode mode; 1444 int ret = 0; 1445 1446 /* Valid input requires a trailing newline */ 1447 if (nbytes == 0 || buf[nbytes - 1] != '\n') 1448 return -EINVAL; 1449 buf[nbytes - 1] = '\0'; 1450 1451 rdtgrp = rdtgroup_kn_lock_live(of->kn); 1452 if (!rdtgrp) { 1453 rdtgroup_kn_unlock(of->kn); 1454 return -ENOENT; 1455 } 1456 1457 rdt_last_cmd_clear(); 1458 1459 mode = rdtgrp->mode; 1460 1461 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || 1462 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) || 1463 (!strcmp(buf, "pseudo-locksetup") && 1464 mode == RDT_MODE_PSEUDO_LOCKSETUP) || 1465 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED)) 1466 goto out; 1467 1468 if (mode == RDT_MODE_PSEUDO_LOCKED) { 1469 rdt_last_cmd_puts("Cannot change pseudo-locked group\n"); 1470 ret = -EINVAL; 1471 goto out; 1472 } 1473 1474 if (!strcmp(buf, "shareable")) { 1475 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1476 ret = rdtgroup_locksetup_exit(rdtgrp); 1477 if (ret) 1478 goto out; 1479 } 1480 rdtgrp->mode = RDT_MODE_SHAREABLE; 1481 } else if (!strcmp(buf, "exclusive")) { 1482 if (!rdtgroup_mode_test_exclusive(rdtgrp)) { 1483 ret = -EINVAL; 1484 goto out; 1485 } 1486 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1487 ret = rdtgroup_locksetup_exit(rdtgrp); 1488 if (ret) 1489 goto out; 1490 } 1491 rdtgrp->mode = RDT_MODE_EXCLUSIVE; 1492 } else if (IS_ENABLED(CONFIG_RESCTRL_FS_PSEUDO_LOCK) && 1493 !strcmp(buf, "pseudo-locksetup")) { 1494 ret = rdtgroup_locksetup_enter(rdtgrp); 1495 if (ret) 1496 goto out; 1497 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP; 1498 } else { 1499 rdt_last_cmd_puts("Unknown or unsupported mode\n"); 1500 ret = -EINVAL; 1501 } 1502 1503 out: 1504 rdtgroup_kn_unlock(of->kn); 1505 return ret ?: nbytes; 1506 } 1507 1508 /** 1509 * rdtgroup_cbm_to_size - Translate CBM to size in bytes 1510 * @r: RDT resource to which @d belongs. 1511 * @d: RDT domain instance. 1512 * @cbm: bitmask for which the size should be computed. 1513 * 1514 * The bitmask provided associated with the RDT domain instance @d will be 1515 * translated into how many bytes it represents. The size in bytes is 1516 * computed by first dividing the total cache size by the CBM length to 1517 * determine how many bytes each bit in the bitmask represents. The result 1518 * is multiplied with the number of bits set in the bitmask. 1519 * 1520 * @cbm is unsigned long, even if only 32 bits are used to make the 1521 * bitmap functions work correctly. 1522 */ 1523 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, 1524 struct rdt_ctrl_domain *d, unsigned long cbm) 1525 { 1526 unsigned int size = 0; 1527 struct cacheinfo *ci; 1528 int num_b; 1529 1530 if (WARN_ON_ONCE(r->ctrl_scope != RESCTRL_L2_CACHE && r->ctrl_scope != RESCTRL_L3_CACHE)) 1531 return size; 1532 1533 num_b = bitmap_weight(&cbm, r->cache.cbm_len); 1534 ci = get_cpu_cacheinfo_level(cpumask_any(&d->hdr.cpu_mask), r->ctrl_scope); 1535 if (ci) 1536 size = ci->size / r->cache.cbm_len * num_b; 1537 1538 return size; 1539 } 1540 1541 bool is_mba_sc(struct rdt_resource *r) 1542 { 1543 if (!r) 1544 r = resctrl_arch_get_resource(RDT_RESOURCE_MBA); 1545 1546 /* 1547 * The software controller support is only applicable to MBA resource. 1548 * Make sure to check for resource type. 1549 */ 1550 if (r->rid != RDT_RESOURCE_MBA) 1551 return false; 1552 1553 return r->membw.mba_sc; 1554 } 1555 1556 /* 1557 * rdtgroup_size_show - Display size in bytes of allocated regions 1558 * 1559 * The "size" file mirrors the layout of the "schemata" file, printing the 1560 * size in bytes of each region instead of the capacity bitmask. 1561 */ 1562 static int rdtgroup_size_show(struct kernfs_open_file *of, 1563 struct seq_file *s, void *v) 1564 { 1565 struct resctrl_schema *schema; 1566 enum resctrl_conf_type type; 1567 struct rdt_ctrl_domain *d; 1568 struct rdtgroup *rdtgrp; 1569 struct rdt_resource *r; 1570 unsigned int size; 1571 int ret = 0; 1572 u32 closid; 1573 bool sep; 1574 u32 ctrl; 1575 1576 rdtgrp = rdtgroup_kn_lock_live(of->kn); 1577 if (!rdtgrp) { 1578 rdtgroup_kn_unlock(of->kn); 1579 return -ENOENT; 1580 } 1581 1582 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 1583 if (!rdtgrp->plr->d) { 1584 rdt_last_cmd_clear(); 1585 rdt_last_cmd_puts("Cache domain offline\n"); 1586 ret = -ENODEV; 1587 } else { 1588 seq_printf(s, "%*s:", max_name_width, 1589 rdtgrp->plr->s->name); 1590 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res, 1591 rdtgrp->plr->d, 1592 rdtgrp->plr->cbm); 1593 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->hdr.id, size); 1594 } 1595 goto out; 1596 } 1597 1598 closid = rdtgrp->closid; 1599 1600 list_for_each_entry(schema, &resctrl_schema_all, list) { 1601 r = schema->res; 1602 type = schema->conf_type; 1603 sep = false; 1604 seq_printf(s, "%*s:", max_name_width, schema->name); 1605 list_for_each_entry(d, &r->ctrl_domains, hdr.list) { 1606 if (sep) 1607 seq_putc(s, ';'); 1608 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1609 size = 0; 1610 } else { 1611 if (is_mba_sc(r)) 1612 ctrl = d->mbps_val[closid]; 1613 else 1614 ctrl = resctrl_arch_get_config(r, d, 1615 closid, 1616 type); 1617 if (r->rid == RDT_RESOURCE_MBA || 1618 r->rid == RDT_RESOURCE_SMBA) 1619 size = ctrl; 1620 else 1621 size = rdtgroup_cbm_to_size(r, d, ctrl); 1622 } 1623 seq_printf(s, "%d=%u", d->hdr.id, size); 1624 sep = true; 1625 } 1626 seq_putc(s, '\n'); 1627 } 1628 1629 out: 1630 rdtgroup_kn_unlock(of->kn); 1631 1632 return ret; 1633 } 1634 1635 static void mondata_config_read(struct resctrl_mon_config_info *mon_info) 1636 { 1637 smp_call_function_any(&mon_info->d->hdr.cpu_mask, 1638 resctrl_arch_mon_event_config_read, mon_info, 1); 1639 } 1640 1641 static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid) 1642 { 1643 struct resctrl_mon_config_info mon_info; 1644 struct rdt_l3_mon_domain *dom; 1645 bool sep = false; 1646 1647 cpus_read_lock(); 1648 mutex_lock(&rdtgroup_mutex); 1649 1650 list_for_each_entry(dom, &r->mon_domains, hdr.list) { 1651 if (sep) 1652 seq_puts(s, ";"); 1653 1654 memset(&mon_info, 0, sizeof(struct resctrl_mon_config_info)); 1655 mon_info.r = r; 1656 mon_info.d = dom; 1657 mon_info.evtid = evtid; 1658 mondata_config_read(&mon_info); 1659 1660 seq_printf(s, "%d=0x%02x", dom->hdr.id, mon_info.mon_config); 1661 sep = true; 1662 } 1663 seq_puts(s, "\n"); 1664 1665 mutex_unlock(&rdtgroup_mutex); 1666 cpus_read_unlock(); 1667 1668 return 0; 1669 } 1670 1671 static int mbm_total_bytes_config_show(struct kernfs_open_file *of, 1672 struct seq_file *seq, void *v) 1673 { 1674 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1675 1676 mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID); 1677 1678 return 0; 1679 } 1680 1681 static int mbm_local_bytes_config_show(struct kernfs_open_file *of, 1682 struct seq_file *seq, void *v) 1683 { 1684 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1685 1686 mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID); 1687 1688 return 0; 1689 } 1690 1691 static void mbm_config_write_domain(struct rdt_resource *r, 1692 struct rdt_l3_mon_domain *d, u32 evtid, u32 val) 1693 { 1694 struct resctrl_mon_config_info mon_info = {0}; 1695 1696 /* 1697 * Read the current config value first. If both are the same then 1698 * no need to write it again. 1699 */ 1700 mon_info.r = r; 1701 mon_info.d = d; 1702 mon_info.evtid = evtid; 1703 mondata_config_read(&mon_info); 1704 if (mon_info.mon_config == val) 1705 return; 1706 1707 mon_info.mon_config = val; 1708 1709 /* 1710 * Update MSR_IA32_EVT_CFG_BASE MSR on one of the CPUs in the 1711 * domain. The MSRs offset from MSR MSR_IA32_EVT_CFG_BASE 1712 * are scoped at the domain level. Writing any of these MSRs 1713 * on one CPU is observed by all the CPUs in the domain. 1714 */ 1715 smp_call_function_any(&d->hdr.cpu_mask, resctrl_arch_mon_event_config_write, 1716 &mon_info, 1); 1717 1718 /* 1719 * When an Event Configuration is changed, the bandwidth counters 1720 * for all RMIDs and Events will be cleared by the hardware. The 1721 * hardware also sets MSR_IA32_QM_CTR.Unavailable (bit 62) for 1722 * every RMID on the next read to any event for every RMID. 1723 * Subsequent reads will have MSR_IA32_QM_CTR.Unavailable (bit 62) 1724 * cleared while it is tracked by the hardware. Clear the 1725 * mbm_local and mbm_total counts for all the RMIDs. 1726 */ 1727 resctrl_arch_reset_rmid_all(r, d); 1728 } 1729 1730 static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid) 1731 { 1732 char *dom_str = NULL, *id_str; 1733 struct rdt_l3_mon_domain *d; 1734 unsigned long dom_id, val; 1735 1736 /* Walking r->domains, ensure it can't race with cpuhp */ 1737 lockdep_assert_cpus_held(); 1738 1739 next: 1740 if (!tok || tok[0] == '\0') 1741 return 0; 1742 1743 /* Start processing the strings for each domain */ 1744 dom_str = strim(strsep(&tok, ";")); 1745 id_str = strsep(&dom_str, "="); 1746 1747 if (!id_str || kstrtoul(id_str, 10, &dom_id)) { 1748 rdt_last_cmd_puts("Missing '=' or non-numeric domain id\n"); 1749 return -EINVAL; 1750 } 1751 1752 if (!dom_str || kstrtoul(dom_str, 16, &val)) { 1753 rdt_last_cmd_puts("Non-numeric event configuration value\n"); 1754 return -EINVAL; 1755 } 1756 1757 /* Value from user cannot be more than the supported set of events */ 1758 if ((val & r->mon.mbm_cfg_mask) != val) { 1759 rdt_last_cmd_printf("Invalid event configuration: max valid mask is 0x%02x\n", 1760 r->mon.mbm_cfg_mask); 1761 return -EINVAL; 1762 } 1763 1764 list_for_each_entry(d, &r->mon_domains, hdr.list) { 1765 if (d->hdr.id == dom_id) { 1766 mbm_config_write_domain(r, d, evtid, val); 1767 goto next; 1768 } 1769 } 1770 1771 return -EINVAL; 1772 } 1773 1774 static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of, 1775 char *buf, size_t nbytes, 1776 loff_t off) 1777 { 1778 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1779 int ret; 1780 1781 /* Valid input requires a trailing newline */ 1782 if (nbytes == 0 || buf[nbytes - 1] != '\n') 1783 return -EINVAL; 1784 1785 cpus_read_lock(); 1786 mutex_lock(&rdtgroup_mutex); 1787 1788 rdt_last_cmd_clear(); 1789 1790 buf[nbytes - 1] = '\0'; 1791 1792 ret = mon_config_write(r, buf, QOS_L3_MBM_TOTAL_EVENT_ID); 1793 1794 mutex_unlock(&rdtgroup_mutex); 1795 cpus_read_unlock(); 1796 1797 return ret ?: nbytes; 1798 } 1799 1800 static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of, 1801 char *buf, size_t nbytes, 1802 loff_t off) 1803 { 1804 struct rdt_resource *r = rdt_kn_parent_priv(of->kn); 1805 int ret; 1806 1807 /* Valid input requires a trailing newline */ 1808 if (nbytes == 0 || buf[nbytes - 1] != '\n') 1809 return -EINVAL; 1810 1811 cpus_read_lock(); 1812 mutex_lock(&rdtgroup_mutex); 1813 1814 rdt_last_cmd_clear(); 1815 1816 buf[nbytes - 1] = '\0'; 1817 1818 ret = mon_config_write(r, buf, QOS_L3_MBM_LOCAL_EVENT_ID); 1819 1820 mutex_unlock(&rdtgroup_mutex); 1821 cpus_read_unlock(); 1822 1823 return ret ?: nbytes; 1824 } 1825 1826 /* 1827 * resctrl_bmec_files_show() — Controls the visibility of BMEC-related resctrl 1828 * files. When @show is true, the files are displayed; when false, the files 1829 * are hidden. 1830 * Don't treat kernfs_find_and_get failure as an error, since this function may 1831 * be called regardless of whether BMEC is supported or the event is enabled. 1832 */ 1833 void resctrl_bmec_files_show(struct rdt_resource *r, struct kernfs_node *l3_mon_kn, 1834 bool show) 1835 { 1836 struct kernfs_node *kn_config, *mon_kn = NULL; 1837 char name[32]; 1838 1839 if (!l3_mon_kn) { 1840 sprintf(name, "%s_MON", r->name); 1841 mon_kn = kernfs_find_and_get(kn_info, name); 1842 if (!mon_kn) 1843 return; 1844 l3_mon_kn = mon_kn; 1845 } 1846 1847 kn_config = kernfs_find_and_get(l3_mon_kn, "mbm_total_bytes_config"); 1848 if (kn_config) { 1849 kernfs_show(kn_config, show); 1850 kernfs_put(kn_config); 1851 } 1852 1853 kn_config = kernfs_find_and_get(l3_mon_kn, "mbm_local_bytes_config"); 1854 if (kn_config) { 1855 kernfs_show(kn_config, show); 1856 kernfs_put(kn_config); 1857 } 1858 1859 /* Release the reference only if it was acquired */ 1860 if (mon_kn) 1861 kernfs_put(mon_kn); 1862 } 1863 1864 const char *rdtgroup_name_by_closid(u32 closid) 1865 { 1866 struct rdtgroup *rdtgrp; 1867 1868 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 1869 if (rdtgrp->closid == closid) 1870 return rdt_kn_name(rdtgrp->kn); 1871 } 1872 1873 return NULL; 1874 } 1875 1876 /* rdtgroup information files for one cache resource. */ 1877 static struct rftype res_common_files[] = { 1878 { 1879 .name = "last_cmd_status", 1880 .mode = 0444, 1881 .kf_ops = &rdtgroup_kf_single_ops, 1882 .seq_show = rdt_last_cmd_status_show, 1883 .fflags = RFTYPE_TOP_INFO, 1884 }, 1885 { 1886 .name = "mbm_assign_on_mkdir", 1887 .mode = 0644, 1888 .kf_ops = &rdtgroup_kf_single_ops, 1889 .seq_show = resctrl_mbm_assign_on_mkdir_show, 1890 .write = resctrl_mbm_assign_on_mkdir_write, 1891 }, 1892 { 1893 .name = "num_closids", 1894 .mode = 0444, 1895 .kf_ops = &rdtgroup_kf_single_ops, 1896 .seq_show = rdt_num_closids_show, 1897 .fflags = RFTYPE_CTRL_INFO, 1898 }, 1899 { 1900 .name = "mon_features", 1901 .mode = 0444, 1902 .kf_ops = &rdtgroup_kf_single_ops, 1903 .seq_show = rdt_mon_features_show, 1904 .fflags = RFTYPE_MON_INFO, 1905 }, 1906 { 1907 .name = "available_mbm_cntrs", 1908 .mode = 0444, 1909 .kf_ops = &rdtgroup_kf_single_ops, 1910 .seq_show = resctrl_available_mbm_cntrs_show, 1911 }, 1912 { 1913 .name = "num_rmids", 1914 .mode = 0444, 1915 .kf_ops = &rdtgroup_kf_single_ops, 1916 .seq_show = rdt_num_rmids_show, 1917 .fflags = RFTYPE_MON_INFO, 1918 }, 1919 { 1920 .name = "cbm_mask", 1921 .mode = 0444, 1922 .kf_ops = &rdtgroup_kf_single_ops, 1923 .seq_show = rdt_default_ctrl_show, 1924 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE, 1925 }, 1926 { 1927 .name = "num_mbm_cntrs", 1928 .mode = 0444, 1929 .kf_ops = &rdtgroup_kf_single_ops, 1930 .seq_show = resctrl_num_mbm_cntrs_show, 1931 }, 1932 { 1933 .name = "min_cbm_bits", 1934 .mode = 0444, 1935 .kf_ops = &rdtgroup_kf_single_ops, 1936 .seq_show = rdt_min_cbm_bits_show, 1937 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE, 1938 }, 1939 { 1940 .name = "shareable_bits", 1941 .mode = 0444, 1942 .kf_ops = &rdtgroup_kf_single_ops, 1943 .seq_show = rdt_shareable_bits_show, 1944 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE, 1945 }, 1946 { 1947 .name = "bit_usage", 1948 .mode = 0444, 1949 .kf_ops = &rdtgroup_kf_single_ops, 1950 .seq_show = rdt_bit_usage_show, 1951 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE, 1952 }, 1953 { 1954 .name = "min_bandwidth", 1955 .mode = 0444, 1956 .kf_ops = &rdtgroup_kf_single_ops, 1957 .seq_show = rdt_min_bw_show, 1958 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB, 1959 }, 1960 { 1961 .name = "bandwidth_gran", 1962 .mode = 0444, 1963 .kf_ops = &rdtgroup_kf_single_ops, 1964 .seq_show = rdt_bw_gran_show, 1965 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB, 1966 }, 1967 { 1968 .name = "delay_linear", 1969 .mode = 0444, 1970 .kf_ops = &rdtgroup_kf_single_ops, 1971 .seq_show = rdt_delay_linear_show, 1972 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB, 1973 }, 1974 /* 1975 * Platform specific which (if any) capabilities are provided by 1976 * thread_throttle_mode. Defer "fflags" initialization to platform 1977 * discovery. 1978 */ 1979 { 1980 .name = "thread_throttle_mode", 1981 .mode = 0444, 1982 .kf_ops = &rdtgroup_kf_single_ops, 1983 .seq_show = rdt_thread_throttle_mode_show, 1984 }, 1985 { 1986 .name = "io_alloc", 1987 .mode = 0644, 1988 .kf_ops = &rdtgroup_kf_single_ops, 1989 .seq_show = resctrl_io_alloc_show, 1990 .write = resctrl_io_alloc_write, 1991 }, 1992 { 1993 .name = "io_alloc_cbm", 1994 .mode = 0644, 1995 .kf_ops = &rdtgroup_kf_single_ops, 1996 .seq_show = resctrl_io_alloc_cbm_show, 1997 .write = resctrl_io_alloc_cbm_write, 1998 }, 1999 { 2000 .name = "max_threshold_occupancy", 2001 .mode = 0644, 2002 .kf_ops = &rdtgroup_kf_single_ops, 2003 .write = max_threshold_occ_write, 2004 .seq_show = max_threshold_occ_show, 2005 .fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE, 2006 }, 2007 { 2008 .name = "mbm_total_bytes_config", 2009 .mode = 0644, 2010 .kf_ops = &rdtgroup_kf_single_ops, 2011 .seq_show = mbm_total_bytes_config_show, 2012 .write = mbm_total_bytes_config_write, 2013 }, 2014 { 2015 .name = "mbm_local_bytes_config", 2016 .mode = 0644, 2017 .kf_ops = &rdtgroup_kf_single_ops, 2018 .seq_show = mbm_local_bytes_config_show, 2019 .write = mbm_local_bytes_config_write, 2020 }, 2021 { 2022 .name = "event_filter", 2023 .mode = 0644, 2024 .kf_ops = &rdtgroup_kf_single_ops, 2025 .seq_show = event_filter_show, 2026 .write = event_filter_write, 2027 }, 2028 { 2029 .name = "mbm_L3_assignments", 2030 .mode = 0644, 2031 .kf_ops = &rdtgroup_kf_single_ops, 2032 .seq_show = mbm_L3_assignments_show, 2033 .write = mbm_L3_assignments_write, 2034 }, 2035 { 2036 .name = "mbm_assign_mode", 2037 .mode = 0644, 2038 .kf_ops = &rdtgroup_kf_single_ops, 2039 .seq_show = resctrl_mbm_assign_mode_show, 2040 .write = resctrl_mbm_assign_mode_write, 2041 .fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE, 2042 }, 2043 { 2044 .name = "cpus", 2045 .mode = 0644, 2046 .kf_ops = &rdtgroup_kf_single_ops, 2047 .write = rdtgroup_cpus_write, 2048 .seq_show = rdtgroup_cpus_show, 2049 .fflags = RFTYPE_BASE, 2050 }, 2051 { 2052 .name = "cpus_list", 2053 .mode = 0644, 2054 .kf_ops = &rdtgroup_kf_single_ops, 2055 .write = rdtgroup_cpus_write, 2056 .seq_show = rdtgroup_cpus_show, 2057 .flags = RFTYPE_FLAGS_CPUS_LIST, 2058 .fflags = RFTYPE_BASE, 2059 }, 2060 { 2061 .name = "tasks", 2062 .mode = 0644, 2063 .kf_ops = &rdtgroup_kf_single_ops, 2064 .write = rdtgroup_tasks_write, 2065 .seq_show = rdtgroup_tasks_show, 2066 .fflags = RFTYPE_BASE, 2067 }, 2068 { 2069 .name = "mon_hw_id", 2070 .mode = 0444, 2071 .kf_ops = &rdtgroup_kf_single_ops, 2072 .seq_show = rdtgroup_rmid_show, 2073 .fflags = RFTYPE_MON_BASE | RFTYPE_DEBUG, 2074 }, 2075 { 2076 .name = "schemata", 2077 .mode = 0644, 2078 .kf_ops = &rdtgroup_kf_single_ops, 2079 .write = rdtgroup_schemata_write, 2080 .seq_show = rdtgroup_schemata_show, 2081 .fflags = RFTYPE_CTRL_BASE, 2082 }, 2083 { 2084 .name = "mba_MBps_event", 2085 .mode = 0644, 2086 .kf_ops = &rdtgroup_kf_single_ops, 2087 .write = rdtgroup_mba_mbps_event_write, 2088 .seq_show = rdtgroup_mba_mbps_event_show, 2089 }, 2090 { 2091 .name = "mode", 2092 .mode = 0644, 2093 .kf_ops = &rdtgroup_kf_single_ops, 2094 .write = rdtgroup_mode_write, 2095 .seq_show = rdtgroup_mode_show, 2096 .fflags = RFTYPE_CTRL_BASE, 2097 }, 2098 { 2099 .name = "size", 2100 .mode = 0444, 2101 .kf_ops = &rdtgroup_kf_single_ops, 2102 .seq_show = rdtgroup_size_show, 2103 .fflags = RFTYPE_CTRL_BASE, 2104 }, 2105 { 2106 .name = "sparse_masks", 2107 .mode = 0444, 2108 .kf_ops = &rdtgroup_kf_single_ops, 2109 .seq_show = rdt_has_sparse_bitmasks_show, 2110 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE, 2111 }, 2112 { 2113 .name = "ctrl_hw_id", 2114 .mode = 0444, 2115 .kf_ops = &rdtgroup_kf_single_ops, 2116 .seq_show = rdtgroup_closid_show, 2117 .fflags = RFTYPE_CTRL_BASE | RFTYPE_DEBUG, 2118 }, 2119 }; 2120 2121 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) 2122 { 2123 struct rftype *rfts, *rft; 2124 int ret, len; 2125 2126 rfts = res_common_files; 2127 len = ARRAY_SIZE(res_common_files); 2128 2129 lockdep_assert_held(&rdtgroup_mutex); 2130 2131 if (resctrl_debug) 2132 fflags |= RFTYPE_DEBUG; 2133 2134 for (rft = rfts; rft < rfts + len; rft++) { 2135 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) { 2136 ret = rdtgroup_add_file(kn, rft); 2137 if (ret) 2138 goto error; 2139 } 2140 } 2141 2142 return 0; 2143 error: 2144 pr_warn("Failed to add %s, err=%d\n", rft->name, ret); 2145 while (--rft >= rfts) { 2146 if ((fflags & rft->fflags) == rft->fflags) 2147 kernfs_remove_by_name(kn, rft->name); 2148 } 2149 return ret; 2150 } 2151 2152 static struct rftype *rdtgroup_get_rftype_by_name(const char *name) 2153 { 2154 struct rftype *rfts, *rft; 2155 int len; 2156 2157 rfts = res_common_files; 2158 len = ARRAY_SIZE(res_common_files); 2159 2160 for (rft = rfts; rft < rfts + len; rft++) { 2161 if (!strcmp(rft->name, name)) 2162 return rft; 2163 } 2164 2165 return NULL; 2166 } 2167 2168 static void thread_throttle_mode_init(void) 2169 { 2170 enum membw_throttle_mode throttle_mode = THREAD_THROTTLE_UNDEFINED; 2171 struct rdt_resource *r_mba, *r_smba; 2172 2173 r_mba = resctrl_arch_get_resource(RDT_RESOURCE_MBA); 2174 if (r_mba->alloc_capable && 2175 r_mba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED) 2176 throttle_mode = r_mba->membw.throttle_mode; 2177 2178 r_smba = resctrl_arch_get_resource(RDT_RESOURCE_SMBA); 2179 if (r_smba->alloc_capable && 2180 r_smba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED) 2181 throttle_mode = r_smba->membw.throttle_mode; 2182 2183 if (throttle_mode == THREAD_THROTTLE_UNDEFINED) 2184 return; 2185 2186 resctrl_file_fflags_init("thread_throttle_mode", 2187 RFTYPE_CTRL_INFO | RFTYPE_RES_MB); 2188 } 2189 2190 /* 2191 * The resctrl file "io_alloc" is added using L3 resource. However, it results 2192 * in this file being visible for *all* cache resources (eg. L2 cache), 2193 * whether it supports "io_alloc" or not. 2194 */ 2195 static void io_alloc_init(void) 2196 { 2197 struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3); 2198 2199 if (r->cache.io_alloc_capable) { 2200 resctrl_file_fflags_init("io_alloc", RFTYPE_CTRL_INFO | 2201 RFTYPE_RES_CACHE); 2202 resctrl_file_fflags_init("io_alloc_cbm", 2203 RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE); 2204 } 2205 } 2206 2207 void resctrl_file_fflags_init(const char *config, unsigned long fflags) 2208 { 2209 struct rftype *rft; 2210 2211 rft = rdtgroup_get_rftype_by_name(config); 2212 if (rft) 2213 rft->fflags = fflags; 2214 } 2215 2216 /** 2217 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file 2218 * @r: The resource group with which the file is associated. 2219 * @name: Name of the file 2220 * 2221 * The permissions of named resctrl file, directory, or link are modified 2222 * to not allow read, write, or execute by any user. 2223 * 2224 * WARNING: This function is intended to communicate to the user that the 2225 * resctrl file has been locked down - that it is not relevant to the 2226 * particular state the system finds itself in. It should not be relied 2227 * on to protect from user access because after the file's permissions 2228 * are restricted the user can still change the permissions using chmod 2229 * from the command line. 2230 * 2231 * Return: 0 on success, <0 on failure. 2232 */ 2233 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name) 2234 { 2235 struct iattr iattr = {.ia_valid = ATTR_MODE,}; 2236 struct kernfs_node *kn; 2237 int ret = 0; 2238 2239 kn = kernfs_find_and_get_ns(r->kn, name, NULL); 2240 if (!kn) 2241 return -ENOENT; 2242 2243 switch (kernfs_type(kn)) { 2244 case KERNFS_DIR: 2245 iattr.ia_mode = S_IFDIR; 2246 break; 2247 case KERNFS_FILE: 2248 iattr.ia_mode = S_IFREG; 2249 break; 2250 case KERNFS_LINK: 2251 iattr.ia_mode = S_IFLNK; 2252 break; 2253 } 2254 2255 ret = kernfs_setattr(kn, &iattr); 2256 kernfs_put(kn); 2257 return ret; 2258 } 2259 2260 /** 2261 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file 2262 * @r: The resource group with which the file is associated. 2263 * @name: Name of the file 2264 * @mask: Mask of permissions that should be restored 2265 * 2266 * Restore the permissions of the named file. If @name is a directory the 2267 * permissions of its parent will be used. 2268 * 2269 * Return: 0 on success, <0 on failure. 2270 */ 2271 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name, 2272 umode_t mask) 2273 { 2274 struct iattr iattr = {.ia_valid = ATTR_MODE,}; 2275 struct kernfs_node *kn, *parent; 2276 struct rftype *rfts, *rft; 2277 int ret, len; 2278 2279 rfts = res_common_files; 2280 len = ARRAY_SIZE(res_common_files); 2281 2282 for (rft = rfts; rft < rfts + len; rft++) { 2283 if (!strcmp(rft->name, name)) 2284 iattr.ia_mode = rft->mode & mask; 2285 } 2286 2287 kn = kernfs_find_and_get_ns(r->kn, name, NULL); 2288 if (!kn) 2289 return -ENOENT; 2290 2291 switch (kernfs_type(kn)) { 2292 case KERNFS_DIR: 2293 parent = kernfs_get_parent(kn); 2294 if (parent) { 2295 iattr.ia_mode |= parent->mode; 2296 kernfs_put(parent); 2297 } 2298 iattr.ia_mode |= S_IFDIR; 2299 break; 2300 case KERNFS_FILE: 2301 iattr.ia_mode |= S_IFREG; 2302 break; 2303 case KERNFS_LINK: 2304 iattr.ia_mode |= S_IFLNK; 2305 break; 2306 } 2307 2308 ret = kernfs_setattr(kn, &iattr); 2309 kernfs_put(kn); 2310 return ret; 2311 } 2312 2313 static int resctrl_mkdir_event_configs(struct rdt_resource *r, struct kernfs_node *l3_mon_kn) 2314 { 2315 struct kernfs_node *kn_subdir, *kn_subdir2; 2316 struct mon_evt *mevt; 2317 int ret; 2318 2319 kn_subdir = kernfs_create_dir(l3_mon_kn, "event_configs", l3_mon_kn->mode, NULL); 2320 if (IS_ERR(kn_subdir)) 2321 return PTR_ERR(kn_subdir); 2322 2323 ret = rdtgroup_kn_set_ugid(kn_subdir); 2324 if (ret) 2325 return ret; 2326 2327 for_each_mon_event(mevt) { 2328 if (mevt->rid != r->rid || !mevt->enabled || !resctrl_is_mbm_event(mevt->evtid)) 2329 continue; 2330 2331 kn_subdir2 = kernfs_create_dir(kn_subdir, mevt->name, kn_subdir->mode, mevt); 2332 if (IS_ERR(kn_subdir2)) { 2333 ret = PTR_ERR(kn_subdir2); 2334 goto out; 2335 } 2336 2337 ret = rdtgroup_kn_set_ugid(kn_subdir2); 2338 if (ret) 2339 goto out; 2340 2341 ret = rdtgroup_add_files(kn_subdir2, RFTYPE_ASSIGN_CONFIG); 2342 if (ret) 2343 break; 2344 } 2345 2346 out: 2347 return ret; 2348 } 2349 2350 static int rdtgroup_mkdir_info_resdir(void *priv, char *name, 2351 unsigned long fflags) 2352 { 2353 struct kernfs_node *kn_subdir; 2354 struct rdt_resource *r; 2355 int ret; 2356 2357 kn_subdir = kernfs_create_dir(kn_info, name, 2358 kn_info->mode, priv); 2359 if (IS_ERR(kn_subdir)) 2360 return PTR_ERR(kn_subdir); 2361 2362 ret = rdtgroup_kn_set_ugid(kn_subdir); 2363 if (ret) 2364 return ret; 2365 2366 ret = rdtgroup_add_files(kn_subdir, fflags); 2367 if (ret) 2368 return ret; 2369 2370 if ((fflags & RFTYPE_MON_INFO) == RFTYPE_MON_INFO) { 2371 r = priv; 2372 if (r->mon.mbm_cntr_assignable) { 2373 ret = resctrl_mkdir_event_configs(r, kn_subdir); 2374 if (ret) 2375 return ret; 2376 /* 2377 * Hide BMEC related files if mbm_event mode 2378 * is enabled. 2379 */ 2380 if (resctrl_arch_mbm_cntr_assign_enabled(r)) 2381 resctrl_bmec_files_show(r, kn_subdir, false); 2382 } 2383 } 2384 2385 kernfs_activate(kn_subdir); 2386 2387 return ret; 2388 } 2389 2390 static unsigned long fflags_from_resource(struct rdt_resource *r) 2391 { 2392 switch (r->rid) { 2393 case RDT_RESOURCE_L3: 2394 case RDT_RESOURCE_L2: 2395 return RFTYPE_RES_CACHE; 2396 case RDT_RESOURCE_MBA: 2397 case RDT_RESOURCE_SMBA: 2398 return RFTYPE_RES_MB; 2399 case RDT_RESOURCE_PERF_PKG: 2400 return RFTYPE_RES_PERF_PKG; 2401 } 2402 2403 return WARN_ON_ONCE(1); 2404 } 2405 2406 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) 2407 { 2408 struct resctrl_schema *s; 2409 struct rdt_resource *r; 2410 unsigned long fflags; 2411 char name[32]; 2412 int ret; 2413 2414 /* create the directory */ 2415 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); 2416 if (IS_ERR(kn_info)) 2417 return PTR_ERR(kn_info); 2418 2419 ret = rdtgroup_add_files(kn_info, RFTYPE_TOP_INFO); 2420 if (ret) 2421 goto out_destroy; 2422 2423 /* loop over enabled controls, these are all alloc_capable */ 2424 list_for_each_entry(s, &resctrl_schema_all, list) { 2425 r = s->res; 2426 fflags = fflags_from_resource(r) | RFTYPE_CTRL_INFO; 2427 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags); 2428 if (ret) 2429 goto out_destroy; 2430 } 2431 2432 for_each_mon_capable_rdt_resource(r) { 2433 fflags = fflags_from_resource(r) | RFTYPE_MON_INFO; 2434 sprintf(name, "%s_MON", r->name); 2435 ret = rdtgroup_mkdir_info_resdir(r, name, fflags); 2436 if (ret) 2437 goto out_destroy; 2438 } 2439 2440 ret = rdtgroup_kn_set_ugid(kn_info); 2441 if (ret) 2442 goto out_destroy; 2443 2444 kernfs_activate(kn_info); 2445 2446 return 0; 2447 2448 out_destroy: 2449 kernfs_remove(kn_info); 2450 return ret; 2451 } 2452 2453 static int 2454 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, 2455 char *name, struct kernfs_node **dest_kn) 2456 { 2457 struct kernfs_node *kn; 2458 int ret; 2459 2460 /* create the directory */ 2461 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 2462 if (IS_ERR(kn)) 2463 return PTR_ERR(kn); 2464 2465 if (dest_kn) 2466 *dest_kn = kn; 2467 2468 ret = rdtgroup_kn_set_ugid(kn); 2469 if (ret) 2470 goto out_destroy; 2471 2472 kernfs_activate(kn); 2473 2474 return 0; 2475 2476 out_destroy: 2477 kernfs_remove(kn); 2478 return ret; 2479 } 2480 2481 static inline bool is_mba_linear(void) 2482 { 2483 return resctrl_arch_get_resource(RDT_RESOURCE_MBA)->membw.delay_linear; 2484 } 2485 2486 static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_ctrl_domain *d) 2487 { 2488 u32 num_closid = resctrl_arch_get_num_closid(r); 2489 int cpu = cpumask_any(&d->hdr.cpu_mask); 2490 int i; 2491 2492 d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val), 2493 GFP_KERNEL, cpu_to_node(cpu)); 2494 if (!d->mbps_val) 2495 return -ENOMEM; 2496 2497 for (i = 0; i < num_closid; i++) 2498 d->mbps_val[i] = MBA_MAX_MBPS; 2499 2500 return 0; 2501 } 2502 2503 static void mba_sc_domain_destroy(struct rdt_resource *r, 2504 struct rdt_ctrl_domain *d) 2505 { 2506 kfree(d->mbps_val); 2507 d->mbps_val = NULL; 2508 } 2509 2510 /* 2511 * MBA software controller is supported only if 2512 * MBM is supported and MBA is in linear scale, 2513 * and the MBM monitor scope is the same as MBA 2514 * control scope. 2515 */ 2516 static bool supports_mba_mbps(void) 2517 { 2518 struct rdt_resource *rmbm = resctrl_arch_get_resource(RDT_RESOURCE_L3); 2519 struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_MBA); 2520 2521 return (resctrl_is_mbm_enabled() && 2522 r->alloc_capable && is_mba_linear() && 2523 r->ctrl_scope == rmbm->mon_scope); 2524 } 2525 2526 /* 2527 * Enable or disable the MBA software controller 2528 * which helps user specify bandwidth in MBps. 2529 */ 2530 static int set_mba_sc(bool mba_sc) 2531 { 2532 struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_MBA); 2533 u32 num_closid = resctrl_arch_get_num_closid(r); 2534 struct rdt_ctrl_domain *d; 2535 unsigned long fflags; 2536 int i; 2537 2538 if (!supports_mba_mbps() || mba_sc == is_mba_sc(r)) 2539 return -EINVAL; 2540 2541 r->membw.mba_sc = mba_sc; 2542 2543 rdtgroup_default.mba_mbps_event = mba_mbps_default_event; 2544 2545 list_for_each_entry(d, &r->ctrl_domains, hdr.list) { 2546 for (i = 0; i < num_closid; i++) 2547 d->mbps_val[i] = MBA_MAX_MBPS; 2548 } 2549 2550 fflags = mba_sc ? RFTYPE_CTRL_BASE | RFTYPE_MON_BASE : 0; 2551 resctrl_file_fflags_init("mba_MBps_event", fflags); 2552 2553 return 0; 2554 } 2555 2556 /* 2557 * We don't allow rdtgroup directories to be created anywhere 2558 * except the root directory. Thus when looking for the rdtgroup 2559 * structure for a kernfs node we are either looking at a directory, 2560 * in which case the rdtgroup structure is pointed at by the "priv" 2561 * field, otherwise we have a file, and need only look to the parent 2562 * to find the rdtgroup. 2563 */ 2564 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) 2565 { 2566 if (kernfs_type(kn) == KERNFS_DIR) { 2567 /* 2568 * All the resource directories use "kn->priv" 2569 * to point to the "struct rdtgroup" for the 2570 * resource. "info" and its subdirectories don't 2571 * have rdtgroup structures, so return NULL here. 2572 */ 2573 if (kn == kn_info || 2574 rcu_access_pointer(kn->__parent) == kn_info) 2575 return NULL; 2576 else 2577 return kn->priv; 2578 } else { 2579 return rdt_kn_parent_priv(kn); 2580 } 2581 } 2582 2583 static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn) 2584 { 2585 atomic_inc(&rdtgrp->waitcount); 2586 kernfs_break_active_protection(kn); 2587 } 2588 2589 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn) 2590 { 2591 if (atomic_dec_and_test(&rdtgrp->waitcount) && 2592 (rdtgrp->flags & RDT_DELETED)) { 2593 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2594 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2595 rdtgroup_pseudo_lock_remove(rdtgrp); 2596 kernfs_unbreak_active_protection(kn); 2597 rdtgroup_remove(rdtgrp); 2598 } else { 2599 kernfs_unbreak_active_protection(kn); 2600 } 2601 } 2602 2603 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) 2604 { 2605 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 2606 2607 if (!rdtgrp) 2608 return NULL; 2609 2610 rdtgroup_kn_get(rdtgrp, kn); 2611 2612 cpus_read_lock(); 2613 mutex_lock(&rdtgroup_mutex); 2614 2615 /* Was this group deleted while we waited? */ 2616 if (rdtgrp->flags & RDT_DELETED) 2617 return NULL; 2618 2619 return rdtgrp; 2620 } 2621 2622 void rdtgroup_kn_unlock(struct kernfs_node *kn) 2623 { 2624 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 2625 2626 if (!rdtgrp) 2627 return; 2628 2629 mutex_unlock(&rdtgroup_mutex); 2630 cpus_read_unlock(); 2631 2632 rdtgroup_kn_put(rdtgrp, kn); 2633 } 2634 2635 static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2636 struct rdtgroup *prgrp, 2637 struct kernfs_node **mon_data_kn); 2638 2639 static void rdt_disable_ctx(void) 2640 { 2641 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false); 2642 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false); 2643 set_mba_sc(false); 2644 2645 resctrl_debug = false; 2646 } 2647 2648 static int rdt_enable_ctx(struct rdt_fs_context *ctx) 2649 { 2650 int ret = 0; 2651 2652 if (ctx->enable_cdpl2) { 2653 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true); 2654 if (ret) 2655 goto out_done; 2656 } 2657 2658 if (ctx->enable_cdpl3) { 2659 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true); 2660 if (ret) 2661 goto out_cdpl2; 2662 } 2663 2664 if (ctx->enable_mba_mbps) { 2665 ret = set_mba_sc(true); 2666 if (ret) 2667 goto out_cdpl3; 2668 } 2669 2670 if (ctx->enable_debug) 2671 resctrl_debug = true; 2672 2673 return 0; 2674 2675 out_cdpl3: 2676 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false); 2677 out_cdpl2: 2678 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false); 2679 out_done: 2680 return ret; 2681 } 2682 2683 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type) 2684 { 2685 struct resctrl_schema *s; 2686 const char *suffix = ""; 2687 int ret, cl; 2688 2689 s = kzalloc(sizeof(*s), GFP_KERNEL); 2690 if (!s) 2691 return -ENOMEM; 2692 2693 s->res = r; 2694 s->num_closid = resctrl_arch_get_num_closid(r); 2695 if (resctrl_arch_get_cdp_enabled(r->rid)) 2696 s->num_closid /= 2; 2697 2698 s->conf_type = type; 2699 switch (type) { 2700 case CDP_CODE: 2701 suffix = "CODE"; 2702 break; 2703 case CDP_DATA: 2704 suffix = "DATA"; 2705 break; 2706 case CDP_NONE: 2707 suffix = ""; 2708 break; 2709 } 2710 2711 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix); 2712 if (ret >= sizeof(s->name)) { 2713 kfree(s); 2714 return -EINVAL; 2715 } 2716 2717 cl = strlen(s->name); 2718 2719 /* 2720 * If CDP is supported by this resource, but not enabled, 2721 * include the suffix. This ensures the tabular format of the 2722 * schemata file does not change between mounts of the filesystem. 2723 */ 2724 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid)) 2725 cl += 4; 2726 2727 if (cl > max_name_width) 2728 max_name_width = cl; 2729 2730 switch (r->schema_fmt) { 2731 case RESCTRL_SCHEMA_BITMAP: 2732 s->fmt_str = "%d=%x"; 2733 break; 2734 case RESCTRL_SCHEMA_RANGE: 2735 s->fmt_str = "%d=%u"; 2736 break; 2737 } 2738 2739 if (WARN_ON_ONCE(!s->fmt_str)) { 2740 kfree(s); 2741 return -EINVAL; 2742 } 2743 2744 INIT_LIST_HEAD(&s->list); 2745 list_add(&s->list, &resctrl_schema_all); 2746 2747 return 0; 2748 } 2749 2750 static int schemata_list_create(void) 2751 { 2752 struct rdt_resource *r; 2753 int ret = 0; 2754 2755 for_each_alloc_capable_rdt_resource(r) { 2756 if (resctrl_arch_get_cdp_enabled(r->rid)) { 2757 ret = schemata_list_add(r, CDP_CODE); 2758 if (ret) 2759 break; 2760 2761 ret = schemata_list_add(r, CDP_DATA); 2762 } else { 2763 ret = schemata_list_add(r, CDP_NONE); 2764 } 2765 2766 if (ret) 2767 break; 2768 } 2769 2770 return ret; 2771 } 2772 2773 static void schemata_list_destroy(void) 2774 { 2775 struct resctrl_schema *s, *tmp; 2776 2777 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) { 2778 list_del(&s->list); 2779 kfree(s); 2780 } 2781 } 2782 2783 static int rdt_get_tree(struct fs_context *fc) 2784 { 2785 struct rdt_fs_context *ctx = rdt_fc2context(fc); 2786 unsigned long flags = RFTYPE_CTRL_BASE; 2787 struct rdt_l3_mon_domain *dom; 2788 struct rdt_resource *r; 2789 int ret; 2790 2791 DO_ONCE_SLEEPABLE(resctrl_arch_pre_mount); 2792 2793 cpus_read_lock(); 2794 mutex_lock(&rdtgroup_mutex); 2795 /* 2796 * resctrl file system can only be mounted once. 2797 */ 2798 if (resctrl_mounted) { 2799 ret = -EBUSY; 2800 goto out; 2801 } 2802 2803 ret = rdtgroup_setup_root(ctx); 2804 if (ret) 2805 goto out; 2806 2807 ret = rdt_enable_ctx(ctx); 2808 if (ret) 2809 goto out_root; 2810 2811 ret = schemata_list_create(); 2812 if (ret) 2813 goto out_schemata_free; 2814 2815 ret = closid_init(); 2816 if (ret) 2817 goto out_schemata_free; 2818 2819 if (resctrl_arch_mon_capable()) 2820 flags |= RFTYPE_MON; 2821 2822 ret = rdtgroup_add_files(rdtgroup_default.kn, flags); 2823 if (ret) 2824 goto out_closid_exit; 2825 2826 kernfs_activate(rdtgroup_default.kn); 2827 2828 ret = rdtgroup_create_info_dir(rdtgroup_default.kn); 2829 if (ret < 0) 2830 goto out_closid_exit; 2831 2832 if (resctrl_arch_mon_capable()) { 2833 ret = mongroup_create_dir(rdtgroup_default.kn, 2834 &rdtgroup_default, "mon_groups", 2835 &kn_mongrp); 2836 if (ret < 0) 2837 goto out_info; 2838 2839 rdtgroup_assign_cntrs(&rdtgroup_default); 2840 2841 ret = mkdir_mondata_all(rdtgroup_default.kn, 2842 &rdtgroup_default, &kn_mondata); 2843 if (ret < 0) 2844 goto out_mongrp; 2845 rdtgroup_default.mon.mon_data_kn = kn_mondata; 2846 } 2847 2848 ret = rdt_pseudo_lock_init(); 2849 if (ret) 2850 goto out_mondata; 2851 2852 ret = kernfs_get_tree(fc); 2853 if (ret < 0) 2854 goto out_psl; 2855 2856 if (resctrl_arch_alloc_capable()) 2857 resctrl_arch_enable_alloc(); 2858 if (resctrl_arch_mon_capable()) 2859 resctrl_arch_enable_mon(); 2860 2861 if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable()) 2862 resctrl_mounted = true; 2863 2864 if (resctrl_is_mbm_enabled()) { 2865 r = resctrl_arch_get_resource(RDT_RESOURCE_L3); 2866 list_for_each_entry(dom, &r->mon_domains, hdr.list) 2867 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL, 2868 RESCTRL_PICK_ANY_CPU); 2869 } 2870 2871 goto out; 2872 2873 out_psl: 2874 rdt_pseudo_lock_release(); 2875 out_mondata: 2876 if (resctrl_arch_mon_capable()) 2877 kernfs_remove(kn_mondata); 2878 out_mongrp: 2879 if (resctrl_arch_mon_capable()) { 2880 rdtgroup_unassign_cntrs(&rdtgroup_default); 2881 kernfs_remove(kn_mongrp); 2882 } 2883 out_info: 2884 kernfs_remove(kn_info); 2885 out_closid_exit: 2886 closid_exit(); 2887 out_schemata_free: 2888 schemata_list_destroy(); 2889 rdt_disable_ctx(); 2890 out_root: 2891 rdtgroup_destroy_root(); 2892 out: 2893 rdt_last_cmd_clear(); 2894 mutex_unlock(&rdtgroup_mutex); 2895 cpus_read_unlock(); 2896 return ret; 2897 } 2898 2899 enum rdt_param { 2900 Opt_cdp, 2901 Opt_cdpl2, 2902 Opt_mba_mbps, 2903 Opt_debug, 2904 nr__rdt_params 2905 }; 2906 2907 static const struct fs_parameter_spec rdt_fs_parameters[] = { 2908 fsparam_flag("cdp", Opt_cdp), 2909 fsparam_flag("cdpl2", Opt_cdpl2), 2910 fsparam_flag("mba_MBps", Opt_mba_mbps), 2911 fsparam_flag("debug", Opt_debug), 2912 {} 2913 }; 2914 2915 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param) 2916 { 2917 struct rdt_fs_context *ctx = rdt_fc2context(fc); 2918 struct fs_parse_result result; 2919 const char *msg; 2920 int opt; 2921 2922 opt = fs_parse(fc, rdt_fs_parameters, param, &result); 2923 if (opt < 0) 2924 return opt; 2925 2926 switch (opt) { 2927 case Opt_cdp: 2928 ctx->enable_cdpl3 = true; 2929 return 0; 2930 case Opt_cdpl2: 2931 ctx->enable_cdpl2 = true; 2932 return 0; 2933 case Opt_mba_mbps: 2934 msg = "mba_MBps requires MBM and linear scale MBA at L3 scope"; 2935 if (!supports_mba_mbps()) 2936 return invalfc(fc, msg); 2937 ctx->enable_mba_mbps = true; 2938 return 0; 2939 case Opt_debug: 2940 ctx->enable_debug = true; 2941 return 0; 2942 } 2943 2944 return -EINVAL; 2945 } 2946 2947 static void rdt_fs_context_free(struct fs_context *fc) 2948 { 2949 struct rdt_fs_context *ctx = rdt_fc2context(fc); 2950 2951 kernfs_free_fs_context(fc); 2952 kfree(ctx); 2953 } 2954 2955 static const struct fs_context_operations rdt_fs_context_ops = { 2956 .free = rdt_fs_context_free, 2957 .parse_param = rdt_parse_param, 2958 .get_tree = rdt_get_tree, 2959 }; 2960 2961 static int rdt_init_fs_context(struct fs_context *fc) 2962 { 2963 struct rdt_fs_context *ctx; 2964 2965 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 2966 if (!ctx) 2967 return -ENOMEM; 2968 2969 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC; 2970 fc->fs_private = &ctx->kfc; 2971 fc->ops = &rdt_fs_context_ops; 2972 put_user_ns(fc->user_ns); 2973 fc->user_ns = get_user_ns(&init_user_ns); 2974 fc->global = true; 2975 return 0; 2976 } 2977 2978 /* 2979 * Move tasks from one to the other group. If @from is NULL, then all tasks 2980 * in the systems are moved unconditionally (used for teardown). 2981 * 2982 * If @mask is not NULL the cpus on which moved tasks are running are set 2983 * in that mask so the update smp function call is restricted to affected 2984 * cpus. 2985 */ 2986 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, 2987 struct cpumask *mask) 2988 { 2989 struct task_struct *p, *t; 2990 2991 read_lock(&tasklist_lock); 2992 for_each_process_thread(p, t) { 2993 if (!from || is_closid_match(t, from) || 2994 is_rmid_match(t, from)) { 2995 resctrl_arch_set_closid_rmid(t, to->closid, 2996 to->mon.rmid); 2997 2998 /* 2999 * Order the closid/rmid stores above before the loads 3000 * in task_curr(). This pairs with the full barrier 3001 * between the rq->curr update and 3002 * resctrl_arch_sched_in() during context switch. 3003 */ 3004 smp_mb(); 3005 3006 /* 3007 * If the task is on a CPU, set the CPU in the mask. 3008 * The detection is inaccurate as tasks might move or 3009 * schedule before the smp function call takes place. 3010 * In such a case the function call is pointless, but 3011 * there is no other side effect. 3012 */ 3013 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t)) 3014 cpumask_set_cpu(task_cpu(t), mask); 3015 } 3016 } 3017 read_unlock(&tasklist_lock); 3018 } 3019 3020 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) 3021 { 3022 struct rdtgroup *sentry, *stmp; 3023 struct list_head *head; 3024 3025 head = &rdtgrp->mon.crdtgrp_list; 3026 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { 3027 rdtgroup_unassign_cntrs(sentry); 3028 free_rmid(sentry->closid, sentry->mon.rmid); 3029 list_del(&sentry->mon.crdtgrp_list); 3030 3031 if (atomic_read(&sentry->waitcount) != 0) 3032 sentry->flags = RDT_DELETED; 3033 else 3034 rdtgroup_remove(sentry); 3035 } 3036 } 3037 3038 /* 3039 * Forcibly remove all of subdirectories under root. 3040 */ 3041 static void rmdir_all_sub(void) 3042 { 3043 struct rdtgroup *rdtgrp, *tmp; 3044 3045 /* Move all tasks to the default resource group */ 3046 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); 3047 3048 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { 3049 /* Free any child rmids */ 3050 free_all_child_rdtgrp(rdtgrp); 3051 3052 /* Remove each rdtgroup other than root */ 3053 if (rdtgrp == &rdtgroup_default) 3054 continue; 3055 3056 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3057 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 3058 rdtgroup_pseudo_lock_remove(rdtgrp); 3059 3060 /* 3061 * Give any CPUs back to the default group. We cannot copy 3062 * cpu_online_mask because a CPU might have executed the 3063 * offline callback already, but is still marked online. 3064 */ 3065 cpumask_or(&rdtgroup_default.cpu_mask, 3066 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 3067 3068 rdtgroup_unassign_cntrs(rdtgrp); 3069 3070 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 3071 3072 kernfs_remove(rdtgrp->kn); 3073 list_del(&rdtgrp->rdtgroup_list); 3074 3075 if (atomic_read(&rdtgrp->waitcount) != 0) 3076 rdtgrp->flags = RDT_DELETED; 3077 else 3078 rdtgroup_remove(rdtgrp); 3079 } 3080 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ 3081 update_closid_rmid(cpu_online_mask, &rdtgroup_default); 3082 3083 kernfs_remove(kn_info); 3084 kernfs_remove(kn_mongrp); 3085 kernfs_remove(kn_mondata); 3086 } 3087 3088 /** 3089 * mon_get_kn_priv() - Get the mon_data priv data for this event. 3090 * 3091 * The same values are used across the mon_data directories of all control and 3092 * monitor groups for the same event in the same domain. Keep a list of 3093 * allocated structures and re-use an existing one with the same values for 3094 * @rid, @domid, etc. 3095 * 3096 * @rid: The resource id for the event file being created. 3097 * @domid: The domain id for the event file being created. 3098 * @mevt: The type of event file being created. 3099 * @do_sum: Whether SNC summing monitors are being created. Only set 3100 * when @rid == RDT_RESOURCE_L3. 3101 */ 3102 static struct mon_data *mon_get_kn_priv(enum resctrl_res_level rid, int domid, 3103 struct mon_evt *mevt, 3104 bool do_sum) 3105 { 3106 struct mon_data *priv; 3107 3108 lockdep_assert_held(&rdtgroup_mutex); 3109 3110 list_for_each_entry(priv, &mon_data_kn_priv_list, list) { 3111 if (priv->rid == rid && priv->domid == domid && 3112 priv->sum == do_sum && priv->evt == mevt) 3113 return priv; 3114 } 3115 3116 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 3117 if (!priv) 3118 return NULL; 3119 3120 priv->rid = rid; 3121 priv->domid = domid; 3122 priv->sum = do_sum; 3123 priv->evt = mevt; 3124 list_add_tail(&priv->list, &mon_data_kn_priv_list); 3125 3126 return priv; 3127 } 3128 3129 /** 3130 * mon_put_kn_priv() - Free all allocated mon_data structures. 3131 * 3132 * Called when resctrl file system is unmounted. 3133 */ 3134 static void mon_put_kn_priv(void) 3135 { 3136 struct mon_data *priv, *tmp; 3137 3138 lockdep_assert_held(&rdtgroup_mutex); 3139 3140 list_for_each_entry_safe(priv, tmp, &mon_data_kn_priv_list, list) { 3141 list_del(&priv->list); 3142 kfree(priv); 3143 } 3144 } 3145 3146 static void resctrl_fs_teardown(void) 3147 { 3148 lockdep_assert_held(&rdtgroup_mutex); 3149 3150 /* Cleared by rdtgroup_destroy_root() */ 3151 if (!rdtgroup_default.kn) 3152 return; 3153 3154 rmdir_all_sub(); 3155 rdtgroup_unassign_cntrs(&rdtgroup_default); 3156 mon_put_kn_priv(); 3157 rdt_pseudo_lock_release(); 3158 rdtgroup_default.mode = RDT_MODE_SHAREABLE; 3159 closid_exit(); 3160 schemata_list_destroy(); 3161 rdtgroup_destroy_root(); 3162 } 3163 3164 static void rdt_kill_sb(struct super_block *sb) 3165 { 3166 struct rdt_resource *r; 3167 3168 cpus_read_lock(); 3169 mutex_lock(&rdtgroup_mutex); 3170 3171 rdt_disable_ctx(); 3172 3173 /* Put everything back to default values. */ 3174 for_each_alloc_capable_rdt_resource(r) 3175 resctrl_arch_reset_all_ctrls(r); 3176 3177 resctrl_fs_teardown(); 3178 if (resctrl_arch_alloc_capable()) 3179 resctrl_arch_disable_alloc(); 3180 if (resctrl_arch_mon_capable()) 3181 resctrl_arch_disable_mon(); 3182 resctrl_mounted = false; 3183 kernfs_kill_sb(sb); 3184 mutex_unlock(&rdtgroup_mutex); 3185 cpus_read_unlock(); 3186 } 3187 3188 static struct file_system_type rdt_fs_type = { 3189 .name = "resctrl", 3190 .init_fs_context = rdt_init_fs_context, 3191 .parameters = rdt_fs_parameters, 3192 .kill_sb = rdt_kill_sb, 3193 }; 3194 3195 static int mon_addfile(struct kernfs_node *parent_kn, const char *name, 3196 void *priv) 3197 { 3198 struct kernfs_node *kn; 3199 int ret = 0; 3200 3201 kn = __kernfs_create_file(parent_kn, name, 0444, 3202 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, 3203 &kf_mondata_ops, priv, NULL, NULL); 3204 if (IS_ERR(kn)) 3205 return PTR_ERR(kn); 3206 3207 ret = rdtgroup_kn_set_ugid(kn); 3208 if (ret) { 3209 kernfs_remove(kn); 3210 return ret; 3211 } 3212 3213 return ret; 3214 } 3215 3216 static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname) 3217 { 3218 struct kernfs_node *kn; 3219 3220 kn = kernfs_find_and_get(pkn, name); 3221 if (!kn) 3222 return; 3223 kernfs_put(kn); 3224 3225 if (kn->dir.subdirs <= 1) 3226 kernfs_remove(kn); 3227 else 3228 kernfs_remove_by_name(kn, subname); 3229 } 3230 3231 /* 3232 * Remove files and directories for one SNC node. If it is the last node 3233 * sharing an L3 cache, then remove the upper level directory containing 3234 * the "sum" files too. 3235 */ 3236 static void rmdir_mondata_subdir_allrdtgrp_snc(struct rdt_resource *r, 3237 struct rdt_domain_hdr *hdr) 3238 { 3239 struct rdtgroup *prgrp, *crgrp; 3240 struct rdt_l3_mon_domain *d; 3241 char subname[32]; 3242 char name[32]; 3243 3244 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 3245 return; 3246 3247 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 3248 sprintf(name, "mon_%s_%02d", r->name, d->ci_id); 3249 sprintf(subname, "mon_sub_%s_%02d", r->name, hdr->id); 3250 3251 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3252 mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname); 3253 3254 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 3255 mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname); 3256 } 3257 } 3258 3259 /* 3260 * Remove all subdirectories of mon_data of ctrl_mon groups 3261 * and monitor groups for the given domain. 3262 */ 3263 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 3264 struct rdt_domain_hdr *hdr) 3265 { 3266 struct rdtgroup *prgrp, *crgrp; 3267 char name[32]; 3268 3269 if (r->rid == RDT_RESOURCE_L3 && r->mon_scope == RESCTRL_L3_NODE) { 3270 rmdir_mondata_subdir_allrdtgrp_snc(r, hdr); 3271 return; 3272 } 3273 3274 sprintf(name, "mon_%s_%02d", r->name, hdr->id); 3275 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3276 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); 3277 3278 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 3279 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); 3280 } 3281 } 3282 3283 /* 3284 * Create a directory for a domain and populate it with monitor files. Create 3285 * summing monitors when @hdr is NULL. No need to initialize summing monitors. 3286 */ 3287 static struct kernfs_node *_mkdir_mondata_subdir(struct kernfs_node *parent_kn, char *name, 3288 struct rdt_domain_hdr *hdr, 3289 struct rdt_resource *r, 3290 struct rdtgroup *prgrp, int domid) 3291 { 3292 struct rmid_read rr = {0}; 3293 struct kernfs_node *kn; 3294 struct mon_data *priv; 3295 struct mon_evt *mevt; 3296 int ret; 3297 3298 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 3299 if (IS_ERR(kn)) 3300 return kn; 3301 3302 ret = rdtgroup_kn_set_ugid(kn); 3303 if (ret) 3304 goto out_destroy; 3305 3306 for_each_mon_event(mevt) { 3307 if (mevt->rid != r->rid || !mevt->enabled) 3308 continue; 3309 priv = mon_get_kn_priv(r->rid, domid, mevt, !hdr); 3310 if (WARN_ON_ONCE(!priv)) { 3311 ret = -EINVAL; 3312 goto out_destroy; 3313 } 3314 3315 ret = mon_addfile(kn, mevt->name, priv); 3316 if (ret) 3317 goto out_destroy; 3318 3319 if (hdr && resctrl_is_mbm_event(mevt->evtid)) 3320 mon_event_read(&rr, r, hdr, prgrp, &hdr->cpu_mask, mevt, true); 3321 } 3322 3323 return kn; 3324 out_destroy: 3325 kernfs_remove(kn); 3326 return ERR_PTR(ret); 3327 } 3328 3329 static int mkdir_mondata_subdir_snc(struct kernfs_node *parent_kn, 3330 struct rdt_domain_hdr *hdr, 3331 struct rdt_resource *r, struct rdtgroup *prgrp) 3332 { 3333 struct kernfs_node *ckn, *kn; 3334 struct rdt_l3_mon_domain *d; 3335 char name[32]; 3336 3337 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 3338 return -EINVAL; 3339 3340 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 3341 sprintf(name, "mon_%s_%02d", r->name, d->ci_id); 3342 kn = kernfs_find_and_get(parent_kn, name); 3343 if (kn) { 3344 /* 3345 * rdtgroup_mutex will prevent this directory from being 3346 * removed. No need to keep this hold. 3347 */ 3348 kernfs_put(kn); 3349 } else { 3350 kn = _mkdir_mondata_subdir(parent_kn, name, NULL, r, prgrp, d->ci_id); 3351 if (IS_ERR(kn)) 3352 return PTR_ERR(kn); 3353 } 3354 3355 sprintf(name, "mon_sub_%s_%02d", r->name, hdr->id); 3356 ckn = _mkdir_mondata_subdir(kn, name, hdr, r, prgrp, hdr->id); 3357 if (IS_ERR(ckn)) { 3358 kernfs_remove(kn); 3359 return PTR_ERR(ckn); 3360 } 3361 3362 kernfs_activate(kn); 3363 return 0; 3364 } 3365 3366 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, 3367 struct rdt_domain_hdr *hdr, 3368 struct rdt_resource *r, struct rdtgroup *prgrp) 3369 { 3370 struct kernfs_node *kn; 3371 char name[32]; 3372 3373 lockdep_assert_held(&rdtgroup_mutex); 3374 3375 if (r->rid == RDT_RESOURCE_L3 && r->mon_scope == RESCTRL_L3_NODE) 3376 return mkdir_mondata_subdir_snc(parent_kn, hdr, r, prgrp); 3377 3378 sprintf(name, "mon_%s_%02d", r->name, hdr->id); 3379 kn = _mkdir_mondata_subdir(parent_kn, name, hdr, r, prgrp, hdr->id); 3380 if (IS_ERR(kn)) 3381 return PTR_ERR(kn); 3382 3383 kernfs_activate(kn); 3384 return 0; 3385 } 3386 3387 /* 3388 * Add all subdirectories of mon_data for "ctrl_mon" groups 3389 * and "monitor" groups with given domain id. 3390 */ 3391 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 3392 struct rdt_domain_hdr *hdr) 3393 { 3394 struct kernfs_node *parent_kn; 3395 struct rdtgroup *prgrp, *crgrp; 3396 struct list_head *head; 3397 3398 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3399 parent_kn = prgrp->mon.mon_data_kn; 3400 mkdir_mondata_subdir(parent_kn, hdr, r, prgrp); 3401 3402 head = &prgrp->mon.crdtgrp_list; 3403 list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 3404 parent_kn = crgrp->mon.mon_data_kn; 3405 mkdir_mondata_subdir(parent_kn, hdr, r, crgrp); 3406 } 3407 } 3408 } 3409 3410 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, 3411 struct rdt_resource *r, 3412 struct rdtgroup *prgrp) 3413 { 3414 struct rdt_domain_hdr *hdr; 3415 int ret; 3416 3417 /* Walking r->domains, ensure it can't race with cpuhp */ 3418 lockdep_assert_cpus_held(); 3419 3420 list_for_each_entry(hdr, &r->mon_domains, list) { 3421 ret = mkdir_mondata_subdir(parent_kn, hdr, r, prgrp); 3422 if (ret) 3423 return ret; 3424 } 3425 3426 return 0; 3427 } 3428 3429 /* 3430 * This creates a directory mon_data which contains the monitored data. 3431 * 3432 * mon_data has one directory for each domain which are named 3433 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data 3434 * with L3 domain looks as below: 3435 * ./mon_data: 3436 * mon_L3_00 3437 * mon_L3_01 3438 * mon_L3_02 3439 * ... 3440 * 3441 * Each domain directory has one file per event: 3442 * ./mon_L3_00/: 3443 * llc_occupancy 3444 * 3445 */ 3446 static int mkdir_mondata_all(struct kernfs_node *parent_kn, 3447 struct rdtgroup *prgrp, 3448 struct kernfs_node **dest_kn) 3449 { 3450 struct rdt_resource *r; 3451 struct kernfs_node *kn; 3452 int ret; 3453 3454 /* 3455 * Create the mon_data directory first. 3456 */ 3457 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn); 3458 if (ret) 3459 return ret; 3460 3461 if (dest_kn) 3462 *dest_kn = kn; 3463 3464 /* 3465 * Create the subdirectories for each domain. Note that all events 3466 * in a domain like L3 are grouped into a resource whose domain is L3 3467 */ 3468 for_each_mon_capable_rdt_resource(r) { 3469 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); 3470 if (ret) 3471 goto out_destroy; 3472 } 3473 3474 return 0; 3475 3476 out_destroy: 3477 kernfs_remove(kn); 3478 return ret; 3479 } 3480 3481 /** 3482 * cbm_ensure_valid - Enforce validity on provided CBM 3483 * @_val: Candidate CBM 3484 * @r: RDT resource to which the CBM belongs 3485 * 3486 * The provided CBM represents all cache portions available for use. This 3487 * may be represented by a bitmap that does not consist of contiguous ones 3488 * and thus be an invalid CBM. 3489 * Here the provided CBM is forced to be a valid CBM by only considering 3490 * the first set of contiguous bits as valid and clearing all bits. 3491 * The intention here is to provide a valid default CBM with which a new 3492 * resource group is initialized. The user can follow this with a 3493 * modification to the CBM if the default does not satisfy the 3494 * requirements. 3495 */ 3496 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r) 3497 { 3498 unsigned int cbm_len = r->cache.cbm_len; 3499 unsigned long first_bit, zero_bit; 3500 unsigned long val; 3501 3502 if (!_val || r->cache.arch_has_sparse_bitmasks) 3503 return _val; 3504 3505 val = _val; 3506 first_bit = find_first_bit(&val, cbm_len); 3507 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit); 3508 3509 /* Clear any remaining bits to ensure contiguous region */ 3510 bitmap_clear(&val, zero_bit, cbm_len - zero_bit); 3511 return (u32)val; 3512 } 3513 3514 /* 3515 * Initialize cache resources per RDT domain 3516 * 3517 * Set the RDT domain up to start off with all usable allocations. That is, 3518 * all shareable and unused bits. All-zero CBM is invalid. 3519 */ 3520 static int __init_one_rdt_domain(struct rdt_ctrl_domain *d, struct resctrl_schema *s, 3521 u32 closid) 3522 { 3523 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type); 3524 enum resctrl_conf_type t = s->conf_type; 3525 struct resctrl_staged_config *cfg; 3526 struct rdt_resource *r = s->res; 3527 u32 used_b = 0, unused_b = 0; 3528 unsigned long tmp_cbm; 3529 enum rdtgrp_mode mode; 3530 u32 peer_ctl, ctrl_val; 3531 int i; 3532 3533 cfg = &d->staged_config[t]; 3534 cfg->have_new_ctrl = false; 3535 cfg->new_ctrl = r->cache.shareable_bits; 3536 used_b = r->cache.shareable_bits; 3537 for (i = 0; i < closids_supported(); i++) { 3538 if (closid_allocated(i) && i != closid) { 3539 mode = rdtgroup_mode_by_closid(i); 3540 if (mode == RDT_MODE_PSEUDO_LOCKSETUP) 3541 /* 3542 * ctrl values for locksetup aren't relevant 3543 * until the schemata is written, and the mode 3544 * becomes RDT_MODE_PSEUDO_LOCKED. 3545 */ 3546 continue; 3547 /* 3548 * If CDP is active include peer domain's 3549 * usage to ensure there is no overlap 3550 * with an exclusive group. 3551 */ 3552 if (resctrl_arch_get_cdp_enabled(r->rid)) 3553 peer_ctl = resctrl_arch_get_config(r, d, i, 3554 peer_type); 3555 else 3556 peer_ctl = 0; 3557 ctrl_val = resctrl_arch_get_config(r, d, i, 3558 s->conf_type); 3559 used_b |= ctrl_val | peer_ctl; 3560 if (mode == RDT_MODE_SHAREABLE) 3561 cfg->new_ctrl |= ctrl_val | peer_ctl; 3562 } 3563 } 3564 if (d->plr && d->plr->cbm > 0) 3565 used_b |= d->plr->cbm; 3566 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); 3567 unused_b &= BIT_MASK(r->cache.cbm_len) - 1; 3568 cfg->new_ctrl |= unused_b; 3569 /* 3570 * Force the initial CBM to be valid, user can 3571 * modify the CBM based on system availability. 3572 */ 3573 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r); 3574 /* 3575 * Assign the u32 CBM to an unsigned long to ensure that 3576 * bitmap_weight() does not access out-of-bound memory. 3577 */ 3578 tmp_cbm = cfg->new_ctrl; 3579 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) { 3580 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->hdr.id); 3581 return -ENOSPC; 3582 } 3583 cfg->have_new_ctrl = true; 3584 3585 return 0; 3586 } 3587 3588 /* 3589 * Initialize cache resources with default values. 3590 * 3591 * A new RDT group is being created on an allocation capable (CAT) 3592 * supporting system. Set this group up to start off with all usable 3593 * allocations. 3594 * 3595 * If there are no more shareable bits available on any domain then 3596 * the entire allocation will fail. 3597 */ 3598 int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid) 3599 { 3600 struct rdt_ctrl_domain *d; 3601 int ret; 3602 3603 list_for_each_entry(d, &s->res->ctrl_domains, hdr.list) { 3604 ret = __init_one_rdt_domain(d, s, closid); 3605 if (ret < 0) 3606 return ret; 3607 } 3608 3609 return 0; 3610 } 3611 3612 /* Initialize MBA resource with default values. */ 3613 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid) 3614 { 3615 struct resctrl_staged_config *cfg; 3616 struct rdt_ctrl_domain *d; 3617 3618 list_for_each_entry(d, &r->ctrl_domains, hdr.list) { 3619 if (is_mba_sc(r)) { 3620 d->mbps_val[closid] = MBA_MAX_MBPS; 3621 continue; 3622 } 3623 3624 cfg = &d->staged_config[CDP_NONE]; 3625 cfg->new_ctrl = resctrl_get_default_ctrl(r); 3626 cfg->have_new_ctrl = true; 3627 } 3628 } 3629 3630 /* Initialize the RDT group's allocations. */ 3631 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) 3632 { 3633 struct resctrl_schema *s; 3634 struct rdt_resource *r; 3635 int ret = 0; 3636 3637 rdt_staged_configs_clear(); 3638 3639 list_for_each_entry(s, &resctrl_schema_all, list) { 3640 r = s->res; 3641 if (r->rid == RDT_RESOURCE_MBA || 3642 r->rid == RDT_RESOURCE_SMBA) { 3643 rdtgroup_init_mba(r, rdtgrp->closid); 3644 if (is_mba_sc(r)) 3645 continue; 3646 } else { 3647 ret = rdtgroup_init_cat(s, rdtgrp->closid); 3648 if (ret < 0) 3649 goto out; 3650 } 3651 3652 ret = resctrl_arch_update_domains(r, rdtgrp->closid); 3653 if (ret < 0) { 3654 rdt_last_cmd_puts("Failed to initialize allocations\n"); 3655 goto out; 3656 } 3657 } 3658 3659 rdtgrp->mode = RDT_MODE_SHAREABLE; 3660 3661 out: 3662 rdt_staged_configs_clear(); 3663 return ret; 3664 } 3665 3666 static int mkdir_rdt_prepare_rmid_alloc(struct rdtgroup *rdtgrp) 3667 { 3668 int ret; 3669 3670 if (!resctrl_arch_mon_capable()) 3671 return 0; 3672 3673 ret = alloc_rmid(rdtgrp->closid); 3674 if (ret < 0) { 3675 rdt_last_cmd_puts("Out of RMIDs\n"); 3676 return ret; 3677 } 3678 rdtgrp->mon.rmid = ret; 3679 3680 rdtgroup_assign_cntrs(rdtgrp); 3681 3682 ret = mkdir_mondata_all(rdtgrp->kn, rdtgrp, &rdtgrp->mon.mon_data_kn); 3683 if (ret) { 3684 rdt_last_cmd_puts("kernfs subdir error\n"); 3685 rdtgroup_unassign_cntrs(rdtgrp); 3686 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 3687 return ret; 3688 } 3689 3690 return 0; 3691 } 3692 3693 static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp) 3694 { 3695 if (resctrl_arch_mon_capable()) { 3696 rdtgroup_unassign_cntrs(rgrp); 3697 free_rmid(rgrp->closid, rgrp->mon.rmid); 3698 } 3699 } 3700 3701 /* 3702 * We allow creating mon groups only with in a directory called "mon_groups" 3703 * which is present in every ctrl_mon group. Check if this is a valid 3704 * "mon_groups" directory. 3705 * 3706 * 1. The directory should be named "mon_groups". 3707 * 2. The mon group itself should "not" be named "mon_groups". 3708 * This makes sure "mon_groups" directory always has a ctrl_mon group 3709 * as parent. 3710 */ 3711 static bool is_mon_groups(struct kernfs_node *kn, const char *name) 3712 { 3713 return (!strcmp(rdt_kn_name(kn), "mon_groups") && 3714 strcmp(name, "mon_groups")); 3715 } 3716 3717 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, 3718 const char *name, umode_t mode, 3719 enum rdt_group_type rtype, struct rdtgroup **r) 3720 { 3721 struct rdtgroup *prdtgrp, *rdtgrp; 3722 unsigned long files = 0; 3723 struct kernfs_node *kn; 3724 int ret; 3725 3726 prdtgrp = rdtgroup_kn_lock_live(parent_kn); 3727 if (!prdtgrp) { 3728 ret = -ENODEV; 3729 goto out_unlock; 3730 } 3731 3732 rdt_last_cmd_clear(); 3733 3734 /* 3735 * Check that the parent directory for a monitor group is a "mon_groups" 3736 * directory. 3737 */ 3738 if (rtype == RDTMON_GROUP && !is_mon_groups(parent_kn, name)) { 3739 ret = -EPERM; 3740 goto out_unlock; 3741 } 3742 3743 if (rtype == RDTMON_GROUP && 3744 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3745 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) { 3746 ret = -EINVAL; 3747 rdt_last_cmd_puts("Pseudo-locking in progress\n"); 3748 goto out_unlock; 3749 } 3750 3751 /* allocate the rdtgroup. */ 3752 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); 3753 if (!rdtgrp) { 3754 ret = -ENOSPC; 3755 rdt_last_cmd_puts("Kernel out of memory\n"); 3756 goto out_unlock; 3757 } 3758 *r = rdtgrp; 3759 rdtgrp->mon.parent = prdtgrp; 3760 rdtgrp->type = rtype; 3761 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); 3762 3763 /* kernfs creates the directory for rdtgrp */ 3764 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); 3765 if (IS_ERR(kn)) { 3766 ret = PTR_ERR(kn); 3767 rdt_last_cmd_puts("kernfs create error\n"); 3768 goto out_free_rgrp; 3769 } 3770 rdtgrp->kn = kn; 3771 3772 /* 3773 * kernfs_remove() will drop the reference count on "kn" which 3774 * will free it. But we still need it to stick around for the 3775 * rdtgroup_kn_unlock(kn) call. Take one extra reference here, 3776 * which will be dropped by kernfs_put() in rdtgroup_remove(). 3777 */ 3778 kernfs_get(kn); 3779 3780 ret = rdtgroup_kn_set_ugid(kn); 3781 if (ret) { 3782 rdt_last_cmd_puts("kernfs perm error\n"); 3783 goto out_destroy; 3784 } 3785 3786 if (rtype == RDTCTRL_GROUP) { 3787 files = RFTYPE_BASE | RFTYPE_CTRL; 3788 if (resctrl_arch_mon_capable()) 3789 files |= RFTYPE_MON; 3790 } else { 3791 files = RFTYPE_BASE | RFTYPE_MON; 3792 } 3793 3794 ret = rdtgroup_add_files(kn, files); 3795 if (ret) { 3796 rdt_last_cmd_puts("kernfs fill error\n"); 3797 goto out_destroy; 3798 } 3799 3800 /* 3801 * The caller unlocks the parent_kn upon success. 3802 */ 3803 return 0; 3804 3805 out_destroy: 3806 kernfs_put(rdtgrp->kn); 3807 kernfs_remove(rdtgrp->kn); 3808 out_free_rgrp: 3809 kfree(rdtgrp); 3810 out_unlock: 3811 rdtgroup_kn_unlock(parent_kn); 3812 return ret; 3813 } 3814 3815 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) 3816 { 3817 kernfs_remove(rgrp->kn); 3818 rdtgroup_remove(rgrp); 3819 } 3820 3821 /* 3822 * Create a monitor group under "mon_groups" directory of a control 3823 * and monitor group(ctrl_mon). This is a resource group 3824 * to monitor a subset of tasks and cpus in its parent ctrl_mon group. 3825 */ 3826 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, 3827 const char *name, umode_t mode) 3828 { 3829 struct rdtgroup *rdtgrp, *prgrp; 3830 int ret; 3831 3832 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp); 3833 if (ret) 3834 return ret; 3835 3836 prgrp = rdtgrp->mon.parent; 3837 rdtgrp->closid = prgrp->closid; 3838 3839 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp); 3840 if (ret) { 3841 mkdir_rdt_prepare_clean(rdtgrp); 3842 goto out_unlock; 3843 } 3844 3845 kernfs_activate(rdtgrp->kn); 3846 3847 /* 3848 * Add the rdtgrp to the list of rdtgrps the parent 3849 * ctrl_mon group has to track. 3850 */ 3851 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); 3852 3853 out_unlock: 3854 rdtgroup_kn_unlock(parent_kn); 3855 return ret; 3856 } 3857 3858 /* 3859 * These are rdtgroups created under the root directory. Can be used 3860 * to allocate and monitor resources. 3861 */ 3862 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, 3863 const char *name, umode_t mode) 3864 { 3865 struct rdtgroup *rdtgrp; 3866 struct kernfs_node *kn; 3867 u32 closid; 3868 int ret; 3869 3870 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp); 3871 if (ret) 3872 return ret; 3873 3874 kn = rdtgrp->kn; 3875 ret = closid_alloc(); 3876 if (ret < 0) { 3877 rdt_last_cmd_puts("Out of CLOSIDs\n"); 3878 goto out_common_fail; 3879 } 3880 closid = ret; 3881 ret = 0; 3882 3883 rdtgrp->closid = closid; 3884 3885 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp); 3886 if (ret) 3887 goto out_closid_free; 3888 3889 kernfs_activate(rdtgrp->kn); 3890 3891 ret = rdtgroup_init_alloc(rdtgrp); 3892 if (ret < 0) 3893 goto out_rmid_free; 3894 3895 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); 3896 3897 if (resctrl_arch_mon_capable()) { 3898 /* 3899 * Create an empty mon_groups directory to hold the subset 3900 * of tasks and cpus to monitor. 3901 */ 3902 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL); 3903 if (ret) { 3904 rdt_last_cmd_puts("kernfs subdir error\n"); 3905 goto out_del_list; 3906 } 3907 if (is_mba_sc(NULL)) 3908 rdtgrp->mba_mbps_event = mba_mbps_default_event; 3909 } 3910 3911 goto out_unlock; 3912 3913 out_del_list: 3914 list_del(&rdtgrp->rdtgroup_list); 3915 out_rmid_free: 3916 mkdir_rdt_prepare_rmid_free(rdtgrp); 3917 out_closid_free: 3918 closid_free(closid); 3919 out_common_fail: 3920 mkdir_rdt_prepare_clean(rdtgrp); 3921 out_unlock: 3922 rdtgroup_kn_unlock(parent_kn); 3923 return ret; 3924 } 3925 3926 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, 3927 umode_t mode) 3928 { 3929 /* Do not accept '\n' to avoid unparsable situation. */ 3930 if (strchr(name, '\n')) 3931 return -EINVAL; 3932 3933 /* 3934 * If the parent directory is the root directory and RDT 3935 * allocation is supported, add a control and monitoring 3936 * subdirectory 3937 */ 3938 if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn) 3939 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode); 3940 3941 /* Else, attempt to add a monitoring subdirectory. */ 3942 if (resctrl_arch_mon_capable()) 3943 return rdtgroup_mkdir_mon(parent_kn, name, mode); 3944 3945 return -EPERM; 3946 } 3947 3948 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 3949 { 3950 struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 3951 u32 closid, rmid; 3952 int cpu; 3953 3954 /* Give any tasks back to the parent group */ 3955 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); 3956 3957 /* 3958 * Update per cpu closid/rmid of the moved CPUs first. 3959 * Note: the closid will not change, but the arch code still needs it. 3960 */ 3961 closid = prdtgrp->closid; 3962 rmid = prdtgrp->mon.rmid; 3963 for_each_cpu(cpu, &rdtgrp->cpu_mask) 3964 resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid); 3965 3966 /* 3967 * Update the MSR on moved CPUs and CPUs which have moved 3968 * task running on them. 3969 */ 3970 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 3971 update_closid_rmid(tmpmask, NULL); 3972 3973 rdtgrp->flags = RDT_DELETED; 3974 3975 rdtgroup_unassign_cntrs(rdtgrp); 3976 3977 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 3978 3979 /* 3980 * Remove the rdtgrp from the parent ctrl_mon group's list 3981 */ 3982 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 3983 list_del(&rdtgrp->mon.crdtgrp_list); 3984 3985 kernfs_remove(rdtgrp->kn); 3986 3987 return 0; 3988 } 3989 3990 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp) 3991 { 3992 rdtgrp->flags = RDT_DELETED; 3993 list_del(&rdtgrp->rdtgroup_list); 3994 3995 kernfs_remove(rdtgrp->kn); 3996 return 0; 3997 } 3998 3999 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 4000 { 4001 u32 closid, rmid; 4002 int cpu; 4003 4004 /* Give any tasks back to the default group */ 4005 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); 4006 4007 /* Give any CPUs back to the default group */ 4008 cpumask_or(&rdtgroup_default.cpu_mask, 4009 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 4010 4011 /* Update per cpu closid and rmid of the moved CPUs first */ 4012 closid = rdtgroup_default.closid; 4013 rmid = rdtgroup_default.mon.rmid; 4014 for_each_cpu(cpu, &rdtgrp->cpu_mask) 4015 resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid); 4016 4017 /* 4018 * Update the MSR on moved CPUs and CPUs which have moved 4019 * task running on them. 4020 */ 4021 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 4022 update_closid_rmid(tmpmask, NULL); 4023 4024 rdtgroup_unassign_cntrs(rdtgrp); 4025 4026 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 4027 closid_free(rdtgrp->closid); 4028 4029 rdtgroup_ctrl_remove(rdtgrp); 4030 4031 /* 4032 * Free all the child monitor group rmids. 4033 */ 4034 free_all_child_rdtgrp(rdtgrp); 4035 4036 return 0; 4037 } 4038 4039 static struct kernfs_node *rdt_kn_parent(struct kernfs_node *kn) 4040 { 4041 /* 4042 * Valid within the RCU section it was obtained or while rdtgroup_mutex 4043 * is held. 4044 */ 4045 return rcu_dereference_check(kn->__parent, lockdep_is_held(&rdtgroup_mutex)); 4046 } 4047 4048 static int rdtgroup_rmdir(struct kernfs_node *kn) 4049 { 4050 struct kernfs_node *parent_kn; 4051 struct rdtgroup *rdtgrp; 4052 cpumask_var_t tmpmask; 4053 int ret = 0; 4054 4055 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 4056 return -ENOMEM; 4057 4058 rdtgrp = rdtgroup_kn_lock_live(kn); 4059 if (!rdtgrp) { 4060 ret = -EPERM; 4061 goto out; 4062 } 4063 parent_kn = rdt_kn_parent(kn); 4064 4065 /* 4066 * If the rdtgroup is a ctrl_mon group and parent directory 4067 * is the root directory, remove the ctrl_mon group. 4068 * 4069 * If the rdtgroup is a mon group and parent directory 4070 * is a valid "mon_groups" directory, remove the mon group. 4071 */ 4072 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn && 4073 rdtgrp != &rdtgroup_default) { 4074 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 4075 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 4076 ret = rdtgroup_ctrl_remove(rdtgrp); 4077 } else { 4078 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask); 4079 } 4080 } else if (rdtgrp->type == RDTMON_GROUP && 4081 is_mon_groups(parent_kn, rdt_kn_name(kn))) { 4082 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask); 4083 } else { 4084 ret = -EPERM; 4085 } 4086 4087 out: 4088 rdtgroup_kn_unlock(kn); 4089 free_cpumask_var(tmpmask); 4090 return ret; 4091 } 4092 4093 /** 4094 * mongrp_reparent() - replace parent CTRL_MON group of a MON group 4095 * @rdtgrp: the MON group whose parent should be replaced 4096 * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp 4097 * @cpus: cpumask provided by the caller for use during this call 4098 * 4099 * Replaces the parent CTRL_MON group for a MON group, resulting in all member 4100 * tasks' CLOSID immediately changing to that of the new parent group. 4101 * Monitoring data for the group is unaffected by this operation. 4102 */ 4103 static void mongrp_reparent(struct rdtgroup *rdtgrp, 4104 struct rdtgroup *new_prdtgrp, 4105 cpumask_var_t cpus) 4106 { 4107 struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 4108 4109 WARN_ON(rdtgrp->type != RDTMON_GROUP); 4110 WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP); 4111 4112 /* Nothing to do when simply renaming a MON group. */ 4113 if (prdtgrp == new_prdtgrp) 4114 return; 4115 4116 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 4117 list_move_tail(&rdtgrp->mon.crdtgrp_list, 4118 &new_prdtgrp->mon.crdtgrp_list); 4119 4120 rdtgrp->mon.parent = new_prdtgrp; 4121 rdtgrp->closid = new_prdtgrp->closid; 4122 4123 /* Propagate updated closid to all tasks in this group. */ 4124 rdt_move_group_tasks(rdtgrp, rdtgrp, cpus); 4125 4126 update_closid_rmid(cpus, NULL); 4127 } 4128 4129 static int rdtgroup_rename(struct kernfs_node *kn, 4130 struct kernfs_node *new_parent, const char *new_name) 4131 { 4132 struct kernfs_node *kn_parent; 4133 struct rdtgroup *new_prdtgrp; 4134 struct rdtgroup *rdtgrp; 4135 cpumask_var_t tmpmask; 4136 int ret; 4137 4138 rdtgrp = kernfs_to_rdtgroup(kn); 4139 new_prdtgrp = kernfs_to_rdtgroup(new_parent); 4140 if (!rdtgrp || !new_prdtgrp) 4141 return -ENOENT; 4142 4143 /* Release both kernfs active_refs before obtaining rdtgroup mutex. */ 4144 rdtgroup_kn_get(rdtgrp, kn); 4145 rdtgroup_kn_get(new_prdtgrp, new_parent); 4146 4147 mutex_lock(&rdtgroup_mutex); 4148 4149 rdt_last_cmd_clear(); 4150 4151 /* 4152 * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if 4153 * either kernfs_node is a file. 4154 */ 4155 if (kernfs_type(kn) != KERNFS_DIR || 4156 kernfs_type(new_parent) != KERNFS_DIR) { 4157 rdt_last_cmd_puts("Source and destination must be directories"); 4158 ret = -EPERM; 4159 goto out; 4160 } 4161 4162 if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) { 4163 ret = -ENOENT; 4164 goto out; 4165 } 4166 4167 kn_parent = rdt_kn_parent(kn); 4168 if (rdtgrp->type != RDTMON_GROUP || !kn_parent || 4169 !is_mon_groups(kn_parent, rdt_kn_name(kn))) { 4170 rdt_last_cmd_puts("Source must be a MON group\n"); 4171 ret = -EPERM; 4172 goto out; 4173 } 4174 4175 if (!is_mon_groups(new_parent, new_name)) { 4176 rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n"); 4177 ret = -EPERM; 4178 goto out; 4179 } 4180 4181 /* 4182 * If the MON group is monitoring CPUs, the CPUs must be assigned to the 4183 * current parent CTRL_MON group and therefore cannot be assigned to 4184 * the new parent, making the move illegal. 4185 */ 4186 if (!cpumask_empty(&rdtgrp->cpu_mask) && 4187 rdtgrp->mon.parent != new_prdtgrp) { 4188 rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n"); 4189 ret = -EPERM; 4190 goto out; 4191 } 4192 4193 /* 4194 * Allocate the cpumask for use in mongrp_reparent() to avoid the 4195 * possibility of failing to allocate it after kernfs_rename() has 4196 * succeeded. 4197 */ 4198 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) { 4199 ret = -ENOMEM; 4200 goto out; 4201 } 4202 4203 /* 4204 * Perform all input validation and allocations needed to ensure 4205 * mongrp_reparent() will succeed before calling kernfs_rename(), 4206 * otherwise it would be necessary to revert this call if 4207 * mongrp_reparent() failed. 4208 */ 4209 ret = kernfs_rename(kn, new_parent, new_name); 4210 if (!ret) 4211 mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask); 4212 4213 free_cpumask_var(tmpmask); 4214 4215 out: 4216 mutex_unlock(&rdtgroup_mutex); 4217 rdtgroup_kn_put(rdtgrp, kn); 4218 rdtgroup_kn_put(new_prdtgrp, new_parent); 4219 return ret; 4220 } 4221 4222 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) 4223 { 4224 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3)) 4225 seq_puts(seq, ",cdp"); 4226 4227 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) 4228 seq_puts(seq, ",cdpl2"); 4229 4230 if (is_mba_sc(resctrl_arch_get_resource(RDT_RESOURCE_MBA))) 4231 seq_puts(seq, ",mba_MBps"); 4232 4233 if (resctrl_debug) 4234 seq_puts(seq, ",debug"); 4235 4236 return 0; 4237 } 4238 4239 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { 4240 .mkdir = rdtgroup_mkdir, 4241 .rmdir = rdtgroup_rmdir, 4242 .rename = rdtgroup_rename, 4243 .show_options = rdtgroup_show_options, 4244 }; 4245 4246 static int rdtgroup_setup_root(struct rdt_fs_context *ctx) 4247 { 4248 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, 4249 KERNFS_ROOT_CREATE_DEACTIVATED | 4250 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, 4251 &rdtgroup_default); 4252 if (IS_ERR(rdt_root)) 4253 return PTR_ERR(rdt_root); 4254 4255 ctx->kfc.root = rdt_root; 4256 rdtgroup_default.kn = kernfs_root_to_node(rdt_root); 4257 4258 return 0; 4259 } 4260 4261 static void rdtgroup_destroy_root(void) 4262 { 4263 lockdep_assert_held(&rdtgroup_mutex); 4264 4265 kernfs_destroy_root(rdt_root); 4266 rdtgroup_default.kn = NULL; 4267 } 4268 4269 static void rdtgroup_setup_default(void) 4270 { 4271 mutex_lock(&rdtgroup_mutex); 4272 4273 rdtgroup_default.closid = RESCTRL_RESERVED_CLOSID; 4274 rdtgroup_default.mon.rmid = RESCTRL_RESERVED_RMID; 4275 rdtgroup_default.type = RDTCTRL_GROUP; 4276 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); 4277 4278 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); 4279 4280 mutex_unlock(&rdtgroup_mutex); 4281 } 4282 4283 static void domain_destroy_l3_mon_state(struct rdt_l3_mon_domain *d) 4284 { 4285 int idx; 4286 4287 kfree(d->cntr_cfg); 4288 bitmap_free(d->rmid_busy_llc); 4289 for_each_mbm_idx(idx) { 4290 kfree(d->mbm_states[idx]); 4291 d->mbm_states[idx] = NULL; 4292 } 4293 } 4294 4295 void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d) 4296 { 4297 mutex_lock(&rdtgroup_mutex); 4298 4299 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) 4300 mba_sc_domain_destroy(r, d); 4301 4302 mutex_unlock(&rdtgroup_mutex); 4303 } 4304 4305 void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *hdr) 4306 { 4307 struct rdt_l3_mon_domain *d; 4308 4309 mutex_lock(&rdtgroup_mutex); 4310 4311 /* 4312 * If resctrl is mounted, remove all the 4313 * per domain monitor data directories. 4314 */ 4315 if (resctrl_mounted && resctrl_arch_mon_capable()) 4316 rmdir_mondata_subdir_allrdtgrp(r, hdr); 4317 4318 if (r->rid != RDT_RESOURCE_L3) 4319 goto out_unlock; 4320 4321 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 4322 goto out_unlock; 4323 4324 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 4325 if (resctrl_is_mbm_enabled()) 4326 cancel_delayed_work(&d->mbm_over); 4327 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && has_busy_rmid(d)) { 4328 /* 4329 * When a package is going down, forcefully 4330 * decrement rmid->ebusy. There is no way to know 4331 * that the L3 was flushed and hence may lead to 4332 * incorrect counts in rare scenarios, but leaving 4333 * the RMID as busy creates RMID leaks if the 4334 * package never comes back. 4335 */ 4336 __check_limbo(d, true); 4337 cancel_delayed_work(&d->cqm_limbo); 4338 } 4339 4340 domain_destroy_l3_mon_state(d); 4341 out_unlock: 4342 mutex_unlock(&rdtgroup_mutex); 4343 } 4344 4345 /** 4346 * domain_setup_l3_mon_state() - Initialise domain monitoring structures. 4347 * @r: The resource for the newly online domain. 4348 * @d: The newly online domain. 4349 * 4350 * Allocate monitor resources that belong to this domain. 4351 * Called when the first CPU of a domain comes online, regardless of whether 4352 * the filesystem is mounted. 4353 * During boot this may be called before global allocations have been made by 4354 * resctrl_l3_mon_resource_init(). 4355 * 4356 * Called during CPU online that may run as soon as CPU online callbacks 4357 * are set up during resctrl initialization. The number of supported RMIDs 4358 * may be reduced if additional mon_capable resources are enumerated 4359 * at mount time. This means the rdt_l3_mon_domain::mbm_states[] and 4360 * rdt_l3_mon_domain::rmid_busy_llc allocations may be larger than needed. 4361 * 4362 * Return: 0 for success, or -ENOMEM. 4363 */ 4364 static int domain_setup_l3_mon_state(struct rdt_resource *r, struct rdt_l3_mon_domain *d) 4365 { 4366 u32 idx_limit = resctrl_arch_system_num_rmid_idx(); 4367 size_t tsize = sizeof(*d->mbm_states[0]); 4368 enum resctrl_event_id eventid; 4369 int idx; 4370 4371 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) { 4372 d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL); 4373 if (!d->rmid_busy_llc) 4374 return -ENOMEM; 4375 } 4376 4377 for_each_mbm_event_id(eventid) { 4378 if (!resctrl_is_mon_event_enabled(eventid)) 4379 continue; 4380 idx = MBM_STATE_IDX(eventid); 4381 d->mbm_states[idx] = kcalloc(idx_limit, tsize, GFP_KERNEL); 4382 if (!d->mbm_states[idx]) 4383 goto cleanup; 4384 } 4385 4386 if (resctrl_is_mbm_enabled() && r->mon.mbm_cntr_assignable) { 4387 tsize = sizeof(*d->cntr_cfg); 4388 d->cntr_cfg = kcalloc(r->mon.num_mbm_cntrs, tsize, GFP_KERNEL); 4389 if (!d->cntr_cfg) 4390 goto cleanup; 4391 } 4392 4393 return 0; 4394 cleanup: 4395 bitmap_free(d->rmid_busy_llc); 4396 for_each_mbm_idx(idx) { 4397 kfree(d->mbm_states[idx]); 4398 d->mbm_states[idx] = NULL; 4399 } 4400 4401 return -ENOMEM; 4402 } 4403 4404 int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d) 4405 { 4406 int err = 0; 4407 4408 mutex_lock(&rdtgroup_mutex); 4409 4410 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) { 4411 /* RDT_RESOURCE_MBA is never mon_capable */ 4412 err = mba_sc_domain_allocate(r, d); 4413 } 4414 4415 mutex_unlock(&rdtgroup_mutex); 4416 4417 return err; 4418 } 4419 4420 int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *hdr) 4421 { 4422 struct rdt_l3_mon_domain *d; 4423 int err = -EINVAL; 4424 4425 mutex_lock(&rdtgroup_mutex); 4426 4427 if (r->rid != RDT_RESOURCE_L3) 4428 goto mkdir; 4429 4430 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 4431 goto out_unlock; 4432 4433 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 4434 err = domain_setup_l3_mon_state(r, d); 4435 if (err) 4436 goto out_unlock; 4437 4438 if (resctrl_is_mbm_enabled()) { 4439 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow); 4440 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL, 4441 RESCTRL_PICK_ANY_CPU); 4442 } 4443 4444 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) 4445 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo); 4446 4447 mkdir: 4448 err = 0; 4449 /* 4450 * If the filesystem is not mounted then only the default resource group 4451 * exists. Creation of its directories is deferred until mount time 4452 * by rdt_get_tree() calling mkdir_mondata_all(). 4453 * If resctrl is mounted, add per domain monitor data directories. 4454 */ 4455 if (resctrl_mounted && resctrl_arch_mon_capable()) 4456 mkdir_mondata_subdir_allrdtgrp(r, hdr); 4457 4458 out_unlock: 4459 mutex_unlock(&rdtgroup_mutex); 4460 4461 return err; 4462 } 4463 4464 void resctrl_online_cpu(unsigned int cpu) 4465 { 4466 mutex_lock(&rdtgroup_mutex); 4467 /* The CPU is set in default rdtgroup after online. */ 4468 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask); 4469 mutex_unlock(&rdtgroup_mutex); 4470 } 4471 4472 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu) 4473 { 4474 struct rdtgroup *cr; 4475 4476 list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) { 4477 if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) 4478 break; 4479 } 4480 } 4481 4482 static struct rdt_l3_mon_domain *get_mon_domain_from_cpu(int cpu, 4483 struct rdt_resource *r) 4484 { 4485 struct rdt_l3_mon_domain *d; 4486 4487 lockdep_assert_cpus_held(); 4488 4489 list_for_each_entry(d, &r->mon_domains, hdr.list) { 4490 /* Find the domain that contains this CPU */ 4491 if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask)) 4492 return d; 4493 } 4494 4495 return NULL; 4496 } 4497 4498 void resctrl_offline_cpu(unsigned int cpu) 4499 { 4500 struct rdt_resource *l3 = resctrl_arch_get_resource(RDT_RESOURCE_L3); 4501 struct rdt_l3_mon_domain *d; 4502 struct rdtgroup *rdtgrp; 4503 4504 mutex_lock(&rdtgroup_mutex); 4505 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 4506 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) { 4507 clear_childcpus(rdtgrp, cpu); 4508 break; 4509 } 4510 } 4511 4512 if (!l3->mon_capable) 4513 goto out_unlock; 4514 4515 d = get_mon_domain_from_cpu(cpu, l3); 4516 if (d) { 4517 if (resctrl_is_mbm_enabled() && cpu == d->mbm_work_cpu) { 4518 cancel_delayed_work(&d->mbm_over); 4519 mbm_setup_overflow_handler(d, 0, cpu); 4520 } 4521 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && 4522 cpu == d->cqm_work_cpu && has_busy_rmid(d)) { 4523 cancel_delayed_work(&d->cqm_limbo); 4524 cqm_setup_limbo_handler(d, 0, cpu); 4525 } 4526 } 4527 4528 out_unlock: 4529 mutex_unlock(&rdtgroup_mutex); 4530 } 4531 4532 /* 4533 * resctrl_init - resctrl filesystem initialization 4534 * 4535 * Setup resctrl file system including set up root, create mount point, 4536 * register resctrl filesystem, and initialize files under root directory. 4537 * 4538 * Return: 0 on success or -errno 4539 */ 4540 int resctrl_init(void) 4541 { 4542 int ret = 0; 4543 4544 seq_buf_init(&last_cmd_status, last_cmd_status_buf, 4545 sizeof(last_cmd_status_buf)); 4546 4547 rdtgroup_setup_default(); 4548 4549 thread_throttle_mode_init(); 4550 4551 io_alloc_init(); 4552 4553 ret = resctrl_l3_mon_resource_init(); 4554 if (ret) 4555 return ret; 4556 4557 ret = sysfs_create_mount_point(fs_kobj, "resctrl"); 4558 if (ret) { 4559 resctrl_l3_mon_resource_exit(); 4560 return ret; 4561 } 4562 4563 ret = register_filesystem(&rdt_fs_type); 4564 if (ret) 4565 goto cleanup_mountpoint; 4566 4567 /* 4568 * Adding the resctrl debugfs directory here may not be ideal since 4569 * it would let the resctrl debugfs directory appear on the debugfs 4570 * filesystem before the resctrl filesystem is mounted. 4571 * It may also be ok since that would enable debugging of RDT before 4572 * resctrl is mounted. 4573 * The reason why the debugfs directory is created here and not in 4574 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and 4575 * during the debugfs directory creation also &sb->s_type->i_mutex_key 4576 * (the lockdep class of inode->i_rwsem). Other filesystem 4577 * interactions (eg. SyS_getdents) have the lock ordering: 4578 * &sb->s_type->i_mutex_key --> &mm->mmap_lock 4579 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex 4580 * is taken, thus creating dependency: 4581 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause 4582 * issues considering the other two lock dependencies. 4583 * By creating the debugfs directory here we avoid a dependency 4584 * that may cause deadlock (even though file operations cannot 4585 * occur until the filesystem is mounted, but I do not know how to 4586 * tell lockdep that). 4587 */ 4588 debugfs_resctrl = debugfs_create_dir("resctrl", NULL); 4589 4590 return 0; 4591 4592 cleanup_mountpoint: 4593 sysfs_remove_mount_point(fs_kobj, "resctrl"); 4594 resctrl_l3_mon_resource_exit(); 4595 4596 return ret; 4597 } 4598 4599 static bool resctrl_online_domains_exist(void) 4600 { 4601 struct rdt_resource *r; 4602 4603 /* 4604 * Only walk capable resources to allow resctrl_arch_get_resource() 4605 * to return dummy 'not capable' resources. 4606 */ 4607 for_each_alloc_capable_rdt_resource(r) { 4608 if (!list_empty(&r->ctrl_domains)) 4609 return true; 4610 } 4611 4612 for_each_mon_capable_rdt_resource(r) { 4613 if (!list_empty(&r->mon_domains)) 4614 return true; 4615 } 4616 4617 return false; 4618 } 4619 4620 /** 4621 * resctrl_exit() - Remove the resctrl filesystem and free resources. 4622 * 4623 * Called by the architecture code in response to a fatal error. 4624 * Removes resctrl files and structures from kernfs to prevent further 4625 * configuration. 4626 * 4627 * When called by the architecture code, all CPUs and resctrl domains must be 4628 * offline. This ensures the limbo and overflow handlers are not scheduled to 4629 * run, meaning the data structures they access can be freed by 4630 * resctrl_l3_mon_resource_exit(). 4631 * 4632 * After resctrl_exit() returns, the architecture code should return an 4633 * error from all resctrl_arch_ functions that can do this. 4634 * resctrl_arch_get_resource() must continue to return struct rdt_resources 4635 * with the correct rid field to ensure the filesystem can be unmounted. 4636 */ 4637 void resctrl_exit(void) 4638 { 4639 cpus_read_lock(); 4640 WARN_ON_ONCE(resctrl_online_domains_exist()); 4641 4642 mutex_lock(&rdtgroup_mutex); 4643 resctrl_fs_teardown(); 4644 mutex_unlock(&rdtgroup_mutex); 4645 4646 cpus_read_unlock(); 4647 4648 debugfs_remove_recursive(debugfs_resctrl); 4649 debugfs_resctrl = NULL; 4650 unregister_filesystem(&rdt_fs_type); 4651 4652 /* 4653 * Do not remove the sysfs mount point added by resctrl_init() so that 4654 * it can be used to umount resctrl. 4655 */ 4656 4657 resctrl_l3_mon_resource_exit(); 4658 } 4659