linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Consider sparse memory while removing unusable memory
@ 2020-09-12  0:23 Atish Patra
  2020-09-12 10:45 ` Mike Rapoport
  0 siblings, 1 reply; 6+ messages in thread
From: Atish Patra @ 2020-09-12  0:23 UTC (permalink / raw)
  To: linux-kernel
  Cc: Atish Patra, Albert Ou, Anup Patel, linux-riscv, Palmer Dabbelt,
	Paul Walmsley, Zong Li, Mike Rapoport

Currently, any usable memory area beyond page_offset is removed by adding the
memory sizes from each memblock. That may not work for sparse memory
as memory regions can be very far apart resulting incorrect removal of some
usable memory.

Just use the start of the first memory block and the end of the last memory
block to compute the size of the total memory that can be used.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
---
 arch/riscv/mm/init.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 787c75f751a5..188281fc2816 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -147,7 +147,6 @@ void __init setup_bootmem(void)
 {
 	struct memblock_region *reg;
 	phys_addr_t mem_size = 0;
-	phys_addr_t total_mem = 0;
 	phys_addr_t mem_start, end = 0;
 	phys_addr_t vmlinux_end = __pa_symbol(&_end);
 	phys_addr_t vmlinux_start = __pa_symbol(&_start);
@@ -155,18 +154,17 @@ void __init setup_bootmem(void)
 	/* Find the memory region containing the kernel */
 	for_each_memblock(memory, reg) {
 		end = reg->base + reg->size;
-		if (!total_mem)
+		if (!mem_start)
 			mem_start = reg->base;
 		if (reg->base <= vmlinux_start && vmlinux_end <= end)
 			BUG_ON(reg->size == 0);
-		total_mem = total_mem + reg->size;
 	}
 
 	/*
 	 * Remove memblock from the end of usable area to the
 	 * end of region
 	 */
-	mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
+	mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
 	if (mem_start + mem_size < end)
 		memblock_remove(mem_start + mem_size,
 				end - mem_start - mem_size);
-- 
2.24.0


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

* Re: [PATCH] RISC-V: Consider sparse memory while removing unusable memory
  2020-09-12  0:23 [PATCH] RISC-V: Consider sparse memory while removing unusable memory Atish Patra
@ 2020-09-12 10:45 ` Mike Rapoport
  2020-09-13 23:47   ` Atish Patra
  2020-09-14 11:49   ` Anup Patel
  0 siblings, 2 replies; 6+ messages in thread
From: Mike Rapoport @ 2020-09-12 10:45 UTC (permalink / raw)
  To: Atish Patra
  Cc: linux-kernel, Albert Ou, Anup Patel, linux-riscv, Palmer Dabbelt,
	Paul Walmsley, Zong Li

Hello Atish,

On Fri, Sep 11, 2020 at 05:23:41PM -0700, Atish Patra wrote:
> Currently, any usable memory area beyond page_offset is removed by adding the
> memory sizes from each memblock. That may not work for sparse memory
> as memory regions can be very far apart resulting incorrect removal of some
> usable memory.

If I understand correctly, the memory with physical addresses larger
than (-PAGE_OFFSET) cannot be used. Since it was aready
memblock_add()'ed during device tree parsing, you need to remove it from
memblock.

For that you can use memblock_enforce_memory_limit(-PAGE_OFFSET).

