intel_rdt_monitor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Resource Director Technology(RDT)
  3. * - Monitoring code
  4. *
  5. * Copyright (C) 2017 Intel Corporation
  6. *
  7. * Author:
  8. * Vikas Shivappa <vikas.shivappa@intel.com>
  9. *
  10. * This replaces the cqm.c based on perf but we reuse a lot of
  11. * code and datastructures originally from Peter Zijlstra and Matt Fleming.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. *
  22. * More information about RDT be found in the Intel (R) x86 Architecture
  23. * Software Developer Manual June 2016, volume 3, section 17.17.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <asm/cpu_device_id.h>
  28. #include "intel_rdt.h"
  29. #define MSR_IA32_QM_CTR 0x0c8e
  30. #define MSR_IA32_QM_EVTSEL 0x0c8d
  31. struct rmid_entry {
  32. u32 rmid;
  33. int busy;
  34. struct list_head list;
  35. };
  36. /**
  37. * @rmid_free_lru A least recently used list of free RMIDs
  38. * These RMIDs are guaranteed to have an occupancy less than the
  39. * threshold occupancy
  40. */
  41. static LIST_HEAD(rmid_free_lru);
  42. /**
  43. * @rmid_limbo_count count of currently unused but (potentially)
  44. * dirty RMIDs.
  45. * This counts RMIDs that no one is currently using but that
  46. * may have a occupancy value > intel_cqm_threshold. User can change
  47. * the threshold occupancy value.
  48. */
  49. static unsigned int rmid_limbo_count;
  50. /**
  51. * @rmid_entry - The entry in the limbo and free lists.
  52. */
  53. static struct rmid_entry *rmid_ptrs;
  54. /*
  55. * Global boolean for rdt_monitor which is true if any
  56. * resource monitoring is enabled.
  57. */
  58. bool rdt_mon_capable;
  59. /*
  60. * Global to indicate which monitoring events are enabled.
  61. */
  62. unsigned int rdt_mon_features;
  63. /*
  64. * This is the threshold cache occupancy at which we will consider an
  65. * RMID available for re-allocation.
  66. */
  67. unsigned int intel_cqm_threshold;
  68. static inline struct rmid_entry *__rmid_entry(u32 rmid)
  69. {
  70. struct rmid_entry *entry;
  71. entry = &rmid_ptrs[rmid];
  72. WARN_ON(entry->rmid != rmid);
  73. return entry;
  74. }
  75. static u64 __rmid_read(u32 rmid, u32 eventid)
  76. {
  77. u64 val;
  78. /*
  79. * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
  80. * with a valid event code for supported resource type and the bits
  81. * IA32_QM_EVTSEL.RMID (bits 41:32) are configured with valid RMID,
  82. * IA32_QM_CTR.data (bits 61:0) reports the monitored data.
  83. * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
  84. * are error bits.
  85. */
  86. wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
  87. rdmsrl(MSR_IA32_QM_CTR, val);
  88. return val;
  89. }
  90. static bool rmid_dirty(struct rmid_entry *entry)
  91. {
  92. u64 val = __rmid_read(entry->rmid, QOS_L3_OCCUP_EVENT_ID);
  93. return val >= intel_cqm_threshold;
  94. }
  95. /*
  96. * Check the RMIDs that are marked as busy for this domain. If the
  97. * reported LLC occupancy is below the threshold clear the busy bit and
  98. * decrement the count. If the busy count gets to zero on an RMID, we
  99. * free the RMID
  100. */
  101. void __check_limbo(struct rdt_domain *d, bool force_free)
  102. {
  103. struct rmid_entry *entry;
  104. struct rdt_resource *r;
  105. u32 crmid = 1, nrmid;
  106. r = &rdt_resources_all[RDT_RESOURCE_L3];
  107. /*
  108. * Skip RMID 0 and start from RMID 1 and check all the RMIDs that
  109. * are marked as busy for occupancy < threshold. If the occupancy
  110. * is less than the threshold decrement the busy counter of the
  111. * RMID and move it to the free list when the counter reaches 0.
  112. */
  113. for (;;) {
  114. nrmid = find_next_bit(d->rmid_busy_llc, r->num_rmid, crmid);
  115. if (nrmid >= r->num_rmid)
  116. break;
  117. entry = __rmid_entry(nrmid);
  118. if (force_free || !rmid_dirty(entry)) {
  119. clear_bit(entry->rmid, d->rmid_busy_llc);
  120. if (!--entry->busy) {
  121. rmid_limbo_count--;
  122. list_add_tail(&entry->list, &rmid_free_lru);
  123. }
  124. }
  125. crmid = nrmid + 1;
  126. }
  127. }
  128. bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d)
  129. {
  130. return find_first_bit(d->rmid_busy_llc, r->num_rmid) != r->num_rmid;
  131. }
  132. /*
  133. * As of now the RMIDs allocation is global.
  134. * However we keep track of which packages the RMIDs
  135. * are used to optimize the limbo list management.
  136. */
  137. int alloc_rmid(void)
  138. {
  139. struct rmid_entry *entry;
  140. lockdep_assert_held(&rdtgroup_mutex);
  141. if (list_empty(&rmid_free_lru))
  142. return rmid_limbo_count ? -EBUSY : -ENOSPC;
  143. entry = list_first_entry(&rmid_free_lru,
  144. struct rmid_entry, list);
  145. list_del(&entry->list);
  146. return entry->rmid;
  147. }
  148. static void add_rmid_to_limbo(struct rmid_entry *entry)
  149. {
  150. struct rdt_resource *r;
  151. struct rdt_domain *d;
  152. int cpu;
  153. u64 val;
  154. r = &rdt_resources_all[RDT_RESOURCE_L3];
  155. entry->busy = 0;
  156. cpu = get_cpu();
  157. list_for_each_entry(d, &r->domains, list) {
  158. if (cpumask_test_cpu(cpu, &d->cpu_mask)) {
  159. val = __rmid_read(entry->rmid, QOS_L3_OCCUP_EVENT_ID);
  160. if (val <= intel_cqm_threshold)
  161. continue;
  162. }
  163. /*
  164. * For the first limbo RMID in the domain,
  165. * setup up the limbo worker.
  166. */
  167. if (!has_busy_rmid(r, d))
  168. cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL);
  169. set_bit(entry->rmid, d->rmid_busy_llc);
  170. entry->busy++;
  171. }
  172. put_cpu();
  173. if (entry->busy)
  174. rmid_limbo_count++;
  175. else
  176. list_add_tail(&entry->list, &rmid_free_lru);
  177. }
  178. void free_rmid(u32 rmid)
  179. {
  180. struct rmid_entry *entry;
  181. if (!rmid)
  182. return;
  183. lockdep_assert_held(&rdtgroup_mutex);
  184. entry = __rmid_entry(rmid);
  185. if (is_llc_occupancy_enabled())
  186. add_rmid_to_limbo(entry);
  187. else
  188. list_add_tail(&entry->list, &rmid_free_lru);
  189. }
  190. static int __mon_event_count(u32 rmid, struct rmid_read *rr)
  191. {
  192. u64 chunks, shift, tval;
  193. struct mbm_state *m;
  194. tval = __rmid_read(rmid, rr->evtid);
  195. if (tval & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL)) {
  196. rr->val = tval;
  197. return -EINVAL;
  198. }
  199. switch (rr->evtid) {
  200. case QOS_L3_OCCUP_EVENT_ID:
  201. rr->val += tval;
  202. return 0;
  203. case QOS_L3_MBM_TOTAL_EVENT_ID:
  204. m = &rr->d->mbm_total[rmid];
  205. break;
  206. case QOS_L3_MBM_LOCAL_EVENT_ID:
  207. m = &rr->d->mbm_local[rmid];
  208. break;
  209. default:
  210. /*
  211. * Code would never reach here because
  212. * an invalid event id would fail the __rmid_read.
  213. */
  214. return -EINVAL;
  215. }
  216. if (rr->first) {
  217. m->prev_msr = tval;
  218. m->chunks = 0;
  219. return 0;
  220. }
  221. shift = 64 - MBM_CNTR_WIDTH;
  222. chunks = (tval << shift) - (m->prev_msr << shift);
  223. chunks >>= shift;
  224. m->chunks += chunks;
  225. m->prev_msr = tval;
  226. rr->val += m->chunks;
  227. return 0;
  228. }
  229. /*
  230. * This is called via IPI to read the CQM/MBM counters
  231. * on a domain.
  232. */
  233. void mon_event_count(void *info)
  234. {
  235. struct rdtgroup *rdtgrp, *entry;
  236. struct rmid_read *rr = info;
  237. struct list_head *head;
  238. rdtgrp = rr->rgrp;
  239. if (__mon_event_count(rdtgrp->mon.rmid, rr))
  240. return;
  241. /*
  242. * For Ctrl groups read data from child monitor groups.
  243. */
  244. head = &rdtgrp->mon.crdtgrp_list;
  245. if (rdtgrp->type == RDTCTRL_GROUP) {
  246. list_for_each_entry(entry, head, mon.crdtgrp_list) {
  247. if (__mon_event_count(entry->mon.rmid, rr))
  248. return;
  249. }
  250. }
  251. }
  252. static void mbm_update(struct rdt_domain *d, int rmid)
  253. {
  254. struct rmid_read rr;
  255. rr.first = false;
  256. rr.d = d;
  257. /*
  258. * This is protected from concurrent reads from user
  259. * as both the user and we hold the global mutex.
  260. */
  261. if (is_mbm_total_enabled()) {
  262. rr.evtid = QOS_L3_MBM_TOTAL_EVENT_ID;
  263. __mon_event_count(rmid, &rr);
  264. }
  265. if (is_mbm_local_enabled()) {
  266. rr.evtid = QOS_L3_MBM_LOCAL_EVENT_ID;
  267. __mon_event_count(rmid, &rr);
  268. }
  269. }
  270. /*
  271. * Handler to scan the limbo list and move the RMIDs
  272. * to free list whose occupancy < threshold_occupancy.
  273. */
  274. void cqm_handle_limbo(struct work_struct *work)
  275. {
  276. unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
  277. int cpu = smp_processor_id();
  278. struct rdt_resource *r;
  279. struct rdt_domain *d;
  280. mutex_lock(&rdtgroup_mutex);
  281. r = &rdt_resources_all[RDT_RESOURCE_L3];
  282. d = get_domain_from_cpu(cpu, r);
  283. if (!d) {
  284. pr_warn_once("Failure to get domain for limbo worker\n");
  285. goto out_unlock;
  286. }
  287. __check_limbo(d, false);
  288. if (has_busy_rmid(r, d))
  289. schedule_delayed_work_on(cpu, &d->cqm_limbo, delay);
  290. out_unlock:
  291. mutex_unlock(&rdtgroup_mutex);
  292. }
  293. void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms)
  294. {
  295. unsigned long delay = msecs_to_jiffies(delay_ms);
  296. struct rdt_resource *r;
  297. int cpu;
  298. r = &rdt_resources_all[RDT_RESOURCE_L3];
  299. cpu = cpumask_any(&dom->cpu_mask);
  300. dom->cqm_work_cpu = cpu;
  301. schedule_delayed_work_on(cpu, &dom->cqm_limbo, delay);
  302. }
  303. void mbm_handle_overflow(struct work_struct *work)
  304. {
  305. unsigned long delay = msecs_to_jiffies(MBM_OVERFLOW_INTERVAL);
  306. struct rdtgroup *prgrp, *crgrp;
  307. int cpu = smp_processor_id();
  308. struct list_head *head;
  309. struct rdt_domain *d;
  310. mutex_lock(&rdtgroup_mutex);
  311. if (!static_branch_likely(&rdt_enable_key))
  312. goto out_unlock;
  313. d = get_domain_from_cpu(cpu, &rdt_resources_all[RDT_RESOURCE_L3]);
  314. if (!d)
  315. goto out_unlock;
  316. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  317. mbm_update(d, prgrp->mon.rmid);
  318. head = &prgrp->mon.crdtgrp_list;
  319. list_for_each_entry(crgrp, head, mon.crdtgrp_list)
  320. mbm_update(d, crgrp->mon.rmid);
  321. }
  322. schedule_delayed_work_on(cpu, &d->mbm_over, delay);
  323. out_unlock:
  324. mutex_unlock(&rdtgroup_mutex);
  325. }
  326. void mbm_setup_overflow_handler(struct rdt_domain *dom, unsigned long delay_ms)
  327. {
  328. unsigned long delay = msecs_to_jiffies(delay_ms);
  329. int cpu;
  330. if (!static_branch_likely(&rdt_enable_key))
  331. return;
  332. cpu = cpumask_any(&dom->cpu_mask);
  333. dom->mbm_work_cpu = cpu;
  334. schedule_delayed_work_on(cpu, &dom->mbm_over, delay);
  335. }
  336. static int dom_data_init(struct rdt_resource *r)
  337. {
  338. struct rmid_entry *entry = NULL;
  339. int i, nr_rmids;
  340. nr_rmids = r->num_rmid;
  341. rmid_ptrs = kcalloc(nr_rmids, sizeof(struct rmid_entry), GFP_KERNEL);
  342. if (!rmid_ptrs)
  343. return -ENOMEM;
  344. for (i = 0; i < nr_rmids; i++) {
  345. entry = &rmid_ptrs[i];
  346. INIT_LIST_HEAD(&entry->list);
  347. entry->rmid = i;
  348. list_add_tail(&entry->list, &rmid_free_lru);
  349. }
  350. /*
  351. * RMID 0 is special and is always allocated. It's used for all
  352. * tasks that are not monitored.
  353. */
  354. entry = __rmid_entry(0);
  355. list_del(&entry->list);
  356. return 0;
  357. }
  358. static struct mon_evt llc_occupancy_event = {
  359. .name = "llc_occupancy",
  360. .evtid = QOS_L3_OCCUP_EVENT_ID,
  361. };
  362. static struct mon_evt mbm_total_event = {
  363. .name = "mbm_total_bytes",
  364. .evtid = QOS_L3_MBM_TOTAL_EVENT_ID,
  365. };
  366. static struct mon_evt mbm_local_event = {
  367. .name = "mbm_local_bytes",
  368. .evtid = QOS_L3_MBM_LOCAL_EVENT_ID,
  369. };
  370. /*
  371. * Initialize the event list for the resource.
  372. *
  373. * Note that MBM events are also part of RDT_RESOURCE_L3 resource
  374. * because as per the SDM the total and local memory bandwidth
  375. * are enumerated as part of L3 monitoring.
  376. */
  377. static void l3_mon_evt_init(struct rdt_resource *r)
  378. {
  379. INIT_LIST_HEAD(&r->evt_list);
  380. if (is_llc_occupancy_enabled())
  381. list_add_tail(&llc_occupancy_event.list, &r->evt_list);
  382. if (is_mbm_total_enabled())
  383. list_add_tail(&mbm_total_event.list, &r->evt_list);
  384. if (is_mbm_local_enabled())
  385. list_add_tail(&mbm_local_event.list, &r->evt_list);
  386. }
  387. int rdt_get_mon_l3_config(struct rdt_resource *r)
  388. {
  389. int ret;
  390. r->mon_scale = boot_cpu_data.x86_cache_occ_scale;
  391. r->num_rmid = boot_cpu_data.x86_cache_max_rmid + 1;
  392. /*
  393. * A reasonable upper limit on the max threshold is the number
  394. * of lines tagged per RMID if all RMIDs have the same number of
  395. * lines tagged in the LLC.
  396. *
  397. * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
  398. */
  399. intel_cqm_threshold = boot_cpu_data.x86_cache_size * 1024 / r->num_rmid;
  400. /* h/w works in units of "boot_cpu_data.x86_cache_occ_scale" */
  401. intel_cqm_threshold /= r->mon_scale;
  402. ret = dom_data_init(r);
  403. if (ret)
  404. return ret;
  405. l3_mon_evt_init(r);
  406. r->mon_capable = true;
  407. r->mon_enabled = true;
  408. return 0;
  409. }