PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/x86/kernel/cpu/resctrl/ctrlmondata.c

https://github.com/acmel/linux
C | 490 lines | 350 code | 62 blank | 78 comment | 80 complexity | c758909ffbc139e084376e4e5ce8f48c MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Resource Director Technology(RDT)
  4. * - Cache Allocation code.
  5. *
  6. * Copyright (C) 2016 Intel Corporation
  7. *
  8. * Authors:
  9. * Fenghua Yu <fenghua.yu@intel.com>
  10. * Tony Luck <tony.luck@intel.com>
  11. *
  12. * More information about RDT be found in the Intel (R) x86 Architecture
  13. * Software Developer Manual June 2016, volume 3, section 17.17.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/cpu.h>
  17. #include <linux/kernfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/slab.h>
  20. #include "internal.h"
  21. /*
  22. * Check whether MBA bandwidth percentage value is correct. The value is
  23. * checked against the minimum and max bandwidth values specified by the
  24. * hardware. The allocated bandwidth percentage is rounded to the next
  25. * control step available on the hardware.
  26. */
  27. static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
  28. {
  29. unsigned long bw;
  30. int ret;
  31. /*
  32. * Only linear delay values is supported for current Intel SKUs.
  33. */
  34. if (!r->membw.delay_linear && r->membw.arch_needs_linear) {
  35. rdt_last_cmd_puts("No support for non-linear MB domains\n");
  36. return false;
  37. }
  38. ret = kstrtoul(buf, 10, &bw);
  39. if (ret) {
  40. rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
  41. return false;
  42. }
  43. if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
  44. !is_mba_sc(r)) {
  45. rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
  46. r->membw.min_bw, r->default_ctrl);
  47. return false;
  48. }
  49. *data = roundup(bw, (unsigned long)r->membw.bw_gran);
  50. return true;
  51. }
  52. int parse_bw(struct rdt_parse_data *data, struct rdt_resource *r,
  53. struct rdt_domain *d)
  54. {
  55. unsigned long bw_val;
  56. if (d->have_new_ctrl) {
  57. rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
  58. return -EINVAL;
  59. }
  60. if (!bw_validate(data->buf, &bw_val, r))
  61. return -EINVAL;
  62. d->new_ctrl = bw_val;
  63. d->have_new_ctrl = true;
  64. return 0;
  65. }
  66. /*
  67. * Check whether a cache bit mask is valid.
  68. * For Intel the SDM says:
  69. * Please note that all (and only) contiguous '1' combinations
  70. * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
  71. * Additionally Haswell requires at least two bits set.
  72. * AMD allows non-contiguous bitmasks.
  73. */
  74. static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
  75. {
  76. unsigned long first_bit, zero_bit, val;
  77. unsigned int cbm_len = r->cache.cbm_len;
  78. int ret;
  79. ret = kstrtoul(buf, 16, &val);
  80. if (ret) {
  81. rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
  82. return false;
  83. }
  84. if ((!r->cache.arch_has_empty_bitmaps && val == 0) ||
  85. val > r->default_ctrl) {
  86. rdt_last_cmd_puts("Mask out of range\n");
  87. return false;
  88. }
  89. first_bit = find_first_bit(&val, cbm_len);
  90. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  91. /* Are non-contiguous bitmaps allowed? */
  92. if (!r->cache.arch_has_sparse_bitmaps &&
  93. (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)) {
  94. rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
  95. return false;
  96. }
  97. if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
  98. rdt_last_cmd_printf("Need at least %d bits in the mask\n",
  99. r->cache.min_cbm_bits);
  100. return false;
  101. }
  102. *data = val;
  103. return true;
  104. }
  105. /*
  106. * Read one cache bit mask (hex). Check that it is valid for the current
  107. * resource type.
  108. */
  109. int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r,
  110. struct rdt_domain *d)
  111. {
  112. struct rdtgroup *rdtgrp = data->rdtgrp;
  113. u32 cbm_val;
  114. if (d->have_new_ctrl) {
  115. rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
  116. return -EINVAL;
  117. }
  118. /*
  119. * Cannot set up more than one pseudo-locked region in a cache
  120. * hierarchy.
  121. */
  122. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  123. rdtgroup_pseudo_locked_in_hierarchy(d)) {
  124. rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
  125. return -EINVAL;
  126. }
  127. if (!cbm_validate(data->buf, &cbm_val, r))
  128. return -EINVAL;
  129. if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
  130. rdtgrp->mode == RDT_MODE_SHAREABLE) &&
  131. rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
  132. rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
  133. return -EINVAL;
  134. }
  135. /*
  136. * The CBM may not overlap with the CBM of another closid if
  137. * either is exclusive.
  138. */
  139. if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
  140. rdt_last_cmd_puts("Overlaps with exclusive group\n");
  141. return -EINVAL;
  142. }
  143. if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
  144. if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
  145. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  146. rdt_last_cmd_puts("Overlaps with other group\n");
  147. return -EINVAL;
  148. }
  149. }
  150. d->new_ctrl = cbm_val;
  151. d->have_new_ctrl = true;
  152. return 0;
  153. }
  154. /*
  155. * For each domain in this resource we expect to find a series of:
  156. * id=mask
  157. * separated by ";". The "id" is in decimal, and must match one of
  158. * the "id"s for this resource.
  159. */
  160. static int parse_line(char *line, struct rdt_resource *r,
  161. struct rdtgroup *rdtgrp)
  162. {
  163. struct rdt_parse_data data;
  164. char *dom = NULL, *id;
  165. struct rdt_domain *d;
  166. unsigned long dom_id;
  167. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  168. r->rid == RDT_RESOURCE_MBA) {
  169. rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
  170. return -EINVAL;
  171. }
  172. next:
  173. if (!line || line[0] == '\0')
  174. return 0;
  175. dom = strsep(&line, ";");
  176. id = strsep(&dom, "=");
  177. if (!dom || kstrtoul(id, 10, &dom_id)) {
  178. rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
  179. return -EINVAL;
  180. }
  181. dom = strim(dom);
  182. list_for_each_entry(d, &r->domains, list) {
  183. if (d->id == dom_id) {
  184. data.buf = dom;
  185. data.rdtgrp = rdtgrp;
  186. if (r->parse_ctrlval(&data, r, d))
  187. return -EINVAL;
  188. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  189. /*
  190. * In pseudo-locking setup mode and just
  191. * parsed a valid CBM that should be
  192. * pseudo-locked. Only one locked region per
  193. * resource group and domain so just do
  194. * the required initialization for single
  195. * region and return.
  196. */
  197. rdtgrp->plr->r = r;
  198. rdtgrp->plr->d = d;
  199. rdtgrp->plr->cbm = d->new_ctrl;
  200. d->plr = rdtgrp->plr;
  201. return 0;
  202. }
  203. goto next;
  204. }
  205. }
  206. return -EINVAL;
  207. }
  208. int update_domains(struct rdt_resource *r, int closid)
  209. {
  210. struct msr_param msr_param;
  211. cpumask_var_t cpu_mask;
  212. struct rdt_domain *d;
  213. bool mba_sc;
  214. u32 *dc;
  215. int cpu;
  216. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  217. return -ENOMEM;
  218. msr_param.low = closid;
  219. msr_param.high = msr_param.low + 1;
  220. msr_param.res = r;
  221. mba_sc = is_mba_sc(r);
  222. list_for_each_entry(d, &r->domains, list) {
  223. dc = !mba_sc ? d->ctrl_val : d->mbps_val;
  224. if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
  225. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  226. dc[closid] = d->new_ctrl;
  227. }
  228. }
  229. /*
  230. * Avoid writing the control msr with control values when
  231. * MBA software controller is enabled
  232. */
  233. if (cpumask_empty(cpu_mask) || mba_sc)
  234. goto done;
  235. cpu = get_cpu();
  236. /* Update resource control msr on this CPU if it's in cpu_mask. */
  237. if (cpumask_test_cpu(cpu, cpu_mask))
  238. rdt_ctrl_update(&msr_param);
  239. /* Update resource control msr on other CPUs. */
  240. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  241. put_cpu();
  242. done:
  243. free_cpumask_var(cpu_mask);
  244. return 0;
  245. }
  246. static int rdtgroup_parse_resource(char *resname, char *tok,
  247. struct rdtgroup *rdtgrp)
  248. {
  249. struct rdt_resource *r;
  250. for_each_alloc_enabled_rdt_resource(r) {
  251. if (!strcmp(resname, r->name) && rdtgrp->closid < r->num_closid)
  252. return parse_line(tok, r, rdtgrp);
  253. }
  254. rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
  255. return -EINVAL;
  256. }
  257. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  258. char *buf, size_t nbytes, loff_t off)
  259. {
  260. struct rdtgroup *rdtgrp;
  261. struct rdt_domain *dom;
  262. struct rdt_resource *r;
  263. char *tok, *resname;
  264. int ret = 0;
  265. /* Valid input requires a trailing newline */
  266. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  267. return -EINVAL;
  268. buf[nbytes - 1] = '\0';
  269. cpus_read_lock();
  270. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  271. if (!rdtgrp) {
  272. rdtgroup_kn_unlock(of->kn);
  273. cpus_read_unlock();
  274. return -ENOENT;
  275. }
  276. rdt_last_cmd_clear();
  277. /*
  278. * No changes to pseudo-locked region allowed. It has to be removed
  279. * and re-created instead.
  280. */
  281. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  282. ret = -EINVAL;
  283. rdt_last_cmd_puts("Resource group is pseudo-locked\n");
  284. goto out;
  285. }
  286. for_each_alloc_enabled_rdt_resource(r) {
  287. list_for_each_entry(dom, &r->domains, list)
  288. dom->have_new_ctrl = false;
  289. }
  290. while ((tok = strsep(&buf, "\n")) != NULL) {
  291. resname = strim(strsep(&tok, ":"));
  292. if (!tok) {
  293. rdt_last_cmd_puts("Missing ':'\n");
  294. ret = -EINVAL;
  295. goto out;
  296. }
  297. if (tok[0] == '\0') {
  298. rdt_last_cmd_printf("Missing '%s' value\n", resname);
  299. ret = -EINVAL;
  300. goto out;
  301. }
  302. ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
  303. if (ret)
  304. goto out;
  305. }
  306. for_each_alloc_enabled_rdt_resource(r) {
  307. ret = update_domains(r, rdtgrp->closid);
  308. if (ret)
  309. goto out;
  310. }
  311. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  312. /*
  313. * If pseudo-locking fails we keep the resource group in
  314. * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
  315. * active and updated for just the domain the pseudo-locked
  316. * region was requested for.
  317. */
  318. ret = rdtgroup_pseudo_lock_create(rdtgrp);
  319. }
  320. out:
  321. rdtgroup_kn_unlock(of->kn);
  322. cpus_read_unlock();
  323. return ret ?: nbytes;
  324. }
  325. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  326. {
  327. struct rdt_domain *dom;
  328. bool sep = false;
  329. u32 ctrl_val;
  330. seq_printf(s, "%*s:", max_name_width, r->name);
  331. list_for_each_entry(dom, &r->domains, list) {
  332. if (sep)
  333. seq_puts(s, ";");
  334. ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
  335. dom->mbps_val[closid]);
  336. seq_printf(s, r->format_str, dom->id, max_data_width,
  337. ctrl_val);
  338. sep = true;
  339. }
  340. seq_puts(s, "\n");
  341. }
  342. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  343. struct seq_file *s, void *v)
  344. {
  345. struct rdtgroup *rdtgrp;
  346. struct rdt_resource *r;
  347. int ret = 0;
  348. u32 closid;
  349. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  350. if (rdtgrp) {
  351. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  352. for_each_alloc_enabled_rdt_resource(r)
  353. seq_printf(s, "%s:uninitialized\n", r->name);
  354. } else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  355. if (!rdtgrp->plr->d) {
  356. rdt_last_cmd_clear();
  357. rdt_last_cmd_puts("Cache domain offline\n");
  358. ret = -ENODEV;
  359. } else {
  360. seq_printf(s, "%s:%d=%x\n",
  361. rdtgrp->plr->r->name,
  362. rdtgrp->plr->d->id,
  363. rdtgrp->plr->cbm);
  364. }
  365. } else {
  366. closid = rdtgrp->closid;
  367. for_each_alloc_enabled_rdt_resource(r) {
  368. if (closid < r->num_closid)
  369. show_doms(s, r, closid);
  370. }
  371. }
  372. } else {
  373. ret = -ENOENT;
  374. }
  375. rdtgroup_kn_unlock(of->kn);
  376. return ret;
  377. }
  378. void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
  379. struct rdt_domain *d, struct rdtgroup *rdtgrp,
  380. int evtid, int first)
  381. {
  382. /*
  383. * setup the parameters to send to the IPI to read the data.
  384. */
  385. rr->rgrp = rdtgrp;
  386. rr->evtid = evtid;
  387. rr->r = r;
  388. rr->d = d;
  389. rr->val = 0;
  390. rr->first = first;
  391. smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
  392. }
  393. int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  394. {
  395. struct kernfs_open_file *of = m->private;
  396. u32 resid, evtid, domid;
  397. struct rdtgroup *rdtgrp;
  398. struct rdt_resource *r;
  399. union mon_data_bits md;
  400. struct rdt_domain *d;
  401. struct rmid_read rr;
  402. int ret = 0;
  403. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  404. if (!rdtgrp) {
  405. ret = -ENOENT;
  406. goto out;
  407. }
  408. md.priv = of->kn->priv;
  409. resid = md.u.rid;
  410. domid = md.u.domid;
  411. evtid = md.u.evtid;
  412. r = &rdt_resources_all[resid];
  413. d = rdt_find_domain(r, domid, NULL);
  414. if (IS_ERR_OR_NULL(d)) {
  415. ret = -ENOENT;
  416. goto out;
  417. }
  418. mon_event_read(&rr, r, d, rdtgrp, evtid, false);
  419. if (rr.val & RMID_VAL_ERROR)
  420. seq_puts(m, "Error\n");
  421. else if (rr.val & RMID_VAL_UNAVAIL)
  422. seq_puts(m, "Unavailable\n");
  423. else
  424. seq_printf(m, "%llu\n", rr.val * r->mon_scale);
  425. out:
  426. rdtgroup_kn_unlock(of->kn);
  427. return ret;
  428. }