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