> Just use the start of the first memory block and the end of the last memory
> block to compute the size of the total memory that can be used.
> 
> Signed-off-by: Atish Patra <atish.patra@wdc.com>
> ---
>  arch/riscv/mm/init.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index 787c75f751a5..188281fc2816 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -147,7 +147,6 @@ void __init setup_bootmem(void)
>  {
>  	struct memblock_region *reg;
>  	phys_addr_t mem_size = 0;
> -	phys_addr_t total_mem = 0;
>  	phys_addr_t mem_start, end = 0;
>  	phys_addr_t vmlinux_end = __pa_symbol(&_end);
>  	phys_addr_t vmlinux_start = __pa_symbol(&_start);
> @@ -155,18 +154,17 @@ void __init setup_bootmem(void)
>  	/* Find the memory region containing the kernel */
>  	for_each_memblock(memory, reg) {
>  		end = reg->base + reg->size;
> -		if (!total_mem)
> +		if (!mem_start)
>  			mem_start = reg->base;
>  		if (reg->base <= vmlinux_start && vmlinux_end <= end)
>  			BUG_ON(reg->size == 0);
> -		total_mem = total_mem + reg->size;
>  	}
>  
>  	/*
>  	 * Remove memblock from the end of usable area to the
>  	 * end of region
>  	 */
> -	mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
> +	mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
>  	if (mem_start + mem_size < end)
>  		memblock_remove(mem_start + mem_size,
>  				end - mem_start - mem_size);
> -- 
> 2.24.0
> 

-- 
Sincerely yours,
Mike.

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

* Re: [PATCH] RISC-V: Consider sparse memory while removing unusable memory
  2020-09-12 10:45 ` Mike Rapoport
@ 2020-09-13 23:47   ` Atish Patra
  2020-09-14 11:52     ` Anup Patel
  2020-09-14 11:49   ` Anup Patel
  1 sibling, 1 reply; 6+ messages in thread
From: Atish Patra @ 2020-09-13 23:47 UTC (permalink / raw)
  To: Mike Rapoport, Anup Patel, Anup Patel
  Cc: Atish Patra, Albert Ou, linux-kernel@vger.kernel.org List,
	Palmer Dabbelt, Zong Li, Paul Walmsley, linux-riscv

On Sat, Sep 12, 2020 at 3:45 AM Mike Rapoport <rppt@kernel.org> wrote:
>
> Hello Atish,
>
> On Fri, Sep 11, 2020 at 05:23:41PM -0700, Atish Patra wrote:
> > Currently, any usable memory area beyond page_offset is removed by adding the
> > memory sizes from each memblock. That may not work for sparse memory
> > as memory regions can be very far apart resulting incorrect removal of some
> > usable memory.
>
> If I understand correctly, the memory with physical addresses larger
> than (-PAGE_OFFSET) cannot be used. Since it was aready
> memblock_add()'ed during device tree parsing, you need to remove it from
> memblock.
>

IIRC, the original intention was to fix MAXPHYSMEM_2GB option for RV64
for the medlow model.
That's why the patch removed any memory beyond -PAGE_OFFSET.

> For that you can use memblock_enforce_memory_limit(-PAGE_OFFSET).
>
Thanks. I think we can just call memblock_enforce_memory_limit without
tracking the total memory size
and whether maximum memory described in DT is greater than  -PAGE_OFFSET.

@Anup Patel Was there any other reason for this change originally?

> > Just use the start of the first memory block and the end of the last memory
> > block to compute the size of the total memory that can be used.
> >
> > Signed-off-by: Atish Patra <atish.patra@wdc.com>
> > ---
> >  arch/riscv/mm/init.c | 6 ++----
> >  1 file changed, 2 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > index 787c75f751a5..188281fc2816 100644
> > --- a/arch/riscv/mm/init.c
> > +++ b/arch/riscv/mm/init.c
> > @@ -147,7 +147,6 @@ void __init setup_bootmem(void)
> >  {
> >       struct memblock_region *reg;
> >       phys_addr_t mem_size = 0;
> > -     phys_addr_t total_mem = 0;
> >       phys_addr_t mem_start, end = 0;
> >       phys_addr_t vmlinux_end = __pa_symbol(&_end);
> >       phys_addr_t vmlinux_start = __pa_symbol(&_start);
> > @@ -155,18 +154,17 @@ void __init setup_bootmem(void)
> >       /* Find the memory region containing the kernel */
> >       for_each_memblock(memory, reg) {
> >               end = reg->base + reg->size;
> > -             if (!total_mem)
> > +             if (!mem_start)
> >                       mem_start = reg->base;
> >               if (reg->base <= vmlinux_start && vmlinux_end <= end)
> >                       BUG_ON(reg->size == 0);
> > -             total_mem = total_mem + reg->size;
> >       }
> >
> >       /*
> >        * Remove memblock from the end of usable area to the
> >        * end of region
> >        */
> > -     mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
> > +     mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
> >       if (mem_start + mem_size < end)
> >               memblock_remove(mem_start + mem_size,
> >                               end - mem_start - mem_size);
> > --
> > 2.24.0
> >
>
> --
> Sincerely yours,
> Mike.
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv



-- 
Regards,
Atish

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

* Re: [PATCH] RISC-V: Consider sparse memory while removing unusable memory
  2020-09-12 10:45 ` Mike Rapoport
  2020-09-13 23:47   ` Atish Patra
@ 2020-09-14 11:49   ` Anup Patel
  1 sibling, 0 replies; 6+ messages in thread
From: Anup Patel @ 2020-09-14 11:49 UTC (permalink / raw)
  To: Mike Rapoport
  Cc: Atish Patra, linux-kernel@vger.kernel.org List, Albert Ou,
	Anup Patel, linux-riscv, Palmer Dabbelt, Paul Walmsley, Zong Li

On Sat, Sep 12, 2020 at 4:15 PM Mike Rapoport <rppt@kernel.org> wrote:
>
> Hello Atish,
>
> On Fri, Sep 11, 2020 at 05:23:41PM -0700, Atish Patra wrote:
> > Currently, any usable memory area beyond page_offset is removed by adding the
> > memory sizes from each memblock. That may not work for sparse memory
> > as memory regions can be very far apart resulting incorrect removal of some
> > usable memory.
>
> If I understand correctly, the memory with physical addresses larger
> than (-PAGE_OFFSET) cannot be used. Since it was aready
> memblock_add()'ed during device tree parsing, you need to remove it from
> memblock.

-PAGE_OFFSET represents the size of memory addressable by the kernel and
not the last physical address.

mem_start + (-PAGE_OFFSET) will be the last physical address usable by kernel.

>
> For that you can use memblock_enforce_memory_limit(-PAGE_OFFSET).

I think we should use memblock_enforce_memory_limit(mem_start - PAGE_OFFSET).

Regards,
Anup

>
> > Just use the start of the first memory block and the end of the last memory
> > block to compute the size of the total memory that can be used.
> >
> > Signed-off-by: Atish Patra <atish.patra@wdc.com>
> > ---
> >  arch/riscv/mm/init.c | 6 ++----
> >  1 file changed, 2 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > index 787c75f751a5..188281fc2816 100644
> > --- a/arch/riscv/mm/init.c
> > +++ b/arch/riscv/mm/init.c
> > @@ -147,7 +147,6 @@ void __init setup_bootmem(void)
> >  {
> >       struct memblock_region *reg;
> >       phys_addr_t mem_size = 0;
> > -     phys_addr_t total_mem = 0;
> >       phys_addr_t mem_start, end = 0;
> >       phys_addr_t vmlinux_end = __pa_symbol(&_end);
> >       phys_addr_t vmlinux_start = __pa_symbol(&_start);
> > @@ -155,18 +154,17 @@ void __init setup_bootmem(void)
> >       /* Find the memory region containing the kernel */
> >       for_each_memblock(memory, reg) {
> >               end = reg->base + reg->size;
> > -             if (!total_mem)
> > +             if (!mem_start)
> >                       mem_start = reg->base;
> >               if (reg->base <= vmlinux_start && vmlinux_end <= end)
> >                       BUG_ON(reg->size == 0);
> > -             total_mem = total_mem + reg->size;
> >       }
> >
> >       /*
> >        * Remove memblock from the end of usable area to the
> >        * end of region
> >        */
> > -     mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
> > +     mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
> >       if (mem_start + mem_size < end)
> >               memblock_remove(mem_start + mem_size,
> >                               end - mem_start - mem_size);
> > --
> > 2.24.0
> >
>
> --
> Sincerely yours,
> Mike.

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

* Re: [PATCH] RISC-V: Consider sparse memory while removing unusable memory
  2020-09-13 23:47   ` Atish Patra
@ 2020-09-14 11:52     ` Anup Patel
  2020-09-14 17:53       ` Atish Patra
  0 siblings, 1 reply; 6+ messages in thread
From: Anup Patel @ 2020-09-14 11:52 UTC (permalink / raw)
  To: Atish Patra
  Cc: Mike Rapoport, Anup Patel, Atish Patra, Albert Ou,
	linux-kernel@vger.kernel.org List, Palmer Dabbelt, Zong Li,
	Paul Walmsley, linux-riscv

On Mon, Sep 14, 2020 at 5:18 AM Atish Patra <atishp@atishpatra.org> wrote:
>
> On Sat, Sep 12, 2020 at 3:45 AM Mike Rapoport <rppt@kernel.org> wrote:
> >
> > Hello Atish,
> >
> > On Fri, Sep 11, 2020 at 05:23:41PM -0700, Atish Patra wrote:
> > > Currently, any usable memory area beyond page_offset is removed by adding the
> > > memory sizes from each memblock. That may not work for sparse memory
> > > as memory regions can be very far apart resulting incorrect removal of some
> > > usable memory.
> >
> > If I understand correctly, the memory with physical addresses larger
> > than (-PAGE_OFFSET) cannot be used. Since it was aready
> > memblock_add()'ed during device tree parsing, you need to remove it from
> > memblock.
> >
>
> IIRC, the original intention was to fix MAXPHYSMEM_2GB option for RV64
> for the medlow model.
> That's why the patch removed any memory beyond -PAGE_OFFSET.
>
> > For that you can use memblock_enforce_memory_limit(-PAGE_OFFSET).
> >
> Thanks. I think we can just call memblock_enforce_memory_limit without
> tracking the total memory size
> and whether maximum memory described in DT is greater than  -PAGE_OFFSET.
>
> @Anup Patel Was there any other reason for this change originally?

No other reason. We just wanted to ensure that amount of memory addressable
by kernel (i.e. -PAGE_OFFSET) is also considered when removing memblock.

Regards,
Anup

>
> > > Just use the start of the first memory block and the end of the last memory
> > > block to compute the size of the total memory that can be used.
> > >
> > > Signed-off-by: Atish Patra <atish.patra@wdc.com>
> > > ---
> > >  arch/riscv/mm/init.c | 6 ++----
> > >  1 file changed, 2 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > > index 787c75f751a5..188281fc2816 100644
> > > --- a/arch/riscv/mm/init.c
> > > +++ b/arch/riscv/mm/init.c
> > > @@ -147,7 +147,6 @@ void __init setup_bootmem(void)
> > >  {
> > >       struct memblock_region *reg;
> > >       phys_addr_t mem_size = 0;
> > > -     phys_addr_t total_mem = 0;
> > >       phys_addr_t mem_start, end = 0;
> > >       phys_addr_t vmlinux_end = __pa_symbol(&_end);
> > >       phys_addr_t vmlinux_start = __pa_symbol(&_start);
> > > @@ -155,18 +154,17 @@ void __init setup_bootmem(void)
> > >       /* Find the memory region containing the kernel */
> > >       for_each_memblock(memory, reg) {
> > >               end = reg->base + reg->size;
> > > -             if (!total_mem)
> > > +             if (!mem_start)
> > >                       mem_start = reg->base;
> > >               if (reg->base <= vmlinux_start && vmlinux_end <= end)
> > >                       BUG_ON(reg->size == 0);
> > > -             total_mem = total_mem + reg->size;
> > >       }
> > >
> > >       /*
> > >        * Remove memblock from the end of usable area to the
> > >        * end of region
> > >        */
> > > -     mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
> > > +     mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
> > >       if (mem_start + mem_size < end)
> > >               memblock_remove(mem_start + mem_size,
> > >                               end - mem_start - mem_size);
> > > --
> > > 2.24.0
> > >
> >
> > --
> > Sincerely yours,
> > Mike.
> >
> > _______________________________________________
> > linux-riscv mailing list
> > linux-riscv@lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/linux-riscv
>
>
>
> --
> Regards,
> Atish

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

* Re: [PATCH] RISC-V: Consider sparse memory while removing unusable memory
  2020-09-14 11:52     ` Anup Patel
@ 2020-09-14 17:53       ` Atish Patra
  0 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2020-09-14 17:53 UTC (permalink / raw)
  To: Anup Patel
  Cc: Mike Rapoport, Anup Patel, Atish Patra, Albert Ou,
	linux-kernel@vger.kernel.org List, Palmer Dabbelt, Zong Li,
	Paul Walmsley, linux-riscv

On Mon, Sep 14, 2020 at 4:52 AM Anup Patel <anup@brainfault.org> wrote:
>
> On Mon, Sep 14, 2020 at 5:18 AM Atish Patra <atishp@atishpatra.org> wrote:
> >
> > On Sat, Sep 12, 2020 at 3:45 AM Mike Rapoport <rppt@kernel.org> wrote:
> > >
> > > Hello Atish,
> > >
> > > On Fri, Sep 11, 2020 at 05:23:41PM -0700, Atish Patra wrote:
> > > > Currently, any usable memory area beyond page_offset is removed by adding the
> > > > memory sizes from each memblock. That may not work for sparse memory
> > > > as memory regions can be very far apart resulting incorrect removal of some
> > > > usable memory.
> > >
> > > If I understand correctly, the memory with physical addresses larger
> > > than (-PAGE_OFFSET) cannot be used. Since it was aready
> > > memblock_add()'ed during device tree parsing, you need to remove it from
> > > memblock.
> > >
> >
> > IIRC, the original intention was to fix MAXPHYSMEM_2GB option for RV64
> > for the medlow model.
> > That's why the patch removed any memory beyond -PAGE_OFFSET.
> >
> > > For that you can use memblock_enforce_memory_limit(-PAGE_OFFSET).
> > >
> > Thanks. I think we can just call memblock_enforce_memory_limit without
> > tracking the total memory size
> > and whether maximum memory described in DT is greater than  -PAGE_OFFSET.
> >
> > @Anup Patel Was there any other reason for this change originally?
>
> No other reason. We just wanted to ensure that amount of memory addressable
> by kernel (i.e. -PAGE_OFFSET) is also considered when removing memblock.
>

It looks like we have an agreement here then. I will update the patch
to directly call
memblock_enforce_memory_limit as suggested by Mike.

> Regards,
> Anup
>
> >
> > > > Just use the start of the first memory block and the end of the last memory
> > > > block to compute the size of the total memory that can be used.
> > > >
> > > > Signed-off-by: Atish Patra <atish.patra@wdc.com>
> > > > ---
> > > >  arch/riscv/mm/init.c | 6 ++----
> > > >  1 file changed, 2 insertions(+), 4 deletions(-)
> > > >
> > > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > > > index 787c75f751a5..188281fc2816 100644
> > > > --- a/arch/riscv/mm/init.c
> > > > +++ b/arch/riscv/mm/init.c
> > > > @@ -147,7 +147,6 @@ void __init setup_bootmem(void)
> > > >  {
> > > >       struct memblock_region *reg;
> > > >       phys_addr_t mem_size = 0;
> > > > -     phys_addr_t total_mem = 0;
> > > >       phys_addr_t mem_start, end = 0;
> > > >       phys_addr_t vmlinux_end = __pa_symbol(&_end);
> > > >       phys_addr_t vmlinux_start = __pa_symbol(&_start);
> > > > @@ -155,18 +154,17 @@ void __init setup_bootmem(void)
> > > >       /* Find the memory region containing the kernel */
> > > >       for_each_memblock(memory, reg) {
> > > >               end = reg->base + reg->size;
> > > > -             if (!total_mem)
> > > > +             if (!mem_start)
> > > >                       mem_start = reg->base;
> > > >               if (reg->base <= vmlinux_start && vmlinux_end <= end)
> > > >                       BUG_ON(reg->size == 0);
> > > > -             total_mem = total_mem + reg->size;
> > > >       }
> > > >
> > > >       /*
> > > >        * Remove memblock from the end of usable area to the
> > > >        * end of region
> > > >        */
> > > > -     mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
> > > > +     mem_size = min(end - mem_start, (phys_addr_t)-PAGE_OFFSET);
> > > >       if (mem_start + mem_size < end)
> > > >               memblock_remove(mem_start + mem_size,
> > > >                               end - mem_start - mem_size);
> > > > --
> > > > 2.24.0
> > > >
> > >
> > > --
> > > Sincerely yours,
> > > Mike.
> > >
> > > _______________________________________________
> > > linux-riscv mailing list
> > > linux-riscv@lists.infradead.org
> > > http://lists.infradead.org/mailman/listinfo/linux-riscv
> >
> >
> >
> > --
> > Regards,
> > Atish



-- 
Regards,
Atish

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

end of thread, other threads:[~2020-09-14 17:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-12  0:23 [PATCH] RISC-V: Consider sparse memory while removing unusable memory Atish Patra
2020-09-12 10:45 ` Mike Rapoport
2020-09-13 23:47   ` Atish Patra
2020-09-14 11:52     ` Anup Patel
2020-09-14 17:53       ` Atish Patra
2020-09-14 11:49   ` Anup Patel

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).