public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kexec: Avoid calculating array size twice
@ 2023-05-25 14:26 Simon Horman
  2023-06-01 10:40 ` Baoquan He
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Horman @ 2023-05-25 14:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Eric Biederman, Baoquan He, Leizhen (ThunderTown), kexec,
	linux-kernel

Avoid calculating array size twice in kexec_purgatory_setup_sechdrs().
Once using array_size(), and once open-coded.

Flagged by Coccinelle:

  .../kexec_file.c:881:8-25: WARNING: array_size is already used (line 877) to compute the same size

No functional change intended.
Compile tested only.

Signed-off-by: Simon Horman <horms@kernel.org>
---
 kernel/kexec_file.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index f989f5f1933b..3f5677679744 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -867,6 +867,7 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
 {
 	unsigned long bss_addr;
 	unsigned long offset;
+	size_t sechdrs_size;
 	Elf_Shdr *sechdrs;
 	int i;
 
@@ -874,11 +875,11 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
 	 * The section headers in kexec_purgatory are read-only. In order to
 	 * have them modifiable make a temporary copy.
 	 */
-	sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
+	sechdrs_size = array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum);
+	sechdrs = vzalloc(sechdrs_size);
 	if (!sechdrs)
 		return -ENOMEM;
-	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
-	       pi->ehdr->e_shnum * sizeof(Elf_Shdr));
+	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, sechdrs_size);
 	pi->sechdrs = sechdrs;
 
 	offset = 0;


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

* Re: [PATCH] kexec: Avoid calculating array size twice
  2023-05-25 14:26 [PATCH] kexec: Avoid calculating array size twice Simon Horman
@ 2023-06-01 10:40 ` Baoquan He
  2023-06-01 10:49   ` Baoquan He
  0 siblings, 1 reply; 3+ messages in thread
From: Baoquan He @ 2023-06-01 10:40 UTC (permalink / raw)
  To: Simon Horman
  Cc: Andrew Morton, Eric Biederman, Leizhen (ThunderTown), kexec,
	linux-kernel

On 05/25/23 at 04:26pm, Simon Horman wrote:
> Avoid calculating array size twice in kexec_purgatory_setup_sechdrs().
> Once using array_size(), and once open-coded.
> 
> Flagged by Coccinelle:
> 
>   .../kexec_file.c:881:8-25: WARNING: array_size is already used (line 877) to compute the same size

Amazingly smart. Thanks.

Acked-by: Baoquan He <bhe@redhat.com>

> 
> No functional change intended.
> Compile tested only.
> 
> Signed-off-by: Simon Horman <horms@kernel.org>
> ---
>  kernel/kexec_file.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
> index f989f5f1933b..3f5677679744 100644
> --- a/kernel/kexec_file.c
> +++ b/kernel/kexec_file.c
> @@ -867,6 +867,7 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
>  {
>  	unsigned long bss_addr;
>  	unsigned long offset;
> +	size_t sechdrs_size;
>  	Elf_Shdr *sechdrs;
>  	int i;
>  
> @@ -874,11 +875,11 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
>  	 * The section headers in kexec_purgatory are read-only. In order to
>  	 * have them modifiable make a temporary copy.
>  	 */
> -	sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
> +	sechdrs_size = array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum);
> +	sechdrs = vzalloc(sechdrs_size);
>  	if (!sechdrs)
>  		return -ENOMEM;
> -	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
> -	       pi->ehdr->e_shnum * sizeof(Elf_Shdr));
> +	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, sechdrs_size);
>  	pi->sechdrs = sechdrs;
>  
>  	offset = 0;
> 


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

* Re: [PATCH] kexec: Avoid calculating array size twice
  2023-06-01 10:40 ` Baoquan He
@ 2023-06-01 10:49   ` Baoquan He
  0 siblings, 0 replies; 3+ messages in thread
From: Baoquan He @ 2023-06-01 10:49 UTC (permalink / raw)
  To: Simon Horman, Andrew Morton
  Cc: Eric Biederman, Leizhen (ThunderTown), kexec, linux-kernel

On 06/01/23 at 06:40pm, Baoquan He wrote:
> On 05/25/23 at 04:26pm, Simon Horman wrote:
> > Avoid calculating array size twice in kexec_purgatory_setup_sechdrs().
> > Once using array_size(), and once open-coded.
> > 
> > Flagged by Coccinelle:
> > 
> >   .../kexec_file.c:881:8-25: WARNING: array_size is already used (line 877) to compute the same size
> 
> Amazingly smart. Thanks.
> 
> Acked-by: Baoquan He <bhe@redhat.com>

Just noticed Andrew has picked this one, please ignore this.

> 
> > 
> > No functional change intended.
> > Compile tested only.
> > 
> > Signed-off-by: Simon Horman <horms@kernel.org>
> > ---
> >  kernel/kexec_file.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> > 
> > diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
> > index f989f5f1933b..3f5677679744 100644
> > --- a/kernel/kexec_file.c
> > +++ b/kernel/kexec_file.c
> > @@ -867,6 +867,7 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
> >  {
> >  	unsigned long bss_addr;
> >  	unsigned long offset;
> > +	size_t sechdrs_size;
> >  	Elf_Shdr *sechdrs;
> >  	int i;
> >  
> > @@ -874,11 +875,11 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
> >  	 * The section headers in kexec_purgatory are read-only. In order to
> >  	 * have them modifiable make a temporary copy.
> >  	 */
> > -	sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
> > +	sechdrs_size = array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum);
> > +	sechdrs = vzalloc(sechdrs_size);
> >  	if (!sechdrs)
> >  		return -ENOMEM;
> > -	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
> > -	       pi->ehdr->e_shnum * sizeof(Elf_Shdr));
> > +	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, sechdrs_size);
> >  	pi->sechdrs = sechdrs;
> >  
> >  	offset = 0;
> > 
> 


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

end of thread, other threads:[~2023-06-01 10:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-25 14:26 [PATCH] kexec: Avoid calculating array size twice Simon Horman
2023-06-01 10:40 ` Baoquan He
2023-06-01 10:49   ` Baoquan He

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