cacheinfo.c 17 KB

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