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 (resctrl_arch_alloc_capable() && (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 ret, 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 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix); 2886 if (ret >= sizeof(s->name)) { 2887 kfree(s); 2888 return -EINVAL; 2889 } 2890 2891 cl = strlen(s->name); 2892 2893 /* 2894 * If CDP is supported by this resource, but not enabled, 2895 * include the suffix. This ensures the tabular format of the 2896 * schemata file does not change between mounts of the filesystem. 2897 */ 2898 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid)) 2899 cl += 4; 2900 2901 if (cl > max_name_width) 2902 max_name_width = cl; 2903 2904 switch (r->schema_fmt) { 2905 case RESCTRL_SCHEMA_BITMAP: 2906 s->fmt_str = "%d=%x"; 2907 break; 2908 case RESCTRL_SCHEMA_RANGE: 2909 s->fmt_str = "%d=%u"; 2910 break; 2911 } 2912 2913 if (WARN_ON_ONCE(!s->fmt_str)) { 2914 kfree(s); 2915 return -EINVAL; 2916 } 2917 2918 INIT_LIST_HEAD(&s->list); 2919 list_add(&s->list, &resctrl_schema_all); 2920 2921 return 0; 2922 } 2923 2924 static int schemata_list_create(void) 2925 { 2926 struct rdt_resource *r; 2927 int ret = 0; 2928 2929 for_each_alloc_capable_rdt_resource(r) { 2930 if (resctrl_arch_get_cdp_enabled(r->rid)) { 2931 ret = schemata_list_add(r, CDP_CODE); 2932 if (ret) 2933 break; 2934 2935 ret = schemata_list_add(r, CDP_DATA); 2936 } else { 2937 ret = schemata_list_add(r, CDP_NONE); 2938 } 2939 2940 if (ret) 2941 break; 2942 } 2943 2944 return ret; 2945 } 2946 2947 static void schemata_list_destroy(void) 2948 { 2949 struct resctrl_schema *s, *tmp; 2950 2951 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) { 2952 list_del(&s->list); 2953 kfree(s); 2954 } 2955 } 2956 2957 /* 2958 * Move tasks from one to the other group. If @from is NULL, then all tasks 2959 * in the systems are moved unconditionally (used for teardown). 2960 * 2961 * If @mask is not NULL the cpus on which moved tasks are running are set 2962 * in that mask so the update smp function call is restricted to affected 2963 * cpus. 2964 */ 2965 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, 2966 struct cpumask *mask) 2967 { 2968 struct task_struct *p, *t; 2969 2970 read_lock(&tasklist_lock); 2971 for_each_process_thread(p, t) { 2972 if (!from || is_closid_match(t, from) || 2973 is_rmid_match(t, from)) { 2974 resctrl_arch_set_closid_rmid(t, to->closid, 2975 to->mon.rmid); 2976 2977 /* 2978 * Order the closid/rmid stores above before the loads 2979 * in task_curr(). This pairs with the full barrier 2980 * between the rq->curr update and 2981 * resctrl_arch_sched_in() during context switch. 2982 */ 2983 smp_mb(); 2984 2985 /* 2986 * If the task is on a CPU, set the CPU in the mask. 2987 * The detection is inaccurate as tasks might move or 2988 * schedule before the smp function call takes place. 2989 * In such a case the function call is pointless, but 2990 * there is no other side effect. 2991 */ 2992 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t)) 2993 cpumask_set_cpu(task_cpu(t), mask); 2994 } 2995 } 2996 read_unlock(&tasklist_lock); 2997 } 2998 2999 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) 3000 { 3001 struct rdtgroup *sentry, *stmp; 3002 struct list_head *head; 3003 3004 head = &rdtgrp->mon.crdtgrp_list; 3005 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { 3006 rdtgroup_unassign_cntrs(sentry); 3007 free_rmid(sentry->closid, sentry->mon.rmid); 3008 list_del(&sentry->mon.crdtgrp_list); 3009 3010 if (atomic_read(&sentry->waitcount) != 0) 3011 sentry->flags = RDT_DELETED; 3012 else 3013 rdtgroup_remove(sentry); 3014 } 3015 } 3016 3017 /* 3018 * Forcibly remove all of subdirectories under root. 3019 */ 3020 static void rmdir_all_sub(void) 3021 { 3022 struct rdtgroup *rdtgrp, *tmp; 3023 3024 /* Move all tasks to the default resource group */ 3025 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); 3026 3027 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { 3028 /* Free any child rmids */ 3029 free_all_child_rdtgrp(rdtgrp); 3030 3031 /* Remove each rdtgroup other than root */ 3032 if (rdtgrp == &rdtgroup_default) 3033 continue; 3034 3035 /* 3036 * Give any CPUs back to the default group. We cannot copy 3037 * cpu_online_mask because a CPU might have executed the 3038 * offline callback already, but is still marked online. 3039 */ 3040 cpumask_or(&rdtgroup_default.cpu_mask, 3041 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 3042 3043 rdtgroup_unassign_cntrs(rdtgrp); 3044 3045 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3046 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 3047 rdtgroup_pseudo_lock_remove(rdtgrp); 3048 } else { 3049 /* Pseudo-locked group's RMID is freed during setup. */ 3050 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 3051 } 3052 3053 kernfs_remove(rdtgrp->kn); 3054 list_del(&rdtgrp->rdtgroup_list); 3055 3056 if (atomic_read(&rdtgrp->waitcount) != 0) 3057 rdtgrp->flags = RDT_DELETED; 3058 else 3059 rdtgroup_remove(rdtgrp); 3060 } 3061 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ 3062 update_closid_rmid(cpu_online_mask, &rdtgroup_default); 3063 3064 kernfs_remove(kn_info); 3065 kernfs_remove(kn_mongrp); 3066 kernfs_remove(kn_mondata); 3067 } 3068 3069 /** 3070 * mon_get_kn_priv() - Get the mon_data priv data for this event. 3071 * 3072 * The same values are used across the mon_data directories of all control and 3073 * monitor groups for the same event in the same domain. Keep a list of 3074 * allocated structures and re-use an existing one with the same values for 3075 * @rid, @domid, etc. 3076 * 3077 * @rid: The resource id for the event file being created. 3078 * @domid: The domain id for the event file being created. 3079 * @mevt: The type of event file being created. 3080 * @do_sum: Whether SNC summing monitors are being created. Only set 3081 * when @rid == RDT_RESOURCE_L3. 3082 * 3083 * Return: Pointer to mon_data private data of the event, NULL on failure. 3084 */ 3085 static struct mon_data *mon_get_kn_priv(enum resctrl_res_level rid, int domid, 3086 struct mon_evt *mevt, 3087 bool do_sum) 3088 { 3089 struct mon_data *priv; 3090 3091 lockdep_assert_held(&rdtgroup_mutex); 3092 3093 list_for_each_entry(priv, &mon_data_kn_priv_list, list) { 3094 if (priv->rid == rid && priv->domid == domid && 3095 priv->sum == do_sum && priv->evt == mevt) 3096 return priv; 3097 } 3098 3099 priv = kzalloc_obj(*priv); 3100 if (!priv) 3101 return NULL; 3102 3103 priv->rid = rid; 3104 priv->domid = domid; 3105 priv->sum = do_sum; 3106 priv->evt = mevt; 3107 list_add_tail(&priv->list, &mon_data_kn_priv_list); 3108 3109 return priv; 3110 } 3111 3112 /** 3113 * mon_put_kn_priv() - Free all allocated mon_data structures. 3114 * 3115 * Called when resctrl file system is unmounted. 3116 */ 3117 static void mon_put_kn_priv(void) 3118 { 3119 struct mon_data *priv, *tmp; 3120 3121 lockdep_assert_held(&rdtgroup_mutex); 3122 3123 list_for_each_entry_safe(priv, tmp, &mon_data_kn_priv_list, list) { 3124 list_del(&priv->list); 3125 kfree(priv); 3126 } 3127 } 3128 3129 static void resctrl_fs_teardown(void) 3130 { 3131 lockdep_assert_held(&rdtgroup_mutex); 3132 3133 /* Cleared by rdtgroup_destroy_root() */ 3134 if (!rdtgroup_default.kn) 3135 return; 3136 3137 rmdir_all_sub(); 3138 rdtgroup_unassign_cntrs(&rdtgroup_default); 3139 mon_put_kn_priv(); 3140 rdt_pseudo_lock_release(); 3141 rdtgroup_default.mode = RDT_MODE_SHAREABLE; 3142 rdtgroup_default.flags = RDT_DELETED; 3143 closid_exit(); 3144 schemata_list_destroy(); 3145 rdtgroup_destroy_root(); 3146 } 3147 3148 static void resctrl_unmount(void) 3149 { 3150 struct rdt_resource *r; 3151 3152 cpus_read_lock(); 3153 mutex_lock(&rdtgroup_mutex); 3154 3155 rdt_disable_ctx(); 3156 3157 /* Put everything back to default values. */ 3158 for_each_alloc_capable_rdt_resource(r) 3159 resctrl_arch_reset_all_ctrls(r); 3160 3161 resctrl_fs_teardown(); 3162 if (resctrl_arch_alloc_capable()) 3163 resctrl_arch_disable_alloc(); 3164 if (resctrl_arch_mon_capable()) 3165 resctrl_arch_disable_mon(); 3166 resctrl_mounted = false; 3167 mutex_unlock(&rdtgroup_mutex); 3168 cpus_read_unlock(); 3169 } 3170 3171 static int rdt_get_tree(struct fs_context *fc) 3172 { 3173 struct rdt_fs_context *ctx = rdt_fc2context(fc); 3174 unsigned long flags = RFTYPE_CTRL_BASE; 3175 struct kernfs_node *rdt_root_kn; 3176 struct rdt_l3_mon_domain *dom; 3177 struct rdt_resource *r; 3178 int ret; 3179 3180 DO_ONCE_SLEEPABLE(resctrl_arch_pre_mount); 3181 3182 cpus_read_lock(); 3183 mutex_lock(&rdtgroup_mutex); 3184 /* 3185 * resctrl file system can only be mounted once. 3186 */ 3187 if (resctrl_mounted) { 3188 ret = -EBUSY; 3189 goto out; 3190 } 3191 3192 /* Avoid races from pending operations from a previous mount */ 3193 if (atomic_read(&rdtgroup_default.waitcount) != 0) { 3194 ret = -EBUSY; 3195 goto out; 3196 } 3197 3198 ret = setup_rmid_lru_list(); 3199 if (ret) 3200 goto out; 3201 3202 ret = rdtgroup_setup_root(ctx); 3203 if (ret) 3204 goto out; 3205 3206 ret = rdt_enable_ctx(ctx); 3207 if (ret) 3208 goto out_root; 3209 3210 ret = schemata_list_create(); 3211 if (ret) 3212 goto out_schemata_free; 3213 3214 ret = closid_init(); 3215 if (ret) 3216 goto out_schemata_free; 3217 3218 if (resctrl_arch_mon_capable()) 3219 flags |= RFTYPE_MON; 3220 3221 ret = rdtgroup_add_files(rdtgroup_default.kn, flags); 3222 if (ret) 3223 goto out_closid_exit; 3224 3225 kernfs_activate(rdtgroup_default.kn); 3226 3227 ret = rdtgroup_create_info_dir(rdtgroup_default.kn); 3228 if (ret < 0) 3229 goto out_closid_exit; 3230 3231 if (resctrl_arch_mon_capable()) { 3232 ret = mongroup_create_dir(rdtgroup_default.kn, 3233 &rdtgroup_default, "mon_groups", 3234 &kn_mongrp); 3235 if (ret < 0) 3236 goto out_info; 3237 3238 rdtgroup_assign_cntrs(&rdtgroup_default); 3239 3240 ret = mkdir_mondata_all(rdtgroup_default.kn, 3241 &rdtgroup_default, &kn_mondata); 3242 if (ret < 0) 3243 goto out_mongrp; 3244 rdtgroup_default.mon.mon_data_kn = kn_mondata; 3245 } 3246 3247 ret = rdt_pseudo_lock_init(); 3248 if (ret) 3249 goto out_mondata; 3250 3251 if (resctrl_arch_alloc_capable()) 3252 resctrl_arch_enable_alloc(); 3253 if (resctrl_arch_mon_capable()) 3254 resctrl_arch_enable_mon(); 3255 3256 if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable()) 3257 resctrl_mounted = true; 3258 3259 if (resctrl_is_mbm_enabled()) { 3260 r = resctrl_arch_get_resource(RDT_RESOURCE_L3); 3261 list_for_each_entry_rcu(dom, &r->mon_domains, hdr.list, lockdep_is_cpus_held()) 3262 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL, 3263 RESCTRL_PICK_ANY_CPU); 3264 } 3265 3266 /* 3267 * Ensure root remains accessible after mutex is unlocked so that 3268 * kernfs_kill_sb() can run safely if called by kernfs_get_tree()'s 3269 * failure path after creating a superblock but before taking reference 3270 * on root kn (for example, if unable to get inode for root kn). 3271 */ 3272 kernfs_get(rdtgroup_default.kn); 3273 3274 /* 3275 * Make backup of the current root kn being created to be used in 3276 * kernfs_put(). The additional reference taken above will prevent the 3277 * kn from being freed before kernfs_kill_sb() can run but 3278 * rdtgroup_default.kn may be set to NULL via rdtgroup_destroy_root() 3279 * and its backing root (rdt_root) could be overwritten before 3280 * kernfs_put() can run. 3281 */ 3282 rdt_root_kn = rdtgroup_default.kn; 3283 3284 rdt_last_cmd_clear(); 3285 mutex_unlock(&rdtgroup_mutex); 3286 cpus_read_unlock(); 3287 3288 ret = kernfs_get_tree(fc); 3289 /* 3290 * resctrl can only be mounted once, new superblock only expected 3291 * to be created once. 3292 */ 3293 if (!ctx->kfc.new_sb_created) 3294 resctrl_unmount(); 3295 kernfs_put(rdt_root_kn); 3296 return ret; 3297 3298 out_mondata: 3299 if (resctrl_arch_mon_capable()) 3300 kernfs_remove(kn_mondata); 3301 out_mongrp: 3302 if (resctrl_arch_mon_capable()) { 3303 mon_put_kn_priv(); 3304 rdtgroup_unassign_cntrs(&rdtgroup_default); 3305 kernfs_remove(kn_mongrp); 3306 } 3307 out_info: 3308 kernfs_remove(kn_info); 3309 out_closid_exit: 3310 closid_exit(); 3311 out_schemata_free: 3312 schemata_list_destroy(); 3313 rdt_disable_ctx(); 3314 out_root: 3315 rdtgroup_destroy_root(); 3316 out: 3317 mutex_unlock(&rdtgroup_mutex); 3318 cpus_read_unlock(); 3319 return ret; 3320 } 3321 3322 enum rdt_param { 3323 Opt_cdp, 3324 Opt_cdpl2, 3325 Opt_mba_mbps, 3326 Opt_debug, 3327 nr__rdt_params 3328 }; 3329 3330 static const struct fs_parameter_spec rdt_fs_parameters[] = { 3331 fsparam_flag("cdp", Opt_cdp), 3332 fsparam_flag("cdpl2", Opt_cdpl2), 3333 fsparam_flag("mba_MBps", Opt_mba_mbps), 3334 fsparam_flag("debug", Opt_debug), 3335 {} 3336 }; 3337 3338 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param) 3339 { 3340 struct rdt_fs_context *ctx = rdt_fc2context(fc); 3341 struct fs_parse_result result; 3342 const char *msg; 3343 int opt; 3344 3345 opt = fs_parse(fc, rdt_fs_parameters, param, &result); 3346 if (opt < 0) 3347 return opt; 3348 3349 switch (opt) { 3350 case Opt_cdp: 3351 ctx->enable_cdpl3 = true; 3352 return 0; 3353 case Opt_cdpl2: 3354 ctx->enable_cdpl2 = true; 3355 return 0; 3356 case Opt_mba_mbps: 3357 msg = "mba_MBps requires MBM (mbm_event mode not supported) and linear scale MBA at L3 scope"; 3358 if (!supports_mba_mbps()) 3359 return invalfc(fc, msg); 3360 ctx->enable_mba_mbps = true; 3361 return 0; 3362 case Opt_debug: 3363 ctx->enable_debug = true; 3364 return 0; 3365 } 3366 3367 return -EINVAL; 3368 } 3369 3370 static void rdt_fs_context_free(struct fs_context *fc) 3371 { 3372 struct rdt_fs_context *ctx = rdt_fc2context(fc); 3373 3374 kernfs_free_fs_context(fc); 3375 kfree(ctx); 3376 } 3377 3378 static const struct fs_context_operations rdt_fs_context_ops = { 3379 .free = rdt_fs_context_free, 3380 .parse_param = rdt_parse_param, 3381 .get_tree = rdt_get_tree, 3382 }; 3383 3384 static int rdt_init_fs_context(struct fs_context *fc) 3385 { 3386 struct rdt_fs_context *ctx; 3387 3388 ctx = kzalloc_obj(*ctx); 3389 if (!ctx) 3390 return -ENOMEM; 3391 3392 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC; 3393 fc->fs_private = &ctx->kfc; 3394 fc->ops = &rdt_fs_context_ops; 3395 put_user_ns(fc->user_ns); 3396 fc->user_ns = get_user_ns(&init_user_ns); 3397 fc->global = true; 3398 return 0; 3399 } 3400 3401 static void rdt_kill_sb(struct super_block *sb) 3402 { 3403 resctrl_unmount(); 3404 kernfs_kill_sb(sb); 3405 } 3406 3407 static struct file_system_type rdt_fs_type = { 3408 .name = "resctrl", 3409 .init_fs_context = rdt_init_fs_context, 3410 .parameters = rdt_fs_parameters, 3411 .kill_sb = rdt_kill_sb, 3412 }; 3413 3414 static int mon_addfile(struct kernfs_node *parent_kn, const char *name, 3415 void *priv) 3416 { 3417 struct kernfs_node *kn; 3418 int ret = 0; 3419 3420 kn = __kernfs_create_file(parent_kn, name, 0444, 3421 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, 3422 &kf_mondata_ops, priv, NULL, NULL); 3423 if (IS_ERR(kn)) 3424 return PTR_ERR(kn); 3425 3426 ret = rdtgroup_kn_set_ugid(kn); 3427 if (ret) { 3428 kernfs_remove(kn); 3429 return ret; 3430 } 3431 3432 return ret; 3433 } 3434 3435 static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname) 3436 { 3437 struct kernfs_node *kn; 3438 3439 kn = kernfs_find_and_get(pkn, name); 3440 if (!kn) 3441 return; 3442 kernfs_put(kn); 3443 3444 if (kn->dir.subdirs <= 1) 3445 kernfs_remove(kn); 3446 else 3447 kernfs_remove_by_name(kn, subname); 3448 } 3449 3450 /* 3451 * Remove files and directories for one SNC node. If it is the last node 3452 * sharing an L3 cache, then remove the upper level directory containing 3453 * the "sum" files too. 3454 */ 3455 static void rmdir_mondata_subdir_allrdtgrp_snc(struct rdt_resource *r, 3456 struct rdt_domain_hdr *hdr) 3457 { 3458 struct rdtgroup *prgrp, *crgrp; 3459 struct rdt_l3_mon_domain *d; 3460 char subname[32]; 3461 char name[32]; 3462 3463 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 3464 return; 3465 3466 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 3467 sprintf(name, "mon_%s_%02d", r->name, d->ci_id); 3468 sprintf(subname, "mon_sub_%s_%02d", r->name, hdr->id); 3469 3470 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3471 mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname); 3472 3473 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 3474 mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname); 3475 } 3476 } 3477 3478 /* 3479 * Remove all subdirectories of mon_data of ctrl_mon groups 3480 * and monitor groups for the given domain. 3481 */ 3482 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 3483 struct rdt_domain_hdr *hdr) 3484 { 3485 struct rdtgroup *prgrp, *crgrp; 3486 char name[32]; 3487 3488 if (r->rid == RDT_RESOURCE_L3 && r->mon_scope == RESCTRL_L3_NODE) { 3489 rmdir_mondata_subdir_allrdtgrp_snc(r, hdr); 3490 return; 3491 } 3492 3493 sprintf(name, "mon_%s_%02d", r->name, hdr->id); 3494 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3495 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); 3496 3497 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 3498 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); 3499 } 3500 } 3501 3502 /* 3503 * Create a directory for a domain and populate it with monitor files. Create 3504 * summing monitors when @hdr is NULL. No need to initialize summing monitors. 3505 */ 3506 static struct kernfs_node *_mkdir_mondata_subdir(struct kernfs_node *parent_kn, char *name, 3507 struct rdt_domain_hdr *hdr, 3508 struct rdt_resource *r, 3509 struct rdtgroup *prgrp, int domid) 3510 { 3511 struct rmid_read rr = {0}; 3512 struct kernfs_node *kn; 3513 struct mon_data *priv; 3514 struct mon_evt *mevt; 3515 int ret; 3516 3517 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 3518 if (IS_ERR(kn)) 3519 return kn; 3520 3521 ret = rdtgroup_kn_set_ugid(kn); 3522 if (ret) 3523 goto out_destroy; 3524 3525 for_each_mon_event(mevt) { 3526 if (mevt->rid != r->rid || !mevt->enabled) 3527 continue; 3528 priv = mon_get_kn_priv(r->rid, domid, mevt, !hdr); 3529 if (WARN_ON_ONCE(!priv)) { 3530 ret = -EINVAL; 3531 goto out_destroy; 3532 } 3533 3534 ret = mon_addfile(kn, mevt->name, priv); 3535 if (ret) 3536 goto out_destroy; 3537 3538 if (hdr && resctrl_is_mbm_event(mevt->evtid)) 3539 mon_event_read(&rr, r, hdr, prgrp, &hdr->cpu_mask, mevt, true); 3540 } 3541 3542 return kn; 3543 out_destroy: 3544 kernfs_remove(kn); 3545 return ERR_PTR(ret); 3546 } 3547 3548 static int mkdir_mondata_subdir_snc(struct kernfs_node *parent_kn, 3549 struct rdt_domain_hdr *hdr, 3550 struct rdt_resource *r, struct rdtgroup *prgrp) 3551 { 3552 struct kernfs_node *ckn, *kn; 3553 struct rdt_l3_mon_domain *d; 3554 char name[32]; 3555 3556 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 3557 return -EINVAL; 3558 3559 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 3560 sprintf(name, "mon_%s_%02d", r->name, d->ci_id); 3561 kn = kernfs_find_and_get(parent_kn, name); 3562 if (kn) { 3563 /* 3564 * rdtgroup_mutex will prevent this directory from being 3565 * removed. No need to keep this hold. 3566 */ 3567 kernfs_put(kn); 3568 } else { 3569 kn = _mkdir_mondata_subdir(parent_kn, name, NULL, r, prgrp, d->ci_id); 3570 if (IS_ERR(kn)) 3571 return PTR_ERR(kn); 3572 } 3573 3574 sprintf(name, "mon_sub_%s_%02d", r->name, hdr->id); 3575 ckn = _mkdir_mondata_subdir(kn, name, hdr, r, prgrp, hdr->id); 3576 if (IS_ERR(ckn)) { 3577 kernfs_remove(kn); 3578 return PTR_ERR(ckn); 3579 } 3580 3581 kernfs_activate(kn); 3582 return 0; 3583 } 3584 3585 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, 3586 struct rdt_domain_hdr *hdr, 3587 struct rdt_resource *r, struct rdtgroup *prgrp) 3588 { 3589 struct kernfs_node *kn; 3590 char name[32]; 3591 3592 lockdep_assert_held(&rdtgroup_mutex); 3593 3594 if (r->rid == RDT_RESOURCE_L3 && r->mon_scope == RESCTRL_L3_NODE) 3595 return mkdir_mondata_subdir_snc(parent_kn, hdr, r, prgrp); 3596 3597 sprintf(name, "mon_%s_%02d", r->name, hdr->id); 3598 kn = _mkdir_mondata_subdir(parent_kn, name, hdr, r, prgrp, hdr->id); 3599 if (IS_ERR(kn)) 3600 return PTR_ERR(kn); 3601 3602 kernfs_activate(kn); 3603 return 0; 3604 } 3605 3606 /* 3607 * Add all subdirectories of mon_data for "ctrl_mon" groups 3608 * and "monitor" groups with given domain id. 3609 */ 3610 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 3611 struct rdt_domain_hdr *hdr) 3612 { 3613 struct kernfs_node *parent_kn; 3614 struct rdtgroup *prgrp, *crgrp; 3615 struct list_head *head; 3616 3617 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 3618 parent_kn = prgrp->mon.mon_data_kn; 3619 mkdir_mondata_subdir(parent_kn, hdr, r, prgrp); 3620 3621 head = &prgrp->mon.crdtgrp_list; 3622 list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 3623 parent_kn = crgrp->mon.mon_data_kn; 3624 mkdir_mondata_subdir(parent_kn, hdr, r, crgrp); 3625 } 3626 } 3627 } 3628 3629 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, 3630 struct rdt_resource *r, 3631 struct rdtgroup *prgrp) 3632 { 3633 struct rdt_domain_hdr *hdr; 3634 int ret; 3635 3636 /* Walking r->domains, ensure it can't race with cpuhp */ 3637 lockdep_assert_cpus_held(); 3638 3639 list_for_each_entry_rcu(hdr, &r->mon_domains, list, lockdep_is_cpus_held()) { 3640 ret = mkdir_mondata_subdir(parent_kn, hdr, r, prgrp); 3641 if (ret) 3642 return ret; 3643 } 3644 3645 return 0; 3646 } 3647 3648 /* 3649 * This creates a directory mon_data which contains the monitored data. 3650 * 3651 * mon_data has one directory for each domain which are named 3652 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data 3653 * with L3 domain looks as below: 3654 * ./mon_data: 3655 * mon_L3_00 3656 * mon_L3_01 3657 * mon_L3_02 3658 * ... 3659 * 3660 * Each domain directory has one file per event: 3661 * ./mon_L3_00/: 3662 * llc_occupancy 3663 * 3664 */ 3665 static int mkdir_mondata_all(struct kernfs_node *parent_kn, 3666 struct rdtgroup *prgrp, 3667 struct kernfs_node **dest_kn) 3668 { 3669 struct rdt_resource *r; 3670 struct kernfs_node *kn; 3671 int ret; 3672 3673 /* 3674 * Create the mon_data directory first. 3675 */ 3676 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn); 3677 if (ret) 3678 return ret; 3679 3680 if (dest_kn) 3681 *dest_kn = kn; 3682 3683 /* 3684 * Create the subdirectories for each domain. Note that all events 3685 * in a domain like L3 are grouped into a resource whose domain is L3 3686 */ 3687 for_each_mon_capable_rdt_resource(r) { 3688 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); 3689 if (ret) 3690 goto out_destroy; 3691 } 3692 3693 return 0; 3694 3695 out_destroy: 3696 kernfs_remove(kn); 3697 return ret; 3698 } 3699 3700 /** 3701 * cbm_ensure_valid - Enforce validity on provided CBM 3702 * @_val: Candidate CBM 3703 * @r: RDT resource to which the CBM belongs 3704 * 3705 * The provided CBM represents all cache portions available for use. This 3706 * may be represented by a bitmap that does not consist of contiguous ones 3707 * and thus be an invalid CBM. 3708 * Here the provided CBM is forced to be a valid CBM by only considering 3709 * the first set of contiguous bits as valid and clearing all bits. 3710 * The intention here is to provide a valid default CBM with which a new 3711 * resource group is initialized. The user can follow this with a 3712 * modification to the CBM if the default does not satisfy the 3713 * requirements. 3714 * 3715 * Return: A CBM that is valid for resource @r. 3716 */ 3717 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r) 3718 { 3719 unsigned int cbm_len = r->cache.cbm_len; 3720 unsigned long first_bit, zero_bit; 3721 unsigned long val; 3722 3723 if (!_val || r->cache.arch_has_sparse_bitmasks) 3724 return _val; 3725 3726 val = _val; 3727 first_bit = find_first_bit(&val, cbm_len); 3728 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit); 3729 3730 /* Clear any remaining bits to ensure contiguous region */ 3731 bitmap_clear(&val, zero_bit, cbm_len - zero_bit); 3732 return (u32)val; 3733 } 3734 3735 /* 3736 * Initialize cache resources per RDT domain 3737 * 3738 * Set the RDT domain up to start off with all usable allocations. That is, 3739 * all shareable and unused bits. All-zero CBM is invalid. 3740 */ 3741 static int __init_one_rdt_domain(struct rdt_ctrl_domain *d, struct resctrl_schema *s, 3742 u32 closid) 3743 { 3744 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type); 3745 enum resctrl_conf_type t = s->conf_type; 3746 struct resctrl_staged_config *cfg; 3747 struct rdt_resource *r = s->res; 3748 u32 used_b = 0, unused_b = 0; 3749 unsigned long tmp_cbm; 3750 enum rdtgrp_mode mode; 3751 u32 peer_ctl, ctrl_val; 3752 int i; 3753 3754 cfg = &d->staged_config[t]; 3755 cfg->have_new_ctrl = false; 3756 cfg->new_ctrl = r->cache.shareable_bits; 3757 used_b = r->cache.shareable_bits; 3758 for (i = 0; i < closids_supported(); i++) { 3759 if (closid_allocated(i) && i != closid) { 3760 mode = rdtgroup_mode_by_closid(i); 3761 if (mode == RDT_MODE_PSEUDO_LOCKSETUP) 3762 /* 3763 * ctrl values for locksetup aren't relevant 3764 * until the schemata is written, and the mode 3765 * becomes RDT_MODE_PSEUDO_LOCKED. 3766 */ 3767 continue; 3768 /* 3769 * If CDP is active include peer domain's 3770 * usage to ensure there is no overlap 3771 * with an exclusive group. 3772 */ 3773 if (resctrl_arch_get_cdp_enabled(r->rid)) 3774 peer_ctl = resctrl_arch_get_config(r, d, i, 3775 peer_type); 3776 else 3777 peer_ctl = 0; 3778 ctrl_val = resctrl_arch_get_config(r, d, i, 3779 s->conf_type); 3780 used_b |= ctrl_val | peer_ctl; 3781 if (mode == RDT_MODE_SHAREABLE) 3782 cfg->new_ctrl |= ctrl_val | peer_ctl; 3783 } 3784 } 3785 if (d->plr && d->plr->cbm > 0) 3786 used_b |= d->plr->cbm; 3787 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); 3788 unused_b &= BIT_MASK(r->cache.cbm_len) - 1; 3789 cfg->new_ctrl |= unused_b; 3790 /* 3791 * Force the initial CBM to be valid, user can 3792 * modify the CBM based on system availability. 3793 */ 3794 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r); 3795 /* 3796 * Assign the u32 CBM to an unsigned long to ensure that 3797 * bitmap_weight() does not access out-of-bound memory. 3798 */ 3799 tmp_cbm = cfg->new_ctrl; 3800 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) { 3801 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->hdr.id); 3802 return -ENOSPC; 3803 } 3804 cfg->have_new_ctrl = true; 3805 3806 return 0; 3807 } 3808 3809 /* 3810 * Initialize cache resources with default values. 3811 * 3812 * A new RDT group is being created on an allocation capable (CAT) 3813 * supporting system. Set this group up to start off with all usable 3814 * allocations. 3815 * 3816 * If there are no more shareable bits available on any domain then 3817 * the entire allocation will fail. 3818 */ 3819 int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid) 3820 { 3821 struct rdt_ctrl_domain *d; 3822 int ret; 3823 3824 list_for_each_entry_rcu(d, &s->res->ctrl_domains, hdr.list, lockdep_is_cpus_held()) { 3825 ret = __init_one_rdt_domain(d, s, closid); 3826 if (ret < 0) 3827 return ret; 3828 } 3829 3830 return 0; 3831 } 3832 3833 /* Initialize MBA resource with default values. */ 3834 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid) 3835 { 3836 struct resctrl_staged_config *cfg; 3837 struct rdt_ctrl_domain *d; 3838 3839 list_for_each_entry_rcu(d, &r->ctrl_domains, hdr.list, lockdep_is_cpus_held()) { 3840 if (is_mba_sc(r)) { 3841 d->mbps_val[closid] = MBA_MAX_MBPS; 3842 continue; 3843 } 3844 3845 cfg = &d->staged_config[CDP_NONE]; 3846 cfg->new_ctrl = resctrl_get_default_ctrl(r); 3847 cfg->have_new_ctrl = true; 3848 } 3849 } 3850 3851 /* Initialize the RDT group's allocations. */ 3852 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) 3853 { 3854 struct resctrl_schema *s; 3855 struct rdt_resource *r; 3856 int ret = 0; 3857 3858 rdt_staged_configs_clear(); 3859 3860 list_for_each_entry(s, &resctrl_schema_all, list) { 3861 r = s->res; 3862 if (r->rid == RDT_RESOURCE_MBA || 3863 r->rid == RDT_RESOURCE_SMBA) { 3864 rdtgroup_init_mba(r, rdtgrp->closid); 3865 if (is_mba_sc(r)) 3866 continue; 3867 } else { 3868 ret = rdtgroup_init_cat(s, rdtgrp->closid); 3869 if (ret < 0) 3870 goto out; 3871 } 3872 3873 ret = resctrl_arch_update_domains(r, rdtgrp->closid); 3874 if (ret < 0) { 3875 rdt_last_cmd_puts("Failed to initialize allocations\n"); 3876 goto out; 3877 } 3878 } 3879 3880 rdtgrp->mode = RDT_MODE_SHAREABLE; 3881 3882 out: 3883 rdt_staged_configs_clear(); 3884 return ret; 3885 } 3886 3887 static int mkdir_rdt_prepare_rmid_alloc(struct rdtgroup *rdtgrp) 3888 { 3889 int ret; 3890 3891 if (!resctrl_arch_mon_capable()) 3892 return 0; 3893 3894 ret = alloc_rmid(rdtgrp->closid); 3895 if (ret < 0) { 3896 rdt_last_cmd_puts("Out of RMIDs\n"); 3897 return ret; 3898 } 3899 rdtgrp->mon.rmid = ret; 3900 3901 rdtgroup_assign_cntrs(rdtgrp); 3902 3903 ret = mkdir_mondata_all(rdtgrp->kn, rdtgrp, &rdtgrp->mon.mon_data_kn); 3904 if (ret) { 3905 rdt_last_cmd_puts("kernfs subdir error\n"); 3906 rdtgroup_unassign_cntrs(rdtgrp); 3907 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 3908 return ret; 3909 } 3910 3911 return 0; 3912 } 3913 3914 static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp) 3915 { 3916 if (resctrl_arch_mon_capable()) { 3917 rdtgroup_unassign_cntrs(rgrp); 3918 free_rmid(rgrp->closid, rgrp->mon.rmid); 3919 } 3920 } 3921 3922 /* 3923 * We allow creating mon groups only with in a directory called "mon_groups" 3924 * which is present in every ctrl_mon group. Check if this is a valid 3925 * "mon_groups" directory. 3926 * 3927 * 1. The directory should be named "mon_groups". 3928 * 2. The mon group itself should "not" be named "mon_groups". 3929 * This makes sure "mon_groups" directory always has a ctrl_mon group 3930 * as parent. 3931 */ 3932 static bool is_mon_groups(struct kernfs_node *kn, const char *name) 3933 { 3934 return (!strcmp(rdt_kn_name(kn), "mon_groups") && 3935 strcmp(name, "mon_groups")); 3936 } 3937 3938 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, 3939 const char *name, umode_t mode, 3940 enum rdt_group_type rtype, struct rdtgroup **r) 3941 { 3942 struct rdtgroup *prdtgrp, *rdtgrp; 3943 unsigned long files = 0; 3944 struct kernfs_node *kn; 3945 int ret; 3946 3947 prdtgrp = rdtgroup_kn_lock_live(parent_kn); 3948 if (!prdtgrp) { 3949 ret = -ENODEV; 3950 goto out_unlock; 3951 } 3952 3953 /* 3954 * Check that the parent directory for a monitor group is a "mon_groups" 3955 * directory. 3956 */ 3957 if (rtype == RDTMON_GROUP && !is_mon_groups(parent_kn, name)) { 3958 ret = -EPERM; 3959 goto out_unlock; 3960 } 3961 3962 if (rtype == RDTMON_GROUP && 3963 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3964 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) { 3965 ret = -EINVAL; 3966 rdt_last_cmd_puts("Pseudo-locking in progress\n"); 3967 goto out_unlock; 3968 } 3969 3970 /* allocate the rdtgroup. */ 3971 rdtgrp = kzalloc_obj(*rdtgrp); 3972 if (!rdtgrp) { 3973 ret = -ENOSPC; 3974 rdt_last_cmd_puts("Kernel out of memory\n"); 3975 goto out_unlock; 3976 } 3977 *r = rdtgrp; 3978 rdtgrp->mon.parent = prdtgrp; 3979 rdtgrp->type = rtype; 3980 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); 3981 3982 /* kernfs creates the directory for rdtgrp */ 3983 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); 3984 if (IS_ERR(kn)) { 3985 ret = PTR_ERR(kn); 3986 rdt_last_cmd_puts("kernfs create error\n"); 3987 goto out_free_rgrp; 3988 } 3989 rdtgrp->kn = kn; 3990 3991 /* 3992 * kernfs_remove() will drop the reference count on "kn" which 3993 * will free it. But we still need it to stick around for the 3994 * rdtgroup_kn_unlock(kn) call. Take one extra reference here, 3995 * which will be dropped by kernfs_put() in rdtgroup_remove(). 3996 */ 3997 kernfs_get(kn); 3998 3999 ret = rdtgroup_kn_set_ugid(kn); 4000 if (ret) { 4001 rdt_last_cmd_puts("kernfs perm error\n"); 4002 goto out_destroy; 4003 } 4004 4005 if (rtype == RDTCTRL_GROUP) { 4006 files = RFTYPE_BASE | RFTYPE_CTRL; 4007 if (resctrl_arch_mon_capable()) 4008 files |= RFTYPE_MON; 4009 } else { 4010 files = RFTYPE_BASE | RFTYPE_MON; 4011 } 4012 4013 ret = rdtgroup_add_files(kn, files); 4014 if (ret) { 4015 rdt_last_cmd_puts("kernfs fill error\n"); 4016 goto out_destroy; 4017 } 4018 4019 /* 4020 * The caller unlocks the parent_kn upon success. 4021 */ 4022 return 0; 4023 4024 out_destroy: 4025 kernfs_put(rdtgrp->kn); 4026 kernfs_remove(rdtgrp->kn); 4027 out_free_rgrp: 4028 kfree(rdtgrp); 4029 out_unlock: 4030 rdtgroup_kn_unlock(parent_kn); 4031 return ret; 4032 } 4033 4034 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) 4035 { 4036 kernfs_remove(rgrp->kn); 4037 rdtgroup_remove(rgrp); 4038 } 4039 4040 /* 4041 * Create a monitor group under "mon_groups" directory of a control 4042 * and monitor group(ctrl_mon). This is a resource group 4043 * to monitor a subset of tasks and cpus in its parent ctrl_mon group. 4044 */ 4045 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, 4046 const char *name, umode_t mode) 4047 { 4048 struct rdtgroup *rdtgrp, *prgrp; 4049 int ret; 4050 4051 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp); 4052 if (ret) 4053 return ret; 4054 4055 prgrp = rdtgrp->mon.parent; 4056 rdtgrp->closid = prgrp->closid; 4057 4058 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp); 4059 if (ret) { 4060 mkdir_rdt_prepare_clean(rdtgrp); 4061 goto out_unlock; 4062 } 4063 4064 kernfs_activate(rdtgrp->kn); 4065 4066 /* 4067 * Add the rdtgrp to the list of rdtgrps the parent 4068 * ctrl_mon group has to track. 4069 */ 4070 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); 4071 4072 out_unlock: 4073 rdtgroup_kn_unlock(parent_kn); 4074 return ret; 4075 } 4076 4077 /* 4078 * These are rdtgroups created under the root directory. Can be used 4079 * to allocate and monitor resources. 4080 */ 4081 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, 4082 const char *name, umode_t mode) 4083 { 4084 struct rdtgroup *rdtgrp; 4085 struct kernfs_node *kn; 4086 u32 closid; 4087 int ret; 4088 4089 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp); 4090 if (ret) 4091 return ret; 4092 4093 kn = rdtgrp->kn; 4094 ret = closid_alloc(); 4095 if (ret < 0) { 4096 rdt_last_cmd_puts("Out of CLOSIDs\n"); 4097 goto out_common_fail; 4098 } 4099 closid = ret; 4100 ret = 0; 4101 4102 rdtgrp->closid = closid; 4103 4104 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp); 4105 if (ret) 4106 goto out_closid_free; 4107 4108 kernfs_activate(rdtgrp->kn); 4109 4110 ret = rdtgroup_init_alloc(rdtgrp); 4111 if (ret < 0) 4112 goto out_rmid_free; 4113 4114 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); 4115 4116 if (resctrl_arch_mon_capable()) { 4117 /* 4118 * Create an empty mon_groups directory to hold the subset 4119 * of tasks and cpus to monitor. 4120 */ 4121 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL); 4122 if (ret) { 4123 rdt_last_cmd_puts("kernfs subdir error\n"); 4124 goto out_del_list; 4125 } 4126 if (is_mba_sc(NULL)) 4127 rdtgrp->mba_mbps_event = mba_mbps_default_event; 4128 } 4129 4130 goto out_unlock; 4131 4132 out_del_list: 4133 list_del(&rdtgrp->rdtgroup_list); 4134 out_rmid_free: 4135 mkdir_rdt_prepare_rmid_free(rdtgrp); 4136 out_closid_free: 4137 closid_free(closid); 4138 out_common_fail: 4139 mkdir_rdt_prepare_clean(rdtgrp); 4140 out_unlock: 4141 rdtgroup_kn_unlock(parent_kn); 4142 return ret; 4143 } 4144 4145 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, 4146 umode_t mode) 4147 { 4148 /* Do not accept '\n' to avoid unparsable situation. */ 4149 if (strchr(name, '\n')) 4150 return -EINVAL; 4151 4152 /* 4153 * If the parent directory is the root directory and RDT 4154 * allocation is supported, add a control and monitoring 4155 * subdirectory 4156 */ 4157 if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn) 4158 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode); 4159 4160 /* Else, attempt to add a monitoring subdirectory. */ 4161 if (resctrl_arch_mon_capable()) 4162 return rdtgroup_mkdir_mon(parent_kn, name, mode); 4163 4164 return -EPERM; 4165 } 4166 4167 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 4168 { 4169 struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 4170 u32 closid, rmid; 4171 int cpu; 4172 4173 /* Give any tasks back to the parent group */ 4174 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); 4175 4176 /* 4177 * Update per cpu closid/rmid of the moved CPUs first. 4178 * Note: the closid will not change, but the arch code still needs it. 4179 */ 4180 closid = prdtgrp->closid; 4181 rmid = prdtgrp->mon.rmid; 4182 for_each_cpu(cpu, &rdtgrp->cpu_mask) 4183 resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid); 4184 4185 /* 4186 * Update the MSR on moved CPUs and CPUs which have moved 4187 * task running on them. 4188 */ 4189 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 4190 update_closid_rmid(tmpmask, NULL); 4191 4192 rdtgrp->flags = RDT_DELETED; 4193 4194 rdtgroup_unassign_cntrs(rdtgrp); 4195 4196 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 4197 4198 /* 4199 * Remove the rdtgrp from the parent ctrl_mon group's list 4200 */ 4201 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 4202 list_del(&rdtgrp->mon.crdtgrp_list); 4203 4204 kernfs_remove(rdtgrp->kn); 4205 4206 return 0; 4207 } 4208 4209 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp) 4210 { 4211 rdtgrp->flags = RDT_DELETED; 4212 list_del(&rdtgrp->rdtgroup_list); 4213 4214 kernfs_remove(rdtgrp->kn); 4215 return 0; 4216 } 4217 4218 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 4219 { 4220 u32 closid, rmid; 4221 int cpu; 4222 4223 /* Give any tasks back to the default group */ 4224 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); 4225 4226 /* Give any CPUs back to the default group */ 4227 cpumask_or(&rdtgroup_default.cpu_mask, 4228 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 4229 4230 /* Update per cpu closid and rmid of the moved CPUs first */ 4231 closid = rdtgroup_default.closid; 4232 rmid = rdtgroup_default.mon.rmid; 4233 for_each_cpu(cpu, &rdtgrp->cpu_mask) 4234 resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid); 4235 4236 /* 4237 * Update the MSR on moved CPUs and CPUs which have moved 4238 * task running on them. 4239 */ 4240 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 4241 update_closid_rmid(tmpmask, NULL); 4242 4243 rdtgroup_unassign_cntrs(rdtgrp); 4244 4245 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); 4246 closid_free(rdtgrp->closid); 4247 4248 rdtgroup_ctrl_remove(rdtgrp); 4249 4250 /* 4251 * Free all the child monitor group rmids. 4252 */ 4253 free_all_child_rdtgrp(rdtgrp); 4254 4255 return 0; 4256 } 4257 4258 static struct kernfs_node *rdt_kn_parent(struct kernfs_node *kn) 4259 { 4260 /* 4261 * Valid within the RCU section it was obtained or while rdtgroup_mutex 4262 * is held. 4263 */ 4264 return rcu_dereference_check(kn->__parent, lockdep_is_held(&rdtgroup_mutex)); 4265 } 4266 4267 static int rdtgroup_rmdir(struct kernfs_node *kn) 4268 { 4269 struct kernfs_node *parent_kn; 4270 struct rdtgroup *rdtgrp; 4271 cpumask_var_t tmpmask; 4272 int ret = 0; 4273 4274 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 4275 return -ENOMEM; 4276 4277 rdtgrp = rdtgroup_kn_lock_live(kn); 4278 if (!rdtgrp) { 4279 ret = -EPERM; 4280 goto out; 4281 } 4282 parent_kn = rdt_kn_parent(kn); 4283 4284 /* 4285 * If the rdtgroup is a ctrl_mon group and parent directory 4286 * is the root directory, remove the ctrl_mon group. 4287 * 4288 * If the rdtgroup is a mon group and parent directory 4289 * is a valid "mon_groups" directory, remove the mon group. 4290 */ 4291 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn && 4292 rdtgrp != &rdtgroup_default) { 4293 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 4294 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 4295 ret = rdtgroup_ctrl_remove(rdtgrp); 4296 } else { 4297 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask); 4298 } 4299 } else if (rdtgrp->type == RDTMON_GROUP && 4300 is_mon_groups(parent_kn, rdt_kn_name(kn))) { 4301 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask); 4302 } else { 4303 ret = -EPERM; 4304 } 4305 4306 out: 4307 rdtgroup_kn_unlock(kn); 4308 free_cpumask_var(tmpmask); 4309 return ret; 4310 } 4311 4312 /** 4313 * mongrp_reparent() - replace parent CTRL_MON group of a MON group 4314 * @rdtgrp: the MON group whose parent should be replaced 4315 * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp 4316 * @cpus: cpumask provided by the caller for use during this call 4317 * 4318 * Replaces the parent CTRL_MON group for a MON group, resulting in all member 4319 * tasks' CLOSID immediately changing to that of the new parent group. 4320 * Monitoring data for the group is unaffected by this operation. 4321 */ 4322 static void mongrp_reparent(struct rdtgroup *rdtgrp, 4323 struct rdtgroup *new_prdtgrp, 4324 cpumask_var_t cpus) 4325 { 4326 struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 4327 4328 WARN_ON(rdtgrp->type != RDTMON_GROUP); 4329 WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP); 4330 4331 /* Nothing to do when simply renaming a MON group. */ 4332 if (prdtgrp == new_prdtgrp) 4333 return; 4334 4335 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 4336 list_move_tail(&rdtgrp->mon.crdtgrp_list, 4337 &new_prdtgrp->mon.crdtgrp_list); 4338 4339 rdtgrp->mon.parent = new_prdtgrp; 4340 rdtgrp->closid = new_prdtgrp->closid; 4341 4342 /* Propagate updated closid to all tasks in this group. */ 4343 rdt_move_group_tasks(rdtgrp, rdtgrp, cpus); 4344 4345 update_closid_rmid(cpus, NULL); 4346 } 4347 4348 static int rdtgroup_rename(struct kernfs_node *kn, 4349 struct kernfs_node *new_parent, const char *new_name) 4350 { 4351 struct kernfs_node *kn_parent; 4352 struct rdtgroup *new_prdtgrp; 4353 struct rdtgroup *rdtgrp; 4354 cpumask_var_t tmpmask; 4355 int ret; 4356 4357 rdtgrp = kernfs_to_rdtgroup(kn); 4358 new_prdtgrp = kernfs_to_rdtgroup(new_parent); 4359 if (!rdtgrp || !new_prdtgrp) 4360 return -ENOENT; 4361 4362 /* Release both kernfs active_refs before obtaining rdtgroup mutex. */ 4363 rdtgroup_kn_get(rdtgrp, kn); 4364 rdtgroup_kn_get(new_prdtgrp, new_parent); 4365 4366 mutex_lock(&rdtgroup_mutex); 4367 4368 rdt_last_cmd_clear(); 4369 4370 /* 4371 * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if 4372 * either kernfs_node is a file. 4373 */ 4374 if (kernfs_type(kn) != KERNFS_DIR || 4375 kernfs_type(new_parent) != KERNFS_DIR) { 4376 rdt_last_cmd_puts("Source and destination must be directories"); 4377 ret = -EPERM; 4378 goto out; 4379 } 4380 4381 if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) { 4382 ret = -ENOENT; 4383 goto out; 4384 } 4385 4386 kn_parent = rdt_kn_parent(kn); 4387 if (rdtgrp->type != RDTMON_GROUP || !kn_parent || 4388 !is_mon_groups(kn_parent, rdt_kn_name(kn))) { 4389 rdt_last_cmd_puts("Source must be a MON group\n"); 4390 ret = -EPERM; 4391 goto out; 4392 } 4393 4394 if (!is_mon_groups(new_parent, new_name)) { 4395 rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n"); 4396 ret = -EPERM; 4397 goto out; 4398 } 4399 4400 /* 4401 * If the MON group is monitoring CPUs, the CPUs must be assigned to the 4402 * current parent CTRL_MON group and therefore cannot be assigned to 4403 * the new parent, making the move illegal. 4404 */ 4405 if (!cpumask_empty(&rdtgrp->cpu_mask) && 4406 rdtgrp->mon.parent != new_prdtgrp) { 4407 rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n"); 4408 ret = -EPERM; 4409 goto out; 4410 } 4411 4412 /* 4413 * Allocate the cpumask for use in mongrp_reparent() to avoid the 4414 * possibility of failing to allocate it after kernfs_rename() has 4415 * succeeded. 4416 */ 4417 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) { 4418 ret = -ENOMEM; 4419 goto out; 4420 } 4421 4422 /* 4423 * Perform all input validation and allocations needed to ensure 4424 * mongrp_reparent() will succeed before calling kernfs_rename(), 4425 * otherwise it would be necessary to revert this call if 4426 * mongrp_reparent() failed. 4427 */ 4428 ret = kernfs_rename(kn, new_parent, new_name); 4429 if (!ret) 4430 mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask); 4431 4432 free_cpumask_var(tmpmask); 4433 4434 out: 4435 mutex_unlock(&rdtgroup_mutex); 4436 rdtgroup_kn_put(rdtgrp, kn); 4437 rdtgroup_kn_put(new_prdtgrp, new_parent); 4438 return ret; 4439 } 4440 4441 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) 4442 { 4443 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3)) 4444 seq_puts(seq, ",cdp"); 4445 4446 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) 4447 seq_puts(seq, ",cdpl2"); 4448 4449 if (is_mba_sc(resctrl_arch_get_resource(RDT_RESOURCE_MBA))) 4450 seq_puts(seq, ",mba_MBps"); 4451 4452 if (resctrl_debug) 4453 seq_puts(seq, ",debug"); 4454 4455 return 0; 4456 } 4457 4458 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { 4459 .mkdir = rdtgroup_mkdir, 4460 .rmdir = rdtgroup_rmdir, 4461 .rename = rdtgroup_rename, 4462 .show_options = rdtgroup_show_options, 4463 }; 4464 4465 static int rdtgroup_setup_root(struct rdt_fs_context *ctx) 4466 { 4467 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, 4468 KERNFS_ROOT_CREATE_DEACTIVATED | 4469 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, 4470 &rdtgroup_default); 4471 if (IS_ERR(rdt_root)) 4472 return PTR_ERR(rdt_root); 4473 4474 ctx->kfc.root = rdt_root; 4475 rdtgroup_default.kn = kernfs_root_to_node(rdt_root); 4476 rdtgroup_default.flags = 0; 4477 4478 return 0; 4479 } 4480 4481 static void rdtgroup_destroy_root(void) 4482 { 4483 lockdep_assert_held(&rdtgroup_mutex); 4484 4485 kernfs_destroy_root(rdt_root); 4486 rdtgroup_default.kn = NULL; 4487 } 4488 4489 static void rdtgroup_setup_default(void) 4490 { 4491 mutex_lock(&rdtgroup_mutex); 4492 4493 rdtgroup_default.closid = RESCTRL_RESERVED_CLOSID; 4494 rdtgroup_default.mon.rmid = RESCTRL_RESERVED_RMID; 4495 rdtgroup_default.type = RDTCTRL_GROUP; 4496 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); 4497 4498 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); 4499 4500 mutex_unlock(&rdtgroup_mutex); 4501 } 4502 4503 static void domain_destroy_l3_mon_state(struct rdt_l3_mon_domain *d) 4504 { 4505 int idx; 4506 4507 kfree(d->cntr_cfg); 4508 bitmap_free(d->rmid_busy_llc); 4509 for_each_mbm_idx(idx) { 4510 kfree(d->mbm_states[idx]); 4511 d->mbm_states[idx] = NULL; 4512 } 4513 } 4514 4515 void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d) 4516 { 4517 /* 4518 * mbm_handle_overflow() may dereference this ctrl domain via 4519 * update_mba_bw()->get_sc_ctrl_domain_from_cpu(). The architecture has 4520 * unlinked the domain from the RCU list and waited a grace period, so 4521 * no new worker iteration can find it; drain any worker that already 4522 * holds a pointer to it before the architecture frees the domain. 4523 * 4524 * Software controller is enabled/disabled on mount/unmount with 4525 * cpus_read_lock() held. Running here with cpus_write_lock() so 4526 * there are no concurrent changes to software controller status. 4527 */ 4528 if (r->rid == RDT_RESOURCE_MBA && is_mba_sc(r)) { 4529 struct rdt_resource *l3 = resctrl_arch_get_resource(RDT_RESOURCE_L3); 4530 struct rdt_l3_mon_domain *mon_d; 4531 4532 list_for_each_entry_rcu(mon_d, &l3->mon_domains, hdr.list, lockdep_is_cpus_held()) { 4533 if (mon_d->hdr.id == d->hdr.id) { 4534 cancel_delayed_work_sync(&mon_d->mbm_over); 4535 break; 4536 } 4537 } 4538 } 4539 4540 mutex_lock(&rdtgroup_mutex); 4541 4542 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) 4543 mba_sc_domain_destroy(r, d); 4544 4545 mutex_unlock(&rdtgroup_mutex); 4546 } 4547 4548 void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *hdr) 4549 { 4550 struct rdt_l3_mon_domain *d; 4551 4552 /* 4553 * Called by architecture under CPU hotplug lock as it prepares to remove 4554 * the domain which is guaranteed to be accessible here. 4555 * The domain has been unlinked from the RCU list and a grace period 4556 * has elapsed, so no new worker can be scheduled. Drain any worker that 4557 * is in flight or pending before letting architecture proceed to free 4558 * the domain that has the workers' struct delayed_work embedded. 4559 * Do so before taking rdtgroup_mutex since the workers also acquire it. 4560 */ 4561 if (r->rid == RDT_RESOURCE_L3 && 4562 domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) { 4563 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 4564 if (resctrl_is_mbm_enabled()) 4565 cancel_delayed_work_sync(&d->mbm_over); 4566 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) 4567 cancel_delayed_work_sync(&d->cqm_limbo); 4568 } 4569 4570 mutex_lock(&rdtgroup_mutex); 4571 4572 /* 4573 * If resctrl is mounted, remove all the 4574 * per domain monitor data directories. 4575 */ 4576 if (resctrl_mounted && resctrl_arch_mon_capable()) 4577 rmdir_mondata_subdir_allrdtgrp(r, hdr); 4578 4579 if (r->rid != RDT_RESOURCE_L3) 4580 goto out_unlock; 4581 4582 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 4583 goto out_unlock; 4584 4585 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 4586 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && has_busy_rmid(d)) { 4587 /* 4588 * When a package is going down, forcefully 4589 * decrement rmid->ebusy. There is no way to know 4590 * that the L3 was flushed and hence may lead to 4591 * incorrect counts in rare scenarios, but leaving 4592 * the RMID as busy creates RMID leaks if the 4593 * package never comes back. 4594 */ 4595 __check_limbo(d, true); 4596 } 4597 4598 domain_destroy_l3_mon_state(d); 4599 out_unlock: 4600 mutex_unlock(&rdtgroup_mutex); 4601 } 4602 4603 /** 4604 * domain_setup_l3_mon_state() - Initialise domain monitoring structures. 4605 * @r: The resource for the newly online domain. 4606 * @d: The newly online domain. 4607 * 4608 * Allocate monitor resources that belong to this domain. 4609 * Called when the first CPU of a domain comes online, regardless of whether 4610 * the filesystem is mounted. 4611 * During boot this may be called before global allocations have been made by 4612 * resctrl_l3_mon_resource_init(). 4613 * 4614 * Called during CPU online that may run as soon as CPU online callbacks 4615 * are set up during resctrl initialization. The number of supported RMIDs 4616 * may be reduced if additional mon_capable resources are enumerated 4617 * at mount time. This means the rdt_l3_mon_domain::mbm_states[] and 4618 * rdt_l3_mon_domain::rmid_busy_llc allocations may be larger than needed. 4619 * 4620 * Return: 0 for success, or -ENOMEM. 4621 */ 4622 static int domain_setup_l3_mon_state(struct rdt_resource *r, struct rdt_l3_mon_domain *d) 4623 { 4624 u32 idx_limit = resctrl_arch_system_num_rmid_idx(); 4625 size_t tsize = sizeof(*d->mbm_states[0]); 4626 enum resctrl_event_id eventid; 4627 int idx; 4628 4629 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) { 4630 d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL); 4631 if (!d->rmid_busy_llc) 4632 return -ENOMEM; 4633 } 4634 4635 for_each_mbm_event_id(eventid) { 4636 if (!resctrl_is_mon_event_enabled(eventid)) 4637 continue; 4638 idx = MBM_STATE_IDX(eventid); 4639 d->mbm_states[idx] = kcalloc(idx_limit, tsize, GFP_KERNEL); 4640 if (!d->mbm_states[idx]) 4641 goto cleanup; 4642 } 4643 4644 if (resctrl_is_mbm_enabled() && r->mon.mbm_cntr_assignable) { 4645 tsize = sizeof(*d->cntr_cfg); 4646 d->cntr_cfg = kcalloc(r->mon.num_mbm_cntrs, tsize, GFP_KERNEL); 4647 if (!d->cntr_cfg) 4648 goto cleanup; 4649 } 4650 4651 return 0; 4652 cleanup: 4653 bitmap_free(d->rmid_busy_llc); 4654 for_each_mbm_idx(idx) { 4655 kfree(d->mbm_states[idx]); 4656 d->mbm_states[idx] = NULL; 4657 } 4658 4659 return -ENOMEM; 4660 } 4661 4662 int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d) 4663 { 4664 int err = 0; 4665 4666 mutex_lock(&rdtgroup_mutex); 4667 4668 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) { 4669 /* RDT_RESOURCE_MBA is never mon_capable */ 4670 err = mba_sc_domain_allocate(r, d); 4671 } 4672 4673 mutex_unlock(&rdtgroup_mutex); 4674 4675 return err; 4676 } 4677 4678 int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *hdr) 4679 { 4680 struct rdt_l3_mon_domain *d; 4681 int err = -EINVAL; 4682 4683 mutex_lock(&rdtgroup_mutex); 4684 4685 if (r->rid != RDT_RESOURCE_L3) 4686 goto mkdir; 4687 4688 if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3)) 4689 goto out_unlock; 4690 4691 d = container_of(hdr, struct rdt_l3_mon_domain, hdr); 4692 err = domain_setup_l3_mon_state(r, d); 4693 if (err) 4694 goto out_unlock; 4695 4696 if (resctrl_is_mbm_enabled()) { 4697 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow); 4698 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL, 4699 RESCTRL_PICK_ANY_CPU); 4700 } 4701 4702 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) 4703 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo); 4704 4705 mkdir: 4706 err = 0; 4707 /* 4708 * If the filesystem is not mounted then only the default resource group 4709 * exists. Creation of its directories is deferred until mount time 4710 * by rdt_get_tree() calling mkdir_mondata_all(). 4711 * If resctrl is mounted, add per domain monitor data directories. 4712 */ 4713 if (resctrl_mounted && resctrl_arch_mon_capable()) 4714 mkdir_mondata_subdir_allrdtgrp(r, hdr); 4715 4716 out_unlock: 4717 mutex_unlock(&rdtgroup_mutex); 4718 4719 return err; 4720 } 4721 4722 void resctrl_online_cpu(unsigned int cpu) 4723 { 4724 mutex_lock(&rdtgroup_mutex); 4725 /* The CPU is set in default rdtgroup after online. */ 4726 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask); 4727 mutex_unlock(&rdtgroup_mutex); 4728 } 4729 4730 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu) 4731 { 4732 struct rdtgroup *cr; 4733 4734 list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) { 4735 if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) 4736 break; 4737 } 4738 } 4739 4740 static struct rdt_l3_mon_domain *get_mon_domain_from_cpu(int cpu, 4741 struct rdt_resource *r) 4742 { 4743 struct rdt_l3_mon_domain *d; 4744 4745 lockdep_assert_cpus_held(); 4746 4747 list_for_each_entry_rcu(d, &r->mon_domains, hdr.list, lockdep_is_cpus_held()) { 4748 /* Find the domain that contains this CPU */ 4749 if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask)) 4750 return d; 4751 } 4752 4753 return NULL; 4754 } 4755 4756 void resctrl_offline_cpu(unsigned int cpu) 4757 { 4758 struct rdt_resource *l3 = resctrl_arch_get_resource(RDT_RESOURCE_L3); 4759 struct rdt_l3_mon_domain *d; 4760 struct rdtgroup *rdtgrp; 4761 4762 mutex_lock(&rdtgroup_mutex); 4763 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 4764 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) { 4765 clear_childcpus(rdtgrp, cpu); 4766 break; 4767 } 4768 } 4769 4770 if (!l3->mon_capable) 4771 goto out_unlock; 4772 4773 d = get_mon_domain_from_cpu(cpu, l3); 4774 if (d) { 4775 if (resctrl_is_mbm_enabled() && cpu == d->mbm_work_cpu) { 4776 mutex_unlock(&rdtgroup_mutex); 4777 cancel_delayed_work_sync(&d->mbm_over); 4778 mutex_lock(&rdtgroup_mutex); 4779 mbm_setup_overflow_handler(d, 0, cpu); 4780 } 4781 if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && 4782 cpu == d->cqm_work_cpu && has_busy_rmid(d)) { 4783 mutex_unlock(&rdtgroup_mutex); 4784 cancel_delayed_work_sync(&d->cqm_limbo); 4785 mutex_lock(&rdtgroup_mutex); 4786 cqm_setup_limbo_handler(d, 0, cpu); 4787 } 4788 } 4789 4790 out_unlock: 4791 mutex_unlock(&rdtgroup_mutex); 4792 } 4793 4794 /* 4795 * resctrl_init - resctrl filesystem initialization 4796 * 4797 * Setup resctrl file system including set up root, create mount point, 4798 * register resctrl filesystem, and initialize files under root directory. 4799 * 4800 * Return: 0 on success or -errno 4801 */ 4802 int resctrl_init(void) 4803 { 4804 int ret = 0; 4805 4806 seq_buf_init(&last_cmd_status, last_cmd_status_buf, 4807 sizeof(last_cmd_status_buf)); 4808 4809 rdtgroup_setup_default(); 4810 4811 thread_throttle_mode_init(); 4812 4813 io_alloc_init(); 4814 4815 ret = resctrl_l3_mon_resource_init(); 4816 if (ret) 4817 return ret; 4818 4819 ret = sysfs_create_mount_point(fs_kobj, "resctrl"); 4820 if (ret) { 4821 resctrl_l3_mon_resource_exit(); 4822 return ret; 4823 } 4824 4825 ret = register_filesystem(&rdt_fs_type); 4826 if (ret) 4827 goto cleanup_mountpoint; 4828 4829 /* 4830 * Adding the resctrl debugfs directory here may not be ideal since 4831 * it would let the resctrl debugfs directory appear on the debugfs 4832 * filesystem before the resctrl filesystem is mounted. 4833 * It may also be ok since that would enable debugging of RDT before 4834 * resctrl is mounted. 4835 * The reason why the debugfs directory is created here and not in 4836 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and 4837 * during the debugfs directory creation also &sb->s_type->i_mutex_key 4838 * (the lockdep class of inode->i_rwsem). Other filesystem 4839 * interactions (eg. SyS_getdents) have the lock ordering: 4840 * &sb->s_type->i_mutex_key --> &mm->mmap_lock 4841 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex 4842 * is taken, thus creating dependency: 4843 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause 4844 * issues considering the other two lock dependencies. 4845 * By creating the debugfs directory here we avoid a dependency 4846 * that may cause deadlock (even though file operations cannot 4847 * occur until the filesystem is mounted, but I do not know how to 4848 * tell lockdep that). 4849 */ 4850 debugfs_resctrl = debugfs_create_dir("resctrl", NULL); 4851 4852 return 0; 4853 4854 cleanup_mountpoint: 4855 sysfs_remove_mount_point(fs_kobj, "resctrl"); 4856 resctrl_l3_mon_resource_exit(); 4857 4858 return ret; 4859 } 4860 4861 static bool resctrl_online_domains_exist(void) 4862 { 4863 struct rdt_resource *r; 4864 4865 /* 4866 * Only walk capable resources to allow resctrl_arch_get_resource() 4867 * to return dummy 'not capable' resources. 4868 */ 4869 for_each_alloc_capable_rdt_resource(r) { 4870 if (!list_empty(&r->ctrl_domains)) 4871 return true; 4872 } 4873 4874 for_each_mon_capable_rdt_resource(r) { 4875 if (!list_empty(&r->mon_domains)) 4876 return true; 4877 } 4878 4879 return false; 4880 } 4881 4882 /** 4883 * resctrl_exit() - Remove the resctrl filesystem and free resources. 4884 * 4885 * Called by the architecture code in response to a fatal error. 4886 * Removes resctrl files and structures from kernfs to prevent further 4887 * configuration. 4888 * 4889 * When called by the architecture code, all CPUs and resctrl domains must be 4890 * offline. This ensures the limbo and overflow handlers are not scheduled to 4891 * run, meaning the data structures they access can be freed by 4892 * resctrl_l3_mon_resource_exit(). 4893 * 4894 * After resctrl_exit() returns, the architecture code should return an 4895 * error from all resctrl_arch_ functions that can do this. 4896 * resctrl_arch_get_resource() must continue to return struct rdt_resources 4897 * with the correct rid field to ensure the filesystem can be unmounted. 4898 */ 4899 void resctrl_exit(void) 4900 { 4901 cpus_read_lock(); 4902 WARN_ON_ONCE(resctrl_online_domains_exist()); 4903 4904 mutex_lock(&rdtgroup_mutex); 4905 resctrl_fs_teardown(); 4906 mutex_unlock(&rdtgroup_mutex); 4907 4908 cpus_read_unlock(); 4909 4910 debugfs_remove_recursive(debugfs_resctrl); 4911 debugfs_resctrl = NULL; 4912 unregister_filesystem(&rdt_fs_type); 4913 4914 /* 4915 * Do not remove the sysfs mount point added by resctrl_init() so that 4916 * it can be used to umount resctrl. 4917 */ 4918 4919 resctrl_l3_mon_resource_exit(); 4920 free_rmid_lru_list(); 4921 } 4922