Skip to contentSkip to conversation

sys_riscv_flush_icache

PATCH In review Christoph Hellwig 3 participants 21 messages Aug 22, 2019 – Sep 13, 2019 RISC-V

lore
Share & tools
lore
#1 lore raw

PATCH 1/8 riscv: fix the flags argument type for riscv_riscv_flush_icache

While uinptr_t is identical to unsigned long for all Linux platforms, defining a flags argument as an uinptr_t doesn't make any sense, so change it to an unsigned long instead.

---
 arch/riscv/include/asm/vdso.h | 2 +-
 arch/riscv/kernel/sys_riscv.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/vdso.h +1 -1
index 7a7fce63c474..a87a9d106220 100644
@@ -27,6 +27,6 @@ struct vdso_data {
 	(void __user *)((unsigned long)(base) + __vdso_##name);			\
 })
 
-asmlinkage long sys_riscv_flush_icache(uintptr_t, uintptr_t, uintptr_t);
+asmlinkage long sys_riscv_flush_icache(uintptr_t, uintptr_t, unsigned long);
 
 #endif /* _ASM_RISCV_VDSO_H */
arch/riscv/kernel/sys_riscv.c +1 -1
index f3619f59d85c..61f100acbd4c 100644
@@ -55,7 +55,7 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
  * in there for forwards compatibility.
  */
 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
-	uintptr_t, flags)
+		unsigned long, flags)
 {
 	/* Check the reserved flags. */
 	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
Signature · 7 lines
2.20.1


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

PATCH 2/8 riscv: remove SYS_RISCV_FLUSH_ICACHE_LOCAL #define

In reply to Christoph Hellwig

It is much better to just check for the supporte flags directly rather than hiding them behind a flag.

---
 arch/riscv/include/asm/cacheflush.h | 1 -
 arch/riscv/kernel/sys_riscv.c       | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
arch/riscv/include/asm/cacheflush.h +0 -1
index 555b20b11dc3..c8f159740c38 100644
@@ -103,6 +103,5 @@ void flush_icache_mm(struct mm_struct *mm, bool local);
  * Bits in sys_riscv_flush_icache()'s flags argument.
  */
 #define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL
-#define SYS_RISCV_FLUSH_ICACHE_ALL   (SYS_RISCV_FLUSH_ICACHE_LOCAL)
 
 #endif /* _ASM_RISCV_CACHEFLUSH_H */
arch/riscv/kernel/sys_riscv.c +1 -1
index 61f100acbd4c..5bad71078b61 100644
@@ -58,7 +58,7 @@ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
 		unsigned long, flags)
 {
 	/* Check the reserved flags. */
-	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
+	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_LOCAL))
 		return -EINVAL;
 
 	flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
Signature · 7 lines
2.20.1


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

PATCH 3/8 riscv: move sys_riscv_flush_icache to cacheflush.c

In reply to Christoph Hellwig

No real need to split this over to files. This allows marking flush_icache_mm static and dropping a superflous argument to it, as well as consolidating the documentation.

---
 arch/riscv/include/asm/cacheflush.h |  2 --
 arch/riscv/kernel/sys_riscv.c       | 27 ---------------
 arch/riscv/mm/cacheflush.c          | 53 +++++++++++++++++++++--------
 3 files changed, 38 insertions(+), 44 deletions(-)
arch/riscv/include/asm/cacheflush.h +0 -2
index c8f159740c38..b86ac3a4653a 100644
@@ -90,12 +90,10 @@ static inline void flush_dcache_page(struct page *page)
 #ifndef CONFIG_SMP
 
 #define flush_icache_all() local_flush_icache_all()
-#define flush_icache_mm(mm, local) flush_icache_all()
 
 #else /* CONFIG_SMP */
 
 void flush_icache_all(void);
-void flush_icache_mm(struct mm_struct *mm, bool local);
 
 #endif /* CONFIG_SMP */
 
arch/riscv/kernel/sys_riscv.c +0 -27
index 5bad71078b61..d13e03de3e1a 100644
@@ -7,7 +7,6 @@
 
 #include <linux/syscalls.h>
 #include <asm/unistd.h>
-#include <asm/cacheflush.h>
 
 static long riscv_sys_mmap(unsigned long addr, unsigned long len,
 			   unsigned long prot, unsigned long flags,
@@ -39,29 +38,3 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
 	return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
 }
 #endif /* !CONFIG_64BIT */
-
-/*
- * Allows the instruction cache to be flushed from userspace.  Despite RISC-V
- * having a direct 'fence.i' instruction available to userspace (which we
- * can't trap!), that's not actually viable when running on Linux because the
- * kernel might schedule a process on another hart.  There is no way for
- * userspace to handle this without invoking the kernel (as it doesn't know the
- * thread->hart mappings), so we've defined a RISC-V specific system call to
- * flush the instruction cache.
- *
- * sys_riscv_flush_icache() is defined to flush the instruction cache over an
- * address range, with the flush applying to either all threads or just the
- * caller.  We don't currently do anything with the address range, that's just
- * in there for forwards compatibility.
- */
-SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
-		unsigned long, flags)
-{
-	/* Check the reserved flags. */
-	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_LOCAL))
-		return -EINVAL;
-
-	flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
-
-	return 0;
-}
arch/riscv/mm/cacheflush.c +38 -15

88 lines not on this page · raw message

Signature · 7 lines
2.20.1


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

PATCH 4/8 riscv: remove the active_mm check in sys_riscv_flush_icache

In reply to Christoph Hellwig

current->active_mm is always the same as current->mm for user processes (see Documentation/vm/active_mm.rst for details), and given that we are directly in a syscall handler this is obviously the case here.

---
 arch/riscv/mm/cacheflush.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/mm/cacheflush.c +1 -1
index 4f78d6552476..eed715de4795 100644
@@ -38,7 +38,7 @@ static void flush_icache_mm(bool local)
 	 */
 	cpumask_andnot(&others, mm_cpumask(current->mm), cpumask_of(cpu));
 	local |= cpumask_empty(&others);
