mm: Don't mark hotplugged pages PG_reserved (including ZONE_DEVICE)
Subsystems (14)
cover mm: Don't mark hotplugged pages PG_reserved (including ZONE_DEVICE)
This is the result of a recent discussion with Michal ([1], [2]). Right now we set all pages PG_reserved when initializing hotplugged memmaps. This includes ZONE_DEVICE memory. In case of system memory, PG_reserved is cleared again when onlining the memory, in case of ZONE_DEVICE memory never.
In ancient times, we needed PG_reserved, because there was no way to tell whether the memmap was already properly initialized. We now have SECTION_IS_ONLINE for that in the case of !ZONE_DEVICE memory. ZONE_DEVICE memory is already initialized deferred, and there shouldn't be a visible change in that regard.
One of the biggest fears were side effects. I went ahead and audited all users of PageReserved(). The details can be found in "mm/memory_hotplug:
Don't mark pages PG_reserved when initializing the memmap".
This patch set adapts all relevant users of PageReserved() to keep the existing behavior in respect to ZONE_DEVICE pages. The biggest part part that needs changes is KVM, to keep the existing behavior (that's all I care about in this series).
Note that this series is able to rely completely on pfn_to_online_page(). No new is_zone_device_page() calles are introduced (as requested by Dan). We are currently discussing a way to mark also ZONE_DEVICE memmaps as active/initialized - pfn_active() - and lightweight locking to make sure memmaps remain active (e.g., using RCU). We might later be able to convert some suers of pfn_to_online_page() to pfn_active(). Details can be found in [3], however, this represents yet another cleanup/fix we'll perform on top of this cleanup.
I only gave it a quick test with DIMMs on x86-64, but didn't test the ZONE_DEVICE part at all (any tips for a nice QEMU setup?). Also, I didn't test the KVM parts (especially with ZONE_DEVICE pages or no memmap at all). Compile-tested on x86-64 and PPC.
Based on next/master. The current version (kept updated) can be found at:
https://github.com/davidhildenbrand/linux.git online_reserved_cleanup
RFC -> v1:
- Dropped "staging/gasket: Prepare gasket_release_page() for PG_reserved changes"
- Dropped "staging: kpc2000: Prepare transfer_complete_cb() for PG_reserved changes"
- Converted "mm/usercopy.c: Prepare check_page_span() for PG_reserved changes" to "mm/usercopy.c: Update comment in check_page_span() regarding ZONE_DEVICE"
- No new users of is_zone_device_page() are introduced.
- Rephrased comments and patch descriptions.
[1] https://lkml.org/lkml/2019/10/21/736
[2] https://lkml.org/lkml/2019/10/21/1034
[3] https://www.spinics.net/lists/linux-mm/msg194112.html
David Hildenbrand (10):
mm/memory_hotplug: Don't allow to online/offline memory blocks with
holes
KVM: x86/mmu: Prepare kvm_is_mmio_pfn() for PG_reserved changes
KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes powerpc/book3s: Prepare kvmppc_book3s_instantiate_page() for
PG_reserved changes
powerpc/64s: Prepare hash_page_do_lazy_icache() for PG_reserved
changes
powerpc/mm: Prepare maybe_pte_to_page() for PG_reserved changes x86/mm: Prepare __ioremap_check_ram() for PG_reserved changes mm/memory_hotplug: Don't mark pages PG_reserved when initializing the
memmap
mm/usercopy.c: Update comment in check_page_span() regarding
ZONE_DEVICE
arch/powerpc/kvm/book3s_64_mmu_radix.c | 14 +++++---- arch/powerpc/mm/book3s64/hash_utils.c | 10 +++--- arch/powerpc/mm/pgtable.c | 10 +++--- arch/x86/kvm/mmu.c | 29 ++++++++++------- arch/x86/mm/ioremap.c | 13 ++++++-- drivers/hv/hv_balloon.c | 6 ++++ drivers/vfio/vfio_iommu_type1.c | 10 ++++-- drivers/xen/balloon.c | 7 +++++ include/linux/page-flags.h | 8 +---- mm/memory_hotplug.c | 43 +++++++++++++++++++------- mm/page_alloc.c | 11 ------- mm/usercopy.c | 6 ++-- virt/kvm/kvm_main.c | 10 ++++-- 13 files changed, 111 insertions(+), 66 deletions(-)
Signature · 7 lines
2.21.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/10 mm/memory_hotplug: Don't allow to online/offline memory blocks with holes
Our onlining/offlining code is unnecessarily complicated. Only memory blocks added during boot can have holes (a range that is not IORESOURCE_SYSTEM_RAM). Hotplugged memory never has holes (e.g., see add_memory_resource()). All boot memory is alread online.
Therefore, when we stop allowing to offline memory blocks with holes, we implicitly no longer have to deal with onlining memory blocks with holes.
This allows to simplify the code. For example, we no longer have to worry about marking pages that fall into memory holes PG_reserved when onlining memory. We can stop setting pages PG_reserved.
Offlining memory blocks added during boot is usually not guranteed to work either way (unmovable data might have easily ended up on that memory during boot). So stopping to do that should not really hurt (+ people are not even aware of a setup where that used to work and that the existing code still works correctly with memory holes). For the use case of offlining memory to unplug DIMMs, we should see no change. (holes on DIMMs would be weird).
Please note that hardware errors (PG_hwpoison) are not memory holes and not affected by this change when offlining.
--- mm/memory_hotplug.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
mm/memory_hotplug.c +24 -2
@@ -1447,10 +1447,19 @@ static void node_states_clear_node(int node, struct memory_notify *arg)node_clear_state (node,N_MEMORY ); } +static int count_system_ram_pages_cb (unsigned long start_pfn, +unsigned long nr_pages,void *data) +{ +unsigned long *nr_system_ram_pages = data; + + *nr_system_ram_pages += nr_pages; +return 0 ; +} +static int __ref __offline_pages (unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn, nr_pages; +unsigned long pfn, nr_pages =0 ;unsigned long offlined_pages =0 ;int ret, node, nr_isolate_pageblock;unsigned long flags;
@@ -1461,6 +1470,20 @@ static int __ref __offline_pages(unsigned long start_pfn,mem_hotplug_begin (); +/* +* Don't allow to offline memory blocks that contain holes. +* Consecuently, memory blocks with holes can never get onlined +* (hotplugged memory has no holes and all boot memory is online). +* This allows to simplify the onlining/offlining code quite a lot. +*/ +walk_system_ram_range (start_pfn, end_pfn - start_pfn, &nr_pages, + count_system_ram_pages_cb); +if (nr_pages != end_pfn - start_pfn) { + ret = -EINVAL ; + reason ="memory holes" ; + goto failed_removal; + } +/* This makes hotplug much easier...and readable. we assume this for now. .*/ if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
@@ -1472,7 +1495,6 @@ static int __ref __offline_pages(unsigned long start_pfn, zone =page_zone (pfn_to_page (valid_start)); node =zone_to_nid (zone); - nr_pages = end_pfn - start_pfn;/* set above range as isolated */ ret = start_isolate_page_range(start_pfn, end_pfn,
Signature · 1 line
2.21.0
Re: [PATCH v1 01/10] mm/memory_hotplug: Don't allow to online/offline memory blocks with holes
Quoted 5 lines · David Hildenbrand
Our onlining/offlining code is unnecessarily complicated. Only memory blocks added during boot can have holes (a range that is not
IORESOURCE_SYSTEM_RAM). Hotplugged memory never has holes (e.g., seeadd_memory_resource()). All boot memory is alread online.
s/alread/already/
...also perhaps clarify "already online" by what point in time and why that is relevant. For example a description of the difference between the SetPageReserved() in the bootmem path and the one in the hotplug path.
Quoted 2 lines
Therefore, when we stop allowing to offline memory blocks with holes, we implicitly no longer have to deal with onlining memory blocks with holes.
Maybe an explicit reference of the code areas that deal with holes would help to back up that assertion. Certainly it would have saved me some time for the review.
Quoted 3 lines
This allows to simplify the code. For example, we no longer have to worry about marking pages that fall into memory holes
PG_reservedwhen onlining memory. We can stop setting pagesPG_reserved.
...but not for bootmem, right?
Quoted 2 lines
Offlining memory blocks added during boot is usually not guranteed to work
s/guranteed/guaranteed/
Quoted 3 lines
either way (unmovable data might have easily ended up on that memory during boot). So stopping to do that should not really hurt (+ people are not even aware of a setup where that used to work
Maybe put a "Link: https://lkml.kernel.org/r/$msg_id" to that discussion?
Quoted 4 lines
and that the existing code
still works correctly with memory holes). For the use case of offlining memory to unplug DIMMs, we should see no change. (holes on DIMMs would be weird).
However, less memory can be offlined than was theoretically allowed previously, so I don't understand the "we should see no change" comment. I still agree that's a price worth paying to get the code cleanups and if someone screams we can look at adding it back, but the fact that it was already fragile seems decent enough protection.
Quoted 41 lines
Please note that hardware errors (
PG_hwpoison) are not memory holes and not affected by this change when offlining.Cc Andrew Morton <[email protected]>Cc Michal Hocko <[email protected]>Cc Oscar Salvador <[email protected]>Cc Pavel Tatashin <[email protected]>Cc Dan Williams <[email protected]>Cc Anshuman Khandual <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- mm/memory_hotplug.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)mm/memory_hotplug.c +10
-1@@ -1447,10 +1447,19 @@ static void node_states_clear_node(int node, struct memory_notify *arg)node_clear_state (node,N_MEMORY ); } +static int count_system_ram_pages_cb (unsigned long start_pfn, +unsigned long nr_pages,void *data) +{ +unsigned long *nr_system_ram_pages = data; + + *nr_system_ram_pages += nr_pages; +return 0 ; +} +static int __ref __offline_pages (unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn, nr_pages; +unsigned long pfn, nr_pages =0 ;unsigned long offlined_pages =0 ;int ret, node, nr_isolate_pageblock;unsigned long flags;
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1475
Quoted 6 lines
mm/memory_hotplug.c +3
-0@@ -1461,6 +1470,20 @@ static int __ref __offline_pages(unsigned long start_pfn,mem_hotplug_begin (); +/* +* Don't allow to offline memory blocks that contain holes. +* Consecuently, memory blocks with holes can never get onlined
s/Consecuently/Consequently/
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1478
Quoted 3 lines
mm/memory_hotplug.c +3
-0+* (hotplugged memory has no holes and all boot memory is online). +* This allows to simplify the onlining/offlining code quite a lot. +*/
The last sentence of this comment makes sense in the context of this patch, but I don't think it stands by itself in the code base after the fact. The person reading the comment can't see the simplifications because the code is already gone. I'd clarify it to talk about why it is safe to not mess around with PG_Reserved in the hotplug path because of this check.
After those clarifications you can add:
Re: [PATCH v1 01/10] mm/memory_hotplug: Don't allow to online/offline memory blocks with holes
Quoted 9 lines · Dan Williams
Quoted 5 lines · David Hildenbrand
Our onlining/offlining code is unnecessarily complicated. Only memory blocks added during boot can have holes (a range that is not
IORESOURCE_SYSTEM_RAM). Hotplugged memory never has holes (e.g., seeadd_memory_resource()). All boot memory is alread online.s/alread/already/
Thanks.
Quoted 4 lines
...also perhaps clarify "already online" by what point in time and why that is relevant. For example a description of the difference between the
SetPageReserved()in the bootmem path and the one in the hotplug path.
Will add.
Quoted 7 lines
Quoted 3 lines
Therefore, when we stop allowing to offline memory blocks with holes, we implicitly no longer have to deal with onlining memory blocks with holes.
Maybe an explicit reference of the code areas that deal with holes would help to back up that assertion. Certainly it would have saved me some time for the review.
I can add a reference to the onlining code that will only online pages that don't fall into memory holes.
Quoted 6 lines
Quoted 4 lines
This allows to simplify the code. For example, we no longer have to worry about marking pages that fall into memory holes
PG_reservedwhen onlining memory. We can stop setting pagesPG_reserved....but not for bootmem, right?
Yes, bootmem is not changed. (especially, early allocations and memory holes are marked PG_reserved - basically everything not given to the buddy after boot)
Quoted 5 lines
Quoted 3 lines
Offlining memory blocks added during boot is usually not guranteed to work
s/guranteed/guaranteed/
Thanks.
Quoted 12 lines
Quoted 4 lines
either way (unmovable data might have easily ended up on that memory during boot). So stopping to do that should not really hurt (+ people are not even aware of a setup where that used to work
Maybe put a "Link: https://lkml.kernel.org/r/$msg_id" to that discussion?
Quoted 4 lines
and that the existing code
still works correctly with memory holes). For the use case of offlining memory to unplug DIMMs, we should see no change. (holes on DIMMs would be weird).
Quoted 5 lines
However, less memory can be offlined than was theoretically allowed previously, so I don't understand the "we should see no change" comment. I still agree that's a price worth paying to get the code cleanups and if someone screams we can look at adding it back, but the fact that it was already fragile seems decent enough protection.
Hotplugged DIMMs (add_memory()) have no holes and will therefore see no change.
Quoted 42 lines
Quoted 42 lines
Please note that hardware errors (
PG_hwpoison) are not memory holes and not affected by this change when offlining.Cc Andrew Morton <[email protected]>Cc Michal Hocko <[email protected]>Cc Oscar Salvador <[email protected]>Cc Pavel Tatashin <[email protected]>Cc Dan Williams <[email protected]>Cc Anshuman Khandual <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- mm/memory_hotplug.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)mm/memory_hotplug.c +10
-1@@ -1447,10 +1447,19 @@ static void node_states_clear_node(int node, struct memory_notify *arg)node_clear_state (node,N_MEMORY ); } +static int count_system_ram_pages_cb (unsigned long start_pfn, +unsigned long nr_pages,void *data) +{ +unsigned long *nr_system_ram_pages = data; + + *nr_system_ram_pages += nr_pages; +return 0 ; +} +static int __ref __offline_pages (unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn, nr_pages; +unsigned long pfn, nr_pages =0 ;unsigned long offlined_pages =0 ;int ret, node, nr_isolate_pageblock;unsigned long flags;@@ -1461,6 +1470,20 @@ static int __ref __offline_pages(unsigned long start_pfn,
Quoted 7 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1475
Quoted 5 lines
mm/memory_hotplug.c +3
-0mem_hotplug_begin (); +/* +* Don't allow to offline memory blocks that contain holes. +* Consecuently, memory blocks with holes can never get onlined s/Consecuently/Consequently/
Thanks.
Quoted 11 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1478
Quoted 4 lines
mm/memory_hotplug.c +3
-0+* (hotplugged memory has no holes and all boot memory is online). +* This allows to simplify the onlining/offlining code quite a lot. +*/ The last sentence of this comment makes sense in the context of this patch, but I don't think it stands by itself in the code base after the fact. The person reading the comment can't see the simplifications because the code is already gone. I'd clarify it to talk about why it is safe to not mess around with
PG_Reservedin the hotplug path because of this check.
I'll think of something. It's not just the PG_reserved handling but the whole pfn_valid()/walk_system_ram_range() handling that can be simplified.
Quoted 5 lines
After those clarifications you can add:
Reviewed-by Dan Williams <[email protected]>
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
PATCH 2/10 KVM: x86/mmu: Prepare kvm_is_mmio_pfn() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
KVM has this weird use case that you can map anything from /dev/mem into the guest. pfn_valid() is not a reliable check whether the memmap was initialized and can be touched. pfn_to_online_page() makes sure that we have an initialized memmap (and don't have ZONE_DEVICE memory).
Rewrite kvm_is_mmio_pfn() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- arch/x86/kvm/mmu.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)
arch/x86/kvm/mmu.c +17 -12
@@ -2962,20 +2962,25 @@ static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,static bool kvm_is_mmio_pfn (kvm_pfn_t pfn) { +struct page *page =pfn_to_online_page (pfn); + +/* +* ZONE_DEVICE pages are never online. Online pages that are reserved +* either indicate the zero page or MMIO pages. +*/ +if (page) +return !is_zero_pfn (pfn) &&PageReserved (pfn_to_page (pfn)); + +/* +* Anything with a valid (but not online) memmap could be ZONE_DEVICE. +* Treat only UC/UC-/WC pages as MMIO. +*/ if (pfn_valid (pfn)) -return !is_zero_pfn (pfn) &&PageReserved (pfn_to_page (pfn)) && -/* -* Some reserved pages, such as those from NVDIMM -* DAX devices, are not for MMIO, and can be mapped -* with cached memory type for better performance. -* However, the above check misconceives those pages -* as MMIO, and results in KVM mapping them with UC -* memory type, which would hurt the performance. -* Therefore, we check the host memory type in addition -* and only treat UC/UC-/WC pages as MMIO. -*/ - (!pat_enabled () ||pat_pfn_immune_to_uc_mtrr (pfn)); +return !pat_enabled () ||pat_pfn_immune_to_uc_mtrr (pfn); +/* +* Any RAM that has no memmap (e.g., mapped via /dev/mem) is not MMIO. +*/ return !e820__mapped_raw_any (pfn_to_hpa (pfn),pfn_to_hpa (pfn +1 ) -1 ,E820_TYPE_RAM );
Signature · 1 line
2.21.0
Re: [PATCH v1 02/10] KVM: x86/mmu: Prepare kvm_is_mmio_pfn() for PG_reserved changes
Quoted 41 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_mmio_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Wanpeng Li <[email protected]>Cc Jim Mattson <[email protected]>Cc Joerg Roedel <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc KarimAllah Ahmed <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- arch/x86/kvm/mmu.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)arch/x86/kvm/mmu.c +2
-0@@ -2962,20 +2962,25 @@ static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,static bool kvm_is_mmio_pfn (kvm_pfn_t pfn) { +struct page *page =pfn_to_online_page (pfn); +
arch/x86/kvm/mmu.c static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, · line 2976
Quoted 10 lines
arch/x86/kvm/mmu.c +10
-0+/* +* ZONE_DEVICE pages are never online. Online pages that are reserved +* either indicate the zero page or MMIO pages. +*/ +if (page) +return !is_zero_pfn (pfn) &&PageReserved (pfn_to_page (pfn)); + +/* +* Anything with a valid (but not online) memmap could be ZONE_DEVICE. +* Treat only UC/UC-/WC pages as MMIO.
You might clarify that ZONE_DEVICE pages that belong to WB cacheable pmem have the correct memtype established by devm_memremap_pages().
Other than that, feel free to add:
Re: [PATCH v1 02/10] KVM: x86/mmu: Prepare kvm_is_mmio_pfn() for PG_reserved changes
Quoted 47 lines · Dan Williams
Quoted 46 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_mmio_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Wanpeng Li <[email protected]>Cc Jim Mattson <[email protected]>Cc Joerg Roedel <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc KarimAllah Ahmed <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- arch/x86/kvm/mmu.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)arch/x86/kvm/mmu.c +7
-0@@ -2962,20 +2962,25 @@ static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,static bool kvm_is_mmio_pfn (kvm_pfn_t pfn) { +struct page *page =pfn_to_online_page (pfn); + +/* +* ZONE_DEVICE pages are never online. Online pages that are reserved +* either indicate the zero page or MMIO pages. +*/ +if (page)
Quoted 8 lines
arch/x86/kvm/mmu.c static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, · line 2976
Quoted 5 lines
arch/x86/kvm/mmu.c +5
-0+return !is_zero_pfn (pfn) &&PageReserved (pfn_to_page (pfn)); + +/* +* Anything with a valid (but not online) memmap could be ZONE_DEVICE. +* Treat only UC/UC-/WC pages as MMIO. You might clarify that
ZONE_DEVICEpages that belong to WB cacheable pmem have the correct memtype established bydevm_memremap_pages().
/*
* Anything with a valid (but not online) memmap could be ZONE_DEVICE.
* Treat only UC/UC-/WC pages as MMIO. devm_memremap_pages() established
* the correct memtype for WB cacheable ZONE_DEVICE pages. */
Thanks!
Quoted 5 lines
Other than that, feel free to add:
Reviewed-by Dan Williams <[email protected]>
Signature · 3 lines
Thanks, David / dhildenb
PATCH 3/10 KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
KVM has this weird use case that you can map anything from /dev/mem into the guest. pfn_valid() is not a reliable check whether the memmap was initialized and can be touched. pfn_to_online_page() makes sure that we have an initialized memmap (and don't have ZONE_DEVICE memory).
Rewrite kvm_is_reserved_pfn() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +8 -2
@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn); +/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }
Signature · 1 line
2.21.0
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 33 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Cc KarimAllah Ahmed <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)virt/kvm/kvm_main.c +1
-2@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);
virt/kvm/kvm_main.c __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 164
Quoted 10 lines
virt/kvm/kvm_main.c +7
-0+/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }
So after this all the pfn_valid() usage in kvm_main.c is replaced with pfn_to_online_page()? Looks correct to me.
However, I'm worried that kvm is taking reference on ZONE_DEVICE pages through some other path resulting in this:
https://lore.kernel.org/linux-nvdimm/[email protected]/
I'll see if this patch set modulates or maintains that failure mode.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 48 lines · Dan Williams
Quoted 33 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Cc KarimAllah Ahmed <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)virt/kvm/kvm_main.c +1
-2@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);virt/kvm/kvm_main.c __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 164
Quoted 10 lines
virt/kvm/kvm_main.c +7
-0+/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }So after this all the
pfn_valid()usage inkvm_main.cis replaced withpfn_to_online_page()? Looks correct to me.
Quoted 7 lines
However, I'm worried that kvm is taking reference on
ZONE_DEVICEpages through some other path resulting in this:https://lore.kernel.org/linux-nvdimm/[email protected]/I'll see if this patch set modulates or maintains that failure mode.
I'd assume that the behavior is unchanged. Ithink we get a reference to these ZONE_DEVICE pages via __get_user_pages_fast() and friends in hva_to_pfn_fast() and friends in virt/kvm/kvm_main.c
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 52 lines · David Hildenbrand
Quoted 51 lines · Dan Williams
Quoted 33 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Cc KarimAllah Ahmed <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)virt/kvm/kvm_main.c +1
-2@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);virt/kvm/kvm_main.c __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 164
Quoted 10 lines
virt/kvm/kvm_main.c +7
-0+/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }So after this all the
pfn_valid()usage inkvm_main.cis replaced withpfn_to_online_page()? Looks correct to me.However, I'm worried that kvm is taking reference on
ZONE_DEVICEpages through some other path resulting in this:
Quoted 9 lines
Quoted 4 lines
https://lore.kernel.org/linux-nvdimm/[email protected]/I'll see if this patch set modulates or maintains that failure mode.
I'd assume that the behavior is unchanged. Ithink we get a reference to these
ZONE_DEVICEpages via__get_user_pages_fast()and friends inhva_to_pfn_fast()and friends invirt/kvm/kvm_main.c
I think I know what's going wrong:
Pages that are pinned via gfn_to_pfn() and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...
E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);
void kvm_release_pfn_clean(kvm_pfn_t pfn)
{
if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
put_page(pfn_to_page(pfn));
}This function makes perfect sense as the counterpart for kvm_get_pfn():
void kvm_get_pfn(kvm_pfn_t pfn)
{
if (!kvm_is_reserved_pfn(pfn))
get_page(pfn_to_page(pfn));
}As all ZONE_DEVICE pages are currently reserved, pages pinned via gfn_to_pfn() and friends will often not see a put_page() AFAIKS.
Now, my patch does not change that, the result of kvm_is_reserved_pfn(pfn) will be unchanged. A proper fix for that would probably be
a) To drop the reference to ZONE_DEVICE pages in gfn_to_pfn() and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)
b) To not use kvm_release_pfn_clean() and friends on pages that were definitely pinned.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 101 lines · David Hildenbrand
Quoted 52 lines · David Hildenbrand
Quoted 51 lines · Dan Williams
Quoted 33 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Cc KarimAllah Ahmed <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)virt/kvm/kvm_main.c +1
-2@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);virt/kvm/kvm_main.c __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 164
Quoted 10 lines
virt/kvm/kvm_main.c +7
-0+/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }So after this all the
pfn_valid()usage inkvm_main.cis replaced withpfn_to_online_page()? Looks correct to me.However, I'm worried that kvm is taking reference on
ZONE_DEVICEpages through some other path resulting in this:Quoted 9 lines
Quoted 4 lines
https://lore.kernel.org/linux-nvdimm/[email protected]/I'll see if this patch set modulates or maintains that failure mode.
I'd assume that the behavior is unchanged. Ithink we get a reference to these
ZONE_DEVICEpages via__get_user_pages_fast()and friends inhva_to_pfn_fast()and friends invirt/kvm/kvm_main.cI think I know what's going wrong:
Pages that are pinned via
gfn_to_pfn()and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn =gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);void
kvm_release_pfn_clean(kvm_pfn_t pfn){ if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) put_page(pfn_to_page(pfn)); }This function makes perfect sense as the counterpart for
kvm_get_pfn():void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }As all
ZONE_DEVICEpages are currently reserved, pages pinned viagfn_to_pfn()and friends will often not see aput_page()AFAIKS.Now, my patch does not change that, the result of
kvm_is_reserved_pfn(pfn)will be unchanged. A proper fix for that would probably be
Quoted 7 lines
a) To drop the reference to
ZONE_DEVICEpages ingfn_to_pfn()and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)b) To not use
kvm_release_pfn_clean()and friends on pages that were definitely pinned.
(talking to myself, sorry)
Thinking again, dropping this patch from this series could effectively also fix that issue. E.g., kvm_release_pfn_clean() and friends would always do a put_page() if "pfn_valid() and !PageReserved()", so after patch 9 also on ZONDE_DEVICE pages.
But it would have side effects that might not be desired. E.g.,:
1. kvm_pfn_to_page() would also return ZONE_DEVICE pages (might even be the right thing to do).
2. kvm_set_pfn_dirty() would also set ZONE_DEVICE pages dirty (might be okay)
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 95 lines · David Hildenbrand
Quoted 94 lines · David Hildenbrand
Quoted 52 lines · David Hildenbrand
Quoted 51 lines · Dan Williams
Quoted 33 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
kvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Michal Hocko <[email protected]>Cc Dan Williams <[email protected]>Cc KarimAllah Ahmed <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- virt/kvm/kvm_main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)virt/kvm/kvm_main.c +1
-2@@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,bool kvm_is_reserved_pfn (kvm_pfn_t pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);virt/kvm/kvm_main.c __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 164
Quoted 10 lines
virt/kvm/kvm_main.c +7
-0+/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }So after this all the
pfn_valid()usage inkvm_main.cis replaced withpfn_to_online_page()? Looks correct to me.However, I'm worried that kvm is taking reference on
ZONE_DEVICEpages through some other path resulting in this:Quoted 9 lines
Quoted 4 lines
https://lore.kernel.org/linux-nvdimm/[email protected]/I'll see if this patch set modulates or maintains that failure mode.
I'd assume that the behavior is unchanged. Ithink we get a reference to these
ZONE_DEVICEpages via__get_user_pages_fast()and friends inhva_to_pfn_fast()and friends invirt/kvm/kvm_main.cI think I know what's going wrong:
Pages that are pinned via
gfn_to_pfn()and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn =gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);void
kvm_release_pfn_clean(kvm_pfn_t pfn){ if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) put_page(pfn_to_page(pfn)); }This function makes perfect sense as the counterpart for
kvm_get_pfn():void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }
Quoted 2 lines
Quoted 2 lines
As all
ZONE_DEVICEpages are currently reserved, pages pinned viagfn_to_pfn()and friends will often not see aput_page()AFAIKS.
Assuming gup() takes a reference for ZONE_DEVICE pages, yes, this is a KVM bug.
Quoted 10 lines
Quoted 10 lines
Now, my patch does not change that, the result of
kvm_is_reserved_pfn(pfn)will be unchanged. A proper fix for that would probably bea) To drop the reference to
ZONE_DEVICEpages ingfn_to_pfn()and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)b) To not use
kvm_release_pfn_clean()and friends on pages that were definitely pinned.
This is already KVM's intent, i.e. the purpose of the PageReserved() check is simply to avoid putting a non-existent reference. The problem is that KVM assumes pages with PG_reserved set are never pinned, which AFAICT was true when the code was first added.
Quoted 6 lines
(talking to myself, sorry)
Thinking again, dropping this patch from this series could effectively also fix that issue. E.g.,
kvm_release_pfn_clean()and friends would always do aput_page()if "pfn_valid()and !PageReserved()", so after patch 9 also onZONDE_DEVICEpages.
Yeah, this appears to be the correct fix.
Quoted 4 lines
But it would have side effects that might not be desired. E.g.,:
1.
kvm_pfn_to_page()would also returnZONE_DEVICEpages (might even be the right thing to do).
This should be ok, at least on x86. There are only three users of kvm_pfn_to_page(). Two of those are on allocations that are controlled by KVM and are guaranteed to be vanilla MAP_ANONYMOUS. The third is on guest memory when running a nested guest, and in that case supporting ZONE_DEVICE memory is desirable, i.e. KVM should play nice with a guest that is backed by ZONE_DEVICE memory.
Quoted 2 lines
2.
kvm_set_pfn_dirty()would also setZONE_DEVICEpages dirty (might be okay)
This is ok from a KVM perspective.
The scarier code (for me) is transparent_hugepage_adjust() and kvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and _PAGE_DEVMAP.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
I think I know what's going wrong:
Pages that are pinned via
gfn_to_pfn()and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn =gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);void
kvm_release_pfn_clean(kvm_pfn_t pfn){ if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) put_page(pfn_to_page(pfn)); }This function makes perfect sense as the counterpart for
kvm_get_pfn():void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
As all
ZONE_DEVICEpages are currently reserved, pages pinned viagfn_to_pfn()and friends will often not see aput_page()AFAIKS.Assuming
gup()takes a reference forZONE_DEVICEpages, yes, this is a KVM bug.
Yes, it does take a reference AFAIKs. E.g.,
mm/gup.c:gup_pte_range():
...
if (pte_devmap(pte)) {
if (unlikely(flags & FOLL_LONGTERM))
goto pte_unmap;
pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
if (unlikely(!pgmap)) {
undo_dev_pagemap(nr, nr_start, pages);
goto pte_unmap;
}
} else if (pte_special(pte))
goto pte_unmap;
VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
page = pte_page(pte);
head = try_get_compound_head(page, 1);try_get_compound_head() will increment the reference count.
Quoted 38 lines
Quoted 11 lines
Quoted 11 lines
Now, my patch does not change that, the result of
kvm_is_reserved_pfn(pfn)will be unchanged. A proper fix for that would probably bea) To drop the reference to
ZONE_DEVICEpages ingfn_to_pfn()and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)b) To not use
kvm_release_pfn_clean()and friends on pages that were definitely pinned.This is already KVM's intent, i.e. the purpose of the
PageReserved()check is simply to avoid putting a non-existent reference. The problem is that KVM assumes pages withPG_reservedset are never pinned, which AFAICT was true when the code was first added.Quoted 6 lines
(talking to myself, sorry)
Thinking again, dropping this patch from this series could effectively also fix that issue. E.g.,
kvm_release_pfn_clean()and friends would always do aput_page()if "pfn_valid()and !PageReserved()", so after patch 9 also onZONDE_DEVICEpages.Yeah, this appears to be the correct fix.
Quoted 4 lines
But it would have side effects that might not be desired. E.g.,:
1.
kvm_pfn_to_page()would also returnZONE_DEVICEpages (might even be the right thing to do).This should be ok, at least on x86. There are only three users of
kvm_pfn_to_page(). Two of those are on allocations that are controlled by KVM and are guaranteed to be vanillaMAP_ANONYMOUS. The third is on guest memory when running a nested guest, and in that case supportingZONE_DEVICEmemory is desirable, i.e. KVM should play nice with a guest that is backed byZONE_DEVICEmemory.
Quoted 4 lines
Quoted 2 lines
2.
kvm_set_pfn_dirty()would also setZONE_DEVICEpages dirty (might be okay)This is ok from a KVM perspective.
What about
void kvm_get_pfn(kvm_pfn_t pfn)
{
if (!kvm_is_reserved_pfn(pfn))
get_page(pfn_to_page(pfn));
}Is a pure get_page() sufficient in case of ZONE_DEVICE?
(asking because of the live references obtained via get_dev_pagemap(pte_pfn(pte), pgmap) in mm/gup.c:gup_pte_range() somewhat confuse me :) )
Quoted 4 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.
The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the PG_reserved thingy AFAIKs.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 58 lines · David Hildenbrand
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
I think I know what's going wrong:
Pages that are pinned via
gfn_to_pfn()and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn =gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);void
kvm_release_pfn_clean(kvm_pfn_t pfn){ if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) put_page(pfn_to_page(pfn)); }This function makes perfect sense as the counterpart for
kvm_get_pfn():void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
As all
ZONE_DEVICEpages are currently reserved, pages pinned viagfn_to_pfn()and friends will often not see aput_page()AFAIKS.Assuming
gup()takes a reference forZONE_DEVICEpages, yes, this is a KVM bug.Yes, it does take a reference AFAIKs. E.g.,
mm/gup.c:gup_pte_range():
...if (pte_devmap(pte)) { if (unlikely(flags & FOLL_LONGTERM)) goto pte_unmap; pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); if (unlikely(!pgmap)) { undo_dev_pagemap(nr, nr_start, pages); goto pte_unmap; } } else if (pte_special(pte)) goto pte_unmap; VM_BUG_ON(!pfn_valid(pte_pfn(pte))); page = pte_page(pte); head = try_get_compound_head(page, 1);
Quoted 1 line
try_get_compound_head()will increment the reference count.
Doh, I looked right at that code and somehow didn't connect the dots. Thanks!
Quoted 50 lines
Quoted 37 lines
Quoted 10 lines
Quoted 10 lines
Now, my patch does not change that, the result of
kvm_is_reserved_pfn(pfn)will be unchanged. A proper fix for that would probably bea) To drop the reference to
ZONE_DEVICEpages ingfn_to_pfn()and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)b) To not use
kvm_release_pfn_clean()and friends on pages that were definitely pinned.This is already KVM's intent, i.e. the purpose of the
PageReserved()check is simply to avoid putting a non-existent reference. The problem is that KVM assumes pages withPG_reservedset are never pinned, which AFAICT was true when the code was first added.Quoted 6 lines
(talking to myself, sorry)
Thinking again, dropping this patch from this series could effectively also fix that issue. E.g.,
kvm_release_pfn_clean()and friends would always do aput_page()if "pfn_valid()and !PageReserved()", so after patch 9 also onZONDE_DEVICEpages.Yeah, this appears to be the correct fix.
Quoted 4 lines
But it would have side effects that might not be desired. E.g.,:
1.
kvm_pfn_to_page()would also returnZONE_DEVICEpages (might even be the right thing to do).This should be ok, at least on x86. There are only three users of
kvm_pfn_to_page(). Two of those are on allocations that are controlled by KVM and are guaranteed to be vanillaMAP_ANONYMOUS. The third is on guest memory when running a nested guest, and in that case supportingZONE_DEVICEmemory is desirable, i.e. KVM should play nice with a guest that is backed byZONE_DEVICEmemory.Quoted 4 lines
Quoted 2 lines
2.
kvm_set_pfn_dirty()would also setZONE_DEVICEpages dirty (might be okay)This is ok from a KVM perspective.
What about
void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }
Quoted 4 lines
Is a pure
get_page()sufficient in case ofZONE_DEVICE? (asking because of the live references obtained viaget_dev_pagemap(pte_pfn(pte), pgmap)in mm/gup.c:gup_pte_range() somewhat confuse me :) )
This ties into my concern with thp_adjust(). On x86, kvm_get_pfn() is only used in two flows, to manually get a ref for VM_IO/VM_PFNMAP pages and to switch the ref when mapping a non-hugetlbfs compound page, i.e. a THP.
I assume VM_IO and PFNMAP can't apply to ZONE_DEVICE pages.
In the thp_adjust() case, when a THP is encountered and the original PFN is for a non-PG_head page, KVM transfers the reference to the associated PG_head page[*] and maps the associated 2mb chunk/page. This is where KVM uses kvm_get_pfn() and could run afoul of the get_dev_pagemap() refcounts.
[*] Technically I don't think it's guaranteed to be a PG_head, e.g. if the
THP is a 1gb page, as KVM currently only maps THP as 2mb pages. But the idea is the same, transfer the refcount the PFN that's actually going into KVM's page tables.
Quoted 8 lines
Quoted 4 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
In reply to David HildenbrandQuoted 112 lines · David Hildenbrand
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
I think I know what's going wrong:
Pages that are pinned via
gfn_to_pfn()and friends take a references, however are often released via kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()...E.g., in arch/x86/kvm/x86.c:reexecute_instruction()
...
pfn =gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
...kvm_release_pfn_clean(pfn);void
kvm_release_pfn_clean(kvm_pfn_t pfn){ if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) put_page(pfn_to_page(pfn)); }This function makes perfect sense as the counterpart for
kvm_get_pfn():void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
As all
ZONE_DEVICEpages are currently reserved, pages pinned viagfn_to_pfn()and friends will often not see aput_page()AFAIKS.Assuming
gup()takes a reference forZONE_DEVICEpages, yes, this is a KVM bug.Yes, it does take a reference AFAIKs. E.g.,
mm/gup.c:gup_pte_range():
...if (pte_devmap(pte)) { if (unlikely(flags & FOLL_LONGTERM)) goto pte_unmap; pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); if (unlikely(!pgmap)) { undo_dev_pagemap(nr, nr_start, pages); goto pte_unmap; } } else if (pte_special(pte)) goto pte_unmap; VM_BUG_ON(!pfn_valid(pte_pfn(pte))); page = pte_page(pte); head = try_get_compound_head(page, 1);
try_get_compound_head()will increment the reference count.Quoted 38 lines
Quoted 11 lines
Quoted 11 lines
Now, my patch does not change that, the result of
kvm_is_reserved_pfn(pfn)will be unchanged. A proper fix for that would probably bea) To drop the reference to
ZONE_DEVICEpages ingfn_to_pfn()and friends, after you successfully pinned the pages. (not sure if that's the right thing to do but you're the expert)b) To not use
kvm_release_pfn_clean()and friends on pages that were definitely pinned.This is already KVM's intent, i.e. the purpose of the
PageReserved()check is simply to avoid putting a non-existent reference. The problem is that KVM assumes pages withPG_reservedset are never pinned, which AFAICT was true when the code was first added.Quoted 6 lines
(talking to myself, sorry)
Thinking again, dropping this patch from this series could effectively also fix that issue. E.g.,
kvm_release_pfn_clean()and friends would always do aput_page()if "pfn_valid()and !PageReserved()", so after patch 9 also onZONDE_DEVICEpages.Yeah, this appears to be the correct fix.
Quoted 4 lines
But it would have side effects that might not be desired. E.g.,:
1.
kvm_pfn_to_page()would also returnZONE_DEVICEpages (might even be the right thing to do).This should be ok, at least on x86. There are only three users of
kvm_pfn_to_page(). Two of those are on allocations that are controlled by KVM and are guaranteed to be vanillaMAP_ANONYMOUS. The third is on guest memory when running a nested guest, and in that case supportingZONE_DEVICEmemory is desirable, i.e. KVM should play nice with a guest that is backed byZONE_DEVICEmemory.Quoted 4 lines
Quoted 2 lines
2.
kvm_set_pfn_dirty()would also setZONE_DEVICEpages dirty (might be okay)This is ok from a KVM perspective.
What about
void
kvm_get_pfn(kvm_pfn_t pfn){ if (!kvm_is_reserved_pfn(pfn)) get_page(pfn_to_page(pfn)); }
Quoted 4 lines
Is a pure
get_page()sufficient in case ofZONE_DEVICE? (asking because of the live references obtained viaget_dev_pagemap(pte_pfn(pte), pgmap)in mm/gup.c:gup_pte_range() somewhat confuse me :) )
It is not sufficient. PTE_DEVMAP is there to tell the gup path "be careful, this pfn has device-lifetime, make sure the device is pinned and not actively in the process of dying before pinning any pages associated with this device".
However, if kvm_get_pfn() is honoring kvm_is_reserved_pfn() that returns true for ZONE_DEVICE, I'm missing how it is messing up the reference counts.
Quoted 7 lines
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.
Both transparent_hugepage_adjust() and kvm_mmu_zap_collapsible_spte() are honoring kvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 5 lines · Dan Williams
Quoted 4 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.
Quoted 7 lines
Quoted 3 lines
The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.
When mapping pages into the guest, KVM gets the page via gup(), which increments the page count for ZONE_DEVICE pages. But KVM puts the page using kvm_release_pfn_clean(), which skips put_page() if PageReserved() and so never puts its reference to ZONE_DEVICE pages.
My transparent_hugepage_adjust() and kvm_mmu_zap_collapsible_spte() comments were for a post-patch/series scenario wheren PageReserved() is no longer true for ZONE_DEVICE pages.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 11 lines · Sean Christopherson
Quoted 9 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.
Quoted 8 lines
Quoted 3 lines
Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.
Oh, yeah, that's busted.
Quoted 3 lines
My
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()comments were for a post-patch/series scenario wherenPageReserved()is no longer true forZONE_DEVICEpages.
Ah, ok, for that David is preserving kvm_is_reserved_pfn() returning true for ZONE_DEVICE because pfn_to_online_page() will fail for ZONE_DEVICE.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 24 lines · Dan Williams
Quoted 11 lines · Sean Christopherson
Quoted 9 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Quoted 8 lines
Quoted 3 lines
Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.Oh, yeah, that's busted.
Quoted 7 lines
Quoted 3 lines
My
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()comments were for a post-patch/series scenario wherenPageReserved()is no longer true forZONE_DEVICEpages.Ah, ok, for that David is preserving
kvm_is_reserved_pfn()returning true forZONE_DEVICEbecausepfn_to_online_page()will fail forZONE_DEVICE.
But David's proposed fix for the above refcount bug is to omit the patch so that KVM no longer treats ZONE_DEVICE pages as reserved. That seems like the right thing to do, including for thp_adjust(), e.g. it would naturally let KVM use 2mb pages for the guest when a ZONE_DEVICE page is mapped with a huge page (2mb or above) in the host. The only hiccup is figuring out how to correctly transfer the reference.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
In reply to Dan WilliamsQuoted 18 lines · Dan Williams
Quoted 15 lines · Sean Christopherson
Quoted 12 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.
Quoted 6 lines
Quoted 4 lines
When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.Oh, yeah, that's busted.
Ugh, it's extra busted because every other gup user in the kernel tracks the pages resulting from gup and puts them (put_page()) when they are done. KVM wants to forget about whether it did a gup to get the page and optionally trigger put_page() based purely on the pfn. Outside of VFIO device assignment that needs pages pinned for DMA, why does KVM itself need to pin pages? If pages are pinned over a return to userspace that needs to be a FOLL_LONGTERM gup.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 26 lines · Dan Williams
Quoted 18 lines · Dan Williams
Quoted 15 lines · Sean Christopherson
Quoted 12 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.Quoted 6 lines
Quoted 4 lines
When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.Oh, yeah, that's busted.
Quoted 7 lines
Ugh, it's extra busted because every other gup user in the kernel tracks the pages resulting from gup and puts them (
put_page()) when they are done. KVM wants to forget about whether it did a gup to get the page and optionally triggerput_page()based purely on the pfn. Outside of VFIO device assignment that needs pages pinned for DMA, why does KVM itself need to pin pages? If pages are pinned over a return to userspace that needs to be aFOLL_LONGTERMgup.
Short answer, KVM pins the page to ensure correctness with respect to the primary MMU invalidating the associated host virtual address, e.g. when the page is being migrated or unmapped from host userspace.
The main use of gup() is to handle guest page faults and map pages into the guest, i.e. into KVM's secondary MMU. KVM uses gup() to both get the PFN and to temporarily pin the page. The pin is held just long enough to guaranteed that any invalidation via the mmu_notifier will be stalled until after KVM finishes installing the page into the secondary MMU, i.e. the pin is short-term and not held across a return to userspace or entry into the guest. When a subsequent mmu_notifier invalidation occurs, KVM pulls the PFN from the secondary MMU and uses that to update accessed and dirty bits in the host.
There are a few other KVM flows that eventually call into gup(), but those are "traditional" short-term pins and use put_page() directly.
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 50 lines · Sean Christopherson
Quoted 26 lines · Dan Williams
Quoted 18 lines · Dan Williams
Quoted 15 lines · Sean Christopherson
Quoted 12 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.Quoted 6 lines
Quoted 4 lines
When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.Oh, yeah, that's busted.
Quoted 7 lines
Ugh, it's extra busted because every other gup user in the kernel tracks the pages resulting from gup and puts them (
put_page()) when they are done. KVM wants to forget about whether it did a gup to get the page and optionally triggerput_page()based purely on the pfn. Outside of VFIO device assignment that needs pages pinned for DMA, why does KVM itself need to pin pages? If pages are pinned over a return to userspace that needs to be aFOLL_LONGTERMgup.Short answer, KVM pins the page to ensure correctness with respect to the primary MMU invalidating the associated host virtual address, e.g. when the page is being migrated or unmapped from host userspace.
The main use of
gup()is to handle guest page faults and map pages into the guest, i.e. into KVM's secondary MMU. KVM usesgup()to both get the PFN and to temporarily pin the page. The pin is held just long enough to guaranteed that any invalidation via themmu_notifierwill be stalled until after KVM finishes installing the page into the secondary MMU, i.e. the pin is short-term and not held across a return to userspace or entry into the guest. When a subsequentmmu_notifierinvalidation occurs, KVM pulls the PFN from the secondary MMU and uses that to update accessed and dirty bits in the host.
Quoted 2 lines
There are a few other KVM flows that eventually call into
gup(), but those are "traditional" short-term pins and useput_page()directly.
Ok, I was misinterpreting the effect of the bug with what KVM is using the reference to do.
To your other point:
Quoted 6 lines
But David's proposed fix for the above refcount bug is to omit the patch so that KVM no longer treats
ZONE_DEVICEpages as reserved. That seems like the right thing to do, including forthp_adjust(), e.g. it would naturally let KVM use 2mb pages for the guest when aZONE_DEVICEpage is mapped with a huge page (2mb or above) in the host. The only hiccup is figuring out how to correctly transfer the reference.
That might not be the only hiccup. There's currently no such thing as huge pages for ZONE_DEVICE, there are huge *mappings* (pmd and pud), but the result of pfn_to_page() on such a mapping does not yield a huge 'struct page'. It seems there are other paths in KVM that assume that more typical page machinery is active like SetPageDirty() based on kvm_is_reserved_pfn(). While I told David that I did not want to see more usage of is_zone_device_page(), this patch below (untested) seems a cleaner path with less surprises:
virt/kvm/kvm_main.c +2 -1
@@ -1831,7 +1831,8 @@ EXPORT_SYMBOL_GPL(kvm_release_page_clean);void kvm_release_pfn_clean (kvm_pfn_t pfn) { -if (!is_error_noslot_pfn (pfn) && !kvm_is_reserved_pfn (pfn)) +if ((!is_error_noslot_pfn (pfn) && !kvm_is_reserved_pfn (pfn)) || + (pfn_valid (pfn) &&is_zone_device_page (pfn_to_page (pfn))))put_page (pfn_to_page (pfn)); }EXPORT_SYMBOL_GPL (kvm_release_pfn_clean);
This is safe because the reference that KVM took earlier protects the is_zone_device_page() lookup from racing device teardown. Otherwise, if KVM does not have a reference it's unsafe, but that's already even more broken because KVM would be releasing a page that it never referenced. Every other KVM operation that assumes page allocator pages would continue to honor kvm_is_reserved_pfn().
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 81 lines · Dan Williams
Quoted 50 lines · Sean Christopherson
Quoted 26 lines · Dan Williams
Quoted 18 lines · Dan Williams
Quoted 15 lines · Sean Christopherson
Quoted 12 lines · Dan Williams
Quoted 7 lines · David Hildenbrand
Quoted 3 lines
The scarier code (for me) is
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte(), as I don't at all understand the interaction between THP and_PAGE_DEVMAP.The x86 KVM MMU code is one of the ugliest code I know (sorry, but it had to be said :/ ). Luckily, this should be independent of the
PG_reservedthingy AFAIKs.Both
transparent_hugepage_adjust()andkvm_mmu_zap_collapsible_spte()are honoringkvm_is_reserved_pfn(), so again I'm missing where the page count gets mismanaged and leads to the reported hang.Quoted 6 lines
Quoted 4 lines
When mapping pages into the guest, KVM gets the page via
gup(), which increments the page count forZONE_DEVICEpages. But KVM puts the page usingkvm_release_pfn_clean(), which skipsput_page()ifPageReserved()and so never puts its reference toZONE_DEVICEpages.Oh, yeah, that's busted.
Quoted 7 lines
Ugh, it's extra busted because every other gup user in the kernel tracks the pages resulting from gup and puts them (
put_page()) when they are done. KVM wants to forget about whether it did a gup to get the page and optionally triggerput_page()based purely on the pfn. Outside of VFIO device assignment that needs pages pinned for DMA, why does KVM itself need to pin pages? If pages are pinned over a return to userspace that needs to be aFOLL_LONGTERMgup.Short answer, KVM pins the page to ensure correctness with respect to the primary MMU invalidating the associated host virtual address, e.g. when the page is being migrated or unmapped from host userspace.
The main use of
gup()is to handle guest page faults and map pages into the guest, i.e. into KVM's secondary MMU. KVM usesgup()to both get the PFN and to temporarily pin the page. The pin is held just long enough to guaranteed that any invalidation via themmu_notifierwill be stalled until after KVM finishes installing the page into the secondary MMU, i.e. the pin is short-term and not held across a return to userspace or entry into the guest. When a subsequentmmu_notifierinvalidation occurs, KVM pulls the PFN from the secondary MMU and uses that to update accessed and dirty bits in the host.Quoted 2 lines
There are a few other KVM flows that eventually call into
gup(), but those are "traditional" short-term pins and useput_page()directly.Ok, I was misinterpreting the effect of the bug with what KVM is using the reference to do.
To your other point:
Quoted 6 lines
But David's proposed fix for the above refcount bug is to omit the patch so that KVM no longer treats
ZONE_DEVICEpages as reserved. That seems like the right thing to do, including forthp_adjust(), e.g. it would naturally let KVM use 2mb pages for the guest when aZONE_DEVICEpage is mapped with a huge page (2mb or above) in the host. The only hiccup is figuring out how to correctly transfer the reference.That might not be the only hiccup. There's currently no such thing as huge pages for
ZONE_DEVICE, there are huge *mappings* (pmd and pud), but the result ofpfn_to_page()on such a mapping does not yield a huge 'struct page'. It seems there are other paths in KVM that assume that more typical page machinery is active likeSetPageDirty()based onkvm_is_reserved_pfn(). While I told David that I did not want to see more usage ofis_zone_device_page(), this patch below (untested) seems a cleaner path with less surprises:virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c EXPORT_SYMBOL_GPL(kvm_release_page_clean); · line 1838
Quoted 9 lines
virt/kvm/kvm_main.c +2
-1@@ -1831,7 +1831,8 @@ EXPORT_SYMBOL_GPL(kvm_release_page_clean);void kvm_release_pfn_clean (kvm_pfn_t pfn) { -if (!is_error_noslot_pfn (pfn) && !kvm_is_reserved_pfn (pfn)) +if ((!is_error_noslot_pfn (pfn) && !kvm_is_reserved_pfn (pfn)) || + (pfn_valid (pfn) &&is_zone_device_page (pfn_to_page (pfn))))put_page (pfn_to_page (pfn)); }EXPORT_SYMBOL_GPL (kvm_release_pfn_clean);
I had the same thought, but I do wonder about the kvm_get_pfn() users, e.g.,:
hva_to_pfn_remapped():
r = follow_pfn(vma, addr, &pfn); ... kvm_get_pfn(pfn); ...
We would not take a reference for ZONE_DEVICE, but later drop one reference via kvm_release_pfn_clean(). IOW, kvm_get_pfn() gets *really* dangerous to use. I can't tell if this can happen right now.
We do have 3 users of kvm_get_pfn() that we have to audit before this change. Also, we should add a comment to kvm_get_pfn() that it should never be used with possible ZONE_DEVICE pages.
Also, we should add a comment to kvm_release_pfn_clean(), describing why we treat ZONE_DEVICE in a special way here.
We can then progress like this
1. Get this fix upstream, it's somewhat unrelated to this series.
2. This patch here remains as is in this series (+/- documentation update)
3. Long term, rework KVM to not have to not treat ZONE_DEVICE like reserved pages. E.g., get rid of kvm_get_pfn(). Then, this special zone check can go.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes
Quoted 24 lines · David Hildenbrand
Quoted 23 lines · Dan Williams
On Tue, Nov 5, 2019 at 4:03 PM Sean Christopherson
Quoted 6 lines
But David's proposed fix for the above refcount bug is to omit the patch so that KVM no longer treats
ZONE_DEVICEpages as reserved. That seems like the right thing to do, including forthp_adjust(), e.g. it would naturally let KVM use 2mb pages for the guest when aZONE_DEVICEpage is mapped with a huge page (2mb or above) in the host. The only hiccup is figuring out how to correctly transfer the reference.That might not be the only hiccup. There's currently no such thing as huge pages for
ZONE_DEVICE, there are huge *mappings* (pmd and pud), but the result ofpfn_to_page()on such a mapping does not yield a huge 'struct page'. It seems there are other paths in KVM that assume that more typical page machinery is active likeSetPageDirty()based onkvm_is_reserved_pfn(). While I told David that I did not want to see more usage ofis_zone_device_page(), this patch below (untested) seems a cleaner path with less surprises:virt/kvm/kvm_main.c +0
-0@@ -1831,7 +1831,8 @@ EXPORT_SYMBOL_GPL(kvm_release_page_clean);
Quoted 4 lines
virt/kvm/kvm_main.c EXPORT_SYMBOL_GPL(kvm_release_page_clean); · line 1834
Quoted 4 lines
virt/kvm/kvm_main.c +1
-1void kvm_release_pfn_clean (kvm_pfn_t pfn) { -if (!is_error_noslot_pfn (pfn) && !kvm_is_reserved_pfn (pfn)) + if ((!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) ||
The is_error_noslot_pfn() check shouldn't be overriden by zone_device.
Quoted 1 line
virt/kvm/kvm_main.c EXPORT_SYMBOL_GPL(kvm_release_page_clean); · line 1835
Quoted 1 line
virt/kvm/kvm_main.c +1
-0+ (pfn_valid(pfn) && is_zone_device_page(pfn_to_page(pfn))))
But rather than special case kvm_release_pfn_clean(), I'd rather KVM explicitly handle ZONE_DEVICE pages, there are other flows where KVM really should be aware of ZONE_DEVICE pages, e.g. for sanity checks and whatnot. There are surprisingly few callers of kvm_is_reserved_pfn(), so it's actually not too big of a change.
Quoted 25 lines
virt/kvm/kvm_main.c EXPORT_SYMBOL_GPL(kvm_release_page_clean); · line 1838
Quoted 3 lines
virt/kvm/kvm_main.c +0
-0put_page (pfn_to_page (pfn)); }EXPORT_SYMBOL_GPL (kvm_release_pfn_clean);I had the same thought, but I do wonder about the
kvm_get_pfn()users, e.g.,:
hva_to_pfn_remapped():r = follow_pfn(vma, addr, &pfn); ... kvm_get_pfn(pfn); ...We would not take a reference for
ZONE_DEVICE, but later drop one reference viakvm_release_pfn_clean(). IOW,kvm_get_pfn()gets *really* dangerous to use. I can't tell if this can happen right now.We do have 3 users of
kvm_get_pfn()that we have to audit before this change. Also, we should add a comment tokvm_get_pfn()that it should never be used with possibleZONE_DEVICEpages.Also, we should add a comment to
kvm_release_pfn_clean(), describing why we treatZONE_DEVICEin a special way here.
Quoted 6 lines
We can then progress like this
1. Get this fix upstream, it's somewhat unrelated to this series.
2. This patch here remains as is in this series (+/- documentation update)
3. Long term, rework KVM to not have to not treatZONE_DEVICElike reserved pages. E.g., get rid ofkvm_get_pfn(). Then, this special zone check can go.
Dropping kvm_get_pfn() is less than ideal, and at this point unnecessary. I'm 99% sure the existing call sites for kvm_get_pfn() can never be reached with ZONE_DEVICE pages. I think we can do:
1. Get a fix upstream to have KVM stop treating ZONE_DEVICE pages as reserved PFNs, i.e. exempt them in kvm_is_reserved_pfn() and change the callers of kvm_is_reserved_pfn() to handle ZONE_DEVICE pages.
2. Drop this patch from the series, and instead remove the special treatment of ZONE_DEVICE pages from kvm_is_reserved_pfn().
Give me a few minutes to prep a patch.
Quoted 7 lines
Signature · 5 lines
-- Thanks, David / dhildenb
PATCH 4/10 vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
KVM has this weird use case that you can map anything from /dev/mem into the guest. pfn_valid() is not a reliable check whether the memmap was initialized and can be touched. pfn_to_online_page() makes sure that we have an initialized memmap (and don't have ZONE_DEVICE memory).
Rewrite is_invalid_reserved_pfn() similar to kvm_is_reserved_pfn() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- drivers/vfio/vfio_iommu_type1.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +8 -2
@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn); +/* +* We treat any pages that are not online (not managed by the buddy) +* as reserved - this includes ZONE_DEVICE pages and pages without +* a memmap (e.g., mapped via /dev/mem). +*/ +if (page) +return PageReserved (page);return true; }
Signature · 1 line
2.21.0
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0
drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);
Ugh, I just realized this is not a safe conversion until pfn_to_online_page() is moved over to subsection granularity. As it stands it will return true for any ZONE_DEVICE pages that share a section with boot memory.
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 25 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 20 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)
Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have ZONE_DEVICE mixed with online pages in a section. Having memory block devices that partially span ZONE_DEVICE would be ... really weird. With something like pfn_active() - as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 42 lines · David Hildenbrand
Quoted 25 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 20 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.
I just realized the "boot memory" part. Is that a real thing? IOW, can we have ZONE_DEVICE falling into a memory block (with holes)? I somewhat have doubts that this would work ...
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 44 lines · David Hildenbrand
Quoted 42 lines · David Hildenbrand
Quoted 41 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.
Quoted 6 lines
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.I just realized the "boot memory" part. Is that a real thing? IOW, can we have
ZONE_DEVICEfalling into a memory block (with holes)? I somewhat have doubts that this would work ...
One of the real world failure cases that started the subsection effect is that Persistent Memory collides with System RAM on a 64MB boundary on shipping platforms. System RAM ends on a 64MB boundary and due to a lack of memory controller resources PMEM is mapped contiguously at the end of that boundary. Some more details in the subsection cover letter / changelogs [1] [2]. It's not sufficient to just lose some memory, that's the broken implementation that lead to the subsection work because the lost memory may change from one boot to the next and software can't reliably inject a padding that conforms to the x86 128MB section constraint.
Suffice to say I think we need your pfn_active() to get subsection granularity pfn_to_online_page() before PageReserved() can be removed.
[1]: https://lore.kernel.org/linux-mm/156092349300.979959.17603710711957735135.stgit@dwillia2-desk3.amr.corp.intel.com/
[2]: https://lore.kernel.org/linux-mm/156092354368.979959.6232443923440952359.stgit@dwillia2-desk3.amr.corp.intel.com/
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 52 lines · Dan Williams
Quoted 44 lines · David Hildenbrand
Quoted 42 lines · David Hildenbrand
Quoted 41 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.Quoted 6 lines
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.I just realized the "boot memory" part. Is that a real thing? IOW, can we have
ZONE_DEVICEfalling into a memory block (with holes)? I somewhat have doubts that this would work ...
Quoted 10 lines
One of the real world failure cases that started the subsection effect is that Persistent Memory collides with System RAM on a 64MB boundary on shipping platforms. System RAM ends on a 64MB boundary and due to a lack of memory controller resources PMEM is mapped contiguously at the end of that boundary. Some more details in the subsection cover letter / changelogs [1] [2]. It's not sufficient to just lose some memory, that's the broken implementation that lead to the subsection work because the lost memory may change from one boot to the next and software can't reliably inject a padding that conforms to the x86 128MB section constraint.
Thanks, I thought it was mostly for weird alignment where other parts of the section are basically "holes" and not memory.
Yes, it is a real bug that ZONE_DEVICE pages fall into sections that are marked SECTION_IS_ONLINE.
Quoted 3 lines
Suffice to say I think we need your
pfn_active()to get subsection granularitypfn_to_online_page()beforePageReserved()can be removed.
I agree that we have to fix this. I don't like ZONE_DEVICE pages falling into memory device blocks (e.g., cannot get offlined), but I guess that train is gone :) As long as it's not for memory hotplug, I can most probably live with this.
Also, I'd like to get Michals opinion on this and the pfn_active() approach, but I can understand he's busy.
This patch set can wait, I won't be working next week besides reading/writing mails either way.
Is anybody looking into the pfn_active() thingy?
Quoted 4 lines
[1]: https://lore.kernel.org/linux-mm/156092349300.979959.17603710711957735135.stgit@dwillia2-desk3.amr.corp.intel.com/
[2]: https://lore.kernel.org/linux-mm/156092354368.979959.6232443923440952359.stgit@dwillia2-desk3.amr.corp.intel.com/
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 79 lines · David Hildenbrand
Quoted 52 lines · Dan Williams
Quoted 44 lines · David Hildenbrand
Quoted 42 lines · David Hildenbrand
Quoted 41 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.Quoted 6 lines
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.I just realized the "boot memory" part. Is that a real thing? IOW, can we have
ZONE_DEVICEfalling into a memory block (with holes)? I somewhat have doubts that this would work ...Quoted 10 lines
One of the real world failure cases that started the subsection effect is that Persistent Memory collides with System RAM on a 64MB boundary on shipping platforms. System RAM ends on a 64MB boundary and due to a lack of memory controller resources PMEM is mapped contiguously at the end of that boundary. Some more details in the subsection cover letter / changelogs [1] [2]. It's not sufficient to just lose some memory, that's the broken implementation that lead to the subsection work because the lost memory may change from one boot to the next and software can't reliably inject a padding that conforms to the x86 128MB section constraint.
Thanks, I thought it was mostly for weird alignment where other parts of the section are basically "holes" and not memory.
Yes, it is a real bug that
ZONE_DEVICEpages fall into sections that are markedSECTION_IS_ONLINE.Quoted 3 lines
Suffice to say I think we need your
pfn_active()to get subsection granularitypfn_to_online_page()beforePageReserved()can be removed.I agree that we have to fix this. I don't like
ZONE_DEVICEpages falling into memory device blocks (e.g., cannot get offlined), but I guess that train is gone :) As long as it's not for memory hotplug, I can most probably live with this.
Quoted 8 lines
Also, I'd like to get Michals opinion on this and the
pfn_active()approach, but I can understand he's busy.This patch set can wait, I won't be working next week besides reading/writing mails either way.
Is anybody looking into the
pfn_active()thingy?
I wonder if we should do something like this right now to fix this (exclude the false positive ZONE_DEVICE pages we could have within an online section, which was not possible before subsection hotplug):
include/linux/memory_hotplug.h +2 -0
@@ -30,6 +30,8 @@ struct vmem_altmap;if (___nr <NR_MEM_SECTIONS &&online_section_nr (___nr) && \pfn_valid_within (___pfn)) \ ___page =pfn_to_page (___pfn); \ +if (unlikely (___page &&is_zone_device_page (___page))) \ + ___page =NULL ; \ ___page; \ })
Yeah, it's another is_zone_device_page(), but it should not be racy here, as we want to exclude, not include ZONE_DEVICE.
I don't have time to look into this right now, unfortunately.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 108 lines · David Hildenbrand
Quoted 79 lines · David Hildenbrand
Quoted 52 lines · Dan Williams
Quoted 44 lines · David Hildenbrand
Quoted 42 lines · David Hildenbrand
Quoted 41 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.Quoted 6 lines
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.I just realized the "boot memory" part. Is that a real thing? IOW, can we have
ZONE_DEVICEfalling into a memory block (with holes)? I somewhat have doubts that this would work ...Quoted 10 lines
One of the real world failure cases that started the subsection effect is that Persistent Memory collides with System RAM on a 64MB boundary on shipping platforms. System RAM ends on a 64MB boundary and due to a lack of memory controller resources PMEM is mapped contiguously at the end of that boundary. Some more details in the subsection cover letter / changelogs [1] [2]. It's not sufficient to just lose some memory, that's the broken implementation that lead to the subsection work because the lost memory may change from one boot to the next and software can't reliably inject a padding that conforms to the x86 128MB section constraint.
Thanks, I thought it was mostly for weird alignment where other parts of the section are basically "holes" and not memory.
Yes, it is a real bug that
ZONE_DEVICEpages fall into sections that are markedSECTION_IS_ONLINE.Quoted 3 lines
Suffice to say I think we need your
pfn_active()to get subsection granularitypfn_to_online_page()beforePageReserved()can be removed.I agree that we have to fix this. I don't like
ZONE_DEVICEpages falling into memory device blocks (e.g., cannot get offlined), but I guess that train is gone :) As long as it's not for memory hotplug, I can most probably live with this.Quoted 8 lines
Also, I'd like to get Michals opinion on this and the
pfn_active()approach, but I can understand he's busy.This patch set can wait, I won't be working next week besides reading/writing mails either way.
Is anybody looking into the
pfn_active()thingy?I wonder if we should do something like this right now to fix this (exclude the false positive
ZONE_DEVICEpages we could have within an online section, which was not possible before subsection hotplug):include/linux/memory_hotplug.h +2
-0@@ -30,6 +30,8 @@ struct vmem_altmap;if (___nr <NR_MEM_SECTIONS &&online_section_nr (___nr) && \pfn_valid_within (___pfn)) \ ___page =pfn_to_page (___pfn); \ +if (unlikely (___page &&is_zone_device_page (___page))) \ + ___page =NULL ; \ ___page; \ })
Quoted 4 lines
Yeah, it's another
is_zone_device_page(), but it should not be racy here, as we want to exclude, not includeZONE_DEVICE.I don't have time to look into this right now, unfortunately.
I don't want to band-aid without an actual bug report. I'll take a look at a subsection-map for the online state.
Re: [PATCH v1 04/10] vfio/type1: Prepare is_invalid_reserved_pfn() for PG_reserved changes
Quoted 109 lines · Dan Williams
Quoted 108 lines · David Hildenbrand
Quoted 79 lines · David Hildenbrand
Quoted 52 lines · Dan Williams
Quoted 44 lines · David Hildenbrand
Quoted 42 lines · David Hildenbrand
Quoted 41 lines
Am 07.11.2019 um 16:40 schrieb Dan Williams <[email protected]>:
Quoted 25 lines · David Hildenbrand
Right now,
ZONE_DEVICEmemory is always setPG_reserved. We want to change that.KVM has this weird use case that you can map anything from
/dev/meminto the guest.pfn_valid()is not a reliable check whether the memmap was initialized and can be touched.pfn_to_online_page()makes sure that we have an initialized memmap (and don't haveZONE_DEVICEmemory).Rewrite
is_invalid_reserved_pfn()similar tokvm_is_reserved_pfn()to make sure the function produces the same result once we stop settingZONE_DEVICEpagesPG_reserved.Cc Alex Williamson <[email protected]>Cc Cornelia Huck <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
drivers/vfio/vfio_iommu_type1.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0drivers/vfio/vfio_iommu_type1.c static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) · line 302
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-2@@ -299,9 +299,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)*/ static bool is_invalid_reserved_pfn (unsigned long pfn) { -if (pfn_valid (pfn)) -return PageReserved (pfn_to_page (pfn)); +struct page *page =pfn_to_online_page (pfn);Ugh, I just realized this is not a safe conversion until
pfn_to_online_page()is moved over to subsection granularity. As it stands it will return true for anyZONE_DEVICEpages that share a section with boot memory.Quoted 6 lines
Quoted 2 lines
That should not happen right now and I commented back when you introduced subsection support that I don’t want to have
ZONE_DEVICEmixed with online pages in a section. Having memory block devices that partially spanZONE_DEVICEwould be ... really weird. With something likepfn_active()- as discussed - we could at least make this check work - but I am not sure if we really want to go down that path. In the worst case, some MB of RAM are lost ... I guess this needs more thought.I just realized the "boot memory" part. Is that a real thing? IOW, can we have
ZONE_DEVICEfalling into a memory block (with holes)? I somewhat have doubts that this would work ...Quoted 10 lines
One of the real world failure cases that started the subsection effect is that Persistent Memory collides with System RAM on a 64MB boundary on shipping platforms. System RAM ends on a 64MB boundary and due to a lack of memory controller resources PMEM is mapped contiguously at the end of that boundary. Some more details in the subsection cover letter / changelogs [1] [2]. It's not sufficient to just lose some memory, that's the broken implementation that lead to the subsection work because the lost memory may change from one boot to the next and software can't reliably inject a padding that conforms to the x86 128MB section constraint.
Thanks, I thought it was mostly for weird alignment where other parts of the section are basically "holes" and not memory.
Yes, it is a real bug that
ZONE_DEVICEpages fall into sections that are markedSECTION_IS_ONLINE.Quoted 3 lines
Suffice to say I think we need your
pfn_active()to get subsection granularitypfn_to_online_page()beforePageReserved()can be removed.I agree that we have to fix this. I don't like
ZONE_DEVICEpages falling into memory device blocks (e.g., cannot get offlined), but I guess that train is gone :) As long as it's not for memory hotplug, I can most probably live with this.Quoted 8 lines
Also, I'd like to get Michals opinion on this and the
pfn_active()approach, but I can understand he's busy.This patch set can wait, I won't be working next week besides reading/writing mails either way.
Is anybody looking into the
pfn_active()thingy?I wonder if we should do something like this right now to fix this (exclude the false positive
ZONE_DEVICEpages we could have within an online section, which was not possible before subsection hotplug):include/linux/memory_hotplug.h +2
-0@@ -30,6 +30,8 @@ struct vmem_altmap;if (___nr <NR_MEM_SECTIONS &&online_section_nr (___nr) && \pfn_valid_within (___pfn)) \ ___page =pfn_to_page (___pfn); \ +if (unlikely (___page &&is_zone_device_page (___page))) \ + ___page =NULL ; \ ___page; \ })
Quoted 8 lines
Quoted 4 lines
Yeah, it's another
is_zone_device_page(), but it should not be racy here, as we want to exclude, not includeZONE_DEVICE.I don't have time to look into this right now, unfortunately.
I don't want to band-aid without an actual bug report. I'll take a look at a subsection-map for the online state.
Fair enough, but at least in what I proposed for pfn_active(), this check would exist in pfn_to_online_page() in a similar way - and it is certainly easier to backport. But yeah, triggering this might not be easy.
Signature · 3 lines
Thanks, David / dhildenb
PATCH 5/10 powerpc/book3s: Prepare kvmppc_book3s_instantiate_page() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
KVM has this weird use case that you can map anything from /dev/mem into the guest. pfn_valid() is not a reliable check whether the memmap was initialized and can be touched. pfn_to_online_page() makes sure that we have an initialized memmap (and don't have ZONE_DEVICE memory).
Rewrite kvmppc_book3s_instantiate_page() similar to kvm_is_reserved_pfn() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- arch/powerpc/kvm/book3s_64_mmu_radix.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_radix.c +8 -6
@@ -801,12 +801,14 @@ int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, writing, upgrade_p);if (is_error_noslot_pfn (pfn))return -EFAULT ; - page =NULL ; -if (pfn_valid (pfn)) { - page =pfn_to_page (pfn); -if (PageReserved (page)) - page =NULL ; - } +/* +* We treat any pages that are not online (not managed by the +* buddy) as reserved - this includes ZONE_DEVICE pages and +* pages without a memmap (e.g., mapped via /dev/mem). +*/ + page =pfn_to_online_page (pfn); +if (page &&PageReserved (page)) + page =NULL ; }/*
Signature · 1 line
2.21.0
PATCH 6/10 powerpc/64s: Prepare hash_page_do_lazy_icache() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
Rewrite hash_page_do_lazy_icache() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- arch/powerpc/mm/book3s64/hash_utils.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/powerpc/mm/book3s64/hash_utils.c +6 -4
@@ -1084,13 +1084,15 @@ void hash__early_init_mmu_secondary(void)*/ unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap) { -struct page *page; +struct page *page =pfn_to_online_page (pte_pfn (pte)); -if (!pfn_valid (pte_pfn (pte))) +/* +* We ignore any pages that are not online (not managed by the buddy). +* This includes ZONE_DEVICE pages. +*/ +if (!page)return pp; - page =pte_page (pte); -/* page is dirty */ if (!test_bit (PG_arch_1, &page->flags) && !PageReserved (page)) {if (trap ==0x400 ) {
Signature · 1 line
2.21.0
PATCH 7/10 powerpc/mm: Prepare maybe_pte_to_page() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
Rewrite maybe_pte_to_page() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- arch/powerpc/mm/pgtable.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/powerpc/mm/pgtable.c +6 -4
@@ -55,10 +55,12 @@ static struct page *maybe_pte_to_page(pte_t pte)unsigned long pfn =pte_pfn (pte);struct page *page; -if (unlikely (!pfn_valid (pfn))) -return NULL ; - page =pfn_to_page (pfn); -if (PageReserved (page)) +/* +* We reject any pages that are not online (not managed by the buddy). +* This includes ZONE_DEVICE pages. +*/ + page =pfn_to_online_page (pfn); +if (unlikely (!page ||PageReserved (page)))return NULL ;return page; }
Signature · 1 line
2.21.0
PATCH 8/10 x86/mm: Prepare __ioremap_check_ram() for PG_reserved changes
In reply to David HildenbrandRight now, ZONE_DEVICE memory is always set PG_reserved. We want to change that.
Rewrite __ioremap_check_ram() to make sure the function produces the same result once we stop setting ZONE_DEVICE pages PG_reserved.
--- arch/x86/mm/ioremap.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/mm/ioremap.c +10 -3
@@ -77,10 +77,17 @@ static unsigned int __ioremap_check_ram(struct resource *res) start_pfn = (res->start +PAGE_SIZE -1 ) >>PAGE_SHIFT ; stop_pfn = (res->end +1 ) >>PAGE_SHIFT ;if (stop_pfn > start_pfn) { -for (i =0 ; i < (stop_pfn - start_pfn); ++i) -if (pfn_valid (start_pfn + i) && - !PageReserved (pfn_to_page (start_pfn + i))) +for (i =0 ; i < (stop_pfn - start_pfn); ++i) { +struct page *page; +/* +* We treat any pages that are not online (not managed +* by the buddy) as not being RAM. This includes +* ZONE_DEVICE pages. +*/ + page =pfn_to_online_page (start_pfn + i); +if (page && !PageReserved (page))return IORES_MAP_SYSTEM_RAM ; + } }return 0 ;
Signature · 1 line
2.21.0
PATCH 9/10 mm/memory_hotplug: Don't mark pages PG_reserved when initializing the memmap
In reply to David HildenbrandEverything should be prepared to stop setting pages PG_reserved when initializing the memmap on memory hotplug. Most importantly, we stop marking ZONE_DEVICE pages PG_reserved.
a) We made sure that any code that relied on PG_reserved to detect ZONE_DEVICE memory will no longer rely on PG_reserved (especially, by relying on pfn_to_online_page() for now). Details can be found below.
b) We made sure that memory blocks with holes cannot be offlined and therefore also not onlined. We have quite some code that relies on memory holes being marked PG_reserved. This is now not an issue anymore.
generic_online_page() still calls __free_pages_core(), which performs __ClearPageReserved(p). AFAIKS, this should not hurt.
It is worth nothing that the users of online_page_callback_t might see a change. E.g., until now, pages not freed to the buddy by the HyperV balloonm were set PG_reserved until freed via generic_online_page(). Now, they would look like ordinarily allocated pages (refcount == 1). This callback is used by the XEN balloon and the HyperV balloon. To not introduce any silent errors, keep marking the pages PG_reserved. We can most probably stop doing that, but have to double check if there are issues (e.g., offlining code aborts right away in has_unmovable_pages() when it runs into a PageReserved(page))
Update the documentation at various places in the MM core.
There are three PageReserved() users that might be affected by this change.
- drivers/staging/gasket/gasket_page_table.c:gasket_release_page() -> We might (unlikely) set SetPageDirty() on a ZONE_DEVICE page -> I assume "we don't care"
- drivers/staging/kpc2000/kpc_dma/fileops.c:transfer_complete_cb() -> We might (unlikely) set SetPageDirty() on a ZONE_DEVICE page -> I assume "we don't care"
- mm/usercopy.c: check_page_span()
-> According to Dan, non-HMM ZONE_DEVICE usage excluded this code since
commit 52f476a323f9 ("libnvdimm/pmem: Bypass CONFIG_HARDENED_USERCOPY overhead")
-> It is unclear whether we rally cared about ZONE_DEVICE here (HMM) or
simply about "PG_reserved". The worst thing that could happen is a false negative with CONFIG_HARDENED_USERCOPY we should be able to identify easily.
-> There is a discussion to rip out that code completely
-> I assume "not relevant" / "we don't care"
I audited the other PageReserved() users. They don't affect ZONE_DEVICE:
- mm/page_owner.c:pagetypeinfo_showmixedcount_print()
-> Never called for ZONE_DEVICE, (+ pfn_to_online_page(pfn))
- mm/page_owner.c:init_pages_in_zone()
-> Never called for ZONE_DEVICE (!populated_zone(zone))
- mm/page_ext.c:free_page_ext()
-> Only a BUG_ON(PageReserved(page)), not relevant
- mm/page_ext.c:has_unmovable_pages()
-> Not releveant for ZONE_DEVICE
- mm/page_ext.c:pfn_range_valid_contig()
-> pfn_to_online_page() already guards us
- mm/mempolicy.c:queue_pages_pte_range()
-> vm_normal_page() checks against pte_devmap()
- mm/memory-failure.c:hwpoison_user_mappings()
-> Not reached via memory_failure() due to pfn_to_online_page() -> Also not reached indirectly via memory_failure_hugetlb()
- mm/hugetlb.c:gather_bootmem_prealloc()
-> Only a WARN_ON(PageReserved(page)), not relevant
- kernel/power/snapshot.c:saveable_highmem_page()
-> pfn_to_online_page() already guards us
- kernel/power/snapshot.c:saveable_page()
-> pfn_to_online_page() already guards us
- fs/proc/task_mmu.c:can_gather_numa_stats()
-> vm_normal_page() checks against pte_devmap()
- fs/proc/task_mmu.c:can_gather_numa_stats_pmd
-> vm_normal_page_pmd() checks against pte_devmap()
- fs/proc/page.c:stable_page_flags()
-> The reserved bit is simply copied, irrelevant
- drivers/firmware/memmap.c:release_firmware_map_entry()
-> really only a check to detect bootmem. Not relevant for ZONE_DEVICE
- arch/ia64/kernel/mca_drv.c
- arch/mips/mm/init.c
- arch/mips/mm/ioremap.c
- arch/nios2/mm/ioremap.c
- arch/parisc/mm/ioremap.c
- arch/sparc/mm/tlb.c
- arch/xtensa/mm/cache.c
-> No ZONE_DEVICE support
- arch/powerpc/mm/init_64.c:vmemmap_free()
-> Special-cases memmap on altmap
-> Only a check for bootmem
- arch/x86/kernel/alternative.c:__text_poke()
-> Only a WARN_ON(!PageReserved(pages[0])) to verify it is bootmem
- arch/x86/mm/init_64.c
-> Only a check for bootmem
--- drivers/hv/hv_balloon.c | 6 ++++++ drivers/xen/balloon.c | 7 +++++++ include/linux/page-flags.h | 8 +------- mm/memory_hotplug.c | 17 +++++++---------- mm/page_alloc.c | 11 ----------- 5 files changed, 21 insertions(+), 28 deletions(-)
drivers/hv/hv_balloon.c +6 -0
@@ -670,6 +670,12 @@ static struct notifier_block hv_memory_nb = {/* Check if the particular page is backed and can be onlined and online it. */ static void hv_page_online_one (struct hv_hotadd_state *has,struct page *pg) { +/* +* TODO: The core used to mark the pages reserved. Most probably +* we can stop doing that now. +*/ +__SetPageReserved (pg); +if (!has_pfn_is_backed (has,page_to_pfn (pg))) {if (!PageOffline (pg))__SetPageOffline (pg);
drivers/xen/balloon.c +7 -0
@@ -374,6 +374,13 @@ static void xen_online_page(struct page *page, unsigned int order)mutex_lock (&balloon_mutex);for (i =0 ; i < size; i++) { p =pfn_to_page (start_pfn + i); +/* +* TODO: The core used to mark the pages reserved. Most probably +* we can stop doing that now. However, especially +* alloc_xenballooned_pages() left PG_reserved set +* on pages that can get mapped to user space. +*/ +__SetPageReserved (p);balloon_append (p); }mutex_unlock (&balloon_mutex);
include/linux/page-flags.h +1 -7
@@ -30,24 +30,18 @@* - Pages falling into physical memory gaps - not IORESOURCE_SYSRAM. Trying * to read/write these pages might end badly. Don't touch! * - The zero page(s) -* - Pages not added to the page allocator when onlining a section because -* they were excluded via the online_page_callback() or because they are -* PG_hwpoison. * - Pages allocated in the context of kexec/kdump (loaded kernel image, * control pages, vmcoreinfo) * - MMIO/DMA pages. Some architectures don't allow to ioremap pages that are * not marked PG_reserved (as they might be in use by somebody else who does * not respect the caching strategy). -* - Pages part of an offline section (struct pages of offline sections should -* not be trusted as they will be initialized when first onlined). * - MCA pages on ia64 * - Pages holding CPU notes for POWER Firmware Assisted Dump -* - Device memory (e.g. PMEM, DAX, HMM) * Some PG_reserved pages will be excluded from the hibernation image. * PG_reserved does in general not hinder anybody from dumping or swapping * and is no longer required for remap_pfn_range(). ioremap might require it. * Consequently, PG_reserved for a page mapped into user space can indicate -* the zero page, the vDSO, MMIO pages or device memory. +* the zero page, the vDSO, or MMIO pages. * * The PG_private bitflag is set on pagecache pages if they contain filesystem * specific data (which is normally at page->private). It can be used by
mm/memory_hotplug.c +7 -10
@@ -501,9 +501,7 @@ static void __remove_section(unsigned long pfn, unsigned long nr_pages,* @altmap: alternative device page map or %NULL if default memmap is used * * Generic helper function to remove section mappings and sysfs entries -* for the section of the memory we are removing. Caller needs to make -* sure that pages are marked reserved and zones are adjust properly by -* calling offline_pages(). +* for the section of the memory we are removing. */ void __remove_pages (unsigned long pfn,unsigned long nr_pages,struct vmem_altmap *altmap)
@@ -584,9 +582,9 @@ static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,int order;/* -* Online the pages. The callback might decide to keep some pages -* PG_reserved (to add them to the buddy later), but we still account -* them as being online/belonging to this zone ("present"). +* Online the pages. The callback might decide to not free some pages +* (to add them to the buddy later), but we still account them as +* being online/belonging to this zone ("present"). */ for (pfn = start_pfn; pfn < end_pfn; pfn +=1ul << order) { order =min (MAX_ORDER -1 ,get_order (PFN_PHYS (end_pfn - pfn)));
@@ -659,8 +657,7 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon }/* * Associate the pfn range with the given zone, initializing the memmaps -* and resizing the pgdat/zone data to span the added pages. After this -* call, all affected pages are PG_reserved. +* and resizing the pgdat/zone data to span the added pages. */ void __ref move_pfn_range_to_zone (struct zone *zone,unsigned long start_pfn,unsigned long nr_pages,struct vmem_altmap *altmap)
@@ -684,8 +681,8 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,/* * TODO now we have a visible range of pages which are not associated * with their zone properly. Not nice but set_pfnblock_flags_mask -* expects the zone spans the pfn range. All the pages in the range -* are reserved so nobody should be touching them so we should be safe +* expects the zone spans the pfn range. The sections are not yet +* marked online so nobody should be touching the memmap. */ memmap_init_zone (nr_pages, nid,zone_idx (zone), start_pfn,MEMMAP_HOTPLUG , altmap);
mm/page_alloc.c +0 -11
@@ -5927,8 +5927,6 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, page =pfn_to_page (pfn);__init_single_page (page, pfn, zone, nid); -if (context ==MEMMAP_HOTPLUG ) -__SetPageReserved (page);/* * Mark the block movable so that blocks are reserved for
@@ -5980,15 +5978,6 @@ void __ref memmap_init_zone_device(struct zone *zone,__init_single_page (page, pfn, zone_idx, nid); -/* -* Mark page reserved as it will need to wait for onlining -* phase for it to be fully associated with a zone. -* -* We can use the non-atomic __set_bit operation for setting -* the flag as we are still initializing the pages. -*/ -__SetPageReserved (page); -/* * ZONE_DEVICE pages union ->lru with a ->pgmap back pointer * and zone_device_data. It is a bug if a ZONE_DEVICE page is
Signature · 1 line
2.21.0
Re: [PATCH v1 09/10] mm/memory_hotplug: Don't mark pages PG_reserved when initializing the memmap
Quoted 5 lines · David Hildenbrand
drivers/xen/balloon.c +0
-0
drivers/xen/balloon.c static void xen_online_page(struct page *page, unsigned int order) · line 383
Quoted 10 lines
drivers/xen/balloon.c +7
-0@@ -374,6 +374,13 @@ static void xen_online_page(struct page *page, unsigned int order)mutex_lock (&balloon_mutex);for (i =0 ; i < size; i++) { p =pfn_to_page (start_pfn + i); +/* +* TODO: The core used to mark the pages reserved. Most probably +* we can stop doing that now. However, especially +* alloc_xenballooned_pages() left PG_reserved set +* on pages that can get mapped to user space. +*/ +__SetPageReserved (p);
I suspect this is not needed. Pages can get into balloon either from here or from non-hotplug path (e.g. decrease_reservation()) and so when we get a page from the balloon we would get a random page that may or may not have Reserved bit set.
-boris
Quoted 4 lines
drivers/xen/balloon.c +0
-0balloon_append (p); }mutex_unlock (&balloon_mutex);
Re: [PATCH v1 09/10] mm/memory_hotplug: Don't mark pages PG_reserved when initializing the memmap
Quoted 13 lines · Boris Ostrovsky
Quoted 12 lines · David Hildenbrand
drivers/xen/balloon.c +4
-0@@ -374,6 +374,13 @@ static void xen_online_page(struct page *page, unsigned int order)mutex_lock (&balloon_mutex);for (i =0 ; i < size; i++) { p =pfn_to_page (start_pfn + i); +/* +* TODO: The core used to mark the pages reserved. Most probably +* we can stop doing that now. However, especially +* alloc_xenballooned_pages() left PG_reserved set
Quoted 8 lines
drivers/xen/balloon.c static void xen_online_page(struct page *page, unsigned int order) · line 383
Quoted 3 lines
drivers/xen/balloon.c +3
-0+* on pages that can get mapped to user space. +*/ +__SetPageReserved (p);I suspect this is not needed. Pages can get into balloon either from here or from non-hotplug path (e.g.
decrease_reservation()) and so when we get a page from the balloon we would get a random page that may or may not have Reserved bit set.
Yeah, I also think it is fine. If you agree, I'll drop this hunk and add details to the patch description.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 09/10] mm/memory_hotplug: Don't mark pages PG_reserved when initializing the memmap
Quoted 25 lines · David Hildenbrand
Quoted 15 lines · Boris Ostrovsky
Quoted 14 lines · David Hildenbrand
drivers/xen/balloon.c +2
-0@@ -374,6 +374,13 @@ static void xen_online_page(struct page *page, unsigned int order) mutex_lock(&balloon_mutex); for (i = 0; i < size; i++) { p = pfn_to_page(start_pfn + i); + /* +* TODO: The core used to mark the pages reserved. Most probably
+ * we can stop doing that now. However, especially + *alloc_xenballooned_pages()leftPG_reservedsetQuoted 8 lines
Quoted 3 lines
+ * on pages that can get mapped to user space. + */
+__SetPageReserved(p);I suspect this is not needed. Pages can get into balloon either from here or from non-hotplug path (e.g.
decrease_reservation()) and so when we get a page from the balloon we would get a random page that may or may not have Reserved bit set.
Quoted 4 lines
Yeah, I also think it is fine. If you agree, I'll drop this hunk and add details to the patch description.
Yes, let's do that. Thanks.
-boris
PATCH 10/10 mm/usercopy.c: Update comment in check_page_span() regarding ZONE_DEVICE
In reply to David HildenbrandZONE_DEVICE (a.k.a. device memory) is no longer marked PG_reserved. Update the comment.
While at it, make it match what the code is acutally doing (reject vs. accept).
--- mm/usercopy.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/usercopy.c +3 -3
@@ -199,9 +199,9 @@ static inline void check_page_span(const void *ptr, unsigned long n,return ;/* -* Reject if range is entirely either Reserved (i.e. special or -* device memory), or CMA. Otherwise, reject since the object spans -* several independently allocated pages. +* Accept if the range is entirely either Reserved ("special") or +* CMA. Otherwise, reject since the object spans several independently +* allocated pages. */ is_reserved =PageReserved (page); is_cma =is_migrate_cma_page (page);
Signature · 1 line
2.21.0
Quoted 30 lines · David Hildenbrand
This is the result of a recent discussion with Michal ([1], [2]). Right now we set all pages
PG_reservedwhen initializing hotplugged memmaps. This includesZONE_DEVICEmemory. In case of system memory,PG_reservedis cleared again when onlining the memory, in case ofZONE_DEVICEmemory never.In ancient times, we needed
PG_reserved, because there was no way to tell whether the memmap was already properly initialized. We now haveSECTION_IS_ONLINEfor that in the case of !ZONE_DEVICE memory.ZONE_DEVICEmemory is already initialized deferred, and there shouldn't be a visible change in that regard.One of the biggest fears were side effects. I went ahead and audited all users of
PageReserved(). The details can be found in "mm/memory_hotplug:
Don't mark pagesPG_reservedwhen initializing the memmap".This patch set adapts all relevant users of
PageReserved()to keep the existing behavior in respect toZONE_DEVICEpages. The biggest part part that needs changes is KVM, to keep the existing behavior (that's all I care about in this series).Note that this series is able to rely completely on
pfn_to_online_page(). No newis_zone_device_page()calles are introduced (as requested by Dan). We are currently discussing a way to mark alsoZONE_DEVICEmemmaps as active/initialized -pfn_active()- and lightweight locking to make sure memmaps remain active (e.g., using RCU). We might later be able to convert some suers ofpfn_to_online_page()topfn_active(). Details can be found in [3], however, this represents yet another cleanup/fix we'll perform on top of this cleanup.
Quoted 5 lines
I only gave it a quick test with DIMMs on x86-64, but didn't test the
ZONE_DEVICEpart at all (any tips for a nice QEMU setup?). Also, I didn't test the KVM parts (especially withZONE_DEVICEpages or no memmap at all). Compile-tested on x86-64 and PPC.
Jeff Moyer ran some NVDIMM test cases for me (thanks!!!), including xfstests, pmdk, and ndctl. No regressions found.
I will run some KVM tests, especially NDIMM passthrough, but will have to setup a test environment first.
I would appreciate some review in the meantime. :)
Signature · 3 lines
Thanks, David / dhildenb