intel_rdt_pseudo_lock.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Resource Director Technology (RDT)
  4. *
  5. * Pseudo-locking support built on top of Cache Allocation Technology (CAT)
  6. *
  7. * Copyright (C) 2018 Intel Corporation
  8. *
  9. * Author: Reinette Chatre <reinette.chatre@intel.com>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/cacheinfo.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/kthread.h>
  17. #include <linux/mman.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/intel-family.h>
  23. #include <asm/intel_rdt_sched.h>
  24. #include <asm/perf_event.h>
  25. #include "intel_rdt.h"
  26. #define CREATE_TRACE_POINTS
  27. #include "intel_rdt_pseudo_lock_event.h"
  28. /*
  29. * MSR_MISC_FEATURE_CONTROL register enables the modification of hardware
  30. * prefetcher state. Details about this register can be found in the MSR
  31. * tables for specific platforms found in Intel's SDM.
  32. */
  33. #define MSR_MISC_FEATURE_CONTROL 0x000001a4
  34. /*
  35. * The bits needed to disable hardware prefetching varies based on the
  36. * platform. During initialization we will discover which bits to use.
  37. */
  38. static u64 prefetch_disable_bits;
  39. /*
  40. * Major number assigned to and shared by all devices exposing
  41. * pseudo-locked regions.
  42. */
  43. static unsigned int pseudo_lock_major;
  44. static unsigned long pseudo_lock_minor_avail = GENMASK(MINORBITS, 0);
  45. static struct class *pseudo_lock_class;
  46. /**
  47. * get_prefetch_disable_bits - prefetch disable bits of supported platforms
  48. *
  49. * Capture the list of platforms that have been validated to support
  50. * pseudo-locking. This includes testing to ensure pseudo-locked regions
  51. * with low cache miss rates can be created under variety of load conditions
  52. * as well as that these pseudo-locked regions can maintain their low cache
  53. * miss rates under variety of load conditions for significant lengths of time.
  54. *
  55. * After a platform has been validated to support pseudo-locking its
  56. * hardware prefetch disable bits are included here as they are documented
  57. * in the SDM.
  58. *
  59. * When adding a platform here also add support for its cache events to
  60. * measure_cycles_perf_fn()
  61. *
  62. * Return:
  63. * If platform is supported, the bits to disable hardware prefetchers, 0
  64. * if platform is not supported.
  65. */
  66. static u64 get_prefetch_disable_bits(void)
  67. {
  68. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  69. boot_cpu_data.x86 != 6)
  70. return 0;
  71. switch (boot_cpu_data.x86_model) {
  72. case INTEL_FAM6_BROADWELL_X:
  73. /*
  74. * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
  75. * as:
  76. * 0 L2 Hardware Prefetcher Disable (R/W)
  77. * 1 L2 Adjacent Cache Line Prefetcher Disable (R/W)
  78. * 2 DCU Hardware Prefetcher Disable (R/W)
  79. * 3 DCU IP Prefetcher Disable (R/W)
  80. * 63:4 Reserved
  81. */
  82. return 0xF;
  83. case INTEL_FAM6_ATOM_GOLDMONT:
  84. case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
  85. /*
  86. * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
  87. * as:
  88. * 0 L2 Hardware Prefetcher Disable (R/W)
  89. * 1 Reserved
  90. * 2 DCU Hardware Prefetcher Disable (R/W)
  91. * 63:3 Reserved
  92. */
  93. return 0x5;
  94. }
  95. return 0;
  96. }
  97. /*
  98. * Helper to write 64bit value to MSR without tracing. Used when
  99. * use of the cache should be restricted and use of registers used
  100. * for local variables avoided.
  101. */
  102. static inline void pseudo_wrmsrl_notrace(unsigned int msr, u64 val)
  103. {
  104. __wrmsr(msr, (u32)(val & 0xffffffffULL), (u32)(val >> 32));
  105. }
  106. /**
  107. * pseudo_lock_minor_get - Obtain available minor number
  108. * @minor: Pointer to where new minor number will be stored
  109. *
  110. * A bitmask is used to track available minor numbers. Here the next free
  111. * minor number is marked as unavailable and returned.
  112. *
  113. * Return: 0 on success, <0 on failure.
  114. */
  115. static int pseudo_lock_minor_get(unsigned int *minor)
  116. {
  117. unsigned long first_bit;
  118. first_bit = find_first_bit(&pseudo_lock_minor_avail, MINORBITS);
  119. if (first_bit == MINORBITS)
  120. return -ENOSPC;
  121. __clear_bit(first_bit, &pseudo_lock_minor_avail);
  122. *minor = first_bit;
  123. return 0;
  124. }
  125. /**
  126. * pseudo_lock_minor_release - Return minor number to available
  127. * @minor: The minor number made available
  128. */
  129. static void pseudo_lock_minor_release(unsigned int minor)
  130. {
  131. __set_bit(minor, &pseudo_lock_minor_avail);
  132. }
  133. /**
  134. * region_find_by_minor - Locate a pseudo-lock region by inode minor number
  135. * @minor: The minor number of the device representing pseudo-locked region
  136. *
  137. * When the character device is accessed we need to determine which
  138. * pseudo-locked region it belongs to. This is done by matching the minor
  139. * number of the device to the pseudo-locked region it belongs.
  140. *
  141. * Minor numbers are assigned at the time a pseudo-locked region is associated
  142. * with a cache instance.
  143. *
  144. * Return: On success return pointer to resource group owning the pseudo-locked
  145. * region, NULL on failure.
  146. */
  147. static struct rdtgroup *region_find_by_minor(unsigned int minor)
  148. {
  149. struct rdtgroup *rdtgrp, *rdtgrp_match = NULL;
  150. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  151. if (rdtgrp->plr && rdtgrp->plr->minor == minor) {
  152. rdtgrp_match = rdtgrp;
  153. break;
  154. }
  155. }
  156. return rdtgrp_match;
  157. }
  158. /**
  159. * pseudo_lock_pm_req - A power management QoS request list entry
  160. * @list: Entry within the @pm_reqs list for a pseudo-locked region
  161. * @req: PM QoS request
  162. */
  163. struct pseudo_lock_pm_req {
  164. struct list_head list;
  165. struct dev_pm_qos_request req;
  166. };
  167. static void pseudo_lock_cstates_relax(struct pseudo_lock_region *plr)
  168. {
  169. struct pseudo_lock_pm_req *pm_req, *next;
  170. list_for_each_entry_safe(pm_req, next, &plr->pm_reqs, list) {
  171. dev_pm_qos_remove_request(&pm_req->req);
  172. list_del(&pm_req->list);
  173. kfree(pm_req);
  174. }
  175. }
  176. /**
  177. * pseudo_lock_cstates_constrain - Restrict cores from entering C6
  178. *
  179. * To prevent the cache from being affected by power management entering
  180. * C6 has to be avoided. This is accomplished by requesting a latency
  181. * requirement lower than lowest C6 exit latency of all supported
  182. * platforms as found in the cpuidle state tables in the intel_idle driver.
  183. * At this time it is possible to do so with a single latency requirement
  184. * for all supported platforms.
  185. *
  186. * Since Goldmont is supported, which is affected by X86_BUG_MONITOR,
  187. * the ACPI latencies need to be considered while keeping in mind that C2
  188. * may be set to map to deeper sleep states. In this case the latency
  189. * requirement needs to prevent entering C2 also.
  190. */
  191. static int pseudo_lock_cstates_constrain(struct pseudo_lock_region *plr)
  192. {
  193. struct pseudo_lock_pm_req *pm_req;
  194. int cpu;
  195. int ret;
  196. for_each_cpu(cpu, &plr->d->cpu_mask) {
  197. pm_req = kzalloc(sizeof(*pm_req), GFP_KERNEL);
  198. if (!pm_req) {
  199. rdt_last_cmd_puts("fail allocating mem for PM QoS\n");
  200. ret = -ENOMEM;
  201. goto out_err;
  202. }
  203. ret = dev_pm_qos_add_request(get_cpu_device(cpu),
  204. &pm_req->req,
  205. DEV_PM_QOS_RESUME_LATENCY,
  206. 30);
  207. if (ret < 0) {
  208. rdt_last_cmd_printf("fail to add latency req cpu%d\n",
  209. cpu);
  210. kfree(pm_req);
  211. ret = -1;
  212. goto out_err;
  213. }
  214. list_add(&pm_req->list, &plr->pm_reqs);
  215. }
  216. return 0;
  217. out_err:
  218. pseudo_lock_cstates_relax(plr);
  219. return ret;
  220. }
  221. /**
  222. * pseudo_lock_region_clear - Reset pseudo-lock region data
  223. * @plr: pseudo-lock region
  224. *
  225. * All content of the pseudo-locked region is reset - any memory allocated
  226. * freed.
  227. *
  228. * Return: void
  229. */
  230. static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)
  231. {
  232. plr->size = 0;
  233. plr->line_size = 0;
  234. kfree(plr->kmem);
  235. plr->kmem = NULL;
  236. plr->r = NULL;
  237. if (plr->d)
  238. plr->d->plr = NULL;
  239. plr->d = NULL;
  240. plr->cbm = 0;
  241. plr->debugfs_dir = NULL;
  242. }
  243. /**
  244. * pseudo_lock_region_init - Initialize pseudo-lock region information
  245. * @plr: pseudo-lock region
  246. *
  247. * Called after user provided a schemata to be pseudo-locked. From the
  248. * schemata the &struct pseudo_lock_region is on entry already initialized
  249. * with the resource, domain, and capacity bitmask. Here the information
  250. * required for pseudo-locking is deduced from this data and &struct
  251. * pseudo_lock_region initialized further. This information includes:
  252. * - size in bytes of the region to be pseudo-locked
  253. * - cache line size to know the stride with which data needs to be accessed
  254. * to be pseudo-locked
  255. * - a cpu associated with the cache instance on which the pseudo-locking
  256. * flow can be executed
  257. *
  258. * Return: 0 on success, <0 on failure. Descriptive error will be written
  259. * to last_cmd_status buffer.
  260. */
  261. static int pseudo_lock_region_init(struct pseudo_lock_region *plr)
  262. {
  263. struct cpu_cacheinfo *ci;
  264. int ret;
  265. int i;
  266. /* Pick the first cpu we find that is associated with the cache. */
  267. plr->cpu = cpumask_first(&plr->d->cpu_mask);
  268. if (!cpu_online(plr->cpu)) {
  269. rdt_last_cmd_printf("cpu %u associated with cache not online\n",
  270. plr->cpu);
  271. ret = -ENODEV;
  272. goto out_region;
  273. }
  274. ci = get_cpu_cacheinfo(plr->cpu);
  275. plr->size = rdtgroup_cbm_to_size(plr->r, plr->d, plr->cbm);
  276. for (i = 0; i < ci->num_leaves; i++) {
  277. if (ci->info_list[i].level == plr->r->cache_level) {
  278. plr->line_size = ci->info_list[i].coherency_line_size;
  279. return 0;
  280. }
  281. }
  282. ret = -1;
  283. rdt_last_cmd_puts("unable to determine cache line size\n");
  284. out_region:
  285. pseudo_lock_region_clear(plr);
  286. return ret;
  287. }
  288. /**
  289. * pseudo_lock_init - Initialize a pseudo-lock region
  290. * @rdtgrp: resource group to which new pseudo-locked region will belong
  291. *
  292. * A pseudo-locked region is associated with a resource group. When this
  293. * association is created the pseudo-locked region is initialized. The
  294. * details of the pseudo-locked region are not known at this time so only
  295. * allocation is done and association established.
  296. *
  297. * Return: 0 on success, <0 on failure
  298. */
  299. static int pseudo_lock_init(struct rdtgroup *rdtgrp)
  300. {
  301. struct pseudo_lock_region *plr;
  302. plr = kzalloc(sizeof(*plr), GFP_KERNEL);
  303. if (!plr)
  304. return -ENOMEM;
  305. init_waitqueue_head(&plr->lock_thread_wq);
  306. INIT_LIST_HEAD(&plr->pm_reqs);
  307. rdtgrp->plr = plr;
  308. return 0;
  309. }
  310. /**
  311. * pseudo_lock_region_alloc - Allocate kernel memory that will be pseudo-locked
  312. * @plr: pseudo-lock region
  313. *
  314. * Initialize the details required to set up the pseudo-locked region and
  315. * allocate the contiguous memory that will be pseudo-locked to the cache.
  316. *
  317. * Return: 0 on success, <0 on failure. Descriptive error will be written
  318. * to last_cmd_status buffer.
  319. */
  320. static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr)
  321. {
  322. int ret;
  323. ret = pseudo_lock_region_init(plr);
  324. if (ret < 0)
  325. return ret;
  326. /*
  327. * We do not yet support contiguous regions larger than
  328. * KMALLOC_MAX_SIZE.
  329. */
  330. if (plr->size > KMALLOC_MAX_SIZE) {
  331. rdt_last_cmd_puts("requested region exceeds maximum size\n");
  332. ret = -E2BIG;
  333. goto out_region;
  334. }
  335. plr->kmem = kzalloc(plr->size, GFP_KERNEL);
  336. if (!plr->kmem) {
  337. rdt_last_cmd_puts("unable to allocate memory\n");
  338. ret = -ENOMEM;
  339. goto out_region;
  340. }
  341. ret = 0;
  342. goto out;
  343. out_region:
  344. pseudo_lock_region_clear(plr);
  345. out:
  346. return ret;
  347. }
  348. /**
  349. * pseudo_lock_free - Free a pseudo-locked region
  350. * @rdtgrp: resource group to which pseudo-locked region belonged
  351. *
  352. * The pseudo-locked region's resources have already been released, or not
  353. * yet created at this point. Now it can be freed and disassociated from the
  354. * resource group.
  355. *
  356. * Return: void
  357. */
  358. static void pseudo_lock_free(struct rdtgroup *rdtgrp)
  359. {
  360. pseudo_lock_region_clear(rdtgrp->plr);
  361. kfree(rdtgrp->plr);
  362. rdtgrp->plr = NULL;
  363. }
  364. /**
  365. * pseudo_lock_fn - Load kernel memory into cache
  366. * @_rdtgrp: resource group to which pseudo-lock region belongs
  367. *
  368. * This is the core pseudo-locking flow.
  369. *
  370. * First we ensure that the kernel memory cannot be found in the cache.
  371. * Then, while taking care that there will be as little interference as
  372. * possible, the memory to be loaded is accessed while core is running
  373. * with class of service set to the bitmask of the pseudo-locked region.
  374. * After this is complete no future CAT allocations will be allowed to
  375. * overlap with this bitmask.
  376. *
  377. * Local register variables are utilized to ensure that the memory region
  378. * to be locked is the only memory access made during the critical locking
  379. * loop.
  380. *
  381. * Return: 0. Waiter on waitqueue will be woken on completion.
  382. */
  383. static int pseudo_lock_fn(void *_rdtgrp)
  384. {
  385. struct rdtgroup *rdtgrp = _rdtgrp;
  386. struct pseudo_lock_region *plr = rdtgrp->plr;
  387. u32 rmid_p, closid_p;
  388. unsigned long i;
  389. #ifdef CONFIG_KASAN
  390. /*
  391. * The registers used for local register variables are also used
  392. * when KASAN is active. When KASAN is active we use a regular
  393. * variable to ensure we always use a valid pointer, but the cost
  394. * is that this variable will enter the cache through evicting the
  395. * memory we are trying to lock into the cache. Thus expect lower
  396. * pseudo-locking success rate when KASAN is active.
  397. */
  398. unsigned int line_size;
  399. unsigned int size;
  400. void *mem_r;
  401. #else
  402. register unsigned int line_size asm("esi");
  403. register unsigned int size asm("edi");
  404. #ifdef CONFIG_X86_64
  405. register void *mem_r asm("rbx");
  406. #else
  407. register void *mem_r asm("ebx");
  408. #endif /* CONFIG_X86_64 */
  409. #endif /* CONFIG_KASAN */
  410. /*
  411. * Make sure none of the allocated memory is cached. If it is we
  412. * will get a cache hit in below loop from outside of pseudo-locked
  413. * region.
  414. * wbinvd (as opposed to clflush/clflushopt) is required to
  415. * increase likelihood that allocated cache portion will be filled
  416. * with associated memory.
  417. */
  418. native_wbinvd();
  419. /*
  420. * Always called with interrupts enabled. By disabling interrupts
  421. * ensure that we will not be preempted during this critical section.
  422. */
  423. local_irq_disable();
  424. /*
  425. * Call wrmsr and rdmsr as directly as possible to avoid tracing
  426. * clobbering local register variables or affecting cache accesses.
  427. *
  428. * Disable the hardware prefetcher so that when the end of the memory
  429. * being pseudo-locked is reached the hardware will not read beyond
  430. * the buffer and evict pseudo-locked memory read earlier from the
  431. * cache.
  432. */
  433. __wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
  434. closid_p = this_cpu_read(pqr_state.cur_closid);
  435. rmid_p = this_cpu_read(pqr_state.cur_rmid);
  436. mem_r = plr->kmem;
  437. size = plr->size;
  438. line_size = plr->line_size;
  439. /*
  440. * Critical section begin: start by writing the closid associated
  441. * with the capacity bitmask of the cache region being
  442. * pseudo-locked followed by reading of kernel memory to load it
  443. * into the cache.
  444. */
  445. __wrmsr(IA32_PQR_ASSOC, rmid_p, rdtgrp->closid);
  446. /*
  447. * Cache was flushed earlier. Now access kernel memory to read it
  448. * into cache region associated with just activated plr->closid.
  449. * Loop over data twice:
  450. * - In first loop the cache region is shared with the page walker
  451. * as it populates the paging structure caches (including TLB).
  452. * - In the second loop the paging structure caches are used and
  453. * cache region is populated with the memory being referenced.
  454. */
  455. for (i = 0; i < size; i += PAGE_SIZE) {
  456. /*
  457. * Add a barrier to prevent speculative execution of this
  458. * loop reading beyond the end of the buffer.
  459. */
  460. rmb();
  461. asm volatile("mov (%0,%1,1), %%eax\n\t"
  462. :
  463. : "r" (mem_r), "r" (i)
  464. : "%eax", "memory");
  465. }
  466. for (i = 0; i < size; i += line_size) {
  467. /*
  468. * Add a barrier to prevent speculative execution of this
  469. * loop reading beyond the end of the buffer.
  470. */
  471. rmb();
  472. asm volatile("mov (%0,%1,1), %%eax\n\t"
  473. :
  474. : "r" (mem_r), "r" (i)
  475. : "%eax", "memory");
  476. }
  477. /*
  478. * Critical section end: restore closid with capacity bitmask that
  479. * does not overlap with pseudo-locked region.
  480. */
  481. __wrmsr(IA32_PQR_ASSOC, rmid_p, closid_p);
  482. /* Re-enable the hardware prefetcher(s) */
  483. wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
  484. local_irq_enable();
  485. plr->thread_done = 1;
  486. wake_up_interruptible(&plr->lock_thread_wq);
  487. return 0;
  488. }
  489. /**
  490. * rdtgroup_monitor_in_progress - Test if monitoring in progress
  491. * @r: resource group being queried
  492. *
  493. * Return: 1 if monitor groups have been created for this resource
  494. * group, 0 otherwise.
  495. */
  496. static int rdtgroup_monitor_in_progress(struct rdtgroup *rdtgrp)
  497. {
  498. return !list_empty(&rdtgrp->mon.crdtgrp_list);
  499. }
  500. /**
  501. * rdtgroup_locksetup_user_restrict - Restrict user access to group
  502. * @rdtgrp: resource group needing access restricted
  503. *
  504. * A resource group used for cache pseudo-locking cannot have cpus or tasks
  505. * assigned to it. This is communicated to the user by restricting access
  506. * to all the files that can be used to make such changes.
  507. *
  508. * Permissions restored with rdtgroup_locksetup_user_restore()
  509. *
  510. * Return: 0 on success, <0 on failure. If a failure occurs during the
  511. * restriction of access an attempt will be made to restore permissions but
  512. * the state of the mode of these files will be uncertain when a failure
  513. * occurs.
  514. */
  515. static int rdtgroup_locksetup_user_restrict(struct rdtgroup *rdtgrp)
  516. {
  517. int ret;
  518. ret = rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
  519. if (ret)
  520. return ret;
  521. ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
  522. if (ret)
  523. goto err_tasks;
  524. ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
  525. if (ret)
  526. goto err_cpus;
  527. if (rdt_mon_capable) {
  528. ret = rdtgroup_kn_mode_restrict(rdtgrp, "mon_groups");
  529. if (ret)
  530. goto err_cpus_list;
  531. }
  532. ret = 0;
  533. goto out;
  534. err_cpus_list:
  535. rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
  536. err_cpus:
  537. rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
  538. err_tasks:
  539. rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
  540. out:
  541. return ret;
  542. }
  543. /**
  544. * rdtgroup_locksetup_user_restore - Restore user access to group
  545. * @rdtgrp: resource group needing access restored
  546. *
  547. * Restore all file access previously removed using
  548. * rdtgroup_locksetup_user_restrict()
  549. *
  550. * Return: 0 on success, <0 on failure. If a failure occurs during the
  551. * restoration of access an attempt will be made to restrict permissions
  552. * again but the state of the mode of these files will be uncertain when
  553. * a failure occurs.
  554. */
  555. static int rdtgroup_locksetup_user_restore(struct rdtgroup *rdtgrp)
  556. {
  557. int ret;
  558. ret = rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
  559. if (ret)
  560. return ret;
  561. ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
  562. if (ret)
  563. goto err_tasks;
  564. ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
  565. if (ret)
  566. goto err_cpus;
  567. if (rdt_mon_capable) {
  568. ret = rdtgroup_kn_mode_restore(rdtgrp, "mon_groups", 0777);
  569. if (ret)
  570. goto err_cpus_list;
  571. }
  572. ret = 0;
  573. goto out;
  574. err_cpus_list:
  575. rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
  576. err_cpus:
  577. rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
  578. err_tasks:
  579. rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
  580. out:
  581. return ret;
  582. }
  583. /**
  584. * rdtgroup_locksetup_enter - Resource group enters locksetup mode
  585. * @rdtgrp: resource group requested to enter locksetup mode
  586. *
  587. * A resource group enters locksetup mode to reflect that it would be used
  588. * to represent a pseudo-locked region and is in the process of being set
  589. * up to do so. A resource group used for a pseudo-locked region would
  590. * lose the closid associated with it so we cannot allow it to have any
  591. * tasks or cpus assigned nor permit tasks or cpus to be assigned in the
  592. * future. Monitoring of a pseudo-locked region is not allowed either.
  593. *
  594. * The above and more restrictions on a pseudo-locked region are checked
  595. * for and enforced before the resource group enters the locksetup mode.
  596. *
  597. * Returns: 0 if the resource group successfully entered locksetup mode, <0
  598. * on failure. On failure the last_cmd_status buffer is updated with text to
  599. * communicate details of failure to the user.
  600. */
  601. int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
  602. {
  603. int ret;
  604. /*
  605. * The default resource group can neither be removed nor lose the
  606. * default closid associated with it.
  607. */
  608. if (rdtgrp == &rdtgroup_default) {
  609. rdt_last_cmd_puts("cannot pseudo-lock default group\n");
  610. return -EINVAL;
  611. }
  612. /*
  613. * Cache Pseudo-locking not supported when CDP is enabled.
  614. *
  615. * Some things to consider if you would like to enable this
  616. * support (using L3 CDP as example):
  617. * - When CDP is enabled two separate resources are exposed,
  618. * L3DATA and L3CODE, but they are actually on the same cache.
  619. * The implication for pseudo-locking is that if a
  620. * pseudo-locked region is created on a domain of one
  621. * resource (eg. L3CODE), then a pseudo-locked region cannot
  622. * be created on that same domain of the other resource
  623. * (eg. L3DATA). This is because the creation of a
  624. * pseudo-locked region involves a call to wbinvd that will
  625. * affect all cache allocations on particular domain.
  626. * - Considering the previous, it may be possible to only
  627. * expose one of the CDP resources to pseudo-locking and
  628. * hide the other. For example, we could consider to only
  629. * expose L3DATA and since the L3 cache is unified it is
  630. * still possible to place instructions there are execute it.
  631. * - If only one region is exposed to pseudo-locking we should
  632. * still keep in mind that availability of a portion of cache
  633. * for pseudo-locking should take into account both resources.
  634. * Similarly, if a pseudo-locked region is created in one
  635. * resource, the portion of cache used by it should be made
  636. * unavailable to all future allocations from both resources.
  637. */
  638. if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled ||
  639. rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) {
  640. rdt_last_cmd_puts("CDP enabled\n");
  641. return -EINVAL;
  642. }
  643. /*
  644. * Not knowing the bits to disable prefetching implies that this
  645. * platform does not support Cache Pseudo-Locking.
  646. */
  647. prefetch_disable_bits = get_prefetch_disable_bits();
  648. if (prefetch_disable_bits == 0) {
  649. rdt_last_cmd_puts("pseudo-locking not supported\n");
  650. return -EINVAL;
  651. }
  652. if (rdtgroup_monitor_in_progress(rdtgrp)) {
  653. rdt_last_cmd_puts("monitoring in progress\n");
  654. return -EINVAL;
  655. }
  656. if (rdtgroup_tasks_assigned(rdtgrp)) {
  657. rdt_last_cmd_puts("tasks assigned to resource group\n");
  658. return -EINVAL;
  659. }
  660. if (!cpumask_empty(&rdtgrp->cpu_mask)) {
  661. rdt_last_cmd_puts("CPUs assigned to resource group\n");
  662. return -EINVAL;
  663. }
  664. if (rdtgroup_locksetup_user_restrict(rdtgrp)) {
  665. rdt_last_cmd_puts("unable to modify resctrl permissions\n");
  666. return -EIO;
  667. }
  668. ret = pseudo_lock_init(rdtgrp);
  669. if (ret) {
  670. rdt_last_cmd_puts("unable to init pseudo-lock region\n");
  671. goto out_release;
  672. }
  673. /*
  674. * If this system is capable of monitoring a rmid would have been
  675. * allocated when the control group was created. This is not needed
  676. * anymore when this group would be used for pseudo-locking. This
  677. * is safe to call on platforms not capable of monitoring.
  678. */
  679. free_rmid(rdtgrp->mon.rmid);
  680. ret = 0;
  681. goto out;
  682. out_release:
  683. rdtgroup_locksetup_user_restore(rdtgrp);
  684. out:
  685. return ret;
  686. }
  687. /**
  688. * rdtgroup_locksetup_exit - resource group exist locksetup mode
  689. * @rdtgrp: resource group
  690. *
  691. * When a resource group exits locksetup mode the earlier restrictions are
  692. * lifted.
  693. *
  694. * Return: 0 on success, <0 on failure
  695. */
  696. int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
  697. {
  698. int ret;
  699. if (rdt_mon_capable) {
  700. ret = alloc_rmid();
  701. if (ret < 0) {
  702. rdt_last_cmd_puts("out of RMIDs\n");
  703. return ret;
  704. }
  705. rdtgrp->mon.rmid = ret;
  706. }
  707. ret = rdtgroup_locksetup_user_restore(rdtgrp);
  708. if (ret) {
  709. free_rmid(rdtgrp->mon.rmid);
  710. return ret;
  711. }
  712. pseudo_lock_free(rdtgrp);
  713. return 0;
  714. }
  715. /**
  716. * rdtgroup_cbm_overlaps_pseudo_locked - Test if CBM or portion is pseudo-locked
  717. * @d: RDT domain
  718. * @cbm: CBM to test
  719. *
  720. * @d represents a cache instance and @cbm a capacity bitmask that is
  721. * considered for it. Determine if @cbm overlaps with any existing
  722. * pseudo-locked region on @d.
  723. *
  724. * @cbm is unsigned long, even if only 32 bits are used, to make the
  725. * bitmap functions work correctly.
  726. *
  727. * Return: true if @cbm overlaps with pseudo-locked region on @d, false
  728. * otherwise.
  729. */
  730. bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm)
  731. {
  732. unsigned int cbm_len;
  733. unsigned long cbm_b;
  734. if (d->plr) {
  735. cbm_len = d->plr->r->cache.cbm_len;
  736. cbm_b = d->plr->cbm;
  737. if (bitmap_intersects(&cbm, &cbm_b, cbm_len))
  738. return true;
  739. }
  740. return false;
  741. }
  742. /**
  743. * rdtgroup_pseudo_locked_in_hierarchy - Pseudo-locked region in cache hierarchy
  744. * @d: RDT domain under test
  745. *
  746. * The setup of a pseudo-locked region affects all cache instances within
  747. * the hierarchy of the region. It is thus essential to know if any
  748. * pseudo-locked regions exist within a cache hierarchy to prevent any
  749. * attempts to create new pseudo-locked regions in the same hierarchy.
  750. *
  751. * Return: true if a pseudo-locked region exists in the hierarchy of @d or
  752. * if it is not possible to test due to memory allocation issue,
  753. * false otherwise.
  754. */
  755. bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d)
  756. {
  757. cpumask_var_t cpu_with_psl;
  758. struct rdt_resource *r;
  759. struct rdt_domain *d_i;
  760. bool ret = false;
  761. if (!zalloc_cpumask_var(&cpu_with_psl, GFP_KERNEL))
  762. return true;
  763. /*
  764. * First determine which cpus have pseudo-locked regions
  765. * associated with them.
  766. */
  767. for_each_alloc_enabled_rdt_resource(r) {
  768. list_for_each_entry(d_i, &r->domains, list) {
  769. if (d_i->plr)
  770. cpumask_or(cpu_with_psl, cpu_with_psl,
  771. &d_i->cpu_mask);
  772. }
  773. }
  774. /*
  775. * Next test if new pseudo-locked region would intersect with
  776. * existing region.
  777. */
  778. if (cpumask_intersects(&d->cpu_mask, cpu_with_psl))
  779. ret = true;
  780. free_cpumask_var(cpu_with_psl);
  781. return ret;
  782. }
  783. /**
  784. * measure_cycles_lat_fn - Measure cycle latency to read pseudo-locked memory
  785. * @_plr: pseudo-lock region to measure
  786. *
  787. * There is no deterministic way to test if a memory region is cached. One
  788. * way is to measure how long it takes to read the memory, the speed of
  789. * access is a good way to learn how close to the cpu the data was. Even
  790. * more, if the prefetcher is disabled and the memory is read at a stride
  791. * of half the cache line, then a cache miss will be easy to spot since the
  792. * read of the first half would be significantly slower than the read of
  793. * the second half.
  794. *
  795. * Return: 0. Waiter on waitqueue will be woken on completion.
  796. */
  797. static int measure_cycles_lat_fn(void *_plr)
  798. {
  799. struct pseudo_lock_region *plr = _plr;
  800. unsigned long i;
  801. u64 start, end;
  802. #ifdef CONFIG_KASAN
  803. /*
  804. * The registers used for local register variables are also used
  805. * when KASAN is active. When KASAN is active we use a regular
  806. * variable to ensure we always use a valid pointer to access memory.
  807. * The cost is that accessing this pointer, which could be in
  808. * cache, will be included in the measurement of memory read latency.
  809. */
  810. void *mem_r;
  811. #else
  812. #ifdef CONFIG_X86_64
  813. register void *mem_r asm("rbx");
  814. #else
  815. register void *mem_r asm("ebx");
  816. #endif /* CONFIG_X86_64 */
  817. #endif /* CONFIG_KASAN */
  818. local_irq_disable();
  819. /*
  820. * The wrmsr call may be reordered with the assignment below it.
  821. * Call wrmsr as directly as possible to avoid tracing clobbering
  822. * local register variable used for memory pointer.
  823. */
  824. __wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
  825. mem_r = plr->kmem;
  826. /*
  827. * Dummy execute of the time measurement to load the needed
  828. * instructions into the L1 instruction cache.
  829. */
  830. start = rdtsc_ordered();
  831. for (i = 0; i < plr->size; i += 32) {
  832. start = rdtsc_ordered();
  833. asm volatile("mov (%0,%1,1), %%eax\n\t"
  834. :
  835. : "r" (mem_r), "r" (i)
  836. : "%eax", "memory");
  837. end = rdtsc_ordered();
  838. trace_pseudo_lock_mem_latency((u32)(end - start));
  839. }
  840. wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
  841. local_irq_enable();
  842. plr->thread_done = 1;
  843. wake_up_interruptible(&plr->lock_thread_wq);
  844. return 0;
  845. }
  846. static int measure_cycles_perf_fn(void *_plr)
  847. {
  848. unsigned long long l3_hits = 0, l3_miss = 0;
  849. u64 l3_hit_bits = 0, l3_miss_bits = 0;
  850. struct pseudo_lock_region *plr = _plr;
  851. unsigned long long l2_hits, l2_miss;
  852. u64 l2_hit_bits, l2_miss_bits;
  853. unsigned long i;
  854. #ifdef CONFIG_KASAN
  855. /*
  856. * The registers used for local register variables are also used
  857. * when KASAN is active. When KASAN is active we use regular variables
  858. * at the cost of including cache access latency to these variables
  859. * in the measurements.
  860. */
  861. unsigned int line_size;
  862. unsigned int size;
  863. void *mem_r;
  864. #else
  865. register unsigned int line_size asm("esi");
  866. register unsigned int size asm("edi");
  867. #ifdef CONFIG_X86_64
  868. register void *mem_r asm("rbx");
  869. #else
  870. register void *mem_r asm("ebx");
  871. #endif /* CONFIG_X86_64 */
  872. #endif /* CONFIG_KASAN */
  873. /*
  874. * Non-architectural event for the Goldmont Microarchitecture
  875. * from Intel x86 Architecture Software Developer Manual (SDM):
  876. * MEM_LOAD_UOPS_RETIRED D1H (event number)
  877. * Umask values:
  878. * L1_HIT 01H
  879. * L2_HIT 02H
  880. * L1_MISS 08H
  881. * L2_MISS 10H
  882. *
  883. * On Broadwell Microarchitecture the MEM_LOAD_UOPS_RETIRED event
  884. * has two "no fix" errata associated with it: BDM35 and BDM100. On
  885. * this platform we use the following events instead:
  886. * L2_RQSTS 24H (Documented in https://download.01.org/perfmon/BDW/)
  887. * REFERENCES FFH
  888. * MISS 3FH
  889. * LONGEST_LAT_CACHE 2EH (Documented in SDM)
  890. * REFERENCE 4FH
  891. * MISS 41H
  892. */
  893. /*
  894. * Start by setting flags for IA32_PERFEVTSELx:
  895. * OS (Operating system mode) 0x2
  896. * INT (APIC interrupt enable) 0x10
  897. * EN (Enable counter) 0x40
  898. *
  899. * Then add the Umask value and event number to select performance
  900. * event.
  901. */
  902. switch (boot_cpu_data.x86_model) {
  903. case INTEL_FAM6_ATOM_GOLDMONT:
  904. case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
  905. l2_hit_bits = (0x52ULL << 16) | (0x2 << 8) | 0xd1;
  906. l2_miss_bits = (0x52ULL << 16) | (0x10 << 8) | 0xd1;
  907. break;
  908. case INTEL_FAM6_BROADWELL_X:
  909. /* On BDW the l2_hit_bits count references, not hits */
  910. l2_hit_bits = (0x52ULL << 16) | (0xff << 8) | 0x24;
  911. l2_miss_bits = (0x52ULL << 16) | (0x3f << 8) | 0x24;
  912. /* On BDW the l3_hit_bits count references, not hits */
  913. l3_hit_bits = (0x52ULL << 16) | (0x4f << 8) | 0x2e;
  914. l3_miss_bits = (0x52ULL << 16) | (0x41 << 8) | 0x2e;
  915. break;
  916. default:
  917. goto out;
  918. }
  919. local_irq_disable();
  920. /*
  921. * Call wrmsr direcly to avoid the local register variables from
  922. * being overwritten due to reordering of their assignment with
  923. * the wrmsr calls.
  924. */
  925. __wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
  926. /* Disable events and reset counters */
  927. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0, 0x0);
  928. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 1, 0x0);
  929. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_PERFCTR0, 0x0);
  930. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_PERFCTR0 + 1, 0x0);
  931. if (l3_hit_bits > 0) {
  932. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 2, 0x0);
  933. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 3, 0x0);
  934. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_PERFCTR0 + 2, 0x0);
  935. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_PERFCTR0 + 3, 0x0);
  936. }
  937. /* Set and enable the L2 counters */
  938. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0, l2_hit_bits);
  939. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 1, l2_miss_bits);
  940. if (l3_hit_bits > 0) {
  941. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 2,
  942. l3_hit_bits);
  943. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 3,
  944. l3_miss_bits);
  945. }
  946. mem_r = plr->kmem;
  947. size = plr->size;
  948. line_size = plr->line_size;
  949. for (i = 0; i < size; i += line_size) {
  950. asm volatile("mov (%0,%1,1), %%eax\n\t"
  951. :
  952. : "r" (mem_r), "r" (i)
  953. : "%eax", "memory");
  954. }
  955. /*
  956. * Call wrmsr directly (no tracing) to not influence
  957. * the cache access counters as they are disabled.
  958. */
  959. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0,
  960. l2_hit_bits & ~(0x40ULL << 16));
  961. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 1,
  962. l2_miss_bits & ~(0x40ULL << 16));
  963. if (l3_hit_bits > 0) {
  964. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 2,
  965. l3_hit_bits & ~(0x40ULL << 16));
  966. pseudo_wrmsrl_notrace(MSR_ARCH_PERFMON_EVENTSEL0 + 3,
  967. l3_miss_bits & ~(0x40ULL << 16));
  968. }
  969. l2_hits = native_read_pmc(0);
  970. l2_miss = native_read_pmc(1);
  971. if (l3_hit_bits > 0) {
  972. l3_hits = native_read_pmc(2);
  973. l3_miss = native_read_pmc(3);
  974. }
  975. wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
  976. local_irq_enable();
  977. /*
  978. * On BDW we count references and misses, need to adjust. Sometimes
  979. * the "hits" counter is a bit more than the references, for
  980. * example, x references but x + 1 hits. To not report invalid
  981. * hit values in this case we treat that as misses eaqual to
  982. * references.
  983. */
  984. if (boot_cpu_data.x86_model == INTEL_FAM6_BROADWELL_X)
  985. l2_hits -= (l2_miss > l2_hits ? l2_hits : l2_miss);
  986. trace_pseudo_lock_l2(l2_hits, l2_miss);
  987. if (l3_hit_bits > 0) {
  988. if (boot_cpu_data.x86_model == INTEL_FAM6_BROADWELL_X)
  989. l3_hits -= (l3_miss > l3_hits ? l3_hits : l3_miss);
  990. trace_pseudo_lock_l3(l3_hits, l3_miss);
  991. }
  992. out:
  993. plr->thread_done = 1;
  994. wake_up_interruptible(&plr->lock_thread_wq);
  995. return 0;
  996. }
  997. /**
  998. * pseudo_lock_measure_cycles - Trigger latency measure to pseudo-locked region
  999. *
  1000. * The measurement of latency to access a pseudo-locked region should be
  1001. * done from a cpu that is associated with that pseudo-locked region.
  1002. * Determine which cpu is associated with this region and start a thread on
  1003. * that cpu to perform the measurement, wait for that thread to complete.
  1004. *
  1005. * Return: 0 on success, <0 on failure
  1006. */
  1007. static int pseudo_lock_measure_cycles(struct rdtgroup *rdtgrp, int sel)
  1008. {
  1009. struct pseudo_lock_region *plr = rdtgrp->plr;
  1010. struct task_struct *thread;
  1011. unsigned int cpu;
  1012. int ret = -1;
  1013. cpus_read_lock();
  1014. mutex_lock(&rdtgroup_mutex);
  1015. if (rdtgrp->flags & RDT_DELETED) {
  1016. ret = -ENODEV;
  1017. goto out;
  1018. }
  1019. if (!plr->d) {
  1020. ret = -ENODEV;
  1021. goto out;
  1022. }
  1023. plr->thread_done = 0;
  1024. cpu = cpumask_first(&plr->d->cpu_mask);
  1025. if (!cpu_online(cpu)) {
  1026. ret = -ENODEV;
  1027. goto out;
  1028. }
  1029. if (sel == 1)
  1030. thread = kthread_create_on_node(measure_cycles_lat_fn, plr,
  1031. cpu_to_node(cpu),
  1032. "pseudo_lock_measure/%u",
  1033. cpu);
  1034. else if (sel == 2)
  1035. thread = kthread_create_on_node(measure_cycles_perf_fn, plr,
  1036. cpu_to_node(cpu),
  1037. "pseudo_lock_measure/%u",
  1038. cpu);
  1039. else
  1040. goto out;
  1041. if (IS_ERR(thread)) {
  1042. ret = PTR_ERR(thread);
  1043. goto out;
  1044. }
  1045. kthread_bind(thread, cpu);
  1046. wake_up_process(thread);
  1047. ret = wait_event_interruptible(plr->lock_thread_wq,
  1048. plr->thread_done == 1);
  1049. if (ret < 0)
  1050. goto out;
  1051. ret = 0;
  1052. out:
  1053. mutex_unlock(&rdtgroup_mutex);
  1054. cpus_read_unlock();
  1055. return ret;
  1056. }
  1057. static ssize_t pseudo_lock_measure_trigger(struct file *file,
  1058. const char __user *user_buf,
  1059. size_t count, loff_t *ppos)
  1060. {
  1061. struct rdtgroup *rdtgrp = file->private_data;
  1062. size_t buf_size;
  1063. char buf[32];
  1064. int ret;
  1065. int sel;
  1066. buf_size = min(count, (sizeof(buf) - 1));
  1067. if (copy_from_user(buf, user_buf, buf_size))
  1068. return -EFAULT;
  1069. buf[buf_size] = '\0';
  1070. ret = kstrtoint(buf, 10, &sel);
  1071. if (ret == 0) {
  1072. if (sel != 1)
  1073. return -EINVAL;
  1074. ret = debugfs_file_get(file->f_path.dentry);
  1075. if (ret)
  1076. return ret;
  1077. ret = pseudo_lock_measure_cycles(rdtgrp, sel);
  1078. if (ret == 0)
  1079. ret = count;
  1080. debugfs_file_put(file->f_path.dentry);
  1081. }
  1082. return ret;
  1083. }
  1084. static const struct file_operations pseudo_measure_fops = {
  1085. .write = pseudo_lock_measure_trigger,
  1086. .open = simple_open,
  1087. .llseek = default_llseek,
  1088. };
  1089. /**
  1090. * rdtgroup_pseudo_lock_create - Create a pseudo-locked region
  1091. * @rdtgrp: resource group to which pseudo-lock region belongs
  1092. *
  1093. * Called when a resource group in the pseudo-locksetup mode receives a
  1094. * valid schemata that should be pseudo-locked. Since the resource group is
  1095. * in pseudo-locksetup mode the &struct pseudo_lock_region has already been
  1096. * allocated and initialized with the essential information. If a failure
  1097. * occurs the resource group remains in the pseudo-locksetup mode with the
  1098. * &struct pseudo_lock_region associated with it, but cleared from all
  1099. * information and ready for the user to re-attempt pseudo-locking by
  1100. * writing the schemata again.
  1101. *
  1102. * Return: 0 if the pseudo-locked region was successfully pseudo-locked, <0
  1103. * on failure. Descriptive error will be written to last_cmd_status buffer.
  1104. */
  1105. int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
  1106. {
  1107. struct pseudo_lock_region *plr = rdtgrp->plr;
  1108. struct task_struct *thread;
  1109. unsigned int new_minor;
  1110. struct device *dev;
  1111. int ret;
  1112. ret = pseudo_lock_region_alloc(plr);
  1113. if (ret < 0)
  1114. return ret;
  1115. ret = pseudo_lock_cstates_constrain(plr);
  1116. if (ret < 0) {
  1117. ret = -EINVAL;
  1118. goto out_region;
  1119. }
  1120. plr->thread_done = 0;
  1121. thread = kthread_create_on_node(pseudo_lock_fn, rdtgrp,
  1122. cpu_to_node(plr->cpu),
  1123. "pseudo_lock/%u", plr->cpu);
  1124. if (IS_ERR(thread)) {
  1125. ret = PTR_ERR(thread);
  1126. rdt_last_cmd_printf("locking thread returned error %d\n", ret);
  1127. goto out_cstates;
  1128. }
  1129. kthread_bind(thread, plr->cpu);
  1130. wake_up_process(thread);
  1131. ret = wait_event_interruptible(plr->lock_thread_wq,
  1132. plr->thread_done == 1);
  1133. if (ret < 0) {
  1134. /*
  1135. * If the thread does not get on the CPU for whatever
  1136. * reason and the process which sets up the region is
  1137. * interrupted then this will leave the thread in runnable
  1138. * state and once it gets on the CPU it will derefence
  1139. * the cleared, but not freed, plr struct resulting in an
  1140. * empty pseudo-locking loop.
  1141. */
  1142. rdt_last_cmd_puts("locking thread interrupted\n");
  1143. goto out_cstates;
  1144. }
  1145. ret = pseudo_lock_minor_get(&new_minor);
  1146. if (ret < 0) {
  1147. rdt_last_cmd_puts("unable to obtain a new minor number\n");
  1148. goto out_cstates;
  1149. }
  1150. /*
  1151. * Unlock access but do not release the reference. The
  1152. * pseudo-locked region will still be here on return.
  1153. *
  1154. * The mutex has to be released temporarily to avoid a potential
  1155. * deadlock with the mm->mmap_sem semaphore which is obtained in
  1156. * the device_create() and debugfs_create_dir() callpath below
  1157. * as well as before the mmap() callback is called.
  1158. */
  1159. mutex_unlock(&rdtgroup_mutex);
  1160. if (!IS_ERR_OR_NULL(debugfs_resctrl)) {
  1161. plr->debugfs_dir = debugfs_create_dir(rdtgrp->kn->name,
  1162. debugfs_resctrl);
  1163. if (!IS_ERR_OR_NULL(plr->debugfs_dir))
  1164. debugfs_create_file("pseudo_lock_measure", 0200,
  1165. plr->debugfs_dir, rdtgrp,
  1166. &pseudo_measure_fops);
  1167. }
  1168. dev = device_create(pseudo_lock_class, NULL,
  1169. MKDEV(pseudo_lock_major, new_minor),
  1170. rdtgrp, "%s", rdtgrp->kn->name);
  1171. mutex_lock(&rdtgroup_mutex);
  1172. if (IS_ERR(dev)) {
  1173. ret = PTR_ERR(dev);
  1174. rdt_last_cmd_printf("failed to create character device: %d\n",
  1175. ret);
  1176. goto out_debugfs;
  1177. }
  1178. /* We released the mutex - check if group was removed while we did so */
  1179. if (rdtgrp->flags & RDT_DELETED) {
  1180. ret = -ENODEV;
  1181. goto out_device;
  1182. }
  1183. plr->minor = new_minor;
  1184. rdtgrp->mode = RDT_MODE_PSEUDO_LOCKED;
  1185. closid_free(rdtgrp->closid);
  1186. rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0444);
  1187. rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0444);
  1188. ret = 0;
  1189. goto out;
  1190. out_device:
  1191. device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, new_minor));
  1192. out_debugfs:
  1193. debugfs_remove_recursive(plr->debugfs_dir);
  1194. pseudo_lock_minor_release(new_minor);
  1195. out_cstates:
  1196. pseudo_lock_cstates_relax(plr);
  1197. out_region:
  1198. pseudo_lock_region_clear(plr);
  1199. out:
  1200. return ret;
  1201. }
  1202. /**
  1203. * rdtgroup_pseudo_lock_remove - Remove a pseudo-locked region
  1204. * @rdtgrp: resource group to which the pseudo-locked region belongs
  1205. *
  1206. * The removal of a pseudo-locked region can be initiated when the resource
  1207. * group is removed from user space via a "rmdir" from userspace or the
  1208. * unmount of the resctrl filesystem. On removal the resource group does
  1209. * not go back to pseudo-locksetup mode before it is removed, instead it is
  1210. * removed directly. There is thus assymmetry with the creation where the
  1211. * &struct pseudo_lock_region is removed here while it was not created in
  1212. * rdtgroup_pseudo_lock_create().
  1213. *
  1214. * Return: void
  1215. */
  1216. void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp)
  1217. {
  1218. struct pseudo_lock_region *plr = rdtgrp->plr;
  1219. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1220. /*
  1221. * Default group cannot be a pseudo-locked region so we can
  1222. * free closid here.
  1223. */
  1224. closid_free(rdtgrp->closid);
  1225. goto free;
  1226. }
  1227. pseudo_lock_cstates_relax(plr);
  1228. debugfs_remove_recursive(rdtgrp->plr->debugfs_dir);
  1229. device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, plr->minor));
  1230. pseudo_lock_minor_release(plr->minor);
  1231. free:
  1232. pseudo_lock_free(rdtgrp);
  1233. }
  1234. static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
  1235. {
  1236. struct rdtgroup *rdtgrp;
  1237. mutex_lock(&rdtgroup_mutex);
  1238. rdtgrp = region_find_by_minor(iminor(inode));
  1239. if (!rdtgrp) {
  1240. mutex_unlock(&rdtgroup_mutex);
  1241. return -ENODEV;
  1242. }
  1243. filp->private_data = rdtgrp;
  1244. atomic_inc(&rdtgrp->waitcount);
  1245. /* Perform a non-seekable open - llseek is not supported */
  1246. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1247. mutex_unlock(&rdtgroup_mutex);
  1248. return 0;
  1249. }
  1250. static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
  1251. {
  1252. struct rdtgroup *rdtgrp;
  1253. mutex_lock(&rdtgroup_mutex);
  1254. rdtgrp = filp->private_data;
  1255. WARN_ON(!rdtgrp);
  1256. if (!rdtgrp) {
  1257. mutex_unlock(&rdtgroup_mutex);
  1258. return -ENODEV;
  1259. }
  1260. filp->private_data = NULL;
  1261. atomic_dec(&rdtgrp->waitcount);
  1262. mutex_unlock(&rdtgroup_mutex);
  1263. return 0;
  1264. }
  1265. static int pseudo_lock_dev_mremap(struct vm_area_struct *area)
  1266. {
  1267. /* Not supported */
  1268. return -EINVAL;
  1269. }
  1270. static const struct vm_operations_struct pseudo_mmap_ops = {
  1271. .mremap = pseudo_lock_dev_mremap,
  1272. };
  1273. static int pseudo_lock_dev_mmap(struct file *filp, struct vm_area_struct *vma)
  1274. {
  1275. unsigned long vsize = vma->vm_end - vma->vm_start;
  1276. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1277. struct pseudo_lock_region *plr;
  1278. struct rdtgroup *rdtgrp;
  1279. unsigned long physical;
  1280. unsigned long psize;
  1281. mutex_lock(&rdtgroup_mutex);
  1282. rdtgrp = filp->private_data;
  1283. WARN_ON(!rdtgrp);
  1284. if (!rdtgrp) {
  1285. mutex_unlock(&rdtgroup_mutex);
  1286. return -ENODEV;
  1287. }
  1288. plr = rdtgrp->plr;
  1289. if (!plr->d) {
  1290. mutex_unlock(&rdtgroup_mutex);
  1291. return -ENODEV;
  1292. }
  1293. /*
  1294. * Task is required to run with affinity to the cpus associated
  1295. * with the pseudo-locked region. If this is not the case the task
  1296. * may be scheduled elsewhere and invalidate entries in the
  1297. * pseudo-locked region.
  1298. */
  1299. if (!cpumask_subset(&current->cpus_allowed, &plr->d->cpu_mask)) {
  1300. mutex_unlock(&rdtgroup_mutex);
  1301. return -EINVAL;
  1302. }
  1303. physical = __pa(plr->kmem) >> PAGE_SHIFT;
  1304. psize = plr->size - off;
  1305. if (off > plr->size) {
  1306. mutex_unlock(&rdtgroup_mutex);
  1307. return -ENOSPC;
  1308. }
  1309. /*
  1310. * Ensure changes are carried directly to the memory being mapped,
  1311. * do not allow copy-on-write mapping.
  1312. */
  1313. if (!(vma->vm_flags & VM_SHARED)) {
  1314. mutex_unlock(&rdtgroup_mutex);
  1315. return -EINVAL;
  1316. }
  1317. if (vsize > psize) {
  1318. mutex_unlock(&rdtgroup_mutex);
  1319. return -ENOSPC;
  1320. }
  1321. memset(plr->kmem + off, 0, vsize);
  1322. if (remap_pfn_range(vma, vma->vm_start, physical + vma->vm_pgoff,
  1323. vsize, vma->vm_page_prot)) {
  1324. mutex_unlock(&rdtgroup_mutex);
  1325. return -EAGAIN;
  1326. }
  1327. vma->vm_ops = &pseudo_mmap_ops;
  1328. mutex_unlock(&rdtgroup_mutex);
  1329. return 0;
  1330. }
  1331. static const struct file_operations pseudo_lock_dev_fops = {
  1332. .owner = THIS_MODULE,
  1333. .llseek = no_llseek,
  1334. .read = NULL,
  1335. .write = NULL,
  1336. .open = pseudo_lock_dev_open,
  1337. .release = pseudo_lock_dev_release,
  1338. .mmap = pseudo_lock_dev_mmap,
  1339. };
  1340. static char *pseudo_lock_devnode(struct device *dev, umode_t *mode)
  1341. {
  1342. struct rdtgroup *rdtgrp;
  1343. rdtgrp = dev_get_drvdata(dev);
  1344. if (mode)
  1345. *mode = 0600;
  1346. return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdtgrp->kn->name);
  1347. }
  1348. int rdt_pseudo_lock_init(void)
  1349. {
  1350. int ret;
  1351. ret = register_chrdev(0, "pseudo_lock", &pseudo_lock_dev_fops);
  1352. if (ret < 0)
  1353. return ret;
  1354. pseudo_lock_major = ret;
  1355. pseudo_lock_class = class_create(THIS_MODULE, "pseudo_lock");
  1356. if (IS_ERR(pseudo_lock_class)) {
  1357. ret = PTR_ERR(pseudo_lock_class);
  1358. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  1359. return ret;
  1360. }
  1361. pseudo_lock_class->devnode = pseudo_lock_devnode;
  1362. return 0;
  1363. }
  1364. void rdt_pseudo_lock_release(void)
  1365. {
  1366. class_destroy(pseudo_lock_class);
  1367. pseudo_lock_class = NULL;
  1368. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  1369. pseudo_lock_major = 0;
  1370. }