[PATCH V2] crash_notes: determine the note size when note_buf_t is absent
by Pu Hu
From: Pu Hu <hupu(a)transsion.com>
note_buf_t is a typedef of a u32 array in include/linux/vmcore_info.h.
It is not architecture-specific. Clang often omits the typedef from
vmlinux DWARF even when the crash_notes symbol is present.
SIZE(note_buf) then aborts session startup:
crash: invalid structure size: note_buf
have_crash_notes() and the per-architecture get_crash_notes() paths
all read the per-cpu crash note with that size. Add crash_note_size()
and use it at those call sites. Look up note_buf_t first and note_buf
second. When neither type is available, measure one NT_PRSTATUS note
from the per-cpu crash note in memory and include the terminating
empty Elf note header. The measured length stays in a local variable
and is not written into size_table. If it still cannot be determined,
warn and continue.
A vmlinux that already contains note_buf_t keeps the previous size.
No constant note length is introduced.
Signed-off-by: Pu Hu <hupu(a)transsion.com>
---
arm.c | 18 ++++++++++--
arm64.c | 22 ++++++++++++---
defs.h | 1 +
diskdump.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--
loongarch64.c | 18 ++++++++++--
mips.c | 18 ++++++++++--
mips64.c | 18 ++++++++++--
ppc.c | 10 +++++--
riscv64.c | 18 ++++++++++--
sadump.c | 15 ++++++----
symbols.c | 4 +--
task.c | 4 +--
12 files changed, 192 insertions(+), 32 deletions(-)
diff --git a/arm.c b/arm.c
index 0532469..36133a3 100644
--- a/arm.c
+++ b/arm.c
@@ -551,6 +551,7 @@ arm_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i, found;
+ long note_sz;
if (!symbol_exists("crash_notes"))
return;
@@ -577,13 +578,24 @@ arm_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = found = 0; i<kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", i);
continue;
@@ -610,7 +622,7 @@ arm_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf32_Nhdr);
+ long notesz = note_sz - sizeof(Elf32_Nhdr);
if (sizeof(Elf32_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/arm64.c b/arm64.c
index 39f745b..ff58226 100644
--- a/arm64.c
+++ b/arm64.c
@@ -2788,6 +2788,8 @@ arm64_stackframe_init(void)
struct syment *sp1, *sp1n, *sp2, *sp2n, *sp3, *sp3n;
STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (INVALID_SIZE(note_buf))
+ STRUCT_SIZE_INIT(note_buf, "note_buf");
STRUCT_SIZE_INIT(elf_prstatus, "elf_prstatus");
MEMBER_OFFSET_INIT(elf_prstatus_pr_pid, "elf_prstatus", "pr_pid");
MEMBER_OFFSET_INIT(elf_prstatus_pr_reg, "elf_prstatus", "pr_reg");
@@ -5066,6 +5068,7 @@ arm64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i, found;
+ long note_sz;
if (!symbol_exists("crash_notes")) {
if (DISKDUMP_DUMPFILE() || KDUMP_DUMPFILE()) {
@@ -5127,13 +5130,24 @@ arm64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return;
+ }
+
+ buf = GETBUF(note_sz);
if (!(ms->panic_task_regs = calloc((size_t)kt->cpus, sizeof(struct arm64_pt_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = found = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf),
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz,
"note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", i);
continue;
@@ -5157,10 +5171,10 @@ arm64_get_crash_notes(void)
note = netdump_get_prstatus_percpu(i);
if (note) {
/*
- * SIZE(note_buf) accounts for a "final note", which is a
+ * note_sz accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/defs.h b/defs.h
index c8708d1..cce535f 100644
--- a/defs.h
+++ b/defs.h
@@ -7569,6 +7569,7 @@ void x86_process_elf_notes(void *, unsigned long);
void *diskdump_get_prstatus_percpu(int);
int diskdump_is_cpu_prstatus_valid(int cpu);
int have_crash_notes(int cpu);
+long crash_note_size(ulong);
void map_cpus_to_prstatus_kdump_cmprs(void);
void diskdump_display_regs(int, FILE *);
void process_elf32_notes(void *, ulong);
diff --git a/diskdump.c b/diskdump.c
index 7263e2c..b7d28bc 100644
--- a/diskdump.c
+++ b/diskdump.c
@@ -104,11 +104,75 @@ int dumpfile_is_split(void)
return KDUMP_SPLIT();
}
+/*
+ * Bytes in one Elf note: header, aligned name, descriptor.
+ * Returns 0 when the header is not a usable NT_PRSTATUS note.
+ * Elf32_Nhdr and Elf64_Nhdr have the same three 32-bit fields.
+ */
+static long
+crash_note_size_from_mem(ulong addr)
+{
+ Elf64_Nhdr hdr;
+ long size, limit;
+
+ if (!addr)
+ return 0;
+
+ if (!readmem(addr, KVADDR, &hdr, sizeof(hdr),
+ "Elf64_Nhdr", RETURN_ON_ERROR|QUIET))
+ return 0;
+
+ limit = PAGESIZE() ? PAGESIZE() : 4096;
+ if (hdr.n_type != NT_PRSTATUS)
+ return 0;
+ if (!hdr.n_namesz || hdr.n_namesz > 256)
+ return 0;
+ if (!hdr.n_descsz || hdr.n_descsz > limit)
+ return 0;
+
+ size = sizeof(hdr);
+ size = roundup(size + hdr.n_namesz, 4);
+ size += hdr.n_descsz;
+ size = roundup(size, 4);
+ /* note_buf_t also contains the terminating empty note header */
+ size += sizeof(hdr);
+ return size;
+}
+
+/*
+ * Bytes to read from one per-cpu crash note.
+ *
+ * note_buf_t is a typedef of a u32 array in include/linux/vmcore_info.h.
+ * It is not architecture-specific. Clang often omits the typedef from
+ * DWARF, and SIZE(note_buf) then aborts startup. Look up note_buf_t,
+ * then note_buf. If neither type exists, measure the NT_PRSTATUS note
+ * at addr. A measured length is not written into size_table.
+ *
+ * Returns a positive size, or 0 if it cannot be determined.
+ */
+long
+crash_note_size(ulong addr)
+{
+ long sz;
+
+ if (!(VALID_SIZE(note_buf) && size_table.note_buf > 0)) {
+ STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (INVALID_SIZE(note_buf))
+ STRUCT_SIZE_INIT(note_buf, "note_buf");
+ }
+ if (VALID_SIZE(note_buf) && size_table.note_buf > 0)
+ return size_table.note_buf;
+
+ sz = crash_note_size_from_mem(addr);
+ return sz > 0 ? sz : 0;
+}
+
int have_crash_notes(int cpu)
{
ulong crash_notes, notes_ptr;
char *buf, *p;
Elf64_Nhdr *note = NULL;
+ long note_sz;
if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes,
sizeof(crash_notes), "crash_notes", RETURN_ON_ERROR)) {
@@ -121,11 +185,18 @@ int have_crash_notes(int cpu)
else
notes_ptr = crash_notes;
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptr);
+ if (note_sz <= 0) {
+ error(WARNING, "cpu %d: cannot determine size of note_buf_t\n", cpu);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!readmem(notes_ptr, KVADDR, buf,
- SIZE(note_buf), "note_buf_t", RETURN_ON_ERROR)) {
+ note_sz, "note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", cpu);
+ FREEBUF(buf);
return FALSE;
}
@@ -134,14 +205,17 @@ int have_crash_notes(int cpu)
if (note->n_type != NT_PRSTATUS) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (n_type != NT_PRSTATUS)\n", cpu);
+ FREEBUF(buf);
return FALSE;
}
if (!STRNEQ(p, "CORE")) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (name != \"CORE\")\n", cpu);
+ FREEBUF(buf);
return FALSE;
}
+ FREEBUF(buf);
return TRUE;
}
diff --git a/loongarch64.c b/loongarch64.c
index 6b518d4..1519de3 100644
--- a/loongarch64.c
+++ b/loongarch64.c
@@ -1559,6 +1559,7 @@ loongarch64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -1596,14 +1597,25 @@ loongarch64_get_crash_notes(void)
}
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -1631,7 +1643,7 @@ loongarch64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/mips.c b/mips.c
index d6602e3..70543eb 100644
--- a/mips.c
+++ b/mips.c
@@ -807,6 +807,7 @@ mips_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
if (!symbol_exists("crash_notes"))
return FALSE;
@@ -834,14 +835,25 @@ mips_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i=0;i<kt->cpus;i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING, "failed to read note_buf_t\n");
goto fail;
@@ -868,7 +880,7 @@ mips_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf32_Nhdr);
+ long notesz = note_sz - sizeof(Elf32_Nhdr);
if (sizeof(Elf32_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/mips64.c b/mips64.c
index b1d6acf..13f7e26 100644
--- a/mips64.c
+++ b/mips64.c
@@ -881,6 +881,7 @@ mips64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -914,14 +915,25 @@ mips64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -949,7 +961,7 @@ mips64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/ppc.c b/ppc.c
index cf5bf56..182e1d7 100644
--- a/ppc.c
+++ b/ppc.c
@@ -1984,6 +1984,7 @@ verify_crash_note_in_kernel(int cpu)
Elf32_Nhdr *note32;
ulong crash_notes_ptr;
char *buf, *name;
+ long note_sz;
ret = TRUE;
if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes_ptr,
@@ -1991,10 +1992,15 @@ verify_crash_note_in_kernel(int cpu)
!crash_notes_ptr)
goto out;
- buf = GETBUF(SIZE(note_buf));
if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF))
crash_notes_ptr += kt->__per_cpu_offset[cpu];
- if (!readmem(crash_notes_ptr, KVADDR, buf, SIZE(note_buf),
+ note_sz = crash_note_size(crash_notes_ptr);
+ if (note_sz <= 0) {
+ ret = FALSE;
+ goto out;
+ }
+ buf = GETBUF(note_sz);
+ if (!readmem(crash_notes_ptr, KVADDR, buf, note_sz,
"cpu crash_notes", QUIET|RETURN_ON_ERROR))
goto freebuf;
diff --git a/riscv64.c b/riscv64.c
index 1b6396c..1d011b6 100644
--- a/riscv64.c
+++ b/riscv64.c
@@ -1381,6 +1381,7 @@ riscv64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -1414,14 +1415,25 @@ riscv64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -1449,7 +1461,7 @@ riscv64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/sadump.c b/sadump.c
index cb43fdb..9fd05c9 100644
--- a/sadump.c
+++ b/sadump.c
@@ -1227,6 +1227,7 @@ get_prstatus_from_crash_notes(int cpu, char *prstatus)
ulong crash_notes, crash_notes_ptr, percpu_addr;
char *prstatus_ptr, *note_buf, *zero_buf, *name;
uint32_t *buf;
+ long note_sz;
if (cpu < 0 || kt->cpus <= cpu) {
error(INFO, "sadump: given cpu is invalid: %d\n", cpu);
@@ -1254,15 +1255,19 @@ get_prstatus_from_crash_notes(int cpu, char *prstatus)
? legacy_per_cpu_ptr(crash_notes_ptr, cpu)
: per_cpu_ptr(crash_notes_ptr, cpu);
- zero_buf = GETBUF(SIZE(note_buf));
- BZERO(zero_buf, SIZE(note_buf));
+ note_sz = crash_note_size(percpu_addr);
+ if (note_sz <= 0)
+ return FALSE;
+
+ zero_buf = GETBUF(note_sz);
+ BZERO(zero_buf, note_sz);
- note_buf = GETBUF(SIZE(note_buf));
+ note_buf = GETBUF(note_sz);
- readmem(percpu_addr, KVADDR, note_buf, SIZE(note_buf),
+ readmem(percpu_addr, KVADDR, note_buf, note_sz,
"read crash_notes", FAULT_ON_ERROR);
- if (memcmp(note_buf, zero_buf, SIZE(note_buf)) == 0)
+ if (memcmp(note_buf, zero_buf, note_sz) == 0)
return FALSE;
if (BITS64()) {
diff --git a/symbols.c b/symbols.c
index 270e14e..b1bc01e 100644
--- a/symbols.c
+++ b/symbols.c
@@ -12218,8 +12218,8 @@ dump_offset_table(char *spec, ulong makestruct)
SIZE(cpu_context_save));
fprintf(fp, " elf_prstatus: %ld\n",
SIZE(elf_prstatus));
- fprintf(fp, " note_buf: %ld\n",
- SIZE(note_buf));
+ fprintf(fp, " note_buf: %ld\n",
+ size_table.note_buf);
fprintf(fp, " unwind_idx: %ld\n",
SIZE(unwind_idx));
fprintf(fp, " s390_stack_frame: %ld\n",
diff --git a/task.c b/task.c
index d9d592e..994f137 100644
--- a/task.c
+++ b/task.c
@@ -687,8 +687,8 @@ task_init(void)
tt->this_task = pid_to_task(active_pid);
}
else {
- if (INVALID_SIZE(note_buf))
- STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (!(VALID_SIZE(note_buf) && size_table.note_buf > 0))
+ crash_note_size(0);
if (KDUMP_DUMPFILE())
map_cpus_to_prstatus();
--
2.43.0
21 hours, 43 minutes
[PATCH] arm64: determine the crash note size when note_buf_t is absent
by Pu Hu
From: Pu Hu <hupu(a)transsion.com>
note_buf_t is a typedef of a u32 array. Clang often omits it from
vmlinux DWARF even when the crash_notes symbol is present.
SIZE(note_buf) then aborts session startup:
crash: invalid structure size: note_buf
FILE: arm64.c FUNCTION: arm64_get_crash_notes()
Look up note_buf_t first and note_buf second. When neither type is
available, measure one NT_PRSTATUS note from the per-cpu crash note
in memory and include the terminating empty Elf note header. The
length stays local to arm64_get_crash_notes() and is not written into
size_table. If it still cannot be determined, warn and continue so a
raw RAM dump can be opened.
A vmlinux that already contains note_buf_t keeps the previous size.
No constant note length is introduced.
Signed-off-by: Pu Hu <hupu(a)transsion.com>
---
arm64.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 86 insertions(+), 4 deletions(-)
diff --git a/arm64.c b/arm64.c
index 39f745b..4f0b621
--- a/arm64.c
+++ b/arm64.c
@@ -2788,6 +2788,8 @@ arm64_stackframe_init(void)
struct syment *sp1, *sp1n, *sp2, *sp2n, *sp3, *sp3n;
STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (INVALID_SIZE(note_buf))
+ STRUCT_SIZE_INIT(note_buf, "note_buf");
STRUCT_SIZE_INIT(elf_prstatus, "elf_prstatus");
MEMBER_OFFSET_INIT(elf_prstatus_pr_pid, "elf_prstatus", "pr_pid");
MEMBER_OFFSET_INIT(elf_prstatus_pr_reg, "elf_prstatus", "pr_reg");
@@ -5053,6 +5055,78 @@ arm64_get_smp_cpus(void)
}
+/*
+ * Bytes described by one Elf note header: header + aligned name + descriptor.
+ * Returns 0 when the header cannot be a crash NT_PRSTATUS note.
+ */
+static long
+arm64_note_size_from_mem(ulong addr)
+{
+ Elf64_Nhdr hdr;
+ long size, limit;
+
+ if (!addr)
+ return 0;
+
+ if (!readmem(addr, KVADDR, &hdr, sizeof(hdr),
+ "Elf64_Nhdr", RETURN_ON_ERROR|QUIET))
+ return 0;
+
+ /*
+ * A corrupt header must not become an allocation size.
+ * NT_PRSTATUS names are short; one elf_prstatus fits in a page.
+ */
+ limit = PAGESIZE() ? PAGESIZE() : 4096;
+ if (hdr.n_type != NT_PRSTATUS)
+ return 0;
+ if (!hdr.n_namesz || hdr.n_namesz > 256)
+ return 0;
+ if (!hdr.n_descsz || hdr.n_descsz > limit)
+ return 0;
+
+ size = sizeof(hdr);
+ size = roundup(size + hdr.n_namesz, 4);
+ size += hdr.n_descsz;
+ size = roundup(size, 4);
+ /* note_buf_t also contains the terminating empty note */
+ size += sizeof(hdr);
+ return size;
+}
+
+/*
+ * Bytes to read from one per-cpu crash note.
+ *
+ * Use the vmlinux type when it exists. note_buf_t is a typedef of a u32
+ * array and Clang often omits it from DWARF, so SIZE(note_buf) would
+ * abort startup. In that case measure the NT_PRSTATUS note that is
+ * actually stored. The result is local to arm64_get_crash_notes() and
+ * is not written back to size_table.
+ *
+ * Returns a positive size, or 0 if it cannot be determined.
+ */
+static long
+arm64_crash_note_size(ulong *notes_ptrs)
+{
+ long sz;
+ ulong i;
+
+ if (VALID_SIZE(note_buf) && size_table.note_buf > 0)
+ return size_table.note_buf;
+
+ if (!notes_ptrs)
+ return 0;
+
+ for (i = 0; i < kt->cpus; i++) {
+ sz = arm64_note_size_from_mem(notes_ptrs[i]);
+ if (sz > 0) {
+ if (CRASHDEBUG(1))
+ fprintf(fp, "arm64_crash_note_size: %ld\n", sz);
+ return sz;
+ }
+ }
+ return 0;
+}
+
/*
* Retrieve task registers for the time of the crash.
*/
@@ -5066,6 +5140,7 @@ arm64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i, found;
+ long note_sz;
if (!symbol_exists("crash_notes")) {
if (DISKDUMP_DUMPFILE() || KDUMP_DUMPFILE()) {
@@ -5127,13 +5202,20 @@ arm64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = arm64_crash_note_size(notes_ptrs);
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return;
+ }
+
+ buf = GETBUF(note_sz);
if (!(ms->panic_task_regs = calloc((size_t)kt->cpus, sizeof(struct arm64_pt_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = found = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf),
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz,
"note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", i);
continue;
@@ -5157,10 +5239,10 @@ arm64_get_crash_notes(void)
note = netdump_get_prstatus_percpu(i);
if (note) {
/*
- * SIZE(note_buf) accounts for a "final note", which is a
+ * note_sz accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
--
2.43.0
21 hours, 59 minutes
[PATCH 00/13] doc: build the crash documentation website from the source tree
by Rui Yang
This series moves the crash documentation into the source tree and
builds the website from it:
- the whitepaper, the extension modules page and the maintenance
guide are converted to reStructuredText and built with Sphinx;
- "make doc" builds the documentation; "make website" additionally
generates the command help pages from the crash binary and the
changelog from the git log;
- a GitHub Actions workflow builds the site.
The pages were converted from the existing crash-utility.github.io
content, so some of it may be out of sync with the current code and
updates are welcome.
Testing:
- make -C doc website
- make website (requires a crash build)
Note:
- kudos to AI tools which helped a lot for the formating.
- Preview is available on my personal fork: https://daveyoung.github.io/crash/
- Once the series merged, I will update the crash-utility.github.io index page
Add a short link there redirect to crash-utility.github.io/crash/
Rui Yang (13):
doc: convert the crash whitepaper to reStructuredText and add make doc
build: remove doc/output from "make clean"
website: build the docs and command help into _site
ci: add the website workflow
website: declutter the landing page
website: convert the extensions and maintenance pages to RST
website: generate the command help as Sphinx pages
website: add the contribution guidelines link and reorder the
navigation
website: add release downloads and changelog links
website: generate the changelog from the git log
website: note that the documentation may be out of sync
website: add Debian, Ubuntu and openSUSE build links
doc: credit the RST conversion
.github/workflows/website.yml | 71 +
.gitignore | 4 +
Makefile | 31 +
doc/.gitignore | 2 +
doc/Makefile | 48 +
doc/_static/.gitkeep | 0
doc/_static/crash.jpg | Bin 0 -> 3479 bytes
doc/conf.py | 73 +
doc/extensions.rst | 617 ++++
doc/index.rst | 20 +
doc/links.rst | 13 +
doc/maintenance.rst | 5587 +++++++++++++++++++++++++++++++++
doc/whitepaper.rst | 3442 ++++++++++++++++++++
scripts/gen-changelog.py | 82 +
scripts/gen-help-rst.py | 106 +
15 files changed, 10096 insertions(+)
create mode 100644 .github/workflows/website.yml
create mode 100644 doc/.gitignore
create mode 100644 doc/Makefile
create mode 100644 doc/_static/.gitkeep
create mode 100644 doc/_static/crash.jpg
create mode 100644 doc/conf.py
create mode 100644 doc/extensions.rst
create mode 100644 doc/index.rst
create mode 100644 doc/links.rst
create mode 100644 doc/maintenance.rst
create mode 100644 doc/whitepaper.rst
create mode 100644 scripts/gen-changelog.py
create mode 100644 scripts/gen-help-rst.py
4 days, 23 hours
[PATCH 0/1] Recover modules from a damaged modules list
by Chao Huang
Hi,
A dumpfile may contain readable struct module objects while a missing
or corrupt list node prevents crash from walking the global modules
list. The current behavior sets NO_MODULE_ACCESS, so "mod -S" cannot
load symbols for any of the remaining modules.
The module_kset list provides an independent path to the same loaded
module objects on KMOD_V2 kernels. The patch uses it only after normal
module access fails and only for dumpfiles. Live kernels keep using the
existing path, avoiding addresses that may become stale.
The recovery reuses do_list() and the existing hash queue. It accepts
the readable prefix of a damaged module_kset list, which is useful for
incomplete dumps. Entries with module_kobject.mod == NULL are skipped.
They are sysfs objects for built-in modules, where THIS_MODULE is NULL.
For non-NULL entries, the embedded module.mkobj backlink is validated
before the struct module address is accepted.
Module symbol collection performs a second pass. The recovered
addresses are therefore kept in a local array and passed explicitly to
the symbol storage functions. This avoids adding persistent state to
kernel_table. Both the pre-6.4 layout and the module.mem layout added
in Linux 6.4 use the same recovered list. If no module is recovered,
the existing NO_MODULE_ACCESS behavior is preserved.
Tested with:
- ARM64 build: make target=ARM64 -j40
- Complete Linux 6.18 sysrq dump: 278 module objects loaded without the
fallback path.
- Incomplete dump: 473 module_kset entries were scanned. The fallback
recovered 346 loaded modules, skipped 126 built-in objects and one
unreadable entry, and "mod -S" loaded the available module symbols.
Only the recovery result is reported by the new code. Its wording
distinguishes loaded modules from built-in kobjects and applies to both
the pre-6.4 and 6.4+ module memory layouts.
Comments are welcome.
wya (1):
kernel: recover modules from module_kset
defs.h | 7 ++-
kernel.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++----
symbols.c | 33 ++++++++--
3 files changed, 195 insertions(+), 20 deletions(-)
--
2.43.0
1 week, 2 days
Self introduction
by Rui Yang
Hi list,
I'm Rui Yang, originally I use Dave Young when I was in Red Hat. But in daily life I use my Chinse name, it is "Rui Yang" in Chinese pinyin. Since I could attend more local conferences if possible, it is better to keep the name consistent to be the "Rui Yang" instead of "Dave Young", so I decided to change the email full name.
I know something about kernel and kexec/kdump and worked in Red Hat around 15 years in this area. Recently the original maintainers (Tao Liu, cced) can not pay too much time on crash maintenance, so I volunteered to maintain this project as a hobby. I'd like to continue to maintain it and improve it, keep in sync with kernel changes. I appreciate the previous great work from Dave Anderson, Lianbo Jiang, Tao Liu and Kazuhito Hagio.
To make crash better, I would welcome more contributors, either test, triage, feedback, or code review.
My recent personal plan about this project is to get more familiar with this project details (with assistance from AI):
* A crash-lab to learn the crash usage
* convert the crash-utility.github.io content to rst files under git repo, and automatically update them when source code changes.
* update the outdated docs if possible.
* Fix bugs I found, review/handle the github issues.
--
Thanks
Rui (AKA. Dave Young)
2 weeks, 5 days
[PATCH] netdump: do not close fd prematurely in proc_kcore_init()
by Curu Wong
When running crash on a live kernel without specifying /proc/kcore as an
explicit argument, crash prints an unexpected error message during startup:
/proc/kcore: read: Bad file descriptor
Commit ad427b7f8ffc ("fix eheader overflow") replaced BCOPY with
read(fd, pkd->elf_header, pkd->header_size) to support large ELF headers.
However, when kcore_fd == UNUSED, fd was already closed immediately after
lseek(fd, 0, SEEK_SET) before read():
if (lseek(fd, 0, SEEK_SET) != 0) {
sprintf(buf, "/proc/kcore: lseek");
perror(buf);
goto bailout;
}
if (fd != kcore_fd)
close(fd);
This causes the subsequent read(fd, pkd->elf_header, pkd->header_size)
to fail with EBADF.
Move close(fd) to the exit points so that fd remains open until all
reads are complete. Also ensure fd is closed via bailout if e_phoff
validation fails.
Fixes: ad427b7f8ffc ("fix eheader overflow")
Signed-off-by: Curu Wong <prinbra(a)gmail.com>
---
netdump.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/netdump.c b/netdump.c
index 64d4edc..b2cf26d 100644
--- a/netdump.c
+++ b/netdump.c
@@ -4652,13 +4652,10 @@ proc_kcore_init_32(FILE *fp, int kcore_fd)
goto bailout;
}
- if (fd != kcore_fd)
- close(fd);
-
elf32 = (Elf32_Ehdr *)&eheader[0];
if (elf32->e_phoff > sizeof(eheader) - 2 * sizeof(Elf32_Phdr)) {
error(INFO, "/proc/kcore: ELF program header offset too big!\n");
- return FALSE;
+ goto bailout;
}
notes32 = (Elf32_Phdr *)&eheader[elf32->e_phoff];
load32 = notes32 + 1;
@@ -4691,6 +4688,9 @@ proc_kcore_init_32(FILE *fp, int kcore_fd)
kcore_memory_dump(CRASHDEBUG(1) ? fp : pc->nullfp);
+ if (fd != kcore_fd)
+ close(fd);
+
return TRUE;
bailout:
@@ -4730,13 +4730,10 @@ proc_kcore_init_64(FILE *fp, int kcore_fd)
goto bailout;
}
- if (fd != kcore_fd)
- close(fd);
-
elf64 = (Elf64_Ehdr *)&eheader[0];
if (elf64->e_phoff > sizeof(eheader) - 2 * sizeof(Elf64_Phdr)) {
error(INFO, "/proc/kcore: ELF program header offset too big!\n");
- return FALSE;
+ goto bailout;
}
notes64 = (Elf64_Phdr *)&eheader[elf64->e_phoff];
load64 = notes64 + 1;
@@ -4770,6 +4767,9 @@ proc_kcore_init_64(FILE *fp, int kcore_fd)
kcore_memory_dump(CRASHDEBUG(1) ? fp : pc->nullfp);
+ if (fd != kcore_fd)
+ close(fd);
+
return TRUE;
bailout:
--
2.43.7
2 weeks, 5 days