perf_event_intel_cqm.c 34 KB

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