ctrlmondata.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/math.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/tick.h>
  22. #include "internal.h"
  23. struct rdt_parse_data {
  24. u32 closid;
  25. enum rdtgrp_mode mode;
  26. char *buf;
  27. };
  28. typedef int (ctrlval_parser_t)(struct rdt_parse_data *data,
  29. struct resctrl_schema *s,
  30. struct rdt_ctrl_domain *d);
  31. /*
  32. * Check whether MBA bandwidth percentage value is correct. The value is
  33. * checked against the minimum and max bandwidth values specified by the
  34. * hardware. The allocated bandwidth percentage is rounded to the next
  35. * control step available on the hardware.
  36. */
  37. static bool bw_validate(char *buf, u32 *data, struct rdt_resource *r)
  38. {
  39. int ret;
  40. u32 bw;
  41. /*
  42. * Only linear delay values is supported for current Intel SKUs.
  43. */
  44. if (!r->membw.delay_linear && r->membw.arch_needs_linear) {
  45. rdt_last_cmd_puts("No support for non-linear MB domains\n");
  46. return false;
  47. }
  48. ret = kstrtou32(buf, 10, &bw);
  49. if (ret) {
  50. rdt_last_cmd_printf("Invalid MB value %s\n", buf);
  51. return false;
  52. }
  53. /* Nothing else to do if software controller is enabled. */
  54. if (is_mba_sc(r)) {
  55. *data = bw;
  56. return true;
  57. }
  58. if (bw < r->membw.min_bw || bw > r->membw.max_bw) {
  59. rdt_last_cmd_printf("MB value %u out of range [%d,%d]\n",
  60. bw, r->membw.min_bw, r->membw.max_bw);
  61. return false;
  62. }
  63. *data = roundup(bw, (unsigned long)r->membw.bw_gran);
  64. return true;
  65. }
  66. static int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
  67. struct rdt_ctrl_domain *d)
  68. {
  69. struct resctrl_staged_config *cfg;
  70. struct rdt_resource *r = s->res;
  71. u32 closid = data->closid;
  72. u32 bw_val;
  73. cfg = &d->staged_config[s->conf_type];
  74. if (cfg->have_new_ctrl) {
  75. rdt_last_cmd_printf("Duplicate domain %d\n", d->hdr.id);
  76. return -EINVAL;
  77. }
  78. if (!bw_validate(data->buf, &bw_val, r))
  79. return -EINVAL;
  80. if (is_mba_sc(r)) {
  81. d->mbps_val[closid] = bw_val;
  82. return 0;
  83. }
  84. cfg->new_ctrl = bw_val;
  85. cfg->have_new_ctrl = true;
  86. return 0;
  87. }
  88. /*
  89. * Check whether a cache bit mask is valid.
  90. * On Intel CPUs, non-contiguous 1s value support is indicated by CPUID:
  91. * - CPUID.0x10.1:ECX[3]: L3 non-contiguous 1s value supported if 1
  92. * - CPUID.0x10.2:ECX[3]: L2 non-contiguous 1s value supported if 1
  93. *
  94. * Haswell does not support a non-contiguous 1s value and additionally
  95. * requires at least two bits set.
  96. * AMD allows non-contiguous bitmasks.
  97. */
  98. static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
  99. {
  100. u32 supported_bits = BIT_MASK(r->cache.cbm_len) - 1;
  101. unsigned int cbm_len = r->cache.cbm_len;
  102. unsigned long first_bit, zero_bit, val;
  103. int ret;
  104. ret = kstrtoul(buf, 16, &val);
  105. if (ret) {
  106. rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
  107. return false;
  108. }
  109. if ((r->cache.min_cbm_bits > 0 && val == 0) || val > supported_bits) {
  110. rdt_last_cmd_puts("Mask out of range\n");
  111. return false;
  112. }
  113. first_bit = find_first_bit(&val, cbm_len);
  114. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  115. /* Are non-contiguous bitmasks allowed? */
  116. if (!r->cache.arch_has_sparse_bitmasks &&
  117. (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)) {
  118. rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
  119. return false;
  120. }
  121. if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
  122. rdt_last_cmd_printf("Need at least %d bits in the mask\n",
  123. r->cache.min_cbm_bits);
  124. return false;
  125. }
  126. *data = val;
  127. return true;
  128. }
  129. /*
  130. * Read one cache bit mask (hex). Check that it is valid for the current
  131. * resource type.
  132. */
  133. static int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
  134. struct rdt_ctrl_domain *d)
  135. {
  136. enum rdtgrp_mode mode = data->mode;
  137. struct resctrl_staged_config *cfg;
  138. struct rdt_resource *r = s->res;
  139. u32 closid = data->closid;
  140. u32 cbm_val;
  141. cfg = &d->staged_config[s->conf_type];
  142. if (cfg->have_new_ctrl) {
  143. rdt_last_cmd_printf("Duplicate domain %d\n", d->hdr.id);
  144. return -EINVAL;
  145. }
  146. /*
  147. * Cannot set up more than one pseudo-locked region in a cache
  148. * hierarchy.
  149. */
  150. if (mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  151. rdtgroup_pseudo_locked_in_hierarchy(d)) {
  152. rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
  153. return -EINVAL;
  154. }
  155. if (!cbm_validate(data->buf, &cbm_val, r))
  156. return -EINVAL;
  157. if ((mode == RDT_MODE_EXCLUSIVE || mode == RDT_MODE_SHAREABLE) &&
  158. rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
  159. rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
  160. return -EINVAL;
  161. }
  162. /*
  163. * The CBM may not overlap with the CBM of another closid if
  164. * either is exclusive.
  165. */
  166. if (rdtgroup_cbm_overlaps(s, d, cbm_val, closid, true)) {
  167. rdt_last_cmd_puts("Overlaps with exclusive group\n");
  168. return -EINVAL;
  169. }
  170. if (rdtgroup_cbm_overlaps(s, d, cbm_val, closid, false)) {
  171. if (mode == RDT_MODE_EXCLUSIVE ||
  172. mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  173. rdt_last_cmd_puts("Overlaps with other group\n");
  174. return -EINVAL;
  175. }
  176. }
  177. cfg->new_ctrl = cbm_val;
  178. cfg->have_new_ctrl = true;
  179. return 0;
  180. }
  181. /*
  182. * For each domain in this resource we expect to find a series of:
  183. * id=mask
  184. * separated by ";". The "id" is in decimal, and must match one of
  185. * the "id"s for this resource.
  186. */
  187. static int parse_line(char *line, struct resctrl_schema *s,
  188. struct rdtgroup *rdtgrp)
  189. {
  190. enum resctrl_conf_type t = s->conf_type;
  191. ctrlval_parser_t *parse_ctrlval = NULL;
  192. struct resctrl_staged_config *cfg;
  193. struct rdt_resource *r = s->res;
  194. struct rdt_parse_data data;
  195. struct rdt_ctrl_domain *d;
  196. char *dom = NULL, *id;
  197. unsigned long dom_id;
  198. /* Walking r->domains, ensure it can't race with cpuhp */
  199. lockdep_assert_cpus_held();
  200. switch (r->schema_fmt) {
  201. case RESCTRL_SCHEMA_BITMAP:
  202. parse_ctrlval = &parse_cbm;
  203. break;
  204. case RESCTRL_SCHEMA_RANGE:
  205. parse_ctrlval = &parse_bw;
  206. break;
  207. }
  208. if (WARN_ON_ONCE(!parse_ctrlval))
  209. return -EINVAL;
  210. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  211. (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)) {
  212. rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
  213. return -EINVAL;
  214. }
  215. next:
  216. if (!line || line[0] == '\0')
  217. return 0;
  218. dom = strsep(&line, ";");
  219. id = strsep(&dom, "=");
  220. if (!dom || kstrtoul(id, 10, &dom_id)) {
  221. rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
  222. return -EINVAL;
  223. }
  224. dom = strim(dom);
  225. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  226. if (d->hdr.id == dom_id) {
  227. data.buf = dom;
  228. data.closid = rdtgrp->closid;
  229. data.mode = rdtgrp->mode;
  230. if (parse_ctrlval(&data, s, d))
  231. return -EINVAL;
  232. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  233. cfg = &d->staged_config[t];
  234. /*
  235. * In pseudo-locking setup mode and just
  236. * parsed a valid CBM that should be
  237. * pseudo-locked. Only one locked region per
  238. * resource group and domain so just do
  239. * the required initialization for single
  240. * region and return.
  241. */
  242. rdtgrp->plr->s = s;
  243. rdtgrp->plr->d = d;
  244. rdtgrp->plr->cbm = cfg->new_ctrl;
  245. d->plr = rdtgrp->plr;
  246. return 0;
  247. }
  248. goto next;
  249. }
  250. }
  251. return -EINVAL;
  252. }
  253. static int rdtgroup_parse_resource(char *resname, char *tok,
  254. struct rdtgroup *rdtgrp)
  255. {
  256. struct resctrl_schema *s;
  257. list_for_each_entry(s, &resctrl_schema_all, list) {
  258. if (!strcmp(resname, s->name) && rdtgrp->closid < s->num_closid)
  259. return parse_line(tok, s, rdtgrp);
  260. }
  261. rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
  262. return -EINVAL;
  263. }
  264. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  265. char *buf, size_t nbytes, loff_t off)
  266. {
  267. struct resctrl_schema *s;
  268. struct rdtgroup *rdtgrp;
  269. struct rdt_resource *r;
  270. char *tok, *resname;
  271. int ret = 0;
  272. /* Valid input requires a trailing newline */
  273. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  274. return -EINVAL;
  275. buf[nbytes - 1] = '\0';
  276. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  277. if (!rdtgrp) {
  278. rdtgroup_kn_unlock(of->kn);
  279. return -ENOENT;
  280. }
  281. rdt_last_cmd_clear();
  282. /*
  283. * No changes to pseudo-locked region allowed. It has to be removed
  284. * and re-created instead.
  285. */
  286. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  287. ret = -EINVAL;
  288. rdt_last_cmd_puts("Resource group is pseudo-locked\n");
  289. goto out;
  290. }
  291. rdt_staged_configs_clear();
  292. while ((tok = strsep(&buf, "\n")) != NULL) {
  293. resname = strim(strsep(&tok, ":"));
  294. if (!tok) {
  295. rdt_last_cmd_puts("Missing ':'\n");
  296. ret = -EINVAL;
  297. goto out;
  298. }
  299. if (tok[0] == '\0') {
  300. rdt_last_cmd_printf("Missing '%s' value\n", resname);
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
  305. if (ret)
  306. goto out;
  307. }
  308. list_for_each_entry(s, &resctrl_schema_all, list) {
  309. r = s->res;
  310. /*
  311. * Writes to mba_sc resources update the software controller,
  312. * not the control MSR.
  313. */
  314. if (is_mba_sc(r))
  315. continue;
  316. ret = resctrl_arch_update_domains(r, rdtgrp->closid);
  317. if (ret)
  318. goto out;
  319. }
  320. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  321. /*
  322. * If pseudo-locking fails we keep the resource group in
  323. * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
  324. * active and updated for just the domain the pseudo-locked
  325. * region was requested for.
  326. */
  327. ret = rdtgroup_pseudo_lock_create(rdtgrp);
  328. }
  329. out:
  330. rdt_staged_configs_clear();
  331. rdtgroup_kn_unlock(of->kn);
  332. return ret ?: nbytes;
  333. }
  334. static void show_doms(struct seq_file *s, struct resctrl_schema *schema,
  335. char *resource_name, int closid)
  336. {
  337. struct rdt_resource *r = schema->res;
  338. struct rdt_ctrl_domain *dom;
  339. bool sep = false;
  340. u32 ctrl_val;
  341. /* Walking r->domains, ensure it can't race with cpuhp */
  342. lockdep_assert_cpus_held();
  343. if (resource_name)
  344. seq_printf(s, "%*s:", max_name_width, resource_name);
  345. list_for_each_entry(dom, &r->ctrl_domains, hdr.list) {
  346. if (sep)
  347. seq_puts(s, ";");
  348. if (is_mba_sc(r))
  349. ctrl_val = dom->mbps_val[closid];
  350. else
  351. ctrl_val = resctrl_arch_get_config(r, dom, closid,
  352. schema->conf_type);
  353. seq_printf(s, schema->fmt_str, dom->hdr.id, ctrl_val);
  354. sep = true;
  355. }
  356. seq_puts(s, "\n");
  357. }
  358. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  359. struct seq_file *s, void *v)
  360. {
  361. struct resctrl_schema *schema;
  362. struct rdtgroup *rdtgrp;
  363. int ret = 0;
  364. u32 closid;
  365. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  366. if (rdtgrp) {
  367. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  368. list_for_each_entry(schema, &resctrl_schema_all, list) {
  369. seq_printf(s, "%s:uninitialized\n", schema->name);
  370. }
  371. } else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  372. if (!rdtgrp->plr->d) {
  373. rdt_last_cmd_clear();
  374. rdt_last_cmd_puts("Cache domain offline\n");
  375. ret = -ENODEV;
  376. } else {
  377. seq_printf(s, "%s:%d=%x\n",
  378. rdtgrp->plr->s->res->name,
  379. rdtgrp->plr->d->hdr.id,
  380. rdtgrp->plr->cbm);
  381. }
  382. } else {
  383. closid = rdtgrp->closid;
  384. list_for_each_entry(schema, &resctrl_schema_all, list) {
  385. if (closid < schema->num_closid)
  386. show_doms(s, schema, schema->name, closid);
  387. }
  388. }
  389. } else {
  390. ret = -ENOENT;
  391. }
  392. rdtgroup_kn_unlock(of->kn);
  393. return ret;
  394. }
  395. static int smp_mon_event_count(void *arg)
  396. {
  397. mon_event_count(arg);
  398. return 0;
  399. }
  400. ssize_t rdtgroup_mba_mbps_event_write(struct kernfs_open_file *of,
  401. char *buf, size_t nbytes, loff_t off)
  402. {
  403. struct rdtgroup *rdtgrp;
  404. int ret = 0;
  405. /* Valid input requires a trailing newline */
  406. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  407. return -EINVAL;
  408. buf[nbytes - 1] = '\0';
  409. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  410. if (!rdtgrp) {
  411. rdtgroup_kn_unlock(of->kn);
  412. return -ENOENT;
  413. }
  414. rdt_last_cmd_clear();
  415. if (!strcmp(buf, "mbm_local_bytes")) {
  416. if (resctrl_is_mon_event_enabled(QOS_L3_MBM_LOCAL_EVENT_ID))
  417. rdtgrp->mba_mbps_event = QOS_L3_MBM_LOCAL_EVENT_ID;
  418. else
  419. ret = -EINVAL;
  420. } else if (!strcmp(buf, "mbm_total_bytes")) {
  421. if (resctrl_is_mon_event_enabled(QOS_L3_MBM_TOTAL_EVENT_ID))
  422. rdtgrp->mba_mbps_event = QOS_L3_MBM_TOTAL_EVENT_ID;
  423. else
  424. ret = -EINVAL;
  425. } else {
  426. ret = -EINVAL;
  427. }
  428. if (ret)
  429. rdt_last_cmd_printf("Unsupported event id '%s'\n", buf);
  430. rdtgroup_kn_unlock(of->kn);
  431. return ret ?: nbytes;
  432. }
  433. int rdtgroup_mba_mbps_event_show(struct kernfs_open_file *of,
  434. struct seq_file *s, void *v)
  435. {
  436. struct rdtgroup *rdtgrp;
  437. int ret = 0;
  438. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  439. if (rdtgrp) {
  440. switch (rdtgrp->mba_mbps_event) {
  441. case QOS_L3_MBM_LOCAL_EVENT_ID:
  442. seq_puts(s, "mbm_local_bytes\n");
  443. break;
  444. case QOS_L3_MBM_TOTAL_EVENT_ID:
  445. seq_puts(s, "mbm_total_bytes\n");
  446. break;
  447. default:
  448. pr_warn_once("Bad event %d\n", rdtgrp->mba_mbps_event);
  449. ret = -EINVAL;
  450. break;
  451. }
  452. } else {
  453. ret = -ENOENT;
  454. }
  455. rdtgroup_kn_unlock(of->kn);
  456. return ret;
  457. }
  458. struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
  459. struct list_head **pos)
  460. {
  461. struct rdt_domain_hdr *d;
  462. struct list_head *l;
  463. list_for_each(l, h) {
  464. d = list_entry(l, struct rdt_domain_hdr, list);
  465. /* When id is found, return its domain. */
  466. if (id == d->id)
  467. return d;
  468. /* Stop searching when finding id's position in sorted list. */
  469. if (id < d->id)
  470. break;
  471. }
  472. if (pos)
  473. *pos = l;
  474. return NULL;
  475. }
  476. void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
  477. struct rdt_domain_hdr *hdr, struct rdtgroup *rdtgrp,
  478. cpumask_t *cpumask, struct mon_evt *evt, int first)
  479. {
  480. int cpu;
  481. /* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
  482. lockdep_assert_cpus_held();
  483. /*
  484. * Setup the parameters to pass to mon_event_count() to read the data.
  485. */
  486. rr->rgrp = rdtgrp;
  487. rr->evt = evt;
  488. rr->r = r;
  489. rr->hdr = hdr;
  490. rr->first = first;
  491. if (resctrl_arch_mbm_cntr_assign_enabled(r) &&
  492. resctrl_is_mbm_event(evt->evtid)) {
  493. rr->is_mbm_cntr = true;
  494. } else {
  495. rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, evt->evtid);
  496. if (IS_ERR(rr->arch_mon_ctx)) {
  497. rr->err = -EINVAL;
  498. return;
  499. }
  500. }
  501. if (evt->any_cpu) {
  502. mon_event_count(rr);
  503. goto out_ctx_free;
  504. }
  505. cpu = cpumask_any_housekeeping(cpumask, RESCTRL_PICK_ANY_CPU);
  506. /*
  507. * cpumask_any_housekeeping() prefers housekeeping CPUs, but
  508. * are all the CPUs nohz_full? If yes, pick a CPU to IPI.
  509. * MPAM's resctrl_arch_rmid_read() is unable to read the
  510. * counters on some platforms if its called in IRQ context.
  511. */
  512. if (tick_nohz_full_cpu(cpu))
  513. smp_call_function_any(cpumask, mon_event_count, rr, 1);
  514. else
  515. smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
  516. out_ctx_free:
  517. if (rr->arch_mon_ctx)
  518. resctrl_arch_mon_ctx_free(r, evt->evtid, rr->arch_mon_ctx);
  519. }
  520. /*
  521. * Decimal place precision to use for each number of fixed-point
  522. * binary bits computed from ceil(binary_bits * log10(2)) except
  523. * binary_bits == 0 which will print "value.0"
  524. */
  525. static const unsigned int decplaces[MAX_BINARY_BITS + 1] = {
  526. [0] = 1,
  527. [1] = 1,
  528. [2] = 1,
  529. [3] = 1,
  530. [4] = 2,
  531. [5] = 2,
  532. [6] = 2,
  533. [7] = 3,
  534. [8] = 3,
  535. [9] = 3,
  536. [10] = 4,
  537. [11] = 4,
  538. [12] = 4,
  539. [13] = 4,
  540. [14] = 5,
  541. [15] = 5,
  542. [16] = 5,
  543. [17] = 6,
  544. [18] = 6,
  545. [19] = 6,
  546. [20] = 7,
  547. [21] = 7,
  548. [22] = 7,
  549. [23] = 7,
  550. [24] = 8,
  551. [25] = 8,
  552. [26] = 8,
  553. [27] = 9
  554. };
  555. static void print_event_value(struct seq_file *m, unsigned int binary_bits, u64 val)
  556. {
  557. unsigned long long frac = 0;
  558. if (binary_bits) {
  559. /* Mask off the integer part of the fixed-point value. */
  560. frac = val & GENMASK_ULL(binary_bits - 1, 0);
  561. /*
  562. * Multiply by 10^{desired decimal places}. The integer part of
  563. * the fixed point value is now almost what is needed.
  564. */
  565. frac *= int_pow(10ull, decplaces[binary_bits]);
  566. /*
  567. * Round to nearest by adding a value that would be a "1" in the
  568. * binary_bits + 1 place. Integer part of fixed point value is
  569. * now the needed value.
  570. */
  571. frac += 1ull << (binary_bits - 1);
  572. /*
  573. * Extract the integer part of the value. This is the decimal
  574. * representation of the original fixed-point fractional value.
  575. */
  576. frac >>= binary_bits;
  577. }
  578. /*
  579. * "frac" is now in the range [0 .. 10^decplaces). I.e. string
  580. * representation will fit into chosen number of decimal places.
  581. */
  582. seq_printf(m, "%llu.%0*llu\n", val >> binary_bits, decplaces[binary_bits], frac);
  583. }
  584. int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  585. {
  586. struct kernfs_open_file *of = m->private;
  587. enum resctrl_res_level resid;
  588. struct rdt_domain_hdr *hdr;
  589. struct rmid_read rr = {0};
  590. struct rdtgroup *rdtgrp;
  591. int domid, cpu, ret = 0;
  592. struct rdt_resource *r;
  593. struct cacheinfo *ci;
  594. struct mon_evt *evt;
  595. struct mon_data *md;
  596. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  597. if (!rdtgrp) {
  598. ret = -ENOENT;
  599. goto out;
  600. }
  601. md = of->kn->priv;
  602. if (WARN_ON_ONCE(!md)) {
  603. ret = -EIO;
  604. goto out;
  605. }
  606. resid = md->rid;
  607. domid = md->domid;
  608. evt = md->evt;
  609. r = resctrl_arch_get_resource(resid);
  610. if (md->sum) {
  611. struct rdt_l3_mon_domain *d;
  612. if (WARN_ON_ONCE(resid != RDT_RESOURCE_L3)) {
  613. ret = -EINVAL;
  614. goto out;
  615. }
  616. /*
  617. * This file requires summing across all domains that share
  618. * the L3 cache id that was provided in the "domid" field of the
  619. * struct mon_data. Search all domains in the resource for
  620. * one that matches this cache id.
  621. */
  622. list_for_each_entry(d, &r->mon_domains, hdr.list) {
  623. if (d->ci_id == domid) {
  624. cpu = cpumask_any(&d->hdr.cpu_mask);
  625. ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
  626. if (!ci)
  627. continue;
  628. rr.ci = ci;
  629. mon_event_read(&rr, r, NULL, rdtgrp,
  630. &ci->shared_cpu_map, evt, false);
  631. goto checkresult;
  632. }
  633. }
  634. ret = -ENOENT;
  635. goto out;
  636. } else {
  637. /*
  638. * This file provides data from a single domain. Search
  639. * the resource to find the domain with "domid".
  640. */
  641. hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
  642. if (!hdr) {
  643. ret = -ENOENT;
  644. goto out;
  645. }
  646. mon_event_read(&rr, r, hdr, rdtgrp, &hdr->cpu_mask, evt, false);
  647. }
  648. checkresult:
  649. /*
  650. * -ENOENT is a special case, set only when "mbm_event" counter assignment
  651. * mode is enabled and no counter has been assigned.
  652. */
  653. if (rr.err == -EIO)
  654. seq_puts(m, "Error\n");
  655. else if (rr.err == -EINVAL)
  656. seq_puts(m, "Unavailable\n");
  657. else if (rr.err == -ENOENT)
  658. seq_puts(m, "Unassigned\n");
  659. else if (evt->is_floating_point)
  660. print_event_value(m, evt->binary_bits, rr.val);
  661. else
  662. seq_printf(m, "%llu\n", rr.val);
  663. out:
  664. rdtgroup_kn_unlock(of->kn);
  665. return ret;
  666. }
  667. int resctrl_io_alloc_show(struct kernfs_open_file *of, struct seq_file *seq, void *v)
  668. {
  669. struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
  670. struct rdt_resource *r = s->res;
  671. mutex_lock(&rdtgroup_mutex);
  672. if (r->cache.io_alloc_capable) {
  673. if (resctrl_arch_get_io_alloc_enabled(r))
  674. seq_puts(seq, "enabled\n");
  675. else
  676. seq_puts(seq, "disabled\n");
  677. } else {
  678. seq_puts(seq, "not supported\n");
  679. }
  680. mutex_unlock(&rdtgroup_mutex);
  681. return 0;
  682. }
  683. /*
  684. * resctrl_io_alloc_closid_supported() - io_alloc feature utilizes the
  685. * highest CLOSID value to direct I/O traffic. Ensure that io_alloc_closid
  686. * is in the supported range.
  687. */
  688. static bool resctrl_io_alloc_closid_supported(u32 io_alloc_closid)
  689. {
  690. return io_alloc_closid < closids_supported();
  691. }
  692. /*
  693. * Initialize io_alloc CLOSID cache resource CBM with all usable (shared
  694. * and unused) cache portions.
  695. */
  696. static int resctrl_io_alloc_init_cbm(struct resctrl_schema *s, u32 closid)
  697. {
  698. enum resctrl_conf_type peer_type;
  699. struct rdt_resource *r = s->res;
  700. struct rdt_ctrl_domain *d;
  701. int ret;
  702. rdt_staged_configs_clear();
  703. ret = rdtgroup_init_cat(s, closid);
  704. if (ret < 0)
  705. goto out;
  706. /* Keep CDP_CODE and CDP_DATA of io_alloc CLOSID's CBM in sync. */
  707. if (resctrl_arch_get_cdp_enabled(r->rid)) {
  708. peer_type = resctrl_peer_type(s->conf_type);
  709. list_for_each_entry(d, &s->res->ctrl_domains, hdr.list)
  710. memcpy(&d->staged_config[peer_type],
  711. &d->staged_config[s->conf_type],
  712. sizeof(d->staged_config[0]));
  713. }
  714. ret = resctrl_arch_update_domains(r, closid);
  715. out:
  716. rdt_staged_configs_clear();
  717. return ret;
  718. }
  719. /*
  720. * resctrl_io_alloc_closid() - io_alloc feature routes I/O traffic using
  721. * the highest available CLOSID. Retrieve the maximum CLOSID supported by the
  722. * resource. Note that if Code Data Prioritization (CDP) is enabled, the number
  723. * of available CLOSIDs is reduced by half.
  724. */
  725. u32 resctrl_io_alloc_closid(struct rdt_resource *r)
  726. {
  727. if (resctrl_arch_get_cdp_enabled(r->rid))
  728. return resctrl_arch_get_num_closid(r) / 2 - 1;
  729. else
  730. return resctrl_arch_get_num_closid(r) - 1;
  731. }
  732. ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
  733. size_t nbytes, loff_t off)
  734. {
  735. struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
  736. struct rdt_resource *r = s->res;
  737. char const *grp_name;
  738. u32 io_alloc_closid;
  739. bool enable;
  740. int ret;
  741. ret = kstrtobool(buf, &enable);
  742. if (ret)
  743. return ret;
  744. cpus_read_lock();
  745. mutex_lock(&rdtgroup_mutex);
  746. rdt_last_cmd_clear();
  747. if (!r->cache.io_alloc_capable) {
  748. rdt_last_cmd_printf("io_alloc is not supported on %s\n", s->name);
  749. ret = -ENODEV;
  750. goto out_unlock;
  751. }
  752. /* If the feature is already up to date, no action is needed. */
  753. if (resctrl_arch_get_io_alloc_enabled(r) == enable)
  754. goto out_unlock;
  755. io_alloc_closid = resctrl_io_alloc_closid(r);
  756. if (!resctrl_io_alloc_closid_supported(io_alloc_closid)) {
  757. rdt_last_cmd_printf("io_alloc CLOSID (ctrl_hw_id) %u is not available\n",
  758. io_alloc_closid);
  759. ret = -EINVAL;
  760. goto out_unlock;
  761. }
  762. if (enable) {
  763. if (!closid_alloc_fixed(io_alloc_closid)) {
  764. grp_name = rdtgroup_name_by_closid(io_alloc_closid);
  765. WARN_ON_ONCE(!grp_name);
  766. rdt_last_cmd_printf("CLOSID (ctrl_hw_id) %u for io_alloc is used by %s group\n",
  767. io_alloc_closid, grp_name ? grp_name : "another");
  768. ret = -ENOSPC;
  769. goto out_unlock;
  770. }
  771. ret = resctrl_io_alloc_init_cbm(s, io_alloc_closid);
  772. if (ret) {
  773. rdt_last_cmd_puts("Failed to initialize io_alloc allocations\n");
  774. closid_free(io_alloc_closid);
  775. goto out_unlock;
  776. }
  777. } else {
  778. closid_free(io_alloc_closid);
  779. }
  780. ret = resctrl_arch_io_alloc_enable(r, enable);
  781. if (enable && ret) {
  782. rdt_last_cmd_puts("Failed to enable io_alloc feature\n");
  783. closid_free(io_alloc_closid);
  784. }
  785. out_unlock:
  786. mutex_unlock(&rdtgroup_mutex);
  787. cpus_read_unlock();
  788. return ret ?: nbytes;
  789. }
  790. int resctrl_io_alloc_cbm_show(struct kernfs_open_file *of, struct seq_file *seq, void *v)
  791. {
  792. struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
  793. struct rdt_resource *r = s->res;
  794. int ret = 0;
  795. cpus_read_lock();
  796. mutex_lock(&rdtgroup_mutex);
  797. rdt_last_cmd_clear();
  798. if (!r->cache.io_alloc_capable) {
  799. rdt_last_cmd_printf("io_alloc is not supported on %s\n", s->name);
  800. ret = -ENODEV;
  801. goto out_unlock;
  802. }
  803. if (!resctrl_arch_get_io_alloc_enabled(r)) {
  804. rdt_last_cmd_printf("io_alloc is not enabled on %s\n", s->name);
  805. ret = -EINVAL;
  806. goto out_unlock;
  807. }
  808. /*
  809. * When CDP is enabled, the CBMs of the highest CLOSID of CDP_CODE and
  810. * CDP_DATA are kept in sync. As a result, the io_alloc CBMs shown for
  811. * either CDP resource are identical and accurately represent the CBMs
  812. * used for I/O.
  813. */
  814. show_doms(seq, s, NULL, resctrl_io_alloc_closid(r));
  815. out_unlock:
  816. mutex_unlock(&rdtgroup_mutex);
  817. cpus_read_unlock();
  818. return ret;
  819. }
  820. static int resctrl_io_alloc_parse_line(char *line, struct rdt_resource *r,
  821. struct resctrl_schema *s, u32 closid)
  822. {
  823. enum resctrl_conf_type peer_type;
  824. struct rdt_parse_data data;
  825. struct rdt_ctrl_domain *d;
  826. char *dom = NULL, *id;
  827. unsigned long dom_id;
  828. next:
  829. if (!line || line[0] == '\0')
  830. return 0;
  831. dom = strsep(&line, ";");
  832. id = strsep(&dom, "=");
  833. if (!dom || kstrtoul(id, 10, &dom_id)) {
  834. rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
  835. return -EINVAL;
  836. }
  837. dom = strim(dom);
  838. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  839. if (d->hdr.id == dom_id) {
  840. data.buf = dom;
  841. data.mode = RDT_MODE_SHAREABLE;
  842. data.closid = closid;
  843. if (parse_cbm(&data, s, d))
  844. return -EINVAL;
  845. /*
  846. * Keep io_alloc CLOSID's CBM of CDP_CODE and CDP_DATA
  847. * in sync.
  848. */
  849. if (resctrl_arch_get_cdp_enabled(r->rid)) {
  850. peer_type = resctrl_peer_type(s->conf_type);
  851. memcpy(&d->staged_config[peer_type],
  852. &d->staged_config[s->conf_type],
  853. sizeof(d->staged_config[0]));
  854. }
  855. goto next;
  856. }
  857. }
  858. return -EINVAL;
  859. }
  860. ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf,
  861. size_t nbytes, loff_t off)
  862. {
  863. struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
  864. struct rdt_resource *r = s->res;
  865. u32 io_alloc_closid;
  866. int ret = 0;
  867. /* Valid input requires a trailing newline */
  868. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  869. return -EINVAL;
  870. buf[nbytes - 1] = '\0';
  871. cpus_read_lock();
  872. mutex_lock(&rdtgroup_mutex);
  873. rdt_last_cmd_clear();
  874. if (!r->cache.io_alloc_capable) {
  875. rdt_last_cmd_printf("io_alloc is not supported on %s\n", s->name);
  876. ret = -ENODEV;
  877. goto out_unlock;
  878. }
  879. if (!resctrl_arch_get_io_alloc_enabled(r)) {
  880. rdt_last_cmd_printf("io_alloc is not enabled on %s\n", s->name);
  881. ret = -EINVAL;
  882. goto out_unlock;
  883. }
  884. io_alloc_closid = resctrl_io_alloc_closid(r);
  885. rdt_staged_configs_clear();
  886. ret = resctrl_io_alloc_parse_line(buf, r, s, io_alloc_closid);
  887. if (ret)
  888. goto out_clear_configs;
  889. ret = resctrl_arch_update_domains(r, io_alloc_closid);
  890. out_clear_configs:
  891. rdt_staged_configs_clear();
  892. out_unlock:
  893. mutex_unlock(&rdtgroup_mutex);
  894. cpus_read_unlock();
  895. return ret ?: nbytes;
  896. }