Skip to contentSkip to conversation

arm64/mm: Enable batched TLB flush in unmap_hotplug_range()

PATCH Superseded Anshuman Khandual 2 participants 11 messages Feb 24 – Mar 4

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover arm64/mm: Enable batched TLB flush in unmap_hotplug_range()

This series enables batched TLB flush in unmap_hotplug_range() which avoids individual page TLB flush for potential CONT blocks in linear mapping while also improving performance due to range based TLB operation along with less synchronization barrier instructions.

It also now rejects memory removal that might split a leaf entry in kernel mapping, which would have otherwise required re-structuring using the break before make (BBM) semantics. But kernel cannot tolerate BBM, so remapping to fine grained leaves would not be possible on systems without BBML2_NOABORT.

This series applies on 7.0-rc1 and tested on KVM guest.

Changes in V3:

- Fixed some typos in the commit message for [PATCH 2/2]
- Dropped copying [email protected] per Ryan

Changes in V2:

https://lore.kernel.org/all/[email protected]/

- Renamed split_kernel_leaf_boundary() as addr_splits_kernel_leaf()
- Dropped pte_valid()/pmd_leaf() before checking pte_cont()/pmd_cont()
- Changed and dropped variables in can_unmap_without_split()
- Moved can_unmap_without_split() after boot memory check
- Renamed 'prevent_bootmem' as 'prevent_memory' across the board
- Updated commit message in both patches

Changes in V1:

- https://lore.kernel.org/all/[email protected]/

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Yang Shi <[email protected]>
Cc Christoph Lameter <[email protected]>

Anshuman Khandual (2):
arm64/mm: Enable batched TLB flush in unmap_hotplug_range() arm64/mm: Reject memory removal that splits a kernel leaf mapping

 arch/arm64/mm/mmu.c | 236 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 216 insertions(+), 20 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/2 arm64/mm: Enable batched TLB flush in unmap_hotplug_range()

During a memory hot remove operation, both linear and vmemmap mappings for the memory range being removed, get unmapped via unmap_hotplug_range() but mapped pages get freed only for vmemmap mapping. This is just a sequential operation where each table entry gets cleared, followed by a leaf specific TLB flush, and then followed by memory free operation when applicable.

This approach was simple and uniform both for vmemmap and linear mappings. But linear mapping might contain CONT marked block memory where it becomes necessary to first clear out all entire in the range before a TLB flush. This is as per the architecture requirement. Hence batch all TLB flushes during the table tear down walk and finally do it in unmap_hotplug_range().

Prior to this fix, it was hypothetically possible for a speculative access to a higher address in the contiguous block to fill the TLB with shattered entries for the entire contiguous range after a lower address had already been cleared and invalidated. Due to the table entries being shattered, the subsequent TLB invalidation for the higher address would not then clear the TLB entries for the lower address, meaning stale TLB entries could persist.

Besides it also helps in improving the performance via TLBI range operation along with reduced synchronization instructions. The time spent executing unmap_hotplug_range() improved 97% measured over a 2GB memory hot removal in KVM guest.

This scheme is not applicable during vmemmap mapping tear down where memory needs to be freed and hence a TLB flush is required after clearing out page table entry.

---
 arch/arm64/mm/mmu.c | 81 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 67 insertions(+), 14 deletions(-)
arch/arm64/mm/mmu.c +67 -14
index a6a00accf4f9..dfb61d218579 100644
@@ -1458,10 +1458,32 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
 
 		WARN_ON(!pte_present(pte));
 		__pte_clear(&init_mm, addr, ptep);
-		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
-		if (free_mapped)
+		if (free_mapped) {
+			/*
+			 * If page is part of an existing contiguous
+			 * memory block, individual TLB invalidation
+			 * here would not be appropriate. Instead it
+			 * will require clearing all entries for the
+			 * memory block and subsequently a TLB flush
+			 * for the entire range.
+			 */
+			WARN_ON(pte_cont(pte));
+
+			/*
+			 * TLB flush is essential for freeing memory.
+			 */
+			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 			free_hotplug_page_range(pte_page(pte),
 						PAGE_SIZE, altmap);
+		}
+
+		/*
+		 * TLB flush is batched in unmap_hotplug_range()
+		 * for the entire range, when memory need not be
+		 * freed. Besides linear mapping might have CONT
+		 * blocks where TLB flush needs to be done after
+		 * clearing all relevant entries.
+		 */
 	} while (addr += PAGE_SIZE, addr < end);
 }
 
@@ -1482,15 +1504,32 @@ static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
 		WARN_ON(!pmd_present(pmd));
 		if (pmd_sect(pmd)) {
 			pmd_clear(pmdp);
+			if (free_mapped) {
+				/*
+				 * If page is part of an existing contiguous
+				 * memory block, individual TLB invalidation
+				 * here would not be appropriate. Instead it
+				 * will require clearing all entries for the
+				 * memory block and subsequently a TLB flush
+				 * for the entire range.
+				 */
+				WARN_ON(pmd_cont(pmd));
+
+				/*
+				 * TLB flush is essential for freeing memory.
+				 */
+				flush_tlb_kernel_range(addr, addr + PMD_SIZE);
+				free_hotplug_page_range(pmd_page(pmd),
+							PMD_SIZE, altmap);
+			}
 
 			/*
-			 * One TLBI should be sufficient here as the PMD_SIZE
-			 * range is mapped with a single block entry.
+			 * TLB flush is batched in unmap_hotplug_range()
+			 * for the entire range, when memory need not be
+			 * freed. Besides linear mapping might have CONT
+			 * blocks where TLB flush needs to be done after
+			 * clearing all relevant entries.
 			 */
-			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
-			if (free_mapped)
-				free_hotplug_page_range(pmd_page(pmd),
-							PMD_SIZE, altmap);
 			continue;
 		}
 		WARN_ON(!pmd_table(pmd));
