public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
@ 2025-11-24 16:48 Alexandre Chartre
  2025-11-24 19:35 ` Peter Zijlstra
  2025-11-25 13:29 ` David Laight
  0 siblings, 2 replies; 8+ messages in thread
From: Alexandre Chartre @ 2025-11-24 16:48 UTC (permalink / raw)
  To: linux-kernel, mingo, jpoimboe, peterz, david.laight.linux
  Cc: alexandre.chartre

Add a function to get the name of a CPU feature. The function is
architecture dependent and currently only implemented for x86. The
feature names are automatically generated from the cpufeatures.h
include file.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
---
 .../x86/tools/gen-cpu-feature-names-x86.awk   | 34 +++++++++++++++++++
 tools/objtool/.gitignore                      |  1 +
 tools/objtool/Makefile                        |  1 +
 tools/objtool/arch/loongarch/special.c        |  5 +++
 tools/objtool/arch/powerpc/special.c          |  5 +++
 tools/objtool/arch/x86/Build                  | 10 ++++++
 tools/objtool/arch/x86/special.c              | 10 ++++++
 tools/objtool/include/objtool/special.h       |  2 ++
 8 files changed, 68 insertions(+)
 create mode 100644 tools/arch/x86/tools/gen-cpu-feature-names-x86.awk

diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
new file mode 100644
index 0000000000000..cc4c7a3e6c2e2
--- /dev/null
+++ b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
@@ -0,0 +1,34 @@
+#!/bin/awk -f
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (c) 2025, Oracle and/or its affiliates.
+#
+# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
+#
+
+BEGIN {
+	print "/* cpu feature name array generated from cpufeatures.h */"
+	print "/* Do not change this code. */"
+	print
+	print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"
+
+	value_expr = "\\([0-9*+ ]+\\)"
+}
+
+/^#define X86_FEATURE_/ {
+	if (match($0, value_expr)) {
+		value = substr($0, RSTART + 1, RLENGTH - 2)
+		print "\t[" value "] = \"" $2 "\","
+	}
+}
+
+/^#define X86_BUG_/ {
+	if (match($0, value_expr)) {
+		value = substr($0, RSTART + 1, RLENGTH - 2)
+		print "\t[NCAPINTS*32+(" value ")] = \"" $2 "\","
+	}
+}
+
+END {
+	print "};"
+}
diff --git a/tools/objtool/.gitignore b/tools/objtool/.gitignore
index 759303657bd7c..73d883128511f 100644
--- a/tools/objtool/.gitignore
+++ b/tools/objtool/.gitignore
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
+arch/x86/lib/cpu-feature-names.c
 arch/x86/lib/inat-tables.c
 /objtool
 feature
diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
index df793ca6fc1a1..66397d755fe4b 100644
--- a/tools/objtool/Makefile
+++ b/tools/objtool/Makefile
@@ -125,6 +125,7 @@ $(LIBSUBCMD)-clean:
 clean: $(LIBSUBCMD)-clean
 	$(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
 	$(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
+	$(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
 	$(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
 	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
 	$(Q)$(RM) -r -- $(OUTPUT)feature
diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index a80b75f7b061f..aba774109437f 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -194,3 +194,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
 
 	return rodata_reloc;
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+	return NULL;
+}
diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c
index 51610689abf72..8f9bf61ca0899 100644
--- a/tools/objtool/arch/powerpc/special.c
+++ b/tools/objtool/arch/powerpc/special.c
@@ -18,3 +18,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
 {
 	exit(-1);
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+	return NULL;
+}
diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
index 3dedb2fd8f3a0..b95448ee01ee4 100644
--- a/tools/objtool/arch/x86/Build
+++ b/tools/objtool/arch/x86/Build
@@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
 $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
 
 CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
+
+cpu_features = ../arch/x86/include/asm/cpufeatures.h
+cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
+
+$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
+	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
+
+$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
+
+CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib
diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
index 09300761f1085..e817a3fff4491 100644
--- a/tools/objtool/arch/x86/special.c
+++ b/tools/objtool/arch/x86/special.c
@@ -4,6 +4,10 @@
 #include <objtool/special.h>
 #include <objtool/builtin.h>
 #include <objtool/warn.h>
+#include <asm/cpufeatures.h>
+
+/* cpu feature name array generated from cpufeatures.h */
+#include "cpu-feature-names.c"
 
 void arch_handle_alternative(struct special_alt *alt)
 {
@@ -134,3 +138,9 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
 	*table_size = 0;
 	return rodata_reloc;
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+	return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
+		cpu_feature_names[feature_number] : NULL;
+}
diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
index b22410745e4a1..121c3761899c1 100644
--- a/tools/objtool/include/objtool/special.h
+++ b/tools/objtool/include/objtool/special.h
@@ -38,4 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
 struct reloc *arch_find_switch_table(struct objtool_file *file,
 				     struct instruction *insn,
 				     unsigned long *table_size);
+const char *arch_cpu_feature_name(int feature_number);
+
 #endif /* _SPECIAL_H */
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-24 16:48 [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature Alexandre Chartre
@ 2025-11-24 19:35 ` Peter Zijlstra
  2025-11-24 19:45   ` Peter Zijlstra
  2025-11-25  7:55   ` Alexandre Chartre
  2025-11-25 13:29 ` David Laight
  1 sibling, 2 replies; 8+ messages in thread
From: Peter Zijlstra @ 2025-11-24 19:35 UTC (permalink / raw)
  To: Alexandre Chartre; +Cc: linux-kernel, mingo, jpoimboe, david.laight.linux

On Mon, Nov 24, 2025 at 05:48:55PM +0100, Alexandre Chartre wrote:
> diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
> index 3dedb2fd8f3a0..b95448ee01ee4 100644
> --- a/tools/objtool/arch/x86/Build
> +++ b/tools/objtool/arch/x86/Build
> @@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
>  $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
>  
>  CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
> +
> +cpu_features = ../arch/x86/include/asm/cpufeatures.h
> +cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
> +
> +$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
> +	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
> +
> +$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
> +
> +CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib

If you'd been careful, you'd have taken the commit from tip and noticed
its Build fragment is slightly different :-)

You're now re-introducing a race in the build where if you build
special.o before decode.o the lib directory isn't yet created and it
goes sideways.

Anyway, let me fold that awk delta into what I have.

Thanks!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-24 19:35 ` Peter Zijlstra
@ 2025-11-24 19:45   ` Peter Zijlstra
  2025-11-25  7:55   ` Alexandre Chartre
  1 sibling, 0 replies; 8+ messages in thread
From: Peter Zijlstra @ 2025-11-24 19:45 UTC (permalink / raw)
  To: Alexandre Chartre; +Cc: linux-kernel, mingo, jpoimboe, david.laight.linux

On Mon, Nov 24, 2025 at 08:35:45PM +0100, Peter Zijlstra wrote:
> On Mon, Nov 24, 2025 at 05:48:55PM +0100, Alexandre Chartre wrote:
> > diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
> > index 3dedb2fd8f3a0..b95448ee01ee4 100644
> > --- a/tools/objtool/arch/x86/Build
> > +++ b/tools/objtool/arch/x86/Build
> > @@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
> >  $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
> >  
> >  CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
> > +
> > +cpu_features = ../arch/x86/include/asm/cpufeatures.h
> > +cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
> > +
> > +$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
> > +	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
> > +
> > +$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
> > +
> > +CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib
> 
> If you'd been careful, you'd have taken the commit from tip and noticed
> its Build fragment is slightly different :-)
> 
> You're now re-introducing a race in the build where if you build
> special.o before decode.o the lib directory isn't yet created and it
> goes sideways.
> 
> Anyway, let me fold that awk delta into what I have.

OK, pushed out a new tip/objtool/core, lets hope its all good now :-)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-24 19:35 ` Peter Zijlstra
  2025-11-24 19:45   ` Peter Zijlstra
@ 2025-11-25  7:55   ` Alexandre Chartre
  1 sibling, 0 replies; 8+ messages in thread
From: Alexandre Chartre @ 2025-11-25  7:55 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: alexandre.chartre, linux-kernel, mingo, jpoimboe,
	david.laight.linux


On 11/24/25 20:35, Peter Zijlstra wrote:
> On Mon, Nov 24, 2025 at 05:48:55PM +0100, Alexandre Chartre wrote:
>> diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
>> index 3dedb2fd8f3a0..b95448ee01ee4 100644
>> --- a/tools/objtool/arch/x86/Build
>> +++ b/tools/objtool/arch/x86/Build
>> @@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
>>   $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
>>   
>>   CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
>> +
>> +cpu_features = ../arch/x86/include/asm/cpufeatures.h
>> +cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
>> +
>> +$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
>> +	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
>> +
>> +$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
>> +
>> +CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib
> 
> If you'd been careful, you'd have taken the commit from tip and noticed
> its Build fragment is slightly different :-)
> 
> You're now re-introducing a race in the build where if you build
> special.o before decode.o the lib directory isn't yet created and it
> goes sideways.
> 
> Anyway, let me fold that awk delta into what I have.

Oups, sorry. I've checked tip but not closely enough :(

Thanks for handling it; that's a good lesson about the many different impacts
and dependencies.

alex.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-24 16:48 [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature Alexandre Chartre
  2025-11-24 19:35 ` Peter Zijlstra
@ 2025-11-25 13:29 ` David Laight
  2025-11-25 14:43   ` Alexandre Chartre
  1 sibling, 1 reply; 8+ messages in thread
From: David Laight @ 2025-11-25 13:29 UTC (permalink / raw)
  To: Alexandre Chartre; +Cc: linux-kernel, mingo, jpoimboe, peterz

On Mon, 24 Nov 2025 17:48:55 +0100
Alexandre Chartre <alexandre.chartre@oracle.com> wrote:

> Add a function to get the name of a CPU feature. The function is
> architecture dependent and currently only implemented for x86. The
> feature names are automatically generated from the cpufeatures.h
> include file.
> 
> Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
> Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
> ---
>  .../x86/tools/gen-cpu-feature-names-x86.awk   | 34 +++++++++++++++++++
>  tools/objtool/.gitignore                      |  1 +
>  tools/objtool/Makefile                        |  1 +
>  tools/objtool/arch/loongarch/special.c        |  5 +++
>  tools/objtool/arch/powerpc/special.c          |  5 +++
>  tools/objtool/arch/x86/Build                  | 10 ++++++
>  tools/objtool/arch/x86/special.c              | 10 ++++++
>  tools/objtool/include/objtool/special.h       |  2 ++
>  8 files changed, 68 insertions(+)
>  create mode 100644 tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> 
> diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> new file mode 100644
> index 0000000000000..cc4c7a3e6c2e2
> --- /dev/null
> +++ b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> @@ -0,0 +1,34 @@
> +#!/bin/awk -f
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# Copyright (c) 2025, Oracle and/or its affiliates.
> +#
> +# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
> +#
> +
> +BEGIN {
> +	print "/* cpu feature name array generated from cpufeatures.h */"
> +	print "/* Do not change this code. */"
> +	print
> +	print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"

Since this code is #include'd into a .c file the line above (and the final })
can be in that source file.
You only need to generate the initialisers here.

> +
> +	value_expr = "\\([0-9*+ ]+\\)"
> +}
> +
> +/^#define X86_FEATURE_/ {
> +	if (match($0, value_expr)) {
> +		value = substr($0, RSTART + 1, RLENGTH - 2)
> +		print "\t[" value "] = \"" $2 "\","
> +	}

Can't you just do:
	print "\t[" $2 "] = \"" $2 "\","

    David

> +}
> +
> +/^#define X86_BUG_/ {
> +	if (match($0, value_expr)) {
> +		value = substr($0, RSTART + 1, RLENGTH - 2)
> +		print "\t[NCAPINTS*32+(" value ")] = \"" $2 "\","
> +	}
> +}
> +
> +END {
> +	print "};"
> +}
> diff --git a/tools/objtool/.gitignore b/tools/objtool/.gitignore
> index 759303657bd7c..73d883128511f 100644
> --- a/tools/objtool/.gitignore
> +++ b/tools/objtool/.gitignore
> @@ -1,4 +1,5 @@
>  # SPDX-License-Identifier: GPL-2.0-only
> +arch/x86/lib/cpu-feature-names.c
>  arch/x86/lib/inat-tables.c
>  /objtool
>  feature
> diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
> index df793ca6fc1a1..66397d755fe4b 100644
> --- a/tools/objtool/Makefile
> +++ b/tools/objtool/Makefile
> @@ -125,6 +125,7 @@ $(LIBSUBCMD)-clean:
>  clean: $(LIBSUBCMD)-clean
>  	$(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
>  	$(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
> +	$(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
>  	$(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
>  	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
>  	$(Q)$(RM) -r -- $(OUTPUT)feature
> diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
> index a80b75f7b061f..aba774109437f 100644
> --- a/tools/objtool/arch/loongarch/special.c
> +++ b/tools/objtool/arch/loongarch/special.c
> @@ -194,3 +194,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>  
>  	return rodata_reloc;
>  }
> +
> +const char *arch_cpu_feature_name(int feature_number)
> +{
> +	return NULL;
> +}
> diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c
> index 51610689abf72..8f9bf61ca0899 100644
> --- a/tools/objtool/arch/powerpc/special.c
> +++ b/tools/objtool/arch/powerpc/special.c
> @@ -18,3 +18,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>  {
>  	exit(-1);
>  }
> +
> +const char *arch_cpu_feature_name(int feature_number)
> +{
> +	return NULL;
> +}
> diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
> index 3dedb2fd8f3a0..b95448ee01ee4 100644
> --- a/tools/objtool/arch/x86/Build
> +++ b/tools/objtool/arch/x86/Build
> @@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
>  $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
>  
>  CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
> +
> +cpu_features = ../arch/x86/include/asm/cpufeatures.h
> +cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
> +
> +$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
> +	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
> +
> +$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
> +
> +CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib
> diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
> index 09300761f1085..e817a3fff4491 100644
> --- a/tools/objtool/arch/x86/special.c
> +++ b/tools/objtool/arch/x86/special.c
> @@ -4,6 +4,10 @@
>  #include <objtool/special.h>
>  #include <objtool/builtin.h>
>  #include <objtool/warn.h>
> +#include <asm/cpufeatures.h>
> +
> +/* cpu feature name array generated from cpufeatures.h */
> +#include "cpu-feature-names.c"
>  
>  void arch_handle_alternative(struct special_alt *alt)
>  {
> @@ -134,3 +138,9 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>  	*table_size = 0;
>  	return rodata_reloc;
>  }
> +
> +const char *arch_cpu_feature_name(int feature_number)
> +{
> +	return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
> +		cpu_feature_names[feature_number] : NULL;
> +}
> diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
> index b22410745e4a1..121c3761899c1 100644
> --- a/tools/objtool/include/objtool/special.h
> +++ b/tools/objtool/include/objtool/special.h
> @@ -38,4 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
>  struct reloc *arch_find_switch_table(struct objtool_file *file,
>  				     struct instruction *insn,
>  				     unsigned long *table_size);
> +const char *arch_cpu_feature_name(int feature_number);
> +
>  #endif /* _SPECIAL_H */


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-25 13:29 ` David Laight
@ 2025-11-25 14:43   ` Alexandre Chartre
  2025-11-25 18:34     ` David Laight
  0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Chartre @ 2025-11-25 14:43 UTC (permalink / raw)
  To: David Laight; +Cc: alexandre.chartre, linux-kernel, mingo, jpoimboe, peterz


On 11/25/25 14:29, David Laight wrote:
> On Mon, 24 Nov 2025 17:48:55 +0100
> Alexandre Chartre <alexandre.chartre@oracle.com> wrote:
> 
>> Add a function to get the name of a CPU feature. The function is
>> architecture dependent and currently only implemented for x86. The
>> feature names are automatically generated from the cpufeatures.h
>> include file.
>>
>> Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
>> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
>> Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
>> Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
>> ---
>>   .../x86/tools/gen-cpu-feature-names-x86.awk   | 34 +++++++++++++++++++
>>   tools/objtool/.gitignore                      |  1 +
>>   tools/objtool/Makefile                        |  1 +
>>   tools/objtool/arch/loongarch/special.c        |  5 +++
>>   tools/objtool/arch/powerpc/special.c          |  5 +++
>>   tools/objtool/arch/x86/Build                  | 10 ++++++
>>   tools/objtool/arch/x86/special.c              | 10 ++++++
>>   tools/objtool/include/objtool/special.h       |  2 ++
>>   8 files changed, 68 insertions(+)
>>   create mode 100644 tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>>
>> diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>> new file mode 100644
>> index 0000000000000..cc4c7a3e6c2e2
>> --- /dev/null
>> +++ b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>> @@ -0,0 +1,34 @@
>> +#!/bin/awk -f
>> +# SPDX-License-Identifier: GPL-2.0
>> +#
>> +# Copyright (c) 2025, Oracle and/or its affiliates.
>> +#
>> +# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
>> +#
>> +
>> +BEGIN {
>> +	print "/* cpu feature name array generated from cpufeatures.h */"
>> +	print "/* Do not change this code. */"
>> +	print
>> +	print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"
> 
> Since this code is #include'd into a .c file the line above (and the final })
> can be in that source file.
> You only need to generate the initialisers here.

Sure, I can do that.

> 
>> +
>> +	value_expr = "\\([0-9*+ ]+\\)"
>> +}
>> +
>> +/^#define X86_FEATURE_/ {
>> +	if (match($0, value_expr)) {
>> +		value = substr($0, RSTART + 1, RLENGTH - 2)
>> +		print "\t[" value "] = \"" $2 "\","
>> +	}
> 
> Can't you just do:
> 	print "\t[" $2 "] = \"" $2 "\","


I presume you mean:  print "\t[" $3 "] = \"" $2 "\","

But this doesn't always work because the value we need can contain spaces
and then it will be split into several fields.

For example, this works with:

#define X86_FEATURE_RRSBA_CTRL          (11*32+11) /* RET prediction control */

But not with:

#define X86_FEATURE_APIC                ( 0*32+ 9) /* "apic" Onboard APIC */


alex.

> 
>> +}
>> +
>> +/^#define X86_BUG_/ {
>> +	if (match($0, value_expr)) {
>> +		value = substr($0, RSTART + 1, RLENGTH - 2)
>> +		print "\t[NCAPINTS*32+(" value ")] = \"" $2 "\","
>> +	}
>> +}
>> +
>> +END {
>> +	print "};"
>> +}
>> diff --git a/tools/objtool/.gitignore b/tools/objtool/.gitignore
>> index 759303657bd7c..73d883128511f 100644
>> --- a/tools/objtool/.gitignore
>> +++ b/tools/objtool/.gitignore
>> @@ -1,4 +1,5 @@
>>   # SPDX-License-Identifier: GPL-2.0-only
>> +arch/x86/lib/cpu-feature-names.c
>>   arch/x86/lib/inat-tables.c
>>   /objtool
>>   feature
>> diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
>> index df793ca6fc1a1..66397d755fe4b 100644
>> --- a/tools/objtool/Makefile
>> +++ b/tools/objtool/Makefile
>> @@ -125,6 +125,7 @@ $(LIBSUBCMD)-clean:
>>   clean: $(LIBSUBCMD)-clean
>>   	$(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
>>   	$(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
>> +	$(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
>>   	$(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
>>   	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
>>   	$(Q)$(RM) -r -- $(OUTPUT)feature
>> diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
>> index a80b75f7b061f..aba774109437f 100644
>> --- a/tools/objtool/arch/loongarch/special.c
>> +++ b/tools/objtool/arch/loongarch/special.c
>> @@ -194,3 +194,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>>   
>>   	return rodata_reloc;
>>   }
>> +
>> +const char *arch_cpu_feature_name(int feature_number)
>> +{
>> +	return NULL;
>> +}
>> diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c
>> index 51610689abf72..8f9bf61ca0899 100644
>> --- a/tools/objtool/arch/powerpc/special.c
>> +++ b/tools/objtool/arch/powerpc/special.c
>> @@ -18,3 +18,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>>   {
>>   	exit(-1);
>>   }
>> +
>> +const char *arch_cpu_feature_name(int feature_number)
>> +{
>> +	return NULL;
>> +}
>> diff --git a/tools/objtool/arch/x86/Build b/tools/objtool/arch/x86/Build
>> index 3dedb2fd8f3a0..b95448ee01ee4 100644
>> --- a/tools/objtool/arch/x86/Build
>> +++ b/tools/objtool/arch/x86/Build
>> @@ -12,3 +12,13 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
>>   $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
>>   
>>   CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
>> +
>> +cpu_features = ../arch/x86/include/asm/cpufeatures.h
>> +cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
>> +
>> +$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
>> +	$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
>> +
>> +$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
>> +
>> +CFLAGS_special.o := -I$(OUTPUT)arch/x86/lib
>> diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
>> index 09300761f1085..e817a3fff4491 100644
>> --- a/tools/objtool/arch/x86/special.c
>> +++ b/tools/objtool/arch/x86/special.c
>> @@ -4,6 +4,10 @@
>>   #include <objtool/special.h>
>>   #include <objtool/builtin.h>
>>   #include <objtool/warn.h>
>> +#include <asm/cpufeatures.h>
>> +
>> +/* cpu feature name array generated from cpufeatures.h */
>> +#include "cpu-feature-names.c"
>>   
>>   void arch_handle_alternative(struct special_alt *alt)
>>   {
>> @@ -134,3 +138,9 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
>>   	*table_size = 0;
>>   	return rodata_reloc;
>>   }
>> +
>> +const char *arch_cpu_feature_name(int feature_number)
>> +{
>> +	return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
>> +		cpu_feature_names[feature_number] : NULL;
>> +}
>> diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
>> index b22410745e4a1..121c3761899c1 100644
>> --- a/tools/objtool/include/objtool/special.h
>> +++ b/tools/objtool/include/objtool/special.h
>> @@ -38,4 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
>>   struct reloc *arch_find_switch_table(struct objtool_file *file,
>>   				     struct instruction *insn,
>>   				     unsigned long *table_size);
>> +const char *arch_cpu_feature_name(int feature_number);
>> +
>>   #endif /* _SPECIAL_H */
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-25 14:43   ` Alexandre Chartre
@ 2025-11-25 18:34     ` David Laight
  2025-11-25 19:36       ` Alexandre Chartre
  0 siblings, 1 reply; 8+ messages in thread
From: David Laight @ 2025-11-25 18:34 UTC (permalink / raw)
  To: Alexandre Chartre; +Cc: linux-kernel, mingo, jpoimboe, peterz

On Tue, 25 Nov 2025 15:43:10 +0100
Alexandre Chartre <alexandre.chartre@oracle.com> wrote:

> On 11/25/25 14:29, David Laight wrote:
> > On Mon, 24 Nov 2025 17:48:55 +0100
> > Alexandre Chartre <alexandre.chartre@oracle.com> wrote:
> >   
> >> Add a function to get the name of a CPU feature. The function is
> >> architecture dependent and currently only implemented for x86. The
> >> feature names are automatically generated from the cpufeatures.h
> >> include file.
> >>
> >> Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
> >> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> >> Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
> >> Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
> >> ---
> >>   .../x86/tools/gen-cpu-feature-names-x86.awk   | 34 +++++++++++++++++++
> >>   tools/objtool/.gitignore                      |  1 +
> >>   tools/objtool/Makefile                        |  1 +
> >>   tools/objtool/arch/loongarch/special.c        |  5 +++
> >>   tools/objtool/arch/powerpc/special.c          |  5 +++
> >>   tools/objtool/arch/x86/Build                  | 10 ++++++
> >>   tools/objtool/arch/x86/special.c              | 10 ++++++
> >>   tools/objtool/include/objtool/special.h       |  2 ++
> >>   8 files changed, 68 insertions(+)
> >>   create mode 100644 tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> >>
> >> diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> >> new file mode 100644
> >> index 0000000000000..cc4c7a3e6c2e2
> >> --- /dev/null
> >> +++ b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
> >> @@ -0,0 +1,34 @@
> >> +#!/bin/awk -f
> >> +# SPDX-License-Identifier: GPL-2.0
> >> +#
> >> +# Copyright (c) 2025, Oracle and/or its affiliates.
> >> +#
> >> +# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
> >> +#
> >> +
> >> +BEGIN {
> >> +	print "/* cpu feature name array generated from cpufeatures.h */"
> >> +	print "/* Do not change this code. */"
> >> +	print
> >> +	print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"  
> > 
> > Since this code is #include'd into a .c file the line above (and the final })
> > can be in that source file.
> > You only need to generate the initialisers here.  
> 
> Sure, I can do that.
> 
> >   
> >> +
> >> +	value_expr = "\\([0-9*+ ]+\\)"
> >> +}
> >> +
> >> +/^#define X86_FEATURE_/ {
> >> +	if (match($0, value_expr)) {
> >> +		value = substr($0, RSTART + 1, RLENGTH - 2)
> >> +		print "\t[" value "] = \"" $2 "\","
> >> +	}  
> > 
> > Can't you just do:
> > 	print "\t[" $2 "] = \"" $2 "\","  
> 
> 
> I presume you mean:  print "\t[" $3 "] = \"" $2 "\","

No $2 and $2, so it generates:
	[X86_FEATURE_RRSBA_CTRL] = "X86_FEATURE_RRSBA_CTRL",

All the #defines are defined - so you might as well use them.
I suggested this sed command that has the same effect:
	sed -n -E '/^#define (X86_(FEATURE|BUG)_([^	 ]*)).*/s//	[\1] = "\1",/p'
which can be put directly into the makefile.

It isn't as though the pattern match for lines has to be very selective.
It just needs to be 'good enough' for the current file.

	David

> 
> But this doesn't always work because the value we need can contain spaces
> and then it will be split into several fields.
> 
> For example, this works with:
> 
> #define X86_FEATURE_RRSBA_CTRL          (11*32+11) /* RET prediction control */
> 
> But not with:
> 
> #define X86_FEATURE_APIC                ( 0*32+ 9) /* "apic" Onboard APIC */
> 
> 
> alex.
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature
  2025-11-25 18:34     ` David Laight
@ 2025-11-25 19:36       ` Alexandre Chartre
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Chartre @ 2025-11-25 19:36 UTC (permalink / raw)
  To: David Laight; +Cc: alexandre.chartre, linux-kernel, mingo, jpoimboe, peterz


On 11/25/25 19:34, David Laight wrote:
> On Tue, 25 Nov 2025 15:43:10 +0100
> Alexandre Chartre <alexandre.chartre@oracle.com> wrote:
> 
>> On 11/25/25 14:29, David Laight wrote:
>>> On Mon, 24 Nov 2025 17:48:55 +0100
>>> Alexandre Chartre <alexandre.chartre@oracle.com> wrote:
>>>    
>>>> Add a function to get the name of a CPU feature. The function is
>>>> architecture dependent and currently only implemented for x86. The
>>>> feature names are automatically generated from the cpufeatures.h
>>>> include file.
>>>>
>>>> Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
>>>> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
>>>> Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
>>>> Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
>>>> ---
>>>>    .../x86/tools/gen-cpu-feature-names-x86.awk   | 34 +++++++++++++++++++
>>>>    tools/objtool/.gitignore                      |  1 +
>>>>    tools/objtool/Makefile                        |  1 +
>>>>    tools/objtool/arch/loongarch/special.c        |  5 +++
>>>>    tools/objtool/arch/powerpc/special.c          |  5 +++
>>>>    tools/objtool/arch/x86/Build                  | 10 ++++++
>>>>    tools/objtool/arch/x86/special.c              | 10 ++++++
>>>>    tools/objtool/include/objtool/special.h       |  2 ++
>>>>    8 files changed, 68 insertions(+)
>>>>    create mode 100644 tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>>>>
>>>> diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>>>> new file mode 100644
>>>> index 0000000000000..cc4c7a3e6c2e2
>>>> --- /dev/null
>>>> +++ b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
>>>> @@ -0,0 +1,34 @@
>>>> +#!/bin/awk -f
>>>> +# SPDX-License-Identifier: GPL-2.0
>>>> +#
>>>> +# Copyright (c) 2025, Oracle and/or its affiliates.
>>>> +#
>>>> +# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
>>>> +#
>>>> +
>>>> +BEGIN {
>>>> +	print "/* cpu feature name array generated from cpufeatures.h */"
>>>> +	print "/* Do not change this code. */"
>>>> +	print
>>>> +	print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"
>>>
>>> Since this code is #include'd into a .c file the line above (and the final })
>>> can be in that source file.
>>> You only need to generate the initialisers here.
>>
>> Sure, I can do that.
>>
>>>    
>>>> +
>>>> +	value_expr = "\\([0-9*+ ]+\\)"
>>>> +}
>>>> +
>>>> +/^#define X86_FEATURE_/ {
>>>> +	if (match($0, value_expr)) {
>>>> +		value = substr($0, RSTART + 1, RLENGTH - 2)
>>>> +		print "\t[" value "] = \"" $2 "\","
>>>> +	}
>>>
>>> Can't you just do:
>>> 	print "\t[" $2 "] = \"" $2 "\","
>>
>>
>> I presume you mean:  print "\t[" $3 "] = \"" $2 "\","
> 
> No $2 and $2, so it generates:
> 	[X86_FEATURE_RRSBA_CTRL] = "X86_FEATURE_RRSBA_CTRL",
> 
> All the #defines are defined - so you might as well use them.
> I suggested this sed command that has the same effect:
> 	sed -n -E '/^#define (X86_(FEATURE|BUG)_([^	 ]*)).*/s//	[\1] = "\1",/p'
> which can be put directly into the makefile.
> 
> It isn't as though the pattern match for lines has to be very selective.
> It just needs to be 'good enough' for the current file.
> 

Yes, right! I initially wanted to not have to include cpufeatures.h so I used
the X86_FEATURE_* values directly. Then I added X86_BUG_* which required NCAPINTS
and so cpufeatures.h.

So. of course, sed is good enough. I will simplify this.

Thanks,

alex.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-11-25 19:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-24 16:48 [PATCH v6 26/30 (mawk fix)] objtool: Add function to get the name of a CPU feature Alexandre Chartre
2025-11-24 19:35 ` Peter Zijlstra
2025-11-24 19:45   ` Peter Zijlstra
2025-11-25  7:55   ` Alexandre Chartre
2025-11-25 13:29 ` David Laight
2025-11-25 14:43   ` Alexandre Chartre
2025-11-25 18:34     ` David Laight
2025-11-25 19:36       ` Alexandre Chartre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox