2021-09-23 22:55:55

by Chris Goldsworthy

[permalink] [raw]
Subject: [RFC] arm64: mm: update max_pfn after memory hotplug

On arm64 we set max_pfn at boot in arch/arm64/mm/init.c. If you
hotplug in memory after booting up, max_pfn is not updated. This
breaks diagnostic functions executed from user space like
read_page_owner():

https://elixir.bootlin.com/linux/v5.14.7/source/mm/page_owner.c#L472

or kpageflags_read() (see how get_max_dump_pfn() is used):

https://elixir.bootlin.com/linux/v5.14.7/source/fs/proc/page.c#L47

Thus, this patch updates max_pfn and max_low_pfn in arm64's
arch_add_memory() function, mirroring what is updatated during boot:

https://elixir.bootlin.com/linux/v5.14.7/source/arch/arm64/mm/init.c#L448

We would appreciate feedback on any other variables that should be
updated when hotplugging in memory - note that we're mirroring x86, in
that max_pfn is only ever incremented when calling arch_add_memory():

https://elixir.bootlin.com/linux/v5.14.7/source/arch/x86/mm/init_64.c#L958

Sudarshan Rajagopalan (1):
arm64: mm: update max_pfn after memory hotplug

arch/arm64/mm/mmu.c | 5 +++++
1 file changed, 5 insertions(+)

--
2.7.4


2021-09-23 22:56:32

by Chris Goldsworthy

[permalink] [raw]
Subject: [RFC] arm64: mm: update max_pfn after memory hotplug

From: Sudarshan Rajagopalan <[email protected]>

After new memory blocks have been hotplugged, max_pfn and max_low_pfn
needs updating to reflect on new PFNs being hot added to system.

Signed-off-by: Sudarshan Rajagopalan <[email protected]>
Signed-off-by: Chris Goldsworthy <[email protected]>
---
arch/arm64/mm/mmu.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index cfd9deb..fd85b51 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
if (ret)
__remove_pgd_mapping(swapper_pg_dir,
__phys_to_virt(start), size);
+ else {
+ max_pfn = PFN_UP(start + size);
+ max_low_pfn = max_pfn;
+ }
+
return ret;
}

--
2.7.4

2021-09-24 02:49:47

by Florian Fainelli

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug



On 9/23/2021 3:54 PM, Chris Goldsworthy wrote:
> From: Sudarshan Rajagopalan <[email protected]>
>
> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> needs updating to reflect on new PFNs being hot added to system.
>
> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> Signed-off-by: Chris Goldsworthy <[email protected]>
> ---
> arch/arm64/mm/mmu.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index cfd9deb..fd85b51 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> if (ret)
> __remove_pgd_mapping(swapper_pg_dir,
> __phys_to_virt(start), size);
> + else {
> + max_pfn = PFN_UP(start + size);
> + max_low_pfn = max_pfn;
> + }

This is a drive by review, but it got me thinking about your changes a bit:

- if you raise max_pfn when you hotplug memory, don't you need to lower
it when you hot unplug memory as well?

- suppose that you have a platform which maps physical memory into the
CPU's address space at 0x00_4000_0000 (1GB offset) and the kernel boots
with 2GB of DRAM plugged by default. At that point we have not
registered a swiotlb because we have less than 4GB of addressable
physical memory, there is no IOMMU in that system, it's a happy world.
Now assume that we plug an additional 2GB of DRAM into that system
adjacent to the previous 2GB, from 0x00_C0000_0000 through
0x14_0000_0000, now we have physical addresses above 4GB, but we still
don't have a swiotlb, some of our DMA_BIT_MASK(32) peripherals are going
to be unable to DMA from that hot plugged memory, but they could if we
had a swiotlb.

- now let's go even further but this is very contrived. Assume that the
firmware has somewhat created a reserved memory region with a 'no-map'
attribute thus indicating it does not want a struct page to be created
for a specific PFN range, is it valid to "blindly" raise max_pfn if that
region were to be at the end of the just hot-plugged memory?
--
Florian

