Skip to content

Commit e557999

Browse files
James Morsebp3tk0v
authored andcommitted
x86/resctrl: Allow arch to allocate memory needed in resctrl_arch_rmid_read()
Depending on the number of monitors available, Arm's MPAM may need to allocate a monitor prior to reading the counter value. Allocating a contended resource may involve sleeping. __check_limbo() and mon_event_count() each make multiple calls to resctrl_arch_rmid_read(), to avoid extra work on contended systems, the allocation should be valid for multiple invocations of resctrl_arch_rmid_read(). The memory or hardware allocated is not specific to a domain. Add arch hooks for this allocation, which need calling before resctrl_arch_rmid_read(). The allocated monitor is passed to resctrl_arch_rmid_read(), then freed again afterwards. The helper can be called on any CPU, and can sleep. Signed-off-by: James Morse <james.morse@arm.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reviewed-by: Babu Moger <babu.moger@amd.com> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com> Tested-by: Peter Newman <peternewman@google.com> Tested-by: Babu Moger <babu.moger@amd.com> Tested-by: Carl Worth <carl@os.amperecomputing.com> # arm64 Link: https://lore.kernel.org/r/20240213184438.16675-16-james.morse@arm.com Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
1 parent 6fde142 commit e557999

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

arch/x86/include/asm/resctrl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ static inline u32 resctrl_arch_rmid_idx_encode(u32 ignored, u32 rmid)
136136
return rmid;
137137
}
138138

139+
/* x86 can always read an rmid, nothing needs allocating */
140+
struct rdt_resource;
141+
static inline void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, int evtid)
142+
{
143+
might_sleep();
144+
return NULL;
145+
};
146+
147+
static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r, int evtid,
148+
void *ctx) { };
149+
139150
void resctrl_cpu_detect(struct cpuinfo_x86 *c);
140151

141152
#else

arch/x86/kernel/cpu/resctrl/ctrlmondata.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
546546
rr->d = d;
547547
rr->val = 0;
548548
rr->first = first;
549+
rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, evtid);
550+
if (IS_ERR(rr->arch_mon_ctx)) {
551+
rr->err = -EINVAL;
552+
return;
553+
}
549554

550555
cpu = cpumask_any_housekeeping(&d->cpu_mask);
551556

@@ -559,6 +564,8 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
559564
smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
560565
else
561566
smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
567+
568+
resctrl_arch_mon_ctx_free(r, evtid, rr->arch_mon_ctx);
562569
}
563570

564571
int rdtgroup_mondata_show(struct seq_file *m, void *arg)

arch/x86/kernel/cpu/resctrl/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct rmid_read {
137137
bool first;
138138
int err;
139139
u64 val;
140+
void *arch_mon_ctx;
140141
};
141142

142143
extern bool rdt_alloc_capable;

arch/x86/kernel/cpu/resctrl/monitor.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static u64 mbm_overflow_count(u64 prev_msr, u64 cur_msr, unsigned int width)
269269

