intel_rdt_pseudo_lock.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. if (!plr->d) {
  1066. ret = -ENODEV;
  1067. goto out;
  1068. }
  1069. plr->thread_done = 0;
  1070. cpu = cpumask_first(&plr->d->cpu_mask);
  1071. if (!cpu_online(cpu)) {
  1072. ret = -ENODEV;
  1073. goto out;
  1074. }
  1075. plr->cpu = cpu;
  1076. if (sel == 1)
  1077. thread = kthread_create_on_node(measure_cycles_lat_fn, plr,
  1078. cpu_to_node(cpu),
  1079. "pseudo_lock_measure/%u",
  1080. cpu);
  1081. else if (sel == 2)
  1082. thread = kthread_create_on_node(measure_l2_residency, plr,
  1083. cpu_to_node(cpu),
  1084. "pseudo_lock_measure/%u",
  1085. cpu);
  1086. else if (sel == 3)
  1087. thread = kthread_create_on_node(measure_l3_residency, plr,
  1088. cpu_to_node(cpu),
  1089. "pseudo_lock_measure/%u",
  1090. cpu);
  1091. else
  1092. goto out;
  1093. if (IS_ERR(thread)) {
  1094. ret = PTR_ERR(thread);
  1095. goto out;
  1096. }
  1097. kthread_bind(thread, cpu);
  1098. wake_up_process(thread);
  1099. ret = wait_event_interruptible(plr->lock_thread_wq,
  1100. plr->thread_done == 1);
  1101. if (ret < 0)
  1102. goto out;
  1103. ret = 0;
  1104. out:
  1105. mutex_unlock(&rdtgroup_mutex);
  1106. cpus_read_unlock();
  1107. return ret;
  1108. }
  1109. static ssize_t pseudo_lock_measure_trigger(struct file *file,
  1110. const char __user *user_buf,
  1111. size_t count, loff_t *ppos)
  1112. {
  1113. struct rdtgroup *rdtgrp = file->private_data;
  1114. size_t buf_size;
  1115. char buf[32];
  1116. int ret;
  1117. int sel;
  1118. buf_size = min(count, (sizeof(buf) - 1));
  1119. if (copy_from_user(buf, user_buf, buf_size))
  1120. return -EFAULT;
  1121. buf[buf_size] = '\0';
  1122. ret = kstrtoint(buf, 10, &sel);
  1123. if (ret == 0) {
  1124. if (sel != 1 && sel != 2 && sel != 3)
  1125. return -EINVAL;
  1126. ret = debugfs_file_get(file->f_path.dentry);
  1127. if (ret)
  1128. return ret;
  1129. ret = pseudo_lock_measure_cycles(rdtgrp, sel);
  1130. if (ret == 0)
  1131. ret = count;
  1132. debugfs_file_put(file->f_path.dentry);
  1133. }
  1134. return ret;
  1135. }
  1136. static const struct file_operations pseudo_measure_fops = {
  1137. .write = pseudo_lock_measure_trigger,
  1138. .open = simple_open,
  1139. .llseek = default_llseek,
  1140. };
  1141. /**
  1142. * rdtgroup_pseudo_lock_create - Create a pseudo-locked region
  1143. * @rdtgrp: resource group to which pseudo-lock region belongs
  1144. *
  1145. * Called when a resource group in the pseudo-locksetup mode receives a
  1146. * valid schemata that should be pseudo-locked. Since the resource group is
  1147. * in pseudo-locksetup mode the &struct pseudo_lock_region has already been
  1148. * allocated and initialized with the essential information. If a failure
  1149. * occurs the resource group remains in the pseudo-locksetup mode with the
  1150. * &struct pseudo_lock_region associated with it, but cleared from all
  1151. * information and ready for the user to re-attempt pseudo-locking by
  1152. * writing the schemata again.
  1153. *
  1154. * Return: 0 if the pseudo-locked region was successfully pseudo-locked, <0
  1155. * on failure. Descriptive error will be written to last_cmd_status buffer.
  1156. */
  1157. int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
  1158. {
  1159. struct pseudo_lock_region *plr = rdtgrp->plr;
  1160. struct task_struct *thread;
  1161. unsigned int new_minor;
  1162. struct device *dev;
  1163. int ret;
  1164. ret = pseudo_lock_region_alloc(plr);
  1165. if (ret < 0)
  1166. return ret;
  1167. ret = pseudo_lock_cstates_constrain(plr);
  1168. if (ret < 0) {
  1169. ret = -EINVAL;
  1170. goto out_region;
  1171. }
  1172. plr->thread_done = 0;
  1173. thread = kthread_create_on_node(pseudo_lock_fn, rdtgrp,
  1174. cpu_to_node(plr->cpu),
  1175. "pseudo_lock/%u", plr->cpu);
  1176. if (IS_ERR(thread)) {
  1177. ret = PTR_ERR(thread);
  1178. rdt_last_cmd_printf("locking thread returned error %d\n", ret);
  1179. goto out_cstates;
  1180. }
  1181. kthread_bind(thread, plr->cpu);
  1182. wake_up_process(thread);
  1183. ret = wait_event_interruptible(plr->lock_thread_wq,
  1184. plr->thread_done == 1);
  1185. if (ret < 0) {
  1186. /*
  1187. * If the thread does not get on the CPU for whatever
  1188. * reason and the process which sets up the region is
  1189. * interrupted then this will leave the thread in runnable
  1190. * state and once it gets on the CPU it will derefence
  1191. * the cleared, but not freed, plr struct resulting in an
  1192. * empty pseudo-locking loop.
  1193. */
  1194. rdt_last_cmd_puts("locking thread interrupted\n");
  1195. goto out_cstates;
  1196. }
  1197. ret = pseudo_lock_minor_get(&new_minor);
  1198. if (ret < 0) {
  1199. rdt_last_cmd_puts("unable to obtain a new minor number\n");
  1200. goto out_cstates;
  1201. }
  1202. /*
  1203. * Unlock access but do not release the reference. The
  1204. * pseudo-locked region will still be here on return.
  1205. *
  1206. * The mutex has to be released temporarily to avoid a potential
  1207. * deadlock with the mm->mmap_sem semaphore which is obtained in
  1208. * the device_create() and debugfs_create_dir() callpath below
  1209. * as well as before the mmap() callback is called.
  1210. */
  1211. mutex_unlock(&rdtgroup_mutex);
  1212. if (!IS_ERR_OR_NULL(debugfs_resctrl)) {
  1213. plr->debugfs_dir = debugfs_create_dir(rdtgrp->kn->name,
  1214. debugfs_resctrl);
  1215. if (!IS_ERR_OR_NULL(plr->debugfs_dir))
  1216. debugfs_create_file("pseudo_lock_measure", 0200,
  1217. plr->debugfs_dir, rdtgrp,
  1218. &pseudo_measure_fops);
  1219. }
  1220. dev = device_create(pseudo_lock_class, NULL,
  1221. MKDEV(pseudo_lock_major, new_minor),
  1222. rdtgrp, "%s", rdtgrp->kn->name);
  1223. mutex_lock(&rdtgroup_mutex);
  1224. if (IS_ERR(dev)) {
  1225. ret = PTR_ERR(dev);
  1226. rdt_last_cmd_printf("failed to create character device: %d\n",
  1227. ret);
  1228. goto out_debugfs;
  1229. }
  1230. /* We released the mutex - check if group was removed while we did so */
  1231. if (rdtgrp->flags & RDT_DELETED) {
  1232. ret = -ENODEV;
  1233. goto out_device;
  1234. }
  1235. plr->minor = new_minor;
  1236. rdtgrp->mode = RDT_MODE_PSEUDO_LOCKED;
  1237. closid_free(rdtgrp->closid);
  1238. rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0444);
  1239. rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0444);
  1240. ret = 0;
  1241. goto out;
  1242. out_device:
  1243. device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, new_minor));
  1244. out_debugfs:
  1245. debugfs_remove_recursive(plr->debugfs_dir);
  1246. pseudo_lock_minor_release(new_minor);
  1247. out_cstates:
  1248. pseudo_lock_cstates_relax(plr);
  1249. out_region:
  1250. pseudo_lock_region_clear(plr);
  1251. out:
  1252. return ret;
  1253. }
  1254. /**
  1255. * rdtgroup_pseudo_lock_remove - Remove a pseudo-locked region
  1256. * @rdtgrp: resource group to which the pseudo-locked region belongs
  1257. *
  1258. * The removal of a pseudo-locked region can be initiated when the resource
  1259. * group is removed from user space via a "rmdir" from userspace or the
  1260. * unmount of the resctrl filesystem. On removal the resource group does
  1261. * not go back to pseudo-locksetup mode before it is removed, instead it is
  1262. * removed directly. There is thus assymmetry with the creation where the
  1263. * &struct pseudo_lock_region is removed here while it was not created in
  1264. * rdtgroup_pseudo_lock_create().
  1265. *
  1266. * Return: void
  1267. */
  1268. void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp)
  1269. {
  1270. struct pseudo_lock_region *plr = rdtgrp->plr;
  1271. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1272. /*
  1273. * Default group cannot be a pseudo-locked region so we can
  1274. * free closid here.
  1275. */
  1276. closid_free(rdtgrp->closid);
  1277. goto free;
  1278. }
  1279. pseudo_lock_cstates_relax(plr);
  1280. debugfs_remove_recursive(rdtgrp->plr->debugfs_dir);
  1281. device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, plr->minor));
  1282. pseudo_lock_minor_release(plr->minor);
  1283. free:
  1284. pseudo_lock_free(rdtgrp);
  1285. }
  1286. static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
  1287. {
  1288. struct rdtgroup *rdtgrp;
  1289. mutex_lock(&rdtgroup_mutex);
  1290. rdtgrp = region_find_by_minor(iminor(inode));
  1291. if (!rdtgrp) {
  1292. mutex_unlock(&rdtgroup_mutex);
  1293. return -ENODEV;
  1294. }
  1295. filp->private_data = rdtgrp;
  1296. atomic_inc(&rdtgrp->waitcount);
  1297. /* Perform a non-seekable open - llseek is not supported */
  1298. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1299. mutex_unlock(&rdtgroup_mutex);
  1300. return 0;
  1301. }
  1302. static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
  1303. {
  1304. struct rdtgroup *rdtgrp;
  1305. mutex_lock(&rdtgroup_mutex);
  1306. rdtgrp = filp->private_data;
  1307. WARN_ON(!rdtgrp);
  1308. if (!rdtgrp) {
  1309. mutex_unlock(&rdtgroup_mutex);
  1310. return -ENODEV;
  1311. }
  1312. filp->private_data = NULL;
  1313. atomic_dec(&rdtgrp->waitcount);
  1314. mutex_unlock(&rdtgroup_mutex);
  1315. return 0;
  1316. }
  1317. static int pseudo_lock_dev_mremap(struct vm_area_struct *area)
  1318. {
  1319. /* Not supported */
  1320. return -EINVAL;
  1321. }
  1322. static const struct vm_operations_struct pseudo_mmap_ops = {
  1323. .mremap = pseudo_lock_dev_mremap,
  1324. };
  1325. static int pseudo_lock_dev_mmap(struct file *filp, struct vm_area_struct *vma)
  1326. {
  1327. unsigned long vsize = vma->vm_end - vma->vm_start;
  1328. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1329. struct pseudo_lock_region *plr;
  1330. struct rdtgroup *rdtgrp;
  1331. unsigned long physical;
  1332. unsigned long psize;
  1333. mutex_lock(&rdtgroup_mutex);
  1334. rdtgrp = filp->private_data;
  1335. WARN_ON(!rdtgrp);
  1336. if (!rdtgrp) {
  1337. mutex_unlock(&rdtgroup_mutex);
  1338. return -ENODEV;
  1339. }
  1340. plr = rdtgrp->plr;
  1341. if (!plr->d) {
  1342. mutex_unlock(&rdtgroup_mutex);
  1343. return -ENODEV;
  1344. }
  1345. /*
  1346. * Task is required to run with affinity to the cpus associated
  1347. * with the pseudo-locked region. If this is not the case the task
  1348. * may be scheduled elsewhere and invalidate entries in the
  1349. * pseudo-locked region.
  1350. */
  1351. if (!cpumask_subset(&current->cpus_allowed, &plr->d->cpu_mask)) {
  1352. mutex_unlock(&rdtgroup_mutex);
  1353. return -EINVAL;
  1354. }
  1355. physical = __pa(plr->kmem) >> PAGE_SHIFT;
  1356. psize = plr->size - off;
  1357. if (off > plr->size) {
  1358. mutex_unlock(&rdtgroup_mutex);
  1359. return -ENOSPC;
  1360. }
  1361. /*
  1362. * Ensure changes are carried directly to the memory being mapped,
  1363. * do not allow copy-on-write mapping.
  1364. */
  1365. if (!(vma->vm_flags & VM_SHARED)) {
  1366. mutex_unlock(&rdtgroup_mutex);
  1367. return -EINVAL;
  1368. }
  1369. if (vsize > psize) {
  1370. mutex_unlock(&rdtgroup_mutex);
  1371. return -ENOSPC;
  1372. }
  1373. memset(plr->kmem + off, 0, vsize);
  1374. if (remap_pfn_range(vma, vma->vm_start, physical + vma->vm_pgoff,
  1375. vsize, vma->vm_page_prot)) {
  1376. mutex_unlock(&rdtgroup_mutex);
  1377. return -EAGAIN;
  1378. }
  1379. vma->vm_ops = &pseudo_mmap_ops;
  1380. mutex_unlock(&rdtgroup_mutex);
  1381. return 0;
  1382. }
  1383. static const struct file_operations pseudo_lock_dev_fops = {
  1384. .owner = THIS_MODULE,
  1385. .llseek = no_llseek,
  1386. .read = NULL,
  1387. .write = NULL,
  1388. .open = pseudo_lock_dev_open,
  1389. .release = pseudo_lock_dev_release,
  1390. .mmap = pseudo_lock_dev_mmap,
  1391. };
  1392. static char *pseudo_lock_devnode(struct device *dev, umode_t *mode)
  1393. {
  1394. struct rdtgroup *rdtgrp;
  1395. rdtgrp = dev_get_drvdata(dev);
  1396. if (mode)
  1397. *mode = 0600;
  1398. return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdtgrp->kn->name);
  1399. }
  1400. int rdt_pseudo_lock_init(void)
  1401. {
  1402. int ret;
  1403. ret = register_chrdev(0, "pseudo_lock", &pseudo_lock_dev_fops);
  1404. if (ret < 0)
  1405. return ret;
  1406. pseudo_lock_major = ret;
  1407. pseudo_lock_class = class_create(THIS_MODULE, "pseudo_lock");
  1408. if (IS_ERR(pseudo_lock_class)) {
  1409. ret = PTR_ERR(pseudo_lock_class);
  1410. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  1411. return ret;
  1412. }
  1413. pseudo_lock_class->devnode = pseudo_lock_devnode;
  1414. return 0;
  1415. }
  1416. void rdt_pseudo_lock_release(void)
  1417. {
  1418. class_destroy(pseudo_lock_class);
  1419. pseudo_lock_class = NULL;
  1420. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  1421. pseudo_lock_major = 0;
  1422. }