[PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()

Ingo Molnar posted 32 patches 7 months ago
[PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()
Posted by Ingo Molnar 7 months ago
This kind of code:

	change_point[chg_idx++]->entry  = &entries[idx];

Can be a bit confusing to human readers, and GCC-15 started
warning about these patterns.

Move the index increment outside the accessor.

Suggested-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Arnd Bergmann <arnd@kernel.org>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 arch/x86/kernel/e820.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 10c6e7dc72d7..afb312620c82 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -421,9 +421,11 @@ __init int e820__update_table(struct e820_table *table)
 	for (idx = 0; idx < table->nr_entries; idx++)	{
 		if (entries[idx].size != 0) {
 			change_point[chg_idx]->addr	= entries[idx].addr;
-			change_point[chg_idx++]->entry	= &entries[idx];
+			change_point[chg_idx]->entry	= &entries[idx];
+			chg_idx++;
 			change_point[chg_idx]->addr	= entries[idx].addr + entries[idx].size;
-			change_point[chg_idx++]->entry	= &entries[idx];
+			change_point[chg_idx]->entry	= &entries[idx];
+			chg_idx++;
 		}
 	}
 	chg_nr = chg_idx;
-- 
2.45.2
Re: [PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()
Posted by Nikolay Borisov 6 months, 2 weeks ago

On 5/15/25 15:05, Ingo Molnar wrote:
> This kind of code:
> 
> 	change_point[chg_idx++]->entry  = &entries[idx];
> 
> Can be a bit confusing to human readers, and GCC-15 started
> warning about these patterns.
> 
> Move the index increment outside the accessor.
> 
> Suggested-by: Andy Shevchenko <andy@kernel.org>
> Signed-off-by: Ingo Molnar <mingo@kernel.org>
> Cc: Arnd Bergmann <arnd@kernel.org>
> Cc: David Woodhouse <dwmw@amazon.co.uk>
> Cc: H. Peter Anvin <hpa@zytor.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
>   arch/x86/kernel/e820.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
> index 10c6e7dc72d7..afb312620c82 100644
> --- a/arch/x86/kernel/e820.c
> +++ b/arch/x86/kernel/e820.c
> @@ -421,9 +421,11 @@ __init int e820__update_table(struct e820_table *table)
>   	for (idx = 0; idx < table->nr_entries; idx++)	{
>   		if (entries[idx].size != 0) {
nit: The level of nesting can easily be reduced by doing
if (entries[idx].size == 0)
	continue;
>   			change_point[chg_idx]->addr	= entries[idx].addr;
> -			change_point[chg_idx++]->entry	= &entries[idx];
> +			change_point[chg_idx]->entry	= &entries[idx];
> +			chg_idx++;

nit: I have to agree with H. Peter Anvin that this seems somewhat odd to 
me as well.

<snip>
Re: [PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()
Posted by Ingo Molnar 2 days, 19 hours ago
* Nikolay Borisov <nik.borisov@suse.com> wrote:

>
>
> On 5/15/25 15:05, Ingo Molnar wrote:
> > This kind of code:
> >
> >	change_point[chg_idx++]->entry  = &entries[idx];
> >
> > Can be a bit confusing to human readers, and GCC-15 started
> > warning about these patterns.
> >
> > Move the index increment outside the accessor.
> >
> > Suggested-by: Andy Shevchenko <andy@kernel.org>
> > Signed-off-by: Ingo Molnar <mingo@kernel.org>
> > Cc: Arnd Bergmann <arnd@kernel.org>
> > Cc: David Woodhouse <dwmw@amazon.co.uk>
> > Cc: H. Peter Anvin <hpa@zytor.com>
> > Cc: Kees Cook <keescook@chromium.org>
> > Cc: Linus Torvalds <torvalds@linux-foundation.org>
> > Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
> > ---
> >   arch/x86/kernel/e820.c | 6 ++++--
> >   1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
> > index 10c6e7dc72d7..afb312620c82 100644
> > --- a/arch/x86/kernel/e820.c
> > +++ b/arch/x86/kernel/e820.c
> > @@ -421,9 +421,11 @@ __init int e820__update_table(struct e820_table *table)
> >	for (idx = 0; idx < table->nr_entries; idx++)	{
> >		if (entries[idx].size != 0) {
> nit: The level of nesting can easily be reduced by doing
> if (entries[idx].size == 0)
>	continue;
> >			change_point[chg_idx]->addr	= entries[idx].addr;
> > -			change_point[chg_idx++]->entry	= &entries[idx];
> > +			change_point[chg_idx]->entry	= &entries[idx];
> > +			chg_idx++;
>
> nit: I have to agree with H. Peter Anvin that this seems somewhat odd to me
> as well.

OK, I've dropped this change.

Thanks,

	Ingo
Re: [PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()
Posted by H. Peter Anvin 7 months ago
On May 15, 2025 5:05:48 AM PDT, Ingo Molnar <mingo@kernel.org> wrote:
>This kind of code:
>
>	change_point[chg_idx++]->entry  = &entries[idx];
>
>Can be a bit confusing to human readers, and GCC-15 started
>warning about these patterns.
>
>Move the index increment outside the accessor.
>
>Suggested-by: Andy Shevchenko <andy@kernel.org>
>Signed-off-by: Ingo Molnar <mingo@kernel.org>
>Cc: Arnd Bergmann <arnd@kernel.org>
>Cc: David Woodhouse <dwmw@amazon.co.uk>
>Cc: H. Peter Anvin <hpa@zytor.com>
>Cc: Kees Cook <keescook@chromium.org>
>Cc: Linus Torvalds <torvalds@linux-foundation.org>
>Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
>---
> arch/x86/kernel/e820.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
>diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
>index 10c6e7dc72d7..afb312620c82 100644
>--- a/arch/x86/kernel/e820.c
>+++ b/arch/x86/kernel/e820.c
>@@ -421,9 +421,11 @@ __init int e820__update_table(struct e820_table *table)
> 	for (idx = 0; idx < table->nr_entries; idx++)	{
> 		if (entries[idx].size != 0) {
> 			change_point[chg_idx]->addr	= entries[idx].addr;
>-			change_point[chg_idx++]->entry	= &entries[idx];
>+			change_point[chg_idx]->entry	= &entries[idx];
>+			chg_idx++;
> 			change_point[chg_idx]->addr	= entries[idx].addr + entries[idx].size;
>-			change_point[chg_idx++]->entry	= &entries[idx];
>+			change_point[chg_idx]->entry	= &entries[idx];
>+			chg_idx++;
> 		}
> 	}
> 	chg_nr = chg_idx;

Really? That seems easier to miss to me.
Re: [PATCH 32/32] x86/boot/e820: Move index increments outside accessors in e820__update_table()
Posted by Ingo Molnar 7 months ago
* H. Peter Anvin <hpa@zytor.com> wrote:

> On May 15, 2025 5:05:48 AM PDT, Ingo Molnar <mingo@kernel.org> wrote:
> >This kind of code:
> >
> >	change_point[chg_idx++]->entry  = &entries[idx];
> >
> >Can be a bit confusing to human readers, and GCC-15 started
> >warning about these patterns.
> >
> >Move the index increment outside the accessor.
> >
> >Suggested-by: Andy Shevchenko <andy@kernel.org>
> >Signed-off-by: Ingo Molnar <mingo@kernel.org>
> >Cc: Arnd Bergmann <arnd@kernel.org>
> >Cc: David Woodhouse <dwmw@amazon.co.uk>
> >Cc: H. Peter Anvin <hpa@zytor.com>
> >Cc: Kees Cook <keescook@chromium.org>
> >Cc: Linus Torvalds <torvalds@linux-foundation.org>
> >Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
> >---
> > arch/x86/kernel/e820.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> >diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
> >index 10c6e7dc72d7..afb312620c82 100644
> >--- a/arch/x86/kernel/e820.c
> >+++ b/arch/x86/kernel/e820.c
> >@@ -421,9 +421,11 @@ __init int e820__update_table(struct e820_table *table)
> > 	for (idx = 0; idx < table->nr_entries; idx++)	{
> > 		if (entries[idx].size != 0) {
> > 			change_point[chg_idx]->addr	= entries[idx].addr;
> >-			change_point[chg_idx++]->entry	= &entries[idx];
> >+			change_point[chg_idx]->entry	= &entries[idx];
> >+			chg_idx++;
> > 			change_point[chg_idx]->addr	= entries[idx].addr + entries[idx].size;
> >-			change_point[chg_idx++]->entry	= &entries[idx];
> >+			change_point[chg_idx]->entry	= &entries[idx];
> >+			chg_idx++;
> > 		}
> > 	}
> > 	chg_nr = chg_idx;
> 
> Really? That seems easier to miss to me.

Maybe writing it in two groups:

			change_point[chg_idx]->addr	= entries[idx].addr;
			change_point[chg_idx]->entry	= &entries[idx];
			chg_idx++;

			change_point[chg_idx]->addr	= entries[idx].addr + entries[idx].size;
			change_point[chg_idx]->entry	= &entries[idx];
			chg_idx++;

makes it a bit easier to read? The chg_idx++ are pretty prominent in 
that form, while it's easier to miss when it's embedded.

Thanks,

	Ingo