270270
int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain *d,
271271
u32 unused, u32 rmid, enum resctrl_event_id eventid,
272-
u64 *val)
272+
u64 *val, void *ignored)
273273
{
274274
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
275275
struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
@@ -324,9 +324,17 @@ void __check_limbo(struct rdt_domain *d, bool force_free)
324324
u32 idx_limit = resctrl_arch_system_num_rmid_idx();
325325
struct rmid_entry *entry;
326326
u32 idx, cur_idx = 1;
327+
void *arch_mon_ctx;
327328
bool rmid_dirty;
328329
u64 val = 0;
329330

331+
arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, QOS_L3_OCCUP_EVENT_ID);
332+
if (IS_ERR(arch_mon_ctx)) {
333+
pr_warn_ratelimited("Failed to allocate monitor context: %ld",
334+
PTR_ERR(arch_mon_ctx));
335+
return;
336+
}
337+
330338
/*
331339
* Skip RMID 0 and start from RMID 1 and check all the RMIDs that
332340
* are marked as busy for occupancy < threshold. If the occupancy
@@ -340,7 +348,8 @@ void __check_limbo(struct rdt_domain *d, bool force_free)
340348

341349
entry = __rmid_entry(idx);
342350
if (resctrl_arch_rmid_read(r, d, entry->closid, entry->rmid,
343-
QOS_L3_OCCUP_EVENT_ID, &val)) {
351+
QOS_L3_OCCUP_EVENT_ID, &val,
352+
arch_mon_ctx)) {
344353
rmid_dirty = true;
345354
} else {
346355
rmid_dirty = (val >= resctrl_rmid_realloc_threshold);
@@ -353,6 +362,8 @@ void __check_limbo(struct rdt_domain *d, bool force_free)
353362
}
354363
cur_idx = idx + 1;
355364
}
365+
366+
resctrl_arch_mon_ctx_free(r, QOS_L3_OCCUP_EVENT_ID, arch_mon_ctx);
356367
}
357368

358369
bool has_busy_rmid(struct rdt_domain *d)
@@ -533,7 +544,7 @@ static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
533544
}
534545

535546
rr->err = resctrl_arch_rmid_read(rr->r, rr->d, closid, rmid, rr->evtid,
536-
&tval);
547+
&tval, rr->arch_mon_ctx);
537548
if (rr->err)
538549
return rr->err;
539550

@@ -722,11 +733,27 @@ static void mbm_update(struct rdt_resource *r, struct rdt_domain *d,
722733
if (is_mbm_total_enabled()) {
723734
rr.evtid = QOS_L3_MBM_TOTAL_EVENT_ID;
724735
rr.val = 0;
736+
rr.arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr.r, rr.evtid);
737+
if (IS_ERR(rr.arch_mon_ctx)) {
738+
pr_warn_ratelimited("Failed to allocate monitor context: %ld",
739+
PTR_ERR(rr.arch_mon_ctx));
740+
return;
741+
}
742+
725743
__mon_event_count(closid, rmid, &rr);
744+
745+
resctrl_arch_mon_ctx_free(rr.r, rr.evtid, rr.arch_mon_ctx);
726746
}
727747
if (is_mbm_local_enabled()) {
728748
rr.evtid = QOS_L3_MBM_LOCAL_EVENT_ID;
729749
rr.val = 0;
750+
rr.arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr.r, rr.evtid);
751+
if (IS_ERR(rr.arch_mon_ctx)) {
752+
pr_warn_ratelimited("Failed to allocate monitor context: %ld",
753+
PTR_ERR(rr.arch_mon_ctx));
754+
return;
755+
}
756+
730757
__mon_event_count(closid, rmid, &rr);
731758

732759
/*
@@ -736,6 +763,8 @@ static void mbm_update(struct rdt_resource *r, struct rdt_domain *d,
736763
*/
737764
if (is_mba_sc(NULL))
738765
mbm_bw_count(closid, rmid, &rr);
766+
767+
resctrl_arch_mon_ctx_free(rr.r, rr.evtid, rr.arch_mon_ctx);
739768
}
740769
}
741770

include/linux/resctrl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d);
235235
* @rmid: rmid of the counter to read.
236236
* @eventid: eventid to read, e.g. L3 occupancy.
237237
* @val: result of the counter read in bytes.
238+
* @arch_mon_ctx: An architecture specific value from
239+
* resctrl_arch_mon_ctx_alloc(), for MPAM this identifies
240+
* the hardware monitor allocated for this read request.
238241
*
239242
* Some architectures need to sleep when first programming some of the counters.
240243
* (specifically: arm64's MPAM cache occupancy counters can return 'not ready'
@@ -248,7 +251,7 @@ void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d);
248251
*/
249252
int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain *d,
250253
u32 closid, u32 rmid, enum resctrl_event_id eventid,
251-
u64 *val);
254+
u64 *val, void *arch_mon_ctx);
252255

253256
/**
254257
* resctrl_arch_rmid_read_context_check() - warn about invalid contexts

0 commit comments

Comments
 (0)