1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Arm Ltd.
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
#include <linux/arm_mpam.h>
#include <linux/cacheinfo.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/errno.h>
#include <linux/limits.h>
#include <linux/list.h>
#include <linux/math.h>
#include <linux/printk.h>
#include <linux/rculist.h>
#include <linux/resctrl.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <asm/mpam.h>
#include "mpam_internal.h"
/*
* The classes we've picked to map to resctrl resources, wrapped
* in with their resctrl structure.
* Class pointer may be NULL.
*/
static struct mpam_resctrl_res mpam_resctrl_controls[RDT_NUM_RESOURCES];
#define for_each_mpam_resctrl_control(res, rid) \
for (rid = 0, res = &mpam_resctrl_controls[rid]; \
rid < RDT_NUM_RESOURCES; \
rid++, res = &mpam_resctrl_controls[rid])
/* The lock for modifying resctrl's domain lists from cpuhp callbacks. */
static DEFINE_MUTEX(domain_list_lock);
/*
* MPAM emulates CDP by setting different PARTID in the I/D fields of MPAM0_EL1.
* This applies globally to all traffic the CPU generates.
*/
static bool cdp_enabled;
bool resctrl_arch_alloc_capable(void)
{
struct mpam_resctrl_res *res;
enum resctrl_res_level rid;
for_each_mpam_resctrl_control(res, rid) {
if (res->resctrl_res.alloc_capable)
return true;
}
return false;
}
bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level rid)
{
return mpam_resctrl_controls[rid].cdp_enabled;
}
/**
* resctrl_reset_task_closids() - Reset the PARTID/PMG values for all tasks.
*
* At boot, all existing tasks use partid zero for D and I.
* To enable/disable CDP emulation, all these tasks need relabelling.
*/
static void resctrl_reset_task_closids(void)
{
struct task_struct *p, *t;
read_lock(&tasklist_lock);
for_each_process_thread(p, t) {
resctrl_arch_set_closid_rmid(t, RESCTRL_RESERVED_CLOSID,
RESCTRL_RESERVED_RMID);
}
read_unlock(&tasklist_lock);
}
int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
{
u32 partid_i = RESCTRL_RESERVED_CLOSID, partid_d = RESCTRL_RESERVED_CLOSID;
int cpu;
if (!IS_ENABLED(CONFIG_EXPERT) && enable) {
/*
* If the resctrl fs is mounted more than once, sequentially,
* then CDP can lead to the use of out of range PARTIDs.
*/
pr_warn("CDP not supported\n");
return -EOPNOTSUPP;
}
if (enable)
pr_warn("CDP is an expert feature and may cause MPAM to malfunction.\n");
/*
* resctrl_arch_set_cdp_enabled() is only called with enable set to
* false on error and unmount.
*/
cdp_enabled = enable;
mpam_resctrl_controls[rid].cdp_enabled = enable;
/* The mbw_max feature can't hide cdp as it's a per-partid maximum. */
if (cdp_enabled && !mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled)
mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = false;
if (mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled &&
mpam_resctrl_controls[RDT_RESOURCE_MBA].class)
mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = true;
if (enable) {
if (mpam_partid_max < 1)
return -EINVAL;
partid_d = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_DATA);
partid_i = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_CODE);
}
mpam_set_task_partid_pmg(current, partid_d, partid_i, 0, 0);
WRITE_ONCE(arm64_mpam_global_default, mpam_get_regval(current));
resctrl_reset_task_closids();
for_each_possible_cpu(cpu)
mpam_set_cpu_defaults(cpu, partid_d, partid_i, 0, 0);
on_each_cpu(resctrl_arch_sync_cpu_closid_rmid, NULL, 1);
return 0;
}
static bool mpam_resctrl_hide_cdp(enum resctrl_res_level rid)
{
return cdp_enabled && !resctrl_arch_get_cdp_enabled(rid);
}
/*
* MSC may raise an error interrupt if it sees an out or range partid/pmg,
* and go on to truncate the value. Regardless of what the hardware supports,
* only the system wide safe value is safe to use.
*/
u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
{
return mpam_partid_max + 1;
}
u32 resctrl_arch_system_num_rmid_idx(void)
{
return (mpam_pmg_max + 1) * (mpam_partid_max + 1);
}
u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid)
{
return closid * (mpam_pmg_max + 1) + rmid;
}
void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid)
{
*closid = idx / (mpam_pmg_max + 1);
*rmid = idx % (mpam_pmg_max + 1);
}
void resctrl_arch_sched_in(struct task_struct *tsk)
{
lockdep_assert_preemption_disabled();
mpam_thread_switch(tsk);
}
void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, u32 rmid)
{
WARN_ON_ONCE(closid > U16_MAX);
WARN_ON_ONCE(rmid > U8_MAX);
if (!cdp_enabled) {
mpam_set_cpu_defaults(cpu, closid, closid, rmid, rmid);
} else {
/*
* When CDP is enabled, resctrl halves the closid range and we
* use odd/even partid for one closid.
*/
u32 partid_d = resctrl_get_config_index(closid, CDP_DATA);
u32 partid_i = resctrl_get_config_index(closid, CDP_CODE);
mpam_set_cpu_defaults(cpu, partid_d, partid_i, rmid, rmid);
}
}
void resctrl_arch_sync_cpu_closid_rmid(void *info)
{
struct resctrl_cpu_defaults *r = info;
lockdep_assert_preemption_disabled();
if (r) {
resctrl_arch_set_cpu_default_closid_rmid(smp_processor_id(),
r->closid, r->rmid);
}
resctrl_arch_sched_in(current);
}
void resctrl_arch_set_closid_rmid(struct task_struct *tsk, u32 closid, u32 rmid)
{
WARN_ON_ONCE(closid > U16_MAX);
WARN_ON_ONCE(rmid > U8_MAX);
if (!cdp_enabled) {
mpam_set_task_partid_pmg(tsk, closid, closid, rmid, rmid);
} else {
u32 partid_d = resctrl_get_config_index(closid, CDP_DATA);
u32 partid_i = resctrl_get_config_index(closid, CDP_CODE);
mpam_set_task_partid_pmg(tsk, partid_d, partid_i, rmid, rmid);
}
}
bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid)
{
u64 regval = mpam_get_regval(tsk);
u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval);
if (cdp_enabled)
tsk_closid >>= 1;
return tsk_closid == closid;
}
/* The task's pmg is not unique, the partid must be considered too */
bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid)
{
u64 regval = mpam_get_regval(tsk);
u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval);
u32 tsk_rmid = FIELD_GET(MPAM0_EL1_PMG_D, regval);
if (cdp_enabled)
tsk_closid >>= 1;
return (tsk_closid == closid) && (tsk_rmid == rmid);
}
struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l)
{
if (l >= RDT_NUM_RESOURCES)
return NULL;
return &mpam_resctrl_controls[l].resctrl_res;
}
static bool cache_has_usable_cpor(struct mpam_class *class)
{
struct mpam_props *cprops = &class->props;
if (!mpam_has_feature(mpam_feat_cpor_part, cprops))
return false;
/* resctrl uses u32 for all bitmap configurations */
return class->props.cpbm_wd <= 32;
}
/*
* Each fixed-point hardware value architecturally represents a range
* of values: the full range 0% - 100% is split contiguously into
* (1 << cprops->bwa_wd) equal bands.
*
* Although the bwa_bwd fields have 6 bits the maximum valid value is 16
* as it reports the width of fields that are at most 16 bits. When
* fewer than 16 bits are valid the least significant bits are
* ignored. The implied binary point is kept between bits 15 and 16 and
* so the valid bits are leftmost.
*
* See ARM IHI0099B.a "MPAM system component specification", Section 9.3,
* "The fixed-point fractional format" for more information.
*
* Find the nearest percentage value to the upper bound of the selected band:
*/
static u32 mbw_max_to_percent(u16 mbw_max, struct mpam_props *cprops)
{
u32 val = mbw_max;
val >>= 16 - cprops->bwa_wd;
val += 1;
val *= MAX_MBA_BW;
val = DIV_ROUND_CLOSEST(val, 1 << cprops->bwa_wd);
return val;
}
/*
* Find the band whose upper bound is closest to the specified percentage.
*
* A round-to-nearest policy is followed here as a balanced compromise
* between unexpected under-commit of the resource (where the total of
* a set of resource allocations after conversion is less than the
* expected total, due to rounding of the individual converted
* percentages) and over-commit (where the total of the converted
* allocations is greater than expected).
*/
static u16 percent_to_mbw_max(u8 pc, struct mpam_props *cprops)
{
u32 val = pc;
val <<= cprops->bwa_wd;
val = DIV_ROUND_CLOSEST(val, MAX_MBA_BW);
val = max(val, 1) - 1;
val <<= 16 - cprops->bwa_wd;
return val;
}
/* Test whether we can export MPAM_CLASS_CACHE:{2,3}? */
static void mpam_resctrl_pick_caches(void)
{
struct mpam_class *class;
struct mpam_resctrl_res *res;
lockdep_assert_cpus_held();
guard(srcu)(&mpam_srcu);
list_for_each_entry_srcu(class, &mpam_classes, classes_list,
srcu_read_lock_held(&mpam_srcu)) {
if (class->type != MPAM_CLASS_CACHE) {
pr_debug("class %u is not a cache\n", class->level);
continue;
}
if (class->level != 2 && class->level != 3) {
pr_debug("class %u is not L2 or L3\n", class->level);
continue;
}
if (!cache_has_usable_cpor(class)) {
pr_debug("class %u cache misses CPOR\n", class->level);
continue;
}
if (!cpumask_equal(&class->affinity, cpu_possible_mask)) {
pr_debug("class %u has missing CPUs, mask %*pb != %*pb\n", class->level,
cpumask_pr_args(&class->affinity),
cpumask_pr_args(cpu_possible_mask));
continue;
}
if (class->level == 2)
res = &mpam_resctrl_controls[RDT_RESOURCE_L2];
else
res = &mpam_resctrl_controls[RDT_RESOURCE_L3];
res->class = class;
}
}
static int mpam_resctrl_control_init(struct mpam_resctrl_res *res)
{
struct mpam_class *class = res->class;
struct rdt_resource *r = &res->resctrl_res;
switch (r->rid) {
case RDT_RESOURCE_L2:
case RDT_RESOURCE_L3:
r->schema_fmt = RESCTRL_SCHEMA_BITMAP;
r->cache.arch_has_sparse_bitmasks = true;
r->cache.cbm_len = class->props.cpbm_wd;
/* mpam_devices will reject empty bitmaps */
r->cache.min_cbm_bits = 1;
if (r->rid == RDT_RESOURCE_L2) {
r->name = "L2";
r->ctrl_scope = RESCTRL_L2_CACHE;
r->cdp_capable = true;
} else {
r->name = "L3";
r->ctrl_scope = RESCTRL_L3_CACHE;
r->cdp_capable = true;
}
/*
* Which bits are shared with other ...things... Unknown
* devices use partid-0 which uses all the bitmap fields. Until
* we have configured the SMMU and GIC not to do this 'all the
* bits' is the correct answer here.
*/
r->cache.shareable_bits = resctrl_get_default_ctrl(r);
r->alloc_capable = true;
break;
default:
return -EINVAL;
}
return 0;
}
static int mpam_resctrl_pick_domain_id(int cpu, struct mpam_component *comp)
{
struct mpam_class *class = comp->class;
if (class->type == MPAM_CLASS_CACHE)
return comp->comp_id;
/* TODO: repaint domain ids to match the L3 domain ids */
/* Otherwise, expose the ID used by the firmware table code. */
return comp->comp_id;
}
u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type type)
{
u32 partid;
struct mpam_config *cfg;
struct mpam_props *cprops;
struct mpam_resctrl_res *res;
struct mpam_resctrl_dom *dom;
enum mpam_device_features configured_by;
lockdep_assert_cpus_held();
if (!mpam_is_enabled())
return resctrl_get_default_ctrl(r);
res = container_of(r, struct mpam_resctrl_res, resctrl_res);
dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom);
cprops = &res->class->props;
/*
* When CDP is enabled, but the resource doesn't support it,
* the control is cloned across both partids.
* Pick one at random to read:
*/
if (mpam_resctrl_hide_cdp(r->rid))
type = CDP_DATA;
partid = resctrl_get_config_index(closid, type);
cfg = &dom->ctrl_comp->cfg[partid];
switch (r->rid) {
case RDT_RESOURCE_L2:
case RDT_RESOURCE_L3:
configured_by = mpam_feat_cpor_part;
break;
default:
return resctrl_get_default_ctrl(r);
}
if (!r->alloc_capable || partid >= resctrl_arch_get_num_closid(r) ||
!mpam_has_feature(configured_by, cfg))
return resctrl_get_default_ctrl(r);
switch (configured_by) {
case mpam_feat_cpor_part:
return cfg->cpbm;
default:
return resctrl_get_default_ctrl(r);
}
}
int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type t, u32 cfg_val)
{
int err;
u32 partid;
struct mpam_config cfg;
struct mpam_props *cprops;
struct mpam_resctrl_res *res;
struct mpam_resctrl_dom *dom;
lockdep_assert_cpus_held();
lockdep_assert_irqs_enabled();
/*
* No need to check the CPU as mpam_apply_config() doesn't care, and
* resctrl_arch_update_domains() relies on this.
*/
res = container_of(r, struct mpam_resctrl_res, resctrl_res);
dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom);
cprops = &res->class->props;
if (mpam_resctrl_hide_cdp(r->rid))
t = CDP_DATA;
partid = resctrl_get_config_index(closid, t);
if (!r->alloc_capable || partid >= resctrl_arch_get_num_closid(r)) {
pr_debug("Not alloc capable or computed PARTID out of range\n");
return -EINVAL;
}
/*
* Copy the current config to avoid clearing other resources when the
* same component is exposed multiple times through resctrl.
*/
cfg = dom->ctrl_comp->cfg[partid];
switch (r->rid) {
case RDT_RESOURCE_L2:
case RDT_RESOURCE_L3:
cfg.cpbm = cfg_val;
mpam_set_feature(mpam_feat_cpor_part, &cfg);
break;
default:
return -EINVAL;
}
/*
* When CDP is enabled, but the resource doesn't support it, we need to
* apply the same configuration to the other partid.
*/
if (mpam_resctrl_hide_cdp(r->rid)) {
partid = resctrl_get_config_index(closid, CDP_CODE);
err = mpam_apply_config(dom->ctrl_comp, partid, &cfg);
if (err)
return err;
partid = resctrl_get_config_index(closid, CDP_DATA);
return mpam_apply_config(dom->ctrl_comp, partid, &cfg);
}
return mpam_apply_config(dom->ctrl_comp, partid, &cfg);
}
int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
{
int err;
struct rdt_ctrl_domain *d;
lockdep_assert_cpus_held();
lockdep_assert_irqs_enabled();
list_for_each_entry_rcu(d, &r->ctrl_domains, hdr.list) {
for (enum resctrl_conf_type t = 0; t < CDP_NUM_TYPES; t++) {
struct resctrl_staged_config *cfg = &d->staged_config[t];
if (!cfg->have_new_ctrl)
continue;
err = resctrl_arch_update_one(r, d, closid, t,
cfg->new_ctrl);
if (err)
return err;
}
}
return 0;
}
void resctrl_arch_reset_all_ctrls(struct rdt_resource *r)
{
struct mpam_resctrl_res *res;
lockdep_assert_cpus_held();
if (!mpam_is_enabled())
return;
res = container_of(r, struct mpam_resctrl_res, resctrl_res);
mpam_reset_class_locked(res->class);
}
static void mpam_resctrl_domain_hdr_init(int cpu, struct mpam_component *comp,
enum resctrl_res_level rid,
struct rdt_domain_hdr *hdr)
{
lockdep_assert_cpus_held();
INIT_LIST_HEAD(&hdr->list);
hdr->id = mpam_resctrl_pick_domain_id(cpu, comp);
hdr->rid = rid;
cpumask_set_cpu(cpu, &hdr->cpu_mask);
}
static void mpam_resctrl_online_domain_hdr(unsigned int cpu,
struct rdt_domain_hdr *hdr)
{
lockdep_assert_cpus_held();
cpumask_set_cpu(cpu, &hdr->cpu_mask);
}
/**
* mpam_resctrl_offline_domain_hdr() - Update the domain header to remove a CPU.
* @cpu: The CPU to remove from the domain.
* @hdr: The domain's header.
*
* Removes @cpu from the header mask. If this was the last CPU in the domain,
* the domain header is removed from its parent list and true is returned,
* indicating the parent structure can be freed.
* If there are other CPUs in the domain, returns false.
*/
static bool mpam_resctrl_offline_domain_hdr(unsigned int cpu,
struct rdt_domain_hdr *hdr)
{
lockdep_assert_held(&domain_list_lock);
cpumask_clear_cpu(cpu, &hdr->cpu_mask);
if (cpumask_empty(&hdr->cpu_mask)) {
list_del_rcu(&hdr->list);
synchronize_rcu();
return true;
}
return false;
}
static void mpam_resctrl_domain_insert(struct list_head *list,
struct rdt_domain_hdr *new)
{
struct rdt_domain_hdr *err;
struct list_head *pos = NULL;
lockdep_assert_held(&domain_list_lock);
err = resctrl_find_domain(list, new->id, &pos);
if (WARN_ON_ONCE(err))
return;
list_add_tail_rcu(&new->list, pos);
}
static struct mpam_resctrl_dom *
mpam_resctrl_alloc_domain(unsigned int cpu, struct mpam_resctrl_res *res)
{
int err;
struct mpam_resctrl_dom *dom;
struct rdt_ctrl_domain *ctrl_d;
struct mpam_class *class = res->class;
struct mpam_component *comp_iter, *ctrl_comp;
struct rdt_resource *r = &res->resctrl_res;
lockdep_assert_held(&domain_list_lock);
ctrl_comp = NULL;
guard(srcu)(&mpam_srcu);
list_for_each_entry_srcu(comp_iter, &class->components, class_list,
srcu_read_lock_held(&mpam_srcu)) {
if (cpumask_test_cpu(cpu, &comp_iter->affinity)) {
ctrl_comp = comp_iter;
break;
}
}
/* class has no component for this CPU */
if (WARN_ON_ONCE(!ctrl_comp))
return ERR_PTR(-EINVAL);
dom = kzalloc_node(sizeof(*dom), GFP_KERNEL, cpu_to_node(cpu));
if (!dom)
return ERR_PTR(-ENOMEM);
if (r->alloc_capable) {
dom->ctrl_comp = ctrl_comp;
ctrl_d = &dom->resctrl_ctrl_dom;
mpam_resctrl_domain_hdr_init(cpu, ctrl_comp, r->rid, &ctrl_d->hdr);
ctrl_d->hdr.type = RESCTRL_CTRL_DOMAIN;
err = resctrl_online_ctrl_domain(r, ctrl_d);
if (err)
goto free_domain;
mpam_resctrl_domain_insert(&r->ctrl_domains, &ctrl_d->hdr);
} else {
pr_debug("Skipped control domain online - no controls\n");
}
return dom;
free_domain:
kfree(dom);
dom = ERR_PTR(err);
return dom;
}
static struct mpam_resctrl_dom *
mpam_resctrl_get_domain_from_cpu(int cpu, struct mpam_resctrl_res *res)
{
struct mpam_resctrl_dom *dom;
struct rdt_resource *r = &res->resctrl_res;
lockdep_assert_cpus_held();
list_for_each_entry_rcu(dom, &r->ctrl_domains, resctrl_ctrl_dom.hdr.list) {
if (cpumask_test_cpu(cpu, &dom->ctrl_comp->affinity))
return dom;
}
return NULL;
}
int mpam_resctrl_online_cpu(unsigned int cpu)
{
struct mpam_resctrl_res *res;
enum resctrl_res_level rid;
guard(mutex)(&domain_list_lock);
for_each_mpam_resctrl_control(res, rid) {
struct mpam_resctrl_dom *dom;
struct rdt_resource *r = &res->resctrl_res;
if (!res->class)
continue; // dummy_resource;
dom = mpam_resctrl_get_domain_from_cpu(cpu, res);
if (!dom) {
dom = mpam_resctrl_alloc_domain(cpu, res);
if (IS_ERR(dom))
return PTR_ERR(dom);
} else {
if (r->alloc_capable) {
struct rdt_ctrl_domain *ctrl_d = &dom->resctrl_ctrl_dom;
mpam_resctrl_online_domain_hdr(cpu, &ctrl_d->hdr);
}
}
}
resctrl_online_cpu(cpu);
return 0;
}
void mpam_resctrl_offline_cpu(unsigned int cpu)
{
struct mpam_resctrl_res *res;
enum resctrl_res_level rid;
resctrl_offline_cpu(cpu);
guard(mutex)(&domain_list_lock);
for_each_mpam_resctrl_control(res, rid) {
struct mpam_resctrl_dom *dom;
struct rdt_ctrl_domain *ctrl_d;
bool ctrl_dom_empty;
struct rdt_resource *r = &res->resctrl_res;
if (!res->class)
continue; // dummy resource
dom = mpam_resctrl_get_domain_from_cpu(cpu, res);
if (WARN_ON_ONCE(!dom))
continue;
if (r->alloc_capable) {
ctrl_d = &dom->resctrl_ctrl_dom;
ctrl_dom_empty = mpam_resctrl_offline_domain_hdr(cpu, &ctrl_d->hdr);
if (ctrl_dom_empty)
resctrl_offline_ctrl_domain(&res->resctrl_res, ctrl_d);
} else {
ctrl_dom_empty = true;
}
if (ctrl_dom_empty)
kfree(dom);
}
}
int mpam_resctrl_setup(void)
{
int err = 0;
struct mpam_resctrl_res *res;
enum resctrl_res_level rid;
cpus_read_lock();
for_each_mpam_resctrl_control(res, rid) {
INIT_LIST_HEAD_RCU(&res->resctrl_res.ctrl_domains);
res->resctrl_res.rid = rid;
}
/* Find some classes to use for controls */
mpam_resctrl_pick_caches();
/* Initialise the resctrl structures from the classes */
for_each_mpam_resctrl_control(res, rid) {
if (!res->class)
continue; // dummy resource
err = mpam_resctrl_control_init(res);
if (err) {
pr_debug("Failed to initialise rid %u\n", rid);
break;
}
}
cpus_read_unlock();
if (err) {
pr_debug("Internal error %d - resctrl not supported\n", err);
return err;
}
if (!resctrl_arch_alloc_capable()) {
pr_debug("No alloc(%u) found - resctrl not supported\n",
resctrl_arch_alloc_capable());
return -EOPNOTSUPP;
}
/* TODO: call resctrl_init() */
return 0;
}
|