pseudo_lock.c 44 KB

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