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