pseudo_lock.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Resource Director Technology (RDT)
  4. *
  5. * Pseudo-locking support built on top of Cache Allocation Technology (CAT)
  6. *
  7. * Copyright (C) 2018 Intel Corporation
  8. *
  9. * Author: Reinette Chatre <reinette.chatre@intel.com>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/cacheinfo.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/kthread.h>
  17. #include <linux/mman.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/resctrl.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include "internal.h"
  23. /*
  24. * Major number assigned to and shared by all devices exposing
  25. * pseudo-locked regions.
  26. */
  27. static unsigned int pseudo_lock_major;
  28. static unsigned long pseudo_lock_minor_avail = GENMASK(MINORBITS, 0);
  29. static char *pseudo_lock_devnode(const struct device *dev, umode_t *mode)
  30. {
  31. const struct rdtgroup *rdtgrp;
  32. rdtgrp = dev_get_drvdata(dev);
  33. if (mode)
  34. *mode = 0600;
  35. guard(mutex)(&rdtgroup_mutex);
  36. return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdt_kn_name(rdtgrp->kn));
  37. }
  38. static const struct class pseudo_lock_class = {
  39. .name = "pseudo_lock",
  40. .devnode = pseudo_lock_devnode,
  41. };
  42. /**
  43. * pseudo_lock_minor_get - Obtain available minor number
  44. * @minor: Pointer to where new minor number will be stored
  45. *
  46. * A bitmask is used to track available minor numbers. Here the next free
  47. * minor number is marked as unavailable and returned.
  48. *
  49. * Return: 0 on success, <0 on failure.
  50. */
  51. static int pseudo_lock_minor_get(unsigned int *minor)
  52. {
  53. unsigned long first_bit;
  54. first_bit = find_first_bit(&pseudo_lock_minor_avail, MINORBITS);
  55. if (first_bit == MINORBITS)
  56. return -ENOSPC;
  57. __clear_bit(first_bit, &pseudo_lock_minor_avail);
  58. *minor = first_bit;
  59. return 0;
  60. }
  61. /**
  62. * pseudo_lock_minor_release - Return minor number to available
  63. * @minor: The minor number made available
  64. */
  65. static void pseudo_lock_minor_release(unsigned int minor)
  66. {
  67. __set_bit(minor, &pseudo_lock_minor_avail);
  68. }
  69. /**
  70. * region_find_by_minor - Locate a pseudo-lock region by inode minor number
  71. * @minor: The minor number of the device representing pseudo-locked region
  72. *
  73. * When the character device is accessed we need to determine which
  74. * pseudo-locked region it belongs to. This is done by matching the minor
  75. * number of the device to the pseudo-locked region it belongs.
  76. *
  77. * Minor numbers are assigned at the time a pseudo-locked region is associated
  78. * with a cache instance.
  79. *
  80. * Return: On success return pointer to resource group owning the pseudo-locked
  81. * region, NULL on failure.
  82. */
  83. static struct rdtgroup *region_find_by_minor(unsigned int minor)
  84. {
  85. struct rdtgroup *rdtgrp, *rdtgrp_match = NULL;
  86. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  87. if (rdtgrp->plr && rdtgrp->plr->minor == minor) {
  88. rdtgrp_match = rdtgrp;
  89. break;
  90. }
  91. }
  92. return rdtgrp_match;
  93. }
  94. /**
  95. * struct pseudo_lock_pm_req - A power management QoS request list entry
  96. * @list: Entry within the @pm_reqs list for a pseudo-locked region
  97. * @req: PM QoS request
  98. */
  99. struct pseudo_lock_pm_req {
  100. struct list_head list;
  101. struct dev_pm_qos_request req;
  102. };
  103. static void pseudo_lock_cstates_relax(struct pseudo_lock_region *plr)
  104. {
  105. struct pseudo_lock_pm_req *pm_req, *next;
  106. list_for_each_entry_safe(pm_req, next, &plr->pm_reqs, list) {
  107. dev_pm_qos_remove_request(&pm_req->req);
  108. list_del(&pm_req->list);
  109. kfree(pm_req);
  110. }
  111. }
  112. /**
  113. * pseudo_lock_cstates_constrain - Restrict cores from entering C6
  114. * @plr: Pseudo-locked region
  115. *
  116. * To prevent the cache from being affected by power management entering
  117. * C6 has to be avoided. This is accomplished by requesting a latency
  118. * requirement lower than lowest C6 exit latency of all supported
  119. * platforms as found in the cpuidle state tables in the intel_idle driver.
  120. * At this time it is possible to do so with a single latency requirement
  121. * for all supported platforms.
  122. *
  123. * Since Goldmont is supported, which is affected by X86_BUG_MONITOR,
  124. * the ACPI latencies need to be considered while keeping in mind that C2
  125. * may be set to map to deeper sleep states. In this case the latency
  126. * requirement needs to prevent entering C2 also.
  127. *
  128. * Return: 0 on success, <0 on failure
  129. */
  130. static int pseudo_lock_cstates_constrain(struct pseudo_lock_region *plr)
  131. {
  132. struct pseudo_lock_pm_req *pm_req;
  133. int cpu;
  134. int ret;
  135. for_each_cpu(cpu, &plr->d->hdr.cpu_mask) {
  136. pm_req = kzalloc_obj(*pm_req);
  137. if (!pm_req) {
  138. rdt_last_cmd_puts("Failure to allocate memory for PM QoS\n");
  139. ret = -ENOMEM;
  140. goto out_err;
  141. }
  142. ret = dev_pm_qos_add_request(get_cpu_device(cpu),
  143. &pm_req->req,
  144. DEV_PM_QOS_RESUME_LATENCY,
  145. 30);
  146. if (ret < 0) {
  147. rdt_last_cmd_printf("Failed to add latency req CPU%d\n",
  148. cpu);
  149. kfree(pm_req);
  150. ret = -1;
  151. goto out_err;
  152. }
  153. list_add(&pm_req->list, &plr->pm_reqs);
  154. }
  155. return 0;
  156. out_err:
  157. pseudo_lock_cstates_relax(plr);
  158. return ret;
  159. }
  160. /**
  161. * pseudo_lock_region_clear - Reset pseudo-lock region data
  162. * @plr: pseudo-lock region
  163. *
  164. * All content of the pseudo-locked region is reset - any memory allocated
  165. * freed.
  166. *
  167. * Return: void
  168. */
  169. static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)
  170. {
  171. plr->size = 0;
  172. plr->line_size = 0;
  173. kfree(plr->kmem);
  174. plr->kmem = NULL;
  175. plr->s = NULL;
  176. if (plr->d)
  177. plr->d->plr = NULL;
  178. plr->d = NULL;
  179. plr->cbm = 0;
  180. plr->debugfs_dir = NULL;
  181. }
  182. /**
  183. * pseudo_lock_region_init - Initialize pseudo-lock region information
  184. * @plr: pseudo-lock region
  185. *
  186. * Called after user provided a schemata to be pseudo-locked. From the
  187. * schemata the &struct pseudo_lock_region is on entry already initialized
  188. * with the resource, domain, and capacity bitmask. Here the information
  189. * required for pseudo-locking is deduced from this data and &struct
  190. * pseudo_lock_region initialized further. This information includes:
  191. * - size in bytes of the region to be pseudo-locked
  192. * - cache line size to know the stride with which data needs to be accessed
  193. * to be pseudo-locked
  194. * - a cpu associated with the cache instance on which the pseudo-locking
  195. * flow can be executed
  196. *
  197. * Return: 0 on success, <0 on failure. Descriptive error will be written
  198. * to last_cmd_status buffer.
  199. */
  200. static int pseudo_lock_region_init(struct pseudo_lock_region *plr)
  201. {
  202. enum resctrl_scope scope = plr->s->res->ctrl_scope;
  203. struct cacheinfo *ci;
  204. int ret;
  205. if (WARN_ON_ONCE(scope != RESCTRL_L2_CACHE && scope != RESCTRL_L3_CACHE))
  206. return -ENODEV;
  207. /* Pick the first cpu we find that is associated with the cache. */
  208. plr->cpu = cpumask_first(&plr->d->hdr.cpu_mask);
  209. if (!cpu_online(plr->cpu)) {
  210. rdt_last_cmd_printf("CPU %u associated with cache not online\n",
  211. plr->cpu);
  212. ret = -ENODEV;
  213. goto out_region;
  214. }
  215. ci = get_cpu_cacheinfo_level(plr->cpu, scope);
  216. if (ci) {
  217. plr->line_size = ci->coherency_line_size;
  218. plr->size = rdtgroup_cbm_to_size(plr->s->res, plr->d, plr->cbm);
  219. return 0;
  220. }
  221. ret = -1;
  222. rdt_last_cmd_puts("Unable to determine cache line size\n");
  223. out_region:
  224. pseudo_lock_region_clear(plr);
  225. return ret;
  226. }
  227. /**
  228. * pseudo_lock_init - Initialize a pseudo-lock region
  229. * @rdtgrp: resource group to which new pseudo-locked region will belong
  230. *
  231. * A pseudo-locked region is associated with a resource group. When this
  232. * association is created the pseudo-locked region is initialized. The
  233. * details of the pseudo-locked region are not known at this time so only
  234. * allocation is done and association established.
  235. *
  236. * Return: 0 on success, <0 on failure
  237. */
  238. static int pseudo_lock_init(struct rdtgroup *rdtgrp)
  239. {
  240. struct pseudo_lock_region *plr;
  241. plr = kzalloc_obj(*plr);
  242. if (!plr)
  243. return -ENOMEM;
  244. init_waitqueue_head(&plr->lock_thread_wq);
  245. INIT_LIST_HEAD(&plr->pm_reqs);
  246. rdtgrp->plr = plr;
  247. return 0;
  248. }
  249. /**
  250. * pseudo_lock_region_alloc - Allocate kernel memory that will be pseudo-locked
  251. * @plr: pseudo-lock region
  252. *
  253. * Initialize the details required to set up the pseudo-locked region and
  254. * allocate the contiguous memory that will be pseudo-locked to the cache.
  255. *
  256. * Return: 0 on success, <0 on failure. Descriptive error will be written
  257. * to last_cmd_status buffer.
  258. */
  259. static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr)
  260. {
  261. int ret;
  262. ret = pseudo_lock_region_init(plr);
  263. if (ret < 0)
  264. return ret;
  265. /*
  266. * We do not yet support contiguous regions larger than
  267. * KMALLOC_MAX_SIZE.
  268. */
  269. if (plr->size > KMALLOC_MAX_SIZE) {
  270. rdt_last_cmd_puts("Requested region exceeds maximum size\n");
  271. ret = -E2BIG;
  272. goto out_region;
  273. }
  274. plr->kmem = kzalloc(plr->size, GFP_KERNEL);
  275. if (!plr->kmem) {
  276. rdt_last_cmd_puts("Unable to allocate memory\n");
  277. ret = -ENOMEM;
  278. goto out_region;
  279. }
  280. ret = 0;
  281. goto out;
  282. out_region:
  283. pseudo_lock_region_clear(plr);
  284. out:
  285. return ret;
  286. }
  287. /**
  288. * pseudo_lock_free - Free a pseudo-locked region
  289. * @rdtgrp: resource group to which pseudo-locked region belonged
  290. *
  291. * The pseudo-locked region's resources have already been released, or not
  292. * yet created at this point. Now it can be freed and disassociated from the
  293. * resource group.
  294. *
  295. * Return: void
  296. */
  297. static void pseudo_lock_free(struct rdtgroup *rdtgrp)
  298. {
  299. pseudo_lock_region_clear(rdtgrp->plr);
  300. kfree(rdtgrp->plr);
  301. rdtgrp->plr = NULL;
  302. }
  303. /**
  304. * rdtgroup_monitor_in_progress - Test if monitoring in progress
  305. * @rdtgrp: resource group being queried
  306. *
  307. * Return: 1 if monitor groups have been created for this resource
  308. * group, 0 otherwise.
  309. */
  310. static int rdtgroup_monitor_in_progress(struct rdtgroup *rdtgrp)
  311. {
  312. return !list_empty(&rdtgrp->mon.crdtgrp_list);
  313. }
  314. /**
  315. * rdtgroup_locksetup_user_restrict - Restrict user access to group
  316. * @rdtgrp: resource group needing access restricted
  317. *
  318. * A resource group used for cache pseudo-locking cannot have cpus or tasks
  319. * assigned to it. This is communicated to the user by restricting access
  320. * to all the files that can be used to make such changes.
  321. *
  322. * Permissions restored with rdtgroup_locksetup_user_restore()
  323. *
  324. * Return: 0 on success, <0 on failure. If a failure occurs during the
  325. * restriction of access an attempt will be made to restore permissions but
  326. * the state of the mode of these files will be uncertain when a failure
  327. * occurs.
  328. */
  329. static int rdtgroup_locksetup_user_restrict(struct rdtgroup *rdtgrp)
  330. {
  331. int ret;
  332. ret = rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
  333. if (ret)
  334. return ret;
  335. ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
  336. if (ret)
  337. goto err_tasks;
  338. ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
  339. if (ret)
  340. goto err_cpus;
  341. if (resctrl_arch_mon_capable()) {
  342. ret = rdtgroup_kn_mode_restrict(rdtgrp, "mon_groups");
  343. if (ret)
  344. goto err_cpus_list;
  345. }
  346. ret = 0;
  347. goto out;
  348. err_cpus_list:
  349. rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
  350. err_cpus:
  351. rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
  352. err_tasks:
  353. rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
  354. out:
  355. return ret;
  356. }
  357. /**
  358. * rdtgroup_locksetup_user_restore - Restore user access to group
  359. * @rdtgrp: resource group needing access restored
  360. *
  361. * Restore all file access previously removed using
  362. * rdtgroup_locksetup_user_restrict()
  363. *
  364. * Return: 0 on success, <0 on failure. If a failure occurs during the
  365. * restoration of access an attempt will be made to restrict permissions
  366. * again but the state of the mode of these files will be uncertain when
  367. * a failure occurs.
  368. */
  369. static int rdtgroup_locksetup_user_restore(struct rdtgroup *rdtgrp)
  370. {
  371. int ret;
  372. ret = rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
  373. if (ret)
  374. return ret;
  375. ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
  376. if (ret)
  377. goto err_tasks;
  378. ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
  379. if (ret)
  380. goto err_cpus;
  381. if (resctrl_arch_mon_capable()) {
  382. ret = rdtgroup_kn_mode_restore(rdtgrp, "mon_groups", 0777);
  383. if (ret)
  384. goto err_cpus_list;
  385. }
  386. ret = 0;
  387. goto out;
  388. err_cpus_list:
  389. rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
  390. err_cpus:
  391. rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
  392. err_tasks:
  393. rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
  394. out:
  395. return ret;
  396. }
  397. /**
  398. * rdtgroup_locksetup_enter - Resource group enters locksetup mode
  399. * @rdtgrp: resource group requested to enter locksetup mode
  400. *
  401. * A resource group enters locksetup mode to reflect that it would be used
  402. * to represent a pseudo-locked region and is in the process of being set
  403. * up to do so. A resource group used for a pseudo-locked region would
  404. * lose the closid associated with it so we cannot allow it to have any
  405. * tasks or cpus assigned nor permit tasks or cpus to be assigned in the
  406. * future. Monitoring of a pseudo-locked region is not allowed either.
  407. *
  408. * The above and more restrictions on a pseudo-locked region are checked
  409. * for and enforced before the resource group enters the locksetup mode.
  410. *
  411. * Returns: 0 if the resource group successfully entered locksetup mode, <0
  412. * on failure. On failure the last_cmd_status buffer is updated with text to
  413. * communicate details of failure to the user.
  414. */
  415. int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
  416. {
  417. int ret;
  418. /*
  419. * The default resource group can neither be removed nor lose the
  420. * default closid associated with it.
  421. */
  422. if (rdtgrp == &rdtgroup_default) {
  423. rdt_last_cmd_puts("Cannot pseudo-lock default group\n");
  424. return -EINVAL;
  425. }
  426. /*
  427. * Cache Pseudo-locking not supported when CDP is enabled.
  428. *
  429. * Some things to consider if you would like to enable this
  430. * support (using L3 CDP as example):
  431. * - When CDP is enabled two separate resources are exposed,
  432. * L3DATA and L3CODE, but they are actually on the same cache.
  433. * The implication for pseudo-locking is that if a
  434. * pseudo-locked region is created on a domain of one
  435. * resource (eg. L3CODE), then a pseudo-locked region cannot
  436. * be created on that same domain of the other resource
  437. * (eg. L3DATA). This is because the creation of a
  438. * pseudo-locked region involves a call to wbinvd that will
  439. * affect all cache allocations on particular domain.
  440. * - Considering the previous, it may be possible to only
  441. * expose one of the CDP resources to pseudo-locking and
  442. * hide the other. For example, we could consider to only
  443. * expose L3DATA and since the L3 cache is unified it is
  444. * still possible to place instructions there are execute it.
  445. * - If only one region is exposed to pseudo-locking we should
  446. * still keep in mind that availability of a portion of cache
  447. * for pseudo-locking should take into account both resources.
  448. * Similarly, if a pseudo-locked region is created in one
  449. * resource, the portion of cache used by it should be made
  450. * unavailable to all future allocations from both resources.
  451. */
  452. if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3) ||
  453. resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) {
  454. rdt_last_cmd_puts("CDP enabled\n");
  455. return -EINVAL;
  456. }
  457. /*
  458. * Not knowing the bits to disable prefetching implies that this
  459. * platform does not support Cache Pseudo-Locking.
  460. */
  461. if (resctrl_arch_get_prefetch_disable_bits() == 0) {
  462. rdt_last_cmd_puts("Pseudo-locking not supported\n");
  463. return -EINVAL;
  464. }
  465. if (rdtgroup_monitor_in_progress(rdtgrp)) {
  466. rdt_last_cmd_puts("Monitoring in progress\n");
  467. return -EINVAL;
  468. }
  469. if (rdtgroup_tasks_assigned(rdtgrp)) {
  470. rdt_last_cmd_puts("Tasks assigned to resource group\n");
  471. return -EINVAL;
  472. }
  473. if (!cpumask_empty(&rdtgrp->cpu_mask)) {
  474. rdt_last_cmd_puts("CPUs assigned to resource group\n");
  475. return -EINVAL;
  476. }
  477. if (rdtgroup_locksetup_user_restrict(rdtgrp)) {
  478. rdt_last_cmd_puts("Unable to modify resctrl permissions\n");
  479. return -EIO;
  480. }
  481. ret = pseudo_lock_init(rdtgrp);
  482. if (ret) {
  483. rdt_last_cmd_puts("Unable to init pseudo-lock region\n");
  484. goto out_release;
  485. }
  486. /*
  487. * If this system is capable of monitoring a rmid would have been
  488. * allocated when the control group was created. This is not needed
  489. * anymore when this group would be used for pseudo-locking. This
  490. * is safe to call on platforms not capable of monitoring.
  491. */
  492. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  493. ret = 0;
  494. goto out;
  495. out_release:
  496. rdtgroup_locksetup_user_restore(rdtgrp);
  497. out:
  498. return ret;
  499. }
  500. /**
  501. * rdtgroup_locksetup_exit - resource group exist locksetup mode
  502. * @rdtgrp: resource group
  503. *
  504. * When a resource group exits locksetup mode the earlier restrictions are
  505. * lifted.
  506. *
  507. * Return: 0 on success, <0 on failure
  508. */
  509. int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
  510. {
  511. int ret;
  512. if (resctrl_arch_mon_capable()) {
  513. ret = alloc_rmid(rdtgrp->closid);
  514. if (ret < 0) {
  515. rdt_last_cmd_puts("Out of RMIDs\n");
  516. return ret;
  517. }
  518. rdtgrp->mon.rmid = ret;
  519. }
  520. ret = rdtgroup_locksetup_user_restore(rdtgrp);
  521. if (ret) {
  522. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  523. return ret;
  524. }
  525. pseudo_lock_free(rdtgrp);
  526. return 0;
  527. }
  528. /**
  529. * rdtgroup_cbm_overlaps_pseudo_locked - Test if CBM or portion is pseudo-locked
  530. * @d: RDT domain
  531. * @cbm: CBM to test
  532. *
  533. * @d represents a cache instance and @cbm a capacity bitmask that is
  534. * considered for it. Determine if @cbm overlaps with any existing
  535. * pseudo-locked region on @d.
  536. *
  537. * @cbm is unsigned long, even if only 32 bits are used, to make the
  538. * bitmap functions work correctly.
  539. *
  540. * Return: true if @cbm overlaps with pseudo-locked region on @d, false
  541. * otherwise.
  542. */
  543. bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_ctrl_domain *d, unsigned long cbm)
  544. {
  545. unsigned int cbm_len;
  546. unsigned long cbm_b;
  547. if (d->plr) {
  548. cbm_len = d->plr->s->res->cache.cbm_len;
  549. cbm_b = d->plr->cbm;
  550. if (bitmap_intersects(&cbm, &cbm_b, cbm_len))
  551. return true;
  552. }
  553. return false;
  554. }
  555. /**
  556. * rdtgroup_pseudo_locked_in_hierarchy - Pseudo-locked region in cache hierarchy
  557. * @d: RDT domain under test
  558. *
  559. * The setup of a pseudo-locked region affects all cache instances within
  560. * the hierarchy of the region. It is thus essential to know if any
  561. * pseudo-locked regions exist within a cache hierarchy to prevent any
  562. * attempts to create new pseudo-locked regions in the same hierarchy.
  563. *
  564. * Return: true if a pseudo-locked region exists in the hierarchy of @d or
  565. * if it is not possible to test due to memory allocation issue,
  566. * false otherwise.
  567. */
  568. bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)
  569. {
  570. struct rdt_ctrl_domain *d_i;
  571. cpumask_var_t cpu_with_psl;
  572. struct rdt_resource *r;
  573. bool ret = false;
  574. /* Walking r->domains, ensure it can't race with cpuhp */
  575. lockdep_assert_cpus_held();
  576. if (!zalloc_cpumask_var(&cpu_with_psl, GFP_KERNEL))
  577. return true;
  578. /*
  579. * First determine which cpus have pseudo-locked regions
  580. * associated with them.
  581. */
  582. for_each_alloc_capable_rdt_resource(r) {
  583. list_for_each_entry(d_i, &r->ctrl_domains, hdr.list) {
  584. if (d_i->plr)
  585. cpumask_or(cpu_with_psl, cpu_with_psl,
  586. &d_i->hdr.cpu_mask);
  587. }
  588. }
  589. /*
  590. * Next test if new pseudo-locked region would intersect with
  591. * existing region.
  592. */
  593. if (cpumask_intersects(&d->hdr.cpu_mask, cpu_with_psl))
  594. ret = true;
  595. free_cpumask_var(cpu_with_psl);
  596. return ret;
  597. }
  598. /**
  599. * pseudo_lock_measure_cycles - Trigger latency measure to pseudo-locked region
  600. * @rdtgrp: Resource group to which the pseudo-locked region belongs.
  601. * @sel: Selector of which measurement to perform on a pseudo-locked region.
  602. *
  603. * The measurement of latency to access a pseudo-locked region should be
  604. * done from a cpu that is associated with that pseudo-locked region.
  605. * Determine which cpu is associated with this region and start a thread on
  606. * that cpu to perform the measurement, wait for that thread to complete.
  607. *
  608. * Return: 0 on success, <0 on failure
  609. */
  610. static int pseudo_lock_measure_cycles(struct rdtgroup *rdtgrp, int sel)
  611. {
  612. struct pseudo_lock_region *plr = rdtgrp->plr;
  613. struct task_struct *thread;
  614. unsigned int cpu;
  615. int ret = -1;
  616. cpus_read_lock();
  617. mutex_lock(&rdtgroup_mutex);
  618. if (rdtgrp->flags & RDT_DELETED) {
  619. ret = -ENODEV;
  620. goto out;
  621. }
  622. if (!plr->d) {
  623. ret = -ENODEV;
  624. goto out;
  625. }
  626. plr->thread_done = 0;
  627. cpu = cpumask_first(&plr->d->hdr.cpu_mask);
  628. if (!cpu_online(cpu)) {
  629. ret = -ENODEV;
  630. goto out;
  631. }
  632. plr->cpu = cpu;
  633. if (sel == 1)
  634. thread = kthread_run_on_cpu(resctrl_arch_measure_cycles_lat_fn,
  635. plr, cpu, "pseudo_lock_measure/%u");
  636. else if (sel == 2)
  637. thread = kthread_run_on_cpu(resctrl_arch_measure_l2_residency,
  638. plr, cpu, "pseudo_lock_measure/%u");
  639. else if (sel == 3)
  640. thread = kthread_run_on_cpu(resctrl_arch_measure_l3_residency,
  641. plr, cpu, "pseudo_lock_measure/%u");
  642. else
  643. goto out;
  644. if (IS_ERR(thread)) {
  645. ret = PTR_ERR(thread);
  646. goto out;
  647. }
  648. ret = wait_event_interruptible(plr->lock_thread_wq,
  649. plr->thread_done == 1);
  650. if (ret < 0)
  651. goto out;
  652. ret = 0;
  653. out:
  654. mutex_unlock(&rdtgroup_mutex);
  655. cpus_read_unlock();
  656. return ret;
  657. }
  658. static ssize_t pseudo_lock_measure_trigger(struct file *file,
  659. const char __user *user_buf,
  660. size_t count, loff_t *ppos)
  661. {
  662. struct rdtgroup *rdtgrp = file->private_data;
  663. size_t buf_size;
  664. char buf[32];
  665. int ret;
  666. int sel;
  667. buf_size = min(count, (sizeof(buf) - 1));
  668. if (copy_from_user(buf, user_buf, buf_size))
  669. return -EFAULT;
  670. buf[buf_size] = '\0';
  671. ret = kstrtoint(buf, 10, &sel);
  672. if (ret == 0) {
  673. if (sel != 1 && sel != 2 && sel != 3)
  674. return -EINVAL;
  675. ret = pseudo_lock_measure_cycles(rdtgrp, sel);
  676. if (ret == 0)
  677. ret = count;
  678. }
  679. return ret;
  680. }
  681. static const struct file_operations pseudo_measure_fops = {
  682. .write = pseudo_lock_measure_trigger,
  683. .open = simple_open,
  684. .llseek = default_llseek,
  685. };
  686. /**
  687. * rdtgroup_pseudo_lock_create - Create a pseudo-locked region
  688. * @rdtgrp: resource group to which pseudo-lock region belongs
  689. *
  690. * Called when a resource group in the pseudo-locksetup mode receives a
  691. * valid schemata that should be pseudo-locked. Since the resource group is
  692. * in pseudo-locksetup mode the &struct pseudo_lock_region has already been
  693. * allocated and initialized with the essential information. If a failure
  694. * occurs the resource group remains in the pseudo-locksetup mode with the
  695. * &struct pseudo_lock_region associated with it, but cleared from all
  696. * information and ready for the user to re-attempt pseudo-locking by
  697. * writing the schemata again.
  698. *
  699. * Return: 0 if the pseudo-locked region was successfully pseudo-locked, <0
  700. * on failure. Descriptive error will be written to last_cmd_status buffer.
  701. */
  702. int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
  703. {
  704. struct pseudo_lock_region *plr = rdtgrp->plr;
  705. struct task_struct *thread;
  706. unsigned int new_minor;
  707. struct device *dev;
  708. char *kn_name __free(kfree) = NULL;
  709. int ret;
  710. ret = pseudo_lock_region_alloc(plr);
  711. if (ret < 0)
  712. return ret;
  713. ret = pseudo_lock_cstates_constrain(plr);
  714. if (ret < 0) {
  715. ret = -EINVAL;
  716. goto out_region;
  717. }
  718. kn_name = kstrdup(rdt_kn_name(rdtgrp->kn), GFP_KERNEL);
  719. if (!kn_name) {
  720. ret = -ENOMEM;
  721. goto out_cstates;
  722. }
  723. plr->thread_done = 0;
  724. thread = kthread_run_on_cpu(resctrl_arch_pseudo_lock_fn, plr,
  725. plr->cpu, "pseudo_lock/%u");
  726. if (IS_ERR(thread)) {
  727. ret = PTR_ERR(thread);
  728. rdt_last_cmd_printf("Locking thread returned error %d\n", ret);
  729. goto out_cstates;
  730. }
  731. ret = wait_event_interruptible(plr->lock_thread_wq,
  732. plr->thread_done == 1);
  733. if (ret < 0) {
  734. /*
  735. * If the thread does not get on the CPU for whatever
  736. * reason and the process which sets up the region is
  737. * interrupted then this will leave the thread in runnable
  738. * state and once it gets on the CPU it will dereference
  739. * the cleared, but not freed, plr struct resulting in an
  740. * empty pseudo-locking loop.
  741. */
  742. rdt_last_cmd_puts("Locking thread interrupted\n");
  743. goto out_cstates;
  744. }
  745. ret = pseudo_lock_minor_get(&new_minor);
  746. if (ret < 0) {
  747. rdt_last_cmd_puts("Unable to obtain a new minor number\n");
  748. goto out_cstates;
  749. }
  750. /*
  751. * Unlock access but do not release the reference. The
  752. * pseudo-locked region will still be here on return.
  753. *
  754. * The mutex has to be released temporarily to avoid a potential
  755. * deadlock with the mm->mmap_lock which is obtained in the
  756. * device_create() and debugfs_create_dir() callpath below as well as
  757. * before the mmap() callback is called.
  758. */
  759. mutex_unlock(&rdtgroup_mutex);
  760. if (!IS_ERR_OR_NULL(debugfs_resctrl)) {
  761. plr->debugfs_dir = debugfs_create_dir(kn_name, debugfs_resctrl);
  762. if (!IS_ERR_OR_NULL(plr->debugfs_dir))
  763. debugfs_create_file("pseudo_lock_measure", 0200,
  764. plr->debugfs_dir, rdtgrp,
  765. &pseudo_measure_fops);
  766. }
  767. dev = device_create(&pseudo_lock_class, NULL,
  768. MKDEV(pseudo_lock_major, new_minor),
  769. rdtgrp, "%s", kn_name);
  770. mutex_lock(&rdtgroup_mutex);
  771. if (IS_ERR(dev)) {
  772. ret = PTR_ERR(dev);
  773. rdt_last_cmd_printf("Failed to create character device: %d\n",
  774. ret);
  775. goto out_debugfs;
  776. }
  777. /* We released the mutex - check if group was removed while we did so */
  778. if (rdtgrp->flags & RDT_DELETED) {
  779. ret = -ENODEV;
  780. goto out_device;
  781. }
  782. plr->minor = new_minor;
  783. rdtgrp->mode = RDT_MODE_PSEUDO_LOCKED;
  784. closid_free(rdtgrp->closid);
  785. rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0444);
  786. rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0444);
  787. ret = 0;
  788. goto out;
  789. out_device:
  790. device_destroy(&pseudo_lock_class, MKDEV(pseudo_lock_major, new_minor));
  791. out_debugfs:
  792. debugfs_remove_recursive(plr->debugfs_dir);
  793. pseudo_lock_minor_release(new_minor);
  794. out_cstates:
  795. pseudo_lock_cstates_relax(plr);
  796. out_region:
  797. pseudo_lock_region_clear(plr);
  798. out:
  799. return ret;
  800. }
  801. /**
  802. * rdtgroup_pseudo_lock_remove - Remove a pseudo-locked region
  803. * @rdtgrp: resource group to which the pseudo-locked region belongs
  804. *
  805. * The removal of a pseudo-locked region can be initiated when the resource
  806. * group is removed from user space via a "rmdir" from userspace or the
  807. * unmount of the resctrl filesystem. On removal the resource group does
  808. * not go back to pseudo-locksetup mode before it is removed, instead it is
  809. * removed directly. There is thus asymmetry with the creation where the
  810. * &struct pseudo_lock_region is removed here while it was not created in
  811. * rdtgroup_pseudo_lock_create().
  812. *
  813. * Return: void
  814. */
  815. void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp)
  816. {
  817. struct pseudo_lock_region *plr = rdtgrp->plr;
  818. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  819. /*
  820. * Default group cannot be a pseudo-locked region so we can
  821. * free closid here.
  822. */
  823. closid_free(rdtgrp->closid);
  824. goto free;
  825. }
  826. pseudo_lock_cstates_relax(plr);
  827. debugfs_remove_recursive(rdtgrp->plr->debugfs_dir);
  828. device_destroy(&pseudo_lock_class, MKDEV(pseudo_lock_major, plr->minor));
  829. pseudo_lock_minor_release(plr->minor);
  830. free:
  831. pseudo_lock_free(rdtgrp);
  832. }
  833. static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
  834. {
  835. struct rdtgroup *rdtgrp;
  836. mutex_lock(&rdtgroup_mutex);
  837. rdtgrp = region_find_by_minor(iminor(inode));
  838. if (!rdtgrp) {
  839. mutex_unlock(&rdtgroup_mutex);
  840. return -ENODEV;
  841. }
  842. filp->private_data = rdtgrp;
  843. atomic_inc(&rdtgrp->waitcount);
  844. /* Perform a non-seekable open - llseek is not supported */
  845. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  846. mutex_unlock(&rdtgroup_mutex);
  847. return 0;
  848. }
  849. static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
  850. {
  851. struct rdtgroup *rdtgrp;
  852. mutex_lock(&rdtgroup_mutex);
  853. rdtgrp = filp->private_data;
  854. WARN_ON(!rdtgrp);
  855. if (!rdtgrp) {
  856. mutex_unlock(&rdtgroup_mutex);
  857. return -ENODEV;
  858. }
  859. filp->private_data = NULL;
  860. atomic_dec(&rdtgrp->waitcount);
  861. mutex_unlock(&rdtgroup_mutex);
  862. return 0;
  863. }
  864. static int pseudo_lock_dev_mremap(struct vm_area_struct *area)
  865. {
  866. /* Not supported */
  867. return -EINVAL;
  868. }
  869. static const struct vm_operations_struct pseudo_mmap_ops = {
  870. .mremap = pseudo_lock_dev_mremap,
  871. };
  872. static int pseudo_lock_dev_mmap_prepare(struct vm_area_desc *desc)
  873. {
  874. unsigned long off = desc->pgoff << PAGE_SHIFT;
  875. unsigned long vsize = vma_desc_size(desc);
  876. struct file *filp = desc->file;
  877. struct pseudo_lock_region *plr;
  878. struct rdtgroup *rdtgrp;
  879. unsigned long physical;
  880. unsigned long psize;
  881. mutex_lock(&rdtgroup_mutex);
  882. rdtgrp = filp->private_data;
  883. WARN_ON(!rdtgrp);
  884. if (!rdtgrp) {
  885. mutex_unlock(&rdtgroup_mutex);
  886. return -ENODEV;
  887. }
  888. plr = rdtgrp->plr;
  889. if (!plr->d) {
  890. mutex_unlock(&rdtgroup_mutex);
  891. return -ENODEV;
  892. }
  893. /*
  894. * Task is required to run with affinity to the cpus associated
  895. * with the pseudo-locked region. If this is not the case the task
  896. * may be scheduled elsewhere and invalidate entries in the
  897. * pseudo-locked region.
  898. */
  899. if (!cpumask_subset(current->cpus_ptr, &plr->d->hdr.cpu_mask)) {
  900. mutex_unlock(&rdtgroup_mutex);
  901. return -EINVAL;
  902. }
  903. physical = __pa(plr->kmem) >> PAGE_SHIFT;
  904. psize = plr->size - off;
  905. if (off > plr->size) {
  906. mutex_unlock(&rdtgroup_mutex);
  907. return -ENOSPC;
  908. }
  909. /*
  910. * Ensure changes are carried directly to the memory being mapped,
  911. * do not allow copy-on-write mapping.
  912. */
  913. if (!vma_desc_test_flags(desc, VMA_SHARED_BIT)) {
  914. mutex_unlock(&rdtgroup_mutex);
  915. return -EINVAL;
  916. }
  917. if (vsize > psize) {
  918. mutex_unlock(&rdtgroup_mutex);
  919. return -ENOSPC;
  920. }
  921. memset(plr->kmem + off, 0, vsize);
  922. desc->vm_ops = &pseudo_mmap_ops;
  923. mmap_action_remap_full(desc, physical + desc->pgoff);
  924. mutex_unlock(&rdtgroup_mutex);
  925. return 0;
  926. }
  927. static const struct file_operations pseudo_lock_dev_fops = {
  928. .owner = THIS_MODULE,
  929. .read = NULL,
  930. .write = NULL,
  931. .open = pseudo_lock_dev_open,
  932. .release = pseudo_lock_dev_release,
  933. .mmap_prepare = pseudo_lock_dev_mmap_prepare,
  934. };
  935. int rdt_pseudo_lock_init(void)
  936. {
  937. int ret;
  938. ret = register_chrdev(0, "pseudo_lock", &pseudo_lock_dev_fops);
  939. if (ret < 0)
  940. return ret;
  941. pseudo_lock_major = ret;
  942. ret = class_register(&pseudo_lock_class);
  943. if (ret) {
  944. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  945. return ret;
  946. }
  947. return 0;
  948. }
  949. void rdt_pseudo_lock_release(void)
  950. {
  951. class_unregister(&pseudo_lock_class);
  952. unregister_chrdev(pseudo_lock_major, "pseudo_lock");
  953. pseudo_lock_major = 0;
  954. }