Skip to contentSkip to conversation

sysfs cache code rewrite

PATCH/RFC Nathan Lynch 2 participants 7 messages Dec 24, 2008 – Jan 7, 2009 PowerPC

lore
Share & tools
lore
#0 lore raw

The current code for providing processor cache information in sysfs has the following deficiencies:
- several complex functions that are hard to understand
- implicit recursion (cache_desc_release -> kobject_put -> cache_desc_release)
- explicit recursion (create_cache_index_info)
- use of two per-cpu arrays when one would suffice
- duplication of work on systems where CPUs share cache

Also, when I looked at implementing support for a shared_cpu_map attribute, it was pretty much impossible to handle hotplug without checking every single online CPU's cache_desc list and fixing things up... not that this is a hot path, but it would have introduced O(n^2)-ish behavior during boot. Addressing this involved rethinking the core data structures used, which didn't lend itself to an incremental approach.

This implementation maintains a "forest" (potentially more than one tree) of cache objects which reflects the system's cache topology. Cache objects are instantiated as needed as CPUs come online. A per-cpu array is used mainly for sysfs-related bookkeeping; the objects in the array just point to the appropriate points in the forest.

This maintains compatibility with the existing code and includes some enhancements:
- Implement the shared_cpu_map attribute, which is essential for enabling userspace to discover the system's overall cache topology.
- Use cache-block-size properties if cache-line-size is not available.

I chose to place this implementation in a new file since it would have roughly doubled the size of sysfs.c, which is already kind of messy.

---
 arch/powerpc/kernel/Makefile    |    2 +-
 arch/powerpc/kernel/cacheinfo.c |  837 +++++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/cacheinfo.h |    8 +
 arch/powerpc/kernel/sysfs.c     |  300 +--------------
 4 files changed, 850 insertions(+), 297 deletions(-)
 create mode 100644 arch/powerpc/kernel/cacheinfo.c
 create mode 100644 arch/powerpc/kernel/cacheinfo.h

I've tested this on various ppc64 systems, making sure that the cache attributes' output remain unchanged, as well as running cpu hotplug stress tests while concurrently accessing the cpu sysfs hierarchy.

arch/powerpc/kernel/Makefile +1 -1
index 089209a..330463d 100644
@@ -28,7 +28,7 @@ endif
 obj-y				:= cputable.o ptrace.o syscalls.o \
 				   irq.o align.o signal_32.o pmc.o vdso.o \
 				   init_task.o process.o systbl.o idle.o \
-				   signal.o sysfs.o
+				   signal.o sysfs.o cacheinfo.o
 obj-y				+= vdso32/
 obj-$(CONFIG_PPC64)		+= setup_64.o sys_ppc32.o \
 				   signal_64.o ptrace32.o \
arch/powerpc/kernel/cacheinfo.c new file +837 -0

837 lines not on this page · raw message

arch/powerpc/kernel/cacheinfo.h new file +8 -0
new file mode 100644
index 0000000..a7b74d3
@@ -0,0 +1,8 @@
+#ifndef _PPC_CACHEINFO_H
+#define _PPC_CACHEINFO_H
+
+/* These are just hooks for sysfs.c to use. */
+extern void cacheinfo_cpu_online(unsigned int cpu_id);
+extern void cacheinfo_cpu_offline(unsigned int cpu_id);
+
+#endif /* _PPC_CACHEINFO_H */
arch/powerpc/kernel/sysfs.c +4 -296

333 lines not on this page · raw message

Signature · 1 line
1.5.5
#1 lore raw

Please unsubscribe me from... wait, that's not what I meant to say.

Any thoughts or questions on this one? I posted this right before the holidays so my feelings aren't hurt by the lack of response (yet!)

