monitor.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Resource Director Technology(RDT)
  4. * - Monitoring code
  5. *
  6. * Copyright (C) 2017 Intel Corporation
  7. *
  8. * Author:
  9. * Vikas Shivappa <vikas.shivappa@intel.com>
  10. *
  11. * This replaces the cqm.c based on perf but we reuse a lot of
  12. * code and datastructures originally from Peter Zijlstra and Matt Fleming.
  13. *
  14. * More information about RDT be found in the Intel (R) x86 Architecture
  15. * Software Developer Manual June 2016, volume 3, section 17.17.
  16. */
  17. #define pr_fmt(fmt) "resctrl: " fmt
  18. #include <linux/cpu.h>
  19. #include <linux/module.h>
  20. #include <linux/sizes.h>
  21. #include <linux/slab.h>
  22. #include <asm/cpu_device_id.h>
  23. #include <asm/resctrl.h>
  24. #include "internal.h"
  25. #include "trace.h"
  26. /**
  27. * struct rmid_entry - dirty tracking for all RMID.
  28. * @closid: The CLOSID for this entry.
  29. * @rmid: The RMID for this entry.
  30. * @busy: The number of domains with cached data using this RMID.
  31. * @list: Member of the rmid_free_lru list when busy == 0.
  32. *
  33. * Depending on the architecture the correct monitor is accessed using
  34. * both @closid and @rmid, or @rmid only.
  35. *
  36. * Take the rdtgroup_mutex when accessing.
  37. */
  38. struct rmid_entry {
  39. u32 closid;
  40. u32 rmid;
  41. int busy;
  42. struct list_head list;
  43. };
  44. /*
  45. * @rmid_free_lru - A least recently used list of free RMIDs
  46. * These RMIDs are guaranteed to have an occupancy less than the
  47. * threshold occupancy
  48. */
  49. static LIST_HEAD(rmid_free_lru);
  50. /*
  51. * @closid_num_dirty_rmid The number of dirty RMID each CLOSID has.
  52. * Only allocated when CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID is defined.
  53. * Indexed by CLOSID. Protected by rdtgroup_mutex.
  54. */
  55. static u32 *closid_num_dirty_rmid;
  56. /*
  57. * @rmid_limbo_count - count of currently unused but (potentially)
  58. * dirty RMIDs.
  59. * This counts RMIDs that no one is currently using but that
  60. * may have a occupancy value > resctrl_rmid_realloc_threshold. User can
  61. * change the threshold occupancy value.
  62. */
  63. static unsigned int rmid_limbo_count;
  64. /*
  65. * @rmid_entry - The entry in the limbo and free lists.
  66. */
  67. static struct rmid_entry *rmid_ptrs;
  68. /*
  69. * Global boolean for rdt_monitor which is true if any
  70. * resource monitoring is enabled.
  71. */
  72. bool rdt_mon_capable;
  73. /*
  74. * Global to indicate which monitoring events are enabled.
  75. */
  76. unsigned int rdt_mon_features;
  77. /*
  78. * This is the threshold cache occupancy in bytes at which we will consider an
  79. * RMID available for re-allocation.
  80. */
  81. unsigned int resctrl_rmid_realloc_threshold;
  82. /*
  83. * This is the maximum value for the reallocation threshold, in bytes.
  84. */
  85. unsigned int resctrl_rmid_realloc_limit;
  86. #define CF(cf) ((unsigned long)(1048576 * (cf) + 0.5))
  87. static int snc_nodes_per_l3_cache = 1;
  88. /*
  89. * The correction factor table is documented in Documentation/arch/x86/resctrl.rst.
  90. * If rmid > rmid threshold, MBM total and local values should be multiplied
  91. * by the correction factor.
  92. *
  93. * The original table is modified for better code:
  94. *
  95. * 1. The threshold 0 is changed to rmid count - 1 so don't do correction
  96. * for the case.
  97. * 2. MBM total and local correction table indexed by core counter which is
  98. * equal to (x86_cache_max_rmid + 1) / 8 - 1 and is from 0 up to 27.
  99. * 3. The correction factor is normalized to 2^20 (1048576) so it's faster
  100. * to calculate corrected value by shifting:
  101. * corrected_value = (original_value * correction_factor) >> 20
  102. */
  103. static const struct mbm_correction_factor_table {
  104. u32 rmidthreshold;
  105. u64 cf;
  106. } mbm_cf_table[] __initconst = {
  107. {7, CF(1.000000)},
  108. {15, CF(1.000000)},
  109. {15, CF(0.969650)},
  110. {31, CF(1.000000)},
  111. {31, CF(1.066667)},
  112. {31, CF(0.969650)},
  113. {47, CF(1.142857)},
  114. {63, CF(1.000000)},
  115. {63, CF(1.185115)},
  116. {63, CF(1.066553)},
  117. {79, CF(1.454545)},
  118. {95, CF(1.000000)},
  119. {95, CF(1.230769)},
  120. {95, CF(1.142857)},
  121. {95, CF(1.066667)},
  122. {127, CF(1.000000)},
  123. {127, CF(1.254863)},
  124. {127, CF(1.185255)},
  125. {151, CF(1.000000)},
  126. {127, CF(1.066667)},
  127. {167, CF(1.000000)},
  128. {159, CF(1.454334)},
  129. {183, CF(1.000000)},
  130. {127, CF(0.969744)},
  131. {191, CF(1.280246)},
  132. {191, CF(1.230921)},
  133. {215, CF(1.000000)},
  134. {191, CF(1.143118)},
  135. };
  136. static u32 mbm_cf_rmidthreshold __read_mostly = UINT_MAX;
  137. static u64 mbm_cf __read_mostly;
  138. static inline u64 get_corrected_mbm_count(u32 rmid, unsigned long val)
  139. {
  140. /* Correct MBM value. */
  141. if (rmid > mbm_cf_rmidthreshold)
  142. val = (val * mbm_cf) >> 20;
  143. return val;
  144. }
  145. /*
  146. * x86 and arm64 differ in their handling of monitoring.
  147. * x86's RMID are independent numbers, there is only one source of traffic
  148. * with an RMID value of '1'.
  149. * arm64's PMG extends the PARTID/CLOSID space, there are multiple sources of
  150. * traffic with a PMG value of '1', one for each CLOSID, meaning the RMID
  151. * value is no longer unique.
  152. * To account for this, resctrl uses an index. On x86 this is just the RMID,
  153. * on arm64 it encodes the CLOSID and RMID. This gives a unique number.
  154. *
  155. * The domain's rmid_busy_llc and rmid_ptrs[] are sized by index. The arch code
  156. * must accept an attempt to read every index.
  157. */
  158. static inline struct rmid_entry *__rmid_entry(u32 idx)
  159. {
  160. struct rmid_entry *entry;
  161. u32 closid, rmid;
  162. entry = &rmid_ptrs[idx];
  163. resctrl_arch_rmid_idx_decode(idx, &closid, &rmid);
  164. WARN_ON_ONCE(entry->closid != closid);
  165. WARN_ON_ONCE(entry->rmid != rmid);
  166. return entry;
  167. }
  168. /*
  169. * When Sub-NUMA Cluster (SNC) mode is not enabled (as indicated by
  170. * "snc_nodes_per_l3_cache == 1") no translation of the RMID value is
  171. * needed. The physical RMID is the same as the logical RMID.
  172. *
  173. * On a platform with SNC mode enabled, Linux enables RMID sharing mode
  174. * via MSR 0xCA0 (see the "RMID Sharing Mode" section in the "Intel
  175. * Resource Director Technology Architecture Specification" for a full
  176. * description of RMID sharing mode).
  177. *
  178. * In RMID sharing mode there are fewer "logical RMID" values available
  179. * to accumulate data ("physical RMIDs" are divided evenly between SNC
  180. * nodes that share an L3 cache). Linux creates an rdt_mon_domain for
  181. * each SNC node.
  182. *
  183. * The value loaded into IA32_PQR_ASSOC is the "logical RMID".
  184. *
  185. * Data is collected independently on each SNC node and can be retrieved
  186. * using the "physical RMID" value computed by this function and loaded
  187. * into IA32_QM_EVTSEL. @cpu can be any CPU in the SNC node.
  188. *
  189. * The scope of the IA32_QM_EVTSEL and IA32_QM_CTR MSRs is at the L3
  190. * cache. So a "physical RMID" may be read from any CPU that shares
  191. * the L3 cache with the desired SNC node, not just from a CPU in
  192. * the specific SNC node.
  193. */
  194. static int logical_rmid_to_physical_rmid(int cpu, int lrmid)
  195. {
  196. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  197. if (snc_nodes_per_l3_cache == 1)
  198. return lrmid;
  199. return lrmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
  200. }
  201. static int __rmid_read_phys(u32 prmid, enum resctrl_event_id eventid, u64 *val)
  202. {
  203. u64 msr_val;
  204. /*
  205. * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
  206. * with a valid event code for supported resource type and the bits
  207. * IA32_QM_EVTSEL.RMID (bits 41:32) are configured with valid RMID,
  208. * IA32_QM_CTR.data (bits 61:0) reports the monitored data.
  209. * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
  210. * are error bits.
  211. */
  212. wrmsr(MSR_IA32_QM_EVTSEL, eventid, prmid);
  213. rdmsrl(MSR_IA32_QM_CTR, msr_val);
  214. if (msr_val & RMID_VAL_ERROR)
  215. return -EIO;
  216. if (msr_val & RMID_VAL_UNAVAIL)
  217. return -EINVAL;
  218. *val = msr_val;
  219. return 0;
  220. }
  221. static struct arch_mbm_state *get_arch_mbm_state(struct rdt_hw_mon_domain *hw_dom,
  222. u32 rmid,
  223. enum resctrl_event_id eventid)
  224. {
  225. switch (eventid) {
  226. case QOS_L3_OCCUP_EVENT_ID:
  227. return NULL;
  228. case QOS_L3_MBM_TOTAL_EVENT_ID:
  229. return &hw_dom->arch_mbm_total[rmid];
  230. case QOS_L3_MBM_LOCAL_EVENT_ID:
  231. return &hw_dom->arch_mbm_local[rmid];
  232. }
  233. /* Never expect to get here */
  234. WARN_ON_ONCE(1);
  235. return NULL;
  236. }
  237. void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
  238. u32 unused, u32 rmid,
  239. enum resctrl_event_id eventid)
  240. {
  241. struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
  242. int cpu = cpumask_any(&d->hdr.cpu_mask);
  243. struct arch_mbm_state *am;
  244. u32 prmid;
  245. am = get_arch_mbm_state(hw_dom, rmid, eventid);
  246. if (am) {
  247. memset(am, 0, sizeof(*am));
  248. prmid = logical_rmid_to_physical_rmid(cpu, rmid);
  249. /* Record any initial, non-zero count value. */
  250. __rmid_read_phys(prmid, eventid, &am->prev_msr);
  251. }
  252. }
  253. /*
  254. * Assumes that hardware counters are also reset and thus that there is
  255. * no need to record initial non-zero counts.
  256. */
  257. void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d)
  258. {
  259. struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
  260. if (is_mbm_total_enabled())
  261. memset(hw_dom->arch_mbm_total, 0,
  262. sizeof(*hw_dom->arch_mbm_total) * r->num_rmid);
  263. if (is_mbm_local_enabled())
  264. memset(hw_dom->arch_mbm_local, 0,
  265. sizeof(*hw_dom->arch_mbm_local) * r->num_rmid);
  266. }
  267. static u64 mbm_overflow_count(u64 prev_msr, u64 cur_msr, unsigned int width)
  268. {
  269. u64 shift = 64 - width, chunks;
  270. chunks = (cur_msr << shift) - (prev_msr << shift);
  271. return chunks >> shift;
  272. }
  273. static u64 get_corrected_val(struct rdt_resource *r, struct rdt_mon_domain *d,
  274. u32 rmid, enum resctrl_event_id eventid, u64 msr_val)
  275. {
  276. struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
  277. struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
  278. struct arch_mbm_state *am;
  279. u64 chunks;
  280. am = get_arch_mbm_state(hw_dom, rmid, eventid);
  281. if (am) {
  282. am->chunks += mbm_overflow_count(am->prev_msr, msr_val,
  283. hw_res->mbm_width);
  284. chunks = get_corrected_mbm_count(rmid, am->chunks);
  285. am->prev_msr = msr_val;
  286. } else {
  287. chunks = msr_val;
  288. }
  289. return chunks * hw_res->mon_scale;
  290. }
  291. int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
  292. u32 unused, u32 rmid, enum resctrl_event_id eventid,
  293. u64 *val, void *ignored)
  294. {
  295. struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
  296. int cpu = cpumask_any(&d->hdr.cpu_mask);
  297. struct arch_mbm_state *am;
  298. u64 msr_val;
  299. u32 prmid;
  300. int ret;
  301. resctrl_arch_rmid_read_context_check();
  302. prmid = logical_rmid_to_physical_rmid(cpu, rmid);
  303. ret = __rmid_read_phys(prmid, eventid, &msr_val);
  304. if (!ret) {
  305. *val = get_corrected_val(r, d, rmid, eventid, msr_val);
  306. } else if (ret == -EINVAL) {
  307. am = get_arch_mbm_state(hw_dom, rmid, eventid);
  308. if (am)
  309. am->prev_msr = 0;
  310. }
  311. return ret;
  312. }
  313. static void limbo_release_entry(struct rmid_entry *entry)
  314. {
  315. lockdep_assert_held(&rdtgroup_mutex);
  316. rmid_limbo_count--;
  317. list_add_tail(&entry->list, &rmid_free_lru);
  318. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))
  319. closid_num_dirty_rmid[entry->closid]--;
  320. }
  321. /*
  322. * Check the RMIDs that are marked as busy for this domain. If the
  323. * reported LLC occupancy is below the threshold clear the busy bit and
  324. * decrement the count. If the busy count gets to zero on an RMID, we
  325. * free the RMID
  326. */
  327. void __check_limbo(struct rdt_mon_domain *d, bool force_free)
  328. {
  329. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  330. u32 idx_limit = resctrl_arch_system_num_rmid_idx();
  331. struct rmid_entry *entry;
  332. u32 idx, cur_idx = 1;
  333. void *arch_mon_ctx;
  334. bool rmid_dirty;
  335. u64 val = 0;
  336. arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, QOS_L3_OCCUP_EVENT_ID);
  337. if (IS_ERR(arch_mon_ctx)) {
  338. pr_warn_ratelimited("Failed to allocate monitor context: %ld",
  339. PTR_ERR(arch_mon_ctx));
  340. return;
  341. }
  342. /*
  343. * Skip RMID 0 and start from RMID 1 and check all the RMIDs that
  344. * are marked as busy for occupancy < threshold. If the occupancy
  345. * is less than the threshold decrement the busy counter of the
  346. * RMID and move it to the free list when the counter reaches 0.
  347. */
  348. for (;;) {
  349. idx = find_next_bit(d->rmid_busy_llc, idx_limit, cur_idx);
  350. if (idx >= idx_limit)
  351. break;
  352. entry = __rmid_entry(idx);
  353. if (resctrl_arch_rmid_read(r, d, entry->closid, entry->rmid,
  354. QOS_L3_OCCUP_EVENT_ID, &val,
  355. arch_mon_ctx)) {
  356. rmid_dirty = true;
  357. } else {
  358. rmid_dirty = (val >= resctrl_rmid_realloc_threshold);
  359. /*
  360. * x86's CLOSID and RMID are independent numbers, so the entry's
  361. * CLOSID is an empty CLOSID (X86_RESCTRL_EMPTY_CLOSID). On Arm the
  362. * RMID (PMG) extends the CLOSID (PARTID) space with bits that aren't
  363. * used to select the configuration. It is thus necessary to track both
  364. * CLOSID and RMID because there may be dependencies between them
  365. * on some architectures.
  366. */
  367. trace_mon_llc_occupancy_limbo(entry->closid, entry->rmid, d->hdr.id, val);
  368. }
  369. if (force_free || !rmid_dirty) {
  370. clear_bit(idx, d->rmid_busy_llc);
  371. if (!--entry->busy)
  372. limbo_release_entry(entry);
  373. }
  374. cur_idx = idx + 1;
  375. }
  376. resctrl_arch_mon_ctx_free(r, QOS_L3_OCCUP_EVENT_ID, arch_mon_ctx);
  377. }
  378. bool has_busy_rmid(struct rdt_mon_domain *d)
  379. {
  380. u32 idx_limit = resctrl_arch_system_num_rmid_idx();
  381. return find_first_bit(d->rmid_busy_llc, idx_limit) != idx_limit;
  382. }
  383. static struct rmid_entry *resctrl_find_free_rmid(u32 closid)
  384. {
  385. struct rmid_entry *itr;
  386. u32 itr_idx, cmp_idx;
  387. if (list_empty(&rmid_free_lru))
  388. return rmid_limbo_count ? ERR_PTR(-EBUSY) : ERR_PTR(-ENOSPC);
  389. list_for_each_entry(itr, &rmid_free_lru, list) {
  390. /*
  391. * Get the index of this free RMID, and the index it would need
  392. * to be if it were used with this CLOSID.
  393. * If the CLOSID is irrelevant on this architecture, the two
  394. * index values are always the same on every entry and thus the
  395. * very first entry will be returned.
  396. */
  397. itr_idx = resctrl_arch_rmid_idx_encode(itr->closid, itr->rmid);
  398. cmp_idx = resctrl_arch_rmid_idx_encode(closid, itr->rmid);
  399. if (itr_idx == cmp_idx)
  400. return itr;
  401. }
  402. return ERR_PTR(-ENOSPC);
  403. }
  404. /**
  405. * resctrl_find_cleanest_closid() - Find a CLOSID where all the associated
  406. * RMID are clean, or the CLOSID that has
  407. * the most clean RMID.
  408. *
  409. * MPAM's equivalent of RMID are per-CLOSID, meaning a freshly allocated CLOSID
  410. * may not be able to allocate clean RMID. To avoid this the allocator will
  411. * choose the CLOSID with the most clean RMID.
  412. *
  413. * When the CLOSID and RMID are independent numbers, the first free CLOSID will
  414. * be returned.
  415. */
  416. int resctrl_find_cleanest_closid(void)
  417. {
  418. u32 cleanest_closid = ~0;
  419. int i = 0;
  420. lockdep_assert_held(&rdtgroup_mutex);
  421. if (!IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))
  422. return -EIO;
  423. for (i = 0; i < closids_supported(); i++) {
  424. int num_dirty;
  425. if (closid_allocated(i))
  426. continue;
  427. num_dirty = closid_num_dirty_rmid[i];
  428. if (num_dirty == 0)
  429. return i;
  430. if (cleanest_closid == ~0)
  431. cleanest_closid = i;
  432. if (num_dirty < closid_num_dirty_rmid[cleanest_closid])
  433. cleanest_closid = i;
  434. }
  435. if (cleanest_closid == ~0)
  436. return -ENOSPC;
  437. return cleanest_closid;
  438. }
  439. /*
  440. * For MPAM the RMID value is not unique, and has to be considered with
  441. * the CLOSID. The (CLOSID, RMID) pair is allocated on all domains, which
  442. * allows all domains to be managed by a single free list.
  443. * Each domain also has a rmid_busy_llc to reduce the work of the limbo handler.
  444. */
  445. int alloc_rmid(u32 closid)
  446. {
  447. struct rmid_entry *entry;
  448. lockdep_assert_held(&rdtgroup_mutex);
  449. entry = resctrl_find_free_rmid(closid);
  450. if (IS_ERR(entry))
  451. return PTR_ERR(entry);
  452. list_del(&entry->list);
  453. return entry->rmid;
  454. }
  455. static void add_rmid_to_limbo(struct rmid_entry *entry)
  456. {
  457. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  458. struct rdt_mon_domain *d;
  459. u32 idx;
  460. lockdep_assert_held(&rdtgroup_mutex);
  461. /* Walking r->domains, ensure it can't race with cpuhp */
  462. lockdep_assert_cpus_held();
  463. idx = resctrl_arch_rmid_idx_encode(entry->closid, entry->rmid);
  464. entry->busy = 0;
  465. list_for_each_entry(d, &r->mon_domains, hdr.list) {
  466. /*
  467. * For the first limbo RMID in the domain,
  468. * setup up the limbo worker.
  469. */
  470. if (!has_busy_rmid(d))
  471. cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL,
  472. RESCTRL_PICK_ANY_CPU);
  473. set_bit(idx, d->rmid_busy_llc);
  474. entry->busy++;
  475. }
  476. rmid_limbo_count++;
  477. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))
  478. closid_num_dirty_rmid[entry->closid]++;
  479. }
  480. void free_rmid(u32 closid, u32 rmid)
  481. {
  482. u32 idx = resctrl_arch_rmid_idx_encode(closid, rmid);
  483. struct rmid_entry *entry;
  484. lockdep_assert_held(&rdtgroup_mutex);
  485. /*
  486. * Do not allow the default rmid to be free'd. Comparing by index
  487. * allows architectures that ignore the closid parameter to avoid an
  488. * unnecessary check.
  489. */
  490. if (!resctrl_arch_mon_capable() ||
  491. idx == resctrl_arch_rmid_idx_encode(RESCTRL_RESERVED_CLOSID,
  492. RESCTRL_RESERVED_RMID))
  493. return;
  494. entry = __rmid_entry(idx);
  495. if (is_llc_occupancy_enabled())
  496. add_rmid_to_limbo(entry);
  497. else
  498. list_add_tail(&entry->list, &rmid_free_lru);
  499. }
  500. static struct mbm_state *get_mbm_state(struct rdt_mon_domain *d, u32 closid,
  501. u32 rmid, enum resctrl_event_id evtid)
  502. {
  503. u32 idx = resctrl_arch_rmid_idx_encode(closid, rmid);
  504. switch (evtid) {
  505. case QOS_L3_MBM_TOTAL_EVENT_ID:
  506. return &d->mbm_total[idx];
  507. case QOS_L3_MBM_LOCAL_EVENT_ID:
  508. return &d->mbm_local[idx];
  509. default:
  510. return NULL;
  511. }
  512. }
  513. static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
  514. {
  515. int cpu = smp_processor_id();
  516. struct rdt_mon_domain *d;
  517. struct mbm_state *m;
  518. int err, ret;
  519. u64 tval = 0;
  520. if (rr->first) {
  521. resctrl_arch_reset_rmid(rr->r, rr->d, closid, rmid, rr->evtid);
  522. m = get_mbm_state(rr->d, closid, rmid, rr->evtid);
  523. if (m)
  524. memset(m, 0, sizeof(struct mbm_state));
  525. return 0;
  526. }
  527. if (rr->d) {
  528. /* Reading a single domain, must be on a CPU in that domain. */
  529. if (!cpumask_test_cpu(cpu, &rr->d->hdr.cpu_mask))
  530. return -EINVAL;
  531. rr->err = resctrl_arch_rmid_read(rr->r, rr->d, closid, rmid,
  532. rr->evtid, &tval, rr->arch_mon_ctx);
  533. if (rr->err)
  534. return rr->err;
  535. rr->val += tval;
  536. return 0;
  537. }
  538. /* Summing domains that share a cache, must be on a CPU for that cache. */
  539. if (!cpumask_test_cpu(cpu, &rr->ci->shared_cpu_map))
  540. return -EINVAL;
  541. /*
  542. * Legacy files must report the sum of an event across all
  543. * domains that share the same L3 cache instance.
  544. * Report success if a read from any domain succeeds, -EINVAL
  545. * (translated to "Unavailable" for user space) if reading from
  546. * all domains fail for any reason.
  547. */
  548. ret = -EINVAL;
  549. list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
  550. if (d->ci->id != rr->ci->id)
  551. continue;
  552. err = resctrl_arch_rmid_read(rr->r, d, closid, rmid,
  553. rr->evtid, &tval, rr->arch_mon_ctx);
  554. if (!err) {
  555. rr->val += tval;
  556. ret = 0;
  557. }
  558. }
  559. if (ret)
  560. rr->err = ret;
  561. return ret;
  562. }
  563. /*
  564. * mbm_bw_count() - Update bw count from values previously read by
  565. * __mon_event_count().
  566. * @closid: The closid used to identify the cached mbm_state.
  567. * @rmid: The rmid used to identify the cached mbm_state.
  568. * @rr: The struct rmid_read populated by __mon_event_count().
  569. *
  570. * Supporting function to calculate the memory bandwidth
  571. * and delta bandwidth in MBps. The chunks value previously read by
  572. * __mon_event_count() is compared with the chunks value from the previous
  573. * invocation. This must be called once per second to maintain values in MBps.
  574. */
  575. static void mbm_bw_count(u32 closid, u32 rmid, struct rmid_read *rr)
  576. {
  577. u32 idx = resctrl_arch_rmid_idx_encode(closid, rmid);
  578. struct mbm_state *m = &rr->d->mbm_local[idx];
  579. u64 cur_bw, bytes, cur_bytes;
  580. cur_bytes = rr->val;
  581. bytes = cur_bytes - m->prev_bw_bytes;
  582. m->prev_bw_bytes = cur_bytes;
  583. cur_bw = bytes / SZ_1M;
  584. m->prev_bw = cur_bw;
  585. }
  586. /*
  587. * This is scheduled by mon_event_read() to read the CQM/MBM counters
  588. * on a domain.
  589. */
  590. void mon_event_count(void *info)
  591. {
  592. struct rdtgroup *rdtgrp, *entry;
  593. struct rmid_read *rr = info;
  594. struct list_head *head;
  595. int ret;
  596. rdtgrp = rr->rgrp;
  597. ret = __mon_event_count(rdtgrp->closid, rdtgrp->mon.rmid, rr);
  598. /*
  599. * For Ctrl groups read data from child monitor groups and
  600. * add them together. Count events which are read successfully.
  601. * Discard the rmid_read's reporting errors.
  602. */
  603. head = &rdtgrp->mon.crdtgrp_list;
  604. if (rdtgrp->type == RDTCTRL_GROUP) {
  605. list_for_each_entry(entry, head, mon.crdtgrp_list) {
  606. if (__mon_event_count(entry->closid, entry->mon.rmid,
  607. rr) == 0)
  608. ret = 0;
  609. }
  610. }
  611. /*
  612. * __mon_event_count() calls for newly created monitor groups may
  613. * report -EINVAL/Unavailable if the monitor hasn't seen any traffic.
  614. * Discard error if any of the monitor event reads succeeded.
  615. */
  616. if (ret == 0)
  617. rr->err = 0;
  618. }
  619. /*
  620. * Feedback loop for MBA software controller (mba_sc)
  621. *
  622. * mba_sc is a feedback loop where we periodically read MBM counters and
  623. * adjust the bandwidth percentage values via the IA32_MBA_THRTL_MSRs so
  624. * that:
  625. *
  626. * current bandwidth(cur_bw) < user specified bandwidth(user_bw)
  627. *
  628. * This uses the MBM counters to measure the bandwidth and MBA throttle
  629. * MSRs to control the bandwidth for a particular rdtgrp. It builds on the
  630. * fact that resctrl rdtgroups have both monitoring and control.
  631. *
  632. * The frequency of the checks is 1s and we just tag along the MBM overflow
  633. * timer. Having 1s interval makes the calculation of bandwidth simpler.
  634. *
  635. * Although MBA's goal is to restrict the bandwidth to a maximum, there may
  636. * be a need to increase the bandwidth to avoid unnecessarily restricting
  637. * the L2 <-> L3 traffic.
  638. *
  639. * Since MBA controls the L2 external bandwidth where as MBM measures the
  640. * L3 external bandwidth the following sequence could lead to such a
  641. * situation.
  642. *
  643. * Consider an rdtgroup which had high L3 <-> memory traffic in initial
  644. * phases -> mba_sc kicks in and reduced bandwidth percentage values -> but
  645. * after some time rdtgroup has mostly L2 <-> L3 traffic.
  646. *
  647. * In this case we may restrict the rdtgroup's L2 <-> L3 traffic as its
  648. * throttle MSRs already have low percentage values. To avoid
  649. * unnecessarily restricting such rdtgroups, we also increase the bandwidth.
  650. */
  651. static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_mon_domain *dom_mbm)
  652. {
  653. u32 closid, rmid, cur_msr_val, new_msr_val;
  654. struct mbm_state *pmbm_data, *cmbm_data;
  655. struct rdt_ctrl_domain *dom_mba;
  656. struct rdt_resource *r_mba;
  657. u32 cur_bw, user_bw, idx;
  658. struct list_head *head;
  659. struct rdtgroup *entry;
  660. if (!is_mbm_local_enabled())
  661. return;
  662. r_mba = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
  663. closid = rgrp->closid;
  664. rmid = rgrp->mon.rmid;
  665. idx = resctrl_arch_rmid_idx_encode(closid, rmid);
  666. pmbm_data = &dom_mbm->mbm_local[idx];
  667. dom_mba = get_ctrl_domain_from_cpu(smp_processor_id(), r_mba);
  668. if (!dom_mba) {
  669. pr_warn_once("Failure to get domain for MBA update\n");
  670. return;
  671. }
  672. cur_bw = pmbm_data->prev_bw;
  673. user_bw = dom_mba->mbps_val[closid];
  674. /* MBA resource doesn't support CDP */
  675. cur_msr_val = resctrl_arch_get_config(r_mba, dom_mba, closid, CDP_NONE);
  676. /*
  677. * For Ctrl groups read data from child monitor groups.
  678. */
  679. head = &rgrp->mon.crdtgrp_list;
  680. list_for_each_entry(entry, head, mon.crdtgrp_list) {
  681. cmbm_data = &dom_mbm->mbm_local[entry->mon.rmid];
  682. cur_bw += cmbm_data->prev_bw;
  683. }
  684. /*
  685. * Scale up/down the bandwidth linearly for the ctrl group. The
  686. * bandwidth step is the bandwidth granularity specified by the
  687. * hardware.
  688. * Always increase throttling if current bandwidth is above the
  689. * target set by user.
  690. * But avoid thrashing up and down on every poll by checking
  691. * whether a decrease in throttling is likely to push the group
  692. * back over target. E.g. if currently throttling to 30% of bandwidth
  693. * on a system with 10% granularity steps, check whether moving to
  694. * 40% would go past the limit by multiplying current bandwidth by
  695. * "(30 + 10) / 30".
  696. */
  697. if (cur_msr_val > r_mba->membw.min_bw && user_bw < cur_bw) {
  698. new_msr_val = cur_msr_val - r_mba->membw.bw_gran;
  699. } else if (cur_msr_val < MAX_MBA_BW &&
  700. (user_bw > (cur_bw * (cur_msr_val + r_mba->membw.min_bw) / cur_msr_val))) {
  701. new_msr_val = cur_msr_val + r_mba->membw.bw_gran;
  702. } else {
  703. return;
  704. }
  705. resctrl_arch_update_one(r_mba, dom_mba, closid, CDP_NONE, new_msr_val);
  706. }
  707. static void mbm_update(struct rdt_resource *r, struct rdt_mon_domain *d,
  708. u32 closid, u32 rmid)
  709. {
  710. struct rmid_read rr = {0};
  711. rr.r = r;
  712. rr.d = d;
  713. /*
  714. * This is protected from concurrent reads from user
  715. * as both the user and we hold the global mutex.
  716. */
  717. if (is_mbm_total_enabled()) {
  718. rr.evtid = QOS_L3_MBM_TOTAL_EVENT_ID;
  719. rr.val = 0;
  720. rr.arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr.r, rr.evtid);
  721. if (IS_ERR(rr.arch_mon_ctx)) {
  722. pr_warn_ratelimited("Failed to allocate monitor context: %ld",
  723. PTR_ERR(rr.arch_mon_ctx));
  724. return;
  725. }
  726. __mon_event_count(closid, rmid, &rr);
  727. resctrl_arch_mon_ctx_free(rr.r, rr.evtid, rr.arch_mon_ctx);
  728. }
  729. if (is_mbm_local_enabled()) {
  730. rr.evtid = QOS_L3_MBM_LOCAL_EVENT_ID;
  731. rr.val = 0;
  732. rr.arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr.r, rr.evtid);
  733. if (IS_ERR(rr.arch_mon_ctx)) {
  734. pr_warn_ratelimited("Failed to allocate monitor context: %ld",
  735. PTR_ERR(rr.arch_mon_ctx));
  736. return;
  737. }
  738. __mon_event_count(closid, rmid, &rr);
  739. /*
  740. * Call the MBA software controller only for the
  741. * control groups and when user has enabled
  742. * the software controller explicitly.
  743. */
  744. if (is_mba_sc(NULL))
  745. mbm_bw_count(closid, rmid, &rr);
  746. resctrl_arch_mon_ctx_free(rr.r, rr.evtid, rr.arch_mon_ctx);
  747. }
  748. }
  749. /*
  750. * Handler to scan the limbo list and move the RMIDs
  751. * to free list whose occupancy < threshold_occupancy.
  752. */
  753. void cqm_handle_limbo(struct work_struct *work)
  754. {
  755. unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
  756. struct rdt_mon_domain *d;
  757. cpus_read_lock();
  758. mutex_lock(&rdtgroup_mutex);
  759. d = container_of(work, struct rdt_mon_domain, cqm_limbo.work);
  760. __check_limbo(d, false);
  761. if (has_busy_rmid(d)) {
  762. d->cqm_work_cpu = cpumask_any_housekeeping(&d->hdr.cpu_mask,
  763. RESCTRL_PICK_ANY_CPU);
  764. schedule_delayed_work_on(d->cqm_work_cpu, &d->cqm_limbo,
  765. delay);
  766. }
  767. mutex_unlock(&rdtgroup_mutex);
  768. cpus_read_unlock();
  769. }
  770. /**
  771. * cqm_setup_limbo_handler() - Schedule the limbo handler to run for this
  772. * domain.
  773. * @dom: The domain the limbo handler should run for.
  774. * @delay_ms: How far in the future the handler should run.
  775. * @exclude_cpu: Which CPU the handler should not run on,
  776. * RESCTRL_PICK_ANY_CPU to pick any CPU.
  777. */
  778. void cqm_setup_limbo_handler(struct rdt_mon_domain *dom, unsigned long delay_ms,
  779. int exclude_cpu)
  780. {
  781. unsigned long delay = msecs_to_jiffies(delay_ms);
  782. int cpu;
  783. cpu = cpumask_any_housekeeping(&dom->hdr.cpu_mask, exclude_cpu);
  784. dom->cqm_work_cpu = cpu;
  785. if (cpu < nr_cpu_ids)
  786. schedule_delayed_work_on(cpu, &dom->cqm_limbo, delay);
  787. }
  788. void mbm_handle_overflow(struct work_struct *work)
  789. {
  790. unsigned long delay = msecs_to_jiffies(MBM_OVERFLOW_INTERVAL);
  791. struct rdtgroup *prgrp, *crgrp;
  792. struct rdt_mon_domain *d;
  793. struct list_head *head;
  794. struct rdt_resource *r;
  795. cpus_read_lock();
  796. mutex_lock(&rdtgroup_mutex);
  797. /*
  798. * If the filesystem has been unmounted this work no longer needs to
  799. * run.
  800. */
  801. if (!resctrl_mounted || !resctrl_arch_mon_capable())
  802. goto out_unlock;
  803. r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  804. d = container_of(work, struct rdt_mon_domain, mbm_over.work);
  805. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  806. mbm_update(r, d, prgrp->closid, prgrp->mon.rmid);
  807. head = &prgrp->mon.crdtgrp_list;
  808. list_for_each_entry(crgrp, head, mon.crdtgrp_list)
  809. mbm_update(r, d, crgrp->closid, crgrp->mon.rmid);
  810. if (is_mba_sc(NULL))
  811. update_mba_bw(prgrp, d);
  812. }
  813. /*
  814. * Re-check for housekeeping CPUs. This allows the overflow handler to
  815. * move off a nohz_full CPU quickly.
  816. */
  817. d->mbm_work_cpu = cpumask_any_housekeeping(&d->hdr.cpu_mask,
  818. RESCTRL_PICK_ANY_CPU);
  819. schedule_delayed_work_on(d->mbm_work_cpu, &d->mbm_over, delay);
  820. out_unlock:
  821. mutex_unlock(&rdtgroup_mutex);
  822. cpus_read_unlock();
  823. }
  824. /**
  825. * mbm_setup_overflow_handler() - Schedule the overflow handler to run for this
  826. * domain.
  827. * @dom: The domain the overflow handler should run for.
  828. * @delay_ms: How far in the future the handler should run.
  829. * @exclude_cpu: Which CPU the handler should not run on,
  830. * RESCTRL_PICK_ANY_CPU to pick any CPU.
  831. */
  832. void mbm_setup_overflow_handler(struct rdt_mon_domain *dom, unsigned long delay_ms,
  833. int exclude_cpu)
  834. {
  835. unsigned long delay = msecs_to_jiffies(delay_ms);
  836. int cpu;
  837. /*
  838. * When a domain comes online there is no guarantee the filesystem is
  839. * mounted. If not, there is no need to catch counter overflow.
  840. */
  841. if (!resctrl_mounted || !resctrl_arch_mon_capable())
  842. return;
  843. cpu = cpumask_any_housekeeping(&dom->hdr.cpu_mask, exclude_cpu);
  844. dom->mbm_work_cpu = cpu;
  845. if (cpu < nr_cpu_ids)
  846. schedule_delayed_work_on(cpu, &dom->mbm_over, delay);
  847. }
  848. static int dom_data_init(struct rdt_resource *r)
  849. {
  850. u32 idx_limit = resctrl_arch_system_num_rmid_idx();
  851. u32 num_closid = resctrl_arch_get_num_closid(r);
  852. struct rmid_entry *entry = NULL;
  853. int err = 0, i;
  854. u32 idx;
  855. mutex_lock(&rdtgroup_mutex);
  856. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
  857. u32 *tmp;
  858. /*
  859. * If the architecture hasn't provided a sanitised value here,
  860. * this may result in larger arrays than necessary. Resctrl will
  861. * use a smaller system wide value based on the resources in
  862. * use.
  863. */
  864. tmp = kcalloc(num_closid, sizeof(*tmp), GFP_KERNEL);
  865. if (!tmp) {
  866. err = -ENOMEM;
  867. goto out_unlock;
  868. }
  869. closid_num_dirty_rmid = tmp;
  870. }
  871. rmid_ptrs = kcalloc(idx_limit, sizeof(struct rmid_entry), GFP_KERNEL);
  872. if (!rmid_ptrs) {
  873. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
  874. kfree(closid_num_dirty_rmid);
  875. closid_num_dirty_rmid = NULL;
  876. }
  877. err = -ENOMEM;
  878. goto out_unlock;
  879. }
  880. for (i = 0; i < idx_limit; i++) {
  881. entry = &rmid_ptrs[i];
  882. INIT_LIST_HEAD(&entry->list);
  883. resctrl_arch_rmid_idx_decode(i, &entry->closid, &entry->rmid);
  884. list_add_tail(&entry->list, &rmid_free_lru);
  885. }
  886. /*
  887. * RESCTRL_RESERVED_CLOSID and RESCTRL_RESERVED_RMID are special and
  888. * are always allocated. These are used for the rdtgroup_default
  889. * control group, which will be setup later in rdtgroup_init().
  890. */
  891. idx = resctrl_arch_rmid_idx_encode(RESCTRL_RESERVED_CLOSID,
  892. RESCTRL_RESERVED_RMID);
  893. entry = __rmid_entry(idx);
  894. list_del(&entry->list);
  895. out_unlock:
  896. mutex_unlock(&rdtgroup_mutex);
  897. return err;
  898. }
  899. static void __exit dom_data_exit(void)
  900. {
  901. mutex_lock(&rdtgroup_mutex);
  902. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
  903. kfree(closid_num_dirty_rmid);
  904. closid_num_dirty_rmid = NULL;
  905. }
  906. kfree(rmid_ptrs);
  907. rmid_ptrs = NULL;
  908. mutex_unlock(&rdtgroup_mutex);
  909. }
  910. static struct mon_evt llc_occupancy_event = {
  911. .name = "llc_occupancy",
  912. .evtid = QOS_L3_OCCUP_EVENT_ID,
  913. };
  914. static struct mon_evt mbm_total_event = {
  915. .name = "mbm_total_bytes",
  916. .evtid = QOS_L3_MBM_TOTAL_EVENT_ID,
  917. };
  918. static struct mon_evt mbm_local_event = {
  919. .name = "mbm_local_bytes",
  920. .evtid = QOS_L3_MBM_LOCAL_EVENT_ID,
  921. };
  922. /*
  923. * Initialize the event list for the resource.
  924. *
  925. * Note that MBM events are also part of RDT_RESOURCE_L3 resource
  926. * because as per the SDM the total and local memory bandwidth
  927. * are enumerated as part of L3 monitoring.
  928. */
  929. static void l3_mon_evt_init(struct rdt_resource *r)
  930. {
  931. INIT_LIST_HEAD(&r->evt_list);
  932. if (is_llc_occupancy_enabled())
  933. list_add_tail(&llc_occupancy_event.list, &r->evt_list);
  934. if (is_mbm_total_enabled())
  935. list_add_tail(&mbm_total_event.list, &r->evt_list);
  936. if (is_mbm_local_enabled())
  937. list_add_tail(&mbm_local_event.list, &r->evt_list);
  938. }
  939. /*
  940. * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
  941. * which indicates that RMIDs are configured in legacy mode.
  942. * This mode is incompatible with Linux resctrl semantics
  943. * as RMIDs are partitioned between SNC nodes, which requires
  944. * a user to know which RMID is allocated to a task.
  945. * Clearing bit 0 reconfigures the RMID counters for use
  946. * in RMID sharing mode. This mode is better for Linux.
  947. * The RMID space is divided between all SNC nodes with the
  948. * RMIDs renumbered to start from zero in each node when
  949. * counting operations from tasks. Code to read the counters
  950. * must adjust RMID counter numbers based on SNC node. See
  951. * logical_rmid_to_physical_rmid() for code that does this.
  952. */
  953. void arch_mon_domain_online(struct rdt_resource *r, struct rdt_mon_domain *d)
  954. {
  955. if (snc_nodes_per_l3_cache > 1)
  956. msr_clear_bit(MSR_RMID_SNC_CONFIG, 0);
  957. }
  958. /* CPU models that support MSR_RMID_SNC_CONFIG */
  959. static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
  960. X86_MATCH_VFM(INTEL_ICELAKE_X, 0),
  961. X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, 0),
  962. X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, 0),
  963. X86_MATCH_VFM(INTEL_GRANITERAPIDS_X, 0),
  964. X86_MATCH_VFM(INTEL_ATOM_CRESTMONT_X, 0),
  965. {}
  966. };
  967. /*
  968. * There isn't a simple hardware bit that indicates whether a CPU is running
  969. * in Sub-NUMA Cluster (SNC) mode. Infer the state by comparing the
  970. * number of CPUs sharing the L3 cache with CPU0 to the number of CPUs in
  971. * the same NUMA node as CPU0.
  972. * It is not possible to accurately determine SNC state if the system is
  973. * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
  974. * to L3 caches. It will be OK if system is booted with hyperthreading
  975. * disabled (since this doesn't affect the ratio).
  976. */
  977. static __init int snc_get_config(void)
  978. {
  979. struct cacheinfo *ci = get_cpu_cacheinfo_level(0, RESCTRL_L3_CACHE);
  980. const cpumask_t *node0_cpumask;
  981. int cpus_per_node, cpus_per_l3;
  982. int ret;
  983. if (!x86_match_cpu(snc_cpu_ids) || !ci)
  984. return 1;
  985. cpus_read_lock();
  986. if (num_online_cpus() != num_present_cpus())
  987. pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
  988. cpus_read_unlock();
  989. node0_cpumask = cpumask_of_node(cpu_to_node(0));
  990. cpus_per_node = cpumask_weight(node0_cpumask);
  991. cpus_per_l3 = cpumask_weight(&ci->shared_cpu_map);
  992. if (!cpus_per_node || !cpus_per_l3)
  993. return 1;
  994. ret = cpus_per_l3 / cpus_per_node;
  995. /* sanity check: Only valid results are 1, 2, 3, 4 */
  996. switch (ret) {
  997. case 1:
  998. break;
  999. case 2 ... 4:
  1000. pr_info("Sub-NUMA Cluster mode detected with %d nodes per L3 cache\n", ret);
  1001. rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_L3_NODE;
  1002. break;
  1003. default:
  1004. pr_warn("Ignore improbable SNC node count %d\n", ret);
  1005. ret = 1;
  1006. break;
  1007. }
  1008. return ret;
  1009. }
  1010. int __init rdt_get_mon_l3_config(struct rdt_resource *r)
  1011. {
  1012. unsigned int mbm_offset = boot_cpu_data.x86_cache_mbm_width_offset;
  1013. struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
  1014. unsigned int threshold;
  1015. int ret;
  1016. snc_nodes_per_l3_cache = snc_get_config();
  1017. resctrl_rmid_realloc_limit = boot_cpu_data.x86_cache_size * 1024;
  1018. hw_res->mon_scale = boot_cpu_data.x86_cache_occ_scale / snc_nodes_per_l3_cache;
  1019. r->num_rmid = (boot_cpu_data.x86_cache_max_rmid + 1) / snc_nodes_per_l3_cache;
  1020. hw_res->mbm_width = MBM_CNTR_WIDTH_BASE;
  1021. if (mbm_offset > 0 && mbm_offset <= MBM_CNTR_WIDTH_OFFSET_MAX)
  1022. hw_res->mbm_width += mbm_offset;
  1023. else if (mbm_offset > MBM_CNTR_WIDTH_OFFSET_MAX)
  1024. pr_warn("Ignoring impossible MBM counter offset\n");
  1025. /*
  1026. * A reasonable upper limit on the max threshold is the number
  1027. * of lines tagged per RMID if all RMIDs have the same number of
  1028. * lines tagged in the LLC.
  1029. *
  1030. * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
  1031. */
  1032. threshold = resctrl_rmid_realloc_limit / r->num_rmid;
  1033. /*
  1034. * Because num_rmid may not be a power of two, round the value
  1035. * to the nearest multiple of hw_res->mon_scale so it matches a
  1036. * value the hardware will measure. mon_scale may not be a power of 2.
  1037. */
  1038. resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(threshold);
  1039. ret = dom_data_init(r);
  1040. if (ret)
  1041. return ret;
  1042. if (rdt_cpu_has(X86_FEATURE_BMEC)) {
  1043. u32 eax, ebx, ecx, edx;
  1044. /* Detect list of bandwidth sources that can be tracked */
  1045. cpuid_count(0x80000020, 3, &eax, &ebx, &ecx, &edx);
  1046. hw_res->mbm_cfg_mask = ecx & MAX_EVT_CONFIG_BITS;
  1047. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL)) {
  1048. mbm_total_event.configurable = true;
  1049. mbm_config_rftype_init("mbm_total_bytes_config");
  1050. }
  1051. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL)) {
  1052. mbm_local_event.configurable = true;
  1053. mbm_config_rftype_init("mbm_local_bytes_config");
  1054. }
  1055. }
  1056. l3_mon_evt_init(r);
  1057. r->mon_capable = true;
  1058. return 0;
  1059. }
  1060. void __exit rdt_put_mon_l3_config(void)
  1061. {
  1062. dom_data_exit();
  1063. }
  1064. void __init intel_rdt_mbm_apply_quirk(void)
  1065. {
  1066. int cf_index;
  1067. cf_index = (boot_cpu_data.x86_cache_max_rmid + 1) / 8 - 1;
  1068. if (cf_index >= ARRAY_SIZE(mbm_cf_table)) {
  1069. pr_info("No MBM correction factor available\n");
  1070. return;
  1071. }
  1072. mbm_cf_rmidthreshold = mbm_cf_table[cf_index].rmidthreshold;
  1073. mbm_cf = mbm_cf_table[cf_index].cf;
  1074. }