@@ -1515,15 +1554,20 @@ static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
 		WARN_ON(!pud_present(pud));
 		if (pud_sect(pud)) {
 			pud_clear(pudp);
+			if (free_mapped) {
+				/*
+				 * TLB flush is essential for freeing memory.
+				 */
+				flush_tlb_kernel_range(addr, addr + PUD_SIZE);
+				free_hotplug_page_range(pud_page(pud),
+							PUD_SIZE, altmap);
+			}
 
 			/*
-			 * One TLBI should be sufficient here as the PUD_SIZE
-			 * range is mapped with a single block entry.
+			 * TLB flush is batched in unmap_hotplug_range()
+			 * for the entire range, when memory need not be
+			 * freed.
 			 */
-			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
-			if (free_mapped)
-				free_hotplug_page_range(pud_page(pud),
-							PUD_SIZE, altmap);
 			continue;
 		}
 		WARN_ON(!pud_table(pud));
@@ -1553,6 +1597,7 @@ static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 				bool free_mapped, struct vmem_altmap *altmap)
 {
+	unsigned long start = addr;
 	unsigned long next;
 	pgd_t *pgdp, pgd;
 
@@ -1574,6 +1619,14 @@ static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 		WARN_ON(!pgd_present(pgd));
 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
 	} while (addr = next, addr < end);
+
+	/*
+	 * Batched TLB flush only for linear mapping which
+	 * might contain CONT blocks, and does not require
+	 * freeing up memory as well.
+	 */
+	if (!free_mapped)
+		flush_tlb_kernel_range(start, end);
 }
 
 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
Signature · 1 line
2.30.2
#2 lore raw
Quoted 51 lines · Anshuman Khandual

During a memory hot remove operation, both linear and vmemmap mappings for the memory range being removed, get unmapped via unmap_hotplug_range() but mapped pages get freed only for vmemmap mapping. This is just a sequential operation where each table entry gets cleared, followed by a leaf specific TLB flush, and then followed by memory free operation when applicable.

This approach was simple and uniform both for vmemmap and linear mappings. But linear mapping might contain CONT marked block memory where it becomes necessary to first clear out all entire in the range before a TLB flush. This is as per the architecture requirement. Hence batch all TLB flushes during the table tear down walk and finally do it in unmap_hotplug_range().

Prior to this fix, it was hypothetically possible for a speculative access to a higher address in the contiguous block to fill the TLB with shattered entries for the entire contiguous range after a lower address had already been cleared and invalidated. Due to the table entries being shattered, the subsequent TLB invalidation for the higher address would not then clear the TLB entries for the lower address, meaning stale TLB entries could persist.

Besides it also helps in improving the performance via TLBI range operation along with reduced synchronization instructions. The time spent executing unmap_hotplug_range() improved 97% measured over a 2GB memory hot removal in KVM guest.

This scheme is not applicable during vmemmap mapping tear down where memory needs to be freed and hence a TLB flush is required after clearing out page table entry.

---
 arch/arm64/mm/mmu.c | 81 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 67 insertions(+), 14 deletions(-)
arch/arm64/mm/mmu.c +0 -1
index a6a00accf4f9..dfb61d218579 100644
@@ -1458,10 +1458,32 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
 
 		WARN_ON(!pte_present(pte));
 		__pte_clear(&init_mm, addr, ptep);
-		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);

arch/arm64/mm/mmu.c static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, · line 1469

Quoted 10 lines
arch/arm64/mm/mmu.c +9 -1
-		if (free_mapped)
+		if (free_mapped) {
+			/*
+			 * If page is part of an existing contiguous
+			 * memory block, individual TLB invalidation
+			 * here would not be appropriate. Instead it
+			 * will require clearing all entries for the
+			 * memory block and subsequently a TLB flush
+			 * for the entire range.
+			 */

I'm not sure about repeating these longish comments a couple of times :)

For example, I think you can drop the ones regarding the TLB flush ("TLB flush is batched in unmap_hotplug_range ...") completely. unmap_hotplug_range(), the only caller, is pretty clear about that. And anybody reading that code should be able to spot the "!free_mapped" case easily.

Alternatively, just say

	/* unmap_hotplug_range() flushes TLB for !free_mapped */

"TLB flush is essential for freeing memory." is rather obvious when freeing memory, so I would drop that as well.

Regarding pte_cont(), can we shorten that to

	/* CONT blocks in the vmemmap are not supported. */

Anybody who wants to figure *why* can lookup your patch where you add that comment+check.

I did not check whether people suggested to add these comments in previous versions. But to me they don't add a lot of real value that couldn't be had from the code already (or common sense: freeing requires prior TLB flush).

Quoted 9 lines
arch/arm64/mm/mmu.c +7 -0
+			WARN_ON(pte_cont(pte));
+
+			/*
+			 * TLB flush is essential for freeing memory.
+			 */
+			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 			free_hotplug_page_range(pte_page(pte),
 						PAGE_SIZE, altmap);
+		}

[...]

Quoted 10 lines
arch/arm64/mm/mmu.c +1 -0
 		WARN_ON(!pud_table(pud));
@@ -1553,6 +1597,7 @@ static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 				bool free_mapped, struct vmem_altmap *altmap)
 {
+	unsigned long start = addr;
 	unsigned long next;
 	pgd_t *pgdp, pgd;
 

arch/arm64/mm/mmu.c static void unmap_hotplug_range(unsigned long addr, unsigned long end, · line 1627

Quoted 9 lines
arch/arm64/mm/mmu.c +6 -0
@@ -1574,6 +1619,14 @@ static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 		WARN_ON(!pgd_present(pgd));
 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
 	} while (addr = next, addr < end);
+
+	/*
+	 * Batched TLB flush only for linear mapping which
+	 * might contain CONT blocks, and does not require
+	 * freeing up memory as well.
+	 */

Also, here, I don't think the comment really adds value.

* !free_mapped -> linear mapping, no freeing of memory
* CONT blocks -> irrelevant, you can batch in either case

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+	if (!free_mapped)
+		flush_tlb_kernel_range(start, end);
 }
Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 71 lines · David Hildenbrand (Arm)
Quoted 51 lines · Anshuman Khandual

During a memory hot remove operation, both linear and vmemmap mappings for the memory range being removed, get unmapped via unmap_hotplug_range() but mapped pages get freed only for vmemmap mapping. This is just a sequential operation where each table entry gets cleared, followed by a leaf specific TLB flush, and then followed by memory free operation when applicable.

This approach was simple and uniform both for vmemmap and linear mappings. But linear mapping might contain CONT marked block memory where it becomes necessary to first clear out all entire in the range before a TLB flush. This is as per the architecture requirement. Hence batch all TLB flushes during the table tear down walk and finally do it in unmap_hotplug_range().

Prior to this fix, it was hypothetically possible for a speculative access to a higher address in the contiguous block to fill the TLB with shattered entries for the entire contiguous range after a lower address had already been cleared and invalidated. Due to the table entries being shattered, the subsequent TLB invalidation for the higher address would not then clear the TLB entries for the lower address, meaning stale TLB entries could persist.

Besides it also helps in improving the performance via TLBI range operation along with reduced synchronization instructions. The time spent executing unmap_hotplug_range() improved 97% measured over a 2GB memory hot removal in KVM guest.

This scheme is not applicable during vmemmap mapping tear down where memory needs to be freed and hence a TLB flush is required after clearing out page table entry.

---
 arch/arm64/mm/mmu.c | 81 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 67 insertions(+), 14 deletions(-)
arch/arm64/mm/mmu.c +0 -1
index a6a00accf4f9..dfb61d218579 100644
@@ -1458,10 +1458,32 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
 
 		WARN_ON(!pte_present(pte));
 		__pte_clear(&init_mm, addr, ptep);
-		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);

arch/arm64/mm/mmu.c static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, · line 1469

Quoted 10 lines
arch/arm64/mm/mmu.c +9 -1
-		if (free_mapped)
+		if (free_mapped) {
+			/*
+			 * If page is part of an existing contiguous
+			 * memory block, individual TLB invalidation
+			 * here would not be appropriate. Instead it
+			 * will require clearing all entries for the
+			 * memory block and subsequently a TLB flush
+			 * for the entire range.
+			 */

I'm not sure about repeating these longish comments a couple of times :)

For example, I think you can drop the ones regarding the TLB flush ("TLB flush is batched in unmap_hotplug_range ...") completely. unmap_hotplug_range(), the only caller, is pretty clear about that. And anybody reading that code should be able to spot the "!free_mapped" case easily.

Quoted 3 lines

Alternatively, just say

	/* unmap_hotplug_range() flushes TLB for !free_mapped */

Will replace as suggested.

Quoted 10 lines

"TLB flush is essential for freeing memory." is rather obvious when freeing memory, so I would drop that as well.

Regarding pte_cont(), can we shorten that to

	/* CONT blocks in the vmemmap are not supported. */

Anybody who wants to figure *why* can lookup your patch where you add that comment+check.

Sure will drop the comment about CONT mapping TLB flush and mention that such mappings are not supported for vmemmap.

Quoted 6 lines

I did not check whether people suggested to add these comments in previous versions. But to me they don't add a lot of real value that couldn't be had from the code already (or common sense: freeing requires prior TLB flush).

Dropped all these comments - "TLB flush is essential for freeing memory".

Quoted 35 lines
Quoted 10 lines
arch/arm64/mm/mmu.c +7 -0
+			WARN_ON(pte_cont(pte));
+
+			/*
+			 * TLB flush is essential for freeing memory.
+			 */
+			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 			free_hotplug_page_range(pte_page(pte),
 						PAGE_SIZE, altmap);
+		}

[...]

Quoted 10 lines
arch/arm64/mm/mmu.c +1 -0
 		WARN_ON(!pud_table(pud));
@@ -1553,6 +1597,7 @@ static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 				bool free_mapped, struct vmem_altmap *altmap)
 {
+	unsigned long start = addr;
 	unsigned long next;
 	pgd_t *pgdp, pgd;
 

arch/arm64/mm/mmu.c static void unmap_hotplug_range(unsigned long addr, unsigned long end, · line 1627

Quoted 9 lines
arch/arm64/mm/mmu.c +6 -0
@@ -1574,6 +1619,14 @@ static void unmap_hotplug_range(unsigned long addr, unsigned long end,
 		WARN_ON(!pgd_present(pgd));
 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
 	} while (addr = next, addr < end);
+
+	/*
+	 * Batched TLB flush only for linear mapping which
+	 * might contain CONT blocks, and does not require
+	 * freeing up memory as well.
+	 */

Also, here, I don't think the comment really adds value.

Quoted 2 lines

* !free_mapped -> linear mapping, no freeing of memory
* CONT blocks -> irrelevant, you can batch in either case

Dropped the comment.

Quoted 4 lines
Quoted 4 lines
arch/arm64/mm/mmu.c +2 -0
+	if (!free_mapped)
+		flush_tlb_kernel_range(start, end);
 }
#4 lore raw

PATCH v3 2/2 arm64/mm: Reject memory removal that splits a kernel leaf mapping

In reply to Anshuman Khandual

Linear and vmemmap mappings that get teared down during a memory hot remove operation might contain leaf level entries on any page table level. If the requested memory range's linear or vmemmap mappings falls within such leaf entries, new mappings need to be created for the remaining memory mapped on the leaf entry earlier, following standard break before make aka BBM rules. But kernel cannot tolerate BBM and hence remapping to fine grained leaves would not be possible on systems without BBML2_NOABORT.

Currently memory hot remove operation does not perform such restructuring, and so removing memory ranges that could split a kernel leaf level mapping need to be rejected.

While memory_hotplug.c does appear to permit hot removing arbitrary ranges of memory, the higher layers that drive memory_hotplug (e.g. ACPI, virtio, ...) all appear to treat memory as fixed size devices. So it is impossible to hot unplug a different amount than was previously hot plugged, and hence we should never see a rejection in practice, but adding the check makes us robust against a future change.

---
 arch/arm64/mm/mmu.c | 155 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 149 insertions(+), 6 deletions(-)
arch/arm64/mm/mmu.c +149 -6
index dfb61d218579..ce0b966f42d1 100644
@@ -2063,6 +2063,142 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
 }
 
+
+static bool addr_splits_kernel_leaf(unsigned long addr)
+{
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
+	pte_t *ptep, pte;
+
+	/*
+	 * PGD level:
+	 *
+	 * If addr is PGD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr)
+		return false;
+
+	pgdp = pgd_offset_k(addr);
+	pgd = pgdp_get(pgdp);
+	if (!pgd_present(pgd))
+		return false;
+
+	/*
+	 * P4D level:
+	 *
+	 * If addr is P4D_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, P4D_SIZE) == addr)
+		return false;
+
+	p4dp = p4d_offset(pgdp, addr);
+	p4d = p4dp_get(p4dp);
+	if (!p4d_present(p4d))
+		return false;
+
+	/*
+	 * PUD level:
+	 *
+	 * If addr is PUD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PUD_SIZE) == addr)
+		return false;
+
+	pudp = pud_offset(p4dp, addr);
+	pud = pudp_get(pudp);
+	if (!pud_present(pud))
+		return false;
+
+	if (pud_leaf(pud))
+		return true;
+
+	/*
+	 * CONT_PMD level:
+	 *
+	 * If addr is CONT_PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr)
+		return false;
+
+	pmdp = pmd_offset(pudp, addr);
+	pmd = pmdp_get(pmdp);
+	if (!pmd_present(pmd))
+		return false;
+
+	if (pmd_cont(pmd))
+		return true;
+
+	/*
+	 * PMD level:
+	 *
+	 * If addr is PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PMD_SIZE) == addr)
+		return false;
+
+	if (pmd_leaf(pmd))
+		return true;
+
+	/*
+	 * CONT_PTE level:
+	 *
+	 * If addr is CONT_PTE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr)
+		return false;
+
+	ptep = pte_offset_kernel(pmdp, addr);
+	pte = __ptep_get(ptep);
+	if (!pte_present(pte))
+		return false;
+
+	if (pte_cont(pte))
+		return true;
+
+	/*
+	 * PTE level:
+	 *
+	 * If addr is PAGE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PAGE_SIZE) == addr)
+		return false;
+	return true;
+}
+
+static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages)
+{
+	unsigned long phys_start, phys_end, size, start, end;
+
+	phys_start = PFN_PHYS(pfn);
+	phys_end = phys_start + nr_pages * PAGE_SIZE;
+
+	/*
+	 * PFN range's linear map edges are leaf entry aligned
+	 */
+	start = __phys_to_virt(phys_start);
+	end =  __phys_to_virt(phys_end);
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in linear map\n",
+			phys_start, phys_end);
+		return false;
+	}
+
+	/*
+	 * PFN range's vmemmap edges are leaf entry aligned
+	 */
+	size = nr_pages * sizeof(struct page);
+	start = (unsigned long)pfn_to_page(pfn);
+	end = start + size;
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in vmemmap\n",
+			phys_start, phys_end);
+		return false;
+	}
+	return true;
+}
+
 /*
  * This memory hotplug notifier helps prevent boot memory from being
  * inadvertently removed as it blocks pfn range offlining process in
@@ -2071,8 +2207,11 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
  * In future if and when boot memory could be removed, this notifier
  * should be dropped and free_hotplug_page_range() should handle any
  * reserved pages allocated during boot.
+ *
+ * This also blocks any memory remove that would have caused a split
+ * in leaf entry in kernel linear or vmemmap mapping.
  */
-static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
+static int prevent_memory_remove_notifier(struct notifier_block *nb,
 					   unsigned long action, void *data)
 {
 	struct mem_section *ms;
@@ -2118,11 +2257,15 @@ static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
 			return NOTIFY_DONE;
 		}
 	}
+
+	if (!can_unmap_without_split(pfn, arg->nr_pages))
+		return NOTIFY_BAD;
+
 	return NOTIFY_OK;
 }
 
-static struct notifier_block prevent_bootmem_remove_nb = {
-	.notifier_call = prevent_bootmem_remove_notifier,
+static struct notifier_block prevent_memory_remove_nb = {
+	.notifier_call = prevent_memory_remove_notifier,
 };
 
 /*
@@ -2172,7 +2315,7 @@ static void validate_bootmem_online(void)
 	}
 }
 
-static int __init prevent_bootmem_remove_init(void)
+static int __init prevent_memory_remove_init(void)
 {
 	int ret = 0;
 
@@ -2180,13 +2323,13 @@ static int __init prevent_bootmem_remove_init(void)
 		return ret;
 
 	validate_bootmem_online();
-	ret = register_memory_notifier(&prevent_bootmem_remove_nb);
+	ret = register_memory_notifier(&prevent_memory_remove_nb);
 	if (ret)
 		pr_err("%s: Notifier registration failed %d\n", __func__, ret);
 
 	return ret;
 }
-early_initcall(prevent_bootmem_remove_init);
+early_initcall(prevent_memory_remove_init);
 #endif
 
 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH V3 2/2] arm64/mm: Reject memory removal that splits a kernel leaf mapping

Quoted 1 line · Anshuman Khandual

Linear and vmemmap mappings that get teared down during a memory hot remove

s/teared/torn/ ?

Quoted 11 lines

operation might contain leaf level entries on any page table level. If the requested memory range's linear or vmemmap mappings falls within such leaf entries, new mappings need to be created for the remaining memory mapped on the leaf entry earlier, following standard break before make aka BBM rules. But kernel cannot tolerate BBM and hence remapping to fine grained leaves would not be possible on systems without BBML2_NOABORT.

Currently memory hot remove operation does not perform such restructuring, and so removing memory ranges that could split a kernel leaf level mapping need to be rejected.

Quoted 6 lines

While memory_hotplug.c does appear to permit hot removing arbitrary ranges of memory, the higher layers that drive memory_hotplug (e.g. ACPI, virtio, ...) all appear to treat memory as fixed size devices. So it is impossible to hot unplug a different amount than was previously hot plugged, and hence we should never see a rejection in practice, but adding the check makes us robust against a future change.

Is this then really a fix that warrens?

Fixes bbd6ec605c0f ("arm64/mm: Enable memory hot remove")

... if nothing is broken?

I can understand the desire for this check, but I am confused about Fixes:

Quoted 23 lines
---
 arch/arm64/mm/mmu.c | 155 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 149 insertions(+), 6 deletions(-)
arch/arm64/mm/mmu.c +1 -0
index dfb61d218579..ce0b966f42d1 100644
@@ -2063,6 +2063,142 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
 }
 
+

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2076

Quoted 10 lines
arch/arm64/mm/mmu.c +10 -0
+static bool addr_splits_kernel_leaf(unsigned long addr)
+{
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
+	pte_t *ptep, pte;
+
+	/*
+	 * PGD level:

I consider all these "XXX level:" comments unhelpful. It's mostly there, in the code already: PGDIR_SIZE, PGDIR_SIZE, PUD_SIZE ... :)

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2078

Quoted 2 lines
arch/arm64/mm/mmu.c +2 -0
+	 *
+	 * If addr is PGD_SIZE aligned - already on a leaf boundary

Similarly, repeating that is not particularly helpful.

You could have a single comment at the very top that says

"If the given address points at a the start address of a possible leaf, we certainly won't split. Otherwise, check if we would actually split a leaf by traversing the page tables further."

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2080

Quoted 2 lines
arch/arm64/mm/mmu.c +2 -0
+	 */
+	if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr)

if (IS_ALIGNED(addr, PGDIR_SIZE))

	return false;

?

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2086

Quoted 6 lines
arch/arm64/mm/mmu.c +6 -0
+		return false;
+
+	pgdp = pgd_offset_k(addr);
+	pgd = pgdp_get(pgdp);
+	if (!pgd_present(pgd))
+		return false;

How could we end up with non-present areas in a range we hotplugged earlier?

Quoted 71 lines
arch/arm64/mm/mmu.c +71 -0
+
+	/*
+	 * P4D level:
+	 *
+	 * If addr is P4D_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, P4D_SIZE) == addr)
+		return false;
+
+	p4dp = p4d_offset(pgdp, addr);
+	p4d = p4dp_get(p4dp);
+	if (!p4d_present(p4d))
+		return false;
+
+	/*
+	 * PUD level:
+	 *
+	 * If addr is PUD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PUD_SIZE) == addr)
+		return false;
+
+	pudp = pud_offset(p4dp, addr);
+	pud = pudp_get(pudp);
+	if (!pud_present(pud))
+		return false;
+
+	if (pud_leaf(pud))
+		return true;
+
+	/*
+	 * CONT_PMD level:
+	 *
+	 * If addr is CONT_PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr)
+		return false;
+
+	pmdp = pmd_offset(pudp, addr);
+	pmd = pmdp_get(pmdp);
+	if (!pmd_present(pmd))
+		return false;
+
+	if (pmd_cont(pmd))
+		return true;
+
+	/*
+	 * PMD level:
+	 *
+	 * If addr is PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PMD_SIZE) == addr)
+		return false;
+
+	if (pmd_leaf(pmd))
+		return true;
+
+	/*
+	 * CONT_PTE level:
+	 *
+	 * If addr is CONT_PTE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr)
+		return false;
+
+	ptep = pte_offset_kernel(pmdp, addr);
+	pte = __ptep_get(ptep);
+	if (!pte_present(pte))
+		return false;
+
+	if (pte_cont(pte))

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2167

Quoted 10 lines
arch/arm64/mm/mmu.c +10 -0
+		return true;
+
+	/*
+	 * PTE level:
+	 *
+	 * If addr is PAGE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PAGE_SIZE) == addr)
+		return false;
+	return true;
	return !IS_ALIGNED(addr, PAGE_SIZE);
Quoted 16 lines
arch/arm64/mm/mmu.c +16 -0
+}
+
+static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages)
+{
+	unsigned long phys_start, phys_end, size, start, end;
+
+	phys_start = PFN_PHYS(pfn);
+	phys_end = phys_start + nr_pages * PAGE_SIZE;
+
+	/*
+	 * PFN range's linear map edges are leaf entry aligned
+	 */
+	start = __phys_to_virt(phys_start);
+	end =  __phys_to_virt(phys_end);
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in linear map\n",

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2193

Quoted 10 lines
arch/arm64/mm/mmu.c +10 -0
+			phys_start, phys_end);
+		return false;
+	}
+
+	/*
+	 * PFN range's vmemmap edges are leaf entry aligned
+	 */
+	size = nr_pages * sizeof(struct page);
+	start = (unsigned long)pfn_to_page(pfn);
+	end = start + size;

As arm64 cannot be used with CONFIG_SPARSEMEM (only with CONFIG_SPARSEMEM_VMEMMAP, as it sets SPARSEMEM_VMEMMAP_ENABLE), I think you can just do

	start = (unsigned long)pfn_to_page(pfn);
	end = (unsigned long)pfn_to_page(pfn + nr_pages);

As pfn_to_page() is just simple calculation that works even if "pfn+nr_pages" does not exist.

Quoted 8 lines
arch/arm64/mm/mmu.c +8 -0
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in vmemmap\n",
+			phys_start, phys_end);
+		return false;
+	}
+	return true;
+}
+
Signature · 3 lines
Cheers,

David
#6 lore raw

Re: [PATCH V3 2/2] arm64/mm: Reject memory removal that splits a kernel leaf mapping

Quoted 4 lines · David Hildenbrand (Arm)
Quoted 1 line · Anshuman Khandual

Linear and vmemmap mappings that get teared down during a memory hot remove

s/teared/torn/ ?

Done !

Quoted 19 lines
Quoted 12 lines

operation might contain leaf level entries on any page table level. If the requested memory range's linear or vmemmap mappings falls within such leaf entries, new mappings need to be created for the remaining memory mapped on the leaf entry earlier, following standard break before make aka BBM rules. But kernel cannot tolerate BBM and hence remapping to fine grained leaves would not be possible on systems without BBML2_NOABORT.

Currently memory hot remove operation does not perform such restructuring, and so removing memory ranges that could split a kernel leaf level mapping need to be rejected.

Quoted 6 lines

While memory_hotplug.c does appear to permit hot removing arbitrary ranges of memory, the higher layers that drive memory_hotplug (e.g. ACPI, virtio, ...) all appear to treat memory as fixed size devices. So it is impossible to hot unplug a different amount than was previously hot plugged, and hence we should never see a rejection in practice, but adding the check makes us robust against a future change.

Quoted 8 lines

Is this then really a fix that warrens?

Fixes bbd6ec605c0f ("arm64/mm: Enable memory hot remove")

... if nothing is broken?

I can understand the desire for this check, but I am confused about Fixes:

Probably nothing is broken now I guess but the original memory hot remove patch should have taken care of this scenario. Although don't have strong opinions either way. We could drop both "Fixes" and "Closes" tags here if that is preferred.

Quoted 24 lines
Quoted 24 lines
---
 arch/arm64/mm/mmu.c | 155 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 149 insertions(+), 6 deletions(-)
arch/arm64/mm/mmu.c +0 -0
index dfb61d218579..ce0b966f42d1 100644
@@ -2063,6 +2063,142 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
 }
 
Quoted 14 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2076

Quoted 11 lines
arch/arm64/mm/mmu.c +11 -0
+
+static bool addr_splits_kernel_leaf(unsigned long addr)
+{
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
+	pte_t *ptep, pte;
+
+	/*
+	 * PGD level:

I consider all these "XXX level:" comments unhelpful. It's mostly there, in the code already: PGDIR_SIZE, PGDIR_SIZE, PUD_SIZE ... :)

Got it.

Quoted 11 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2078

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+	 *
+	 * If addr is PGD_SIZE aligned - already on a leaf boundary

Similarly, repeating that is not particularly helpful.

You could have a single comment at the very top that says

"If the given address points at a the start address of a possible leaf, we certainly won't split. Otherwise, check if we would actually split a leaf by traversing the page tables further."

Will drop all the existing comments for each level and instead add the above unified comment as suggested.

Quoted 8 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2080

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+	 */
+	if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr)

if (IS_ALIGNED(addr, PGDIR_SIZE))

	return false;

?

Looks cleaner, will convert all ALIGN_DOWN() level checks into IS_ALIGNED().

Quoted 9 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2086

Quoted 7 lines
arch/arm64/mm/mmu.c +6 -0
+		return false;
+
+	pgdp = pgd_offset_k(addr);
+	pgd = pgdp_get(pgdp);
+	if (!pgd_present(pgd))
+		return false;

How could we end up with non-present areas in a range we hotplugged earlier?

We might not in reality but in order to be sure just an additional protection.

Quoted 116 lines
Quoted 72 lines
arch/arm64/mm/mmu.c +71 -0
+
+	/*
+	 * P4D level:
+	 *
+	 * If addr is P4D_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, P4D_SIZE) == addr)
+		return false;
+
+	p4dp = p4d_offset(pgdp, addr);
+	p4d = p4dp_get(p4dp);
+	if (!p4d_present(p4d))
+		return false;
+
+	/*
+	 * PUD level:
+	 *
+	 * If addr is PUD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PUD_SIZE) == addr)
+		return false;
+
+	pudp = pud_offset(p4dp, addr);
+	pud = pudp_get(pudp);
+	if (!pud_present(pud))
+		return false;
+
+	if (pud_leaf(pud))
+		return true;
+
+	/*
+	 * CONT_PMD level:
+	 *
+	 * If addr is CONT_PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr)
+		return false;
+
+	pmdp = pmd_offset(pudp, addr);
+	pmd = pmdp_get(pmdp);
+	if (!pmd_present(pmd))
+		return false;
+
+	if (pmd_cont(pmd))
+		return true;
+
+	/*
+	 * PMD level:
+	 *
+	 * If addr is PMD_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PMD_SIZE) == addr)
+		return false;
+
+	if (pmd_leaf(pmd))
+		return true;
+
+	/*
+	 * CONT_PTE level:
+	 *
+	 * If addr is CONT_PTE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr)
+		return false;
+
+	ptep = pte_offset_kernel(pmdp, addr);
+	pte = __ptep_get(ptep);
+	if (!pte_present(pte))
+		return false;
+
+	if (pte_cont(pte))

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2167

Quoted 10 lines
arch/arm64/mm/mmu.c +10 -0
+		return true;
+
+	/*
+	 * PTE level:
+	 *
+	 * If addr is PAGE_SIZE aligned - already on a leaf boundary
+	 */
+	if (ALIGN_DOWN(addr, PAGE_SIZE) == addr)
+		return false;
+	return true;
	return !IS_ALIGNED(addr, PAGE_SIZE);
Quoted 16 lines
arch/arm64/mm/mmu.c +16 -0
+}
+
+static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages)
+{
+	unsigned long phys_start, phys_end, size, start, end;
+
+	phys_start = PFN_PHYS(pfn);
+	phys_end = phys_start + nr_pages * PAGE_SIZE;
+
+	/*
+	 * PFN range's linear map edges are leaf entry aligned
+	 */
+	start = __phys_to_virt(phys_start);
+	end =  __phys_to_virt(phys_end);
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in linear map\n",

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2193

Quoted 10 lines
arch/arm64/mm/mmu.c +10 -0
+			phys_start, phys_end);
+		return false;
+	}
+
+	/*
+	 * PFN range's vmemmap edges are leaf entry aligned
+	 */
+	size = nr_pages * sizeof(struct page);
+	start = (unsigned long)pfn_to_page(pfn);
+	end = start + size;

As arm64 cannot be used with CONFIG_SPARSEMEM (only with CONFIG_SPARSEMEM_VMEMMAP, as it sets SPARSEMEM_VMEMMAP_ENABLE), I think you can just do

Quoted 4 lines
	start = (unsigned long)pfn_to_page(pfn);
	end = (unsigned long)pfn_to_page(pfn + nr_pages);

As pfn_to_page() is just simple calculation that works even if "pfn+nr_pages" does not exist.

Sure, will drop the element 'size' and derive 'end' as suggested above via pfn_to_page().

Quoted 10 lines
Quoted 9 lines
arch/arm64/mm/mmu.c +8 -0
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in vmemmap\n",
+			phys_start, phys_end);
+		return false;
+	}
+	return true;
+}
+
#7 lore raw

Re: [PATCH V3 2/2] arm64/mm: Reject memory removal that splits a kernel leaf mapping

Quoted 7 lines
Quoted 1 line

I can understand the desire for this check, but I am confused about Fixes:

Probably nothing is broken now I guess but the original memory hot remove patch should have taken care of this scenario. Although don't have strong opinions either way. We could drop both "Fixes" and "Closes" tags here if that is preferred.

We tend to only tag actual fixes. If we consider this a possible fix, we should ask ourselves whether this would be stable material.

...

Quoted 10 lines
Quoted 8 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2086

Quoted 6 lines
arch/arm64/mm/mmu.c +6 -0
+		return false;
+
+	pgdp = pgd_offset_k(addr);
+	pgd = pgdp_get(pgdp);
+	if (!pgd_present(pgd))
+		return false;

How could we end up with non-present areas in a range we hotplugged earlier?

We might not in reality but in order to be sure just an additional protection.

I'm rather wondering if this would indicate a real bug somewhere else that we would silently swallow.

Anyhow, no real preference from my side, just something I considered weird.

[...]

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
arch/arm64/mm/mmu.c +21 -0
+
+static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages)
+{
+	unsigned long phys_start, phys_end, size, start, end;
+
+	phys_start = PFN_PHYS(pfn);
+	phys_end = phys_start + nr_pages * PAGE_SIZE;
+
+	/*
+	 * PFN range's linear map edges are leaf entry aligned
+	 */
+	start = __phys_to_virt(phys_start);
+	end =  __phys_to_virt(phys_end);
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in linear map\n",
+			phys_start, phys_end);
+		return false;
+	}
+
+	/*
+	 * PFN range's vmemmap edges are leaf entry aligned
Quoted 8 lines
Quoted 8 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2193

Quoted 4 lines
arch/arm64/mm/mmu.c +4 -0
+	 */
+	size = nr_pages * sizeof(struct page);
+	start = (unsigned long)pfn_to_page(pfn);
+	end = start + size;

As arm64 cannot be used with CONFIG_SPARSEMEM (only with CONFIG_SPARSEMEM_VMEMMAP, as it sets SPARSEMEM_VMEMMAP_ENABLE), I think you can just do

Just to clarify:

What I meant here is: with CONFIG_SPARSEMEM but without CONFIG_SPARSEMEM_VMEMMAP.

Signature · 3 lines
Cheers,

David
#8 lore raw

Re: [PATCH V3 2/2] arm64/mm: Reject memory removal that splits a kernel leaf mapping

Quoted 10 lines · David Hildenbrand (Arm)
Quoted 7 lines
Quoted 1 line

I can understand the desire for this check, but I am confused about Fixes:

Probably nothing is broken now I guess but the original memory hot remove patch should have taken care of this scenario. Although don't have strong opinions either way. We could drop both "Fixes" and "Closes" tags here if that is preferred.

We tend to only tag actual fixes. If we consider this a possible fix, we should ask ourselves whether this would be stable material.

Ryan had earlier asked for the Cc: stable to be dropped as this was not an actual fix. But seems like we should drop these Fixes/Closes tags as well.

Quoted 51 lines

...

Quoted 10 lines
Quoted 8 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2086

Quoted 6 lines
arch/arm64/mm/mmu.c +6 -0
+		return false;
+
+	pgdp = pgd_offset_k(addr);
+	pgd = pgdp_get(pgdp);
+	if (!pgd_present(pgd))
+		return false;

How could we end up with non-present areas in a range we hotplugged earlier?

We might not in reality but in order to be sure just an additional protection.

I'm rather wondering if this would indicate a real bug somewhere else that we would silently swallow.

Anyhow, no real preference from my side, just something I considered weird.

[...]

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
arch/arm64/mm/mmu.c +21 -0
+
+static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages)
+{
+	unsigned long phys_start, phys_end, size, start, end;
+
+	phys_start = PFN_PHYS(pfn);
+	phys_end = phys_start + nr_pages * PAGE_SIZE;
+
+	/*
+	 * PFN range's linear map edges are leaf entry aligned
+	 */
+	start = __phys_to_virt(phys_start);
+	end =  __phys_to_virt(phys_end);
+	if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) {
+		pr_warn("[%lx %lx] splits a leaf entry in linear map\n",
+			phys_start, phys_end);
+		return false;
+	}
+
+	/*
+	 * PFN range's vmemmap edges are leaf entry aligned
Quoted 8 lines
Quoted 8 lines

arch/arm64/mm/mmu.c void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) · line 2193

Quoted 4 lines
arch/arm64/mm/mmu.c +4 -0
+	 */
+	size = nr_pages * sizeof(struct page);
+	start = (unsigned long)pfn_to_page(pfn);
+	end = start + size;

As arm64 cannot be used with CONFIG_SPARSEMEM (only with CONFIG_SPARSEMEM_VMEMMAP, as it sets SPARSEMEM_VMEMMAP_ENABLE), I think you can just do

Quoted 4 lines

Just to clarify:

What I meant here is: with CONFIG_SPARSEMEM but without CONFIG_SPARSEMEM_VMEMMAP.

Alright but does the commit message or pfn_to_page() code block here needs a comment about this ? OR it is apparent enough ?

#9 lore raw

Re: [PATCH V3 2/2] arm64/mm: Reject memory removal that splits a kernel leaf mapping

Quoted 10 lines · Anshuman Khandual
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines

Probably nothing is broken now I guess but the original memory hot remove patch should have taken care of this scenario. Although don't have strong opinions either way. We could drop both "Fixes" and "Closes" tags here if that is preferred.

Quoted 6 lines
Quoted 2 lines

We tend to only tag actual fixes. If we consider this a possible fix, we should ask ourselves whether this would be stable material.

Ryan had earlier asked for the Cc: stable to be dropped as this was not an actual fix. But seems like we should drop these Fixes/Closes tags as well.

If there are no know problems (no bugs), then I would just use

Link instead of Closes:

And the Suggested-by:.

Quoted 15 lines
Quoted 15 lines

...

Quoted 2 lines

We might not in reality but in order to be sure just an additional protection.

I'm rather wondering if this would indicate a real bug somewhere else that we would silently swallow.

Anyhow, no real preference from my side, just something I considered weird.

[...]

Quoted 8 lines
Quoted 4 lines

Just to clarify:

What I meant here is: with CONFIG_SPARSEMEM but without CONFIG_SPARSEMEM_VMEMMAP.

Alright but does the commit message or pfn_to_page() code block here needs a comment about this ? OR it is apparent enough ?

You could add a

	BUILD_BUG_ON(!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP));

to check and document in a single statement.

Signature · 3 lines
Cheers,

David
#10 lore raw
In reply to Anshuman Khandual
Quoted 39 lines · Anshuman Khandual

This series enables batched TLB flush in unmap_hotplug_range() which avoids individual page TLB flush for potential CONT blocks in linear mapping while also improving performance due to range based TLB operation along with less synchronization barrier instructions.

It also now rejects memory removal that might split a leaf entry in kernel mapping, which would have otherwise required re-structuring using the break before make (BBM) semantics. But kernel cannot tolerate BBM, so remapping to fine grained leaves would not be possible on systems without BBML2_NOABORT.

This series applies on 7.0-rc1 and tested on KVM guest.

Changes in V3:

- Fixed some typos in the commit message for [PATCH 2/2]
- Dropped copying [email protected] per Ryan

Changes in V2:

https://lore.kernel.org/all/[email protected]/

- Renamed split_kernel_leaf_boundary() as addr_splits_kernel_leaf()
- Dropped pte_valid()/pmd_leaf() before checking pte_cont()/pmd_cont()
- Changed and dropped variables in can_unmap_without_split()
- Moved can_unmap_without_split() after boot memory check
- Renamed 'prevent_bootmem' as 'prevent_memory' across the board
- Updated commit message in both patches

Quoted 1 line

Changes in V1:

- https://lore.kernel.org/all/[email protected]/

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Yang Shi <[email protected]>
Cc Christoph Lameter <[email protected]>
Quoted 7 lines

Anshuman Khandual (2):
arm64/mm: Enable batched TLB flush in unmap_hotplug_range() arm64/mm: Reject memory removal that splits a kernel leaf mapping

 arch/arm64/mm/mmu.c | 236 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 216 insertions(+), 20 deletions(-)

Gentle ping. Any updates on this ?