2021-09-24 08:19:31

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 24.09.21 04:47, Florian Fainelli wrote:
>
>
> On 9/23/2021 3:54 PM, Chris Goldsworthy wrote:
>> From: Sudarshan Rajagopalan <[email protected]>
>>
>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>> needs updating to reflect on new PFNs being hot added to system.
>>
>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>> Signed-off-by: Chris Goldsworthy <[email protected]>
>> ---
>> arch/arm64/mm/mmu.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>> index cfd9deb..fd85b51 100644
>> --- a/arch/arm64/mm/mmu.c
>> +++ b/arch/arm64/mm/mmu.c
>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>> if (ret)
>> __remove_pgd_mapping(swapper_pg_dir,
>> __phys_to_virt(start), size);
>> + else {
>> + max_pfn = PFN_UP(start + size);
>> + max_low_pfn = max_pfn;
>> + }
>
> This is a drive by review, but it got me thinking about your changes a bit:
>
> - if you raise max_pfn when you hotplug memory, don't you need to lower
> it when you hot unplug memory as well?

The issue with lowering is that you actually have to do some search to
figure out the actual value -- and it's not really worth the trouble.
Raising the limit is easy.

With memory hotunplug, anybody wanting to take a look at a "struct page"
via a pfn has to do a pfn_to_online_page() either way. That will fail if
there isn't actually a memmap anymore because the memory has been
unplugged. So "max_pfn" is actually rather a hint what maximum pfn to
look at, and it can be bigger than it actually is.

The a look at the example usage in fs/proc/page.c:kpageflags_read()

pfn_to_online_page() will simply fail and stable_page_flags() will
indicate a KPF_NOPAGE.

Just like we would have a big memory hole now at the end of memory.

>
> - suppose that you have a platform which maps physical memory into the
> CPU's address space at 0x00_4000_0000 (1GB offset) and the kernel boots
> with 2GB of DRAM plugged by default. At that point we have not
> registered a swiotlb because we have less than 4GB of addressable
> physical memory, there is no IOMMU in that system, it's a happy world.
> Now assume that we plug an additional 2GB of DRAM into that system
> adjacent to the previous 2GB, from 0x00_C0000_0000 through
> 0x14_0000_0000, now we have physical addresses above 4GB, but we still
> don't have a swiotlb, some of our DMA_BIT_MASK(32) peripherals are going
> to be unable to DMA from that hot plugged memory, but they could if we
> had a swiotlb.

That's why platforms that hotplug memory should indicate the maximum
possible PFN via some mechanism during boot. On x86-64 (and IIRC also
arm64 now), this is done via the ACPI SRAT.

And that's where "max_possible_pfn" and "max_pfn" differ. See
drivers/acpi/numa/srat.c:acpi_numa_memory_affinity_init():

max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));$


Using max_possible_pfn, the OS can properly setup the swiotlb, even
thought it wouldn't currently be required when just looking at max_pfn.

I documented that for virtio-mem in
https://virtio-mem.gitlab.io/user-guide/user-guide-linux.html
"swiotlb and DMA memory".

>
> - now let's go even further but this is very contrived. Assume that the
> firmware has somewhat created a reserved memory region with a 'no-map'
> attribute thus indicating it does not want a struct page to be created
> for a specific PFN range, is it valid to "blindly" raise max_pfn if that
> region were to be at the end of the just hot-plugged memory?

no-map means that no direct mapping is to be created, right? We would
still have a memmap IIRC, and the pages are PG_reserved.

Again, I think this is very similar to just having no-map regions like
random memory holes within the existing memory layout.


What Chris proposes here is very similar to
arch/x86/mm/init_64.c:update_end_of_memory_vars() called during
arch_add_memory()->add_pages() on x86-64.

--
Thanks,

David / dhildenb

2021-09-24 22:45:01

by Chris Goldsworthy

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug


Thanks for the response David.

On Fri, Sep 24, 2021 at 10:17:46AM +0200, David Hildenbrand wrote:
> no-map means that no direct mapping is to be created, right? We would still
> have a memmap IIRC, and the pages are PG_reserved.
>
> Again, I think this is very similar to just having no-map regions like
> random memory holes within the existing memory layout.

For those curious, see __reserved_mem_alloc_size() >
early_init_dt_alloc_reserved_memory_arch() > memblock_mark_nomap() - the
'no-map' attribute is read in __reserved_mem_alloc_size() and the pre-requisite
steps need to have the relevant struct pages marked as PG_reserved
are taken in memblock_mark_nomap().