To summarize why I think this should go in 2.6.29:
- Userspace will now be able to determine the true cache topology (the current code doesn't tell you which caches are shared between CPUs).
- More complete information will be presented on systems that use [di]-cache-block-size properties instead of [di]-cache-line-size.
- While overall LOC has increased, the documentation is better, and the functions are much shorter and less complex.

Quoted 46 lines · Nathan Lynch

The current code for providing processor cache information in sysfs has the following deficiencies:
- several complex functions that are hard to understand
- implicit recursion (cache_desc_release -> kobject_put -> cache_desc_release)
- explicit recursion (create_cache_index_info)
- use of two per-cpu arrays when one would suffice
- duplication of work on systems where CPUs share cache

Also, when I looked at implementing support for a shared_cpu_map attribute, it was pretty much impossible to handle hotplug without checking every single online CPU's cache_desc list and fixing things up... not that this is a hot path, but it would have introduced O(n^2)-ish behavior during boot. Addressing this involved rethinking the core data structures used, which didn't lend itself to an incremental approach.

This implementation maintains a "forest" (potentially more than one tree) of cache objects which reflects the system's cache topology. Cache objects are instantiated as needed as CPUs come online. A per-cpu array is used mainly for sysfs-related bookkeeping; the objects in the array just point to the appropriate points in the forest.

This maintains compatibility with the existing code and includes some enhancements:
- Implement the shared_cpu_map attribute, which is essential for enabling userspace to discover the system's overall cache topology.
- Use cache-block-size properties if cache-line-size is not available.

I chose to place this implementation in a new file since it would have roughly doubled the size of sysfs.c, which is already kind of messy.

---
 arch/powerpc/kernel/Makefile    |    2 +-
 arch/powerpc/kernel/cacheinfo.c |  837 +++++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/cacheinfo.h |    8 +
 arch/powerpc/kernel/sysfs.c     |  300 +--------------
 4 files changed, 850 insertions(+), 297 deletions(-)
 create mode 100644 arch/powerpc/kernel/cacheinfo.c
 create mode 100644 arch/powerpc/kernel/cacheinfo.h

I've tested this on various ppc64 systems, making sure that the cache attributes' output remain unchanged, as well as running cpu hotplug stress tests while concurrently accessing the cpu sysfs hierarchy.

#2 lore raw
Quoted 5 lines · Nathan Lynch

Please unsubscribe me from... wait, that's not what I meant to say.

Any thoughts or questions on this one? I posted this right before the holidays so my feelings aren't hurt by the lack of response (yet!)

Quoted 8 lines

To summarize why I think this should go in 2.6.29:
- Userspace will now be able to determine the true cache topology (the current code doesn't tell you which caches are shared between CPUs).
- More complete information will be presented on systems that use [di]-cache-block-size properties instead of [di]-cache-line-size.
- While overall LOC has increased, the documentation is better, and the functions are much shorter and less complex.

I'm pretty happy about it. However, it fails build when merged on top of linus current upstream due to some cpumask changes.

I don't know quite the detail of the new cpumask stuff ... It could be as simple as passing a pointer instead of the value in the cpumask_scnprintf call though...

Can you check and produce a new patch pls ?

Cheers,
Ben.

#3 lore raw
Quoted 4 lines

I don't know quite the detail of the new cpumask stuff ... It could be as simple as passing a pointer instead of the value in the cpumask_scnprintf call though...

Actually, I'll do more tests and if that ends up being the only needed change, I'll push your patch with that small change out to powerpc next tonight.

Cheers,
Ben.

#4 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 4 lines

I don't know quite the detail of the new cpumask stuff ... It could be as simple as passing a pointer instead of the value in the cpumask_scnprintf call though...

Actually, I'll do more tests and if that ends up being the only needed change, I'll push your patch with that small change out to powerpc next tonight.

Sorry about that, here's an incremental... let me know if you want the whole thing re-posted.

Thanks.

arch/powerpc/kernel/cacheinfo.c +1 -1
index f3e3ae3..b33f041 100644
@@ -611,7 +611,7 @@ static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *att
 	len = PAGE_SIZE - 2;
 
 	if (len > 1) {
-		n = cpumask_scnprintf(buf, len, cache->shared_cpu_map);
+		n = cpumask_scnprintf(buf, len, &cache->shared_cpu_map);
 		buf[n++] = '\n';
 		buf[n] = '\0';
 	}
#5 lore raw
Quoted 6 lines · Nathan Lynch
Quoted 5 lines · Benjamin Herrenschmidt
Quoted 4 lines

I don't know quite the detail of the new cpumask stuff ... It could be as simple as passing a pointer instead of the value in the cpumask_scnprintf call though...

Quoted 6 lines
Quoted 3 lines

Actually, I'll do more tests and if that ends up being the only needed change, I'll push your patch with that small change out to powerpc next tonight.

Sorry about that, here's an incremental... let me know if you want the whole thing re-posted.

Nah, that's fine. I did that exact change in the patch before putting in my tree. I haven't had a chance to test boot tho, did you ?

Cheers,
Ben.

Quoted 16 lines

Thanks.

arch/powerpc/kernel/cacheinfo.c +1 -1
index f3e3ae3..b33f041 100644
@@ -611,7 +611,7 @@ static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *att
 	len = PAGE_SIZE - 2;
 
 	if (len > 1) {
-		n = cpumask_scnprintf(buf, len, cache->shared_cpu_map);
+		n = cpumask_scnprintf(buf, len, &cache->shared_cpu_map);
 		buf[n++] = '\n';
 		buf[n] = '\0';
 	}
#6 lore raw
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 10 lines · Nathan Lynch
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 4 lines

I don't know quite the detail of the new cpumask stuff ... It could be as simple as passing a pointer instead of the value in the cpumask_scnprintf call though...

Actually, I'll do more tests and if that ends up being the only needed change, I'll push your patch with that small change out to powerpc next tonight.

Quoted 5 lines
Quoted 2 lines

Sorry about that, here's an incremental... let me know if you want the whole thing re-posted.

Nah, that's fine. I did that exact change in the patch before putting in my tree. I haven't had a chance to test boot tho, did you ?

I have now, on Power6, with latest Linus -git snapshot; comes up fine and the shared_cpu_map attrs display as expected. This one is in shared processor mode so we only have L1 information:

# uname -r
2.6.28-git9-autokern1
# grep -r . /sys/devices/system/cpu/cpu*/cache
/sys/devices/system/cpu/cpu0/cache/index0/type:Data
/sys/devices/system/cpu/cpu0/cache/index0/level:1
/sys/devices/system/cpu/cpu0/cache/index0/shared_cpu_map:00000000,00000000,00000000,00000003
/sys/devices/system/cpu/cpu0/cache/index0/size:64K
/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu0/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu0/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu0/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu0/cache/index1/level:1
/sys/devices/system/cpu/cpu0/cache/index1/shared_cpu_map:00000000,00000000,00000000,00000003
/sys/devices/system/cpu/cpu0/cache/index1/size:64K
/sys/devices/system/cpu/cpu0/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu0/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu0/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu1/cache/index0/type:Data
/sys/devices/system/cpu/cpu1/cache/index0/level:1
/sys/devices/system/cpu/cpu1/cache/index0/shared_cpu_map:00000000,00000000,00000000,00000003
/sys/devices/system/cpu/cpu1/cache/index0/size:64K
/sys/devices/system/cpu/cpu1/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu1/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu1/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu1/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu1/cache/index1/level:1
/sys/devices/system/cpu/cpu1/cache/index1/shared_cpu_map:00000000,00000000,00000000,00000003
/sys/devices/system/cpu/cpu1/cache/index1/size:64K
/sys/devices/system/cpu/cpu1/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu1/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu1/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu2/cache/index0/type:Data
/sys/devices/system/cpu/cpu2/cache/index0/level:1
/sys/devices/system/cpu/cpu2/cache/index0/shared_cpu_map:00000000,00000000,00000000,0000000c
/sys/devices/system/cpu/cpu2/cache/index0/size:64K
/sys/devices/system/cpu/cpu2/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu2/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu2/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu2/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu2/cache/index1/level:1
/sys/devices/system/cpu/cpu2/cache/index1/shared_cpu_map:00000000,00000000,00000000,0000000c
/sys/devices/system/cpu/cpu2/cache/index1/size:64K
/sys/devices/system/cpu/cpu2/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu2/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu2/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu3/cache/index0/type:Data
/sys/devices/system/cpu/cpu3/cache/index0/level:1
/sys/devices/system/cpu/cpu3/cache/index0/shared_cpu_map:00000000,00000000,00000000,0000000c
/sys/devices/system/cpu/cpu3/cache/index0/size:64K
/sys/devices/system/cpu/cpu3/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu3/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu3/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu3/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu3/cache/index1/level:1
/sys/devices/system/cpu/cpu3/cache/index1/shared_cpu_map:00000000,00000000,00000000,0000000c
/sys/devices/system/cpu/cpu3/cache/index1/size:64K
/sys/devices/system/cpu/cpu3/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu3/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu3/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu4/cache/index0/type:Data
/sys/devices/system/cpu/cpu4/cache/index0/level:1
/sys/devices/system/cpu/cpu4/cache/index0/shared_cpu_map:00000000,00000000,00000000,00000030
/sys/devices/system/cpu/cpu4/cache/index0/size:64K
/sys/devices/system/cpu/cpu4/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu4/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu4/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu4/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu4/cache/index1/level:1
/sys/devices/system/cpu/cpu4/cache/index1/shared_cpu_map:00000000,00000000,00000000,00000030
/sys/devices/system/cpu/cpu4/cache/index1/size:64K
/sys/devices/system/cpu/cpu4/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu4/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu4/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu5/cache/index0/type:Data
/sys/devices/system/cpu/cpu5/cache/index0/level:1
/sys/devices/system/cpu/cpu5/cache/index0/shared_cpu_map:00000000,00000000,00000000,00000030
/sys/devices/system/cpu/cpu5/cache/index0/size:64K
/sys/devices/system/cpu/cpu5/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu5/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu5/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu5/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu5/cache/index1/level:1
/sys/devices/system/cpu/cpu5/cache/index1/shared_cpu_map:00000000,00000000,00000000,00000030
/sys/devices/system/cpu/cpu5/cache/index1/size:64K
/sys/devices/system/cpu/cpu5/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu5/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu5/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu6/cache/index0/type:Data
/sys/devices/system/cpu/cpu6/cache/index0/level:1
/sys/devices/system/cpu/cpu6/cache/index0/shared_cpu_map:00000000,00000000,00000000,000000c0
/sys/devices/system/cpu/cpu6/cache/index0/size:64K
/sys/devices/system/cpu/cpu6/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu6/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu6/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu6/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu6/cache/index1/level:1
/sys/devices/system/cpu/cpu6/cache/index1/shared_cpu_map:00000000,00000000,00000000,000000c0
/sys/devices/system/cpu/cpu6/cache/index1/size:64K
/sys/devices/system/cpu/cpu6/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu6/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu6/cache/index1/ways_of_associativity:4
/sys/devices/system/cpu/cpu7/cache/index0/type:Data
/sys/devices/system/cpu/cpu7/cache/index0/level:1
/sys/devices/system/cpu/cpu7/cache/index0/shared_cpu_map:00000000,00000000,00000000,000000c0
/sys/devices/system/cpu/cpu7/cache/index0/size:64K
/sys/devices/system/cpu/cpu7/cache/index0/coherency_line_size:128
/sys/devices/system/cpu/cpu7/cache/index0/number_of_sets:128
/sys/devices/system/cpu/cpu7/cache/index0/ways_of_associativity:4
/sys/devices/system/cpu/cpu7/cache/index1/type:Instruction
/sys/devices/system/cpu/cpu7/cache/index1/level:1
/sys/devices/system/cpu/cpu7/cache/index1/shared_cpu_map:00000000,00000000,00000000,000000c0
/sys/devices/system/cpu/cpu7/cache/index1/size:64K
/sys/devices/system/cpu/cpu7/cache/index1/coherency_line_size:128
/sys/devices/system/cpu/cpu7/cache/index1/number_of_sets:128
/sys/devices/system/cpu/cpu7/cache/index1/ways_of_associativity:4