cacheinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * cacheinfo support - processor cache information via sysfs
  3. *
  4. * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
  5. * Author: Sudeep Holla <sudeep.holla@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/acpi.h>
  21. #include <linux/bitops.h>
  22. #include <linux/cacheinfo.h>
  23. #include <linux/compiler.h>
  24. #include <linux/cpu.h>
  25. #include <linux/device.h>
  26. #include <linux/init.h>
  27. #include <linux/of.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/smp.h>
  31. #include <linux/sysfs.h>
  32. /* pointer to per cpu cacheinfo */
  33. static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
  34. #define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu))
  35. #define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves)
  36. #define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list)
  37. struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
  38. {
  39. return ci_cacheinfo(cpu);
  40. }
  41. #ifdef CONFIG_OF
  42. static int cache_setup_of_node(unsigned int cpu)
  43. {
  44. struct device_node *np;
  45. struct cacheinfo *this_leaf;
  46. struct device *cpu_dev = get_cpu_device(cpu);
  47. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  48. unsigned int index = 0;
  49. /* skip if of_node is already populated */
  50. if (this_cpu_ci->info_list->of_node)
  51. return 0;
  52. if (!cpu_dev) {
  53. pr_err("No cpu device for CPU %d\n", cpu);
  54. return -ENODEV;
  55. }
  56. np = cpu_dev->of_node;
  57. if (!np) {
  58. pr_err("Failed to find cpu%d device node\n", cpu);
  59. return -ENOENT;
  60. }
  61. while (index < cache_leaves(cpu)) {
  62. this_leaf = this_cpu_ci->info_list + index;
  63. if (this_leaf->level != 1)
  64. np = of_find_next_cache_node(np);
  65. else
  66. np = of_node_get(np);/* cpu node itself */
  67. if (!np)
  68. break;
  69. this_leaf->of_node = np;
  70. index++;
  71. }
  72. if (index != cache_leaves(cpu)) /* not all OF nodes populated */
  73. return -ENOENT;
  74. return 0;
  75. }
  76. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  77. struct cacheinfo *sib_leaf)
  78. {
  79. return sib_leaf->of_node == this_leaf->of_node;
  80. }
  81. /* OF properties to query for a given cache type */
  82. struct cache_type_info {
  83. const char *size_prop;
  84. const char *line_size_props[2];
  85. const char *nr_sets_prop;
  86. };
  87. static const struct cache_type_info cache_type_info[] = {
  88. {
  89. .size_prop = "cache-size",
  90. .line_size_props = { "cache-line-size",
  91. "cache-block-size", },
  92. .nr_sets_prop = "cache-sets",
  93. }, {
  94. .size_prop = "i-cache-size",
  95. .line_size_props = { "i-cache-line-size",
  96. "i-cache-block-size", },
  97. .nr_sets_prop = "i-cache-sets",
  98. }, {
  99. .size_prop = "d-cache-size",
  100. .line_size_props = { "d-cache-line-size",
  101. "d-cache-block-size", },
  102. .nr_sets_prop = "d-cache-sets",
  103. },
  104. };
  105. static inline int get_cacheinfo_idx(enum cache_type type)
  106. {
  107. if (type == CACHE_TYPE_UNIFIED)
  108. return 0;
  109. return type;
  110. }
  111. static void cache_size(struct cacheinfo *this_leaf)
  112. {
  113. const char *propname;
  114. const __be32 *cache_size;
  115. int ct_idx;
  116. ct_idx = get_cacheinfo_idx(this_leaf->type);
  117. propname = cache_type_info[ct_idx].size_prop;
  118. cache_size = of_get_property(this_leaf->of_node, propname, NULL);
  119. if (cache_size)
  120. this_leaf->size = of_read_number(cache_size, 1);
  121. }
  122. /* not cache_line_size() because that's a macro in include/linux/cache.h */
  123. static void cache_get_line_size(struct cacheinfo *this_leaf)
  124. {
  125. const __be32 *line_size;
  126. int i, lim, ct_idx;
  127. ct_idx = get_cacheinfo_idx(this_leaf->type);
  128. lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
  129. for (i = 0; i < lim; i++) {
  130. const char *propname;
  131. propname = cache_type_info[ct_idx].line_size_props[i];
  132. line_size = of_get_property(this_leaf->of_node, propname, NULL);
  133. if (line_size)
  134. break;
  135. }
  136. if (line_size)
  137. this_leaf->coherency_line_size = of_read_number(line_size, 1);
  138. }
  139. static void cache_nr_sets(struct cacheinfo *this_leaf)
  140. {
  141. const char *propname;
  142. const __be32 *nr_sets;
  143. int ct_idx;
  144. ct_idx = get_cacheinfo_idx(this_leaf->type);
  145. propname = cache_type_info[ct_idx].nr_sets_prop;
  146. nr_sets = of_get_property(this_leaf->of_node, propname, NULL);
  147. if (nr_sets)
  148. this_leaf->number_of_sets = of_read_number(nr_sets, 1);
  149. }
  150. static void cache_associativity(struct cacheinfo *this_leaf)
  151. {
  152. unsigned int line_size = this_leaf->coherency_line_size;
  153. unsigned int nr_sets = this_leaf->number_of_sets;
  154. unsigned int size = this_leaf->size;
  155. /*
  156. * If the cache is fully associative, there is no need to
  157. * check the other properties.
  158. */
  159. if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
  160. this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
  161. }
  162. static void cache_of_override_properties(unsigned int cpu)
  163. {
  164. int index;
  165. struct cacheinfo *this_leaf;
  166. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  167. for (index = 0; index < cache_leaves(cpu); index++) {
  168. this_leaf = this_cpu_ci->info_list + index;
  169. cache_size(this_leaf);
  170. cache_get_line_size(this_leaf);
  171. cache_nr_sets(this_leaf);
  172. cache_associativity(this_leaf);
  173. }
  174. }
  175. #else
  176. static void cache_of_override_properties(unsigned int cpu) { }
  177. static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
  178. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  179. struct cacheinfo *sib_leaf)
  180. {
  181. /*
  182. * For non-DT systems, assume unique level 1 cache, system-wide
  183. * shared caches for all other levels. This will be used only if
  184. * arch specific code has not populated shared_cpu_map
  185. */
  186. return !(this_leaf->level == 1);
  187. }
  188. #endif
  189. static int cache_shared_cpu_map_setup(unsigned int cpu)
  190. {
  191. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  192. struct cacheinfo *this_leaf, *sib_leaf;
  193. unsigned int index;
  194. int ret = 0;
  195. if (this_cpu_ci->cpu_map_populated)
  196. return 0;
  197. if (of_have_populated_dt())
  198. ret = cache_setup_of_node(cpu);
  199. else if (!acpi_disabled)
  200. /* No cache property/hierarchy support yet in ACPI */
  201. ret = -ENOTSUPP;
  202. if (ret)
  203. return ret;
  204. for (index = 0; index < cache_leaves(cpu); index++) {
  205. unsigned int i;
  206. this_leaf = this_cpu_ci->info_list + index;
  207. /* skip if shared_cpu_map is already populated */
  208. if (!cpumask_empty(&this_leaf->shared_cpu_map))
  209. continue;
  210. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  211. for_each_online_cpu(i) {
  212. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  213. if (i == cpu || !sib_cpu_ci->info_list)
  214. continue;/* skip if itself or no cacheinfo */
  215. sib_leaf = sib_cpu_ci->info_list + index;
  216. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  217. cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
  218. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. static void cache_shared_cpu_map_remove(unsigned int cpu)
  225. {
  226. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  227. struct cacheinfo *this_leaf, *sib_leaf;
  228. unsigned int sibling, index;
  229. for (index = 0; index < cache_leaves(cpu); index++) {
  230. this_leaf = this_cpu_ci->info_list + index;
  231. for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
  232. struct cpu_cacheinfo *sib_cpu_ci;
  233. if (sibling == cpu) /* skip itself */
  234. continue;
  235. sib_cpu_ci = get_cpu_cacheinfo(sibling);
  236. if (!sib_cpu_ci->info_list)
  237. continue;
  238. sib_leaf = sib_cpu_ci->info_list + index;
  239. cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
  240. cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
  241. }
  242. of_node_put(this_leaf->of_node);
  243. }
  244. }
  245. static void cache_override_properties(unsigned int cpu)
  246. {
  247. if (of_have_populated_dt())
  248. return cache_of_override_properties(cpu);
  249. }
  250. static void free_cache_attributes(unsigned int cpu)
  251. {
  252. if (!per_cpu_cacheinfo(cpu))
  253. return;
  254. cache_shared_cpu_map_remove(cpu);
  255. kfree(per_cpu_cacheinfo(cpu));
  256. per_cpu_cacheinfo(cpu) = NULL;
  257. }
  258. int __weak init_cache_level(unsigned int cpu)
  259. {
  260. return -ENOENT;
  261. }
  262. int __weak populate_cache_leaves(unsigned int cpu)
  263. {
  264. return -ENOENT;
  265. }
  266. static int detect_cache_attributes(unsigned int cpu)
  267. {
  268. int ret;
  269. if (init_cache_level(cpu) || !cache_leaves(cpu))
  270. return -ENOENT;
  271. per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
  272. sizeof(struct cacheinfo), GFP_KERNEL);
  273. if (per_cpu_cacheinfo(cpu) == NULL)
  274. return -ENOMEM;
  275. ret = populate_cache_leaves(cpu);
  276. if (ret)
  277. goto free_ci;
  278. /*
  279. * For systems using DT for cache hierarchy, of_node and shared_cpu_map
  280. * will be set up here only if they are not populated already
  281. */
  282. ret = cache_shared_cpu_map_setup(cpu);
  283. if (ret) {
  284. pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
  285. goto free_ci;
  286. }
  287. cache_override_properties(cpu);
  288. return 0;
  289. free_ci:
  290. free_cache_attributes(cpu);
  291. return ret;
  292. }
  293. /* pointer to cpuX/cache device */
  294. static DEFINE_PER_CPU(struct device *, ci_cache_dev);
  295. #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
  296. static cpumask_t cache_dev_map;
  297. /* pointer to array of devices for cpuX/cache/indexY */
  298. static DEFINE_PER_CPU(struct device **, ci_index_dev);
  299. #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
  300. #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
  301. #define show_one(file_name, object) \
  302. static ssize_t file_name##_show(struct device *dev, \
  303. struct device_attribute *attr, char *buf) \
  304. { \
  305. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  306. return sprintf(buf, "%u\n", this_leaf->object); \
  307. }
  308. show_one(id, id);
  309. show_one(level, level);
  310. show_one(coherency_line_size, coherency_line_size);
  311. show_one(number_of_sets, number_of_sets);
  312. show_one(physical_line_partition, physical_line_partition);
  313. show_one(ways_of_associativity, ways_of_associativity);
  314. static ssize_t size_show(struct device *dev,
  315. struct device_attribute *attr, char *buf)
  316. {
  317. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  318. return sprintf(buf, "%uK\n", this_leaf->size >> 10);
  319. }
  320. static ssize_t shared_cpumap_show_func(struct device *dev, bool list, char *buf)
  321. {
  322. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  323. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  324. return cpumap_print_to_pagebuf(list, buf, mask);
  325. }
  326. static ssize_t shared_cpu_map_show(struct device *dev,
  327. struct device_attribute *attr, char *buf)
  328. {
  329. return shared_cpumap_show_func(dev, false, buf);
  330. }
  331. static ssize_t shared_cpu_list_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. return shared_cpumap_show_func(dev, true, buf);
  335. }
  336. static ssize_t type_show(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  340. switch (this_leaf->type) {
  341. case CACHE_TYPE_DATA:
  342. return sprintf(buf, "Data\n");
  343. case CACHE_TYPE_INST:
  344. return sprintf(buf, "Instruction\n");
  345. case CACHE_TYPE_UNIFIED:
  346. return sprintf(buf, "Unified\n");
  347. default:
  348. return -EINVAL;
  349. }
  350. }
  351. static ssize_t allocation_policy_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  355. unsigned int ci_attr = this_leaf->attributes;
  356. int n = 0;
  357. if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
  358. n = sprintf(buf, "ReadWriteAllocate\n");
  359. else if (ci_attr & CACHE_READ_ALLOCATE)
  360. n = sprintf(buf, "ReadAllocate\n");
  361. else if (ci_attr & CACHE_WRITE_ALLOCATE)
  362. n = sprintf(buf, "WriteAllocate\n");
  363. return n;
  364. }
  365. static ssize_t write_policy_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  369. unsigned int ci_attr = this_leaf->attributes;
  370. int n = 0;
  371. if (ci_attr & CACHE_WRITE_THROUGH)
  372. n = sprintf(buf, "WriteThrough\n");
  373. else if (ci_attr & CACHE_WRITE_BACK)
  374. n = sprintf(buf, "WriteBack\n");
  375. return n;
  376. }
  377. static DEVICE_ATTR_RO(id);
  378. static DEVICE_ATTR_RO(level);
  379. static DEVICE_ATTR_RO(type);
  380. static DEVICE_ATTR_RO(coherency_line_size);
  381. static DEVICE_ATTR_RO(ways_of_associativity);
  382. static DEVICE_ATTR_RO(number_of_sets);
  383. static DEVICE_ATTR_RO(size);
  384. static DEVICE_ATTR_RO(allocation_policy);
  385. static DEVICE_ATTR_RO(write_policy);
  386. static DEVICE_ATTR_RO(shared_cpu_map);
  387. static DEVICE_ATTR_RO(shared_cpu_list);
  388. static DEVICE_ATTR_RO(physical_line_partition);
  389. static struct attribute *cache_default_attrs[] = {
  390. &dev_attr_id.attr,
  391. &dev_attr_type.attr,
  392. &dev_attr_level.attr,
  393. &dev_attr_shared_cpu_map.attr,
  394. &dev_attr_shared_cpu_list.attr,
  395. &dev_attr_coherency_line_size.attr,
  396. &dev_attr_ways_of_associativity.attr,
  397. &dev_attr_number_of_sets.attr,
  398. &dev_attr_size.attr,
  399. &dev_attr_allocation_policy.attr,
  400. &dev_attr_write_policy.attr,
  401. &dev_attr_physical_line_partition.attr,
  402. NULL
  403. };
  404. static umode_t
  405. cache_default_attrs_is_visible(struct kobject *kobj,
  406. struct attribute *attr, int unused)
  407. {
  408. struct device *dev = kobj_to_dev(kobj);
  409. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  410. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  411. umode_t mode = attr->mode;
  412. if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
  413. return mode;
  414. if ((attr == &dev_attr_type.attr) && this_leaf->type)
  415. return mode;
  416. if ((attr == &dev_attr_level.attr) && this_leaf->level)
  417. return mode;
  418. if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
  419. return mode;
  420. if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
  421. return mode;
  422. if ((attr == &dev_attr_coherency_line_size.attr) &&
  423. this_leaf->coherency_line_size)
  424. return mode;
  425. if ((attr == &dev_attr_ways_of_associativity.attr) &&
  426. this_leaf->size) /* allow 0 = full associativity */
  427. return mode;
  428. if ((attr == &dev_attr_number_of_sets.attr) &&
  429. this_leaf->number_of_sets)
  430. return mode;
  431. if ((attr == &dev_attr_size.attr) && this_leaf->size)
  432. return mode;
  433. if ((attr == &dev_attr_write_policy.attr) &&
  434. (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
  435. return mode;
  436. if ((attr == &dev_attr_allocation_policy.attr) &&
  437. (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
  438. return mode;
  439. if ((attr == &dev_attr_physical_line_partition.attr) &&
  440. this_leaf->physical_line_partition)
  441. return mode;
  442. return 0;
  443. }
  444. static const struct attribute_group cache_default_group = {
  445. .attrs = cache_default_attrs,
  446. .is_visible = cache_default_attrs_is_visible,
  447. };
  448. static const struct attribute_group *cache_default_groups[] = {
  449. &cache_default_group,
  450. NULL,
  451. };
  452. static const struct attribute_group *cache_private_groups[] = {
  453. &cache_default_group,
  454. NULL, /* Place holder for private group */
  455. NULL,
  456. };
  457. const struct attribute_group *
  458. __weak cache_get_priv_group(struct cacheinfo *this_leaf)
  459. {
  460. return NULL;
  461. }
  462. static const struct attribute_group **
  463. cache_get_attribute_groups(struct cacheinfo *this_leaf)
  464. {
  465. const struct attribute_group *priv_group =
  466. cache_get_priv_group(this_leaf);
  467. if (!priv_group)
  468. return cache_default_groups;
  469. if (!cache_private_groups[1])
  470. cache_private_groups[1] = priv_group;
  471. return cache_private_groups;
  472. }
  473. /* Add/Remove cache interface for CPU device */
  474. static void cpu_cache_sysfs_exit(unsigned int cpu)
  475. {
  476. int i;
  477. struct device *ci_dev;
  478. if (per_cpu_index_dev(cpu)) {
  479. for (i = 0; i < cache_leaves(cpu); i++) {
  480. ci_dev = per_cache_index_dev(cpu, i);
  481. if (!ci_dev)
  482. continue;
  483. device_unregister(ci_dev);
  484. }
  485. kfree(per_cpu_index_dev(cpu));
  486. per_cpu_index_dev(cpu) = NULL;
  487. }
  488. device_unregister(per_cpu_cache_dev(cpu));
  489. per_cpu_cache_dev(cpu) = NULL;
  490. }
  491. static int cpu_cache_sysfs_init(unsigned int cpu)
  492. {
  493. struct device *dev = get_cpu_device(cpu);
  494. if (per_cpu_cacheinfo(cpu) == NULL)
  495. return -ENOENT;
  496. per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
  497. if (IS_ERR(per_cpu_cache_dev(cpu)))
  498. return PTR_ERR(per_cpu_cache_dev(cpu));
  499. /* Allocate all required memory */
  500. per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
  501. sizeof(struct device *), GFP_KERNEL);
  502. if (unlikely(per_cpu_index_dev(cpu) == NULL))
  503. goto err_out;
  504. return 0;
  505. err_out:
  506. cpu_cache_sysfs_exit(cpu);
  507. return -ENOMEM;
  508. }
  509. static int cache_add_dev(unsigned int cpu)
  510. {
  511. unsigned int i;
  512. int rc;
  513. struct device *ci_dev, *parent;
  514. struct cacheinfo *this_leaf;
  515. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  516. const struct attribute_group **cache_groups;
  517. rc = cpu_cache_sysfs_init(cpu);
  518. if (unlikely(rc < 0))
  519. return rc;
  520. parent = per_cpu_cache_dev(cpu);
  521. for (i = 0; i < cache_leaves(cpu); i++) {
  522. this_leaf = this_cpu_ci->info_list + i;
  523. if (this_leaf->disable_sysfs)
  524. continue;
  525. cache_groups = cache_get_attribute_groups(this_leaf);
  526. ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
  527. "index%1u", i);
  528. if (IS_ERR(ci_dev)) {
  529. rc = PTR_ERR(ci_dev);
  530. goto err;
  531. }
  532. per_cache_index_dev(cpu, i) = ci_dev;
  533. }
  534. cpumask_set_cpu(cpu, &cache_dev_map);
  535. return 0;
  536. err:
  537. cpu_cache_sysfs_exit(cpu);
  538. return rc;
  539. }
  540. static int cacheinfo_cpu_online(unsigned int cpu)
  541. {
  542. int rc = detect_cache_attributes(cpu);
  543. if (rc)
  544. return rc;
  545. rc = cache_add_dev(cpu);
  546. if (rc)
  547. free_cache_attributes(cpu);
  548. return rc;
  549. }
  550. static int cacheinfo_cpu_pre_down(unsigned int cpu)
  551. {
  552. if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
  553. cpu_cache_sysfs_exit(cpu);
  554. free_cache_attributes(cpu);
  555. return 0;
  556. }
  557. static int __init cacheinfo_sysfs_init(void)
  558. {
  559. return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "base/cacheinfo:online",
  560. cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
  561. }
  562. device_initcall(cacheinfo_sysfs_init);