Skip to contentSkip to conversation

Add XSAVE layout description to Core files for debuggers to support varying XSAVE layouts

PATCH Superseded Vignesh Balasubramanian 5 participants 15 messages May 7, 2024 – Jul 9, 2024 1 message not in the archive

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add XSAVE layout description to Core files for debuggers to support varying XSAVE layouts

This patch proposes to add an extra .note section in the corefile to dump the CPUID information of a machine. This is being done to solve the issue of tools like the debuggers having to deal with coredumps from machines with varying XSAVE layouts in spite of having the same XCR0 bits. The new proposed .note section, at this point, consists of an array of records containing the information of each extended feature that is present. This provides details about the offsets and the sizes of the various extended save state components of the machine where the application crash occurred. Requesting a review for this patch.

Please NOTE that this patch has to be applied on top of the patch (https://lore.kernel.org/lkml/[email protected]/T/).

Vignesh Balasubramanian (1):
x86/elf: Add a new .note section containing Xfeatures information to
x86 core files

 arch/x86/Kconfig             |   1 +
 arch/x86/include/asm/elf.h   |  34 +++++++++
 arch/x86/kernel/fpu/xstate.c | 141 +++++++++++++++++++++++++++++++++++
 fs/binfmt_elf.c              |   4 +-
 include/uapi/linux/elf.h     |   1 +
 5 files changed, 179 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/1 x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Add a new .note section containing type, size, offset and flags of every xfeature that is present.

This information will be used by the debuggers to understand the XSAVE layout of the machine where the core file is dumped, and to read XSAVE registers, especially during cross-platform debugging.

Some background:

The XSAVE layouts of modern AMD and Intel CPUs differ, especially since Memory Protection Keys and the AVX-512 features have been inculcated into the AMD CPUs.
This is since AMD never adopted (and hence never left room in the XSAVE layout for) the Intel MPX feature. Tools like GDB had assumed a fixed XSAVE layout matching that of Intel (based on the XCR0 mask).
Hence, the core dumps from AMD CPUs didn't match the known size for the XCR0 mask. This resulted in GDB and other tools not being able to access the values of the AVX-512 and PKRU registers on AMD CPUs.
To solve this, an interim solution has been accepted into GDB, and is already a part of GDB 14, thanks to these series of patches
[ https://sourceware.org/pipermail/gdb-patches/2023-March/198081.html ]. But this patch series depends on heuristics based on the total XSAVE register set size and the XCR0 mask to infer the layouts of the various register blocks for core dumps, and hence, is not a foolproof mechanism to determine the layout of the XSAVE area.

Hence this new core dump note has been proposed as a more sturdy mechanism to allow GDB/LLDB and other relevant tools to determine the layout of the XSAVE area of the machine where the corefile was dumped.
The new core dump note (which is being proposed as a per-process .note section), NT_X86_XSAVE_LAYOUT (0x205) contains an array of structures. Each structure describes an individual extended feature containing offset, size and flags (that is obtained through CPUID instruction) in a format roughly matching the follow C structure:

struct xfeat_component {
       u32 xfeat_type;
       u32 xfeat_sz;
       u32 xfeat_off;
       u32 xfeat_flags;
};

---
v1->v2: Removed kernel internal defn dependency, code improvements

 arch/x86/Kconfig             |   1 +
 arch/x86/include/asm/elf.h   |  34 +++++++++
 arch/x86/kernel/fpu/xstate.c | 141 +++++++++++++++++++++++++++++++++++
 fs/binfmt_elf.c              |   4 +-
 include/uapi/linux/elf.h     |   1 +
 5 files changed, 179 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -0
index 928820e61cb5..cc67daab3396 100644
@@ -105,6 +105,7 @@ config X86
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAS_ZONE_DMA_SET if EXPERT
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
+	select ARCH_HAVE_EXTRA_ELF_NOTES
 	select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
 	select ARCH_MIGHT_HAVE_ACPI_PDC		if ACPI
 	select ARCH_MIGHT_HAVE_PC_PARPORT
arch/x86/include/asm/elf.h +34 -0
index 1fb83d47711f..5952574db64b 100644
@@ -13,6 +13,40 @@
 #include <asm/auxvec.h>
 #include <asm/fsgsbase.h>
 
+struct xfeat_component {
+	u32 xfeat_type;
+	u32 xfeat_sz;
+	u32 xfeat_off;
+	u32 xfeat_flags;
+} __packed;
+
+_Static_assert(sizeof(struct xfeat_component)%4 == 0, "xfeat_component is not aligned");
+
+enum custom_feature {
+	FEATURE_XSAVE_FP = 0,
+	FEATURE_XSAVE_SSE = 1,
+	FEATURE_XSAVE_YMM = 2,
+	FEATURE_XSAVE_BNDREGS = 3,
+	FEATURE_XSAVE_BNDCSR = 4,
+	FEATURE_XSAVE_OPMASK = 5,
+	FEATURE_XSAVE_ZMM_Hi256 = 6,
+	FEATURE_XSAVE_Hi16_ZMM = 7,
+	FEATURE_XSAVE_PT = 8,
+	FEATURE_XSAVE_PKRU = 9,
+	FEATURE_XSAVE_PASID = 10,
+	FEATURE_XSAVE_CET_USER = 11,
+	FEATURE_XSAVE_CET_SHADOW_STACK = 12,
+	FEATURE_XSAVE_HDC = 13,
+	FEATURE_XSAVE_UINTR = 14,
+	FEATURE_XSAVE_LBR = 15,
+	FEATURE_XSAVE_HWP = 16,
+	FEATURE_XSAVE_XTILE_CFG = 17,
+	FEATURE_XSAVE_XTILE_DATA = 18,
+	FEATURE_MAX,
+	FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+	FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};
+
 typedef unsigned long elf_greg_t;
 
 #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t))
arch/x86/kernel/fpu/xstate.c +141 -0

156 lines not on this page · raw message

fs/binfmt_elf.c +2 -2
index 5397b552fbeb..833bcb7e957b 100644
@@ -2000,7 +2000,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	{
 		size_t sz = info.size;
 
-		/* For cell spufs */
+		/* For cell spufs and x86 xstate */
 		sz += elf_coredump_extra_notes_size();
 
 		phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
@@ -2064,7 +2064,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	if (!write_note_info(&info, cprm))
 		goto end_coredump;
 
-	/* For cell spufs */
+	/* For cell spufs and x86 xstate */
 	if (elf_coredump_extra_notes_write(cprm))
 		goto end_coredump;
 
include/uapi/linux/elf.h +1 -0
index b54b313bcf07..e30a9b47dc87 100644
@@ -411,6 +411,7 @@ typedef struct elf64_shdr {
 #define NT_X86_XSTATE	0x202		/* x86 extended state using xsave */
 /* Old binutils treats 0x203 as a CET state */
 #define NT_X86_SHSTK	0x204		/* x86 SHSTK state */
+#define NT_X86_XSAVE_LAYOUT	0x205	/* XSAVE layout description */
 #define NT_S390_HIGH_GPRS	0x300	/* s390 upper register halves */
 #define NT_S390_TIMER	0x301		/* s390 timer register */
 #define NT_S390_TODCMP	0x302		/* s390 TOD clock comparator register */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

kernel test robot noticed the following build warnings:

Hi Vignesh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on kees/for-next/execve]
[also build test WARNING on tip/x86/core kees/for-next/pstore kees/for-next/kspp linus/master v6.9-rc7 next-20240507] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vignesh-Balasubramanian/x86-elf-Add-a-new-note-section-containing-Xfeatures-information-to-x86-core-files/20240507-175615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/execve
patch link:    https://lore.kernel.org/r/20240507095330.2674-2-vigbalas%40amd.com
patch subject: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files
config: i386-buildonly-randconfig-006-20240508 (https://download.01.org/0day-ci/archive/20240508/[email protected]/config)
compiler: gcc-8 (Ubuntu 8.4.0-3ubuntu2) 8.4.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240508/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/fpu/xstate.c:91:19: warning: 'owner_name' defined but not used [-Wunused-const-variable=]
    static const char owner_name[] = "LINUX";
                      ^~~~~~~~~~
   In file included from include/linux/string.h:369,
                    from arch/x86/include/asm/page_32.h:18,
                    from arch/x86/include/asm/page.h:14,
                    from arch/x86/include/asm/processor.h:20,
                    from arch/x86/include/asm/timex.h:5,
                    from include/linux/timex.h:67,
                    from include/linux/time32.h:13,
                    from include/linux/time.h:60,
                    from include/linux/compat.h:10,
                    from arch/x86/kernel/fpu/xstate.c:8:
   In function 'fortify_memcpy_chk',
       inlined from 'membuf_write.isra.6' at include/linux/regset.h:42:3,
       inlined from '__copy_xstate_to_uabi_buf' at arch/x86/kernel/fpu/xstate.c:1049:2:
   include/linux/fortify-string.h:562:4: warning: call to '__read_overflow2_field' declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()?
       __read_overflow2_field(q_size_field, size);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/owner_name +91 arch/x86/kernel/fpu/xstate.c

    90	
  > 91	static const char owner_name[] = "LINUX";
    92	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

In reply to Vignesh Balasubramanian
kernel test robot noticed the following build warnings:

Hi Vignesh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on kees/for-next/execve]
[also build test WARNING on tip/x86/core kees/for-next/pstore kees/for-next/kspp linus/master v6.9-rc7 next-20240507] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vignesh-Balasubramanian/x86-elf-Add-a-new-note-section-containing-Xfeatures-information-to-x86-core-files/20240507-175615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/execve
patch link:    https://lore.kernel.org/r/20240507095330.2674-2-vigbalas%40amd.com
patch subject: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files
config: i386-randconfig-013-20240508 (https://download.01.org/0day-ci/archive/20240508/[email protected]/config)
compiler: clang version 18.1.4 (https://github.com/llvm/llvm-project e6c3289804a67ea0bb6a86fadbe454dd93b8d855)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240508/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/fpu/xstate.c:91:19: warning: unused variable 'owner_name' [-Wunused-const-variable]
      91 | static const char owner_name[] = "LINUX";
         |                   ^~~~~~~~~~

1 warning generated.

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for DRM_I915_DEBUG_GEM
Depends on [n]: HAS_IOMEM [=y] && DRM_I915 [=m] && EXPERT [=y] && DRM_I915_WERROR [=n] Selected by [m]:
- DRM_I915_DEBUG [=y] && HAS_IOMEM [=y] && DRM_I915 [=m] && EXPERT [=y] && !COMPILE_TEST [=n]

vim +/owner_name +91 arch/x86/kernel/fpu/xstate.c

    90	
  > 91	static const char owner_name[] = "LINUX";
    92	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

In reply to Vignesh Balasubramanian
Quoted 125 lines · Vignesh Balasubramanian

Add a new .note section containing type, size, offset and flags of every xfeature that is present.

This information will be used by the debuggers to understand the XSAVE layout of the machine where the core file is dumped, and to read XSAVE registers, especially during cross-platform debugging.

Some background:

The XSAVE layouts of modern AMD and Intel CPUs differ, especially since Memory Protection Keys and the AVX-512 features have been inculcated into the AMD CPUs.
This is since AMD never adopted (and hence never left room in the XSAVE layout for) the Intel MPX feature. Tools like GDB had assumed a fixed XSAVE layout matching that of Intel (based on the XCR0 mask).
Hence, the core dumps from AMD CPUs didn't match the known size for the XCR0 mask. This resulted in GDB and other tools not being able to access the values of the AVX-512 and PKRU registers on AMD CPUs.
To solve this, an interim solution has been accepted into GDB, and is already a part of GDB 14, thanks to these series of patches
[ https://sourceware.org/pipermail/gdb-patches/2023-March/198081.html ]. But this patch series depends on heuristics based on the total XSAVE register set size and the XCR0 mask to infer the layouts of the various register blocks for core dumps, and hence, is not a foolproof mechanism to determine the layout of the XSAVE area.

Hence this new core dump note has been proposed as a more sturdy mechanism to allow GDB/LLDB and other relevant tools to determine the layout of the XSAVE area of the machine where the corefile was dumped.
The new core dump note (which is being proposed as a per-process .note section), NT_X86_XSAVE_LAYOUT (0x205) contains an array of structures. Each structure describes an individual extended feature containing offset, size and flags (that is obtained through CPUID instruction) in a format roughly matching the follow C structure:

struct xfeat_component {
       u32 xfeat_type;
       u32 xfeat_sz;
       u32 xfeat_off;
       u32 xfeat_flags;
};

---
v1->v2: Removed kernel internal defn dependency, code improvements

 arch/x86/Kconfig             |   1 +
 arch/x86/include/asm/elf.h   |  34 +++++++++
 arch/x86/kernel/fpu/xstate.c | 141 +++++++++++++++++++++++++++++++++++
 fs/binfmt_elf.c              |   4 +-
 include/uapi/linux/elf.h     |   1 +
 5 files changed, 179 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -0
index 928820e61cb5..cc67daab3396 100644
@@ -105,6 +105,7 @@ config X86
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAS_ZONE_DMA_SET if EXPERT
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
+	select ARCH_HAVE_EXTRA_ELF_NOTES
 	select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
 	select ARCH_MIGHT_HAVE_ACPI_PDC		if ACPI
 	select ARCH_MIGHT_HAVE_PC_PARPORT
arch/x86/include/asm/elf.h +34 -0
index 1fb83d47711f..5952574db64b 100644
@@ -13,6 +13,40 @@
 #include <asm/auxvec.h>
 #include <asm/fsgsbase.h>
 
+struct xfeat_component {
+	u32 xfeat_type;
+	u32 xfeat_sz;
+	u32 xfeat_off;
+	u32 xfeat_flags;
+} __packed;
+
+_Static_assert(sizeof(struct xfeat_component)%4 == 0, "xfeat_component is not aligned");
+
+enum custom_feature {
+	FEATURE_XSAVE_FP = 0,
+	FEATURE_XSAVE_SSE = 1,
+	FEATURE_XSAVE_YMM = 2,
+	FEATURE_XSAVE_BNDREGS = 3,
+	FEATURE_XSAVE_BNDCSR = 4,
+	FEATURE_XSAVE_OPMASK = 5,
+	FEATURE_XSAVE_ZMM_Hi256 = 6,
+	FEATURE_XSAVE_Hi16_ZMM = 7,
+	FEATURE_XSAVE_PT = 8,
+	FEATURE_XSAVE_PKRU = 9,
+	FEATURE_XSAVE_PASID = 10,
+	FEATURE_XSAVE_CET_USER = 11,
+	FEATURE_XSAVE_CET_SHADOW_STACK = 12,
+	FEATURE_XSAVE_HDC = 13,
+	FEATURE_XSAVE_UINTR = 14,
+	FEATURE_XSAVE_LBR = 15,
+	FEATURE_XSAVE_HWP = 16,
+	FEATURE_XSAVE_XTILE_CFG = 17,
+	FEATURE_XSAVE_XTILE_DATA = 18,
+	FEATURE_MAX,
+	FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+	FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};
+
 typedef unsigned long elf_greg_t;
 
 #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t))
arch/x86/kernel/fpu/xstate.c +1 -0
index 33a214b1a4ce..3d1c3c96e34d 100644
@@ -13,6 +13,7 @@
 #include <linux/seq_file.h>
 #include <linux/proc_fs.h>
 #include <linux/vmalloc.h>
+#include <linux/coredump.h>
 
 #include <asm/fpu/api.h>
 #include <asm/fpu/regset.h>

arch/x86/kernel/fpu/xstate.c static unsigned int xstate_flags[XFEATURE_MAX] __ro_after_init; · line 91

Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +1 -0
@@ -87,6 +88,8 @@ static unsigned int xstate_flags[XFEATURE_MAX] __ro_after_init;
 #define XSTATE_FLAG_SUPERVISOR	BIT(0)
 #define XSTATE_FLAG_ALIGNED64	BIT(1)
 
+static const char owner_name[] = "LINUX";

This needs to move under the CONFIG_COREDUMP below (so says the build bots).

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1845

Quoted 11 lines
arch/x86/kernel/fpu/xstate.c +4 -0
+
 /*
  * Return whether the system supports a given xfeature.
  *
@@ -1837,3 +1840,141 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
 	return 0;
 }
 #endif /* CONFIG_PROC_PID_ARCH_STATUS */
+
+#ifdef CONFIG_COREDUMP
+static int get_sub_leaf(int custom_xfeat)

Why is this "int"? I don't imagine there are negative features?

Quoted 11 lines
arch/x86/kernel/fpu/xstate.c +11 -0
+{
+	switch (custom_xfeat) {
+	case FEATURE_XSAVE_YMM:			return XFEATURE_YMM;
+	case FEATURE_XSAVE_BNDREGS:		return XFEATURE_BNDREGS;
+	case FEATURE_XSAVE_BNDCSR:		return XFEATURE_BNDCSR;
+	case FEATURE_XSAVE_OPMASK:		return XFEATURE_OPMASK;
+	case FEATURE_XSAVE_ZMM_Hi256:		return XFEATURE_ZMM_Hi256;
+	case FEATURE_XSAVE_Hi16_ZMM:		return XFEATURE_Hi16_ZMM;
+	case FEATURE_XSAVE_PT:			return XFEATURE_PT_UNIMPLEMENTED_SO_FAR;
+	case FEATURE_XSAVE_PKRU:		return XFEATURE_PKRU;
+	case FEATURE_XSAVE_PASID:		return XFEATURE_PASID;

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1866

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+	case FEATURE_XSAVE_CET_USER:		return XFEATURE_CET_USER;
+	case FEATURE_XSAVE_CET_SHADOW_STACK:	return XFEATURE_CET_KERNEL_UNUSED;
+	case FEATURE_XSAVE_HDC:			return XFEATURE_RSRVD_COMP_13;
+	case FEATURE_XSAVE_UINTR:		return XFEATURE_RSRVD_COMP_14;
+	case FEATURE_XSAVE_LBR:			return XFEATURE_LBR;
+	case FEATURE_XSAVE_HWP:			return XFEATURE_RSRVD_COMP_16;
+	case FEATURE_XSAVE_XTILE_CFG:		return XFEATURE_XTILE_CFG;
+	case FEATURE_XSAVE_XTILE_DATA:		return XFEATURE_XTILE_DATA;
+	default:
+		pr_warn_ratelimited("Not a valid XSAVE Feature.");

This isn't very friendly; it's keeping secrets about the unknown value. :) Also it's missing a newline. How about:

		pr_warn_ratelimited("Not a known XSAVE Feature: %u\n",
				    custom_xfeat);
Quoted 34 lines
arch/x86/kernel/fpu/xstate.c +34 -0
+		return 0;
+	}
+}
+
+/*
+ * Dump type, size, offset and flag values for every xfeature that is present.
+ */
+static int dump_xsave_layout_desc(struct coredump_params *cprm)
+{
+	u32 supported_features = 0;
+	struct xfeat_component xc;
+	u32 eax, ebx, ecx, edx;
+	int num_records = 0;
+	int sub_leaf = 0;
+	int i;
+
+	/* Find supported extended features */
+	cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+	supported_features = eax;
+
+	for (i = FEATURE_XSAVE_EXTENDED_START;
+			i <= FEATURE_XSAVE_EXTENDED_END; i++) {
+		sub_leaf = get_sub_leaf(i);
+		if (!sub_leaf)
+			continue;
+		if (supported_features & (1U << sub_leaf)) {
+			cpuid_count(XSTATE_CPUID, sub_leaf, &eax, &ebx, &ecx, &edx);
+			xc.xfeat_type = i;
+			xc.xfeat_sz = eax;
+			xc.xfeat_off = ebx;
+			/* Reserved for future use */
+			xc.xfeat_flags = 0;
+
+			if (!dump_emit(cprm, &xc,

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1910

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+				       sizeof(struct xfeat_component)))
+				return 0;
+			num_records++;
+		}
+	}
+
+	return num_records;
+}
+
+static int get_xsave_desc_size(void)

This can return u32: never negative.

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1916

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +6 -0
+{
+	int supported_features = 0;
+	int xfeatures_count = 0;
+	u32 eax, ebx, ecx, edx;
+	int sub_leaf = 0;
+	int i;

"i" can be u32 and then we can fix the get_sub_leaf() arg type.

Quoted 26 lines
arch/x86/kernel/fpu/xstate.c +26 -0
+
+	/* Find supported extended features */
+	cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+	supported_features = eax;
+
+	for (i = FEATURE_XSAVE_EXTENDED_START;
+			i <= FEATURE_XSAVE_EXTENDED_END; i++) {
+		sub_leaf = get_sub_leaf(i);
+		if (!sub_leaf)
+			continue;
+		if (supported_features & (1U << sub_leaf))
+			xfeatures_count++;
+	}
+
+	return xfeatures_count * (sizeof(struct xfeat_component));
+}
+
+int elf_coredump_extra_notes_write(struct coredump_params *cprm)
+{
+	int num_records = 0;
+	struct elf_note en;
+
+	en.n_namesz = sizeof(owner_name);
+	en.n_descsz = get_xsave_desc_size();
+	en.n_type = NT_X86_XSAVE_LAYOUT;
+

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1952

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+	if (!dump_emit(cprm, &en, sizeof(en)))
+		return 1;
+	if (!dump_emit(cprm, owner_name, en.n_namesz))
+		return 1;
+	if (!dump_align(cprm, 4))
+		return 1;
+
+	num_records = dump_xsave_layout_desc(cprm);
+	if (!num_records) {
+		pr_warn_ratelimited("Error adding XSTATE layout ELF note. XSTATE buffer in the core file will be unparseable.");

Missing trailing newline.

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1958

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +6 -0
+		return 1;
+	}
+
+	/* Total size should be equal to the number of records */
+	if ((sizeof(struct xfeat_component) * num_records) != en.n_descsz) {
+		pr_warn_ratelimited("Error adding XSTATE layout ELF note. The size of the .note section does not match with the total size of the records.");

Same.

Quoted 12 lines
arch/x86/kernel/fpu/xstate.c +12 -0
+		return 1;
+	}
+
+	return 0;
+}
+
+/*
+ * Return the size of new note.
+ */
+int elf_coredump_extra_notes_size(void)
+{
+	int size = 0;

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1980

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+
+	/* NOTE Header */
+	size += sizeof(struct elf_note);
+	/* name + align */
+	size += roundup(sizeof(owner_name), 4);
+	size += get_xsave_desc_size();
+
+	return size;
+}
+#endif

Since it's a long if/endif, add: /* CONFIG_COREDUMP */ after the endif here.

Quoted 37 lines
fs/binfmt_elf.c +2 -2
index 5397b552fbeb..833bcb7e957b 100644
@@ -2000,7 +2000,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	{
 		size_t sz = info.size;
 
-		/* For cell spufs */
+		/* For cell spufs and x86 xstate */
 		sz += elf_coredump_extra_notes_size();
 
 		phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
@@ -2064,7 +2064,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	if (!write_note_info(&info, cprm))
 		goto end_coredump;
 
-	/* For cell spufs */
+	/* For cell spufs and x86 xstate */
 	if (elf_coredump_extra_notes_write(cprm))
 		goto end_coredump;
 
include/uapi/linux/elf.h +1 -0
index b54b313bcf07..e30a9b47dc87 100644
@@ -411,6 +411,7 @@ typedef struct elf64_shdr {
 #define NT_X86_XSTATE	0x202		/* x86 extended state using xsave */
 /* Old binutils treats 0x203 as a CET state */
 #define NT_X86_SHSTK	0x204		/* x86 SHSTK state */
+#define NT_X86_XSAVE_LAYOUT	0x205	/* XSAVE layout description */
 #define NT_S390_HIGH_GPRS	0x300	/* s390 upper register halves */
 #define NT_S390_TIMER	0x301		/* s390 timer register */
 #define NT_S390_TODCMP	0x302		/* s390 TOD clock comparator register */
Signature · 1 line
2.34.1

Otherwise looks good. I'd like to see feedback from Intel folks too.

Thanks for working on this!

-Kees

Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

In reply to Vignesh Balasubramanian

arch/x86/include/asm/elf.h line 21

Quoted 6 lines · Vignesh Balasubramanian
arch/x86/include/asm/elf.h +6 -0
+struct xfeat_component {
+	u32 xfeat_type;
+	u32 xfeat_sz;
+	u32 xfeat_off;
+	u32 xfeat_flags;
+} __packed;

Why repeating xfeat_ for all member names?

    u32       type;
    u32       size;
    u32       offset;
    u32       flags;

is sufficient and obvious, no?

Quoted 14 lines
arch/x86/include/asm/elf.h +14 -0
+enum custom_feature {
+	FEATURE_XSAVE_FP = 0,
+	FEATURE_XSAVE_SSE = 1,
+	FEATURE_XSAVE_YMM = 2,
+	FEATURE_XSAVE_BNDREGS = 3,
+	FEATURE_XSAVE_BNDCSR = 4,
+	FEATURE_XSAVE_OPMASK = 5,
+	FEATURE_XSAVE_ZMM_Hi256 = 6,
+	FEATURE_XSAVE_Hi16_ZMM = 7,
+	FEATURE_XSAVE_PT = 8,
+	FEATURE_XSAVE_PKRU = 9,
+	FEATURE_XSAVE_PASID = 10,
+	FEATURE_XSAVE_CET_USER = 11,
+	FEATURE_XSAVE_CET_SHADOW_STACK = 12,

arch/x86/include/asm/elf.h line 48

Quoted 10 lines
arch/x86/include/asm/elf.h +10 -0
+	FEATURE_XSAVE_HDC = 13,
+	FEATURE_XSAVE_UINTR = 14,
+	FEATURE_XSAVE_LBR = 15,
+	FEATURE_XSAVE_HWP = 16,
+	FEATURE_XSAVE_XTILE_CFG = 17,
+	FEATURE_XSAVE_XTILE_DATA = 18,
+	FEATURE_MAX,
+	FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+	FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};

Why can't this use the existing 'enum xfeature' which is providing exactly the same information already?

Quoted 16 lines
arch/x86/kernel/fpu/xstate.c +16 -0
+#ifdef CONFIG_COREDUMP
+static int get_sub_leaf(int custom_xfeat)
+{
+	switch (custom_xfeat) {
+	case FEATURE_XSAVE_YMM:			return XFEATURE_YMM;
+	case FEATURE_XSAVE_BNDREGS:		return XFEATURE_BNDREGS;
+	case FEATURE_XSAVE_BNDCSR:		return XFEATURE_BNDCSR;
+	case FEATURE_XSAVE_OPMASK:		return XFEATURE_OPMASK;
+	case FEATURE_XSAVE_ZMM_Hi256:		return XFEATURE_ZMM_Hi256;
+	case FEATURE_XSAVE_Hi16_ZMM:		return XFEATURE_Hi16_ZMM;
+	case FEATURE_XSAVE_PT:			return XFEATURE_PT_UNIMPLEMENTED_SO_FAR;
+	case FEATURE_XSAVE_PKRU:		return XFEATURE_PKRU;
+	case FEATURE_XSAVE_PASID:		return XFEATURE_PASID;
+	case FEATURE_XSAVE_CET_USER:		return XFEATURE_CET_USER;
+	case FEATURE_XSAVE_CET_SHADOW_STACK:	return XFEATURE_CET_KERNEL_UNUSED;
+	case FEATURE_XSAVE_HDC:			return XFEATURE_RSRVD_COMP_13;

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1869

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+	case FEATURE_XSAVE_UINTR:		return XFEATURE_RSRVD_COMP_14;
+	case FEATURE_XSAVE_LBR:			return XFEATURE_LBR;
+	case FEATURE_XSAVE_HWP:			return XFEATURE_RSRVD_COMP_16;
+	case FEATURE_XSAVE_XTILE_CFG:		return XFEATURE_XTILE_CFG;
+	case FEATURE_XSAVE_XTILE_DATA:		return XFEATURE_XTILE_DATA;
+	default:
+		pr_warn_ratelimited("Not a valid XSAVE Feature.");
+		return 0;
+	}
+}

This function then maps the identical enums one to one. The only actual "functionality" is the default case and that's completely pointless.

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +5 -0
+/*
+ * Dump type, size, offset and flag values for every xfeature that is present.
+ */
+static int dump_xsave_layout_desc(struct coredump_params *cprm)
+{

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1885

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+	u32 supported_features = 0;
+	struct xfeat_component xc;
+	u32 eax, ebx, ecx, edx;
+	int num_records = 0;
+	int sub_leaf = 0;
+	int i;
+
+	/* Find supported extended features */
+	cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+	supported_features = eax;

Why does this need to re-evaluate CPUID instead of just using the existing fpu_user_cfg.max_features?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	for (i = FEATURE_XSAVE_EXTENDED_START;
+			i <= FEATURE_XSAVE_EXTENDED_END; i++) {

Please use the full 100 character line width.

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1901

Quoted 13 lines
arch/x86/kernel/fpu/xstate.c +13 -0
+		sub_leaf = get_sub_leaf(i);
+		if (!sub_leaf)
+			continue;
+		if (supported_features & (1U << sub_leaf)) {
+			cpuid_count(XSTATE_CPUID, sub_leaf, &eax, &ebx, &ecx, &edx);
+			xc.xfeat_type = i;
+			xc.xfeat_sz = eax;
+			xc.xfeat_off = ebx;
+			/* Reserved for future use */
+			xc.xfeat_flags = 0;
+
+			if (!dump_emit(cprm, &xc,
+				       sizeof(struct xfeat_component)))

sizeof(xc), no?

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1905

Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +4 -0
+				return 0;
+			num_records++;
+		}
+	}

This whole thing can be written as:

	for_each_extended_xfeature(i, fpu_user_cfg.max_features) {
		struct xfeat_component xc = {
                	.type	= i,
                        .size	= xstate_sizes[i],
                        .offset	= xstate_offsets[i],
		};

		if (!dump_emit(cprm, &xc, sizeof(xc)))
			return 0;
                num_records++;
	}

It omits the features which are supported by the CPU, but not enabled by the kernel. That's perfectly fine because:

1) the corresponding xfeature bits of those component in the actual XSAVE dump are guaranteed to be zero

2) the corresponding regions in the actual XSAVE dump are zeroed

So there is absolutely no point in having notes for the not enabled features at all.

Hmm?

Quoted 15 lines
arch/x86/kernel/fpu/xstate.c +15 -0
+
+	return num_records;
+}
+
+static int get_xsave_desc_size(void)
+{
+	int supported_features = 0;
+	int xfeatures_count = 0;
+	u32 eax, ebx, ecx, edx;
+	int sub_leaf = 0;
+	int i;
+
+	/* Find supported extended features */
+	cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+	supported_features = eax;

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1931

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+
+	for (i = FEATURE_XSAVE_EXTENDED_START;
+			i <= FEATURE_XSAVE_EXTENDED_END; i++) {
+		sub_leaf = get_sub_leaf(i);
+		if (!sub_leaf)
+			continue;
+		if (supported_features & (1U << sub_leaf))
+			xfeatures_count++;
+	}
+	return xfeatures_count * (sizeof(struct xfeat_component));

Then this can be replaced by:

	int i, cnt = 0;

	for_each_extended_xfeature(i, fpu_user_cfg.max_features)
        	cnt++;

        return cnt * sizeof(struct xfeat_component);

In fact the number of extended features can be calculated once during boot during xstate initialization.

No?

Quoted 12 lines
arch/x86/kernel/fpu/xstate.c +12 -0
+}
+
+int elf_coredump_extra_notes_write(struct coredump_params *cprm)
+{
+	int num_records = 0;
+	struct elf_note en;
+
+	en.n_namesz = sizeof(owner_name);
+	en.n_descsz = get_xsave_desc_size();
+	en.n_type = NT_X86_XSAVE_LAYOUT;
+
+	if (!dump_emit(cprm, &en, sizeof(en)))

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1953

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+		return 1;
+	if (!dump_emit(cprm, owner_name, en.n_namesz))
+		return 1;
+	if (!dump_align(cprm, 4))
+		return 1;
+
+	num_records = dump_xsave_layout_desc(cprm);
+	if (!num_records) {
+		pr_warn_ratelimited("Error adding XSTATE layout ELF note. XSTATE buffer in the core file will be unparseable.");
+		return 1;

This is going to trigger on all systems which do not support XSAVE. So why emitting this note in the first place on such systems?

The function should have

	if (!fpu_kernel_cfg.max_features)
        	return 0;

right at the beginning.

Aside of that, these warnings are pointless noise in the case that dump_emit() caused the function to return early. Dumps can be truncated.

Quoted 2 lines

+/*
+ * Return the size of new note.

Which new note? This is extra notes, no?

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1970

Quoted 4 lines

+ */

arch/x86/kernel/fpu/xstate.c +3 -0
+int elf_coredump_extra_notes_size(void)
+{
+	int size = 0;
	int size;

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	/* NOTE Header */

Note header ?

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1973

Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1 -0
+	size += sizeof(struct elf_note);
	size = ....

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1974

Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1 -0
+	/* name + align */

Name plus alignment to 4 bytes ?

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1979

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +5 -0
+	size += roundup(sizeof(owner_name), 4);
+	size += get_xsave_desc_size();
+
+	return size;
+}

And like the write function this wants:

	if (!fpu_kernel_cfg.max_features)
        	return 0;

at the beginning. No point in emitting useless notes and headers.

Thanks,

tglx

#6 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 22 lines · Thomas Gleixner

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 18 lines · Vignesh Balasubramanian
arch/x86/include/asm/elf.h +18 -0
+enum custom_feature {
+     FEATURE_XSAVE_FP = 0,
+     FEATURE_XSAVE_SSE = 1,
+     FEATURE_XSAVE_YMM = 2,
+     FEATURE_XSAVE_BNDREGS = 3,
+     FEATURE_XSAVE_BNDCSR = 4,
+     FEATURE_XSAVE_OPMASK = 5,
+     FEATURE_XSAVE_ZMM_Hi256 = 6,
+     FEATURE_XSAVE_Hi16_ZMM = 7,
+     FEATURE_XSAVE_PT = 8,
+     FEATURE_XSAVE_PKRU = 9,
+     FEATURE_XSAVE_PASID = 10,
+     FEATURE_XSAVE_CET_USER = 11,
+     FEATURE_XSAVE_CET_SHADOW_STACK = 12,
+     FEATURE_XSAVE_HDC = 13,
+     FEATURE_XSAVE_UINTR = 14,
+     FEATURE_XSAVE_LBR = 15,
+     FEATURE_XSAVE_HWP = 16,
Quoted 8 lines

arch/x86/include/asm/elf.h line 48

Quoted 6 lines
arch/x86/include/asm/elf.h +6 -0
+     FEATURE_XSAVE_XTILE_CFG = 17,
+     FEATURE_XSAVE_XTILE_DATA = 18,
+     FEATURE_MAX,
+     FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+     FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};

Why can't this use the existing 'enum xfeature' which is providing exactly the same information already?

First version of patch was similar to what you mentioned here and other review comments to use existing kernel definitions.
https://lore.kernel.org/linux-mm/[email protected]/T/

As per the review comment https://lore.kernel.org/linux-mm/20240314162954.GAZfMmAnYQoRjRbRzc@fat_crate.local/ , modified the patch to be a independent of kernel internal definitions. Though this enum and below function  "get_sub_leaf" are not useful now,  it will be required when we extend for a new/different features.

Please let  us know your suggestions.

I will fix all other review comments in my next version.

thanks,
vigneshbalu.

Quoted 29 lines
Quoted 16 lines
arch/x86/kernel/fpu/xstate.c +16 -0
+#ifdef CONFIG_COREDUMP
+static int get_sub_leaf(int custom_xfeat)
+{
+     switch (custom_xfeat) {
+     case FEATURE_XSAVE_YMM:                 return XFEATURE_YMM;
+     case FEATURE_XSAVE_BNDREGS:             return XFEATURE_BNDREGS;
+     case FEATURE_XSAVE_BNDCSR:              return XFEATURE_BNDCSR;
+     case FEATURE_XSAVE_OPMASK:              return XFEATURE_OPMASK;
+     case FEATURE_XSAVE_ZMM_Hi256:           return XFEATURE_ZMM_Hi256;
+     case FEATURE_XSAVE_Hi16_ZMM:            return XFEATURE_Hi16_ZMM;
+     case FEATURE_XSAVE_PT:                  return XFEATURE_PT_UNIMPLEMENTED_SO_FAR;
+     case FEATURE_XSAVE_PKRU:                return XFEATURE_PKRU;
+     case FEATURE_XSAVE_PASID:               return XFEATURE_PASID;
+     case FEATURE_XSAVE_CET_USER:            return XFEATURE_CET_USER;
+     case FEATURE_XSAVE_CET_SHADOW_STACK:    return XFEATURE_CET_KERNEL_UNUSED;
+     case FEATURE_XSAVE_HDC:                 return XFEATURE_RSRVD_COMP_13;

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1869

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+     case FEATURE_XSAVE_UINTR:               return XFEATURE_RSRVD_COMP_14;
+     case FEATURE_XSAVE_LBR:                 return XFEATURE_LBR;
+     case FEATURE_XSAVE_HWP:                 return XFEATURE_RSRVD_COMP_16;
+     case FEATURE_XSAVE_XTILE_CFG:           return XFEATURE_XTILE_CFG;
+     case FEATURE_XSAVE_XTILE_DATA:          return XFEATURE_XTILE_DATA;
+     default:
+             pr_warn_ratelimited("Not a valid XSAVE Feature.");
+             return 0;
+     }
+}

This function then maps the identical enums one to one. The only actual "functionality" is the default case and that's completely pointless.

#7 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

In reply to Thomas Gleixner
Quoted 37 lines · Thomas Gleixner

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

arch/x86/include/asm/elf.h line 21

Quoted 6 lines · Vignesh Balasubramanian
arch/x86/include/asm/elf.h +6 -0
+struct xfeat_component {
+     u32 xfeat_type;
+     u32 xfeat_sz;
+     u32 xfeat_off;
+     u32 xfeat_flags;
+} __packed;

Why repeating xfeat_ for all member names?

     u32       type;
     u32       size;
     u32       offset;
     u32       flags;

is sufficient and obvious, no?

Quoted 18 lines
arch/x86/include/asm/elf.h +18 -0
+enum custom_feature {
+     FEATURE_XSAVE_FP = 0,
+     FEATURE_XSAVE_SSE = 1,
+     FEATURE_XSAVE_YMM = 2,
+     FEATURE_XSAVE_BNDREGS = 3,
+     FEATURE_XSAVE_BNDCSR = 4,
+     FEATURE_XSAVE_OPMASK = 5,
+     FEATURE_XSAVE_ZMM_Hi256 = 6,
+     FEATURE_XSAVE_Hi16_ZMM = 7,
+     FEATURE_XSAVE_PT = 8,
+     FEATURE_XSAVE_PKRU = 9,
+     FEATURE_XSAVE_PASID = 10,
+     FEATURE_XSAVE_CET_USER = 11,
+     FEATURE_XSAVE_CET_SHADOW_STACK = 12,
+     FEATURE_XSAVE_HDC = 13,
+     FEATURE_XSAVE_UINTR = 14,
+     FEATURE_XSAVE_LBR = 15,
+     FEATURE_XSAVE_HWP = 16,
Quoted 8 lines

arch/x86/include/asm/elf.h line 48

Quoted 6 lines
arch/x86/include/asm/elf.h +6 -0
+     FEATURE_XSAVE_XTILE_CFG = 17,
+     FEATURE_XSAVE_XTILE_DATA = 18,
+     FEATURE_MAX,
+     FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+     FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};

Why can't this use the existing 'enum xfeature' which is providing exactly the same information already?

First version of patch was similar to what you mentioned here and other review comments to use existing kernel definitions.
https://lore.kernel.org/linux-mm/[email protected]/T/

As per the review comment https://lore.kernel.org/linux-mm/20240314162954.GAZfMmAnYQoRjRbRzc@fat_crate.local/ , modified the patch to be a independent of kernel internal definitions. Though this enum and below function  "get_sub_leaf" are not useful now,  it will be required when we extend for a new/different features.

Please let  us know your suggestions.

I will fix all other review comments in my next version.

Quoted 20 lines
Quoted 20 lines
arch/x86/kernel/fpu/xstate.c +20 -0
+#ifdef CONFIG_COREDUMP
+static int get_sub_leaf(int custom_xfeat)
+{
+     switch (custom_xfeat) {
+     case FEATURE_XSAVE_YMM:                 return XFEATURE_YMM;
+     case FEATURE_XSAVE_BNDREGS:             return XFEATURE_BNDREGS;
+     case FEATURE_XSAVE_BNDCSR:              return XFEATURE_BNDCSR;
+     case FEATURE_XSAVE_OPMASK:              return XFEATURE_OPMASK;
+     case FEATURE_XSAVE_ZMM_Hi256:           return XFEATURE_ZMM_Hi256;
+     case FEATURE_XSAVE_Hi16_ZMM:            return XFEATURE_Hi16_ZMM;
+     case FEATURE_XSAVE_PT:                  return XFEATURE_PT_UNIMPLEMENTED_SO_FAR;
+     case FEATURE_XSAVE_PKRU:                return XFEATURE_PKRU;
+     case FEATURE_XSAVE_PASID:               return XFEATURE_PASID;
+     case FEATURE_XSAVE_CET_USER:            return XFEATURE_CET_USER;
+     case FEATURE_XSAVE_CET_SHADOW_STACK:    return XFEATURE_CET_KERNEL_UNUSED;
+     case FEATURE_XSAVE_HDC:                 return XFEATURE_RSRVD_COMP_13;
+     case FEATURE_XSAVE_UINTR:               return XFEATURE_RSRVD_COMP_14;
+     case FEATURE_XSAVE_LBR:                 return XFEATURE_LBR;
+     case FEATURE_XSAVE_HWP:                 return XFEATURE_RSRVD_COMP_16;
+     case FEATURE_XSAVE_XTILE_CFG:           return XFEATURE_XTILE_CFG;
Quoted 8 lines

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1869

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +6 -0
+     case FEATURE_XSAVE_XTILE_DATA:          return XFEATURE_XTILE_DATA;
+     default:
+             pr_warn_ratelimited("Not a valid XSAVE Feature.");
+             return 0;
+     }
+}

This function then maps the identical enums one to one. The only actual "functionality" is the default case and that's completely pointless.

thanks,
vigneshbalu.

maintainer
#8 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 26 lines · Vignesh
Quoted 18 lines
Quoted 18 lines
arch/x86/include/asm/elf.h +18 -0
+enum custom_feature {
+     FEATURE_XSAVE_FP = 0,
+     FEATURE_XSAVE_SSE = 1,
+     FEATURE_XSAVE_YMM = 2,
+     FEATURE_XSAVE_BNDREGS = 3,
+     FEATURE_XSAVE_BNDCSR = 4,
+     FEATURE_XSAVE_OPMASK = 5,
+     FEATURE_XSAVE_ZMM_Hi256 = 6,
+     FEATURE_XSAVE_Hi16_ZMM = 7,
+     FEATURE_XSAVE_PT = 8,
+     FEATURE_XSAVE_PKRU = 9,
+     FEATURE_XSAVE_PASID = 10,
+     FEATURE_XSAVE_CET_USER = 11,
+     FEATURE_XSAVE_CET_SHADOW_STACK = 12,
+     FEATURE_XSAVE_HDC = 13,
+     FEATURE_XSAVE_UINTR = 14,
+     FEATURE_XSAVE_LBR = 15,
+     FEATURE_XSAVE_HWP = 16,
Quoted 8 lines

arch/x86/include/asm/elf.h line 48

Quoted 6 lines
arch/x86/include/asm/elf.h +6 -0
+     FEATURE_XSAVE_XTILE_CFG = 17,
+     FEATURE_XSAVE_XTILE_DATA = 18,
+     FEATURE_MAX,
+     FEATURE_XSAVE_EXTENDED_START = FEATURE_XSAVE_YMM,
+     FEATURE_XSAVE_EXTENDED_END = FEATURE_XSAVE_XTILE_DATA,
+};

Why can't this use the existing 'enum xfeature' which is providing exactly the same information already?

Quoted 8 lines

First version of patch was similar to what you mentioned here and other review comments to use existing kernel definitions.
https://lore.kernel.org/linux-mm/[email protected]/T/

As per the review comment https://lore.kernel.org/linux-mm/20240314162954.GAZfMmAnYQoRjRbRzc@fat_crate.local/
, modified the patch to be a independent of kernel internal definitions.
Though this enum and below function  "get_sub_leaf" are not useful now,  it
will be required when we extend for a new/different features.

No, Thomas' sugggestion is to use the existing xfeature enum - not define the same thing again.

Why do you need that enum custom_feature thing if you can use

/*
* List of XSAVE features Linux knows about:
*/
enum xfeature {

from arch/x86/include/asm/fpu/types.h

?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#10 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 3 lines · Vignesh

Currently, this enum is the same as XSAVE, but when we add other features, this enum might have a different value of the XSAVE features and can be modified without disturbing the existing kernel code.

We will do that when we cross that bridge, right?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 8 lines · Borislav Petkov

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 3 lines · Vignesh

Currently, this enum is the same as XSAVE, but when we add other features, this enum might have a different value of the XSAVE features and can be modified without disturbing the existing kernel code.

We will do that when we cross that bridge, right?

I am struggling to interpret.
If we can add a new enum only when we extend, then as Thomas suggested can we use other kernel variables as in the first version of the patch until we extend for other/new features?

thanks,
vigneshbalu.

Quoted 6 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

maintainer
#12 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 3 lines · Vignesh

If we can add a new enum only when we extend, then as Thomas suggested can we use other kernel variables as in the first version of the patch until we extend for other/new features?

I assume by "other kernel variables" you mean CPUID?

If so, can you change the layout of your buffer once you export it to userspace?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Quoted 11 lines · Borislav Petkov

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 3 lines · Vignesh

If we can add a new enum only when we extend, then as Thomas suggested can we use other kernel variables as in the first version of the patch until we extend for other/new features?

I assume by "other kernel variables" you mean CPUID?

If so, can you change the layout of your buffer once you export it to userspace?

In a couple of the previous review mails
(https://lore.kernel.org/lkml/[email protected]/
and
https://lore.kernel.org/lkml/20240314162954.GAZfMmAnYQoRjRbRzc@fat_crate.local/),
it was suggested that the new .note should not use any internal definitions like "xstate_sizes", "xstate_offsets" and ""xstate_flags" which are also the direct output of cpuid instruction.

Also, the feature ID in .note records should be independent of the existing XSAVE feature IDs (this was the comment as I understood). I defined the new enum and mapping function to ensure that these remain independent of each other.

Thomas' comments on this version are that we should use existing variables instead of re-evaluating cpuid. Also, to avoid the new enum and mapping function which will make not any sense unless it is extended for newer/different features.

That will be like our first version of the patch
https://lore.kernel.org/lkml/[email protected]/

So other than with the new enum (custom_feature) and the new mapping function (get_sub_leaf), we are unsure as to how to maintain the layout to be independent of x86' cpuid.

In the current version of the patch, the fields -- type, size, and offset are derived from the cpuid instruction currently (and could be derived from existing kernel variables in the future). The xsave flags are not used currently, it can be zero(reserved) for now and its layout can be modified (as per the need at that time) when the need arises.

If there are other ways to maintain the independence of the layout of a record in .note section from the cpuid instruction other than depending on a new enum and a new mapping function, we would be glad to follow it.

thanks
vigneshbalu

Quoted 5 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

maintainer
#14 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Ok,

I went and worked in tglx's comments. This is what this should look like.

Only build-tested.

---
From cf23110f5cc24b6072ba7a26f31cff3ed486e6b3 Mon Sep 17 00:00:00 2001
From: Vignesh Balasubramanian <[email protected]>
Date: Tue, 7 May 2024 15:23:31 +0530
Subject: [PATCH] x86/elf: Add a new .note section containing xfeatures buffer
layout info to x86 core files

Add a new .note section containing type, size, offset and flags of every xfeature that is present.

This information will be used by debuggers to understand the XSAVE layout of the machine where the core file has been dumped, and to read XSAVE registers, especially during cross-platform debugging.

The XSAVE layouts of modern AMD and Intel CPUs differ, especially since Memory Protection Keys and the AVX-512 features have been inculcated into the AMD CPUs.

Since AMD never adopted (and hence never left room in the XSAVE layout for) the Intel MPX feature. Tools like GDB had assumed a fixed XSAVE layout matching that of Intel (based on the XCR0 mask).

Hence, core dumps from AMD CPUs didn't match the known size for the XCR0 mask. This resulted in GDB and other tools not being able to access the values of the AVX-512 and PKRU registers on AMD CPUs.

To solve this, an interim solution has been accepted into GDB, and is already a part of GDB 14, see
https://sourceware.org/pipermail/gdb-patches/2023-March/198081.html.

But it depends on heuristics based on the total XSAVE register set size and the XCR0 mask to infer the layouts of the various register blocks for core dumps, and hence, is not a foolproof mechanism to determine the layout of the XSAVE area.

Therefore, add a new core dump note in order to allow GDB/LLDB and other relevant tools to determine the layout of the XSAVE area of the machine where the corefile was dumped.

The new core dump note (which is being proposed as a per-process .note section), NT_X86_XSAVE_LAYOUT (0x205) contains an array of structures.

Each structure describes an individual extended feature containing offset, size and flags in this format:

struct xfeat_component {
       u32 type;
       u32 size;
       u32 offset;
       u32 flags;
};

and in an independent manner, allowing for future extensions without depending on hw arch specifics like CPUID etc.

[ bp: Work in tglx' comments from
https://lore.kernel.org/r/87wmo4o3r4.ffs@tglx, massage. ]

---
 arch/x86/Kconfig             |  1 +
 arch/x86/include/asm/elf.h   |  9 ++++
 arch/x86/kernel/fpu/xstate.c | 84 ++++++++++++++++++++++++++++++++++++
 fs/binfmt_elf.c              |  4 +-
 include/uapi/linux/elf.h     |  1 +
 5 files changed, 97 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -0
index e30ea4129d2c..46e44b087c94 100644
@@ -107,6 +107,7 @@ config X86
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAS_ZONE_DMA_SET if EXPERT
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
+	select ARCH_HAVE_EXTRA_ELF_NOTES
 	select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
 	select ARCH_MIGHT_HAVE_ACPI_PDC		if ACPI
 	select ARCH_MIGHT_HAVE_PC_PARPORT
arch/x86/include/asm/elf.h +9 -0
index 1fb83d47711f..cad37090bbd3 100644
@@ -13,6 +13,15 @@
 #include <asm/auxvec.h>
 #include <asm/fsgsbase.h>
 
+struct xfeat_component {
+	u32 type;
+	u32 size;
+	u32 offset;
+	u32 flags;
+} __packed;
+
+_Static_assert(sizeof(struct xfeat_component)%4 == 0, "xfeat_component is not aligned");
+
 typedef unsigned long elf_greg_t;
 
 #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t))
arch/x86/kernel/fpu/xstate.c +84 -0

93 lines not on this page · raw message

fs/binfmt_elf.c +2 -2
index a43897b03ce9..3d15c7369b29 100644
@@ -2006,7 +2006,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	{
 		size_t sz = info.size;
 
-		/* For cell spufs */
+		/* For cell spufs and x86 xstate */
 		sz += elf_coredump_extra_notes_size();
 
 		phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
@@ -2070,7 +2070,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 	if (!write_note_info(&info, cprm))
 		goto end_coredump;
 
-	/* For cell spufs */
+	/* For cell spufs and x86 xstate */
 	if (elf_coredump_extra_notes_write(cprm))
 		goto end_coredump;
 
include/uapi/linux/elf.h +1 -0
index b54b313bcf07..e30a9b47dc87 100644
@@ -411,6 +411,7 @@ typedef struct elf64_shdr {
 #define NT_X86_XSTATE	0x202		/* x86 extended state using xsave */
 /* Old binutils treats 0x203 as a CET state */
 #define NT_X86_SHSTK	0x204		/* x86 SHSTK state */
+#define NT_X86_XSAVE_LAYOUT	0x205	/* XSAVE layout description */
 #define NT_S390_HIGH_GPRS	0x300	/* s390 upper register halves */
 #define NT_S390_TIMER	0x301		/* s390 timer register */
 #define NT_S390_TODCMP	0x302		/* s390 TOD clock comparator register */
Signature · 8 lines
2.43.0


-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#15 lore raw

Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files

Apologies for the delay. I am sending the new version patch with below changes.

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1854

Quoted 8 lines · Borislav Petkov

p type, size, offset and flag values for every xfeature that is present.
+ */

arch/x86/kernel/fpu/xstate.c +6 -0
+static int dump_xsave_layout_desc(struct coredump_params *cprm)
+{
+       int num_records = 0;
+       int i;
+
+       for_each_extended_xfeature(i, fpu_kernel_cfg.max_features) {

Modifying "fpu_kernel_cfg.max_features" to "fpu_user_cfg.max_features"

Supervisor bits also set in "fpu_kernel_cfg.max_features"

(https://github.com/torvalds/linux/blob/master/arch/x86/kernel/fpu/xstate.c#L777)

Quoted 11 lines
arch/x86/kernel/fpu/xstate.c +11 -0
+               struct xfeat_component xc = {
+                       .type   = i,
+                       .size   = xstate_sizes[i],
+                       .offset = xstate_offsets[i],
+                       /* reserved for future use */
+                       .flags  = 0,
+               };
+
+               if (!dump_emit(cprm, &xc, sizeof(xc)))
+                       return 0;
+

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1875

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+               num_records++;
+       }
+       return num_records;
+}
+
+static int get_xsave_desc_size(void)
+{
+       int cnt = 0;
+       int i;
+

changing "int" to "u32" as per review comment.

arch/x86/kernel/fpu/xstate.c int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, · line 1914

Quoted 11 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+       for_each_extended_xfeature(i, fpu_kernel_cfg.max_features)
+               cnt++;
+
+       return cnt * (sizeof(struct xfeat_component));
+}
+
 
+
+int elf_coredump_extra_notes_size(void)
+{
+       int size;

Adding below check, same as in "elf_coredump_extra_notes_write"

if (!fpu_user_cfg.max_features)

return 0;

thanks,
vigneshbalu.

Quoted 18 lines
arch/x86/kernel/fpu/xstate.c +6 -1
+       /* .note header */
+       size  = sizeof(struct elf_note);
+       /* name + align */
+       size += roundup(sizeof(owner_name), 4);
+       size += get_xsave_desc_size();
+
 
  #define NT_S390_TIMER  0x301           /* s390 timer register */
  #define NT_S390_TODCMP 0x302           /* s390 TOD clock comparator register */
--

2.43.0

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette