Skip to contentSkip to conversation

Limit the size of vmcore-dmesg.txt to 2G

PATCH Superseded Lianbo Jiang 2 participants 8 messages Aug 15, 2019 – Aug 23, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Limit the size of vmcore-dmesg.txt to 2G

[PATCH 1/2] cleanup: move it back from util_lib/elf_info.c Some code related to vmcore-dmesg.c is put into the util_lib, which is not very reasonable, so lets move it back and tidy up those code.

In addition, that will also help to limit the size of vmcore-dmesg.txt.

[PATCH 2/2] Limit the size of vmcore-dmesg.txt to 2G With some corrupted vmcore files, the vmcore-dmesg.txt file may grow forever till the kdump disk becomes full. Lets limit the size of vmcore-dmesg.txt to avoid such problems.

BTW: I tested this patch on x86 64 and arm64, it also worked well.

Lianbo Jiang (2):
cleanup: move it back from util_lib/elf_info.c
Limit the size of vmcore-dmesg.txt to 2G

 kexec/arch/arm64/kexec-arm64.c |  2 +-
 util_lib/elf_info.c            | 73 ++++++++--------------------------
 util_lib/include/elf_info.h    |  8 +++-
 vmcore-dmesg/vmcore-dmesg.c    | 54 ++++++++++++++++++++++---
 4 files changed, 71 insertions(+), 66 deletions(-)
Signature · 7 lines
2.17.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH 1/2 cleanup: move it back from util_lib/elf_info.c

Some code related to vmcore-dmesg.c is put into the util_lib, which is not very reasonable, so lets move it back and tidy up those code.

In addition, that will also help to limit the size of vmcore-dmesg.txt.

---
 kexec/arch/arm64/kexec-arm64.c |  2 +-
 util_lib/elf_info.c            | 73 ++++++++--------------------------
 util_lib/include/elf_info.h    |  8 +++-
 vmcore-dmesg/vmcore-dmesg.c    | 44 +++++++++++++++++---
 4 files changed, 61 insertions(+), 66 deletions(-)
kexec/arch/arm64/kexec-arm64.c +1 -1
index eb3a3a37307c..6ad3b0a134b3 100644
@@ -889,7 +889,7 @@ int get_phys_base_from_pt_load(unsigned long *phys_offset)
 		return EFAILED;
 	}
 
-	read_elf_kcore(fd);
+	read_elf(fd);
 
 	for (i = 0; get_pt_load(i,
 		    &phys_start, NULL, &virt_start, NULL);
util_lib/elf_info.c +16 -57

144 lines not on this page · raw message

util_lib/include/elf_info.h +6 -2
index 1a4debd2d4ba..8ee7d3e2763f 100644
@@ -23,13 +23,17 @@
 #include <inttypes.h>
 #include <ctype.h>
 
+typedef void (*handler_t)(char *msg, unsigned int bytes);
+
 int get_pt_load(int idx,
 	unsigned long long *phys_start,
 	unsigned long long *phys_end,
 	unsigned long long *virt_start,
 	unsigned long long *virt_end);
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
-int read_elf_kcore(int fd);
-int read_elf_vmcore(int fd);
+int check_log_first_idx_vaddr(void);
+void dump_dmesg_structured(int fd, handler_t handler);
+void dump_dmesg_legacy(int fd, handler_t handler);
+int read_elf(int fd);
 
 #endif /* ELF_INFO_H */
vmcore-dmesg/vmcore-dmesg.c +38 -6

67 lines not on this page · raw message

Signature · 7 lines
2.17.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

Re: [PATCH 1/2] cleanup: move it back from util_lib/elf_info.c

Hi Lianbo,

I like where this patch is going but I would like to request a few changes. Please see comments inline.

Quoted 39 lines · Lianbo Jiang

Some code related to vmcore-dmesg.c is put into the util_lib, which is not very reasonable, so lets move it back and tidy up those code.

In addition, that will also help to limit the size of vmcore-dmesg.txt.

---
 kexec/arch/arm64/kexec-arm64.c |  2 +-
 util_lib/elf_info.c            | 73 ++++++++--------------------------
 util_lib/include/elf_info.h    |  8 +++-
 vmcore-dmesg/vmcore-dmesg.c    | 44 +++++++++++++++++---
 4 files changed, 61 insertions(+), 66 deletions(-)
kexec/arch/arm64/kexec-arm64.c +1 -1
index eb3a3a37307c..6ad3b0a134b3 100644
@@ -889,7 +889,7 @@ int get_phys_base_from_pt_load(unsigned long *phys_offset)
 		return EFAILED;
 	}
 
-	read_elf_kcore(fd);
+	read_elf(fd);
 
 	for (i = 0; get_pt_load(i,
 		    &phys_start, NULL, &virt_start, NULL);
util_lib/elf_info.c +0 -1
index 90a3b21662e7..2f254e972721 100644
@@ -20,7 +20,6 @@
 /* The 32bit and 64bit note headers make it clear we don't care */
 typedef Elf32_Nhdr Elf_Nhdr;
 
-static const char *fname;
 static Elf64_Ehdr ehdr;
 static Elf64_Phdr *phdr;
 static int num_pt_loads;

util_lib/elf_info.c void read_elf32(int fd) · line 123

Quoted 7 lines
util_lib/elf_info.c +2 -2
@@ -120,8 +119,8 @@ void read_elf32(int fd)
 
 	ret = pread(fd, &ehdr32, sizeof(ehdr32), 0);
 	if (ret != sizeof(ehdr32)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));

I'm not sure of the merit of changing the loging output. And moreover I don't think it belongs in this patch as it doesn't seem related to the other changes.

Quoted 115 lines
util_lib/elf_info.c +13 -42
 		exit(10);
 	}
 
@@ -193,8 +192,8 @@ void read_elf64(int fd)
 
 	ret = pread(fd, &ehdr64, sizeof(ehdr64), 0);
 	if (ret < 0 || (size_t)ret != sizeof(ehdr)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));
 		exit(10);
 	}
 
@@ -531,19 +530,7 @@ static int32_t read_file_s32(int fd, uint64_t addr)
 	return read_file_u32(fd, addr);
 }
 
-static void write_to_stdout(char *buf, unsigned int nr)
-{
-	ssize_t ret;
-
-	ret = write(STDOUT_FILENO, buf, nr);
-	if (ret != nr) {
-		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
-			" %s\n", strerror(errno));
-		exit(54);
-	}
-}
-
-static void dump_dmesg_legacy(int fd)
+void dump_dmesg_legacy(int fd, handler_t handler)
 {
 	uint64_t log_buf, log_buf_offset;
 	unsigned log_end, logged_chars, log_end_wrapped;
@@ -604,7 +591,7 @@ static void dump_dmesg_legacy(int fd)
 	 */
 	logged_chars = log_end < log_buf_len ? log_end : log_buf_len;
 
-	write_to_stdout(buf + (log_buf_len - logged_chars), logged_chars);
+	handler(buf + (log_buf_len - logged_chars), logged_chars);
 }
 
 static inline uint16_t struct_val_u16(char *ptr, unsigned int offset)
@@ -623,7 +610,7 @@ static inline uint64_t struct_val_u64(char *ptr, unsigned int offset)
 }
 
 /* Read headers of log records and dump accordingly */
-static void dump_dmesg_structured(int fd)
+void dump_dmesg_structured(int fd, handler_t handler)
 {
 #define OUT_BUF_SIZE	4096
 	uint64_t log_buf, log_buf_offset, ts_nsec;
@@ -733,7 +720,7 @@ static void dump_dmesg_structured(int fd)
 				out_buf[len++] = c;
 
 			if (len >= OUT_BUF_SIZE - 64) {
-				write_to_stdout(out_buf, len);
+				handler(out_buf, len);
 				len = 0;
 			}
 		}
@@ -753,25 +740,24 @@ static void dump_dmesg_structured(int fd)
 	}
 	free(buf);
 	if (len)
-		write_to_stdout(out_buf, len);
+		handler(out_buf, len);
 }
 
-static void dump_dmesg(int fd)
+int check_log_first_idx_vaddr(void)
 {
 	if (log_first_idx_vaddr)
-		dump_dmesg_structured(fd);
-	else
-		dump_dmesg_legacy(fd);
+		return 1;
+
+	return 0;
 }
 
-static int read_elf(int fd)
+int read_elf(int fd)
 {
 	int ret;
 
 	ret = pread(fd, ehdr.e_ident, EI_NIDENT, 0);
 	if (ret != EI_NIDENT) {
-		fprintf(stderr, "Read of e_ident from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of e_ident failed: %s\n", strerror(errno));
 		return 3;
 	}
 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
@@ -808,40 +794,13 @@ static int read_elf(int fd)
 	return 0;
 }
 
-int read_elf_vmcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
-	if (ret > 0) {
-		fprintf(stderr, "Unable to read ELF information"
-			" from vmcore\n");
-		return ret;
-	}
-
-	dump_dmesg(fd);
-
-	return 0;
-}
-

util_lib/elf_info.c static int read_elf(int fd) · line 836

Quoted 10 lines
util_lib/elf_info.c +0 -10
-int read_elf_kcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
-	if (ret != 0)
-		return ret;
-
-	return 0;
-}

I think that removing read_elf_kcore is not related to the rest of this patch and should be in a separate patch - it is a nice cleanup.