-	if (current->mm != current->active_mm || !local) {
+	if (!local) {
 		riscv_cpuid_to_hartid_mask(&others, &hmask);
 		sbi_remote_fence_i(hmask.bits);
 	} else {
Signature · 7 lines
2.20.1


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

PATCH 5/8 riscv: actually clear icache_stale_mask for all harts in mm_cpumask

In reply to Christoph Hellwig

The comment in flush_icache_mm claim that we mark all harts that we are sending the remote sfence.i to are marked as flushed, but we only actually do this for the current one. Fix the code to actually mark all.

---
 arch/riscv/mm/cacheflush.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
arch/riscv/mm/cacheflush.c +9 -7
index eed715de4795..dacf72f94d12 100644
@@ -20,21 +20,23 @@ void flush_icache_all(void)
 static void flush_icache_mm(bool local)
 {
 	unsigned int cpu;
-	cpumask_t others, hmask, *mask;
+	cpumask_t others, hmask;
 
 	preempt_disable();
 
-	/* Mark every hart's icache as needing a flush for this MM. */
-	mask = &current->mm->context.icache_stale_mask;
-	cpumask_setall(mask);
+	/*
+	 * Mark the I$ for all harts not concurrently executing as needing a
+	 * flush for this MM.
+	 */
+	cpumask_andnot(&current->mm->context.icache_stale_mask,
+		       cpu_possible_mask, mm_cpumask(current->mm));
+
 	/* Flush this hart's I$ now, and mark it as flushed. */
 	cpu = smp_processor_id();
-	cpumask_clear_cpu(cpu, mask);
 	local_flush_icache_all();
 
 	/*
-	 * Flush the I$ of other harts concurrently executing, and mark them as
-	 * flushed.
+	 * Flush the I$ of other harts concurrently executing.
 	 */
 	cpumask_andnot(&others, mm_cpumask(current->mm), cpumask_of(cpu));
 	local |= cpumask_empty(&others);
Signature · 7 lines
2.20.1


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

Re: [PATCH 5/8] riscv: actually clear icache_stale_mask for all harts in mm_cpumask

Quoted 29 lines · Christoph Hellwig

The comment in flush_icache_mm claim that we mark all harts that we are sending the remote sfence.i to are marked as flushed, but we only actually do this for the current one. Fix the code to actually mark all.

---
 arch/riscv/mm/cacheflush.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
arch/riscv/mm/cacheflush.c +3 -4
index eed715de4795..dacf72f94d12 100644
@@ -20,21 +20,23 @@ void flush_icache_all(void)
 static void flush_icache_mm(bool local)
 {
 	unsigned int cpu;
-	cpumask_t others, hmask, *mask;
+	cpumask_t others, hmask;
 
 	preempt_disable();
 
-	/* Mark every hart's icache as needing a flush for this MM. */
-	mask = &current->mm->context.icache_stale_mask;
-	cpumask_setall(mask);
+	/*
+	 * Mark the I$ for all harts not concurrently executing as

needing a

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 33

Quoted 5 lines
arch/riscv/mm/cacheflush.c +5 -0
+	 * flush for this MM.
+	 */
+	cpumask_andnot(&current->mm->context.icache_stale_mask,
+		       cpu_possible_mask, mm_cpumask(current->mm));
+

I guess you have added cpu_possible_mask keeping cpu hotplug in mind for future.

I think it should be cpu_present_mask instead of cpu_possible_mask as they can be different in case where some cpus are just waiting in the boot loader.

Quoted 12 lines
arch/riscv/mm/cacheflush.c +0 -2
 	/* Flush this hart's I$ now, and mark it as flushed. */
 	cpu = smp_processor_id();
-	cpumask_clear_cpu(cpu, mask);
 	local_flush_icache_all();
 
 	/*
-	 * Flush the I$ of other harts concurrently executing, and mark

them as

arch/riscv/mm/cacheflush.c +1 -1
-	 * flushed.
+	 * Flush the I$ of other harts concurrently executing.
 	 */
 	cpumask_andnot(&others, mm_cpumask(current->mm),
Quoted 2 lines

cpumask_of(cpu));
local |= cpumask_empty(&others);

Otherwise, looks good.

Signature · 6 lines
Regards,
Atish
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw

Re: [PATCH 5/8] riscv: actually clear icache_stale_mask for all harts in mm_cpumask

Quoted 6 lines · Atish Patra

I guess you have added cpu_possible_mask keeping cpu hotplug in mind for future.

I think it should be cpu_present_mask instead of cpu_possible_mask as they can be different in case where some cpus are just waiting in the boot loader.

I don't think it matters. The only place where we actually check icache_stale_mask is in flush_icache_deferred, which only does a cpumask_test_cpu for the currently running CPU. So I'd rather opt for setting a few more bits and prepare for cpu hotplug.

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

#8 lore raw

PATCH 6/8 riscv: use get_cpu and put_cpu in sys_riscv_flush_icache

In reply to Christoph Hellwig

Use get_cpu/put_cpu instead of opencoding them.

---
 arch/riscv/mm/cacheflush.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/riscv/mm/cacheflush.c +2 -5
index dacf72f94d12..9180b2e93058 100644
@@ -19,11 +19,9 @@ void flush_icache_all(void)
 
 static void flush_icache_mm(bool local)
 {
-	unsigned int cpu;
+	unsigned int cpu = get_cpu();
 	cpumask_t others, hmask;
 
-	preempt_disable();
-
 	/*
 	 * Mark the I$ for all harts not concurrently executing as needing a
 	 * flush for this MM.
@@ -32,7 +30,6 @@ static void flush_icache_mm(bool local)
 		       cpu_possible_mask, mm_cpumask(current->mm));
 
 	/* Flush this hart's I$ now, and mark it as flushed. */
-	cpu = smp_processor_id();
 	local_flush_icache_all();
 
 	/*
@@ -55,7 +52,7 @@ static void flush_icache_mm(bool local)
 		smp_mb();
 	}
 
-	preempt_enable();
+	put_cpu();
 }
 #else
 #define flush_icache_mm(local)	flush_icache_all()
Signature · 7 lines
2.20.1


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

Re: [PATCH 6/8] riscv: use get_cpu and put_cpu in sys_riscv_flush_icache

Quoted 12 lines · Christoph Hellwig

Use get_cpu/put_cpu instead of opencoding them.

---
 arch/riscv/mm/cacheflush.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/riscv/mm/cacheflush.c +0 -0
index dacf72f94d12..9180b2e93058 100644

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 22

Quoted 5 lines
arch/riscv/mm/cacheflush.c +1 -1
@@ -19,11 +19,9 @@ void flush_icache_all(void)
 
 static void flush_icache_mm(bool local)
 {
-	unsigned int cpu;
+	unsigned int cpu = get_cpu();

I thought it is recommended to put variables with assignment at the end. But I can't find anything about it in coding guidelines. So it might be all in my head ;-)

Quoted 17 lines
arch/riscv/mm/cacheflush.c +0 -2
 	cpumask_t others, hmask;
 
-	preempt_disable();
-
 	/*
 	 * Mark the I$ for all harts not concurrently executing as

needing a

arch/riscv/mm/cacheflush.c +0 -1
 	 * flush for this MM.
@@ -32,7 +30,6 @@ static void flush_icache_mm(bool local)
 		       cpu_possible_mask, mm_cpumask(current->mm));
 
 	/* Flush this hart's I$ now, and mark it as flushed. */
-	cpu = smp_processor_id();
 	local_flush_icache_all();
 
 	/*

arch/riscv/mm/cacheflush.c static void flush_icache_mm(bool local) · line 58

Quoted 8 lines
arch/riscv/mm/cacheflush.c +1 -1
@@ -55,7 +52,7 @@ static void flush_icache_mm(bool local)
 		smp_mb();
 	}
 
-	preempt_enable();
+	put_cpu();
 }
 #else
 #define flush_icache_mm(local)	flush_icache_all()

Otherwise, looks good.

Signature · 6 lines
Regards,
Atish
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#10 lore raw

Re: [PATCH 6/8] riscv: use get_cpu and put_cpu in sys_riscv_flush_icache

Quoted 14 lines · Atish Patra
Quoted 13 lines · Christoph Hellwig

Use get_cpu/put_cpu instead of opencoding them.

---
 arch/riscv/mm/cacheflush.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/riscv/mm/cacheflush.c +0 -0
index dacf72f94d12..9180b2e93058 100644
@@ -19,11 +19,9 @@ void flush_icache_all(void)
 
Quoted 8 lines

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 22

Quoted 4 lines
arch/riscv/mm/cacheflush.c +1 -1
 static void flush_icache_mm(bool local)
 {
-	unsigned int cpu;
+	unsigned int cpu = get_cpu();

I thought it is recommended to put variables with assignment at the end. But I can't find anything about it in coding guidelines. So it might be all in my head ;-)

I've never heard of that before. In fact I usually keep them at the beginning.

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

#11 lore raw

Re: [PATCH 6/8] riscv: use get_cpu and put_cpu in sys_riscv_flush_icache

Quoted 25 lines · On Mon, 2019-08-26 at 13:38 +0200, [email protected] wrote:
Quoted 15 lines · Atish Patra
Quoted 14 lines · Christoph Hellwig

Use get_cpu/put_cpu instead of opencoding them.

---
 arch/riscv/mm/cacheflush.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/riscv/mm/cacheflush.c +0 -0
index dacf72f94d12..9180b2e93058 100644
@@ -19,11 +19,9 @@ void flush_icache_all(void)
 
Quoted 8 lines

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 22

Quoted 4 lines
arch/riscv/mm/cacheflush.c +1 -1
 static void flush_icache_mm(bool local)
 {
-	unsigned int cpu;
+	unsigned int cpu = get_cpu();

I thought it is recommended to put variables with assignment at the end. But I can't find anything about it in coding guidelines. So it might be all in my head ;-)

Quoted 2 lines

I've never heard of that before. In fact I usually keep them at the beginning.

Ok. I will keep that in mind :)

Signature · 6 lines
Regards,
Atish
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#12 lore raw

PATCH 7/8 riscv: improve the local flushing logic in sys_riscv_flush_icache

In reply to Christoph Hellwig

If we have to offload any remote sfence the SBI we might as well let it handle the local one as well. This significantly simplifies the cpumask operations and streamlines the code.

---
 arch/riscv/mm/cacheflush.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
arch/riscv/mm/cacheflush.c +14 -19
index 9180b2e93058..8f1134715fec 100644
@@ -20,7 +20,6 @@ void flush_icache_all(void)
 static void flush_icache_mm(bool local)
 {
 	unsigned int cpu = get_cpu();
-	cpumask_t others, hmask;
 
 	/*
 	 * Mark the I$ for all harts not concurrently executing as needing a
@@ -29,27 +28,23 @@ static void flush_icache_mm(bool local)
 	cpumask_andnot(&current->mm->context.icache_stale_mask,
 		       cpu_possible_mask, mm_cpumask(current->mm));
 
-	/* Flush this hart's I$ now, and mark it as flushed. */
-	local_flush_icache_all();
-
 	/*
-	 * Flush the I$ of other harts concurrently executing.
+	 * It's assumed that at least one strongly ordered operation is
+	 * performed on this hart between setting a hart's cpumask bit and
+	 * scheduling this MM context on that hart.  Sending an SBI remote
+	 * message will do this, but in the case where no messages are sent we
+	 * still need to order this hart's writes with flush_icache_deferred().
 	 */
-	cpumask_andnot(&others, mm_cpumask(current->mm), cpumask_of(cpu));
-	local |= cpumask_empty(&others);
-	if (!local) {
-		riscv_cpuid_to_hartid_mask(&others, &hmask);
-		sbi_remote_fence_i(hmask.bits);
-	} else {
-		/*
-		 * It's assumed that at least one strongly ordered operation is
-		 * performed on this hart between setting a hart's cpumask bit
-		 * and scheduling this MM context on that hart.  Sending an SBI
-		 * remote message will do this, but in the case where no
-		 * messages are sent we still need to order this hart's writes
-		 * with flush_icache_deferred().
-		 */
+	cpu = get_cpu();
+	if (local ||
+	    cpumask_any_but(mm_cpumask(current->mm), cpu) >= nr_cpu_ids) {
+		local_flush_icache_all();
 		smp_mb();
+	} else {
+		cpumask_t hmask;
+
+		riscv_cpuid_to_hartid_mask(mm_cpumask(current->mm), &hmask);
+		sbi_remote_fence_i(cpumask_bits(&hmask));
 	}
 
 	put_cpu();
Signature · 7 lines
2.20.1


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

Re: [PATCH 7/8] riscv: improve the local flushing logic in sys_riscv_flush_icache

Quoted 16 lines · Christoph Hellwig

If we have to offload any remote sfence the SBI we might as well let it
handle the local one as well. This significantly simplifies the cpumask
operations and streamlines the code.

---
 arch/riscv/mm/cacheflush.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
arch/riscv/mm/cacheflush.c +0 -0
index 9180b2e93058..8f1134715fec 100644

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 22

Quoted 3 lines
arch/riscv/mm/cacheflush.c +0 -0
@@ -20,7 +20,6 @@ void flush_icache_all(void)
 static void flush_icache_mm(bool local)
 {
 	unsigned int cpu = get_cpu();

get_cpu should be removed from here as it is called again in below.

Quoted 55 lines
arch/riscv/mm/cacheflush.c +0 -1
-	cpumask_t others, hmask;
 
 	/*
 	 * Mark the I$ for all harts not concurrently executing as

needing a

arch/riscv/mm/cacheflush.c +2 -4
@@ -29,27 +28,23 @@ static void flush_icache_mm(bool local)
 	cpumask_andnot(&current->mm->context.icache_stale_mask,
 		       cpu_possible_mask, mm_cpumask(current->mm));
 
-	/* Flush this hart's I$ now, and mark it as flushed. */
-	local_flush_icache_all();
-
 	/*
-	 * Flush the I$ of other harts concurrently executing.
+	 * It's assumed that at least one strongly ordered operation is
+	 * performed on this hart between setting a hart's cpumask bit

and

arch/riscv/mm/cacheflush.c +1 -0
+	 * scheduling this MM context on that hart.  Sending an SBI

remote

arch/riscv/mm/cacheflush.c +1 -0
+	 * message will do this, but in the case where no messages are

sent we

arch/riscv/mm/cacheflush.c +1 -0
+	 * still need to order this hart's writes with

flush_icache_deferred().

arch/riscv/mm/cacheflush.c +0 -1
 	 */
-	cpumask_andnot(&others, mm_cpumask(current->mm),

cpumask_of(cpu));

arch/riscv/mm/cacheflush.c +0 -7
-	local |= cpumask_empty(&others);
-	if (!local) {
-		riscv_cpuid_to_hartid_mask(&others, &hmask);
-		sbi_remote_fence_i(hmask.bits);
-	} else {
-		/*
-		 * It's assumed that at least one strongly ordered

operation is

arch/riscv/mm/cacheflush.c +0 -1
-		 * performed on this hart between setting a hart's

cpumask bit

arch/riscv/mm/cacheflush.c +0 -1
-		 * and scheduling this MM context on that

hart. Sending an SBI

arch/riscv/mm/cacheflush.c +0 -1
-		 * remote message will do this, but in the case where

no

arch/riscv/mm/cacheflush.c +0 -1
-		 * messages are sent we still need to order this hart's

writes

arch/riscv/mm/cacheflush.c +3 -2
-		 * with flush_icache_deferred().
-		 */
+	cpu = get_cpu();
+	if (local ||
+	    cpumask_any_but(mm_cpumask(current->mm), cpu) >=

nr_cpu_ids) {

arch/riscv/mm/cacheflush.c +5 -0
+		local_flush_icache_all();
 		smp_mb();
+	} else {
+		cpumask_t hmask;
+
+		riscv_cpuid_to_hartid_mask(mm_cpumask(current->mm),

&hmask);

arch/riscv/mm/cacheflush.c static void flush_icache_mm(bool local) · line 50

Quoted 4 lines
arch/riscv/mm/cacheflush.c +1 -0
+		sbi_remote_fence_i(cpumask_bits(&hmask));
 	}
 
 	put_cpu();

Otherwise, looks good to me.

Signature · 6 lines
Regards,
Atish
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#14 lore raw

Re: [PATCH 7/8] riscv: improve the local flushing logic in sys_riscv_flush_icache

Quoted 9 lines · Atish Patra

arch/riscv/mm/cacheflush.c void flush_icache_all(void) · line 22

Quoted 7 lines

index 9180b2e93058..8f1134715fec 100644

arch/riscv/mm/cacheflush.c +0 -0
@@ -20,7 +20,6 @@ void flush_icache_all(void)
 static void flush_icache_mm(bool local)
 {
 	unsigned int cpu = get_cpu();

get_cpu should be removed from here as it is called again in below.

Indeed.

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

#15 lore raw

PATCH 8/8 riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

In reply to Christoph Hellwig

The SYS_RISCV_FLUSH_ICACHE_LOCAL is built on the flawed assumption that there is such a thing as a local cpu outside of in-kernel preemption disabled sections. Just ignore the flag as it can't be used in a safe way.

---
 arch/riscv/include/asm/cacheflush.h |  2 +-
 arch/riscv/mm/cacheflush.c          | 13 ++++++++-----
 2 files changed, 9 insertions(+), 6 deletions(-)
arch/riscv/include/asm/cacheflush.h +1 -1
index b86ac3a4653a..3c18d956c970 100644
@@ -100,6 +100,6 @@ void flush_icache_all(void);
 /*
  * Bits in sys_riscv_flush_icache()'s flags argument.
  */
-#define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL
+#define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL /* ignored */
 
 #endif /* _ASM_RISCV_CACHEFLUSH_H */
arch/riscv/mm/cacheflush.c +8 -5
index 8f1134715fec..4b6ecc3431e2 100644
@@ -17,7 +17,7 @@ void flush_icache_all(void)
 	sbi_remote_fence_i(NULL);
 }
 
-static void flush_icache_mm(bool local)
+static void flush_icache_mm(void)
 {
 	unsigned int cpu = get_cpu();
 
@@ -36,8 +36,7 @@ static void flush_icache_mm(bool local)
 	 * still need to order this hart's writes with flush_icache_deferred().
 	 */
 	cpu = get_cpu();
-	if (local ||
-	    cpumask_any_but(mm_cpumask(current->mm), cpu) >= nr_cpu_ids) {
+	if (cpumask_any_but(mm_cpumask(current->mm), cpu) >= nr_cpu_ids) {
 		local_flush_icache_all();
 		smp_mb();
 	} else {
@@ -50,7 +49,7 @@ static void flush_icache_mm(bool local)
 	put_cpu();
 }
 #else
-#define flush_icache_mm(local)	flush_icache_all()
+#define flush_icache_mm()	flush_icache_all()
 #endif /* CONFIG_SMP */
 
 /*
@@ -72,6 +71,10 @@ static void flush_icache_mm(bool local)
  * remove flush for harts that are not currently executing a MM context and
  * instead schedule a deferred local instruction cache flush to be performed
  * before execution resumes on each hart.
+ *
+ * Note that we ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag, as there is
+ * absolutely not way to ensure the local CPU is still the same by the time we
+ * return from the syscall.
  */
 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
 		unsigned long, flags)
@@ -79,7 +82,7 @@ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
 	/* Check the reserved flags. */
 	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_LOCAL))
 		return -EINVAL;
-	flush_icache_mm(flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
+	flush_icache_mm();
 	return 0;
 }
 
Signature · 7 lines
2.20.1


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

Re: [PATCH 8/8] riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

Quoted 4 lines · Christoph Hellwig

The SYS_RISCV_FLUSH_ICACHE_LOCAL is built on the flawed assumption that there is such a thing as a local cpu outside of in-kernel preemption disabled sections. Just ignore the flag as it can't be used in a safe way.

This is meant to perform a context-local flush, not a cpu-local flush. The whole point here is that userspace doesn't know anything about CPUs, just contexts -- that's why we have this deferred flush mechanism. I think the logic is complicated but sound, and removing this will almost certainly lead to huge performance degradation.

Maybe I'm missing something, what is the specific issue?

Quoted 62 lines
---
 arch/riscv/include/asm/cacheflush.h |  2 +-
 arch/riscv/mm/cacheflush.c          | 13 ++++++++-----
 2 files changed, 9 insertions(+), 6 deletions(-)
arch/riscv/include/asm/cacheflush.h +1 -1
index b86ac3a4653a..3c18d956c970 100644
@@ -100,6 +100,6 @@ void flush_icache_all(void);
 /*
  * Bits in sys_riscv_flush_icache()'s flags argument.
  */
-#define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL
+#define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL /* ignored */
 
 #endif /* _ASM_RISCV_CACHEFLUSH_H */
arch/riscv/mm/cacheflush.c +7 -4
index 8f1134715fec..4b6ecc3431e2 100644
@@ -17,7 +17,7 @@ void flush_icache_all(void)
 	sbi_remote_fence_i(NULL);
 }
 
-static void flush_icache_mm(bool local)
+static void flush_icache_mm(void)
 {
 	unsigned int cpu = get_cpu();
 
@@ -36,8 +36,7 @@ static void flush_icache_mm(bool local)
 	 * still need to order this hart's writes with flush_icache_deferred().
 	 */
 	cpu = get_cpu();
-	if (local ||
-	    cpumask_any_but(mm_cpumask(current->mm), cpu) >= nr_cpu_ids) {
+	if (cpumask_any_but(mm_cpumask(current->mm), cpu) >= nr_cpu_ids) {
 		local_flush_icache_all();
 		smp_mb();
 	} else {
@@ -50,7 +49,7 @@ static void flush_icache_mm(bool local)
 	put_cpu();
 }
 #else
-#define flush_icache_mm(local)	flush_icache_all()
+#define flush_icache_mm()	flush_icache_all()
 #endif /* CONFIG_SMP */
 
 /*
@@ -72,6 +71,10 @@ static void flush_icache_mm(bool local)
  * remove flush for harts that are not currently executing a MM context and
  * instead schedule a deferred local instruction cache flush to be performed
  * before execution resumes on each hart.
+ *
+ * Note that we ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag, as there is
+ * absolutely not way to ensure the local CPU is still the same by the time we
+ * return from the syscall.
  */
 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
 		unsigned long, flags)

arch/riscv/mm/cacheflush.c SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end, · line 87

Quoted 7 lines
arch/riscv/mm/cacheflush.c +1 -1
@@ -79,7 +82,7 @@ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
 	/* Check the reserved flags. */
 	if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_LOCAL))
 		return -EINVAL;
-	flush_icache_mm(flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
+	flush_icache_mm();
 	return 0;
 }

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

#17 lore raw

Re: [PATCH 8/8] riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

Quoted 5 lines · Palmer Dabbelt

This is meant to perform a context-local flush, not a cpu-local flush. The whole point here is that userspace doesn't know anything about CPUs, just contexts -- that's why we have this deferred flush mechanism. I think the logic is complicated but sound, and removing this will almost certainly lead to huge performance degradation.

All calls to flush_icache_mm are local to the context. Take a look at what the current code does:

- set all bits in context.icache_stale_mask
- clear the current cpu from context.icache_stale_mask
- flush the cpu local icache
- create a local others mask containing every cpu running the context except for the current one
- now if others is empty OR the local flag is set don't do anything but a memory barrier, else flush the other cpus

Quoted 2 lines

Maybe I'm missing something, what is the specific issue?

The issue is that the current implementation of SYS_RISCV_FLUSH_ICACHE_LOCAL only flushes the icache of the currently running core, which is an interface that can't be used correctly.

riscv_flush_icache without that flag on the other handle already just flushes the caches for the cpus that run the current context, and then causes a deferred flush if the context gets run on another cpu eventually.

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

#18 lore raw

Re: [PATCH 8/8] riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

Quoted 19 lines · Christoph Hellwig
Quoted 5 lines · Palmer Dabbelt

This is meant to perform a context-local flush, not a cpu-local flush. The whole point here is that userspace doesn't know anything about CPUs, just contexts -- that's why we have this deferred flush mechanism. I think the logic is complicated but sound, and removing this will almost certainly lead to huge performance degradation.

All calls to flush_icache_mm are local to the context. Take a look at what the current code does:

- set all bits in context.icache_stale_mask
- clear the current cpu from context.icache_stale_mask
- flush the cpu local icache
- create a local others mask containing every cpu running the context except for the current one
- now if others is empty OR the local flag is set don't do anything but a memory barrier, else flush the other cpus

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Maybe I'm missing something, what is the specific issue?

The issue is that the current implementation of SYS_RISCV_FLUSH_ICACHE_LOCAL only flushes the icache of the currently running core, which is an interface that can't be used correctly.

This is used by userspace as a thread-local icache barrier: there's an immediate fence on the current hart, and one will be executed before that thread makes it to userspace on another hart. As far as I can tell this is implemented correctly but not optimally: there's always a fence, but we emit an unnecessary fence when a different thread in the same context is scheduled on a different hart.

I suppose maybe we should attach the local fence mask to a task_struct instead of an mm_struct, which would trade off an extra load in the scheduler (to check both places) or more fences in the global case (on every thread getting scheduled) for fewer fences in the local case. I feel like it's not really worth worrying about this for now.

The construct seems reasonable to me.

Quoted 4 lines

riscv_flush_icache without that flag on the other handle already just flushes the caches for the cpus that run the current context, and then causes a deferred flush if the context gets run on another cpu eventually.

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

#19 lore raw

Re: [PATCH 8/8] riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

Quoted 7 lines · Palmer Dabbelt

This is used by userspace as a thread-local icache barrier: there's an immediate fence on the current hart, and one will be executed before that thread makes it to userspace on another hart. As far as I can tell this is implemented correctly but not optimally: there's always a fence, but we emit an unnecessary fence when a different thread in the same context is scheduled on a different hart.

Quoted 7 lines

I suppose maybe we should attach the local fence mask to a task_struct instead of an mm_struct, which would trade off an extra load in the scheduler (to check both places) or more fences in the global case (on every thread getting scheduled) for fewer fences in the local case. I feel like it's not really worth worrying about this for now.

The construct seems reasonable to me.

I haven't been able to poke holes into that idea yet, but I'll try a bit more once I find a little time.

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

#20 lore raw

Re: [PATCH 8/8] riscv: ignore the SYS_RISCV_FLUSH_ICACHE_LOCAL flag

Quoted 14 lines · Christoph Hellwig
Quoted 13 lines · Palmer Dabbelt

This is used by userspace as a thread-local icache barrier: there's an immediate fence on the current hart, and one will be executed before that thread makes it to userspace on another hart. As far as I can tell this is implemented correctly but not optimally: there's always a fence, but we emit an unnecessary fence when a different thread in the same context is scheduled on a different hart.

I suppose maybe we should attach the local fence mask to a task_struct instead of an mm_struct, which would trade off an extra load in the scheduler (to check both places) or more fences in the global case (on every thread getting scheduled) for fewer fences in the local case. I feel like it's not really worth worrying about this for now.

Quoted 4 lines
Quoted 1 line

The construct seems reasonable to me.

I haven't been able to poke holes into that idea yet, but I'll try a bit more once I find a little time.

OK, well, LMK if you find anything -- it's in the user ABI, which is a bad place to have something fundamentally broken :)

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