> What Chris proposes here is very similar to
> arch/x86/mm/init_64.c:update_end_of_memory_vars() called during
> arch_add_memory()->add_pages() on x86-64.
>

For other's reference, the patch was derived from what x86 is doing with max_pfn
(such that we also set max_low_pfn as is done in arm64's mm/init.c.

2021-09-27 15:56:30

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 24.09.21 00:54, Chris Goldsworthy wrote:
> From: Sudarshan Rajagopalan <[email protected]>
>
> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> needs updating to reflect on new PFNs being hot added to system.
>
> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> Signed-off-by: Chris Goldsworthy <[email protected]>
> ---
> arch/arm64/mm/mmu.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index cfd9deb..fd85b51 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> if (ret)
> __remove_pgd_mapping(swapper_pg_dir,
> __phys_to_virt(start), size);
> + else {
> + max_pfn = PFN_UP(start + size);
> + max_low_pfn = max_pfn;
> + }
> +
> return ret;

Note: didn't verify if updating max_low_pfn is correct here.

Acked-by: David Hildenbrand <[email protected]>


--
Thanks,

David / dhildenb

2021-09-27 17:29:33

by Georgi Djakov

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 9/24/2021 1:54 AM, Chris Goldsworthy wrote:
> From: Sudarshan Rajagopalan <[email protected]>
>
> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> needs updating to reflect on new PFNs being hot added to system.
>
> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> Signed-off-by: Chris Goldsworthy <[email protected]>

Thanks for the patch, Chris!

With this patch, the data in /proc/kpageflags appears to be correct and
memory tools like procrank work again on arm64 platforms.

Tested-by: Georgi Djakov <[email protected]>

Maybe we should add fixes tag, as it has been broken since the following
commit:
Fixes: abec749facff ("fs/proc/page.c: allow inspection of last section
and fix end detection")

Thanks,
Georgi

> ---
> arch/arm64/mm/mmu.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index cfd9deb..fd85b51 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> if (ret)
> __remove_pgd_mapping(swapper_pg_dir,
> __phys_to_virt(start), size);
> + else {
> + max_pfn = PFN_UP(start + size);
> + max_low_pfn = max_pfn;
> + }
> +
> return ret;
> }
>
>

2021-09-27 17:37:59

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 27.09.21 19:22, Georgi Djakov wrote:
> On 9/24/2021 1:54 AM, Chris Goldsworthy wrote:
>> From: Sudarshan Rajagopalan <[email protected]>
>>
>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>> needs updating to reflect on new PFNs being hot added to system.
>>
>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>> Signed-off-by: Chris Goldsworthy <[email protected]>
>
> Thanks for the patch, Chris!
>
> With this patch, the data in /proc/kpageflags appears to be correct and
> memory tools like procrank work again on arm64 platforms.
>
> Tested-by: Georgi Djakov <[email protected]>
>
> Maybe we should add fixes tag, as it has been broken since the following
> commit:
> Fixes: abec749facff ("fs/proc/page.c: allow inspection of last section
> and fix end detection")

Are you sure that that commit broke it?

I recall that we would naturally run into the limit, because

count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);

wouldn't really do what you would expect either. But you could
force-read beyond max_pfn, yes, because the count computation was just
weird.


I think the real issue is not properly adjusting max_pfn in the first
place when we introduced memoruy hotplug on arm64.

--
Thanks,

David / dhildenb

2021-09-27 20:04:44

by Georgi Djakov

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 9/27/2021 8:34 PM, David Hildenbrand wrote:
> On 27.09.21 19:22, Georgi Djakov wrote:
>> On 9/24/2021 1:54 AM, Chris Goldsworthy wrote:
>>> From: Sudarshan Rajagopalan <[email protected]>
>>>
>>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>>> needs updating to reflect on new PFNs being hot added to system.
>>>
>>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>>> Signed-off-by: Chris Goldsworthy <[email protected]>
>>
>> Thanks for the patch, Chris!
>>
>> With this patch, the data in /proc/kpageflags appears to be correct and
>> memory tools like procrank work again on arm64 platforms.
>>
>> Tested-by: Georgi Djakov <[email protected]>
>>
>> Maybe we should add fixes tag, as it has been broken since the following
>> commit:
>> Fixes: abec749facff ("fs/proc/page.c: allow inspection of last section
>> and fix end detection")
>
> Are you sure that that commit broke it?

Reverting the above commit also "fixes" kpageflags, otherwise
kpageflags_read() returns 0 in the following check:
if (src >= max_dump_pfn * KPMSIZE)
return 0;

> I recall that we would naturally run into the limit, because
>
> count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);

The function returns before we reach this line.

Thanks,
Georgi

> wouldn't really do what you would expect either. But you could
> force-read beyond max_pfn, yes, because the count computation was just
> weird.
>
>
> I think the real issue is not properly adjusting max_pfn in the first
> place when we introduced memoruy hotplug on arm64

2021-09-27 20:17:02

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 27.09.21 22:00, Georgi Djakov wrote:
> On 9/27/2021 8:34 PM, David Hildenbrand wrote:
>> On 27.09.21 19:22, Georgi Djakov wrote:
>>> On 9/24/2021 1:54 AM, Chris Goldsworthy wrote:
>>>> From: Sudarshan Rajagopalan <[email protected]>
>>>>
>>>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>>>> needs updating to reflect on new PFNs being hot added to system.
>>>>
>>>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>>>> Signed-off-by: Chris Goldsworthy <[email protected]>
>>>
>>> Thanks for the patch, Chris!
>>>
>>> With this patch, the data in /proc/kpageflags appears to be correct and
>>> memory tools like procrank work again on arm64 platforms.
>>>
>>> Tested-by: Georgi Djakov <[email protected]>
>>>
>>> Maybe we should add fixes tag, as it has been broken since the following
>>> commit:
>>> Fixes: abec749facff ("fs/proc/page.c: allow inspection of last section
>>> and fix end detection")
>>
>> Are you sure that that commit broke it?
>
> Reverting the above commit also "fixes" kpageflags, otherwise
> kpageflags_read() returns 0 in the following check:
> if (src >= max_dump_pfn * KPMSIZE)
> return 0;
>
>> I recall that we would naturally run into the limit, because
>>
>> count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
>
> The function returns before we reach this line.

That is the old code. I don't see how the behavior of the old code with
wrong max_pfn was doing what it's supposed to do.

page_idle and page_owner also rely on max_pfn. The root issue is that
max_pfn wasn't updated properly.

--
Thanks,

David / dhildenb

2021-09-27 23:27:05

by Georgi Djakov

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 9/27/2021 6:51 PM, David Hildenbrand wrote:
> On 24.09.21 00:54, Chris Goldsworthy wrote:
>> From: Sudarshan Rajagopalan <[email protected]>
>>
>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>> needs updating to reflect on new PFNs being hot added to system.
>>
>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>> Signed-off-by: Chris Goldsworthy <[email protected]>
>> ---
>>   arch/arm64/mm/mmu.c | 5 +++++
>>   1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>> index cfd9deb..fd85b51 100644
>> --- a/arch/arm64/mm/mmu.c
>> +++ b/arch/arm64/mm/mmu.c
>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>       if (ret)
>>           __remove_pgd_mapping(swapper_pg_dir,
>>                        __phys_to_virt(start), size);
>> +    else {
>> +        max_pfn = PFN_UP(start + size);
>> +        max_low_pfn = max_pfn;
>> +    }
>> +
>>       return ret;
>
> Note: didn't verify if updating max_low_pfn is correct here.

My understanding is that max_low_pfn defines the low/high memory
boundary and it should be also updated.

Thanks,
Georgi

2021-09-28 06:13:25

by Chris Goldsworthy

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On Tue, Sep 28, 2021 at 02:22:59AM +0300, Georgi Djakov wrote:
> On 9/27/2021 6:51 PM, David Hildenbrand wrote:
> >On 24.09.21 00:54, Chris Goldsworthy wrote:
> >>From: Sudarshan Rajagopalan <[email protected]>
> >>
> >>After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> >>needs updating to reflect on new PFNs being hot added to system.
> >>
> >>Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> >>Signed-off-by: Chris Goldsworthy <[email protected]>
> >>---
> >>? arch/arm64/mm/mmu.c | 5 +++++
> >>? 1 file changed, 5 insertions(+)
> >>
> >>diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> >>index cfd9deb..fd85b51 100644
> >>--- a/arch/arm64/mm/mmu.c
> >>+++ b/arch/arm64/mm/mmu.c
> >>@@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> >>????? if (ret)
> >>????????? __remove_pgd_mapping(swapper_pg_dir,
> >>?????????????????????? __phys_to_virt(start), size);
> >>+??? else {
> >>+??????? max_pfn = PFN_UP(start + size);
> >>+??????? max_low_pfn = max_pfn;
> >>+??? }
> >>+
> >>????? return ret;
> >
> >Note: didn't verify if updating max_low_pfn is correct here.
>
> My understanding is that max_low_pfn defines the low/high memory
> boundary and it should be also updated.
>
> Thanks,
> Georgi

To build more on Georgi's response, our assumption here after an offline
discussion is that max_low_pfn would not be equal to max_pfn only if there is
high memory - another assumption is that arm64 kernels will not need high memory
due to their large logical mappings. Under these two assumptions, the patch is
correct. Perhaps Catalin can ack or critique this, as he initially set max_pfn =
max_low_pfn in the first arm64 mm initialization code:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c1cc1552616d0f354d040823151e61634e7ad01f

2021-09-28 07:35:25

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 28.09.21 08:12, Chris Goldsworthy wrote:
> On Tue, Sep 28, 2021 at 02:22:59AM +0300, Georgi Djakov wrote:
>> On 9/27/2021 6:51 PM, David Hildenbrand wrote:
>>> On 24.09.21 00:54, Chris Goldsworthy wrote:
>>>> From: Sudarshan Rajagopalan <[email protected]>
>>>>
>>>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>>>> needs updating to reflect on new PFNs being hot added to system.
>>>>
>>>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>>>> Signed-off-by: Chris Goldsworthy <[email protected]>
>>>> ---
>>>>   arch/arm64/mm/mmu.c | 5 +++++
>>>>   1 file changed, 5 insertions(+)
>>>>
>>>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>>>> index cfd9deb..fd85b51 100644
>>>> --- a/arch/arm64/mm/mmu.c
>>>> +++ b/arch/arm64/mm/mmu.c
>>>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>>>       if (ret)
>>>>           __remove_pgd_mapping(swapper_pg_dir,
>>>>                        __phys_to_virt(start), size);
>>>> +    else {
>>>> +        max_pfn = PFN_UP(start + size);
>>>> +        max_low_pfn = max_pfn;
>>>> +    }
>>>> +
>>>>       return ret;
>>>
>>> Note: didn't verify if updating max_low_pfn is correct here.
>>
>> My understanding is that max_low_pfn defines the low/high memory
>> boundary and it should be also updated.
>>
>> Thanks,
>> Georgi
>
> To build more on Georgi's response, our assumption here after an offline
> discussion is that max_low_pfn would not be equal to max_pfn only if there is
> high memory - another assumption is that arm64 kernels will not need high memory
> due to their large logical mappings. Under these two assumptions, the patch is
> correct. Perhaps Catalin can ack or critique this, as he initially set max_pfn =
> max_low_pfn in the first arm64 mm initialization code:

Makes sense to me, thanks.


--
Thanks,

David / dhildenb

2021-09-29 10:14:04

by Will Deacon

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
> From: Sudarshan Rajagopalan <[email protected]>
>
> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> needs updating to reflect on new PFNs being hot added to system.
>
> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> Signed-off-by: Chris Goldsworthy <[email protected]>
> ---
> arch/arm64/mm/mmu.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index cfd9deb..fd85b51 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> if (ret)
> __remove_pgd_mapping(swapper_pg_dir,
> __phys_to_virt(start), size);
> + else {
> + max_pfn = PFN_UP(start + size);
> + max_low_pfn = max_pfn;
> + }

We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
updating as well?

Do we have sufficient locking to ensure nobody is looking at max_pfn or
max_low_pfn while we update them?

Will

2021-09-29 10:31:22

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 29.09.21 12:10, Will Deacon wrote:
> On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
>> From: Sudarshan Rajagopalan <[email protected]>
>>
>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>> needs updating to reflect on new PFNs being hot added to system.
>>
>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>> Signed-off-by: Chris Goldsworthy <[email protected]>
>> ---
>> arch/arm64/mm/mmu.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>> index cfd9deb..fd85b51 100644
>> --- a/arch/arm64/mm/mmu.c
>> +++ b/arch/arm64/mm/mmu.c
>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>> if (ret)
>> __remove_pgd_mapping(swapper_pg_dir,
>> __phys_to_virt(start), size);
>> + else {
>> + max_pfn = PFN_UP(start + size);
>> + max_low_pfn = max_pfn;
>> + }
>
> We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
> updating as well?
>
> Do we have sufficient locking to ensure nobody is looking at max_pfn or
> max_low_pfn while we update them?

Only the write side is protected by memory hotplug locking. The read
side is lockless -- just like all of the other pfn_to_online_page()
machinery.

>
> Will
>


--
Thanks,

David / dhildenb

2021-09-29 11:08:03

by Will Deacon

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On Wed, Sep 29, 2021 at 12:29:32PM +0200, David Hildenbrand wrote:
> On 29.09.21 12:10, Will Deacon wrote:
> > On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
> > > From: Sudarshan Rajagopalan <[email protected]>
> > >
> > > After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> > > needs updating to reflect on new PFNs being hot added to system.
> > >
> > > Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> > > Signed-off-by: Chris Goldsworthy <[email protected]>
> > > ---
> > > arch/arm64/mm/mmu.c | 5 +++++
> > > 1 file changed, 5 insertions(+)
> > >
> > > diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> > > index cfd9deb..fd85b51 100644
> > > --- a/arch/arm64/mm/mmu.c
> > > +++ b/arch/arm64/mm/mmu.c
> > > @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> > > if (ret)
> > > __remove_pgd_mapping(swapper_pg_dir,
> > > __phys_to_virt(start), size);
> > > + else {
> > > + max_pfn = PFN_UP(start + size);
> > > + max_low_pfn = max_pfn;
> > > + }
> >
> > We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
> > updating as well?
> >
> > Do we have sufficient locking to ensure nobody is looking at max_pfn or
> > max_low_pfn while we update them?
>
> Only the write side is protected by memory hotplug locking. The read side is
> lockless -- just like all of the other pfn_to_online_page() machinery.

Hmm. So the readers can see one of the variables updated but the other one
stale?

Will

2021-09-29 11:22:29

by Will Deacon

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On Wed, Sep 29, 2021 at 12:49:58PM +0200, David Hildenbrand wrote:
> On 29.09.21 12:42, Will Deacon wrote:
> > On Wed, Sep 29, 2021 at 12:29:32PM +0200, David Hildenbrand wrote:
> > > On 29.09.21 12:10, Will Deacon wrote:
> > > > On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
> > > > > From: Sudarshan Rajagopalan <[email protected]>
> > > > >
> > > > > After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> > > > > needs updating to reflect on new PFNs being hot added to system.
> > > > >
> > > > > Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> > > > > Signed-off-by: Chris Goldsworthy <[email protected]>
> > > > > ---
> > > > > arch/arm64/mm/mmu.c | 5 +++++
> > > > > 1 file changed, 5 insertions(+)
> > > > >
> > > > > diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> > > > > index cfd9deb..fd85b51 100644
> > > > > --- a/arch/arm64/mm/mmu.c
> > > > > +++ b/arch/arm64/mm/mmu.c
> > > > > @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> > > > > if (ret)
> > > > > __remove_pgd_mapping(swapper_pg_dir,
> > > > > __phys_to_virt(start), size);
> > > > > + else {
> > > > > + max_pfn = PFN_UP(start + size);
> > > > > + max_low_pfn = max_pfn;
> > > > > + }
> > > >
> > > > We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
> > > > updating as well?
> > > >
> > > > Do we have sufficient locking to ensure nobody is looking at max_pfn or
> > > > max_low_pfn while we update them?
> > >
> > > Only the write side is protected by memory hotplug locking. The read side is
> > > lockless -- just like all of the other pfn_to_online_page() machinery.
> >
> > Hmm. So the readers can see one of the variables updated but the other one
> > stale?
>
> Yes, just like it has been on x86-64 for a long time:
>
> arch/x86/mm/init_64.c:update_end_of_memory_vars()
>
> Not sure if anyone really cares about slightly delayed updates while memory
> is getting hotplugged. The users that I am aware of don't care.

Thanks, I'd missed that x86 also updates max_low_pfn. So at least we're not
worse off in that respect.

Looking at set_max_mapnr(), I'm wondering why we need to call that at all
on arm64 as 'max_mapnr' only seems to be used for nommu.

Will

2021-09-29 12:11:52

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 29.09.21 13:03, Will Deacon wrote:
> On Wed, Sep 29, 2021 at 12:49:58PM +0200, David Hildenbrand wrote:
>> On 29.09.21 12:42, Will Deacon wrote:
>>> On Wed, Sep 29, 2021 at 12:29:32PM +0200, David Hildenbrand wrote:
>>>> On 29.09.21 12:10, Will Deacon wrote:
>>>>> On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
>>>>>> From: Sudarshan Rajagopalan <[email protected]>
>>>>>>
>>>>>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>>>>>> needs updating to reflect on new PFNs being hot added to system.
>>>>>>
>>>>>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>>>>>> Signed-off-by: Chris Goldsworthy <[email protected]>
>>>>>> ---
>>>>>> arch/arm64/mm/mmu.c | 5 +++++
>>>>>> 1 file changed, 5 insertions(+)
>>>>>>
>>>>>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>>>>>> index cfd9deb..fd85b51 100644
>>>>>> --- a/arch/arm64/mm/mmu.c
>>>>>> +++ b/arch/arm64/mm/mmu.c
>>>>>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>>>>> if (ret)
>>>>>> __remove_pgd_mapping(swapper_pg_dir,
>>>>>> __phys_to_virt(start), size);
>>>>>> + else {
>>>>>> + max_pfn = PFN_UP(start + size);
>>>>>> + max_low_pfn = max_pfn;
>>>>>> + }
>>>>>
>>>>> We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
>>>>> updating as well?
>>>>>
>>>>> Do we have sufficient locking to ensure nobody is looking at max_pfn or
>>>>> max_low_pfn while we update them?
>>>>
>>>> Only the write side is protected by memory hotplug locking. The read side is
>>>> lockless -- just like all of the other pfn_to_online_page() machinery.
>>>
>>> Hmm. So the readers can see one of the variables updated but the other one
>>> stale?
>>
>> Yes, just like it has been on x86-64 for a long time:
>>
>> arch/x86/mm/init_64.c:update_end_of_memory_vars()
>>
>> Not sure if anyone really cares about slightly delayed updates while memory
>> is getting hotplugged. The users that I am aware of don't care.
>
> Thanks, I'd missed that x86 also updates max_low_pfn. So at least we're not
> worse off in that respect.
>
> Looking at set_max_mapnr(), I'm wondering why we need to call that at all
> on arm64 as 'max_mapnr' only seems to be used for nommu.

I think max_mapnr is only helpful without SPARSE, I can spot the most
prominent consumer being simplistic pfn_valid() implementation.

MEMORY_HOTPLUG on arm64 implies SPARSE. ... and I recall that FLATMEM is
no longer possible on arm64. So most probably the arm64 call of
set_max_mapnr() can just be dropped.

--
Thanks,

David / dhildenb

2021-09-29 12:55:38

by Will Deacon

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On Wed, Sep 29, 2021 at 02:09:35PM +0200, David Hildenbrand wrote:
> On 29.09.21 13:03, Will Deacon wrote:
> > On Wed, Sep 29, 2021 at 12:49:58PM +0200, David Hildenbrand wrote:
> > > On 29.09.21 12:42, Will Deacon wrote:
> > > > On Wed, Sep 29, 2021 at 12:29:32PM +0200, David Hildenbrand wrote:
> > > > > On 29.09.21 12:10, Will Deacon wrote:
> > > > > > On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
> > > > > > > From: Sudarshan Rajagopalan <[email protected]>
> > > > > > >
> > > > > > > After new memory blocks have been hotplugged, max_pfn and max_low_pfn
> > > > > > > needs updating to reflect on new PFNs being hot added to system.
> > > > > > >
> > > > > > > Signed-off-by: Sudarshan Rajagopalan <[email protected]>
> > > > > > > Signed-off-by: Chris Goldsworthy <[email protected]>
> > > > > > > ---
> > > > > > > arch/arm64/mm/mmu.c | 5 +++++
> > > > > > > 1 file changed, 5 insertions(+)
> > > > > > >
> > > > > > > diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> > > > > > > index cfd9deb..fd85b51 100644
> > > > > > > --- a/arch/arm64/mm/mmu.c
> > > > > > > +++ b/arch/arm64/mm/mmu.c
> > > > > > > @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
> > > > > > > if (ret)
> > > > > > > __remove_pgd_mapping(swapper_pg_dir,
> > > > > > > __phys_to_virt(start), size);
> > > > > > > + else {
> > > > > > > + max_pfn = PFN_UP(start + size);
> > > > > > > + max_low_pfn = max_pfn;
> > > > > > > + }
> > > > > >
> > > > > > We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
> > > > > > updating as well?
> > > > > >
> > > > > > Do we have sufficient locking to ensure nobody is looking at max_pfn or
> > > > > > max_low_pfn while we update them?
> > > > >
> > > > > Only the write side is protected by memory hotplug locking. The read side is
> > > > > lockless -- just like all of the other pfn_to_online_page() machinery.
> > > >
> > > > Hmm. So the readers can see one of the variables updated but the other one
> > > > stale?
> > >
> > > Yes, just like it has been on x86-64 for a long time:
> > >
> > > arch/x86/mm/init_64.c:update_end_of_memory_vars()
> > >
> > > Not sure if anyone really cares about slightly delayed updates while memory
> > > is getting hotplugged. The users that I am aware of don't care.
> >
> > Thanks, I'd missed that x86 also updates max_low_pfn. So at least we're not
> > worse off in that respect.
> >
> > Looking at set_max_mapnr(), I'm wondering why we need to call that at all
> > on arm64 as 'max_mapnr' only seems to be used for nommu.
>
> I think max_mapnr is only helpful without SPARSE, I can spot the most
> prominent consumer being simplistic pfn_valid() implementation.

Yeah, and that's only used #ifndef CONFIG_MMU (there's a #error otherwise at
the top of the file).

> MEMORY_HOTPLUG on arm64 implies SPARSE. ... and I recall that FLATMEM is no
> longer possible on arm64. So most probably the arm64 call of set_max_mapnr()
> can just be dropped.

I'll do that and see if anything catches fire.

Will

2021-09-29 14:01:27

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] arm64: mm: update max_pfn after memory hotplug

On 29.09.21 12:42, Will Deacon wrote:
> On Wed, Sep 29, 2021 at 12:29:32PM +0200, David Hildenbrand wrote:
>> On 29.09.21 12:10, Will Deacon wrote:
>>> On Thu, Sep 23, 2021 at 03:54:48PM -0700, Chris Goldsworthy wrote:
>>>> From: Sudarshan Rajagopalan <[email protected]>
>>>>
>>>> After new memory blocks have been hotplugged, max_pfn and max_low_pfn
>>>> needs updating to reflect on new PFNs being hot added to system.
>>>>
>>>> Signed-off-by: Sudarshan Rajagopalan <[email protected]>
>>>> Signed-off-by: Chris Goldsworthy <[email protected]>
>>>> ---
>>>> arch/arm64/mm/mmu.c | 5 +++++
>>>> 1 file changed, 5 insertions(+)
>>>>
>>>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>>>> index cfd9deb..fd85b51 100644
>>>> --- a/arch/arm64/mm/mmu.c
>>>> +++ b/arch/arm64/mm/mmu.c
>>>> @@ -1499,6 +1499,11 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>>> if (ret)
>>>> __remove_pgd_mapping(swapper_pg_dir,
>>>> __phys_to_virt(start), size);
>>>> + else {
>>>> + max_pfn = PFN_UP(start + size);
>>>> + max_low_pfn = max_pfn;
>>>> + }
>>>
>>> We use 'max_pfn' as part of the argument to set_max_mapnr(). Does that need
>>> updating as well?
>>>
>>> Do we have sufficient locking to ensure nobody is looking at max_pfn or
>>> max_low_pfn while we update them?
>>
>> Only the write side is protected by memory hotplug locking. The read side is
>> lockless -- just like all of the other pfn_to_online_page() machinery.
>
> Hmm. So the readers can see one of the variables updated but the other one
> stale?

Yes, just like it has been on x86-64 for a long time:

arch/x86/mm/init_64.c:update_end_of_memory_vars()

Not sure if anyone really cares about slightly delayed updates while
memory is getting hotplugged. The users that I am aware of don't care.

--
Thanks,

David / dhildenb