Quoted 17 lines
util_lib/elf_info.c +1 -2
-
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off)
 {
 	int ret;
 
 	*phys_off = UINT64_MAX;
 
-	ret = read_elf_kcore(fd);
+	ret = read_elf(fd);
 	if (!ret) {
 		/* If we have a valid 'PHYS_OFFSET' by now,
 		 * return it to the caller now.
util_lib/include/elf_info.h +0 -0
index 1a4debd2d4ba..8ee7d3e2763f 100644

util_lib/include/elf_info.h line 26

Quoted 4 lines
util_lib/include/elf_info.h +1 -0
@@ -23,13 +23,17 @@
 #include <inttypes.h>
 #include <ctype.h>
 
+typedef void (*handler_t)(char *msg, unsigned int bytes);

I would prefer it if we did not add new typedefs.

Quoted 33 lines
util_lib/include/elf_info.h +5 -2
+
 int get_pt_load(int idx,
 	unsigned long long *phys_start,
 	unsigned long long *phys_end,
 	unsigned long long *virt_start,
 	unsigned long long *virt_end);
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
-int read_elf_kcore(int fd);
-int read_elf_vmcore(int fd);
+int check_log_first_idx_vaddr(void);
+void dump_dmesg_structured(int fd, handler_t handler);
+void dump_dmesg_legacy(int fd, handler_t handler);
+int read_elf(int fd);
 
 #endif /* ELF_INFO_H */
vmcore-dmesg/vmcore-dmesg.c +9 -2
index 7a386b380291..ff0d540c9130 100644
@@ -1,21 +1,53 @@
 #include <elf_info.h>
 
-/* The 32bit and 64bit note headers make it clear we don't care */
-typedef Elf32_Nhdr Elf_Nhdr;
+static void write_to_stdout(char *buf, unsigned int nr)
+{
+	ssize_t ret;
+
+	ret = write(STDOUT_FILENO, buf, nr);
+	if (ret != nr) {
+		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
+				" %s\n", strerror(errno));
+		exit(54);

vmcore-dmesg/vmcore-dmesg.c line 21

Quoted 10 lines
vmcore-dmesg/vmcore-dmesg.c +10 -0
+	}
+}
+
+static void dump_dmesg(int fd, handler_t handler)
+{
+	if (check_log_first_idx_vaddr())
+		dump_dmesg_structured(fd, handler);
+	else
+		dump_dmesg_legacy(fd, handler);
+}

I think that dump_dmesg() could stay in kexec-arm64.c, it does not seem specific to vmcore handling.

Quoted 53 lines
vmcore-dmesg/vmcore-dmesg.c +19 -4
-static const char *fname;
+static int read_vmcore_dmesg(int fd)
+{
+        int ret;
+
+        ret = read_elf(fd);
+        if (ret > 0) {
+                fprintf(stderr, "Unable to read ELF information"
+                        " from vmcore\n");
+                return ret;
+        }
+
+        dump_dmesg(fd, write_to_stdout);
+
+        return 0;
+}
 
 int main(int argc, char **argv)
 {
 	ssize_t ret;
 	int fd;
+	const char *fname;
 
 	if (argc != 2) {
 		fprintf(stderr, "usage: %s <kernel core file>\n", argv[0]);
 		return 1;
 	}
-	fname = argv[1];
 
+	fname = argv[1];
 	fd = open(fname, O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open %s: %s\n",
@@ -23,8 +55,8 @@ int main(int argc, char **argv)
 		return 2;
 	}
 
-	ret = read_elf_vmcore(fd);
-	
+	ret = read_vmcore_dmesg(fd);
+
 	close(fd);
 
 	return ret;
Signature · 7 lines
2.17.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

author
#3 lore raw

Re: [PATCH 1/2] cleanup: move it back from util_lib/elf_info.c

Quoted 5 lines · Simon Horman

Hi Lianbo,

I like where this patch is going but I would like to request a few changes. Please see comments inline.

Thanks for your comment, Simon.

Quoted 41 lines · Lianbo Jiang
Quoted 40 lines

Some code related to vmcore-dmesg.c is put into the util_lib, which is not very reasonable, so lets move it back and tidy up those code.

In addition, that will also help to limit the size of vmcore-dmesg.txt.

---
 kexec/arch/arm64/kexec-arm64.c |  2 +-
 util_lib/elf_info.c            | 73 ++++++++--------------------------
 util_lib/include/elf_info.h    |  8 +++-
 vmcore-dmesg/vmcore-dmesg.c    | 44 +++++++++++++++++---
 4 files changed, 61 insertions(+), 66 deletions(-)
kexec/arch/arm64/kexec-arm64.c +1 -1
index eb3a3a37307c..6ad3b0a134b3 100644
@@ -889,7 +889,7 @@ int get_phys_base_from_pt_load(unsigned long *phys_offset)
 		return EFAILED;
 	}
 
-	read_elf_kcore(fd);
+	read_elf(fd);
 
 	for (i = 0; get_pt_load(i,
 		    &phys_start, NULL, &virt_start, NULL);
util_lib/elf_info.c +0 -1
index 90a3b21662e7..2f254e972721 100644
@@ -20,7 +20,6 @@
 /* The 32bit and 64bit note headers make it clear we don't care */
 typedef Elf32_Nhdr Elf_Nhdr;
 
-static const char *fname;
 static Elf64_Ehdr ehdr;
 static Elf64_Phdr *phdr;
 static int num_pt_loads;
@@ -120,8 +119,8 @@ void read_elf32(int fd)
 
Quoted 8 lines

util_lib/elf_info.c void read_elf32(int fd) · line 123

Quoted 6 lines
util_lib/elf_info.c +2 -2
 	ret = pread(fd, &ehdr32, sizeof(ehdr32), 0);
 	if (ret != sizeof(ehdr32)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));

I'm not sure of the merit of changing the loging output.

The variable 'fname' is defined two twice, the first definition is in the vmcore-dmesg.c, and the second definition is in the elf_info.c. That is confused although it's a static type, because i do not see the place where the value of variable 'fname' is set in elf_info.c. So i guess that it should be a same variable within the vmcore-dmesg.c and also need to clean up.

Quoted 3 lines

And moreover I don't think it belongs in this patch as it doesn't seem related to the other changes.

Good question. I will consider how to clean up. Probably, it should be a separate patch.

Quoted 120 lines
Quoted 120 lines
util_lib/elf_info.c +13 -47
 		exit(10);
 	}
 
@@ -193,8 +192,8 @@ void read_elf64(int fd)
 
 	ret = pread(fd, &ehdr64, sizeof(ehdr64), 0);
 	if (ret < 0 || (size_t)ret != sizeof(ehdr)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));
 		exit(10);
 	}
 
@@ -531,19 +530,7 @@ static int32_t read_file_s32(int fd, uint64_t addr)
 	return read_file_u32(fd, addr);
 }
 
-static void write_to_stdout(char *buf, unsigned int nr)
-{
-	ssize_t ret;
-
-	ret = write(STDOUT_FILENO, buf, nr);
-	if (ret != nr) {
-		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
-			" %s\n", strerror(errno));
-		exit(54);
-	}
-}
-
-static void dump_dmesg_legacy(int fd)
+void dump_dmesg_legacy(int fd, handler_t handler)
 {
 	uint64_t log_buf, log_buf_offset;
 	unsigned log_end, logged_chars, log_end_wrapped;
@@ -604,7 +591,7 @@ static void dump_dmesg_legacy(int fd)
 	 */
 	logged_chars = log_end < log_buf_len ? log_end : log_buf_len;
 
-	write_to_stdout(buf + (log_buf_len - logged_chars), logged_chars);
+	handler(buf + (log_buf_len - logged_chars), logged_chars);
 }
 
 static inline uint16_t struct_val_u16(char *ptr, unsigned int offset)
@@ -623,7 +610,7 @@ static inline uint64_t struct_val_u64(char *ptr, unsigned int offset)
 }
 
 /* Read headers of log records and dump accordingly */
-static void dump_dmesg_structured(int fd)
+void dump_dmesg_structured(int fd, handler_t handler)
 {
 #define OUT_BUF_SIZE	4096
 	uint64_t log_buf, log_buf_offset, ts_nsec;
@@ -733,7 +720,7 @@ static void dump_dmesg_structured(int fd)
 				out_buf[len++] = c;
 
 			if (len >= OUT_BUF_SIZE - 64) {
-				write_to_stdout(out_buf, len);
+				handler(out_buf, len);
 				len = 0;
 			}
 		}
@@ -753,25 +740,24 @@ static void dump_dmesg_structured(int fd)
 	}
 	free(buf);
 	if (len)
-		write_to_stdout(out_buf, len);
+		handler(out_buf, len);
 }
 
-static void dump_dmesg(int fd)
+int check_log_first_idx_vaddr(void)
 {
 	if (log_first_idx_vaddr)
-		dump_dmesg_structured(fd);
-	else
-		dump_dmesg_legacy(fd);
+		return 1;
+
+	return 0;
 }
 
-static int read_elf(int fd)
+int read_elf(int fd)
 {
 	int ret;
 
 	ret = pread(fd, ehdr.e_ident, EI_NIDENT, 0);
 	if (ret != EI_NIDENT) {
-		fprintf(stderr, "Read of e_ident from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of e_ident failed: %s\n", strerror(errno));
 		return 3;
 	}
 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
@@ -808,40 +794,13 @@ static int read_elf(int fd)
 	return 0;
 }
 
-int read_elf_vmcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
-	if (ret > 0) {
-		fprintf(stderr, "Unable to read ELF information"
-			" from vmcore\n");
-		return ret;
-	}
-
-	dump_dmesg(fd);
-
-	return 0;
-}
-
-int read_elf_kcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
Quoted 9 lines

util_lib/elf_info.c static int read_elf(int fd) · line 836

Quoted 5 lines
util_lib/elf_info.c +0 -5
-	if (ret != 0)
-		return ret;
-
-	return 0;
-}

I think that removing read_elf_kcore is not related to the rest of this patch and should be in a separate patch - it is a nice cleanup.

Good idea, i will split them into a separate patch.

Quoted 20 lines
Quoted 20 lines
util_lib/elf_info.c +1 -2
-
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off)
 {
 	int ret;
 
 	*phys_off = UINT64_MAX;
 
-	ret = read_elf_kcore(fd);
+	ret = read_elf(fd);
 	if (!ret) {
 		/* If we have a valid 'PHYS_OFFSET' by now,
 		 * return it to the caller now.
util_lib/include/elf_info.h +0 -0
index 1a4debd2d4ba..8ee7d3e2763f 100644
@@ -23,13 +23,17 @@
 #include <inttypes.h>
 #include <ctype.h>
 
Quoted 4 lines

util_lib/include/elf_info.h line 26

Quoted 1 line
util_lib/include/elf_info.h +1 -0
+typedef void (*handler_t)(char *msg, unsigned int bytes);

I would prefer it if we did not add new typedefs.

OK. I'm considering how to handle it. Any suggestions?

Quoted 38 lines
Quoted 38 lines
util_lib/include/elf_info.h +5 -2
+
 int get_pt_load(int idx,
 	unsigned long long *phys_start,
 	unsigned long long *phys_end,
 	unsigned long long *virt_start,
 	unsigned long long *virt_end);
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
-int read_elf_kcore(int fd);
-int read_elf_vmcore(int fd);
+int check_log_first_idx_vaddr(void);
+void dump_dmesg_structured(int fd, handler_t handler);
+void dump_dmesg_legacy(int fd, handler_t handler);
+int read_elf(int fd);
 
 #endif /* ELF_INFO_H */
vmcore-dmesg/vmcore-dmesg.c +14 -2
index 7a386b380291..ff0d540c9130 100644
@@ -1,21 +1,53 @@
 #include <elf_info.h>
 
-/* The 32bit and 64bit note headers make it clear we don't care */
-typedef Elf32_Nhdr Elf_Nhdr;
+static void write_to_stdout(char *buf, unsigned int nr)
+{
+	ssize_t ret;
+
+	ret = write(STDOUT_FILENO, buf, nr);
+	if (ret != nr) {
+		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
+				" %s\n", strerror(errno));
+		exit(54);
+	}
+}
+
+static void dump_dmesg(int fd, handler_t handler)
+{
Quoted 9 lines

vmcore-dmesg/vmcore-dmesg.c line 21

Quoted 5 lines
vmcore-dmesg/vmcore-dmesg.c +5 -0
+	if (check_log_first_idx_vaddr())
+		dump_dmesg_structured(fd, handler);
+	else
+		dump_dmesg_legacy(fd, handler);
+}

I think that dump_dmesg() could stay in kexec-arm64.c, it does not seem specific to vmcore handling.

The dump_dmesg() should be a common function, not only arm64 will use it, but x86 64 will also call it. It would be good to put the dump_dmesg() to a common place.

Quoted 48 lines
Quoted 48 lines
vmcore-dmesg/vmcore-dmesg.c +19 -4
-static const char *fname;
+static int read_vmcore_dmesg(int fd)
+{
+        int ret;
+
+        ret = read_elf(fd);
+        if (ret > 0) {
+                fprintf(stderr, "Unable to read ELF information"
+                        " from vmcore\n");
+                return ret;
+        }
+
+        dump_dmesg(fd, write_to_stdout);
+
+        return 0;
+}
 
 int main(int argc, char **argv)
 {
 	ssize_t ret;
 	int fd;
+	const char *fname;
 
 	if (argc != 2) {
 		fprintf(stderr, "usage: %s <kernel core file>\n", argv[0]);
 		return 1;
 	}
-	fname = argv[1];
 
+	fname = argv[1];
 	fd = open(fname, O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open %s: %s\n",
@@ -23,8 +55,8 @@ int main(int argc, char **argv)
 		return 2;
 	}
 
-	ret = read_elf_vmcore(fd);
-	
+	ret = read_vmcore_dmesg(fd);
+
 	close(fd);
 
 	return ret;
Signature · 1 line
2.17.1
Quoted 5 lines
Quoted 5 lines

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

author
#4 lore raw

Re: [PATCH 1/2] cleanup: move it back from util_lib/elf_info.c

Quoted 59 lines · lijiang
Quoted 5 lines · Simon Horman

Hi Lianbo,

I like where this patch is going but I would like to request a few changes. Please see comments inline.

Thanks for your comment, Simon.

Quoted 41 lines · Lianbo Jiang
Quoted 40 lines

Some code related to vmcore-dmesg.c is put into the util_lib, which is not very reasonable, so lets move it back and tidy up those code.

In addition, that will also help to limit the size of vmcore-dmesg.txt.

---
 kexec/arch/arm64/kexec-arm64.c |  2 +-
 util_lib/elf_info.c            | 73 ++++++++--------------------------
 util_lib/include/elf_info.h    |  8 +++-
 vmcore-dmesg/vmcore-dmesg.c    | 44 +++++++++++++++++---
 4 files changed, 61 insertions(+), 66 deletions(-)
kexec/arch/arm64/kexec-arm64.c +1 -1
index eb3a3a37307c..6ad3b0a134b3 100644
@@ -889,7 +889,7 @@ int get_phys_base_from_pt_load(unsigned long *phys_offset)
 		return EFAILED;
 	}
 
-	read_elf_kcore(fd);
+	read_elf(fd);
 
 	for (i = 0; get_pt_load(i,
 		    &phys_start, NULL, &virt_start, NULL);
util_lib/elf_info.c +0 -1
index 90a3b21662e7..2f254e972721 100644
@@ -20,7 +20,6 @@
 /* The 32bit and 64bit note headers make it clear we don't care */
 typedef Elf32_Nhdr Elf_Nhdr;
 
-static const char *fname;
 static Elf64_Ehdr ehdr;
 static Elf64_Phdr *phdr;
 static int num_pt_loads;
@@ -120,8 +119,8 @@ void read_elf32(int fd)
 
Quoted 8 lines

util_lib/elf_info.c void read_elf32(int fd) · line 123

Quoted 6 lines
util_lib/elf_info.c +2 -2
 	ret = pread(fd, &ehdr32, sizeof(ehdr32), 0);
 	if (ret != sizeof(ehdr32)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));

I'm not sure of the merit of changing the loging output.

Quoted 5 lines

The variable 'fname' is defined two twice, the first definition is in the vmcore-dmesg.c, and the second definition is in the elf_info.c. That is confused although it's a static type, because i do not see the place where the value of variable 'fname' is set in elf_info.c. So i guess that it should be a same variable within the vmcore-dmesg.c and also need to clean up.

BTW: i guess the original definition of 'fname' should look like this:

util_lib/elf_info.c +1 -1
index d9397ecd8626..5d0efaafab53 100644
@@ -20,7 +20,7 @@
 /* The 32bit and 64bit note headers make it clear we don't care */
 typedef Elf32_Nhdr Elf_Nhdr;
 
-static const char *fname;
+const char *fname;
 static Elf64_Ehdr ehdr;
 static Elf64_Phdr *phdr;
 static int num_pt_loads;
vmcore-dmesg/vmcore-dmesg.c +1 -1
index 7a386b380291..bebc348a657e 100644
@@ -3,7 +3,7 @@
 /* The 32bit and 64bit note headers make it clear we don't care */
 typedef Elf32_Nhdr Elf_Nhdr;
 
-static const char *fname;
+extern const char *fname;
 
 int main(int argc, char **argv)
 {
Quoted 264 lines
Quoted 3 lines

And moreover I don't think it belongs in this patch as it doesn't seem related to the other changes.

Good question. I will consider how to clean up. Probably, it should be a separate patch.

Quoted 120 lines
Quoted 120 lines
util_lib/elf_info.c +13 -47
 		exit(10);
 	}
 
@@ -193,8 +192,8 @@ void read_elf64(int fd)
 
 	ret = pread(fd, &ehdr64, sizeof(ehdr64), 0);
 	if (ret < 0 || (size_t)ret != sizeof(ehdr)) {
-		fprintf(stderr, "Read of Elf header from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of Elf header failed in %s: %s\n",
+				__func__, strerror(errno));
 		exit(10);
 	}
 
@@ -531,19 +530,7 @@ static int32_t read_file_s32(int fd, uint64_t addr)
 	return read_file_u32(fd, addr);
 }
 
