cacheinfo.c 17 KB

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