Skip to contentSkip to conversation

riscv: Reduce ARCH_KMALLOC_MINALIGN to 8

PATCH Superseded Jisheng Zhang 4 participants 20 messages May 26, 2023 – Jun 1, 2023 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover riscv: Reduce ARCH_KMALLOC_MINALIGN to 8

Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E 64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus it brings some bad effects to for coherent platforms:

Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and kmalloc-8 slab caches don't exist any more, they are replaced with either kmalloc-128 or kmalloc-64.

Secondly, larger than necessary kmalloc aligned allocations results in unnecessary cache/TLB pressure.

This issue also exists on arm64 platforms. From last year, Catalin tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage in various drivers with ARCH_DMA_MINALIGN etc.

One fact we can make use of for riscv: if the CPU doesn't support ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on Catalin's work and above fact, we can easily solve the kmalloc align issue for riscv: we can override dma_get_cache_alignment(), then let it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.

So what about if the CPU supports ZICBOM and T-HEAD CMO, but all the devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the kmalloc minimum alignment, nothing changed in this case. This case can be improved in the future.

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

patch 1,2,3,4 are either clean up or preparation patches. patch5 allows kmalloc() caches aligned to the smallest value. patch6 enables DMA_BOUNCE_UNALIGNED_KMALLOC.

After this series:

As for coherent platforms, kmalloc-{8,16,32,96} caches come back on coherent both RV32 and RV64 platforms, I.E !ZICBOM and !THEAD_CMO.

As for noncoherent RV32 platforms, nothing changed.

As for noncoherent RV64 platforms, I.E either ZICBOM or THEAD_CMO, the above kmalloc caches also come back if > 4GB memory or users pass "swiotlb=mmnn,force" to force swiotlb creation if <= 4GB memory. How much mmnn should be depends on the specific platform, it need to be tried and tested all possible usage case on the specific hardware. For example, I can use the minimal I/O TLB slabs on Sipeed M1S Dock.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

Jisheng Zhang (6):
riscv: errata: thead: only set cbom size & noncoherent during boot
riscv: mm: mark CBO relate initialization funcs as __init
riscv: mm: mark noncoherent_supported as __ro_after_init
riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()
riscv: allow kmalloc() caches aligned to the smallest value
riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC for !dma_coherent

 arch/riscv/Kconfig                  |  1 +
 arch/riscv/errata/thead/errata.c    | 22 ++++++++++++++--------
 arch/riscv/include/asm/cache.h      | 14 ++++++++++++++
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/cacheflush.c          |  8 ++++----
 arch/riscv/mm/dma-noncoherent.c     | 16 +++++++++++-----
 7 files changed, 51 insertions(+), 20 deletions(-)
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 1/6 riscv: errata: thead: only set cbom size & noncoherent during boot

The CBOM size and whether the HW is noncoherent is known and determined during booting and won't change after that.

---
 arch/riscv/errata/thead/errata.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/riscv/errata/thead/errata.c +5 -2
index c259dc925ec1..be84b14f0118 100644
@@ -45,8 +45,11 @@ static bool errata_probe_cmo(unsigned int stage,
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
-	riscv_cbom_block_size = L1_CACHE_BYTES;
-	riscv_noncoherent_supported();
+	if (stage == RISCV_ALTERNATIVES_BOOT) {
+		riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported();
+	}
+
 	return true;
 }
 
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw

PATCH 2/6 riscv: mm: mark CBO relate initialization funcs as __init

In reply to Jisheng Zhang

The two functions cbo_get_block_size() and riscv_init_cbo_blocksizes() are only called during booting, mark them as __init.

---
 arch/riscv/mm/cacheflush.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/riscv/mm/cacheflush.c +4 -4
index fca532ddf3ec..fbc59b3f69f2 100644
@@ -104,9 +104,9 @@ EXPORT_SYMBOL_GPL(riscv_cbom_block_size);
 unsigned int riscv_cboz_block_size;
 EXPORT_SYMBOL_GPL(riscv_cboz_block_size);
 
-static void cbo_get_block_size(struct device_node *node,
-			       const char *name, u32 *block_size,
-			       unsigned long *first_hartid)
+static void __init cbo_get_block_size(struct device_node *node,
+				      const char *name, u32 *block_size,
+				      unsigned long *first_hartid)
 {
 	unsigned long hartid;
 	u32 val;
@@ -126,7 +126,7 @@ static void cbo_get_block_size(struct device_node *node,
 	}
 }
 
-void riscv_init_cbo_blocksizes(void)
+void __init riscv_init_cbo_blocksizes(void)
 {
 	unsigned long cbom_hartid, cboz_hartid;
 	u32 cbom_block_size = 0, cboz_block_size = 0;
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw

PATCH 3/6 riscv: mm: mark noncoherent_supported as __ro_after_init

In reply to Jisheng Zhang

The noncoherent_supported indicates whether the HW is coherent or not, it won't change after booting, mark it as __ro_after_init.

---
 arch/riscv/mm/dma-noncoherent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/mm/dma-noncoherent.c +1 -1
index d919efab6eba..d51a75864e53 100644
@@ -10,7 +10,7 @@
 #include <linux/mm.h>
 #include <asm/cacheflush.h>
 
-static bool noncoherent_supported;
+static bool noncoherent_supported __ro_after_init;
 
 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
 			      enum dma_data_direction dir)
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw

PATCH 4/6 riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

In reply to Jisheng Zhang

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +11 -8
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
-	return true;
+	return cmo;
 }
 
 static bool errata_probe_pmu(unsigned int stage,
arch/riscv/include/asm/cacheflush.h +2 -2
index 8091b8bf4883..9d056c9b625a 100644
@@ -54,9 +54,9 @@ extern unsigned int riscv_cboz_block_size;
 void riscv_init_cbo_blocksizes(void);
 
 #ifdef CONFIG_RISCV_DMA_NONCOHERENT
-void riscv_noncoherent_supported(void);
+void riscv_noncoherent_supported(bool cmo);
 #else
-static inline void riscv_noncoherent_supported(void) {}
+static inline void riscv_noncoherent_supported(bool cmo) {}
 #endif
 
 /*
arch/riscv/kernel/setup.c +5 -1
index 36b026057503..565f3e20169b 100644
@@ -264,6 +264,7 @@ static void __init parse_dtb(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	bool cmo;
 	parse_dtb();
 	setup_initial_init_mm(_stext, _etext, _edata, _end);
 
@@ -298,7 +299,10 @@ void __init setup_arch(char **cmdline_p)
 	apply_boot_alternatives();
 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
 	    riscv_isa_extension_available(NULL, ZICBOM))
-		riscv_noncoherent_supported();
+		cmo = true;
+	else
+		cmo = false;
+	riscv_noncoherent_supported(cmo);
 }
 
 static int __init topology_init(void)
arch/riscv/mm/dma-noncoherent.c +6 -4
index d51a75864e53..0e172e2b4751 100644
@@ -72,9 +72,11 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	dev->dma_coherent = coherent;
 }
 
-void riscv_noncoherent_supported(void)
+void riscv_noncoherent_supported(bool cmo)
 {
-	WARN(!riscv_cbom_block_size,
-	     "Non-coherent DMA support enabled without a block size\n");
-	noncoherent_supported = true;
+	if (cmo) {
+		WARN(!riscv_cbom_block_size,
+		     "Non-coherent DMA support enabled without a block size\n");
+		noncoherent_supported = true;
+	}
 }
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#13 lore raw

PATCH 5/6 riscv: allow kmalloc() caches aligned to the smallest value

In reply to Jisheng Zhang

Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E 64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus it brings some bad effects to for coherent platforms:

Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and kmalloc-8 slab caches don't exist any more, they are replaced with either kmalloc-128 or kmalloc-64.

Secondly, larger than necessary kmalloc aligned allocations results in unnecessary cache/TLB pressure.

This issue also exists on arm64 platforms. From last year, Catalin tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage in various drivers with ARCH_DMA_MINALIGN etc.

One fact we can make use of for riscv: if the CPU doesn't support ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on Catalin's work and above fact, we can easily solve the kmalloc align issue for riscv: we can override dma_get_cache_alignment(), then let it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.

So what about if the CPU supports ZICBOM and T-HEAD CMO, but all the devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the kmalloc minimum alignment, nothing changed in this case. This case can be improved in the future.

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

---
 arch/riscv/include/asm/cache.h  | 14 ++++++++++++++
 arch/riscv/mm/dma-noncoherent.c |  4 ++++
 2 files changed, 18 insertions(+)
arch/riscv/include/asm/cache.h +14 -0
index d3036df23ccb..2174fe7bac9a 100644
@@ -13,6 +13,7 @@
 
 #ifdef CONFIG_RISCV_DMA_NONCOHERENT
 #define ARCH_DMA_MINALIGN L1_CACHE_BYTES
+#define ARCH_KMALLOC_MINALIGN	(8)
 #endif
 
 /*
@@ -23,4 +24,17 @@
 #define ARCH_SLAB_MINALIGN	16
 #endif
 
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_RISCV_DMA_NONCOHERENT
+extern int dma_cache_alignment;
+#define dma_get_cache_alignment dma_get_cache_alignment
+static inline int dma_get_cache_alignment(void)
+{
+	return dma_cache_alignment;
+}
+#endif
+
+#endif	/* __ASSEMBLY__ */
+
 #endif /* _ASM_RISCV_CACHE_H */
arch/riscv/mm/dma-noncoherent.c +4 -0
index 0e172e2b4751..21b553c299db 100644
@@ -11,6 +11,8 @@
 #include <asm/cacheflush.h>
 
 static bool noncoherent_supported __ro_after_init;
+int dma_cache_alignment __ro_after_init = ARCH_DMA_MINALIGN;
+EXPORT_SYMBOL(dma_cache_alignment);
 
 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
 			      enum dma_data_direction dir)
@@ -78,5 +80,7 @@ void riscv_noncoherent_supported(bool cmo)
 		WARN(!riscv_cbom_block_size,
 		     "Non-coherent DMA support enabled without a block size\n");
 		noncoherent_supported = true;
+	} else {
+		dma_cache_alignment = 1;
 	}
 }
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#19 lore raw

PATCH 6/6 riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC for !dma_coherent

In reply to Jisheng Zhang

With the DMA bouncing of unaligned kmalloc() buffers now in place, enable it for riscv when RISCV_DMA_NONCOHERENT=y to allow the kmalloc-{8,16,32,96} caches. Since RV32 doesn't enable SWIOTLB yet, and I didn't see any dma noncoherent RV32 platforms in the mainline, so skip RV32 now by only enabling DMA_BOUNCE_UNALIGNED_KMALLOC if SWIOTLB is available. Once we see such requirement on RV32, we can enable it then.

NOTE: we didn't force to create the swiotlb buffer even when the end of RAM is within the 32-bit physical address range. That's to say:
For RV64 with > 4GB memory, the feature is enabled.
For RV64 with <= 4GB memory, the feature isn't enabled by default. We rely on users to pass "swiotlb=mmnn,force" where mmnn is the Number of I/O TLB slabs, see kernel-parameters.txt for details.

Tested on Sipeed Lichee Pi 4A with 8GB DDR and Sipeed M1S BL808 Dock board.

---
 arch/riscv/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/Kconfig +1 -0
index b958f67f9a12..14f030cd6357 100644
@@ -260,6 +260,7 @@ config RISCV_DMA_NONCOHERENT
 	select ARCH_HAS_SYNC_DMA_FOR_CPU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select DMA_DIRECT_REMAP
+	select DMA_BOUNCE_UNALIGNED_KMALLOC if SWIOTLB
 
 config AS_HAS_INSN
 	def_bool $(as-instr,.insn r 51$(comma) 0$(comma) 0$(comma) t0$(comma) t0$(comma) zero)
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] riscv: mm: mark CBO relate initialization funcs as __init

In reply to Jisheng Zhang
Quoted 4 lines · Jisheng Zhang

The two functions cbo_get_block_size() and riscv_init_cbo_blocksizes() are only called during booting, mark them as __init.

Thanks,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#8 lore raw

Re: [PATCH 4/6] riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

In reply to Jisheng Zhang

Hey Jisheng,

Quoted 35 lines · Jisheng Zhang

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +7 -5
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+

arch/riscv/errata/thead/errata.c static bool errata_probe_pbmt(unsigned int stage, · line 56

Quoted 10 lines
arch/riscv/errata/thead/errata.c +4 -3
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
-	return true;
+	return cmo;

I don't really understand the changes that you are making to this function, so that is tries really hard to call riscv_noncoherent_supported(). Why do we need to always call the function in the erratum's probe function, if the erratum is not detected, given that riscv_noncoherent_supported() is called immediately after apply_boot_alternatives() in setup_arch()?

arch/riscv/include/asm/cacheflush.h extern unsigned int riscv_cboz_block_size; · line 57

Quoted 13 lines
arch/riscv/errata/thead/errata.c +0 -0
 }
 
 static bool errata_probe_pmu(unsigned int stage,
arch/riscv/include/asm/cacheflush.h +1 -1
index 8091b8bf4883..9d056c9b625a 100644
@@ -54,9 +54,9 @@ extern unsigned int riscv_cboz_block_size;
 void riscv_init_cbo_blocksizes(void);
 
 #ifdef CONFIG_RISCV_DMA_NONCOHERENT
-void riscv_noncoherent_supported(void);
+void riscv_noncoherent_supported(bool cmo);

I think it would "read better" if you renamed this variable to "have_cmo".

Quoted 19 lines
arch/riscv/include/asm/cacheflush.h +1 -1
 #else
-static inline void riscv_noncoherent_supported(void) {}
+static inline void riscv_noncoherent_supported(bool cmo) {}
 #endif
 
 /*
arch/riscv/kernel/setup.c +1 -0
index 36b026057503..565f3e20169b 100644
@@ -264,6 +264,7 @@ static void __init parse_dtb(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	bool cmo;
 	parse_dtb();
 	setup_initial_init_mm(_stext, _etext, _edata, _end);
 

arch/riscv/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 305

Quoted 8 lines
arch/riscv/kernel/setup.c +4 -1
@@ -298,7 +299,10 @@ void __init setup_arch(char **cmdline_p)
 	apply_boot_alternatives();
 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
 	    riscv_isa_extension_available(NULL, ZICBOM))
-		riscv_noncoherent_supported();
+		cmo = true;
+	else
+		cmo = false;
+	riscv_noncoherent_supported(cmo);

As a nit, could you put a newline before the call to riscv_noncoherent_supported()?

Quoted 12 lines
arch/riscv/kernel/setup.c +0 -0
 }
 
 static int __init topology_init(void)
arch/riscv/mm/dma-noncoherent.c +0 -1
index d51a75864e53..0e172e2b4751 100644
@@ -72,9 +72,11 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	dev->dma_coherent = coherent;
 }
 
-void riscv_noncoherent_supported(void)

arch/riscv/mm/dma-noncoherent.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 81

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c +6 -3
+void riscv_noncoherent_supported(bool cmo)
 {
-	WARN(!riscv_cbom_block_size,
-	     "Non-coherent DMA support enabled without a block size\n");
-	noncoherent_supported = true;
+	if (cmo) {
+		WARN(!riscv_cbom_block_size,
+		     "Non-coherent DMA support enabled without a block size\n");
+		noncoherent_supported = true;
+	}

The other places that we do a WARN() because of screwed up devicetrees for CMO things, we do a WARN_TAINT(CPU_OUT_OF_SPEC). Should we do the same here too?

Cheers,
Conor.

#14 lore raw

Re: [PATCH 5/6] riscv: allow kmalloc() caches aligned to the smallest value

In reply to Jisheng Zhang
Quoted 39 lines · Jisheng Zhang

Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E 64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus it brings some bad effects to for coherent platforms:

Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and kmalloc-8 slab caches don't exist any more, they are replaced with either kmalloc-128 or kmalloc-64.

Secondly, larger than necessary kmalloc aligned allocations results in unnecessary cache/TLB pressure.

This issue also exists on arm64 platforms. From last year, Catalin tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage in various drivers with ARCH_DMA_MINALIGN etc.

One fact we can make use of for riscv: if the CPU doesn't support ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on Catalin's work and above fact, we can easily solve the kmalloc align issue for riscv: we can override dma_get_cache_alignment(), then let it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.

So what about if the CPU supports ZICBOM and T-HEAD CMO, but all the devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the kmalloc minimum alignment, nothing changed in this case. This case can be improved in the future.

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...
Quoted 6 lines

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

Fails to build chief, with loads of:
linux/dma-mapping.h:546:19: error: redefinition of 'dma_get_cache_alignment'

And for 32-bit there's also a rake of:
include/linux/slab.h:239:9: warning: 'ARCH_KMALLOC_MINALIGN' macro redefined [-Wmacro-redefined]

At the very least, reproducable with rv32_defconfig.

Cheers,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#15 lore raw

Re: [PATCH 5/6] riscv: allow kmalloc() caches aligned to the smallest value

Quoted 17 lines · Conor Dooley
Quoted 9 lines · Jisheng Zhang

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...
Quoted 6 lines

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

Quoted 7 lines

Fails to build chief, with loads of:
linux/dma-mapping.h:546:19: error: redefinition of 'dma_get_cache_alignment'

And for 32-bit there's also a rake of:
include/linux/slab.h:239:9: warning: 'ARCH_KMALLOC_MINALIGN' macro redefined [-Wmacro-redefined]

At the very least, reproducable with rv32_defconfig.

Have you this it on top of the KMALLOC_MINALIGN preparation series?

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

Signature · 6 lines
Catalin

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#16 lore raw

Re: [PATCH 5/6] riscv: allow kmalloc() caches aligned to the smallest value

Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Conor Dooley
Quoted 9 lines · Jisheng Zhang

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

While I think of it, Link: goes at the start of the line, the [1] should go at the end (although I don't think you actually reference the link anywhere in the text & it'll probably not be particularly relevant if a subsequent revision of that patchset is applied.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Fails to build chief, with loads of:
linux/dma-mapping.h:546:19: error: redefinition of 'dma_get_cache_alignment'

Quoted 8 lines
Quoted 4 lines

And for 32-bit there's also a rake of:
include/linux/slab.h:239:9: warning: 'ARCH_KMALLOC_MINALIGN' macro redefined [-Wmacro-redefined]

At the very least, reproducable with rv32_defconfig.

Have you this it on top of the KMALLOC_MINALIGN preparation series?

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

Oh, no. Thanks for pointing that out.
Our automation stuff only uses what is in riscv/{for-next,master,fixes}. Unless my reading comprehension is particularly bad of late it was non-obvious that this depended on something that had not yet been applied - it sounded like your series had already been merged last year. Apologies for the noise then on this patch, but please try to be more clear about what the dependencies actually are Jisheng.

Cheers,
Conor.

#17 lore raw

Re: [PATCH 5/6] riscv: allow kmalloc() caches aligned to the smallest value

Quoted 15 lines · Conor Dooley
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Conor Dooley
Quoted 12 lines · Jisheng Zhang

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

While I think of it, Link: goes at the start of the line, the [1] should go at the end (although I don't think you actually reference the link anywhere in the text & it'll probably not be particularly relevant if a subsequent revision of that patchset is applied.

I plan to post at least one more. I'd suggest the risc-v patchset to only go in once my series landed.

Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 1 line

Fails to build chief, with loads of:
linux/dma-mapping.h:546:19: error: redefinition of 'dma_get_cache_alignment'

And for 32-bit there's also a rake of:
include/linux/slab.h:239:9: warning: 'ARCH_KMALLOC_MINALIGN' macro redefined [-Wmacro-redefined]

At the very least, reproducable with rv32_defconfig.

Have you this it on top of the KMALLOC_MINALIGN preparation series?

Quoted 7 lines
Quoted 1 line

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

Oh, no. Thanks for pointing that out.
Our automation stuff only uses what is in riscv/{for-next,master,fixes}. Unless my reading comprehension is particularly bad of late it was non-obvious that this depended on something that had not yet been applied - it sounded like your series had already been merged last year.

Yeah, it was only obvious to me since it was my series ;).

Signature · 1 line
Catalin
#18 lore raw

Re: [PATCH 5/6] riscv: allow kmalloc() caches aligned to the smallest value

Quoted 18 lines · Catalin Marinas
Quoted 17 lines · Conor Dooley
Quoted 15 lines · Catalin Marinas
Quoted 14 lines · Conor Dooley
Quoted 13 lines · Jisheng Zhang

After this patch, a simple test of booting to a small buildroot rootfs on qemu shows:

kmalloc-96           5041    5041     96  ...
kmalloc-64           9606    9606     64  ...
kmalloc-32           5128    5128     32  ...
kmalloc-16           7682    7682     16  ...
kmalloc-8           10246   10246      8  ...

So we save about 1268KB memory. The saving will be much larger in normal OS env on real HW platforms.

[1] Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/

Quoted 7 lines
Quoted 4 lines

While I think of it, Link: goes at the start of the line, the [1] should go at the end (although I don't think you actually reference the link anywhere in the text & it'll probably not be particularly relevant if a subsequent revision of that patchset is applied.

I plan to post at least one more. I'd suggest the risc-v patchset to only go in once my series landed.

Sure I will wait for your series landing in linus tree firstly.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 2 lines

Fails to build chief, with loads of:
linux/dma-mapping.h:546:19: error: redefinition of 'dma_get_cache_alignment'

And for 32-bit there's also a rake of:
include/linux/slab.h:239:9: warning: 'ARCH_KMALLOC_MINALIGN' macro redefined [-Wmacro-redefined]

At the very least, reproducable with rv32_defconfig.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Have you this it on top of the KMALLOC_MINALIGN preparation series?

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

Oh, no. Thanks for pointing that out.
Our automation stuff only uses what is in riscv/{for-next,master,fixes}. Unless my reading comprehension is particularly bad of late it was

Aha I dunno this mechanism before.

Quoted 1 line
Quoted 1 line

non-obvious that this depended on something that had not yet been

Your reading comprehension is good ;) I just listed the dependency but didn't explictly mention its merge status.

I will wait for Catalin's series being merged.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw

Re: [PATCH 4/6] riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

In reply to Conor Dooley
Quoted 1 line · Conor Dooley

Hey Jisheng,

Hi Conor,

Quoted 45 lines · Jisheng Zhang
Quoted 43 lines

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +10 -7
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
Quoted 7 lines

arch/riscv/errata/thead/errata.c static bool errata_probe_pbmt(unsigned int stage, · line 56

Quoted 2 lines
arch/riscv/errata/thead/errata.c +1 -1
-	return true;
+	return cmo;

I don't really understand the changes that you are making to this function, so that is tries really hard to call riscv_noncoherent_supported(). Why do we need to always call the function in the erratum's probe function, if the erratum is not detected, given

In one unified kernel Image, to support both coherent and noncoherent platforms(currently, either T-HEAD CMO or ZICBOM), we need to let the kmalloc meet both cases, specifically, ARCH_DMA_MINALIGN aligned. Once we know the underlying HW is coherent, I.E neither T-HEAD CMO nor ZICBOM, we need to notice kmalloc we are safe to reduce the alignment to 1. The notice action is done in patch 5:

+ } else {
+ dma_cache_alignment = 1;

Quoted 78 lines

that riscv_noncoherent_supported() is called immediately after apply_boot_alternatives() in setup_arch()?

arch/riscv/include/asm/cacheflush.h extern unsigned int riscv_cboz_block_size; · line 57

Quoted 13 lines
arch/riscv/errata/thead/errata.c +0 -0
 }
 
 static bool errata_probe_pmu(unsigned int stage,
arch/riscv/include/asm/cacheflush.h +1 -1
index 8091b8bf4883..9d056c9b625a 100644
@@ -54,9 +54,9 @@ extern unsigned int riscv_cboz_block_size;
 void riscv_init_cbo_blocksizes(void);
 
 #ifdef CONFIG_RISCV_DMA_NONCOHERENT
-void riscv_noncoherent_supported(void);
+void riscv_noncoherent_supported(bool cmo);

I think it would "read better" if you renamed this variable to "have_cmo".

Quoted 19 lines
arch/riscv/include/asm/cacheflush.h +1 -1
 #else
-static inline void riscv_noncoherent_supported(void) {}
+static inline void riscv_noncoherent_supported(bool cmo) {}
 #endif
 
 /*
arch/riscv/kernel/setup.c +1 -0
index 36b026057503..565f3e20169b 100644
@@ -264,6 +264,7 @@ static void __init parse_dtb(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	bool cmo;
 	parse_dtb();
 	setup_initial_init_mm(_stext, _etext, _edata, _end);
 

arch/riscv/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 305

Quoted 8 lines
arch/riscv/kernel/setup.c +4 -1
@@ -298,7 +299,10 @@ void __init setup_arch(char **cmdline_p)
 	apply_boot_alternatives();
 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
 	    riscv_isa_extension_available(NULL, ZICBOM))
-		riscv_noncoherent_supported();
+		cmo = true;
+	else
+		cmo = false;
+	riscv_noncoherent_supported(cmo);

As a nit, could you put a newline before the call to riscv_noncoherent_supported()?

Quoted 12 lines
arch/riscv/kernel/setup.c +0 -0
 }
 
 static int __init topology_init(void)
arch/riscv/mm/dma-noncoherent.c +0 -1
index d51a75864e53..0e172e2b4751 100644
@@ -72,9 +72,11 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	dev->dma_coherent = coherent;
 }
 
-void riscv_noncoherent_supported(void)

arch/riscv/mm/dma-noncoherent.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 81

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c +6 -3
+void riscv_noncoherent_supported(bool cmo)
 {
-	WARN(!riscv_cbom_block_size,
-	     "Non-coherent DMA support enabled without a block size\n");
-	noncoherent_supported = true;
+	if (cmo) {
+		WARN(!riscv_cbom_block_size,
+		     "Non-coherent DMA support enabled without a block size\n");
+		noncoherent_supported = true;
+	}

The other places that we do a WARN() because of screwed up devicetrees for CMO things, we do a WARN_TAINT(CPU_OUT_OF_SPEC). Should we do the same here too?

Quoted 2 lines

Cheers,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#10 lore raw

Re: [PATCH 4/6] riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

Quoted 58 lines · Jisheng Zhang
Quoted 1 line · Conor Dooley

Hey Jisheng,

Hi Conor,

Quoted 45 lines · Jisheng Zhang
Quoted 43 lines

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +10 -7
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
Quoted 7 lines

arch/riscv/errata/thead/errata.c static bool errata_probe_pbmt(unsigned int stage, · line 56

Quoted 2 lines
arch/riscv/errata/thead/errata.c +1 -1
-	return true;
+	return cmo;

I don't really understand the changes that you are making to this function, so that is tries really hard to call riscv_noncoherent_supported(). Why do we need to always call the function in the erratum's probe function, if the erratum is not detected, given

Quoted 3 lines

In one unified kernel Image, to support both coherent and noncoherent platforms(currently, either T-HEAD CMO or ZICBOM), we need to let the kmalloc meet both cases, specifically, ARCH_DMA_MINALIGN aligned.

seems adding three words can make it better:

kmalloc meet both cases at the beginning, specifically ...

Quoted 90 lines

Once we know the underlying HW is coherent, I.E neither T-HEAD CMO nor ZICBOM, we need to notice kmalloc we are safe to reduce the alignment to 1. The notice action is done in patch 5:

(quoted diff) +2 -0
+       } else {
+               dma_cache_alignment = 1;
Quoted 82 lines

that riscv_noncoherent_supported() is called immediately after apply_boot_alternatives() in setup_arch()?

arch/riscv/include/asm/cacheflush.h extern unsigned int riscv_cboz_block_size; · line 57

Quoted 13 lines
arch/riscv/errata/thead/errata.c +0 -0
 }
 
 static bool errata_probe_pmu(unsigned int stage,
arch/riscv/include/asm/cacheflush.h +1 -1
index 8091b8bf4883..9d056c9b625a 100644
@@ -54,9 +54,9 @@ extern unsigned int riscv_cboz_block_size;
 void riscv_init_cbo_blocksizes(void);
 
 #ifdef CONFIG_RISCV_DMA_NONCOHERENT
-void riscv_noncoherent_supported(void);
+void riscv_noncoherent_supported(bool cmo);

I think it would "read better" if you renamed this variable to "have_cmo".

Quoted 19 lines
arch/riscv/include/asm/cacheflush.h +1 -1
 #else
-static inline void riscv_noncoherent_supported(void) {}
+static inline void riscv_noncoherent_supported(bool cmo) {}
 #endif
 
 /*
arch/riscv/kernel/setup.c +1 -0
index 36b026057503..565f3e20169b 100644
@@ -264,6 +264,7 @@ static void __init parse_dtb(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	bool cmo;
 	parse_dtb();
 	setup_initial_init_mm(_stext, _etext, _edata, _end);
 

arch/riscv/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 305

Quoted 8 lines
arch/riscv/kernel/setup.c +4 -1
@@ -298,7 +299,10 @@ void __init setup_arch(char **cmdline_p)
 	apply_boot_alternatives();
 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
 	    riscv_isa_extension_available(NULL, ZICBOM))
-		riscv_noncoherent_supported();
+		cmo = true;
+	else
+		cmo = false;
+	riscv_noncoherent_supported(cmo);

As a nit, could you put a newline before the call to riscv_noncoherent_supported()?

Quoted 12 lines
arch/riscv/kernel/setup.c +0 -0
 }
 
 static int __init topology_init(void)
arch/riscv/mm/dma-noncoherent.c +0 -1
index d51a75864e53..0e172e2b4751 100644
@@ -72,9 +72,11 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	dev->dma_coherent = coherent;
 }
 
-void riscv_noncoherent_supported(void)

arch/riscv/mm/dma-noncoherent.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 81

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c +6 -3
+void riscv_noncoherent_supported(bool cmo)
 {
-	WARN(!riscv_cbom_block_size,
-	     "Non-coherent DMA support enabled without a block size\n");
-	noncoherent_supported = true;
+	if (cmo) {
+		WARN(!riscv_cbom_block_size,
+		     "Non-coherent DMA support enabled without a block size\n");
+		noncoherent_supported = true;
+	}

The other places that we do a WARN() because of screwed up devicetrees for CMO things, we do a WARN_TAINT(CPU_OUT_OF_SPEC). Should we do the same here too?

Cheers,
Conor.

#11 lore raw

Re: [PATCH 4/6] riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

Quoted 54 lines · Jisheng Zhang
Quoted 53 lines · Jisheng Zhang
Quoted 51 lines · Conor Dooley
Quoted 35 lines · Jisheng Zhang

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +7 -5
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+

arch/riscv/errata/thead/errata.c static bool errata_probe_pbmt(unsigned int stage, · line 56

Quoted 10 lines
arch/riscv/errata/thead/errata.c +4 -3
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
-	return true;
+	return cmo;

I don't really understand the changes that you are making to this function, so that is tries really hard to call riscv_noncoherent_supported(). Why do we need to always call the function in the erratum's probe function, if the erratum is not detected, given

Quoted 18 lines
Quoted 3 lines

In one unified kernel Image, to support both coherent and noncoherent platforms(currently, either T-HEAD CMO or ZICBOM), we need to let the kmalloc meet both cases, specifically, ARCH_DMA_MINALIGN aligned.

seems adding three words can make it better:

kmalloc meet both cases at the beginning, specifically ...

Quoted 10 lines

Once we know the underlying HW is coherent, I.E neither T-HEAD CMO nor ZICBOM, we need to notice kmalloc we are safe to reduce the alignment to 1. The notice action is done in patch 5:

(quoted diff) +2 -0
+       } else {
+               dma_cache_alignment = 1;
Quoted 4 lines

that riscv_noncoherent_supported() is called immediately after apply_boot_alternatives() in setup_arch()?

This bit here is the key part of my confusion. You try really hard in the errata stuff to call riscv_noncoherent_supported(), which I do understand is because of the other branch that you add to the function later in the series.

What I do not understand is why we are not able to rely on the call to it in setup_arch() to trigger it when we do not have T-HEAD CMOs or Zicbom.
You've explained why you want to make sure it always gets called during boot, but my question is about why it looks like it is being called more than once.

Actually, now that I think of it, what happens on a T-HEAD system where there is no T-HEAD CMOs, but there is Zicbom. In theory, this could exist.
Bear with me here a moment in case I am completely wrong, snippet is from setup_arch()

	apply_boot_alternatives();

On my example system, this will trigger, eventually sending us into errata_probe_cmo(), where we will call riscv_noncoherent_supported() with false, setting dma_cache_alignment to 1.

	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
	    riscv_isa_extension_available(NULL, ZICBOM))
		cmo = true;

On this system, this will be true.

	else
		cmo = false;
	riscv_noncoherent_supported(cmo);

now riscv_noncoherent_supported() is called with true, and we have dma_cache_alignment = 1 still. Is that not problematic? Or the inverse, where the T-HEAD system has its custom CMOs and there is no Zicbom, it gets called twice with different args too.

There's clearly something fundamental that I am missing here, this seems like it should be immediately obvious why this either cannot happen or is not a problem, but I can't see it.

Sorry,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#12 lore raw

Re: [PATCH 4/6] riscv: mm: pass noncoherent or not to riscv_noncoherent_supported()

Quoted 102 lines · Conor Dooley
Quoted 54 lines · Jisheng Zhang
Quoted 53 lines · Jisheng Zhang
Quoted 51 lines · Conor Dooley
Quoted 35 lines · Jisheng Zhang

We will soon take different actions by checking the HW is noncoherent or not, I.E ZICBOM/ERRATA_THEAD_CMO or not.

---
 arch/riscv/errata/thead/errata.c    | 19 +++++++++++--------
 arch/riscv/include/asm/cacheflush.h |  4 ++--
 arch/riscv/kernel/setup.c           |  6 +++++-
 arch/riscv/mm/dma-noncoherent.c     | 10 ++++++----
 4 files changed, 24 insertions(+), 15 deletions(-)
arch/riscv/errata/thead/errata.c +7 -5
index be84b14f0118..c192b80a5166 100644
@@ -36,21 +36,24 @@ static bool errata_probe_pbmt(unsigned int stage,
 static bool errata_probe_cmo(unsigned int stage,
 			     unsigned long arch_id, unsigned long impid)
 {
-	if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
-		return false;
-
-	if (arch_id != 0 || impid != 0)
-		return false;
+	bool cmo;
 
 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
 		return false;
 
+	if (IS_ENABLED(CONFIG_ERRATA_THEAD_CMO) &&
+	    (arch_id == 0 && impid == 0))
+		cmo = true;
+	else
+		cmo = false;
+

arch/riscv/errata/thead/errata.c static bool errata_probe_pbmt(unsigned int stage, · line 56

Quoted 10 lines
arch/riscv/errata/thead/errata.c +4 -3
 	if (stage == RISCV_ALTERNATIVES_BOOT) {
-		riscv_cbom_block_size = L1_CACHE_BYTES;
-		riscv_noncoherent_supported();
+		if (cmo)
+			riscv_cbom_block_size = L1_CACHE_BYTES;
+		riscv_noncoherent_supported(cmo);
 	}
 
-	return true;
+	return cmo;

I don't really understand the changes that you are making to this function, so that is tries really hard to call riscv_noncoherent_supported(). Why do we need to always call the function in the erratum's probe function, if the erratum is not detected, given

Quoted 18 lines
Quoted 3 lines

In one unified kernel Image, to support both coherent and noncoherent platforms(currently, either T-HEAD CMO or ZICBOM), we need to let the kmalloc meet both cases, specifically, ARCH_DMA_MINALIGN aligned.

seems adding three words can make it better:

kmalloc meet both cases at the beginning, specifically ...

Quoted 10 lines

Once we know the underlying HW is coherent, I.E neither T-HEAD CMO nor ZICBOM, we need to notice kmalloc we are safe to reduce the alignment to 1. The notice action is done in patch 5:

(quoted diff) +2 -0
+       } else {
+               dma_cache_alignment = 1;
Quoted 4 lines

that riscv_noncoherent_supported() is called immediately after apply_boot_alternatives() in setup_arch()?

This bit here is the key part of my confusion. You try really hard in the errata stuff to call riscv_noncoherent_supported(), which I do understand is because of the other branch that you add to the function later in the series.

What I do not understand is why we are not able to rely on the call to it in setup_arch() to trigger it when we do not have T-HEAD CMOs or Zicbom.
You've explained why you want to make sure it always gets called during boot, but my question is about why it looks like it is being called more than once.

Actually, now that I think of it, what happens on a T-HEAD system where there is no T-HEAD CMOs, but there is Zicbom. In theory, this could exist.
Bear with me here a moment in case I am completely wrong, snippet is from setup_arch()

	apply_boot_alternatives();

On my example system, this will trigger, eventually sending us into errata_probe_cmo(), where we will call riscv_noncoherent_supported() with false, setting dma_cache_alignment to 1.

	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
	    riscv_isa_extension_available(NULL, ZICBOM))
		cmo = true;

On this system, this will be true.

Quoted 9 lines
	else
		cmo = false;
	riscv_noncoherent_supported(cmo);

now riscv_noncoherent_supported() is called with true, and we have dma_cache_alignment = 1 still. Is that not problematic? Or the inverse, where the T-HEAD system has its custom CMOs and there is no Zicbom, it gets called twice with different args too.

Thank you Conor. You pointed out a bug in my series. It looks like we need to defer the dma_cache_alignment modification a bit until T-HEAD CMO probing is done, but we also need to think carefully about T-HEAD related CONFIG option is disabled. I will take care this case in v2 once Catalin's series is merged.