intel_rdt_schemata.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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->default_ctrl)
  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, struct rdt_domain *d)
  52. {
  53. unsigned long data;
  54. int ret;
  55. if (d->have_new_ctrl)
  56. return -EINVAL;
  57. ret = kstrtoul(buf, 16, &data);
  58. if (ret)
  59. return ret;
  60. if (!cbm_validate(data, r))
  61. return -EINVAL;
  62. d->new_ctrl = data;
  63. d->have_new_ctrl = true;
  64. return 0;
  65. }
  66. /*
  67. * For each domain in this resource we expect to find a series of:
  68. * id=mask
  69. * separated by ";". The "id" is in decimal, and must match one of
  70. * the "id"s for this resource.
  71. */
  72. static int parse_line(char *line, struct rdt_resource *r)
  73. {
  74. char *dom = NULL, *id;
  75. struct rdt_domain *d;
  76. unsigned long dom_id;
  77. next:
  78. if (!line || line[0] == '\0')
  79. return 0;
  80. dom = strsep(&line, ";");
  81. id = strsep(&dom, "=");
  82. if (!dom || kstrtoul(id, 10, &dom_id))
  83. return -EINVAL;
  84. list_for_each_entry(d, &r->domains, list) {
  85. if (d->id == dom_id) {
  86. if (parse_cbm(dom, r, d))
  87. return -EINVAL;
  88. goto next;
  89. }
  90. }
  91. return -EINVAL;
  92. }
  93. static int update_domains(struct rdt_resource *r, int closid)
  94. {
  95. struct msr_param msr_param;
  96. cpumask_var_t cpu_mask;
  97. struct rdt_domain *d;
  98. int cpu;
  99. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  100. return -ENOMEM;
  101. msr_param.low = closid;
  102. msr_param.high = msr_param.low + 1;
  103. msr_param.res = r;
  104. list_for_each_entry(d, &r->domains, list) {
  105. if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
  106. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  107. d->ctrl_val[closid] = d->new_ctrl;
  108. }
  109. }
  110. if (cpumask_empty(cpu_mask))
  111. goto done;
  112. cpu = get_cpu();
  113. /* Update CBM on this cpu if it's in cpu_mask. */
  114. if (cpumask_test_cpu(cpu, cpu_mask))
  115. rdt_ctrl_update(&msr_param);
  116. /* Update CBM on other cpus. */
  117. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  118. put_cpu();
  119. done:
  120. free_cpumask_var(cpu_mask);
  121. return 0;
  122. }
  123. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  124. char *buf, size_t nbytes, loff_t off)
  125. {
  126. struct rdtgroup *rdtgrp;
  127. struct rdt_domain *dom;
  128. struct rdt_resource *r;
  129. char *tok, *resname;
  130. int closid, ret = 0;
  131. /* Valid input requires a trailing newline */
  132. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  133. return -EINVAL;
  134. buf[nbytes - 1] = '\0';
  135. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  136. if (!rdtgrp) {
  137. rdtgroup_kn_unlock(of->kn);
  138. return -ENOENT;
  139. }
  140. closid = rdtgrp->closid;
  141. for_each_enabled_rdt_resource(r)
  142. list_for_each_entry(dom, &r->domains, list)
  143. dom->have_new_ctrl = false;
  144. while ((tok = strsep(&buf, "\n")) != NULL) {
  145. resname = strsep(&tok, ":");
  146. if (!tok) {
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. for_each_enabled_rdt_resource(r) {
  151. if (!strcmp(resname, r->name) &&
  152. closid < r->num_closid) {
  153. ret = parse_line(tok, r);
  154. if (ret)
  155. goto out;
  156. break;
  157. }
  158. }
  159. if (!r->name) {
  160. ret = -EINVAL;
  161. goto out;
  162. }
  163. }
  164. for_each_enabled_rdt_resource(r) {
  165. ret = update_domains(r, closid);
  166. if (ret)
  167. goto out;
  168. }
  169. out:
  170. rdtgroup_kn_unlock(of->kn);
  171. return ret ?: nbytes;
  172. }
  173. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  174. {
  175. struct rdt_domain *dom;
  176. bool sep = false;
  177. seq_printf(s, "%*s:", max_name_width, r->name);
  178. list_for_each_entry(dom, &r->domains, list) {
  179. if (sep)
  180. seq_puts(s, ";");
  181. seq_printf(s, "%d=%0*x", dom->id, max_data_width,
  182. dom->ctrl_val[closid]);
  183. sep = true;
  184. }
  185. seq_puts(s, "\n");
  186. }
  187. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  188. struct seq_file *s, void *v)
  189. {
  190. struct rdtgroup *rdtgrp;
  191. struct rdt_resource *r;
  192. int closid, ret = 0;
  193. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  194. if (rdtgrp) {
  195. closid = rdtgrp->closid;
  196. for_each_enabled_rdt_resource(r) {
  197. if (closid < r->num_closid)
  198. show_doms(s, r, closid);
  199. }
  200. } else {
  201. ret = -ENOENT;
  202. }
  203. rdtgroup_kn_unlock(of->kn);
  204. return ret;
  205. }