-static void write_to_stdout(char *buf, unsigned int nr)
-{
-	ssize_t ret;
-
-	ret = write(STDOUT_FILENO, buf, nr);
-	if (ret != nr) {
-		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
-			" %s\n", strerror(errno));
-		exit(54);
-	}
-}
-
-static void dump_dmesg_legacy(int fd)
+void dump_dmesg_legacy(int fd, handler_t handler)
 {
 	uint64_t log_buf, log_buf_offset;
 	unsigned log_end, logged_chars, log_end_wrapped;
@@ -604,7 +591,7 @@ static void dump_dmesg_legacy(int fd)
 	 */
 	logged_chars = log_end < log_buf_len ? log_end : log_buf_len;
 
-	write_to_stdout(buf + (log_buf_len - logged_chars), logged_chars);
+	handler(buf + (log_buf_len - logged_chars), logged_chars);
 }
 
 static inline uint16_t struct_val_u16(char *ptr, unsigned int offset)
@@ -623,7 +610,7 @@ static inline uint64_t struct_val_u64(char *ptr, unsigned int offset)
 }
 
 /* Read headers of log records and dump accordingly */
-static void dump_dmesg_structured(int fd)
+void dump_dmesg_structured(int fd, handler_t handler)
 {
 #define OUT_BUF_SIZE	4096
 	uint64_t log_buf, log_buf_offset, ts_nsec;
@@ -733,7 +720,7 @@ static void dump_dmesg_structured(int fd)
 				out_buf[len++] = c;
 
 			if (len >= OUT_BUF_SIZE - 64) {
-				write_to_stdout(out_buf, len);
+				handler(out_buf, len);
 				len = 0;
 			}
 		}
@@ -753,25 +740,24 @@ static void dump_dmesg_structured(int fd)
 	}
 	free(buf);
 	if (len)
-		write_to_stdout(out_buf, len);
+		handler(out_buf, len);
 }
 
-static void dump_dmesg(int fd)
+int check_log_first_idx_vaddr(void)
 {
 	if (log_first_idx_vaddr)
-		dump_dmesg_structured(fd);
-	else
-		dump_dmesg_legacy(fd);
+		return 1;
+
+	return 0;
 }
 
