Skip to contentSkip to conversation

checkstack: bug fix and minor improvements

PATCH Applied Heiko Carstens 1 participant 4 messages Nov 20, 2023 Kbuild

lore
Share & tools
lore
#0 lore raw

cover checkstack: bug fix and minor improvements

While trying to use the checkstack script to print the stack usage of all functions, I noticed that this is not possible without changing it. In addition the current output contains a bug, and the output can be improved.

Therefore fix the bug and improve the script a bit.

Heiko Carstens (3):
checkstack: fix printed address
checkstack: sort output by size and function name
checkstack: allow to pass MINSTACKSIZE parameter

 Makefile              |  6 ++++--
 scripts/checkstack.pl | 27 +++++++++++++++++++--------
 2 files changed, 23 insertions(+), 10 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/3 checkstack: fix printed address

All addresses printed by checkstack have an extra incorrect 0 appended at the end.

This was introduced with commit 677f1410e058 ("scripts/checkstack.pl: don't display $dre as different entity"): since then the address is taken from the line which contains the function name, instead of the line which contains stack consumption. E.g. on s390:

0000000000100a30 <do_one_initcall>:
...
100a44: e3 f0 ff 70 ff 71 lay %r15,-144(%r15)

So the used regex which matches spaces and hexadecimal numbers to extract an address now matches a different substring. Subsequently replacing spaces with 0 appends a zero at the and, instead of replacing leading spaces.

Fix this by using the proper regex, and simplify the code a bit.

Fixes 677f1410e058 ("scripts/checkstack.pl: don't display $dre as different entity")
---
 scripts/checkstack.pl | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
scripts/checkstack.pl +2 -6
index d83ba5d8f3f4..f27d552aec43 100755
@@ -138,15 +138,11 @@ $total_size = 0;
 while (my $line = <STDIN>) {
 	if ($line =~ m/$funcre/) {
 		$func = $1;
-		next if $line !~ m/^($xs*)/;
+		next if $line !~ m/^($x*)/;
 		if ($total_size > $min_stack) {
 			push @stack, "$intro$total_size\n";
 		}
-
-		$addr = $1;
-		$addr =~ s/ /0/g;
-		$addr = "0x$addr";
-
+		$addr = "0x$1";
 		$intro = "$addr $func [$file]:";
 		my $padlen = 56 - length($intro);
 		while ($padlen > 0) {
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/3 checkstack: sort output by size and function name

In reply to Heiko Carstens

Sort output by size and in addition by function name. This increases readability for cases where there are many functions with the same stack usage.

---
 scripts/checkstack.pl | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
scripts/checkstack.pl +17 -2
index f27d552aec43..13408714ba0f 100755
@@ -189,5 +189,20 @@ if ($total_size > $min_stack) {
 	push @stack, "$intro$total_size\n";
 }
 
-# Sort output by size (last field)
-print sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;
+# Sort output by size (last field) and function name if size is the same
+sub sort_lines {
+	my ($a, $b) = @_;
+
+	my $num_a = $1 if $a =~ /:\t*(\d+)$/;
+	my $num_b = $1 if $b =~ /:\t*(\d+)$/;
+	my $func_a = $1 if $a =~ / (.*):/;
+	my $func_b = $1 if $b =~ / (.*):/;
+
+	if ($num_a != $num_b) {
+		return $num_b <=> $num_a;
+	} else {
+		return $func_a cmp $func_b;
+	}
+}
+
+print sort { sort_lines($a, $b) } @stack;
Signature · 1 line
2.39.2
#3 lore raw

PATCH 3/3 checkstack: allow to pass MINSTACKSIZE parameter

In reply to Heiko Carstens

The checkstack script omits all functions with a stack usage of less than 100 bytes. However the script already has support for a parameter which allows to override the default, but it cannot be set with

$ make checkstack

Add a MINSTACKSIZE parameter which allows to change the default. This might be useful in order to print the stack usage of all functions, or only those with large stack usage:

$ make checkstack MINSTACKSIZE=0
$ make checkstack MINSTACKSIZE=800
---
 Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Makefile +4 -2
index 724c79bebe72..b90413e7e590 100644
@@ -1576,7 +1576,8 @@ help:
 	 echo  '                    (default: $(INSTALL_HDR_PATH))'; \
 	 echo  ''
 	@echo  'Static analysers:'
-	@echo  '  checkstack      - Generate a list of stack hogs'
+	@echo  '  checkstack      - Generate a list of stack hogs and consider all functions'
+	@echo  '                    with a stack size larger than MINSTACKSIZE (default: 100)'
 	@echo  '  versioncheck    - Sanity check on version.h usage'
 	@echo  '  includecheck    - Check for duplicate included header files'
 	@echo  '  export_report   - List the usages of all exported symbols'
@@ -2016,9 +2017,10 @@ CHECKSTACK_ARCH := $(SUBARCH)
 else
 CHECKSTACK_ARCH := $(ARCH)
 endif
+MINSTACKSIZE	?= 100
 checkstack:
 	$(OBJDUMP) -d vmlinux $$(find . -name '*.ko') | \
-	$(PERL) $(srctree)/scripts/checkstack.pl $(CHECKSTACK_ARCH)
+	$(PERL) $(srctree)/scripts/checkstack.pl $(CHECKSTACK_ARCH) $(MINSTACKSIZE)
 
 kernelrelease:
 	@$(filechk_kernel.release)
Signature · 1 line
2.39.2