cqm.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. /*
  2. * Intel Cache Quality-of-Service Monitoring (CQM) support.
  3. *
  4. * Based very, very heavily on work by Peter Zijlstra.
  5. */
  6. #include <linux/perf_event.h>
  7. #include <linux/slab.h>
  8. #include <asm/cpu_device_id.h>
  9. #include "../perf_event.h"
  10. #define MSR_IA32_PQR_ASSOC 0x0c8f
  11. #define MSR_IA32_QM_CTR 0x0c8e
  12. #define MSR_IA32_QM_EVTSEL 0x0c8d
  13. #define MBM_CNTR_WIDTH 24
  14. /*
  15. * Guaranteed time in ms as per SDM where MBM counters will not overflow.
  16. */
  17. #define MBM_CTR_OVERFLOW_TIME 1000
  18. static u32 cqm_max_rmid = -1;
  19. static unsigned int cqm_l3_scale; /* supposedly cacheline size */
  20. static bool cqm_enabled, mbm_enabled;
  21. unsigned int mbm_socket_max;
  22. /**
  23. * struct intel_pqr_state - State cache for the PQR MSR
  24. * @rmid: The cached Resource Monitoring ID
  25. * @closid: The cached Class Of Service ID
  26. * @rmid_usecnt: The usage counter for rmid
  27. *
  28. * The upper 32 bits of MSR_IA32_PQR_ASSOC contain closid and the
  29. * lower 10 bits rmid. The update to MSR_IA32_PQR_ASSOC always
  30. * contains both parts, so we need to cache them.
  31. *
  32. * The cache also helps to avoid pointless updates if the value does
  33. * not change.
  34. */
  35. struct intel_pqr_state {
  36. u32 rmid;
  37. u32 closid;
  38. int rmid_usecnt;
  39. };
  40. /*
  41. * The cached intel_pqr_state is strictly per CPU and can never be
  42. * updated from a remote CPU. Both functions which modify the state
  43. * (intel_cqm_event_start and intel_cqm_event_stop) are called with
  44. * interrupts disabled, which is sufficient for the protection.
  45. */
  46. static DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
  47. static struct hrtimer *mbm_timers;
  48. /**
  49. * struct sample - mbm event's (local or total) data
  50. * @total_bytes #bytes since we began monitoring
  51. * @prev_msr previous value of MSR
  52. */
  53. struct sample {
  54. u64 total_bytes;
  55. u64 prev_msr;
  56. };
  57. /*
  58. * samples profiled for total memory bandwidth type events
  59. */
  60. static struct sample *mbm_total;
  61. /*
  62. * samples profiled for local memory bandwidth type events
  63. */
  64. static struct sample *mbm_local;
  65. #define pkg_id topology_physical_package_id(smp_processor_id())
  66. /*
  67. * rmid_2_index returns the index for the rmid in mbm_local/mbm_total array.
  68. * mbm_total[] and mbm_local[] are linearly indexed by socket# * max number of
  69. * rmids per socket, an example is given below
  70. * RMID1 of Socket0: vrmid = 1
  71. * RMID1 of Socket1: vrmid = 1 * (cqm_max_rmid + 1) + 1
  72. * RMID1 of Socket2: vrmid = 2 * (cqm_max_rmid + 1) + 1
  73. */
  74. #define rmid_2_index(rmid) ((pkg_id * (cqm_max_rmid + 1)) + rmid)
  75. /*
  76. * Protects cache_cgroups and cqm_rmid_free_lru and cqm_rmid_limbo_lru.
  77. * Also protects event->hw.cqm_rmid
  78. *
  79. * Hold either for stability, both for modification of ->hw.cqm_rmid.
  80. */
  81. static DEFINE_MUTEX(cache_mutex);
  82. static DEFINE_RAW_SPINLOCK(cache_lock);
  83. /*
  84. * Groups of events that have the same target(s), one RMID per group.
  85. */
  86. static LIST_HEAD(cache_groups);
  87. /*
  88. * Mask of CPUs for reading CQM values. We only need one per-socket.
  89. */
  90. static cpumask_t cqm_cpumask;
  91. #define RMID_VAL_ERROR (1ULL << 63)
  92. #define RMID_VAL_UNAVAIL (1ULL << 62)
  93. /*
  94. * Event IDs are used to program IA32_QM_EVTSEL before reading event
  95. * counter from IA32_QM_CTR
  96. */
  97. #define QOS_L3_OCCUP_EVENT_ID 0x01
  98. #define QOS_MBM_TOTAL_EVENT_ID 0x02
  99. #define QOS_MBM_LOCAL_EVENT_ID 0x03
  100. /*
  101. * This is central to the rotation algorithm in __intel_cqm_rmid_rotate().
  102. *
  103. * This rmid is always free and is guaranteed to have an associated
  104. * near-zero occupancy value, i.e. no cachelines are tagged with this
  105. * RMID, once __intel_cqm_rmid_rotate() returns.
  106. */
  107. static u32 intel_cqm_rotation_rmid;
  108. #define INVALID_RMID (-1)
  109. /*
  110. * Is @rmid valid for programming the hardware?
  111. *
  112. * rmid 0 is reserved by the hardware for all non-monitored tasks, which
  113. * means that we should never come across an rmid with that value.
  114. * Likewise, an rmid value of -1 is used to indicate "no rmid currently
  115. * assigned" and is used as part of the rotation code.
  116. */
  117. static inline bool __rmid_valid(u32 rmid)
  118. {
  119. if (!rmid || rmid == INVALID_RMID)
  120. return false;
  121. return true;
  122. }
  123. static u64 __rmid_read(u32 rmid)
  124. {
  125. u64 val;
  126. /*
  127. * Ignore the SDM, this thing is _NOTHING_ like a regular perfcnt,
  128. * it just says that to increase confusion.
  129. */
  130. wrmsr(MSR_IA32_QM_EVTSEL, QOS_L3_OCCUP_EVENT_ID, rmid);
  131. rdmsrl(MSR_IA32_QM_CTR, val);
  132. /*
  133. * Aside from the ERROR and UNAVAIL bits, assume this thing returns
  134. * the number of cachelines tagged with @rmid.
  135. */
  136. return val;
  137. }
  138. enum rmid_recycle_state {
  139. RMID_YOUNG = 0,
  140. RMID_AVAILABLE,
  141. RMID_DIRTY,
  142. };
  143. struct cqm_rmid_entry {
  144. u32 rmid;
  145. enum rmid_recycle_state state;
  146. struct list_head list;
  147. unsigned long queue_time;
  148. };
  149. /*
  150. * cqm_rmid_free_lru - A least recently used list of RMIDs.
  151. *
  152. * Oldest entry at the head, newest (most recently used) entry at the
  153. * tail. This list is never traversed, it's only used to keep track of
  154. * the lru order. That is, we only pick entries of the head or insert
  155. * them on the tail.
  156. *
  157. * All entries on the list are 'free', and their RMIDs are not currently
  158. * in use. To mark an RMID as in use, remove its entry from the lru
  159. * list.
  160. *
  161. *
  162. * cqm_rmid_limbo_lru - list of currently unused but (potentially) dirty RMIDs.
  163. *
  164. * This list is contains RMIDs that no one is currently using but that
  165. * may have a non-zero occupancy value associated with them. The
  166. * rotation worker moves RMIDs from the limbo list to the free list once
  167. * the occupancy value drops below __intel_cqm_threshold.
  168. *
  169. * Both lists are protected by cache_mutex.
  170. */
  171. static LIST_HEAD(cqm_rmid_free_lru);
  172. static LIST_HEAD(cqm_rmid_limbo_lru);
  173. /*
  174. * We use a simple array of pointers so that we can lookup a struct
  175. * cqm_rmid_entry in O(1). This alleviates the callers of __get_rmid()
  176. * and __put_rmid() from having to worry about dealing with struct
  177. * cqm_rmid_entry - they just deal with rmids, i.e. integers.
  178. *
  179. * Once this array is initialized it is read-only. No locks are required
  180. * to access it.
  181. *
  182. * All entries for all RMIDs can be looked up in the this array at all
  183. * times.
  184. */
  185. static struct cqm_rmid_entry **cqm_rmid_ptrs;
  186. static inline struct cqm_rmid_entry *__rmid_entry(u32 rmid)
  187. {
  188. struct cqm_rmid_entry *entry;
  189. entry = cqm_rmid_ptrs[rmid];
  190. WARN_ON(entry->rmid != rmid);
  191. return entry;
  192. }
  193. /*
  194. * Returns < 0 on fail.
  195. *
  196. * We expect to be called with cache_mutex held.
  197. */
  198. static u32 __get_rmid(void)
  199. {
  200. struct cqm_rmid_entry *entry;
  201. lockdep_assert_held(&cache_mutex);
  202. if (list_empty(&cqm_rmid_free_lru))
  203. return INVALID_RMID;
  204. entry = list_first_entry(&cqm_rmid_free_lru, struct cqm_rmid_entry, list);
  205. list_del(&entry->list);
  206. return entry->rmid;
  207. }
  208. static void __put_rmid(u32 rmid)
  209. {
  210. struct cqm_rmid_entry *entry;
  211. lockdep_assert_held(&cache_mutex);
  212. WARN_ON(!__rmid_valid(rmid));
  213. entry = __rmid_entry(rmid);
  214. entry->queue_time = jiffies;
  215. entry->state = RMID_YOUNG;
  216. list_add_tail(&entry->list, &cqm_rmid_limbo_lru);
  217. }
  218. static void cqm_cleanup(void)
  219. {
  220. int i;
  221. if (!cqm_rmid_ptrs)
  222. return;
  223. for (i = 0; i < cqm_max_rmid; i++)
  224. kfree(cqm_rmid_ptrs[i]);
  225. kfree(cqm_rmid_ptrs);
  226. cqm_rmid_ptrs = NULL;
  227. cqm_enabled = false;
  228. }
  229. static int intel_cqm_setup_rmid_cache(void)
  230. {
  231. struct cqm_rmid_entry *entry;
  232. unsigned int nr_rmids;
  233. int r = 0;
  234. nr_rmids = cqm_max_rmid + 1;
  235. cqm_rmid_ptrs = kzalloc(sizeof(struct cqm_rmid_entry *) *
  236. nr_rmids, GFP_KERNEL);
  237. if (!cqm_rmid_ptrs)
  238. return -ENOMEM;
  239. for (; r <= cqm_max_rmid; r++) {
  240. struct cqm_rmid_entry *entry;
  241. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  242. if (!entry)
  243. goto fail;
  244. INIT_LIST_HEAD(&entry->list);
  245. entry->rmid = r;
  246. cqm_rmid_ptrs[r] = entry;
  247. list_add_tail(&entry->list, &cqm_rmid_free_lru);
  248. }
  249. /*
  250. * RMID 0 is special and is always allocated. It's used for all
  251. * tasks that are not monitored.
  252. */
  253. entry = __rmid_entry(0);
  254. list_del(&entry->list);
  255. mutex_lock(&cache_mutex);
  256. intel_cqm_rotation_rmid = __get_rmid();
  257. mutex_unlock(&cache_mutex);
  258. return 0;
  259. fail:
  260. cqm_cleanup();
  261. return -ENOMEM;
  262. }
  263. /*
  264. * Determine if @a and @b measure the same set of tasks.
  265. *
  266. * If @a and @b measure the same set of tasks then we want to share a
  267. * single RMID.
  268. */
  269. static bool __match_event(struct perf_event *a, struct perf_event *b)
  270. {
  271. /* Per-cpu and task events don't mix */
  272. if ((a->attach_state & PERF_ATTACH_TASK) !=
  273. (b->attach_state & PERF_ATTACH_TASK))
  274. return false;
  275. #ifdef CONFIG_CGROUP_PERF
  276. if (a->cgrp != b->cgrp)
  277. return false;
  278. #endif
  279. /* If not task event, we're machine wide */
  280. if (!(b->attach_state & PERF_ATTACH_TASK))
  281. return true;
  282. /*
  283. * Events that target same task are placed into the same cache group.
  284. * Mark it as a multi event group, so that we update ->count
  285. * for every event rather than just the group leader later.
  286. */
  287. if (a->hw.target == b->hw.target) {
  288. b->hw.is_group_event = true;
  289. return true;
  290. }
  291. /*
  292. * Are we an inherited event?
  293. */
  294. if (b->parent == a)
  295. return true;
  296. return false;
  297. }
  298. #ifdef CONFIG_CGROUP_PERF
  299. static inline struct perf_cgroup *event_to_cgroup(struct perf_event *event)
  300. {
  301. if (event->attach_state & PERF_ATTACH_TASK)
  302. return perf_cgroup_from_task(event->hw.target, event->ctx);
  303. return event->cgrp;
  304. }
  305. #endif
  306. /*
  307. * Determine if @a's tasks intersect with @b's tasks
  308. *
  309. * There are combinations of events that we explicitly prohibit,
  310. *
  311. * PROHIBITS
  312. * system-wide -> cgroup and task
  313. * cgroup -> system-wide
  314. * -> task in cgroup
  315. * task -> system-wide
  316. * -> task in cgroup
  317. *
  318. * Call this function before allocating an RMID.
  319. */
  320. static bool __conflict_event(struct perf_event *a, struct perf_event *b)
  321. {
  322. #ifdef CONFIG_CGROUP_PERF
  323. /*
  324. * We can have any number of cgroups but only one system-wide
  325. * event at a time.
  326. */
  327. if (a->cgrp && b->cgrp) {
  328. struct perf_cgroup *ac = a->cgrp;
  329. struct perf_cgroup *bc = b->cgrp;
  330. /*
  331. * This condition should have been caught in
  332. * __match_event() and we should be sharing an RMID.
  333. */
  334. WARN_ON_ONCE(ac == bc);
  335. if (cgroup_is_descendant(ac->css.cgroup, bc->css.cgroup) ||
  336. cgroup_is_descendant(bc->css.cgroup, ac->css.cgroup))
  337. return true;
  338. return false;
  339. }
  340. if (a->cgrp || b->cgrp) {
  341. struct perf_cgroup *ac, *bc;
  342. /*
  343. * cgroup and system-wide events are mutually exclusive
  344. */
  345. if ((a->cgrp && !(b->attach_state & PERF_ATTACH_TASK)) ||
  346. (b->cgrp && !(a->attach_state & PERF_ATTACH_TASK)))
  347. return true;
  348. /*
  349. * Ensure neither event is part of the other's cgroup
  350. */
  351. ac = event_to_cgroup(a);
  352. bc = event_to_cgroup(b);
  353. if (ac == bc)
  354. return true;
  355. /*
  356. * Must have cgroup and non-intersecting task events.
  357. */
  358. if (!ac || !bc)
  359. return false;
  360. /*
  361. * We have cgroup and task events, and the task belongs
  362. * to a cgroup. Check for for overlap.
  363. */
  364. if (cgroup_is_descendant(ac->css.cgroup, bc->css.cgroup) ||
  365. cgroup_is_descendant(bc->css.cgroup, ac->css.cgroup))
  366. return true;
  367. return false;
  368. }
  369. #endif
  370. /*
  371. * If one of them is not a task, same story as above with cgroups.
  372. */
  373. if (!(a->attach_state & PERF_ATTACH_TASK) ||
  374. !(b->attach_state & PERF_ATTACH_TASK))
  375. return true;
  376. /*
  377. * Must be non-overlapping.
  378. */
  379. return false;
  380. }
  381. struct rmid_read {
  382. u32 rmid;
  383. u32 evt_type;
  384. atomic64_t value;
  385. };
  386. static void __intel_cqm_event_count(void *info);
  387. static void init_mbm_sample(u32 rmid, u32 evt_type);
  388. static void __intel_mbm_event_count(void *info);
  389. static bool is_cqm_event(int e)
  390. {
  391. return (e == QOS_L3_OCCUP_EVENT_ID);
  392. }
  393. static bool is_mbm_event(int e)
  394. {
  395. return (e >= QOS_MBM_TOTAL_EVENT_ID && e <= QOS_MBM_LOCAL_EVENT_ID);
  396. }
  397. static void cqm_mask_call(struct rmid_read *rr)
  398. {
  399. if (is_mbm_event(rr->evt_type))
  400. on_each_cpu_mask(&cqm_cpumask, __intel_mbm_event_count, rr, 1);
  401. else
  402. on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, rr, 1);
  403. }
  404. /*
  405. * Exchange the RMID of a group of events.
  406. */
  407. static u32 intel_cqm_xchg_rmid(struct perf_event *group, u32 rmid)
  408. {
  409. struct perf_event *event;
  410. struct list_head *head = &group->hw.cqm_group_entry;
  411. u32 old_rmid = group->hw.cqm_rmid;
  412. lockdep_assert_held(&cache_mutex);
  413. /*
  414. * If our RMID is being deallocated, perform a read now.
  415. */
  416. if (__rmid_valid(old_rmid) && !__rmid_valid(rmid)) {
  417. struct rmid_read rr = {
  418. .rmid = old_rmid,
  419. .evt_type = group->attr.config,
  420. .value = ATOMIC64_INIT(0),
  421. };
  422. cqm_mask_call(&rr);
  423. local64_set(&group->count, atomic64_read(&rr.value));
  424. }
  425. raw_spin_lock_irq(&cache_lock);
  426. group->hw.cqm_rmid = rmid;
  427. list_for_each_entry(event, head, hw.cqm_group_entry)
  428. event->hw.cqm_rmid = rmid;
  429. raw_spin_unlock_irq(&cache_lock);
  430. /*
  431. * If the allocation is for mbm, init the mbm stats.
  432. * Need to check if each event in the group is mbm event
  433. * because there could be multiple type of events in the same group.
  434. */
  435. if (__rmid_valid(rmid)) {
  436. event = group;
  437. if (is_mbm_event(event->attr.config))
  438. init_mbm_sample(rmid, event->attr.config);
  439. list_for_each_entry(event, head, hw.cqm_group_entry) {
  440. if (is_mbm_event(event->attr.config))
  441. init_mbm_sample(rmid, event->attr.config);
  442. }
  443. }
  444. return old_rmid;
  445. }
  446. /*
  447. * If we fail to assign a new RMID for intel_cqm_rotation_rmid because
  448. * cachelines are still tagged with RMIDs in limbo, we progressively
  449. * increment the threshold until we find an RMID in limbo with <=
  450. * __intel_cqm_threshold lines tagged. This is designed to mitigate the
  451. * problem where cachelines tagged with an RMID are not steadily being
  452. * evicted.
  453. *
  454. * On successful rotations we decrease the threshold back towards zero.
  455. *
  456. * __intel_cqm_max_threshold provides an upper bound on the threshold,
  457. * and is measured in bytes because it's exposed to userland.
  458. */
  459. static unsigned int __intel_cqm_threshold;
  460. static unsigned int __intel_cqm_max_threshold;
  461. /*
  462. * Test whether an RMID has a zero occupancy value on this cpu.
  463. */
  464. static void intel_cqm_stable(void *arg)
  465. {
  466. struct cqm_rmid_entry *entry;
  467. list_for_each_entry(entry, &cqm_rmid_limbo_lru, list) {
  468. if (entry->state != RMID_AVAILABLE)
  469. break;
  470. if (__rmid_read(entry->rmid) > __intel_cqm_threshold)
  471. entry->state = RMID_DIRTY;
  472. }
  473. }
  474. /*
  475. * If we have group events waiting for an RMID that don't conflict with
  476. * events already running, assign @rmid.
  477. */
  478. static bool intel_cqm_sched_in_event(u32 rmid)
  479. {
  480. struct perf_event *leader, *event;
  481. lockdep_assert_held(&cache_mutex);
  482. leader = list_first_entry(&cache_groups, struct perf_event,
  483. hw.cqm_groups_entry);
  484. event = leader;
  485. list_for_each_entry_continue(event, &cache_groups,
  486. hw.cqm_groups_entry) {
  487. if (__rmid_valid(event->hw.cqm_rmid))
  488. continue;
  489. if (__conflict_event(event, leader))
  490. continue;
  491. intel_cqm_xchg_rmid(event, rmid);
  492. return true;
  493. }
  494. return false;
  495. }
  496. /*
  497. * Initially use this constant for both the limbo queue time and the
  498. * rotation timer interval, pmu::hrtimer_interval_ms.
  499. *
  500. * They don't need to be the same, but the two are related since if you
  501. * rotate faster than you recycle RMIDs, you may run out of available
  502. * RMIDs.
  503. */
  504. #define RMID_DEFAULT_QUEUE_TIME 250 /* ms */
  505. static unsigned int __rmid_queue_time_ms = RMID_DEFAULT_QUEUE_TIME;
  506. /*
  507. * intel_cqm_rmid_stabilize - move RMIDs from limbo to free list
  508. * @nr_available: number of freeable RMIDs on the limbo list
  509. *
  510. * Quiescent state; wait for all 'freed' RMIDs to become unused, i.e. no
  511. * cachelines are tagged with those RMIDs. After this we can reuse them
  512. * and know that the current set of active RMIDs is stable.
  513. *
  514. * Return %true or %false depending on whether stabilization needs to be
  515. * reattempted.
  516. *
  517. * If we return %true then @nr_available is updated to indicate the
  518. * number of RMIDs on the limbo list that have been queued for the
  519. * minimum queue time (RMID_AVAILABLE), but whose data occupancy values
  520. * are above __intel_cqm_threshold.
  521. */
  522. static bool intel_cqm_rmid_stabilize(unsigned int *available)
  523. {
  524. struct cqm_rmid_entry *entry, *tmp;
  525. lockdep_assert_held(&cache_mutex);
  526. *available = 0;
  527. list_for_each_entry(entry, &cqm_rmid_limbo_lru, list) {
  528. unsigned long min_queue_time;
  529. unsigned long now = jiffies;
  530. /*
  531. * We hold RMIDs placed into limbo for a minimum queue
  532. * time. Before the minimum queue time has elapsed we do
  533. * not recycle RMIDs.
  534. *
  535. * The reasoning is that until a sufficient time has
  536. * passed since we stopped using an RMID, any RMID
  537. * placed onto the limbo list will likely still have
  538. * data tagged in the cache, which means we'll probably
  539. * fail to recycle it anyway.
  540. *
  541. * We can save ourselves an expensive IPI by skipping
  542. * any RMIDs that have not been queued for the minimum
  543. * time.
  544. */
  545. min_queue_time = entry->queue_time +
  546. msecs_to_jiffies(__rmid_queue_time_ms);
  547. if (time_after(min_queue_time, now))
  548. break;
  549. entry->state = RMID_AVAILABLE;
  550. (*available)++;
  551. }
  552. /*
  553. * Fast return if none of the RMIDs on the limbo list have been
  554. * sitting on the queue for the minimum queue time.
  555. */
  556. if (!*available)
  557. return false;
  558. /*
  559. * Test whether an RMID is free for each package.
  560. */
  561. on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL, true);
  562. list_for_each_entry_safe(entry, tmp, &cqm_rmid_limbo_lru, list) {
  563. /*
  564. * Exhausted all RMIDs that have waited min queue time.
  565. */
  566. if (entry->state == RMID_YOUNG)
  567. break;
  568. if (entry->state == RMID_DIRTY)
  569. continue;
  570. list_del(&entry->list); /* remove from limbo */
  571. /*
  572. * The rotation RMID gets priority if it's
  573. * currently invalid. In which case, skip adding
  574. * the RMID to the the free lru.
  575. */
  576. if (!__rmid_valid(intel_cqm_rotation_rmid)) {
  577. intel_cqm_rotation_rmid = entry->rmid;
  578. continue;
  579. }
  580. /*
  581. * If we have groups waiting for RMIDs, hand
  582. * them one now provided they don't conflict.
  583. */
  584. if (intel_cqm_sched_in_event(entry->rmid))
  585. continue;
  586. /*
  587. * Otherwise place it onto the free list.
  588. */
  589. list_add_tail(&entry->list, &cqm_rmid_free_lru);
  590. }
  591. return __rmid_valid(intel_cqm_rotation_rmid);
  592. }
  593. /*
  594. * Pick a victim group and move it to the tail of the group list.
  595. * @next: The first group without an RMID
  596. */
  597. static void __intel_cqm_pick_and_rotate(struct perf_event *next)
  598. {
  599. struct perf_event *rotor;
  600. u32 rmid;
  601. lockdep_assert_held(&cache_mutex);
  602. rotor = list_first_entry(&cache_groups, struct perf_event,
  603. hw.cqm_groups_entry);
  604. /*
  605. * The group at the front of the list should always have a valid
  606. * RMID. If it doesn't then no groups have RMIDs assigned and we
  607. * don't need to rotate the list.
  608. */
  609. if (next == rotor)
  610. return;
  611. rmid = intel_cqm_xchg_rmid(rotor, INVALID_RMID);
  612. __put_rmid(rmid);
  613. list_rotate_left(&cache_groups);
  614. }
  615. /*
  616. * Deallocate the RMIDs from any events that conflict with @event, and
  617. * place them on the back of the group list.
  618. */
  619. static void intel_cqm_sched_out_conflicting_events(struct perf_event *event)
  620. {
  621. struct perf_event *group, *g;
  622. u32 rmid;
  623. lockdep_assert_held(&cache_mutex);
  624. list_for_each_entry_safe(group, g, &cache_groups, hw.cqm_groups_entry) {
  625. if (group == event)
  626. continue;
  627. rmid = group->hw.cqm_rmid;
  628. /*
  629. * Skip events that don't have a valid RMID.
  630. */
  631. if (!__rmid_valid(rmid))
  632. continue;
  633. /*
  634. * No conflict? No problem! Leave the event alone.
  635. */
  636. if (!__conflict_event(group, event))
  637. continue;
  638. intel_cqm_xchg_rmid(group, INVALID_RMID);
  639. __put_rmid(rmid);
  640. }
  641. }
  642. /*
  643. * Attempt to rotate the groups and assign new RMIDs.
  644. *
  645. * We rotate for two reasons,
  646. * 1. To handle the scheduling of conflicting events
  647. * 2. To recycle RMIDs
  648. *
  649. * Rotating RMIDs is complicated because the hardware doesn't give us
  650. * any clues.
  651. *
  652. * There's problems with the hardware interface; when you change the
  653. * task:RMID map cachelines retain their 'old' tags, giving a skewed
  654. * picture. In order to work around this, we must always keep one free
  655. * RMID - intel_cqm_rotation_rmid.
  656. *
  657. * Rotation works by taking away an RMID from a group (the old RMID),
  658. * and assigning the free RMID to another group (the new RMID). We must
  659. * then wait for the old RMID to not be used (no cachelines tagged).
  660. * This ensure that all cachelines are tagged with 'active' RMIDs. At
  661. * this point we can start reading values for the new RMID and treat the
  662. * old RMID as the free RMID for the next rotation.
  663. *
  664. * Return %true or %false depending on whether we did any rotating.
  665. */
  666. static bool __intel_cqm_rmid_rotate(void)
  667. {
  668. struct perf_event *group, *start = NULL;
  669. unsigned int threshold_limit;
  670. unsigned int nr_needed = 0;
  671. unsigned int nr_available;
  672. bool rotated = false;
  673. mutex_lock(&cache_mutex);
  674. again:
  675. /*
  676. * Fast path through this function if there are no groups and no
  677. * RMIDs that need cleaning.
  678. */
  679. if (list_empty(&cache_groups) && list_empty(&cqm_rmid_limbo_lru))
  680. goto out;
  681. list_for_each_entry(group, &cache_groups, hw.cqm_groups_entry) {
  682. if (!__rmid_valid(group->hw.cqm_rmid)) {
  683. if (!start)
  684. start = group;
  685. nr_needed++;
  686. }
  687. }
  688. /*
  689. * We have some event groups, but they all have RMIDs assigned
  690. * and no RMIDs need cleaning.
  691. */
  692. if (!nr_needed && list_empty(&cqm_rmid_limbo_lru))
  693. goto out;
  694. if (!nr_needed)
  695. goto stabilize;
  696. /*
  697. * We have more event groups without RMIDs than available RMIDs,
  698. * or we have event groups that conflict with the ones currently
  699. * scheduled.
  700. *
  701. * We force deallocate the rmid of the group at the head of
  702. * cache_groups. The first event group without an RMID then gets
  703. * assigned intel_cqm_rotation_rmid. This ensures we always make
  704. * forward progress.
  705. *
  706. * Rotate the cache_groups list so the previous head is now the
  707. * tail.
  708. */
  709. __intel_cqm_pick_and_rotate(start);
  710. /*
  711. * If the rotation is going to succeed, reduce the threshold so
  712. * that we don't needlessly reuse dirty RMIDs.
  713. */
  714. if (__rmid_valid(intel_cqm_rotation_rmid)) {
  715. intel_cqm_xchg_rmid(start, intel_cqm_rotation_rmid);
  716. intel_cqm_rotation_rmid = __get_rmid();
  717. intel_cqm_sched_out_conflicting_events(start);
  718. if (__intel_cqm_threshold)
  719. __intel_cqm_threshold--;
  720. }
  721. rotated = true;
  722. stabilize:
  723. /*
  724. * We now need to stablize the RMID we freed above (if any) to
  725. * ensure that the next time we rotate we have an RMID with zero
  726. * occupancy value.
  727. *
  728. * Alternatively, if we didn't need to perform any rotation,
  729. * we'll have a bunch of RMIDs in limbo that need stabilizing.
  730. */
  731. threshold_limit = __intel_cqm_max_threshold / cqm_l3_scale;
  732. while (intel_cqm_rmid_stabilize(&nr_available) &&
  733. __intel_cqm_threshold < threshold_limit) {
  734. unsigned int steal_limit;
  735. /*
  736. * Don't spin if nobody is actively waiting for an RMID,
  737. * the rotation worker will be kicked as soon as an
  738. * event needs an RMID anyway.
  739. */
  740. if (!nr_needed)
  741. break;
  742. /* Allow max 25% of RMIDs to be in limbo. */
  743. steal_limit = (cqm_max_rmid + 1) / 4;
  744. /*
  745. * We failed to stabilize any RMIDs so our rotation
  746. * logic is now stuck. In order to make forward progress
  747. * we have a few options:
  748. *
  749. * 1. rotate ("steal") another RMID
  750. * 2. increase the threshold
  751. * 3. do nothing
  752. *
  753. * We do both of 1. and 2. until we hit the steal limit.
  754. *
  755. * The steal limit prevents all RMIDs ending up on the
  756. * limbo list. This can happen if every RMID has a
  757. * non-zero occupancy above threshold_limit, and the
  758. * occupancy values aren't dropping fast enough.
  759. *
  760. * Note that there is prioritisation at work here - we'd
  761. * rather increase the number of RMIDs on the limbo list
  762. * than increase the threshold, because increasing the
  763. * threshold skews the event data (because we reuse
  764. * dirty RMIDs) - threshold bumps are a last resort.
  765. */
  766. if (nr_available < steal_limit)
  767. goto again;
  768. __intel_cqm_threshold++;
  769. }
  770. out:
  771. mutex_unlock(&cache_mutex);
  772. return rotated;
  773. }
  774. static void intel_cqm_rmid_rotate(struct work_struct *work);
  775. static DECLARE_DELAYED_WORK(intel_cqm_rmid_work, intel_cqm_rmid_rotate);
  776. static struct pmu intel_cqm_pmu;
  777. static void intel_cqm_rmid_rotate(struct work_struct *work)
  778. {
  779. unsigned long delay;
  780. __intel_cqm_rmid_rotate();
  781. delay = msecs_to_jiffies(intel_cqm_pmu.hrtimer_interval_ms);
  782. schedule_delayed_work(&intel_cqm_rmid_work, delay);
  783. }
  784. static u64 update_sample(unsigned int rmid, u32 evt_type, int first)
  785. {
  786. struct sample *mbm_current;
  787. u32 vrmid = rmid_2_index(rmid);
  788. u64 val, bytes, shift;
  789. u32 eventid;
  790. if (evt_type == QOS_MBM_LOCAL_EVENT_ID) {
  791. mbm_current = &mbm_local[vrmid];
  792. eventid = QOS_MBM_LOCAL_EVENT_ID;
  793. } else {
  794. mbm_current = &mbm_total[vrmid];
  795. eventid = QOS_MBM_TOTAL_EVENT_ID;
  796. }
  797. wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
  798. rdmsrl(MSR_IA32_QM_CTR, val);
  799. if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
  800. return mbm_current->total_bytes;
  801. if (first) {
  802. mbm_current->prev_msr = val;
  803. mbm_current->total_bytes = 0;
  804. return mbm_current->total_bytes;
  805. }
  806. /*
  807. * The h/w guarantees that counters will not overflow
  808. * so long as we poll them at least once per second.
  809. */
  810. shift = 64 - MBM_CNTR_WIDTH;
  811. bytes = (val << shift) - (mbm_current->prev_msr << shift);
  812. bytes >>= shift;
  813. bytes *= cqm_l3_scale;
  814. mbm_current->total_bytes += bytes;
  815. mbm_current->prev_msr = val;
  816. return mbm_current->total_bytes;
  817. }
  818. static u64 rmid_read_mbm(unsigned int rmid, u32 evt_type)
  819. {
  820. return update_sample(rmid, evt_type, 0);
  821. }
  822. static void __intel_mbm_event_init(void *info)
  823. {
  824. struct rmid_read *rr = info;
  825. update_sample(rr->rmid, rr->evt_type, 1);
  826. }
  827. static void init_mbm_sample(u32 rmid, u32 evt_type)
  828. {
  829. struct rmid_read rr = {
  830. .rmid = rmid,
  831. .evt_type = evt_type,
  832. .value = ATOMIC64_INIT(0),
  833. };
  834. /* on each socket, init sample */
  835. on_each_cpu_mask(&cqm_cpumask, __intel_mbm_event_init, &rr, 1);
  836. }
  837. /*
  838. * Find a group and setup RMID.
  839. *
  840. * If we're part of a group, we use the group's RMID.
  841. */
  842. static void intel_cqm_setup_event(struct perf_event *event,
  843. struct perf_event **group)
  844. {
  845. struct perf_event *iter;
  846. bool conflict = false;
  847. u32 rmid;
  848. event->hw.is_group_event = false;
  849. list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
  850. rmid = iter->hw.cqm_rmid;
  851. if (__match_event(iter, event)) {
  852. /* All tasks in a group share an RMID */
  853. event->hw.cqm_rmid = rmid;
  854. *group = iter;
  855. if (is_mbm_event(event->attr.config) && __rmid_valid(rmid))
  856. init_mbm_sample(rmid, event->attr.config);
  857. return;
  858. }
  859. /*
  860. * We only care about conflicts for events that are
  861. * actually scheduled in (and hence have a valid RMID).
  862. */
  863. if (__conflict_event(iter, event) && __rmid_valid(rmid))
  864. conflict = true;
  865. }
  866. if (conflict)
  867. rmid = INVALID_RMID;
  868. else
  869. rmid = __get_rmid();
  870. if (is_mbm_event(event->attr.config) && __rmid_valid(rmid))
  871. init_mbm_sample(rmid, event->attr.config);
  872. event->hw.cqm_rmid = rmid;
  873. }
  874. static void intel_cqm_event_read(struct perf_event *event)
  875. {
  876. unsigned long flags;
  877. u32 rmid;
  878. u64 val;
  879. /*
  880. * Task events are handled by intel_cqm_event_count().
  881. */
  882. if (event->cpu == -1)
  883. return;
  884. raw_spin_lock_irqsave(&cache_lock, flags);
  885. rmid = event->hw.cqm_rmid;
  886. if (!__rmid_valid(rmid))
  887. goto out;
  888. if (is_mbm_event(event->attr.config))
  889. val = rmid_read_mbm(rmid, event->attr.config);
  890. else
  891. val = __rmid_read(rmid);
  892. /*
  893. * Ignore this reading on error states and do not update the value.
  894. */
  895. if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
  896. goto out;
  897. local64_set(&event->count, val);
  898. out:
  899. raw_spin_unlock_irqrestore(&cache_lock, flags);
  900. }
  901. static void __intel_cqm_event_count(void *info)
  902. {
  903. struct rmid_read *rr = info;
  904. u64 val;
  905. val = __rmid_read(rr->rmid);
  906. if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
  907. return;
  908. atomic64_add(val, &rr->value);
  909. }
  910. static inline bool cqm_group_leader(struct perf_event *event)
  911. {
  912. return !list_empty(&event->hw.cqm_groups_entry);
  913. }
  914. static void __intel_mbm_event_count(void *info)
  915. {
  916. struct rmid_read *rr = info;
  917. u64 val;
  918. val = rmid_read_mbm(rr->rmid, rr->evt_type);
  919. if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
  920. return;
  921. atomic64_add(val, &rr->value);
  922. }
  923. static enum hrtimer_restart mbm_hrtimer_handle(struct hrtimer *hrtimer)
  924. {
  925. struct perf_event *iter, *iter1;
  926. int ret = HRTIMER_RESTART;
  927. struct list_head *head;
  928. unsigned long flags;
  929. u32 grp_rmid;
  930. /*
  931. * Need to cache_lock as the timer Event Select MSR reads
  932. * can race with the mbm/cqm count() and mbm_init() reads.
  933. */
  934. raw_spin_lock_irqsave(&cache_lock, flags);
  935. if (list_empty(&cache_groups)) {
  936. ret = HRTIMER_NORESTART;
  937. goto out;
  938. }
  939. list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
  940. grp_rmid = iter->hw.cqm_rmid;
  941. if (!__rmid_valid(grp_rmid))
  942. continue;
  943. if (is_mbm_event(iter->attr.config))
  944. update_sample(grp_rmid, iter->attr.config, 0);
  945. head = &iter->hw.cqm_group_entry;
  946. if (list_empty(head))
  947. continue;
  948. list_for_each_entry(iter1, head, hw.cqm_group_entry) {
  949. if (!iter1->hw.is_group_event)
  950. break;
  951. if (is_mbm_event(iter1->attr.config))
  952. update_sample(iter1->hw.cqm_rmid,
  953. iter1->attr.config, 0);
  954. }
  955. }
  956. hrtimer_forward_now(hrtimer, ms_to_ktime(MBM_CTR_OVERFLOW_TIME));
  957. out:
  958. raw_spin_unlock_irqrestore(&cache_lock, flags);
  959. return ret;
  960. }
  961. static void __mbm_start_timer(void *info)
  962. {
  963. hrtimer_start(&mbm_timers[pkg_id], ms_to_ktime(MBM_CTR_OVERFLOW_TIME),
  964. HRTIMER_MODE_REL_PINNED);
  965. }
  966. static void __mbm_stop_timer(void *info)
  967. {
  968. hrtimer_cancel(&mbm_timers[pkg_id]);
  969. }
  970. static void mbm_start_timers(void)
  971. {
  972. on_each_cpu_mask(&cqm_cpumask, __mbm_start_timer, NULL, 1);
  973. }
  974. static void mbm_stop_timers(void)
  975. {
  976. on_each_cpu_mask(&cqm_cpumask, __mbm_stop_timer, NULL, 1);
  977. }
  978. static void mbm_hrtimer_init(void)
  979. {
  980. struct hrtimer *hr;
  981. int i;
  982. for (i = 0; i < mbm_socket_max; i++) {
  983. hr = &mbm_timers[i];
  984. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  985. hr->function = mbm_hrtimer_handle;
  986. }
  987. }
  988. static u64 intel_cqm_event_count(struct perf_event *event)
  989. {
  990. unsigned long flags;
  991. struct rmid_read rr = {
  992. .evt_type = event->attr.config,
  993. .value = ATOMIC64_INIT(0),
  994. };
  995. /*
  996. * We only need to worry about task events. System-wide events
  997. * are handled like usual, i.e. entirely with
  998. * intel_cqm_event_read().
  999. */
  1000. if (event->cpu != -1)
  1001. return __perf_event_count(event);
  1002. /*
  1003. * Only the group leader gets to report values except in case of
  1004. * multiple events in the same group, we still need to read the
  1005. * other events.This stops us
  1006. * reporting duplicate values to userspace, and gives us a clear
  1007. * rule for which task gets to report the values.
  1008. *
  1009. * Note that it is impossible to attribute these values to
  1010. * specific packages - we forfeit that ability when we create
  1011. * task events.
  1012. */
  1013. if (!cqm_group_leader(event) && !event->hw.is_group_event)
  1014. return 0;
  1015. /*
  1016. * Getting up-to-date values requires an SMP IPI which is not
  1017. * possible if we're being called in interrupt context. Return
  1018. * the cached values instead.
  1019. */
  1020. if (unlikely(in_interrupt()))
  1021. goto out;
  1022. /*
  1023. * Notice that we don't perform the reading of an RMID
  1024. * atomically, because we can't hold a spin lock across the
  1025. * IPIs.
  1026. *
  1027. * Speculatively perform the read, since @event might be
  1028. * assigned a different (possibly invalid) RMID while we're
  1029. * busying performing the IPI calls. It's therefore necessary to
  1030. * check @event's RMID afterwards, and if it has changed,
  1031. * discard the result of the read.
  1032. */
  1033. rr.rmid = ACCESS_ONCE(event->hw.cqm_rmid);
  1034. if (!__rmid_valid(rr.rmid))
  1035. goto out;
  1036. cqm_mask_call(&rr);
  1037. raw_spin_lock_irqsave(&cache_lock, flags);
  1038. if (event->hw.cqm_rmid == rr.rmid)
  1039. local64_set(&event->count, atomic64_read(&rr.value));
  1040. raw_spin_unlock_irqrestore(&cache_lock, flags);
  1041. out:
  1042. return __perf_event_count(event);
  1043. }
  1044. static void intel_cqm_event_start(struct perf_event *event, int mode)
  1045. {
  1046. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  1047. u32 rmid = event->hw.cqm_rmid;
  1048. if (!(event->hw.cqm_state & PERF_HES_STOPPED))
  1049. return;
  1050. event->hw.cqm_state &= ~PERF_HES_STOPPED;
  1051. if (state->rmid_usecnt++) {
  1052. if (!WARN_ON_ONCE(state->rmid != rmid))
  1053. return;
  1054. } else {
  1055. WARN_ON_ONCE(state->rmid);
  1056. }
  1057. state->rmid = rmid;
  1058. wrmsr(MSR_IA32_PQR_ASSOC, rmid, state->closid);
  1059. }
  1060. static void intel_cqm_event_stop(struct perf_event *event, int mode)
  1061. {
  1062. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  1063. if (event->hw.cqm_state & PERF_HES_STOPPED)
  1064. return;
  1065. event->hw.cqm_state |= PERF_HES_STOPPED;
  1066. intel_cqm_event_read(event);
  1067. if (!--state->rmid_usecnt) {
  1068. state->rmid = 0;
  1069. wrmsr(MSR_IA32_PQR_ASSOC, 0, state->closid);
  1070. } else {
  1071. WARN_ON_ONCE(!state->rmid);
  1072. }
  1073. }
  1074. static int intel_cqm_event_add(struct perf_event *event, int mode)
  1075. {
  1076. unsigned long flags;
  1077. u32 rmid;
  1078. raw_spin_lock_irqsave(&cache_lock, flags);
  1079. event->hw.cqm_state = PERF_HES_STOPPED;
  1080. rmid = event->hw.cqm_rmid;
  1081. if (__rmid_valid(rmid) && (mode & PERF_EF_START))
  1082. intel_cqm_event_start(event, mode);
  1083. raw_spin_unlock_irqrestore(&cache_lock, flags);
  1084. return 0;
  1085. }
  1086. static void intel_cqm_event_destroy(struct perf_event *event)
  1087. {
  1088. struct perf_event *group_other = NULL;
  1089. unsigned long flags;
  1090. mutex_lock(&cache_mutex);
  1091. /*
  1092. * Hold the cache_lock as mbm timer handlers could be
  1093. * scanning the list of events.
  1094. */
  1095. raw_spin_lock_irqsave(&cache_lock, flags);
  1096. /*
  1097. * If there's another event in this group...
  1098. */
  1099. if (!list_empty(&event->hw.cqm_group_entry)) {
  1100. group_other = list_first_entry(&event->hw.cqm_group_entry,
  1101. struct perf_event,
  1102. hw.cqm_group_entry);
  1103. list_del(&event->hw.cqm_group_entry);
  1104. }
  1105. /*
  1106. * And we're the group leader..
  1107. */
  1108. if (cqm_group_leader(event)) {
  1109. /*
  1110. * If there was a group_other, make that leader, otherwise
  1111. * destroy the group and return the RMID.
  1112. */
  1113. if (group_other) {
  1114. list_replace(&event->hw.cqm_groups_entry,
  1115. &group_other->hw.cqm_groups_entry);
  1116. } else {
  1117. u32 rmid = event->hw.cqm_rmid;
  1118. if (__rmid_valid(rmid))
  1119. __put_rmid(rmid);
  1120. list_del(&event->hw.cqm_groups_entry);
  1121. }
  1122. }
  1123. raw_spin_unlock_irqrestore(&cache_lock, flags);
  1124. /*
  1125. * Stop the mbm overflow timers when the last event is destroyed.
  1126. */
  1127. if (mbm_enabled && list_empty(&cache_groups))
  1128. mbm_stop_timers();
  1129. mutex_unlock(&cache_mutex);
  1130. }
  1131. static int intel_cqm_event_init(struct perf_event *event)
  1132. {
  1133. struct perf_event *group = NULL;
  1134. bool rotate = false;
  1135. unsigned long flags;
  1136. if (event->attr.type != intel_cqm_pmu.type)
  1137. return -ENOENT;
  1138. if ((event->attr.config < QOS_L3_OCCUP_EVENT_ID) ||
  1139. (event->attr.config > QOS_MBM_LOCAL_EVENT_ID))
  1140. return -EINVAL;
  1141. if ((is_cqm_event(event->attr.config) && !cqm_enabled) ||
  1142. (is_mbm_event(event->attr.config) && !mbm_enabled))
  1143. return -EINVAL;
  1144. /* unsupported modes and filters */
  1145. if (event->attr.exclude_user ||
  1146. event->attr.exclude_kernel ||
  1147. event->attr.exclude_hv ||
  1148. event->attr.exclude_idle ||
  1149. event->attr.exclude_host ||
  1150. event->attr.exclude_guest ||
  1151. event->attr.sample_period) /* no sampling */
  1152. return -EINVAL;
  1153. INIT_LIST_HEAD(&event->hw.cqm_group_entry);
  1154. INIT_LIST_HEAD(&event->hw.cqm_groups_entry);
  1155. event->destroy = intel_cqm_event_destroy;
  1156. mutex_lock(&cache_mutex);
  1157. /*
  1158. * Start the mbm overflow timers when the first event is created.
  1159. */
  1160. if (mbm_enabled && list_empty(&cache_groups))
  1161. mbm_start_timers();
  1162. /* Will also set rmid */
  1163. intel_cqm_setup_event(event, &group);
  1164. /*
  1165. * Hold the cache_lock as mbm timer handlers be
  1166. * scanning the list of events.
  1167. */
  1168. raw_spin_lock_irqsave(&cache_lock, flags);
  1169. if (group) {
  1170. list_add_tail(&event->hw.cqm_group_entry,
  1171. &group->hw.cqm_group_entry);
  1172. } else {
  1173. list_add_tail(&event->hw.cqm_groups_entry,
  1174. &cache_groups);
  1175. /*
  1176. * All RMIDs are either in use or have recently been
  1177. * used. Kick the rotation worker to clean/free some.
  1178. *
  1179. * We only do this for the group leader, rather than for
  1180. * every event in a group to save on needless work.
  1181. */
  1182. if (!__rmid_valid(event->hw.cqm_rmid))
  1183. rotate = true;
  1184. }
  1185. raw_spin_unlock_irqrestore(&cache_lock, flags);
  1186. mutex_unlock(&cache_mutex);
  1187. if (rotate)
  1188. schedule_delayed_work(&intel_cqm_rmid_work, 0);
  1189. return 0;
  1190. }
  1191. EVENT_ATTR_STR(llc_occupancy, intel_cqm_llc, "event=0x01");
  1192. EVENT_ATTR_STR(llc_occupancy.per-pkg, intel_cqm_llc_pkg, "1");
  1193. EVENT_ATTR_STR(llc_occupancy.unit, intel_cqm_llc_unit, "Bytes");
  1194. EVENT_ATTR_STR(llc_occupancy.scale, intel_cqm_llc_scale, NULL);
  1195. EVENT_ATTR_STR(llc_occupancy.snapshot, intel_cqm_llc_snapshot, "1");
  1196. EVENT_ATTR_STR(total_bytes, intel_cqm_total_bytes, "event=0x02");
  1197. EVENT_ATTR_STR(total_bytes.per-pkg, intel_cqm_total_bytes_pkg, "1");
  1198. EVENT_ATTR_STR(total_bytes.unit, intel_cqm_total_bytes_unit, "MB");
  1199. EVENT_ATTR_STR(total_bytes.scale, intel_cqm_total_bytes_scale, "1e-6");
  1200. EVENT_ATTR_STR(local_bytes, intel_cqm_local_bytes, "event=0x03");
  1201. EVENT_ATTR_STR(local_bytes.per-pkg, intel_cqm_local_bytes_pkg, "1");
  1202. EVENT_ATTR_STR(local_bytes.unit, intel_cqm_local_bytes_unit, "MB");
  1203. EVENT_ATTR_STR(local_bytes.scale, intel_cqm_local_bytes_scale, "1e-6");
  1204. static struct attribute *intel_cqm_events_attr[] = {
  1205. EVENT_PTR(intel_cqm_llc),
  1206. EVENT_PTR(intel_cqm_llc_pkg),
  1207. EVENT_PTR(intel_cqm_llc_unit),
  1208. EVENT_PTR(intel_cqm_llc_scale),
  1209. EVENT_PTR(intel_cqm_llc_snapshot),
  1210. NULL,
  1211. };
  1212. static struct attribute *intel_mbm_events_attr[] = {
  1213. EVENT_PTR(intel_cqm_total_bytes),
  1214. EVENT_PTR(intel_cqm_local_bytes),
  1215. EVENT_PTR(intel_cqm_total_bytes_pkg),
  1216. EVENT_PTR(intel_cqm_local_bytes_pkg),
  1217. EVENT_PTR(intel_cqm_total_bytes_unit),
  1218. EVENT_PTR(intel_cqm_local_bytes_unit),
  1219. EVENT_PTR(intel_cqm_total_bytes_scale),
  1220. EVENT_PTR(intel_cqm_local_bytes_scale),
  1221. NULL,
  1222. };
  1223. static struct attribute *intel_cmt_mbm_events_attr[] = {
  1224. EVENT_PTR(intel_cqm_llc),
  1225. EVENT_PTR(intel_cqm_total_bytes),
  1226. EVENT_PTR(intel_cqm_local_bytes),
  1227. EVENT_PTR(intel_cqm_llc_pkg),
  1228. EVENT_PTR(intel_cqm_total_bytes_pkg),
  1229. EVENT_PTR(intel_cqm_local_bytes_pkg),
  1230. EVENT_PTR(intel_cqm_llc_unit),
  1231. EVENT_PTR(intel_cqm_total_bytes_unit),
  1232. EVENT_PTR(intel_cqm_local_bytes_unit),
  1233. EVENT_PTR(intel_cqm_llc_scale),
  1234. EVENT_PTR(intel_cqm_total_bytes_scale),
  1235. EVENT_PTR(intel_cqm_local_bytes_scale),
  1236. EVENT_PTR(intel_cqm_llc_snapshot),
  1237. NULL,
  1238. };
  1239. static struct attribute_group intel_cqm_events_group = {
  1240. .name = "events",
  1241. .attrs = NULL,
  1242. };
  1243. PMU_FORMAT_ATTR(event, "config:0-7");
  1244. static struct attribute *intel_cqm_formats_attr[] = {
  1245. &format_attr_event.attr,
  1246. NULL,
  1247. };
  1248. static struct attribute_group intel_cqm_format_group = {
  1249. .name = "format",
  1250. .attrs = intel_cqm_formats_attr,
  1251. };
  1252. static ssize_t
  1253. max_recycle_threshold_show(struct device *dev, struct device_attribute *attr,
  1254. char *page)
  1255. {
  1256. ssize_t rv;
  1257. mutex_lock(&cache_mutex);
  1258. rv = snprintf(page, PAGE_SIZE-1, "%u\n", __intel_cqm_max_threshold);
  1259. mutex_unlock(&cache_mutex);
  1260. return rv;
  1261. }
  1262. static ssize_t
  1263. max_recycle_threshold_store(struct device *dev,
  1264. struct device_attribute *attr,
  1265. const char *buf, size_t count)
  1266. {
  1267. unsigned int bytes, cachelines;
  1268. int ret;
  1269. ret = kstrtouint(buf, 0, &bytes);
  1270. if (ret)
  1271. return ret;
  1272. mutex_lock(&cache_mutex);
  1273. __intel_cqm_max_threshold = bytes;
  1274. cachelines = bytes / cqm_l3_scale;
  1275. /*
  1276. * The new maximum takes effect immediately.
  1277. */
  1278. if (__intel_cqm_threshold > cachelines)
  1279. __intel_cqm_threshold = cachelines;
  1280. mutex_unlock(&cache_mutex);
  1281. return count;
  1282. }
  1283. static DEVICE_ATTR_RW(max_recycle_threshold);
  1284. static struct attribute *intel_cqm_attrs[] = {
  1285. &dev_attr_max_recycle_threshold.attr,
  1286. NULL,
  1287. };
  1288. static const struct attribute_group intel_cqm_group = {
  1289. .attrs = intel_cqm_attrs,
  1290. };
  1291. static const struct attribute_group *intel_cqm_attr_groups[] = {
  1292. &intel_cqm_events_group,
  1293. &intel_cqm_format_group,
  1294. &intel_cqm_group,
  1295. NULL,
  1296. };
  1297. static struct pmu intel_cqm_pmu = {
  1298. .hrtimer_interval_ms = RMID_DEFAULT_QUEUE_TIME,
  1299. .attr_groups = intel_cqm_attr_groups,
  1300. .task_ctx_nr = perf_sw_context,
  1301. .event_init = intel_cqm_event_init,
  1302. .add = intel_cqm_event_add,
  1303. .del = intel_cqm_event_stop,
  1304. .start = intel_cqm_event_start,
  1305. .stop = intel_cqm_event_stop,
  1306. .read = intel_cqm_event_read,
  1307. .count = intel_cqm_event_count,
  1308. };
  1309. static inline void cqm_pick_event_reader(int cpu)
  1310. {
  1311. int reader;
  1312. /* First online cpu in package becomes the reader */
  1313. reader = cpumask_any_and(&cqm_cpumask, topology_core_cpumask(cpu));
  1314. if (reader >= nr_cpu_ids)
  1315. cpumask_set_cpu(cpu, &cqm_cpumask);
  1316. }
  1317. static int intel_cqm_cpu_starting(unsigned int cpu)
  1318. {
  1319. struct intel_pqr_state *state = &per_cpu(pqr_state, cpu);
  1320. struct cpuinfo_x86 *c = &cpu_data(cpu);
  1321. state->rmid = 0;
  1322. state->closid = 0;
  1323. state->rmid_usecnt = 0;
  1324. WARN_ON(c->x86_cache_max_rmid != cqm_max_rmid);
  1325. WARN_ON(c->x86_cache_occ_scale != cqm_l3_scale);
  1326. cqm_pick_event_reader(cpu);
  1327. return 0;
  1328. }
  1329. static int intel_cqm_cpu_exit(unsigned int cpu)
  1330. {
  1331. int target;
  1332. /* Is @cpu the current cqm reader for this package ? */
  1333. if (!cpumask_test_and_clear_cpu(cpu, &cqm_cpumask))
  1334. return 0;
  1335. /* Find another online reader in this package */
  1336. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  1337. if (target < nr_cpu_ids)
  1338. cpumask_set_cpu(target, &cqm_cpumask);
  1339. return 0;
  1340. }
  1341. static const struct x86_cpu_id intel_cqm_match[] = {
  1342. { .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_OCCUP_LLC },
  1343. {}
  1344. };
  1345. static void mbm_cleanup(void)
  1346. {
  1347. if (!mbm_enabled)
  1348. return;
  1349. kfree(mbm_local);
  1350. kfree(mbm_total);
  1351. mbm_enabled = false;
  1352. }
  1353. static const struct x86_cpu_id intel_mbm_local_match[] = {
  1354. { .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_MBM_LOCAL },
  1355. {}
  1356. };
  1357. static const struct x86_cpu_id intel_mbm_total_match[] = {
  1358. { .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_MBM_TOTAL },
  1359. {}
  1360. };
  1361. static int intel_mbm_init(void)
  1362. {
  1363. int ret = 0, array_size, maxid = cqm_max_rmid + 1;
  1364. mbm_socket_max = topology_max_packages();
  1365. array_size = sizeof(struct sample) * maxid * mbm_socket_max;
  1366. mbm_local = kmalloc(array_size, GFP_KERNEL);
  1367. if (!mbm_local)
  1368. return -ENOMEM;
  1369. mbm_total = kmalloc(array_size, GFP_KERNEL);
  1370. if (!mbm_total) {
  1371. ret = -ENOMEM;
  1372. goto out;
  1373. }
  1374. array_size = sizeof(struct hrtimer) * mbm_socket_max;
  1375. mbm_timers = kmalloc(array_size, GFP_KERNEL);
  1376. if (!mbm_timers) {
  1377. ret = -ENOMEM;
  1378. goto out;
  1379. }
  1380. mbm_hrtimer_init();
  1381. out:
  1382. if (ret)
  1383. mbm_cleanup();
  1384. return ret;
  1385. }
  1386. static int __init intel_cqm_init(void)
  1387. {
  1388. char *str = NULL, scale[20];
  1389. int cpu, ret;
  1390. if (x86_match_cpu(intel_cqm_match))
  1391. cqm_enabled = true;
  1392. if (x86_match_cpu(intel_mbm_local_match) &&
  1393. x86_match_cpu(intel_mbm_total_match))
  1394. mbm_enabled = true;
  1395. if (!cqm_enabled && !mbm_enabled)
  1396. return -ENODEV;
  1397. cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
  1398. /*
  1399. * It's possible that not all resources support the same number
  1400. * of RMIDs. Instead of making scheduling much more complicated
  1401. * (where we have to match a task's RMID to a cpu that supports
  1402. * that many RMIDs) just find the minimum RMIDs supported across
  1403. * all cpus.
  1404. *
  1405. * Also, check that the scales match on all cpus.
  1406. */
  1407. get_online_cpus();
  1408. for_each_online_cpu(cpu) {
  1409. struct cpuinfo_x86 *c = &cpu_data(cpu);
  1410. if (c->x86_cache_max_rmid < cqm_max_rmid)
  1411. cqm_max_rmid = c->x86_cache_max_rmid;
  1412. if (c->x86_cache_occ_scale != cqm_l3_scale) {
  1413. pr_err("Multiple LLC scale values, disabling\n");
  1414. ret = -EINVAL;
  1415. goto out;
  1416. }
  1417. }
  1418. /*
  1419. * A reasonable upper limit on the max threshold is the number
  1420. * of lines tagged per RMID if all RMIDs have the same number of
  1421. * lines tagged in the LLC.
  1422. *
  1423. * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
  1424. */
  1425. __intel_cqm_max_threshold =
  1426. boot_cpu_data.x86_cache_size * 1024 / (cqm_max_rmid + 1);
  1427. snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
  1428. str = kstrdup(scale, GFP_KERNEL);
  1429. if (!str) {
  1430. ret = -ENOMEM;
  1431. goto out;
  1432. }
  1433. event_attr_intel_cqm_llc_scale.event_str = str;
  1434. ret = intel_cqm_setup_rmid_cache();
  1435. if (ret)
  1436. goto out;
  1437. if (mbm_enabled)
  1438. ret = intel_mbm_init();
  1439. if (ret && !cqm_enabled)
  1440. goto out;
  1441. if (cqm_enabled && mbm_enabled)
  1442. intel_cqm_events_group.attrs = intel_cmt_mbm_events_attr;
  1443. else if (!cqm_enabled && mbm_enabled)
  1444. intel_cqm_events_group.attrs = intel_mbm_events_attr;
  1445. else if (cqm_enabled && !mbm_enabled)
  1446. intel_cqm_events_group.attrs = intel_cqm_events_attr;
  1447. ret = perf_pmu_register(&intel_cqm_pmu, "intel_cqm", -1);
  1448. if (ret) {
  1449. pr_err("Intel CQM perf registration failed: %d\n", ret);
  1450. goto out;
  1451. }
  1452. if (cqm_enabled)
  1453. pr_info("Intel CQM monitoring enabled\n");
  1454. if (mbm_enabled)
  1455. pr_info("Intel MBM enabled\n");
  1456. /*
  1457. * Setup the hot cpu notifier once we are sure cqm
  1458. * is enabled to avoid notifier leak.
  1459. */
  1460. cpuhp_setup_state(CPUHP_AP_PERF_X86_CQM_STARTING,
  1461. "AP_PERF_X86_CQM_STARTING",
  1462. intel_cqm_cpu_starting, NULL);
  1463. cpuhp_setup_state(CPUHP_AP_PERF_X86_CQM_ONLINE, "AP_PERF_X86_CQM_ONLINE",
  1464. NULL, intel_cqm_cpu_exit);
  1465. out:
  1466. put_online_cpus();
  1467. if (ret) {
  1468. kfree(str);
  1469. cqm_cleanup();
  1470. mbm_cleanup();
  1471. }
  1472. return ret;
  1473. }
  1474. device_initcall(intel_cqm_init);