intel_rdt_schemata.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Resource Director Technology(RDT)
  3. * - Cache Allocation code.
  4. *
  5. * Copyright (C) 2016 Intel Corporation
  6. *
  7. * Authors:
  8. * Fenghua Yu <fenghua.yu@intel.com>
  9. * Tony Luck <tony.luck@intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * More information about RDT be found in the Intel (R) x86 Architecture
  21. * Software Developer Manual June 2016, volume 3, section 17.17.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/kernfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/slab.h>
  27. #include <asm/intel_rdt.h>
  28. /*
  29. * Check whether a cache bit mask is valid. The SDM says:
  30. * Please note that all (and only) contiguous '1' combinations
  31. * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
  32. * Additionally Haswell requires at least two bits set.
  33. */
  34. static bool cbm_validate(unsigned long var, struct rdt_resource *r)
  35. {
  36. unsigned long first_bit, zero_bit;
  37. if (var == 0 || var > r->max_cbm)
  38. return false;
  39. first_bit = find_first_bit(&var, r->cbm_len);
  40. zero_bit = find_next_zero_bit(&var, r->cbm_len, first_bit);
  41. if (find_next_bit(&var, r->cbm_len, zero_bit) < r->cbm_len)
  42. return false;
  43. if ((zero_bit - first_bit) < r->min_cbm_bits)
  44. return false;
  45. return true;
  46. }
  47. /*
  48. * Read one cache bit mask (hex). Check that it is valid for the current
  49. * resource type.
  50. */
  51. static int parse_cbm(char *buf, struct rdt_resource *r)
  52. {
  53. unsigned long data;
  54. int ret;
  55. ret = kstrtoul(buf, 16, &data);
  56. if (ret)
  57. return ret;
  58. if (!cbm_validate(data, r))
  59. return -EINVAL;
  60. r->tmp_cbms[r->num_tmp_cbms++] = data;
  61. return 0;
  62. }
  63. /*
  64. * For each domain in this resource we expect to find a series of:
  65. * id=mask
  66. * separated by ";". The "id" is in decimal, and must appear in the
  67. * right order.
  68. */
  69. static int parse_line(char *line, struct rdt_resource *r)
  70. {
  71. char *dom = NULL, *id;
  72. struct rdt_domain *d;
  73. unsigned long dom_id;
  74. list_for_each_entry(d, &r->domains, list) {
  75. dom = strsep(&line, ";");
  76. if (!dom)
  77. return -EINVAL;
  78. id = strsep(&dom, "=");
  79. if (kstrtoul(id, 10, &dom_id) || dom_id != d->id)
  80. return -EINVAL;
  81. if (parse_cbm(dom, r))
  82. return -EINVAL;
  83. }
  84. /* Any garbage at the end of the line? */
  85. if (line && line[0])
  86. return -EINVAL;
  87. return 0;
  88. }
  89. static int update_domains(struct rdt_resource *r, int closid)
  90. {
  91. struct msr_param msr_param;
  92. cpumask_var_t cpu_mask;
  93. struct rdt_domain *d;
  94. int cpu, idx = 0;
  95. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  96. return -ENOMEM;
  97. msr_param.low = closid;
  98. msr_param.high = msr_param.low + 1;
  99. msr_param.res = r;
  100. list_for_each_entry(d, &r->domains, list) {
  101. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  102. d->cbm[msr_param.low] = r->tmp_cbms[idx++];
  103. }
  104. cpu = get_cpu();
  105. /* Update CBM on this cpu if it's in cpu_mask. */
  106. if (cpumask_test_cpu(cpu, cpu_mask))
  107. rdt_cbm_update(&msr_param);
  108. /* Update CBM on other cpus. */
  109. smp_call_function_many(cpu_mask, rdt_cbm_update, &msr_param, 1);
  110. put_cpu();
  111. free_cpumask_var(cpu_mask);
  112. return 0;
  113. }
  114. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  115. char *buf, size_t nbytes, loff_t off)
  116. {
  117. struct rdtgroup *rdtgrp;
  118. struct rdt_resource *r;
  119. char *tok, *resname;
  120. int closid, ret = 0;
  121. u32 *l3_cbms = NULL;
  122. /* Valid input requires a trailing newline */
  123. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  124. return -EINVAL;
  125. buf[nbytes - 1] = '\0';
  126. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  127. if (!rdtgrp) {
  128. rdtgroup_kn_unlock(of->kn);
  129. return -ENOENT;
  130. }
  131. closid = rdtgrp->closid;
  132. /* get scratch space to save all the masks while we validate input */
  133. for_each_enabled_rdt_resource(r) {
  134. r->tmp_cbms = kcalloc(r->num_domains, sizeof(*l3_cbms),
  135. GFP_KERNEL);
  136. if (!r->tmp_cbms) {
  137. ret = -ENOMEM;
  138. goto out;
  139. }
  140. r->num_tmp_cbms = 0;
  141. }
  142. while ((tok = strsep(&buf, "\n")) != NULL) {
  143. resname = strsep(&tok, ":");
  144. if (!tok) {
  145. ret = -EINVAL;
  146. goto out;
  147. }
  148. for_each_enabled_rdt_resource(r) {
  149. if (!strcmp(resname, r->name) &&
  150. closid < r->num_closid) {
  151. ret = parse_line(tok, r);
  152. if (ret)
  153. goto out;
  154. break;
  155. }
  156. }
  157. if (!r->name) {
  158. ret = -EINVAL;
  159. goto out;
  160. }
  161. }
  162. /* Did the parser find all the masks we need? */
  163. for_each_enabled_rdt_resource(r) {
  164. if (r->num_tmp_cbms != r->num_domains) {
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. }
  169. for_each_enabled_rdt_resource(r) {
  170. ret = update_domains(r, closid);
  171. if (ret)
  172. goto out;
  173. }
  174. out:
  175. rdtgroup_kn_unlock(of->kn);
  176. for_each_enabled_rdt_resource(r) {
  177. kfree(r->tmp_cbms);
  178. r->tmp_cbms = NULL;
  179. }
  180. return ret ?: nbytes;
  181. }
  182. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  183. {
  184. struct rdt_domain *dom;
  185. bool sep = false;
  186. seq_printf(s, "%s:", r->name);
  187. list_for_each_entry(dom, &r->domains, list) {
  188. if (sep)
  189. seq_puts(s, ";");
  190. seq_printf(s, "%d=%x", dom->id, dom->cbm[closid]);
  191. sep = true;
  192. }
  193. seq_puts(s, "\n");
  194. }
  195. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  196. struct seq_file *s, void *v)
  197. {
  198. struct rdtgroup *rdtgrp;
  199. struct rdt_resource *r;
  200. int closid, ret = 0;
  201. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  202. if (rdtgrp) {
  203. closid = rdtgrp->closid;
  204. for_each_enabled_rdt_resource(r) {
  205. if (closid < r->num_closid)
  206. show_doms(s, r, closid);
  207. }
  208. } else {
  209. ret = -ENOENT;
  210. }
  211. rdtgroup_kn_unlock(of->kn);
  212. return ret;
  213. }