Skip to contentSkip to conversation

mm/arm64: re-enable HVO

PATCH Superseded Yu Zhao 3 participants 18 messages Oct 21, 2024 – Oct 31, 2024

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yu Zhao

author
#0 lore raw

cover mm/arm64: re-enable HVO

This series presents one of the previously discussed approaches to re-enable HugeTLB Vmemmap Optimization (HVO) on arm64. HVO was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the following reason:

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Other approaches that have been discussed include:
A. Handle kernel PF while doing BBM [1],
B. Use stop_machine() while doing BBM [2], and,
C. Enable FEAT_BBM level 2 and keep the memory contents at the old
and new output addresses unchanged to avoid BBM (D8.16.1-2) [3].

A quick comparison between this approach (D) and the above approaches:
--+------------------------------+-----------------------------+
| Pro | Con |
--+------------------------------+-----------------------------+

  A | Low latency, h/w independent | Predictability concerns [4] |
  B | Predictable, h/w independent | High latency                |
  C | Predictable, low latency     | H/w dependent, complex      |
  D | Predictable, h/w independent | Medium latency              |

--+------------------------------+-----------------------------+

This approach is being tested for Google's production systems, which generally find the "con" above acceptable, making it the preferred tradeoff for our use cases:

  +------------------------------+------------+----------+--------+
  | HugeTLB operations           | Before [0] + After    | Change |
  +------------------------------+------------+----------+--------+
  | Alloc 600 1GB                | 0m3.526s   | 0m3.779s | +7%    |
  | Free 600 1GB                 | 0m0.880s   | 0m0.940s | +7%    |
  | Demote 600 1GB to 307200 2MB | 0m1.575s   | 0m5.132s | +326%  |
  | Free 307200 2MB              | 0m0.946s   | 0m4.456s | +471%  |
  +------------------------------+------------+----------+--------+