-static int read_elf(int fd)
+int read_elf(int fd)
 {
 	int ret;
 
 	ret = pread(fd, ehdr.e_ident, EI_NIDENT, 0);
 	if (ret != EI_NIDENT) {
-		fprintf(stderr, "Read of e_ident from %s failed: %s\n",
-			fname, strerror(errno));
+		fprintf(stderr, "Read of e_ident failed: %s\n", strerror(errno));
 		return 3;
 	}
 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
@@ -808,40 +794,13 @@ static int read_elf(int fd)
 	return 0;
 }
 
-int read_elf_vmcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
-	if (ret > 0) {
-		fprintf(stderr, "Unable to read ELF information"
-			" from vmcore\n");
-		return ret;
-	}
-
-	dump_dmesg(fd);
-
-	return 0;
-}
-
-int read_elf_kcore(int fd)
-{
-	int ret;
-
-	ret = read_elf(fd);
Quoted 9 lines

util_lib/elf_info.c static int read_elf(int fd) · line 836

Quoted 5 lines
util_lib/elf_info.c +0 -5
-	if (ret != 0)
-		return ret;
-
-	return 0;
-}

I think that removing read_elf_kcore is not related to the rest of this patch and should be in a separate patch - it is a nice cleanup.

Good idea, i will split them into a separate patch.

Quoted 20 lines
Quoted 20 lines
util_lib/elf_info.c +1 -2
-
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off)
 {
 	int ret;
 
 	*phys_off = UINT64_MAX;
 
-	ret = read_elf_kcore(fd);
+	ret = read_elf(fd);
 	if (!ret) {
 		/* If we have a valid 'PHYS_OFFSET' by now,
 		 * return it to the caller now.
util_lib/include/elf_info.h +0 -0
index 1a4debd2d4ba..8ee7d3e2763f 100644
@@ -23,13 +23,17 @@
 #include <inttypes.h>
 #include <ctype.h>
 
Quoted 4 lines

util_lib/include/elf_info.h line 26

Quoted 1 line
util_lib/include/elf_info.h +1 -0
+typedef void (*handler_t)(char *msg, unsigned int bytes);

I would prefer it if we did not add new typedefs.

OK. I'm considering how to handle it. Any suggestions?

Quoted 38 lines
Quoted 38 lines
util_lib/include/elf_info.h +5 -2
+
 int get_pt_load(int idx,
 	unsigned long long *phys_start,
 	unsigned long long *phys_end,
 	unsigned long long *virt_start,
 	unsigned long long *virt_end);
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
-int read_elf_kcore(int fd);
-int read_elf_vmcore(int fd);
+int check_log_first_idx_vaddr(void);
+void dump_dmesg_structured(int fd, handler_t handler);
+void dump_dmesg_legacy(int fd, handler_t handler);
+int read_elf(int fd);
 
 #endif /* ELF_INFO_H */
vmcore-dmesg/vmcore-dmesg.c +14 -2
index 7a386b380291..ff0d540c9130 100644
@@ -1,21 +1,53 @@
 #include <elf_info.h>
 
-/* The 32bit and 64bit note headers make it clear we don't care */
-typedef Elf32_Nhdr Elf_Nhdr;
+static void write_to_stdout(char *buf, unsigned int nr)
+{
+	ssize_t ret;
+
+	ret = write(STDOUT_FILENO, buf, nr);
+	if (ret != nr) {
+		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
+				" %s\n", strerror(errno));
+		exit(54);
+	}
+}
+
+static void dump_dmesg(int fd, handler_t handler)
+{
Quoted 9 lines

vmcore-dmesg/vmcore-dmesg.c line 21

Quoted 5 lines
vmcore-dmesg/vmcore-dmesg.c +5 -0
+	if (check_log_first_idx_vaddr())
+		dump_dmesg_structured(fd, handler);
+	else
+		dump_dmesg_legacy(fd, handler);
+}

I think that dump_dmesg() could stay in kexec-arm64.c, it does not seem specific to vmcore handling.

The dump_dmesg() should be a common function, not only arm64 will use it, but x86 64 will also call it. It would be good to put the dump_dmesg() to a common place.

Quoted 48 lines
Quoted 48 lines
vmcore-dmesg/vmcore-dmesg.c +19 -4
-static const char *fname;
+static int read_vmcore_dmesg(int fd)
+{
+        int ret;
+
+        ret = read_elf(fd);
+        if (ret > 0) {
+                fprintf(stderr, "Unable to read ELF information"
+                        " from vmcore\n");
+                return ret;
+        }
+
+        dump_dmesg(fd, write_to_stdout);
+
+        return 0;
+}
 
 int main(int argc, char **argv)
 {
 	ssize_t ret;
 	int fd;
+	const char *fname;
 
 	if (argc != 2) {
 		fprintf(stderr, "usage: %s <kernel core file>\n", argv[0]);
 		return 1;
 	}
-	fname = argv[1];
 
+	fname = argv[1];
 	fd = open(fname, O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open %s: %s\n",
@@ -23,8 +55,8 @@ int main(int argc, char **argv)
 		return 2;
 	}
 
-	ret = read_elf_vmcore(fd);
-	
+	ret = read_vmcore_dmesg(fd);
+
 	close(fd);
 
 	return ret;
Signature · 1 line
2.17.1
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw

PATCH 2/2 Limit the size of vmcore-dmesg.txt to 2G

In reply to Lianbo Jiang

With some corrupted vmcore files, the vmcore-dmesg.txt file may grow forever till the kdump disk becomes full, and also probably causes
the disk error messages as follow:
...
sd 0:0:0:0: [sda] tag#6 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#6 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
sd 0:0:0:0: [sda] tag#7 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#7 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
...

If vmcore-dmesg.txt occupies the whole disk, the vmcore can not be saved, this is also a problem.

Lets limit the size of vmcore-dmesg.txt to avoid such problems.

---
 vmcore-dmesg/vmcore-dmesg.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
vmcore-dmesg/vmcore-dmesg.c +10 -0
index ff0d540c9130..5ada3566972b 100644
@@ -1,8 +1,18 @@
 #include <elf_info.h>
 
+/* stole this macro from kernel printk.c */
+#define LOG_BUF_LEN_MAX (uint32_t)(1 << 31)
+
 static void write_to_stdout(char *buf, unsigned int nr)
 {
 	ssize_t ret;
+	static uint32_t n_bytes = 0;
+
+	n_bytes += nr;
+	if (n_bytes > LOG_BUF_LEN_MAX) {
+		fprintf(stderr, "The vmcore-dmesg.txt over 2G in size is not supported.\n");
+		exit(55);
+	}
 
 	ret = write(STDOUT_FILENO, buf, nr);
 	if (ret != nr) {
Signature · 7 lines
2.17.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#6 lore raw
Quoted 12 lines · Lianbo Jiang

With some corrupted vmcore files, the vmcore-dmesg.txt file may grow forever till the kdump disk becomes full, and also probably causes
the disk error messages as follow:
...
sd 0:0:0:0: [sda] tag#6 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#6 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
sd 0:0:0:0: [sda] tag#7 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#7 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
...

Quoted 6 lines

If vmcore-dmesg.txt occupies the whole disk, the vmcore can not be saved, this is also a problem.

Lets limit the size of vmcore-dmesg.txt to avoid such problems.

Thanks, this looks good to me.

Please repost this patch with an updated version of Patch 1/2.

Quoted 36 lines
---
 vmcore-dmesg/vmcore-dmesg.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
vmcore-dmesg/vmcore-dmesg.c +10 -0
index ff0d540c9130..5ada3566972b 100644
@@ -1,8 +1,18 @@
 #include <elf_info.h>
 
+/* stole this macro from kernel printk.c */
+#define LOG_BUF_LEN_MAX (uint32_t)(1 << 31)
+
 static void write_to_stdout(char *buf, unsigned int nr)
 {
 	ssize_t ret;
+	static uint32_t n_bytes = 0;
+
+	n_bytes += nr;
+	if (n_bytes > LOG_BUF_LEN_MAX) {
+		fprintf(stderr, "The vmcore-dmesg.txt over 2G in size is not supported.\n");
+		exit(55);
+	}
 
 	ret = write(STDOUT_FILENO, buf, nr);
 	if (ret != nr) {
Signature · 7 lines
2.17.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

author
#7 lore raw
Quoted 16 lines · Simon Horman
Quoted 15 lines · Lianbo Jiang

With some corrupted vmcore files, the vmcore-dmesg.txt file may grow forever till the kdump disk becomes full, and also probably causes
the disk error messages as follow:
...
sd 0:0:0:0: [sda] tag#6 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#6 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
sd 0:0:0:0: [sda] tag#7 FAILED Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK sd 0:0:0:0: [sda] tag#7 CDB: Read(10) 28 00 08 06 4c 98 00 00 08 00
blk_update_request: I/O error, dev sda, sector 134630552
...

If vmcore-dmesg.txt occupies the whole disk, the vmcore can not be saved, this is also a problem.

Quoted 8 lines
Quoted 3 lines

Lets limit the size of vmcore-dmesg.txt to avoid such problems.

Thanks, this looks good to me.

Please repost this patch with an updated version of Patch 1/2.

OK, thank you, Simon. I will improve them and post again.

Quoted 31 lines
Quoted 31 lines
---
 vmcore-dmesg/vmcore-dmesg.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
vmcore-dmesg/vmcore-dmesg.c +10 -0
index ff0d540c9130..5ada3566972b 100644
@@ -1,8 +1,18 @@
 #include <elf_info.h>
 
+/* stole this macro from kernel printk.c */
+#define LOG_BUF_LEN_MAX (uint32_t)(1 << 31)
+
 static void write_to_stdout(char *buf, unsigned int nr)
 {
 	ssize_t ret;
+	static uint32_t n_bytes = 0;
+
+	n_bytes += nr;
+	if (n_bytes > LOG_BUF_LEN_MAX) {
+		fprintf(stderr, "The vmcore-dmesg.txt over 2G in size is not supported.\n");
+		exit(55);
+	}
 
 	ret = write(STDOUT_FILENO, buf, nr);
 	if (ret != nr) {
Signature · 1 line
2.17.1
Quoted 5 lines
Quoted 5 lines

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec