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