[0] For comparison purposes, this only includes the last patch in the series, i.e., CONFIG_ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP=y.
[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/
[3] https://lore.kernel.org/[email protected]/
[4] https://lore.kernel.org/20240326125409.GA9552@willie-the-truck/

Yu Zhao (6):
mm/hugetlb_vmemmap: batch update PTEs mm/hugetlb_vmemmap: add arch-independent helpers irqchip/gic-v3: support SGI broadcast
arm64: broadcast IPIs to pause remote CPUs
arm64: pause remote CPUs to update vmemmap
arm64: select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP

 arch/arm64/Kconfig               |   1 +
 arch/arm64/include/asm/pgalloc.h |  69 ++++++++
 arch/arm64/include/asm/smp.h     |   3 +
 arch/arm64/kernel/smp.c          |  92 ++++++++++-
 drivers/irqchip/irq-gic-v3.c     |  20 ++-
 include/linux/mm_types.h         |   7 +
 mm/hugetlb_vmemmap.c             | 262 +++++++++++++++++++++----------
 7 files changed, 360 insertions(+), 94 deletions(-)

base-commit: 42f7652d3eb527d03665b09edac47f85fb600924

Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#1 lore raw

PATCH 1/6 mm/hugetlb_vmemmap: batch update PTEs

Convert vmemmap_remap_walk->remap_pte to ->remap_pte_range so that vmemmap remap walks can batch update PTEs.

The goal of this conversion is to allow architectures to implement their own optimizations if possible, e.g., only to stop remote CPUs once for each batch when updating vmemmap on arm64. It is not intended to change the remap workflow nor should it by itself have any side effects on performance.

---
 mm/hugetlb_vmemmap.c | 163 ++++++++++++++++++++++++-------------------
 1 file changed, 91 insertions(+), 72 deletions(-)
mm/hugetlb_vmemmap.c +91 -72
index 57b7f591eee8..46befab48d41 100644
@@ -22,7 +22,7 @@
 /**
  * struct vmemmap_remap_walk - walk vmemmap page table
  *
- * @remap_pte:		called for each lowest-level entry (PTE).
+ * @remap_pte_range:	called on a range of PTEs.
  * @nr_walked:		the number of walked pte.
  * @reuse_page:		the page which is reused for the tail vmemmap pages.
  * @reuse_addr:		the virtual address of the @reuse_page page.
@@ -32,8 +32,8 @@
  *			operations.
  */
 struct vmemmap_remap_walk {
-	void			(*remap_pte)(pte_t *pte, unsigned long addr,
-					     struct vmemmap_remap_walk *walk);
+	void			(*remap_pte_range)(pte_t *pte, unsigned long start,
+					unsigned long end, struct vmemmap_remap_walk *walk);
 	unsigned long		nr_walked;
 	struct page		*reuse_page;
 	unsigned long		reuse_addr;
@@ -101,10 +101,6 @@ static int vmemmap_pmd_entry(pmd_t *pmd, unsigned long addr,
 	struct page *head;
 	struct vmemmap_remap_walk *vmemmap_walk = walk->private;
 
-	/* Only splitting, not remapping the vmemmap pages. */
-	if (!vmemmap_walk->remap_pte)
-		walk->action = ACTION_CONTINUE;
-
 	spin_lock(&init_mm.page_table_lock);
 	head = pmd_leaf(*pmd) ? pmd_page(*pmd) : NULL;
 	/*
@@ -129,33 +125,36 @@ static int vmemmap_pmd_entry(pmd_t *pmd, unsigned long addr,
 			ret = -ENOTSUPP;
 	}
 	spin_unlock(&init_mm.page_table_lock);
-	if (!head || ret)
+	if (ret)
 		return ret;
 
-	return vmemmap_split_pmd(pmd, head, addr & PMD_MASK, vmemmap_walk);
-}
+	if (head) {
+		ret = vmemmap_split_pmd(pmd, head, addr & PMD_MASK, vmemmap_walk);
+		if (ret)
+			return ret;
+	}
 
-static int vmemmap_pte_entry(pte_t *pte, unsigned long addr,
-			     unsigned long next, struct mm_walk *walk)
-{
-	struct vmemmap_remap_walk *vmemmap_walk = walk->private;
+	if (vmemmap_walk->remap_pte_range) {
+		pte_t *pte = pte_offset_kernel(pmd, addr);
 
-	/*
-	 * The reuse_page is found 'first' in page table walking before
-	 * starting remapping.
-	 */
-	if (!vmemmap_walk->reuse_page)
-		vmemmap_walk->reuse_page = pte_page(ptep_get(pte));
-	else
-		vmemmap_walk->remap_pte(pte, addr, vmemmap_walk);
-	vmemmap_walk->nr_walked++;
+		vmemmap_walk->nr_walked += (next - addr) / PAGE_SIZE;
+		/*
+		 * The reuse_page is found 'first' in page table walking before
+		 * starting remapping.
+		 */
+		if (!vmemmap_walk->reuse_page) {
+			vmemmap_walk->reuse_page = pte_page(ptep_get(pte));
+			pte++;
+			addr += PAGE_SIZE;
+		}
+		vmemmap_walk->remap_pte_range(pte, addr, next, vmemmap_walk);
+	}
 
 	return 0;
 }
 
 static const struct mm_walk_ops vmemmap_remap_ops = {
 	.pmd_entry	= vmemmap_pmd_entry,
-	.pte_entry	= vmemmap_pte_entry,
 };
 
 static int vmemmap_remap_range(unsigned long start, unsigned long end,
@@ -172,7 +171,7 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
 	if (ret)
 		return ret;
 
-	if (walk->remap_pte && !(walk->flags & VMEMMAP_REMAP_NO_TLB_FLUSH))
+	if (walk->remap_pte_range && !(walk->flags & VMEMMAP_REMAP_NO_TLB_FLUSH))
 		flush_tlb_kernel_range(start, end);
 
 	return 0;
@@ -204,33 +203,45 @@ static void free_vmemmap_page_list(struct list_head *list)
 		free_vmemmap_page(page);
 }
 
-static void vmemmap_remap_pte(pte_t *pte, unsigned long addr,
-			      struct vmemmap_remap_walk *walk)
+static void vmemmap_remap_pte_range(pte_t *pte, unsigned long start, unsigned long end,
+				    struct vmemmap_remap_walk *walk)
 {
-	/*
-	 * Remap the tail pages as read-only to catch illegal write operation
-	 * to the tail pages.
-	 */
-	pgprot_t pgprot = PAGE_KERNEL_RO;
-	struct page *page = pte_page(ptep_get(pte));
-	pte_t entry;
-
-	/* Remapping the head page requires r/w */
-	if (unlikely(addr == walk->reuse_addr)) {
-		pgprot = PAGE_KERNEL;
-		list_del(&walk->reuse_page->lru);
+	int i;
+	struct page *page;
+	int nr_pages = (end - start) / PAGE_SIZE;
 
+	for (i = 0; i < nr_pages; i++) {
+		page = pte_page(ptep_get(pte + i));
+
+		list_add(&page->lru, walk->vmemmap_pages);
+	}
+
+	page = walk->reuse_page;
+
+	if (start == walk->reuse_addr) {
+		list_del(&page->lru);
+		copy_page(page_to_virt(page), (void *)walk->reuse_addr);
 		/*
-		 * Makes sure that preceding stores to the page contents from
-		 * vmemmap_remap_free() become visible before the set_pte_at()
-		 * write.
+		 * Makes sure that preceding stores to the page contents become
+		 * visible before set_pte_at().
 		 */
 		smp_wmb();
 	}
 
-	entry = mk_pte(walk->reuse_page, pgprot);
-	list_add(&page->lru, walk->vmemmap_pages);
-	set_pte_at(&init_mm, addr, pte, entry);
+	for (i = 0; i < nr_pages; i++) {
+		pte_t val;
+
+		/*
+		 * The head page must be mapped read-write; the tail pages are
+		 * mapped read-only to catch illegal modifications.
+		 */
+		if (!i && start == walk->reuse_addr)
+			val = mk_pte(page, PAGE_KERNEL);
+		else
+			val = mk_pte(page, PAGE_KERNEL_RO);
+
+		set_pte_at(&init_mm, start + PAGE_SIZE * i, pte + i, val);
+	}
 }
 
 /*
@@ -252,27 +263,39 @@ static inline void reset_struct_pages(struct page *start)
 	memcpy(start, from, sizeof(*from) * NR_RESET_STRUCT_PAGE);
 }
 
-static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
-				struct vmemmap_remap_walk *walk)
+static void vmemmap_restore_pte_range(pte_t *pte, unsigned long start, unsigned long end,
+				      struct vmemmap_remap_walk *walk)
 {
-	pgprot_t pgprot = PAGE_KERNEL;
+	int i;
 	struct page *page;
-	void *to;
-
-	BUG_ON(pte_page(ptep_get(pte)) != walk->reuse_page);
+	int nr_pages = (end - start) / PAGE_SIZE;
 
 	page = list_first_entry(walk->vmemmap_pages, struct page, lru);
-	list_del(&page->lru);
-	to = page_to_virt(page);
-	copy_page(to, (void *)walk->reuse_addr);
-	reset_struct_pages(to);
+
+	for (i = 0; i < nr_pages; i++) {
+		BUG_ON(pte_page(ptep_get(pte + i)) != walk->reuse_page);
+
+		copy_page(page_to_virt(page), (void *)walk->reuse_addr);
+		reset_struct_pages(page_to_virt(page));
+
+		page = list_next_entry(page, lru);
+	}
 
 	/*
 	 * Makes sure that preceding stores to the page contents become visible
-	 * before the set_pte_at() write.
+	 * before set_pte_at().
 	 */
 	smp_wmb();
-	set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
+
+	for (i = 0; i < nr_pages; i++) {
+		pte_t val;
+
+		page = list_first_entry(walk->vmemmap_pages, struct page, lru);
+		list_del(&page->lru);
+
+		val = mk_pte(page, PAGE_KERNEL);
+		set_pte_at(&init_mm, start + PAGE_SIZE * i, pte + i, val);
+	}
 }
 
 /**
@@ -290,7 +313,6 @@ static int vmemmap_remap_split(unsigned long start, unsigned long end,
 			       unsigned long reuse)
 {
 	struct vmemmap_remap_walk walk = {
-		.remap_pte	= NULL,
 		.flags		= VMEMMAP_SPLIT_NO_TLB_FLUSH,
 	};
 
@@ -322,10 +344,10 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
 {
 	int ret;
 	struct vmemmap_remap_walk walk = {
-		.remap_pte	= vmemmap_remap_pte,
-		.reuse_addr	= reuse,
-		.vmemmap_pages	= vmemmap_pages,
-		.flags		= flags,
+		.remap_pte_range	= vmemmap_remap_pte_range,
+		.reuse_addr		= reuse,
+		.vmemmap_pages		= vmemmap_pages,
+		.flags			= flags,
 	};
 	int nid = page_to_nid((struct page *)reuse);
 	gfp_t gfp_mask = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
@@ -340,8 +362,6 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
 	 */
 	walk.reuse_page = alloc_pages_node(nid, gfp_mask, 0);
 	if (walk.reuse_page) {
-		copy_page(page_to_virt(walk.reuse_page),
-			  (void *)walk.reuse_addr);
 		list_add(&walk.reuse_page->lru, vmemmap_pages);
 		memmap_pages_add(1);
 	}
@@ -371,10 +391,9 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
 		 * They will be restored in the following call.
 		 */
 		walk = (struct vmemmap_remap_walk) {
-			.remap_pte	= vmemmap_restore_pte,
-			.reuse_addr	= reuse,
-			.vmemmap_pages	= vmemmap_pages,
-			.flags		= 0,
+			.remap_pte_range	= vmemmap_restore_pte_range,
+			.reuse_addr		= reuse,
+			.vmemmap_pages		= vmemmap_pages,
 		};
 
 		vmemmap_remap_range(reuse, end, &walk);
@@ -425,10 +444,10 @@ static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
 {
 	LIST_HEAD(vmemmap_pages);
 	struct vmemmap_remap_walk walk = {
-		.remap_pte	= vmemmap_restore_pte,
-		.reuse_addr	= reuse,
-		.vmemmap_pages	= &vmemmap_pages,
-		.flags		= flags,
+		.remap_pte_range	= vmemmap_restore_pte_range,
+		.reuse_addr		= reuse,
+		.vmemmap_pages		= &vmemmap_pages,
+		.flags			= flags,
 	};
 
 	/* See the comment in the vmemmap_remap_free(). */
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#2 lore raw

PATCH 2/6 mm/hugetlb_vmemmap: add arch-independent helpers

In reply to Yu Zhao

Add architecture-independent helpers to allow individual architectures to work around their own limitations when updating vmemmap.

Specifically, the current remap workflow requires break-before-make (BBM) on arm64. By overriding the default helpers later in this series, arm64 will be able to support the current HVO implementation.

---
 include/linux/mm_types.h |  7 +++
 mm/hugetlb_vmemmap.c     | 99 ++++++++++++++++++++++++++++++++++------
 2 files changed, 92 insertions(+), 14 deletions(-)
include/linux/mm_types.h +7 -0
index 6e3bdf8e38bc..0f3ae6e173f6 100644
@@ -1499,4 +1499,11 @@ enum {
 	/* See also internal only FOLL flags in mm/internal.h */
 };
 
+/* Skip the TLB flush when we split the PMD */
+#define VMEMMAP_SPLIT_NO_TLB_FLUSH	BIT(0)
+/* Skip the TLB flush when we remap the PTE */
+#define VMEMMAP_REMAP_NO_TLB_FLUSH	BIT(1)
+/* synchronize_rcu() to avoid writes from page_ref_add_unless() */
+#define VMEMMAP_SYNCHRONIZE_RCU		BIT(2)
+
 #endif /* _LINUX_MM_TYPES_H */
mm/hugetlb_vmemmap.c +85 -14

200 lines not on this page · raw message

Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#3 lore raw

PATCH 3/6 irqchip/gic-v3: support SGI broadcast

In reply to Yu Zhao

GIC v3 and later support SGI broadcast, i.e., the mode that routes interrupts to all PEs in the system excluding the local CPU.

Supporting this mode can avoid looping through all the remote CPUs when broadcasting SGIs, especially for systems with 200+ CPUs. The performance improvement can be measured with the rest of this series booted with "hugetlb_free_vmemmap=on irqchip.gicv3_pseudo_nmi=1":

cd /sys/kernel/mm/hugepages/
echo 600 >hugepages-1048576kB/nr_hugepages
echo 2048kB >hugepages-1048576kB/demote_size
perf record -g -- bash -c "echo 600 >hugepages-1048576kB/demote"

         gic_ipi_send_mask()  bash sys time
Before:  38.14%               0m10.513s
After:    0.20%               0m5.132s
---
 drivers/irqchip/irq-gic-v3.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3.c +19 -1
index ce87205e3e82..42c39385e1b9 100644
@@ -1394,9 +1394,20 @@ static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
 	gic_write_sgi1r(val);
 }
 
+static void gic_broadcast_sgi(unsigned int irq)
+{
+	u64 val;
+
+	val = BIT(ICC_SGI1R_IRQ_ROUTING_MODE_BIT) | (irq << ICC_SGI1R_SGI_ID_SHIFT);
+
+	pr_devel("CPU %d: broadcasting SGI %u\n", smp_processor_id(), irq);
+	gic_write_sgi1r(val);
+}
+
 static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 {
 	int cpu;
+	cpumask_t broadcast;
 
 	if (WARN_ON(d->hwirq >= 16))
 		return;
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 	 */
 	dsb(ishst);
 
+	cpumask_copy(&broadcast, cpu_present_mask);
+	cpumask_clear_cpu(smp_processor_id(), &broadcast);
+	if (cpumask_equal(&broadcast, mask)) {
+		gic_broadcast_sgi(d->hwirq);
+		goto done;
+	}
+
 	for_each_cpu(cpu, mask) {
 		u64 cluster_id = MPIDR_TO_SGI_CLUSTER_ID(gic_cpu_to_affinity(cpu));
 		u16 tlist;
@@ -1414,7 +1432,7 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 		tlist = gic_compute_target_list(&cpu, mask, cluster_id);
 		gic_send_sgi(cluster_id, tlist, d->hwirq);
 	}
-
+done:
 	/* Force the above writes to ICC_SGI1R_EL1 to be executed */
 	isb();
 }
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
#4 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

kernel test robot noticed the following build warnings:

Hi Yu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 42f7652d3eb527d03665b09edac47f85fb600924]

url:    https://github.com/intel-lab-lkp/linux/commits/Yu-Zhao/mm-hugetlb_vmemmap-batch-update-PTEs/20241021-122330
base:   42f7652d3eb527d03665b09edac47f85fb600924
patch link:    https://lore.kernel.org/r/20241021042218.746659-4-yuzhao%40google.com
patch subject: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast
config: arm-defconfig (https://download.01.org/0day-ci/archive/20241022/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241022/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/irqchip/irq-gic-v3.c:1401:8: warning: shift count >= width of type [-Wshift-count-overflow]
           val = BIT(ICC_SGI1R_IRQ_ROUTING_MODE_BIT) | (irq << ICC_SGI1R_SGI_ID_SHIFT);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
   #define BIT(nr)                 (UL(1) << (nr))
                                          ^  ~~~~

1 warning generated.

vim +1401 drivers/irqchip/irq-gic-v3.c

1396
1397 static void gic_broadcast_sgi(unsigned int irq)
1398 {
1399 u64 val;
1400

> 1401		val = BIT(ICC_SGI1R_IRQ_ROUTING_MODE_BIT) | (irq << ICC_SGI1R_SGI_ID_SHIFT);

1402
1403 pr_devel("CPU %d: broadcasting SGI %u\n", smp_processor_id(), irq);
1404 gic_write_sgi1r(val);
1405 }
1406

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#5 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

In reply to Yu Zhao
Quoted 28 lines · Yu Zhao

GIC v3 and later support SGI broadcast, i.e., the mode that routes interrupts to all PEs in the system excluding the local CPU.

Supporting this mode can avoid looping through all the remote CPUs when broadcasting SGIs, especially for systems with 200+ CPUs. The performance improvement can be measured with the rest of this series booted with "hugetlb_free_vmemmap=on irqchip.gicv3_pseudo_nmi=1":

cd /sys/kernel/mm/hugepages/
echo 600 >hugepages-1048576kB/nr_hugepages
echo 2048kB >hugepages-1048576kB/demote_size
perf record -g -- bash -c "echo 600 >hugepages-1048576kB/demote"

         gic_ipi_send_mask()  bash sys time
Before:  38.14%               0m10.513s
After:    0.20%               0m5.132s
---
 drivers/irqchip/irq-gic-v3.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3.c +0 -0
index ce87205e3e82..42c39385e1b9 100644

drivers/irqchip/irq-gic-v3.c static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq) · line 1401

Quoted 8 lines
drivers/irqchip/irq-gic-v3.c +5 -0
@@ -1394,9 +1394,20 @@ static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
 	gic_write_sgi1r(val);
 }
 
+static void gic_broadcast_sgi(unsigned int irq)
+{
+	u64 val;
+
+	val = BIT(ICC_SGI1R_IRQ_ROUTING_MODE_BIT) | (irq << ICC_SGI1R_SGI_ID_SHIFT);

As picked up by the test bot, please fix the 32bit build.

Quoted 13 lines
drivers/irqchip/irq-gic-v3.c +6 -0
+
+	pr_devel("CPU %d: broadcasting SGI %u\n", smp_processor_id(), irq);
+	gic_write_sgi1r(val);
+}
+
 static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 {
 	int cpu;
+	cpumask_t broadcast;
 
 	if (WARN_ON(d->hwirq >= 16))
 		return;

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 4 lines
drivers/irqchip/irq-gic-v3.c +1 -0
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 	 */
 	dsb(ishst);
 
+	cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1426

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +5 -0
+	cpumask_clear_cpu(smp_processor_id(), &broadcast);
+	if (cpumask_equal(&broadcast, mask)) {
+		gic_broadcast_sgi(d->hwirq);
+		goto done;
+	}

So the (valid) case where you would IPI *everyone* is not handled as a fast path? That seems a missed opportunity.

This also seem an like expensive way to do it. How about something like:

	int mcnt = cpumask_weight(mask);
	int ocnt = cpumask_weight(cpu_online_mask);
	if (mcnt == ocnt)  {
		/* Broadcast to all CPUs including self */
	} else if (mcnt == (ocnt - 1) &&
		   !cpumask_test_cpu(smp_processor_id(), mask)) {
		/* Broadcast to all but self */
	}

which avoids the copy+update_full compare.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#6 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

Hi Marc,

Quoted 34 lines · Marc Zyngier
Quoted 31 lines · Yu Zhao

GIC v3 and later support SGI broadcast, i.e., the mode that routes interrupts to all PEs in the system excluding the local CPU.

Supporting this mode can avoid looping through all the remote CPUs when broadcasting SGIs, especially for systems with 200+ CPUs. The performance improvement can be measured with the rest of this series booted with "hugetlb_free_vmemmap=on irqchip.gicv3_pseudo_nmi=1":

cd /sys/kernel/mm/hugepages/
echo 600 >hugepages-1048576kB/nr_hugepages
echo 2048kB >hugepages-1048576kB/demote_size
perf record -g -- bash -c "echo 600 >hugepages-1048576kB/demote"

         gic_ipi_send_mask()  bash sys time
Before:  38.14%               0m10.513s
After:    0.20%               0m5.132s
---
 drivers/irqchip/irq-gic-v3.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3.c +0 -0
index ce87205e3e82..42c39385e1b9 100644
@@ -1394,9 +1394,20 @@ static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
      gic_write_sgi1r(val);
 }
 
Quoted 7 lines

