diff mbox

arm64: move back to generic memblock_enforce_memory_limit()

Message ID 1454351417-23732-1-git-send-email-ard.biesheuvel@linaro.org
State New
Headers show

Commit Message

Ard Biesheuvel Feb. 1, 2016, 6:30 p.m. UTC
Rather than implementing our own elaborate logic to clip the memory ranges
in order of preference, use the generic memblock_enforce_memory_limit() as
before, and explicitly add back that part of the kernel that needs to be
accessible via the linear mapping as well as via the kernel mapping.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 arch/arm64/mm/init.c | 59 +++++++---------------------------------------------
 1 file changed, 7 insertions(+), 52 deletions(-)

-- 
2.5.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Comments

Catalin Marinas Feb. 2, 2016, 10:19 a.m. UTC | #1
On Mon, Feb 01, 2016 at 07:30:17PM +0100, Ard Biesheuvel wrote:
>  void __init arm64_memblock_init(void)

>  {

>  	const s64 linear_region_size = -(s64)PAGE_OFFSET;

> @@ -215,24 +180,14 @@ void __init arm64_memblock_init(void)

>  	if (memblock_end_of_DRAM() > linear_region_size)

>  		memblock_remove(0, memblock_end_of_DRAM() - linear_region_size);

>  

> +	/*

> +	 * Apply the memory limit if it was set. Since the kernel may be loaded

> +	 * high up in memory, add back the kernel region that must be accessible

> +	 * via the linear mapping.

> +	 */

>  	if (memory_limit != (phys_addr_t)ULLONG_MAX) {

> -		u64 kbase = round_down(__pa(_text), MIN_KIMG_ALIGN);

> -		u64 kend = PAGE_ALIGN(__pa(_end));

> -		u64 const sz_4g = 0x100000000UL;

> -

> -		/*

> -		 * Clip memory in order of preference:

> -		 * - above the kernel and above 4 GB

> -		 * - between 4 GB and the start of the kernel (if the kernel

> -		 *   is loaded high in memory)

> -		 * - between the kernel and 4 GB (if the kernel is loaded

> -		 *   low in memory)

> -		 * - below 4 GB

> -		 */

> -		clip_mem_range(max(sz_4g, kend), ULLONG_MAX);

> -		clip_mem_range(sz_4g, kbase);

> -		clip_mem_range(kend, sz_4g);

> -		clip_mem_range(0, min(kbase, sz_4g));

> +		memblock_enforce_memory_limit(memory_limit);

> +		memblock_add(__pa(__init_begin), (u64)(_end - __init_begin));


Thanks, it looks much simpler now. However, loading the kernel 1GB
higher with mem=1G fails somewhere during the KVM hyp initialisation. It
works if I change the last line below to:

	memblock_add(__pa(_text), (u64)(_end - _text));

I can fold the change in.

-- 
Catalin

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ard Biesheuvel Feb. 2, 2016, 10:28 a.m. UTC | #2
On 2 February 2016 at 11:19, Catalin Marinas <catalin.marinas@arm.com> wrote:
> On Mon, Feb 01, 2016 at 07:30:17PM +0100, Ard Biesheuvel wrote:

>>  void __init arm64_memblock_init(void)

>>  {

>>       const s64 linear_region_size = -(s64)PAGE_OFFSET;

>> @@ -215,24 +180,14 @@ void __init arm64_memblock_init(void)

>>       if (memblock_end_of_DRAM() > linear_region_size)

>>               memblock_remove(0, memblock_end_of_DRAM() - linear_region_size);

>>

>> +     /*

>> +      * Apply the memory limit if it was set. Since the kernel may be loaded

>> +      * high up in memory, add back the kernel region that must be accessible

>> +      * via the linear mapping.

>> +      */

>>       if (memory_limit != (phys_addr_t)ULLONG_MAX) {

>> -             u64 kbase = round_down(__pa(_text), MIN_KIMG_ALIGN);

>> -             u64 kend = PAGE_ALIGN(__pa(_end));

>> -             u64 const sz_4g = 0x100000000UL;

>> -

>> -             /*

>> -              * Clip memory in order of preference:

>> -              * - above the kernel and above 4 GB

>> -              * - between 4 GB and the start of the kernel (if the kernel

>> -              *   is loaded high in memory)

>> -              * - between the kernel and 4 GB (if the kernel is loaded

>> -              *   low in memory)

>> -              * - below 4 GB

>> -              */

>> -             clip_mem_range(max(sz_4g, kend), ULLONG_MAX);

>> -             clip_mem_range(sz_4g, kbase);

>> -             clip_mem_range(kend, sz_4g);

>> -             clip_mem_range(0, min(kbase, sz_4g));

>> +             memblock_enforce_memory_limit(memory_limit);

>> +             memblock_add(__pa(__init_begin), (u64)(_end - __init_begin));

>

> Thanks, it looks much simpler now. However, loading the kernel 1GB

> higher with mem=1G fails somewhere during the KVM hyp initialisation. It

> works if I change the last line below to:

>

>         memblock_add(__pa(_text), (u64)(_end - _text));

>


OK, that should work as well.

I suppose the fact that mem= loses some of its accuracy is not an
issue? If you need it to be exact, you should simply not load your
kernel outside your mem= range ...

> I can fold the change in.

>


OK

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Catalin Marinas Feb. 2, 2016, 10:44 a.m. UTC | #3
On Tue, Feb 02, 2016 at 11:28:41AM +0100, Ard Biesheuvel wrote:
> On 2 February 2016 at 11:19, Catalin Marinas <catalin.marinas@arm.com> wrote:

> > On Mon, Feb 01, 2016 at 07:30:17PM +0100, Ard Biesheuvel wrote:

> >>  void __init arm64_memblock_init(void)

> >>  {

> >>       const s64 linear_region_size = -(s64)PAGE_OFFSET;

> >> @@ -215,24 +180,14 @@ void __init arm64_memblock_init(void)

> >>       if (memblock_end_of_DRAM() > linear_region_size)

> >>               memblock_remove(0, memblock_end_of_DRAM() - linear_region_size);

> >>

> >> +     /*

> >> +      * Apply the memory limit if it was set. Since the kernel may be loaded

> >> +      * high up in memory, add back the kernel region that must be accessible

> >> +      * via the linear mapping.

> >> +      */

> >>       if (memory_limit != (phys_addr_t)ULLONG_MAX) {

> >> -             u64 kbase = round_down(__pa(_text), MIN_KIMG_ALIGN);

> >> -             u64 kend = PAGE_ALIGN(__pa(_end));

> >> -             u64 const sz_4g = 0x100000000UL;

> >> -

> >> -             /*

> >> -              * Clip memory in order of preference:

> >> -              * - above the kernel and above 4 GB

> >> -              * - between 4 GB and the start of the kernel (if the kernel

> >> -              *   is loaded high in memory)

> >> -              * - between the kernel and 4 GB (if the kernel is loaded

> >> -              *   low in memory)

> >> -              * - below 4 GB

> >> -              */

> >> -             clip_mem_range(max(sz_4g, kend), ULLONG_MAX);

> >> -             clip_mem_range(sz_4g, kbase);

> >> -             clip_mem_range(kend, sz_4g);

> >> -             clip_mem_range(0, min(kbase, sz_4g));

> >> +             memblock_enforce_memory_limit(memory_limit);

> >> +             memblock_add(__pa(__init_begin), (u64)(_end - __init_begin));

> >

> > Thanks, it looks much simpler now. However, loading the kernel 1GB

> > higher with mem=1G fails somewhere during the KVM hyp initialisation. It

> > works if I change the last line below to:

> >

> >         memblock_add(__pa(_text), (u64)(_end - _text));

> 

> OK, that should work as well.

> 

> I suppose the fact that mem= loses some of its accuracy is not an

> issue? If you need it to be exact, you should simply not load your

> kernel outside your mem= range ...


I'm not worried about accuracy. We could avoid freeing the init mem if
the kernel is outside the memory_limit range but I don't really think
it's worth.

-- 
Catalin

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
diff mbox

Patch

diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index e8e853a1024c..361c91209031 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -160,41 +160,6 @@  static int __init early_mem(char *p)
 }
 early_param("mem", early_mem);
 
-/*
- * clip_mem_range() - remove memblock memory between @min and @max until
- *                    we meet the limit in 'memory_limit'.
- */
-static void __init clip_mem_range(u64 min, u64 max)
-{
-	u64 mem_size, to_remove;
-	int i;
-
-again:
-	mem_size = memblock_phys_mem_size();
-	if (mem_size <= memory_limit || max <= min)
-		return;
-
-	to_remove = mem_size - memory_limit;
-
-	for (i = memblock.memory.cnt - 1; i >= 0; i--) {
-		struct memblock_region *r = memblock.memory.regions + i;
-		u64 start = max(min, r->base);
-		u64 end = min(max, r->base + r->size);
-
-		if (start >= max || end <= min)
-			continue;
-
-		if (end > min) {
-			u64 size = min(to_remove, end - max(start, min));
-
-			memblock_remove(end - size, size);
-		} else {
-			memblock_remove(start, min(max - start, to_remove));
-		}
-		goto again;
-	}
-}
-
 void __init arm64_memblock_init(void)
 {
 	const s64 linear_region_size = -(s64)PAGE_OFFSET;
@@ -215,24 +180,14 @@  void __init arm64_memblock_init(void)
 	if (memblock_end_of_DRAM() > linear_region_size)
 		memblock_remove(0, memblock_end_of_DRAM() - linear_region_size);
 
+	/*
+	 * Apply the memory limit if it was set. Since the kernel may be loaded
+	 * high up in memory, add back the kernel region that must be accessible
+	 * via the linear mapping.
+	 */
 	if (memory_limit != (phys_addr_t)ULLONG_MAX) {
-		u64 kbase = round_down(__pa(_text), MIN_KIMG_ALIGN);
-		u64 kend = PAGE_ALIGN(__pa(_end));
-		u64 const sz_4g = 0x100000000UL;
-
-		/*
-		 * Clip memory in order of preference:
-		 * - above the kernel and above 4 GB
-		 * - between 4 GB and the start of the kernel (if the kernel
-		 *   is loaded high in memory)
-		 * - between the kernel and 4 GB (if the kernel is loaded
-		 *   low in memory)
-		 * - below 4 GB
-		 */
-		clip_mem_range(max(sz_4g, kend), ULLONG_MAX);
-		clip_mem_range(sz_4g, kbase);
-		clip_mem_range(kend, sz_4g);
-		clip_mem_range(0, min(kbase, sz_4g));
+		memblock_enforce_memory_limit(memory_limit);
+		memblock_add(__pa(__init_begin), (u64)(_end - __init_begin));
 	}
 
 	/*