kexec.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [Patch] kexec: fix several issues in get_crash_notes()
@ 2011-11-16 13:09 WANG Cong
  2011-11-18  2:11 ` Simon Horman
  0 siblings, 1 reply; 4+ messages in thread
From: WANG Cong @ 2011-11-16 13:09 UTC (permalink / raw)
  To: kexec; +Cc: WANG Cong, horms

a) We don't need 'crash_notes' array at all, save some bytes on stack.
b) We forgot to fclose 'fp' before return.

Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>

---
diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
index 98cda72..333f6ba 100644
--- a/kexec/arch/i386/crashdump-x86.c
+++ b/kexec/arch/i386/crashdump-x86.c
@@ -589,20 +589,18 @@ static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
  */
 static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
 {
-	char crash_notes[PATH_MAX];
 	char line[MAX_LINE];
 	FILE *fp;
 	unsigned long vaddr;
 	int count;
 
-	sprintf(crash_notes, "/sys/kernel/crash_notes");
-	fp = fopen(crash_notes, "r");
+	fp = fopen("/sys/kernel/crash_notes", "r");
 	if (fp) {
 		if (fgets(line, sizeof(line), fp) != 0) {
 			count = sscanf(line, "%lx", &vaddr);
 			if (count != 1)
-				die("Cannot parse %s: %s\n", crash_notes,
-						strerror(errno));
+				die("Cannot parse /sys/kernel/crash_notes:"
+					" %s\n", strerror(errno));
 		}
 
 		*addr = x86__pa(vaddr + (cpu * MAX_NOTE_BYTES));
@@ -610,6 +608,7 @@ static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
 #ifdef DEBUG
 		printf("crash_notes addr = %Lx\n", *addr);
 #endif
+		fclose(fp);
 		return 0;
 	} else
 		return get_crash_notes_per_cpu(cpu, addr, len);

_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

* Re: [Patch] kexec: fix several issues in get_crash_notes()
  2011-11-16 13:09 [Patch] kexec: fix several issues in get_crash_notes() WANG Cong
@ 2011-11-18  2:11 ` Simon Horman
  2011-11-21  7:43   ` Cong Wang
  0 siblings, 1 reply; 4+ messages in thread
From: Simon Horman @ 2011-11-18  2:11 UTC (permalink / raw)
  To: WANG Cong; +Cc: kexec

On Wed, Nov 16, 2011 at 09:09:26PM +0800, WANG Cong wrote:
> a) We don't need 'crash_notes' array at all, save some bytes on stack.
> b) We forgot to fclose 'fp' before return.
> 
> Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
> 
> ---
> diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
> index 98cda72..333f6ba 100644
> --- a/kexec/arch/i386/crashdump-x86.c
> +++ b/kexec/arch/i386/crashdump-x86.c
> @@ -589,20 +589,18 @@ static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
>   */
>  static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
>  {
> -	char crash_notes[PATH_MAX];

I wonder if the following might reduce memory usage:

	const char *crash_notes = "/sys/kernel/crash_notes";

>  	char line[MAX_LINE];
>  	FILE *fp;
>  	unsigned long vaddr;
>  	int count;
>  
> -	sprintf(crash_notes, "/sys/kernel/crash_notes");
> -	fp = fopen(crash_notes, "r");
> +	fp = fopen("/sys/kernel/crash_notes", "r");
>  	if (fp) {
>  		if (fgets(line, sizeof(line), fp) != 0) {
>  			count = sscanf(line, "%lx", &vaddr);
>  			if (count != 1)
> -				die("Cannot parse %s: %s\n", crash_notes,
> -						strerror(errno));
> +				die("Cannot parse /sys/kernel/crash_notes:"
> +					" %s\n", strerror(errno));
>  		}
>  
>  		*addr = x86__pa(vaddr + (cpu * MAX_NOTE_BYTES));
> @@ -610,6 +608,7 @@ static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
>  #ifdef DEBUG
>  		printf("crash_notes addr = %Lx\n", *addr);
>  #endif
> +		fclose(fp);
>  		return 0;
>  	} else
>  		return get_crash_notes_per_cpu(cpu, addr, len);
> 
> _______________________________________________
> kexec mailing list
> kexec@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kexec
> 

_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

* Re: [Patch] kexec: fix several issues in get_crash_notes()
  2011-11-18  2:11 ` Simon Horman
@ 2011-11-21  7:43   ` Cong Wang
  2011-11-21  8:50     ` Simon Horman
  0 siblings, 1 reply; 4+ messages in thread
From: Cong Wang @ 2011-11-21  7:43 UTC (permalink / raw)
  To: Simon Horman; +Cc: kexec

[-- Attachment #1: Type: text/plain, Size: 1128 bytes --]

On Fri, Nov 18, 2011 at 10:11 AM, Simon Horman <horms@verge.net.au> wrote:
> On Wed, Nov 16, 2011 at 09:09:26PM +0800, WANG Cong wrote:
>> a) We don't need 'crash_notes' array at all, save some bytes on stack.
>> b) We forgot to fclose 'fp' before return.
>>
>> Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
>>
>> ---
>> diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
>> index 98cda72..333f6ba 100644
>> --- a/kexec/arch/i386/crashdump-x86.c
>> +++ b/kexec/arch/i386/crashdump-x86.c
>> @@ -589,20 +589,18 @@ static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
>>   */
>>  static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
>>  {
>> -     char crash_notes[PATH_MAX];
>
> I wonder if the following might reduce memory usage:
>
>        const char *crash_notes = "/sys/kernel/crash_notes";

Yeah, sure. Below is the new patch.
-------------------->

a) We don't need 'crash_notes' array at all, save some bytes on stack.
b) We forgot to fclose 'fp' before return.

Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
---

[-- Attachment #2: kexec-tools-fix-get_crash_notes.diff --]
[-- Type: text/x-patch, Size: 893 bytes --]

diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
index 98cda72..436797a 100644
--- a/kexec/arch/i386/crashdump-x86.c
+++ b/kexec/arch/i386/crashdump-x86.c
@@ -589,13 +589,12 @@ static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
  */
 static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
 {
-	char crash_notes[PATH_MAX];
+	const char *crash_notes = "/sys/kernel/crash_notes";
 	char line[MAX_LINE];
 	FILE *fp;
 	unsigned long vaddr;
 	int count;
 
-	sprintf(crash_notes, "/sys/kernel/crash_notes");
 	fp = fopen(crash_notes, "r");
 	if (fp) {
 		if (fgets(line, sizeof(line), fp) != 0) {
@@ -610,6 +609,7 @@ static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
 #ifdef DEBUG
 		printf("crash_notes addr = %Lx\n", *addr);
 #endif
+		fclose(fp);
 		return 0;
 	} else
 		return get_crash_notes_per_cpu(cpu, addr, len);

[-- Attachment #3: Type: text/plain, Size: 143 bytes --]

_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

* Re: [Patch] kexec: fix several issues in get_crash_notes()
  2011-11-21  7:43   ` Cong Wang
@ 2011-11-21  8:50     ` Simon Horman
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Horman @ 2011-11-21  8:50 UTC (permalink / raw)
  To: Cong Wang; +Cc: kexec

On Mon, Nov 21, 2011 at 03:43:34PM +0800, Cong Wang wrote:
> On Fri, Nov 18, 2011 at 10:11 AM, Simon Horman <horms@verge.net.au> wrote:
> > On Wed, Nov 16, 2011 at 09:09:26PM +0800, WANG Cong wrote:
> >> a) We don't need 'crash_notes' array at all, save some bytes on stack.
> >> b) We forgot to fclose 'fp' before return.
> >>
> >> Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
> >>
> >> ---
> >> diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
> >> index 98cda72..333f6ba 100644
> >> --- a/kexec/arch/i386/crashdump-x86.c
> >> +++ b/kexec/arch/i386/crashdump-x86.c
> >> @@ -589,20 +589,18 @@ static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
> >>   */
> >>  static int get_crash_notes(int cpu, uint64_t *addr, uint64_t *len)
> >>  {
> >> -     char crash_notes[PATH_MAX];
> >
> > I wonder if the following might reduce memory usage:
> >
> >        const char *crash_notes = "/sys/kernel/crash_notes";
> 
> Yeah, sure. Below is the new patch.
> -------------------->
> 
> a) We don't need 'crash_notes' array at all, save some bytes on stack.
> b) We forgot to fclose 'fp' before return.

Thanks, applied.


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

end of thread, other threads:[~2011-11-21  8:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-16 13:09 [Patch] kexec: fix several issues in get_crash_notes() WANG Cong
2011-11-18  2:11 ` Simon Horman
2011-11-21  7:43   ` Cong Wang
2011-11-21  8:50     ` Simon Horman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).