drivers/irqchip/irq-gic-v3.c static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq) · line 1401

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +5 -0
+static void gic_broadcast_sgi(unsigned int irq)
+{
+     u64 val;
+
+     val = BIT(ICC_SGI1R_IRQ_ROUTING_MODE_BIT) | (irq << ICC_SGI1R_SGI_ID_SHIFT);

As picked up by the test bot, please fix the 32bit build.

Will do.

Quoted 16 lines
Quoted 16 lines
drivers/irqchip/irq-gic-v3.c +6 -0
+
+     pr_devel("CPU %d: broadcasting SGI %u\n", smp_processor_id(), irq);
+     gic_write_sgi1r(val);
+}
+
 static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
 {
      int cpu;
+     cpumask_t broadcast;
 
      if (WARN_ON(d->hwirq >= 16))
              return;
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
       */
      dsb(ishst);
 
Quoted 4 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 1 line
drivers/irqchip/irq-gic-v3.c +1 -0
+     cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

This is exactly because "we don't IPI offline CPUs, in general", assuming "we" means the kernel, not GIC.

My interpretation of what the GIC spec says ("0b1: Interrupts routed to all PEs in the system, excluding self") is that it broadcasts IPIs to "cpu_present_mask" (minus the local one). So if the kernel uses "cpu_online_mask" here, GIC would send IPIs to offline CPUs (cpu_present_mask ^ cpu_online_mask), which I don't know whether it's a defined behavior.

But if you actually meant GIC doesn't IPI offline CPUs, then yes, here the kernel should use "cpu_online_mask".

Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1426

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +5 -0
+     cpumask_clear_cpu(smp_processor_id(), &broadcast);
+     if (cpumask_equal(&broadcast, mask)) {
+             gic_broadcast_sgi(d->hwirq);
+             goto done;
+     }

So the (valid) case where you would IPI *everyone* is not handled as a fast path? That seems a missed opportunity.

You are right: it should handle that case.

Quoted 7 lines

This also seem an like expensive way to do it. How about something like:

        int mcnt = cpumask_weight(mask);
        int ocnt = cpumask_weight(cpu_online_mask);
        if (mcnt == ocnt)  {
                /* Broadcast to all CPUs including self */

Does the comment mean the following two steps?
1. Broadcasting to everyone else.
2. Sending to self.

My understanding of the "Interrupt Routing Mode" is that it can't broadcast to all CPUs including self, and therefore we need the above two steps, which still can be a lot faster. Is my understanding correct?

Quoted 6 lines
        } else if (mcnt == (ocnt - 1) &&
                   !cpumask_test_cpu(smp_processor_id(), mask)) {
                /* Broadcast to all but self */
        }

which avoids the copy+update_full compare.

Thank you.

maintainer
#7 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

Quoted 20 lines · Yu Zhao

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Yu Zhao
Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +1 -0
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
       */
      dsb(ishst);
 
+     cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

This is exactly because "we don't IPI offline CPUs, in general", assuming "we" means the kernel, not GIC.

Quoted 6 lines

My interpretation of what the GIC spec says ("0b1: Interrupts routed to all PEs in the system, excluding self") is that it broadcasts IPIs to "cpu_present_mask" (minus the local one). So if the kernel uses "cpu_online_mask" here, GIC would send IPIs to offline CPUs (cpu_present_mask ^ cpu_online_mask), which I don't know whether it's a defined behavior.

Offline CPUs are not known to the kernel. Most likely, they are either powered off, or spending quality time in Secure or Realm mode. Either way, this is none of our business.

Your approach would make also the kernel perform pretty inconsistently depending on whether CPUs are offline and not.

Quoted 18 lines

But if you actually meant GIC doesn't IPI offline CPUs, then yes, here the kernel should use "cpu_online_mask".

Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1426

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +5 -0
+     cpumask_clear_cpu(smp_processor_id(), &broadcast);
+     if (cpumask_equal(&broadcast, mask)) {
+             gic_broadcast_sgi(d->hwirq);
+             goto done;
+     }

So the (valid) case where you would IPI *everyone* is not handled as a fast path? That seems a missed opportunity.

You are right: it should handle that case.

Quoted 3 lines

This also seem an like expensive way to do it. How about something like:

Quoted 8 lines
Quoted 4 lines
        int mcnt = cpumask_weight(mask);
        int ocnt = cpumask_weight(cpu_online_mask);
        if (mcnt == ocnt)  {
                /* Broadcast to all CPUs including self */

Does the comment mean the following two steps?
1. Broadcasting to everyone else.
2. Sending to self.

Correct.

Quoted 4 lines

My understanding of the "Interrupt Routing Mode" is that it can't broadcast to all CPUs including self, and therefore we need the above two steps, which still can be a lot faster. Is my understanding correct?

Yes.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#8 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

Quoted 23 lines · Marc Zyngier
Quoted 20 lines · Yu Zhao

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Yu Zhao
Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +1 -0
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
       */
      dsb(ishst);
 
+     cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

This is exactly because "we don't IPI offline CPUs, in general", assuming "we" means the kernel, not GIC.

Quoted 6 lines
Quoted 6 lines

My interpretation of what the GIC spec says ("0b1: Interrupts routed to all PEs in the system, excluding self") is that it broadcasts IPIs to "cpu_present_mask" (minus the local one). So if the kernel uses "cpu_online_mask" here, GIC would send IPIs to offline CPUs (cpu_present_mask ^ cpu_online_mask), which I don't know whether it's a defined behavior.

Thanks for clarifying.

Quoted 1 line

Offline CPUs are not known to the kernel.

I assume it wouldn't matter to firmware either, correct? IOW, we wouldn't cause firmware any trouble by letting GIC send IPIs to (cpu_present_mask ^ cpu_online_mask), assuming those two masks can be different on arm64 when hotplug is enabled?

Quoted 49 lines

Most likely, they are either
powered off, or spending quality time in Secure or Realm mode. Either way, this is none of our business.

Your approach would make also the kernel perform pretty inconsistently depending on whether CPUs are offline and not.

Quoted 18 lines

But if you actually meant GIC doesn't IPI offline CPUs, then yes, here the kernel should use "cpu_online_mask".

Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1426

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +5 -0
+     cpumask_clear_cpu(smp_processor_id(), &broadcast);
+     if (cpumask_equal(&broadcast, mask)) {
+             gic_broadcast_sgi(d->hwirq);
+             goto done;
+     }

So the (valid) case where you would IPI *everyone* is not handled as a fast path? That seems a missed opportunity.

You are right: it should handle that case.

Quoted 3 lines

This also seem an like expensive way to do it. How about something like:

Quoted 8 lines
Quoted 4 lines
        int mcnt = cpumask_weight(mask);
        int ocnt = cpumask_weight(cpu_online_mask);
        if (mcnt == ocnt)  {
                /* Broadcast to all CPUs including self */

Does the comment mean the following two steps?
1. Broadcasting to everyone else.
2. Sending to self.

Correct.

Quoted 4 lines

My understanding of the "Interrupt Routing Mode" is that it can't broadcast to all CPUs including self, and therefore we need the above two steps, which still can be a lot faster. Is my understanding correct?

Yes.

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

maintainer
#9 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

Quoted 32 lines · Yu Zhao
Quoted 23 lines · Marc Zyngier
Quoted 20 lines · Yu Zhao

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Yu Zhao
Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +1 -0
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
       */
      dsb(ishst);
 
+     cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

This is exactly because "we don't IPI offline CPUs, in general", assuming "we" means the kernel, not GIC.

Quoted 6 lines
Quoted 6 lines

My interpretation of what the GIC spec says ("0b1: Interrupts routed to all PEs in the system, excluding self") is that it broadcasts IPIs to "cpu_present_mask" (minus the local one). So if the kernel uses "cpu_online_mask" here, GIC would send IPIs to offline CPUs (cpu_present_mask ^ cpu_online_mask), which I don't know whether it's a defined behavior.

Quoted 5 lines

Thanks for clarifying.

Quoted 1 line

Offline CPUs are not known to the kernel.

I assume it wouldn't matter to firmware either, correct? IOW, we

Firmware is on the secure side of the stack.

Quoted 3 lines

wouldn't cause firmware any trouble by letting GIC send IPIs to (cpu_present_mask ^ cpu_online_mask), assuming those two masks can be different on arm64 when hotplug is enabled?

You can't send SGIs from non-secure to secure using ICC_SGI1R_EL1. You would need to use ICC_ASGI1R_EL1, and have secure to allow such brokenness via a configuration of GICR_NSACR. Linux doesn't use the former, and no sane software touches the latter.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#10 lore raw

Re: [PATCH v1 3/6] irqchip/gic-v3: support SGI broadcast

Quoted 41 lines · Marc Zyngier
Quoted 32 lines · Yu Zhao
Quoted 23 lines · Marc Zyngier
Quoted 20 lines · Yu Zhao

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Yu Zhao
Quoted 8 lines

drivers/irqchip/irq-gic-v3.c static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask) · line 1421

Quoted 5 lines
drivers/irqchip/irq-gic-v3.c +1 -0
@@ -1407,6 +1418,13 @@ static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
       */
      dsb(ishst);
 
+     cpumask_copy(&broadcast, cpu_present_mask);

Why cpu_present_mask? I'd expect that cpu_online_mask should be the correct mask to use -- we don't IPI offline CPUs, in general.

This is exactly because "we don't IPI offline CPUs, in general", assuming "we" means the kernel, not GIC.

Quoted 6 lines
Quoted 6 lines

My interpretation of what the GIC spec says ("0b1: Interrupts routed to all PEs in the system, excluding self") is that it broadcasts IPIs to "cpu_present_mask" (minus the local one). So if the kernel uses "cpu_online_mask" here, GIC would send IPIs to offline CPUs (cpu_present_mask ^ cpu_online_mask), which I don't know whether it's a defined behavior.

Quoted 5 lines

Thanks for clarifying.

Quoted 1 line

Offline CPUs are not known to the kernel.

I assume it wouldn't matter to firmware either, correct? IOW, we

Quoted 7 lines

Firmware is on the secure side of the stack.

Quoted 3 lines

wouldn't cause firmware any trouble by letting GIC send IPIs to (cpu_present_mask ^ cpu_online_mask), assuming those two masks can be different on arm64 when hotplug is enabled?

You can't send SGIs from non-secure to secure using ICC_SGI1R_EL1.

Right, this makes sense now.

Quoted 9 lines

You
would need to use ICC_ASGI1R_EL1, and have secure to allow such brokenness via a configuration of GICR_NSACR. Linux doesn't use the former, and no sane software touches the latter.

        M.

--
Without deviation from the norm, progress is not possible.

author
#11 lore raw

PATCH 4/6 arm64: broadcast IPIs to pause remote CPUs

In reply to Yu Zhao

Broadcast pseudo-NMI IPIs to pause remote CPUs for a short period of time, and then reliably resume them when the local CPU exits critical sections that preclude the execution of remote CPUs.

A typical example of such critical sections is BBM on kernel PTEs. HugeTLB Vmemmap Optimization (HVO) on arm64 was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the folllowing reason:

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Supporting BBM on kernel PTEs is one of the approaches that can make HVO theoretically safe on arm64.

Note that it is still possible for the paused CPUs to perform speculative translations. Such translations would cause spurious kernel PFs, which should be properly handled by is_spurious_el1_translation_fault().

---
 arch/arm64/include/asm/smp.h |  3 ++
 arch/arm64/kernel/smp.c      | 92 +++++++++++++++++++++++++++++++++---
 2 files changed, 88 insertions(+), 7 deletions(-)
arch/arm64/include/asm/smp.h +3 -0
index 2510eec026f7..cffb0cfed961 100644
@@ -133,6 +133,9 @@ bool cpus_are_stuck_in_kernel(void);
 extern void crash_smp_send_stop(void);
 extern bool smp_crash_stop_failed(void);
 
+void pause_remote_cpus(void);
+void resume_remote_cpus(void);
+
 #endif /* ifndef __ASSEMBLY__ */
 
 #endif /* ifndef __ASM_SMP_H */
arch/arm64/kernel/smp.c +85 -7

131 lines not on this page · raw message

Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
maintainer
#12 lore raw

Re: [PATCH v1 4/6] arm64: broadcast IPIs to pause remote CPUs

Quoted 10 lines · Yu Zhao

Broadcast pseudo-NMI IPIs to pause remote CPUs for a short period of time, and then reliably resume them when the local CPU exits critical sections that preclude the execution of remote CPUs.

A typical example of such critical sections is BBM on kernel PTEs. HugeTLB Vmemmap Optimization (HVO) on arm64 was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the folllowing reason:

Quoted 7 lines

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Supporting BBM on kernel PTEs is one of the approaches that can make HVO theoretically safe on arm64.

Is the safety only theoretical? I would have expected that we'd use an approach that is absolutely rock-solid.

Quoted 5 lines

Note that it is still possible for the paused CPUs to perform speculative translations. Such translations would cause spurious kernel PFs, which should be properly handled by is_spurious_el1_translation_fault().

Speculative translation faults are never reported, that'd be a CPU bug. *Spurious* translation faults can be reported if the CPU doesn't implement FEAT_ETS2, for example, and that has to do with the ordering of memory access wrt page-table walking for the purpose of translations.

Quoted 40 lines
---
 arch/arm64/include/asm/smp.h |  3 ++
 arch/arm64/kernel/smp.c      | 92 +++++++++++++++++++++++++++++++++---
 2 files changed, 88 insertions(+), 7 deletions(-)
arch/arm64/include/asm/smp.h +3 -0
index 2510eec026f7..cffb0cfed961 100644
@@ -133,6 +133,9 @@ bool cpus_are_stuck_in_kernel(void);
 extern void crash_smp_send_stop(void);
 extern bool smp_crash_stop_failed(void);
 
+void pause_remote_cpus(void);
+void resume_remote_cpus(void);
+
 #endif /* ifndef __ASSEMBLY__ */
 
 #endif /* ifndef __ASM_SMP_H */
arch/arm64/kernel/smp.c +6 -1
index 3b3f6b56e733..68829c6de1b1 100644
@@ -85,7 +85,12 @@ static int ipi_irq_base __ro_after_init;
 static int nr_ipi __ro_after_init = NR_IPI;
 static struct irq_desc *ipi_desc[MAX_IPI] __ro_after_init;
 
-static bool crash_stop;
+enum {
+	SEND_STOP = BIT(0),
+	CRASH_STOP = BIT(1),
+};
+
+static unsigned long stop_in_progress;
 
 static void ipi_setup(int cpu);
 

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 925

Quoted 4 lines
arch/arm64/kernel/smp.c +1 -0
@@ -917,6 +922,79 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
 #endif
 }
 
+static DEFINE_SPINLOCK(cpu_pause_lock);

PREEMPT_RT will turn this into a sleeping lock. Is it safe to sleep as you are dealing with kernel mappings?

Quoted 6 lines
arch/arm64/kernel/smp.c +6 -0
+static cpumask_t paused_cpus;
+static cpumask_t resumed_cpus;
+
+static void pause_local_cpu(void)
+{
+	int cpu = smp_processor_id();

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 941

Quoted 10 lines
arch/arm64/kernel/smp.c +10 -0
+
+	cpumask_clear_cpu(cpu, &resumed_cpus);
+	/*
+	 * Paired with pause_remote_cpus() to confirm that this CPU not only
+	 * will be paused but also can be reliably resumed.
+	 */
+	smp_wmb();
+	cpumask_set_cpu(cpu, &paused_cpus);
+	/* paused_cpus must be set before waiting on resumed_cpus. */
+	barrier();

I'm not sure what this is trying to enforce. Yes, the compiler won't reorder the set and the test. But your comment seems to indicate that also need to make sure the CPU preserves that ordering, and short of a DMB, the test below could be reordered.

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 944

Quoted 3 lines
arch/arm64/kernel/smp.c +3 -0
+	while (!cpumask_test_cpu(cpu, &resumed_cpus))
+		cpu_relax();
+	/* A typical example for sleep and wake-up functions. */

I'm not sure this is "typical",...

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 957

Quoted 13 lines
arch/arm64/kernel/smp.c +13 -0
+	smp_mb();
+	cpumask_clear_cpu(cpu, &paused_cpus);
+}
+
+void pause_remote_cpus(void)
+{
+	cpumask_t cpus_to_pause;
+
+	lockdep_assert_cpus_held();
+	lockdep_assert_preemption_disabled();
+
+	cpumask_copy(&cpus_to_pause, cpu_online_mask);
+	cpumask_clear_cpu(smp_processor_id(), &cpus_to_pause);

This bitmap is manipulated outside of your cpu_pause_lock. What guarantees you can't have two CPUs stepping on each other here?

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 966

Quoted 9 lines
arch/arm64/kernel/smp.c +9 -0
+
+	spin_lock(&cpu_pause_lock);
+
+	WARN_ON_ONCE(!cpumask_empty(&paused_cpus));
+
+	smp_cross_call(&cpus_to_pause, IPI_CPU_STOP_NMI);
+
+	while (!cpumask_equal(&cpus_to_pause, &paused_cpus))
+		cpu_relax();

This can be a lot of things to compare, specially that you are explicitly mentioning large systems. Why can't this be implemented as a counter instead?

Overall, this looks like stop_machine() in disguise. Why can't this use the existing infrastructure?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#13 lore raw

Re: [PATCH v1 4/6] arm64: broadcast IPIs to pause remote CPUs

Quoted 18 lines · Marc Zyngier
Quoted 15 lines · Yu Zhao

Broadcast pseudo-NMI IPIs to pause remote CPUs for a short period of time, and then reliably resume them when the local CPU exits critical sections that preclude the execution of remote CPUs.

A typical example of such critical sections is BBM on kernel PTEs. HugeTLB Vmemmap Optimization (HVO) on arm64 was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the folllowing reason:

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Quoted 5 lines
Quoted 2 lines

Supporting BBM on kernel PTEs is one of the approaches that can make HVO theoretically safe on arm64.

Is the safety only theoretical? I would have expected that we'd use an approach that is absolutely rock-solid.

We've been trying to construct a repro against the original HVO (missing BBM), but so far no success. Hopefully a repro does exist, and then we'd be able to demonstrate the effectiveness of this series, which is only theoretical at the moment.

Quoted 7 lines
Quoted 4 lines

Note that it is still possible for the paused CPUs to perform speculative translations. Such translations would cause spurious kernel PFs, which should be properly handled by is_spurious_el1_translation_fault().

Speculative translation faults are never reported, that'd be a CPU bug.

Right, I meant to say "speculative accesses that cause translations".

Quoted 3 lines

*Spurious* translation faults can be reported if the CPU doesn't implement FEAT_ETS2, for example, and that has to do with the ordering of memory access wrt page-table walking for the purpose of translations.

Just want to make sure I fully understand: after the local CPU sends TLBI (followed by DSB & ISB), FEAT_ETS2 would act like DSB & ISB on remote CPUs when they perform the invalidation, and therefore speculative accesses are ordered as well on remote CPUs.

Also I'm assuming IPIs on remote CPUs don't act like a full barrier, and whatever they interrupt still can be speculatively executed even though the IPI hander itself doesn't access the vmemmap area undergoing BBM. Is this correct?

Quoted 38 lines
Quoted 38 lines
---
 arch/arm64/include/asm/smp.h |  3 ++
 arch/arm64/kernel/smp.c      | 92 +++++++++++++++++++++++++++++++++---
 2 files changed, 88 insertions(+), 7 deletions(-)
arch/arm64/include/asm/smp.h +3 -0
index 2510eec026f7..cffb0cfed961 100644
@@ -133,6 +133,9 @@ bool cpus_are_stuck_in_kernel(void);
 extern void crash_smp_send_stop(void);
 extern bool smp_crash_stop_failed(void);
 
+void pause_remote_cpus(void);
+void resume_remote_cpus(void);
+
 #endif /* ifndef __ASSEMBLY__ */
 
 #endif /* ifndef __ASM_SMP_H */
arch/arm64/kernel/smp.c +6 -1
index 3b3f6b56e733..68829c6de1b1 100644
@@ -85,7 +85,12 @@ static int ipi_irq_base __ro_after_init;
 static int nr_ipi __ro_after_init = NR_IPI;
 static struct irq_desc *ipi_desc[MAX_IPI] __ro_after_init;
 
-static bool crash_stop;
+enum {
+     SEND_STOP = BIT(0),
+     CRASH_STOP = BIT(1),
+};
+
+static unsigned long stop_in_progress;
 
 static void ipi_setup(int cpu);
 
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 925

Quoted 5 lines
arch/arm64/kernel/smp.c +1 -0
@@ -917,6 +922,79 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
 #endif
 }
 
+static DEFINE_SPINLOCK(cpu_pause_lock);

PREEMPT_RT will turn this into a sleeping lock. Is it safe to sleep as you are dealing with kernel mappings?

Right, it should be a raw spinlock -- the caller disabled preemption, which as you said is required when dealing with the kernel mappings.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/smp.c +11 -0
+static cpumask_t paused_cpus;
+static cpumask_t resumed_cpus;
+
+static void pause_local_cpu(void)
+{
+     int cpu = smp_processor_id();
+
+     cpumask_clear_cpu(cpu, &resumed_cpus);
+     /*
+      * Paired with pause_remote_cpus() to confirm that this CPU not only
+      * will be paused but also can be reliably resumed.
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 941

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+      */
+     smp_wmb();
+     cpumask_set_cpu(cpu, &paused_cpus);
+     /* paused_cpus must be set before waiting on resumed_cpus. */
+     barrier();

I'm not sure what this is trying to enforce. Yes, the compiler won't reorder the set and the test.

Sorry I don't follow: does cpumask_set_cpu(), i.e., set_bit(), already contain a compiler barrier?

My understanding is that the compiler is free to reorder the set and test on those two independent variables, and make it like this:

  while (!cpumask_test_cpu(cpu, &resumed_cpus))
      cpu_relax();
  cpumask_set_cpu(cpu, &paused_cpus);

So the CPU sent the IPI would keep waiting on paused_cpus being set, and this CPU would keep waiting on resumed_cpus being set, which would end up with a deadlock.

Quoted 4 lines

But your comment seems to indicate that
also need to make sure the CPU preserves that ordering and short of a
DMB, the test below could be reordered.

If this CPU reorders the set and test like above, it wouldn't be a problem because the set would eventually appear on the other CPU that sent the IPI.

Quoted 5 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 944

Quoted 3 lines
arch/arm64/kernel/smp.c +3 -0
+     while (!cpumask_test_cpu(cpu, &resumed_cpus))
+             cpu_relax();
+     /* A typical example for sleep and wake-up functions. */

I'm not sure this is "typical",...

Sorry, this full barrier isn't needed. Apparently I didn't properly fix this from the previous attempt to use wfe()/sev() to make this function the sleeper for resume_remote_cpus() to wake up.

Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/smp.c +8 -0
+     smp_mb();
+     cpumask_clear_cpu(cpu, &paused_cpus);
+}
+
+void pause_remote_cpus(void)
+{
+     cpumask_t cpus_to_pause;
+
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 957

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+     lockdep_assert_cpus_held();
+     lockdep_assert_preemption_disabled();
+
+     cpumask_copy(&cpus_to_pause, cpu_online_mask);
+     cpumask_clear_cpu(smp_processor_id(), &cpus_to_pause);

This bitmap is manipulated outside of your cpu_pause_lock. What guarantees you can't have two CPUs stepping on each other here?

Do you mean cpus_to_pause? If so, that's a local bitmap.

Quoted 5 lines
Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+
+     spin_lock(&cpu_pause_lock);
+
+     WARN_ON_ONCE(!cpumask_empty(&paused_cpus));
+
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 966

Quoted 4 lines
arch/arm64/kernel/smp.c +4 -0
+     smp_cross_call(&cpus_to_pause, IPI_CPU_STOP_NMI);
+
+     while (!cpumask_equal(&cpus_to_pause, &paused_cpus))
+             cpu_relax();

This can be a lot of things to compare, specially that you are explicitly mentioning large systems. Why can't this be implemented as a counter instead?

Agreed - that'd be sufficient and simpler.

Quoted 2 lines

Overall, this looks like stop_machine() in disguise. Why can't this use the existing infrastructure?

This came up during the previous discussion [1]. There are similarities. The main concern with reusing stop_machine() (or part of its current implementation) is that it may not meet the performance requirements. Refactoring might be possible, however, it seems to me (after checking the code again) that such a refactoring unlikely ends up cleaner or simpler codebase, especially after I got rid of CPU masks, as you suggested.

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

arch/arm64/kernel/smp.c +0 -0
index 3b3f6b56e733..b672af2441a3 100644
@@ -917,6 +922,64 @@ static void __noreturn

ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
#endif
}

+static DEFINE_RAW_SPINLOCK(cpu_pause_lock); +static bool __cacheline_aligned_in_smp cpu_paused; +static atomic_t __cacheline_aligned_in_smp nr_cpus_paused; +
+static void pause_local_cpu(void)
+{
+ atomic_inc(&nr_cpus_paused);
+
+ while (READ_ONCE(cpu_paused))
+ cpu_relax();
+
+ atomic_dec(&nr_cpus_paused);
+}
+
+void pause_remote_cpus(void)
+{
+ cpumask_t cpus_to_pause;
+ int nr_cpus_to_pause = num_online_cpus() - 1;
+
+ lockdep_assert_cpus_held();
+ lockdep_assert_preemption_disabled();
+
+ if (!nr_cpus_to_pause)
+ return;
+
+ cpumask_copy(&cpus_to_pause, cpu_online_mask);
+ cpumask_clear_cpu(smp_processor_id(), &cpus_to_pause); +
+ raw_spin_lock(&cpu_pause_lock);
+
+ WARN_ON_ONCE(cpu_paused);
+ WARN_ON_ONCE(atomic_read(&nr_cpus_paused));
+
+ cpu_paused = true;
+
+ smp_cross_call(&cpus_to_pause, IPI_CPU_STOP_NMI); +
+ while (atomic_read(&nr_cpus_paused) != nr_cpus_to_pause)
+ cpu_relax();
+
+ raw_spin_unlock(&cpu_pause_lock);
+}
+
+void resume_remote_cpus(void)
+{
+ if (!cpu_paused)
+ return;
+
+ raw_spin_lock(&cpu_pause_lock);
+
+ WRITE_ONCE(cpu_paused, false);
+
+ while (atomic_read(&nr_cpus_paused))
+ cpu_relax();
+
+ raw_spin_unlock(&cpu_pause_lock);

+}
+
static void arm64_backtrace_ipi(cpumask_t *mask) {
__ipi_send_mask(ipi_desc[IPI_CPU_BACKTRACE], mask);

maintainer
#14 lore raw

Re: [PATCH v1 4/6] arm64: broadcast IPIs to pause remote CPUs

Quoted 23 lines · Yu Zhao
Quoted 21 lines · Marc Zyngier
Quoted 17 lines · Yu Zhao

Broadcast pseudo-NMI IPIs to pause remote CPUs for a short period of time, and then reliably resume them when the local CPU exits critical sections that preclude the execution of remote CPUs.

A typical example of such critical sections is BBM on kernel PTEs. HugeTLB Vmemmap Optimization (HVO) on arm64 was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the folllowing reason:

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Supporting BBM on kernel PTEs is one of the approaches that can make HVO theoretically safe on arm64.

Quoted 7 lines
Quoted 2 lines

Is the safety only theoretical? I would have expected that we'd use an approach that is absolutely rock-solid.

We've been trying to construct a repro against the original HVO (missing BBM), but so far no success. Hopefully a repro does exist, and then we'd be able to demonstrate the effectiveness of this series, which is only theoretical at the moment.

That wasn't my question.

Just because your HW doesn't show you a failure mode doesn't mean the issue doesn't exist. Or that someone will eventually make use of the relaxed aspects of the architecture and turn the behaviour you are relying on into the perfect memory corruption You absolutely cannot rely on an implementation-defined behaviour for this stuff.

Hence my question: is your current approach actually safe? In a non-theoretical manner?

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

Note that it is still possible for the paused CPUs to perform speculative translations. Such translations would cause spurious kernel PFs, which should be properly handled by is_spurious_el1_translation_fault().

Speculative translation faults are never reported, that'd be a CPU bug.

Right, I meant to say "speculative accesses that cause translations".

Quoted 8 lines
Quoted 3 lines

*Spurious* translation faults can be reported if the CPU doesn't implement FEAT_ETS2, for example, and that has to do with the ordering of memory access wrt page-table walking for the purpose of translations.

Just want to make sure I fully understand: after the local CPU sends TLBI (followed by DSB & ISB), FEAT_ETS2 would act like DSB & ISB on remote CPUs when they perform the invalidation, and therefore speculative accesses are ordered as well on remote CPUs.

ETS2 has nothing to do with TLBI. It has to do with non-cacheable (translation, access and address size) faults, and whether an older update to a translation is visible to a younger memory access without extra synchronisation. It definitely has nothing to do with remote CPUs (and the idea of a remote ISB, while cute, doesn't exist).

Quoted 4 lines

Also I'm assuming IPIs on remote CPUs don't act like a full barrier, and whatever they interrupt still can be speculatively executed even though the IPI hander itself doesn't access the vmemmap area undergoing BBM. Is this correct?

Full barrier *for what*? An interrupt is a CSE, which has the effects described in the ARM ARM, but that's about it.

Quoted 85 lines
Quoted 39 lines
Quoted 39 lines
---
 arch/arm64/include/asm/smp.h |  3 ++
 arch/arm64/kernel/smp.c      | 92 +++++++++++++++++++++++++++++++++---
 2 files changed, 88 insertions(+), 7 deletions(-)
arch/arm64/include/asm/smp.h +3 -0
index 2510eec026f7..cffb0cfed961 100644
@@ -133,6 +133,9 @@ bool cpus_are_stuck_in_kernel(void);
 extern void crash_smp_send_stop(void);
 extern bool smp_crash_stop_failed(void);
 
+void pause_remote_cpus(void);
+void resume_remote_cpus(void);
+
 #endif /* ifndef __ASSEMBLY__ */
 
 #endif /* ifndef __ASM_SMP_H */
arch/arm64/kernel/smp.c +6 -1
index 3b3f6b56e733..68829c6de1b1 100644
@@ -85,7 +85,12 @@ static int ipi_irq_base __ro_after_init;
 static int nr_ipi __ro_after_init = NR_IPI;
 static struct irq_desc *ipi_desc[MAX_IPI] __ro_after_init;
 
-static bool crash_stop;
+enum {
+     SEND_STOP = BIT(0),
+     CRASH_STOP = BIT(1),
+};
+
+static unsigned long stop_in_progress;
 
 static void ipi_setup(int cpu);
 
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 925

Quoted 5 lines
arch/arm64/kernel/smp.c +1 -0
@@ -917,6 +922,79 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
 #endif
 }
 
+static DEFINE_SPINLOCK(cpu_pause_lock);

PREEMPT_RT will turn this into a sleeping lock. Is it safe to sleep as you are dealing with kernel mappings?

Right, it should be a raw spinlock -- the caller disabled preemption, which as you said is required when dealing with the kernel mappings.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/smp.c +11 -0
+static cpumask_t paused_cpus;
+static cpumask_t resumed_cpus;
+
+static void pause_local_cpu(void)
+{
+     int cpu = smp_processor_id();
+
+     cpumask_clear_cpu(cpu, &resumed_cpus);
+     /*
+      * Paired with pause_remote_cpus() to confirm that this CPU not only
+      * will be paused but also can be reliably resumed.
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 941

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+      */
+     smp_wmb();
+     cpumask_set_cpu(cpu, &paused_cpus);
+     /* paused_cpus must be set before waiting on resumed_cpus. */
+     barrier();

I'm not sure what this is trying to enforce. Yes, the compiler won't reorder the set and the test.

Sorry I don't follow: does cpumask_set_cpu(), i.e., set_bit(), already contain a compiler barrier?

My understanding is that the compiler is free to reorder the set and test on those two independent variables, and make it like this:

  while (!cpumask_test_cpu(cpu, &resumed_cpus))
      cpu_relax();
  cpumask_set_cpu(cpu, &paused_cpus);

So the CPU sent the IPI would keep waiting on paused_cpus being set, and this CPU would keep waiting on resumed_cpus being set, which would end up with a deadlock.

Quoted 8 lines
Quoted 4 lines

But your comment seems to indicate that
also need to make sure the CPU preserves that ordering and short of a
DMB, the test below could be reordered.

If this CPU reorders the set and test like above, it wouldn't be a problem because the set would eventually appear on the other CPU that sent the IPI.

I don't get it. You are requiring that the compiler doesn't reorder things, but you're happy that the CPU reorders the same things. Surely this leads to the same outcome...

Quoted 28 lines
Quoted 6 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 944

Quoted 4 lines
arch/arm64/kernel/smp.c +3 -0
+     while (!cpumask_test_cpu(cpu, &resumed_cpus))
+             cpu_relax();
+     /* A typical example for sleep and wake-up functions. */

I'm not sure this is "typical",...

Sorry, this full barrier isn't needed. Apparently I didn't properly fix this from the previous attempt to use wfe()/sev() to make this function the sleeper for resume_remote_cpus() to wake up.

Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/smp.c +8 -0
+     smp_mb();
+     cpumask_clear_cpu(cpu, &paused_cpus);
+}
+
+void pause_remote_cpus(void)
+{
+     cpumask_t cpus_to_pause;
+
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 957

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+     lockdep_assert_cpus_held();
+     lockdep_assert_preemption_disabled();
+
+     cpumask_copy(&cpus_to_pause, cpu_online_mask);
+     cpumask_clear_cpu(smp_processor_id(), &cpus_to_pause);

This bitmap is manipulated outside of your cpu_pause_lock. What guarantees you can't have two CPUs stepping on each other here?

Quoted 1 line

Do you mean cpus_to_pause? If so, that's a local bitmap.

Ah yes, sorry.

Quoted 20 lines
Quoted 14 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 966

Quoted 10 lines
arch/arm64/kernel/smp.c +9 -0
+
+     spin_lock(&cpu_pause_lock);
+
+     WARN_ON_ONCE(!cpumask_empty(&paused_cpus));
+
+     smp_cross_call(&cpus_to_pause, IPI_CPU_STOP_NMI);
+
+     while (!cpumask_equal(&cpus_to_pause, &paused_cpus))
+             cpu_relax();

This can be a lot of things to compare, specially that you are explicitly mentioning large systems. Why can't this be implemented as a counter instead?

Agreed - that'd be sufficient and simpler.

Quoted 2 lines

Overall, this looks like stop_machine() in disguise. Why can't this use the existing infrastructure?

Quoted 7 lines

This came up during the previous discussion [1]. There are similarities. The main concern with reusing stop_machine() (or part of its current implementation) is that it may not meet the performance requirements. Refactoring might be possible, however, it seems to me (after checking the code again) that such a refactoring unlikely ends up cleaner or simpler codebase, especially after I got rid of CPU masks, as you suggested.

I would have expected to see an implementation handling faults during the break window. IPIs are slow, virtualised extremely badly, and pNMIs are rarely enabled, due to the long history of issues with it. At this stage, I'm not sure what you have here is any better than stop_machine(). On the other hand, faults should be the rarest thing,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#15 lore raw

Re: [PATCH v1 4/6] arm64: broadcast IPIs to pause remote CPUs

Quoted 34 lines · Marc Zyngier
Quoted 23 lines · Yu Zhao
Quoted 21 lines · Marc Zyngier
Quoted 17 lines · Yu Zhao

Broadcast pseudo-NMI IPIs to pause remote CPUs for a short period of time, and then reliably resume them when the local CPU exits critical sections that preclude the execution of remote CPUs.

A typical example of such critical sections is BBM on kernel PTEs. HugeTLB Vmemmap Optimization (HVO) on arm64 was disabled by commit 060a2c92d1b6 ("arm64: mm: hugetlb: Disable HUGETLB_PAGE_OPTIMIZE_VMEMMAP") due to the folllowing reason:

This is deemed UNPREDICTABLE by the Arm architecture without a break-before-make sequence (make the PTE invalid, TLBI, write the new valid PTE). However, such sequence is not possible since the vmemmap may be concurrently accessed by the kernel.

Supporting BBM on kernel PTEs is one of the approaches that can make HVO theoretically safe on arm64.

Quoted 7 lines
Quoted 2 lines

Is the safety only theoretical? I would have expected that we'd use an approach that is absolutely rock-solid.

We've been trying to construct a repro against the original HVO (missing BBM), but so far no success. Hopefully a repro does exist, and then we'd be able to demonstrate the effectiveness of this series, which is only theoretical at the moment.

Quoted 7 lines

That wasn't my question.

Just because your HW doesn't show you a failure mode doesn't mean the issue doesn't exist. Or that someone will eventually make use of the relaxed aspects of the architecture and turn the behaviour you are relying on into the perfect memory corruption You absolutely cannot rely on an implementation-defined behaviour for this stuff.

I agree.

Quoted 1 line

Hence my question: is your current approach actually safe?

AFAIK, yes.

Quoted 2 lines

In a
non-theoretical manner?

We are confident enough to try it in our production, but it doesn't mean it's bug free. It would take more eyeballs and soak time before it can prove itself.

Quoted 19 lines
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Note that it is still possible for the paused CPUs to perform speculative translations. Such translations would cause spurious kernel PFs, which should be properly handled by is_spurious_el1_translation_fault().

Speculative translation faults are never reported, that'd be a CPU bug.

Right, I meant to say "speculative accesses that cause translations".

Quoted 8 lines
Quoted 3 lines

*Spurious* translation faults can be reported if the CPU doesn't implement FEAT_ETS2, for example, and that has to do with the ordering of memory access wrt page-table walking for the purpose of translations.

Just want to make sure I fully understand: after the local CPU sends TLBI (followed by DSB & ISB), FEAT_ETS2 would act like DSB & ISB on remote CPUs when they perform the invalidation, and therefore speculative accesses are ordered as well on remote CPUs.

Quoted 4 lines

ETS2 has nothing to do with TLBI. It has to do with non-cacheable (translation, access and address size) faults, and whether an older update to a translation is visible to a younger memory access without extra synchronisation.

Is it correct to say that *without* ETS2, we also wouldn't see spurious faults on the local CPU from the following BBM sequence?
clear_pte(); dsb(); isb(); tlbi(); dsb(); isb(); set_valid_pte();
dsb(); isb(); ...

Quoted 10 lines

It definitely has nothing to do with remote
CPUs (and the idea of a remote ISB, while cute, doesn't exist).

Quoted 4 lines

Also I'm assuming IPIs on remote CPUs don't act like a full barrier, and whatever they interrupt still can be speculatively executed even though the IPI hander itself doesn't access the vmemmap area undergoing BBM. Is this correct?

Full barrier *for what*? An interrupt is a CSE, which has the effects described in the ARM ARM, but that's about it.

Sorry for the confusion. I hope the following could explain what's in my mind:

local cpu remote cpu

send ipi
cse (= dsb(); isb() ?)
enters ipi handler
sets cpu_paused
sees cpu_paused
clear_pte()
dsb();
tlbi();
dsb(); isb()
set_valid_pte()
dsb();
sets cpu_resumed
sees cpu_resumed
No isb()
exits ipi handler

My current understanding is:
Because exception exit doesn't have the CSE and there are no isb()s within the IPI handler running on the remote CPU, it's possible for the remote CPU to speculative execute the next instruction before it sees the valid PTE and trigger a spurious fault.

Quoted 89 lines
Quoted 89 lines
Quoted 38 lines
Quoted 38 lines
---
 arch/arm64/include/asm/smp.h |  3 ++
 arch/arm64/kernel/smp.c      | 92 +++++++++++++++++++++++++++++++++---
 2 files changed, 88 insertions(+), 7 deletions(-)
arch/arm64/include/asm/smp.h +3 -0
index 2510eec026f7..cffb0cfed961 100644
@@ -133,6 +133,9 @@ bool cpus_are_stuck_in_kernel(void);
 extern void crash_smp_send_stop(void);
 extern bool smp_crash_stop_failed(void);
 
+void pause_remote_cpus(void);
+void resume_remote_cpus(void);
+
 #endif /* ifndef __ASSEMBLY__ */
 
 #endif /* ifndef __ASM_SMP_H */
arch/arm64/kernel/smp.c +6 -1
index 3b3f6b56e733..68829c6de1b1 100644
@@ -85,7 +85,12 @@ static int ipi_irq_base __ro_after_init;
 static int nr_ipi __ro_after_init = NR_IPI;
 static struct irq_desc *ipi_desc[MAX_IPI] __ro_after_init;
 
-static bool crash_stop;
+enum {
+     SEND_STOP = BIT(0),
+     CRASH_STOP = BIT(1),
+};
+
+static unsigned long stop_in_progress;
 
 static void ipi_setup(int cpu);
 
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 925

Quoted 5 lines
arch/arm64/kernel/smp.c +1 -0
@@ -917,6 +922,79 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
 #endif
 }
 
+static DEFINE_SPINLOCK(cpu_pause_lock);

PREEMPT_RT will turn this into a sleeping lock. Is it safe to sleep as you are dealing with kernel mappings?

Right, it should be a raw spinlock -- the caller disabled preemption, which as you said is required when dealing with the kernel mappings.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/smp.c +11 -0
+static cpumask_t paused_cpus;
+static cpumask_t resumed_cpus;
+
+static void pause_local_cpu(void)
+{
+     int cpu = smp_processor_id();
+
+     cpumask_clear_cpu(cpu, &resumed_cpus);
+     /*
+      * Paired with pause_remote_cpus() to confirm that this CPU not only
+      * will be paused but also can be reliably resumed.
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 941

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+      */
+     smp_wmb();
+     cpumask_set_cpu(cpu, &paused_cpus);
+     /* paused_cpus must be set before waiting on resumed_cpus. */
+     barrier();

I'm not sure what this is trying to enforce. Yes, the compiler won't reorder the set and the test.

Sorry I don't follow: does cpumask_set_cpu(), i.e., set_bit(), already contain a compiler barrier?

My understanding is that the compiler is free to reorder the set and test on those two independent variables, and make it like this:

  while (!cpumask_test_cpu(cpu, &resumed_cpus))
      cpu_relax();
  cpumask_set_cpu(cpu, &paused_cpus);

So the CPU sent the IPI would keep waiting on paused_cpus being set, and this CPU would keep waiting on resumed_cpus being set, which would end up with a deadlock.

Quoted 4 lines

But your comment seems to indicate that
also need to make sure the CPU preserves that ordering and short of a
DMB, the test below could be reordered.

Quoted 7 lines
Quoted 3 lines

If this CPU reorders the set and test like above, it wouldn't be a problem because the set would eventually appear on the other CPU that sent the IPI.

I don't get it. You are requiring that the compiler doesn't reorder things, but you're happy that the CPU reorders the same things. Surely this leads to the same outcome...

I was thinking that the compiler might be able to reorder even if it can't prove the loop actually terminates. But I now think that shouldn't happen. So yes, I agree with what you said earlier that "the compiler won't reorder the set and the test".

Quoted 59 lines
Quoted 27 lines
Quoted 5 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 944

Quoted 3 lines
arch/arm64/kernel/smp.c +3 -0
+     while (!cpumask_test_cpu(cpu, &resumed_cpus))
+             cpu_relax();
+     /* A typical example for sleep and wake-up functions. */

I'm not sure this is "typical",...

Sorry, this full barrier isn't needed. Apparently I didn't properly fix this from the previous attempt to use wfe()/sev() to make this function the sleeper for resume_remote_cpus() to wake up.

Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/smp.c +8 -0
+     smp_mb();
+     cpumask_clear_cpu(cpu, &paused_cpus);
+}
+
+void pause_remote_cpus(void)
+{
+     cpumask_t cpus_to_pause;
+
Quoted 8 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 957

Quoted 5 lines
arch/arm64/kernel/smp.c +5 -0
+     lockdep_assert_cpus_held();
+     lockdep_assert_preemption_disabled();
+
+     cpumask_copy(&cpus_to_pause, cpu_online_mask);
+     cpumask_clear_cpu(smp_processor_id(), &cpus_to_pause);

This bitmap is manipulated outside of your cpu_pause_lock. What guarantees you can't have two CPUs stepping on each other here?

Quoted 1 line

Do you mean cpus_to_pause? If so, that's a local bitmap.

Ah yes, sorry.

Quoted 20 lines
Quoted 14 lines

arch/arm64/kernel/smp.c static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs · line 966

Quoted 10 lines
arch/arm64/kernel/smp.c +9 -0
+
+     spin_lock(&cpu_pause_lock);
+
+     WARN_ON_ONCE(!cpumask_empty(&paused_cpus));
+
+     smp_cross_call(&cpus_to_pause, IPI_CPU_STOP_NMI);
+
+     while (!cpumask_equal(&cpus_to_pause, &paused_cpus))
+             cpu_relax();

This can be a lot of things to compare, specially that you are explicitly mentioning large systems. Why can't this be implemented as a counter instead?

Agreed - that'd be sufficient and simpler.

Quoted 2 lines

Overall, this looks like stop_machine() in disguise. Why can't this use the existing infrastructure?

Quoted 7 lines

This came up during the previous discussion [1]. There are similarities. The main concern with reusing stop_machine() (or part of its current implementation) is that it may not meet the performance requirements. Refactoring might be possible, however, it seems to me (after checking the code again) that such a refactoring unlikely ends up cleaner or simpler codebase, especially after I got rid of CPU masks, as you suggested.

Quoted 5 lines

I would have expected to see an implementation handling faults during the break window. IPIs are slow, virtualised extremely badly, and pNMIs are rarely enabled, due to the long history of issues with it. At this stage, I'm not sure what you have here is any better than stop_machine(). On the other hand, faults should be the rarest thing,

Let me measure stop_machine() and see how that works for our production. Meanwhile, if you don't mind that we leave the IPI approach along with the kernel PF approach on the table?

Thanks.

author
#16 lore raw

PATCH 5/6 arm64: pause remote CPUs to update vmemmap

In reply to Yu Zhao

Pause remote CPUs so that the local CPU can follow the proper BBM sequence to safely update the vmemmap mapping struct page areas.

While updating the vmemmap, it is guaranteed that neither the local CPU nor the remote ones will access the struct page area being updated, and therefore they should not trigger (non-spurious) kernel PFs.

---
 arch/arm64/include/asm/pgalloc.h | 69 ++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
arch/arm64/include/asm/pgalloc.h +69 -0
index 8ff5f2a2579e..f50f79f57c1e 100644
@@ -12,6 +12,7 @@
 #include <asm/processor.h>
 #include <asm/cacheflush.h>
 #include <asm/tlbflush.h>
+#include <asm/cpu.h>
 
 #define __HAVE_ARCH_PGD_FREE
 #define __HAVE_ARCH_PUD_FREE
@@ -137,4 +138,72 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
 	__pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE | PMD_TABLE_PXN);
 }
 
+#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+
+#define VMEMMAP_ARCH_TLB_FLUSH_FLAGS (VMEMMAP_SPLIT_NO_TLB_FLUSH | VMEMMAP_REMAP_NO_TLB_FLUSH)
+
+#define vmemmap_update_supported vmemmap_update_supported
+static inline bool vmemmap_update_supported(void)
+{
+	return system_uses_irq_prio_masking();
+}
+
+#define vmemmap_update_lock vmemmap_update_lock
+static inline void vmemmap_update_lock(void)
+{
+	cpus_read_lock();
+}
+
+#define vmemmap_update_unlock vmemmap_update_unlock
+static inline void vmemmap_update_unlock(void)
+{
+	cpus_read_unlock();
+}
+
+#define vmemmap_update_pte_range_start vmemmap_update_pte_range_start
+static inline void vmemmap_update_pte_range_start(pte_t *pte,
+						  unsigned long start, unsigned long end)
+{
+	unsigned long addr;
+
+	local_irq_disable();
+	pause_remote_cpus();
+
+	for (addr = start; addr != end; addr += PAGE_SIZE, pte++)
+		pte_clear(&init_mm, addr, pte);
+
+	flush_tlb_kernel_range(start, end);
+}
+
+#define vmemmap_update_pte_range_end vmemmap_update_pte_range_end
+static inline void vmemmap_update_pte_range_end(void)
+{
+	resume_remote_cpus();
+	local_irq_enable();
+}
+
+#define vmemmap_update_pmd_range_start vmemmap_update_pmd_range_start
+static inline void vmemmap_update_pmd_range_start(pmd_t *pmd,
+						  unsigned long start, unsigned long end)
+{
+	unsigned long addr;
+
+	local_irq_disable();
+	pause_remote_cpus();
+
+	for (addr = start; addr != end; addr += PMD_SIZE, pmd++)
+		pmd_clear(pmd);
+
+	flush_tlb_kernel_range(start, end);
+}
+
+#define vmemmap_update_pmd_range_end vmemmap_update_pmd_range_end
+static inline void vmemmap_update_pmd_range_end(void)
+{
+	resume_remote_cpus();
+	local_irq_enable();
+}
+
+#endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */
+
 #endif
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
author
#17 lore raw

PATCH 6/6 arm64: select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP

In reply to Yu Zhao

To use HVO, make sure that the kernel is booted with pseudo-NMI enabled by "irqchip.gicv3_pseudo_nmi=1", as well as "hugetlb_free_vmemmap=on" unless HVO is enabled by default.

Note that HVO checks the pseudo-NMI capability and is disabled at runtime if the capability turns out not supported. Successfully enabling HVO should have the following:

  # dmesg | grep NMI
  GICv3: Pseudo-NMIs enabled using ...
  # sysctl vm.hugetlb_optimize_vmemmap
  vm.hugetlb_optimize_vmemmap = 1
---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
index fd9df6dcc593..e93745f819d9 100644
@@ -109,6 +109,7 @@ config ARM64
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
 	select ARCH_WANT_FRAME_POINTERS
 	select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36)
+	select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP
 	select ARCH_WANT_LD_ORPHAN_WARN
 	select ARCH_WANTS_EXECMEM_LATE if EXECMEM
 	select ARCH_WANTS_NO_INSTR
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog