Skip to contentSkip to conversation

KVM: arm64: Drop sort_memblock_regions()

PATCH Applied Gavin Shan 5 participants 7 messages Mar 11, 2025 – May 8, 2025

Subsystems (2)
lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH 1/1 KVM: arm64: Drop sort_memblock_regions()

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

---
 arch/arm64/kvm/pkvm.c | 19 -------------------
 1 file changed, 19 deletions(-)
arch/arm64/kvm/pkvm.c +0 -19
index 930b677eb9b0..d9c9174f89a1 100644
@@ -10,7 +10,6 @@
 #include <asm/kvm_mmu.h>
 #include <linux/memblock.h>
 #include <linux/mutex.h>
-#include <linux/sort.h>
 
 #include <asm/kvm_pkvm.h>
 
@@ -24,23 +23,6 @@ static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr);
 phys_addr_t hyp_mem_base;
 phys_addr_t hyp_mem_size;
 
-static int cmp_hyp_memblock(const void *p1, const void *p2)
-{
-	const struct memblock_region *r1 = p1;
-	const struct memblock_region *r2 = p2;
-
-	return r1->base < r2->base ? -1 : (r1->base > r2->base);
-}
-
-static void __init sort_memblock_regions(void)
-{
-	sort(hyp_memory,
-	     *hyp_memblock_nr_ptr,
-	     sizeof(struct memblock_region),
-	     cmp_hyp_memblock,
-	     NULL);
-}
-
 static int __init register_memblock_regions(void)
 {
 	struct memblock_region *reg;
@@ -52,7 +34,6 @@ static int __init register_memblock_regions(void)
 		hyp_memory[*hyp_memblock_nr_ptr] = *reg;
 		(*hyp_memblock_nr_ptr)++;
 	}
-	sort_memblock_regions();
 
 	return 0;
 }
Signature · 1 line
2.48.1
#1 lore raw
Quoted 7 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

Just wondering what prompted this change ?

Quoted 50 lines
---
 arch/arm64/kvm/pkvm.c | 19 -------------------
 1 file changed, 19 deletions(-)
arch/arm64/kvm/pkvm.c +0 -19
index 930b677eb9b0..d9c9174f89a1 100644
@@ -10,7 +10,6 @@
 #include <asm/kvm_mmu.h>
 #include <linux/memblock.h>
 #include <linux/mutex.h>
-#include <linux/sort.h>
 
 #include <asm/kvm_pkvm.h>
 
@@ -24,23 +23,6 @@ static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr);
 phys_addr_t hyp_mem_base;
 phys_addr_t hyp_mem_size;
 
-static int cmp_hyp_memblock(const void *p1, const void *p2)
-{
-	const struct memblock_region *r1 = p1;
-	const struct memblock_region *r2 = p2;
-
-	return r1->base < r2->base ? -1 : (r1->base > r2->base);
-}
-
-static void __init sort_memblock_regions(void)
-{
-	sort(hyp_memory,
-	     *hyp_memblock_nr_ptr,
-	     sizeof(struct memblock_region),
-	     cmp_hyp_memblock,
-	     NULL);
-}
-
 static int __init register_memblock_regions(void)
 {
 	struct memblock_region *reg;
@@ -52,7 +34,6 @@ static int __init register_memblock_regions(void)
 		hyp_memory[*hyp_memblock_nr_ptr] = *reg;
 		(*hyp_memblock_nr_ptr)++;
 	}
-	sort_memblock_regions();
 
 	return 0;
 }
author
#2 lore raw
Quoted 11 lines · Anshuman Khandual
Quoted 7 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

Just wondering what prompted this change ?

I found the unnecessary sorting by code inspection. Hope there is nothing I missed and it's why Quentin Perret has been copied, to confirm it. Commit a14307f5310c was introduced by the initial series [1/2] to support pKVM.

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

Thanks,
Gavin

Quoted 51 lines
Quoted 50 lines
---
  arch/arm64/kvm/pkvm.c | 19 -------------------
  1 file changed, 19 deletions(-)
arch/arm64/kvm/pkvm.c +0 -19
index 930b677eb9b0..d9c9174f89a1 100644
@@ -10,7 +10,6 @@
  #include <asm/kvm_mmu.h>
  #include <linux/memblock.h>
  #include <linux/mutex.h>
-#include <linux/sort.h>
 
  #include <asm/kvm_pkvm.h>
 
@@ -24,23 +23,6 @@ static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr);
  phys_addr_t hyp_mem_base;
  phys_addr_t hyp_mem_size;
 
-static int cmp_hyp_memblock(const void *p1, const void *p2)
-{
-	const struct memblock_region *r1 = p1;
-	const struct memblock_region *r2 = p2;
-
-	return r1->base < r2->base ? -1 : (r1->base > r2->base);
-}
-
-static void __init sort_memblock_regions(void)
-{
-	sort(hyp_memory,
-	     *hyp_memblock_nr_ptr,
-	     sizeof(struct memblock_region),
-	     cmp_hyp_memblock,
-	     NULL);
-}
-
  static int __init register_memblock_regions(void)
  {
  	struct memblock_region *reg;
@@ -52,7 +34,6 @@ static int __init register_memblock_regions(void)
  		hyp_memory[*hyp_memblock_nr_ptr] = *reg;
  		(*hyp_memblock_nr_ptr)++;
  	}
-	sort_memblock_regions();
 
  	return 0;
  }
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Gavin Shan
Quoted 5 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

Quoted 7 lines

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

---
 arch/arm64/kvm/pkvm.c | 19 -------------------
 1 file changed, 19 deletions(-)

It's not especially obvious from the memblock API that the memblocks are sorted and so for_each_mem_region() will traverse the regions in order. But it does appear to be true, so I suppose this is fine:

Will

Reviewed-by on v1 1/1
#4 lore raw
In reply to Gavin Shan
Quoted 7 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

I think this was originally introduced in an early version of the code where the reserved regions were also registered, hence requiring sorting. But yes, with the code as it is today I can't see what would break without it, so:

Thanks!
Quentin

author
#5 lore raw
In reply to Gavin Shan

Hi Marc/Oliver,

Quoted 5 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

Quoted 8 lines

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

---
  arch/arm64/kvm/pkvm.c | 19 -------------------
  1 file changed, 19 deletions(-)

Please let me know if I need to resend after a rebase, or anything I need to do so that it can be merged? :-)

Thanks,
Gavin

Quoted 44 lines
arch/arm64/kvm/pkvm.c +0 -19
index 930b677eb9b0..d9c9174f89a1 100644
@@ -10,7 +10,6 @@
  #include <asm/kvm_mmu.h>
  #include <linux/memblock.h>
  #include <linux/mutex.h>
-#include <linux/sort.h>
 
  #include <asm/kvm_pkvm.h>
 
@@ -24,23 +23,6 @@ static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr);
  phys_addr_t hyp_mem_base;
  phys_addr_t hyp_mem_size;
 
-static int cmp_hyp_memblock(const void *p1, const void *p2)
-{
-	const struct memblock_region *r1 = p1;
-	const struct memblock_region *r2 = p2;
-
-	return r1->base < r2->base ? -1 : (r1->base > r2->base);
-}
-
-static void __init sort_memblock_regions(void)
-{
-	sort(hyp_memory,
-	     *hyp_memblock_nr_ptr,
-	     sizeof(struct memblock_region),
-	     cmp_hyp_memblock,
-	     NULL);
-}
-
  static int __init register_memblock_regions(void)
  {
  	struct memblock_region *reg;
@@ -52,7 +34,6 @@ static int __init register_memblock_regions(void)
  		hyp_memory[*hyp_memblock_nr_ptr] = *reg;
  		(*hyp_memblock_nr_ptr)++;
  	}
-	sort_memblock_regions();
 
  	return 0;
  }
maintainer Applied
#6 lore raw
In reply to Gavin Shan
Quoted 9 lines · Gavin Shan

Drop sort_memblock_regions() and avoid sorting the copied memory regions to be ascending order on their base addresses, because the source memory regions should have been sorted correctly when they are added by memblock_add() or its variants.

This is generally reverting commit a14307f5310c ("KVM: arm64: Sort the hypervisor memblocks"). No functional changes intended.

[...]

Applied to next, thanks!

[1/1] KVM: arm64: Drop sort_memblock_regions()

      commit: 00b0300cf1e265b8f3fa57106b940eff6f388d57

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.