intel_rdt_pseudo_lock.c 43 KB

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