Skip to contentSkip to conversation

riscv: mm: Fix incorrect ASID argument when flushing TLB

PATCH Applied Dylan Jhong 5 participants 8 messages Mar 13, 2023 – Mar 23, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 riscv: mm: Fix incorrect ASID argument when flushing TLB

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---
Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
 	__asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -1
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
 	/* check if the tlbflush needs to be sent to other CPUs */
 	broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
 	if (static_branch_unlikely(&use_asid_allocator)) {
-		unsigned long asid = atomic_long_read(&mm->context.id);
+		unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
 		/*
 		 * TLB will be immediately flushed on harts concurrently
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v2 1/1
#1 lore raw
Quoted 46 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---
Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
 	__asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +0 -0
index ce7dfc81bb3f..ba4c27187c95 100644

arch/riscv/mm/tlbflush.c static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start, · line 33

Quoted 8 lines
arch/riscv/mm/tlbflush.c +1 -1
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
 	/* check if the tlbflush needs to be sent to other CPUs */
 	broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
 	if (static_branch_unlikely(&use_asid_allocator)) {
-		unsigned long asid = atomic_long_read(&mm->context.id);
+		unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
 		/*
 		 * TLB will be immediately flushed on harts concurrently

Thanks !

#2 lore raw
In reply to Dylan Jhong

Dylan Jhong <[email protected]> 於 2023年3月13日 週一 下午12:29寫道:

Quoted 8 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---

Hi Dylan,
Thanks for your patch, if I remember correctly, there was a patch from Alistair Francis did the similar fix. Perhaps we should track that patch to see why it doesn't be merged. Thanks.

http://lists.infradead.org/pipermail/linux-riscv/2022-March/013558.html

Quoted 48 lines

Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
        __asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -2
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
        /* check if the tlbflush needs to be sent to other CPUs */
        broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
        if (static_branch_unlikely(&use_asid_allocator)) {
-               unsigned long asid = atomic_long_read(&mm->context.id);
+               unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
                /*
                 * TLB will be immediately flushed on harts concurrently
--
Quoted 2 lines

2.34.1

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

#3 lore raw
Quoted 10 lines · Zong Li

Dylan Jhong <[email protected]> 於 2023年3月13日 週一 下午12:29寫道:

Quoted 8 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---

Quoted 7 lines

Hi Dylan,
Thanks for your patch, if I remember correctly, there was a patch from Alistair Francis did the similar fix. Perhaps we should track that patch to see why it doesn't be merged. Thanks.

http://lists.infradead.org/pipermail/linux-riscv/2022-March/013558.html

Hi Zong,
Thanks for the reminder, I didn't notice that Alistair had sent the same patch before.

Hi Palmer, Alistair
http://lists.infradead.org/pipermail/linux-riscv/2022-March/013597.html
This patch does not seem to be cherry-picked back to the released linux kernel, and I have not seen the v4 patch. May I ask how is the follow-up progress of this patch?

Best,
Dylan

Quoted 50 lines
Quoted 50 lines

Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
        __asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -2
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
        /* check if the tlbflush needs to be sent to other CPUs */
        broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
        if (static_branch_unlikely(&use_asid_allocator)) {
-               unsigned long asid = atomic_long_read(&mm->context.id);
+               unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
                /*
                 * TLB will be immediately flushed on harts concurrently
--

2.34.1

#4 lore raw
Quoted 21 lines · Dylan Jhong
Quoted 10 lines · Zong Li

Dylan Jhong <[email protected]> 於 2023年3月13日 週一 下午12:29寫道:

Quoted 8 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---

Quoted 7 lines

Hi Dylan,
Thanks for your patch, if I remember correctly, there was a patch from Alistair Francis did the similar fix. Perhaps we should track that patch to see why it doesn't be merged. Thanks.

http://lists.infradead.org/pipermail/linux-riscv/2022-March/013558.html

Hi Zong,
Thanks for the reminder, I didn't notice that Alistair had sent the same patch before.

Quoted 8 lines

Hi Palmer, Alistair
http://lists.infradead.org/pipermail/linux-riscv/2022-March/013597.html
This patch does not seem to be cherry-picked back to the released linux kernel, and I have not seen the v4 patch. May I ask how is the follow-up progress of this patch?

Best,
Dylan

Hi Palmer,

Ping. Any update on this?

Best,
Dylan

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines

Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
        __asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -1
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
        /* check if the tlbflush needs to be sent to other CPUs */
        broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
        if (static_branch_unlikely(&use_asid_allocator)) {
-               unsigned long asid = atomic_long_read(&mm->context.id);
+               unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/riscv/mm/tlbflush.c +0 -1
                /*
                 * TLB will be immediately flushed on harts concurrently
--

2.34.1

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

maintainer
#5 lore raw
Quoted 19 lines · On Mon, 20 Mar 2023 19:42:37 PDT (-0700), [email protected] wrote:
Quoted 18 lines · Dylan Jhong
Quoted 17 lines · Zong Li

Dylan Jhong <[email protected]> 於 2023年3月13日 週一 下午12:29寫道:

Quoted 8 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---

Hi Dylan,
Thanks for your patch, if I remember correctly, there was a patch from Alistair Francis did the similar fix. Perhaps we should track that patch to see why it doesn't be merged. Thanks.

http://lists.infradead.org/pipermail/linux-riscv/2022-March/013558.html

Quoted 7 lines
Quoted 7 lines

Hi Zong,
Thanks for the reminder, I didn't notice that Alistair had sent the same patch before.

Hi Palmer, Alistair
http://lists.infradead.org/pipermail/linux-riscv/2022-March/013597.html
This patch does not seem to be cherry-picked back to the released linux kernel, and I have not seen the v4 patch. May I ask how is the follow-up progress of this patch?

Sorry about that, I guess I left it kind of vague as to who was doing what. It looks like we've ended up with enough includes that v3 actually builds now, it's queued up and should end up in fixes tomorrow morning.

Thanks!

Quoted 62 lines
Quoted 4 lines

Best,
Dylan

Hi Palmer,

Ping. Any update on this?

Best,
Dylan

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines

Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
        __asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -2
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
        /* check if the tlbflush needs to be sent to other CPUs */
        broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
        if (static_branch_unlikely(&use_asid_allocator)) {
-               unsigned long asid = atomic_long_read(&mm->context.id);
+               unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
                /*
                 * TLB will be immediately flushed on harts concurrently
--

2.34.1

maintainer
#6 lore raw
Quoted 28 lines · Palmer Dabbelt
Quoted 19 lines · On Mon, 20 Mar 2023 19:42:37 PDT (-0700), [email protected] wrote:
Quoted 18 lines · Dylan Jhong
Quoted 17 lines · Zong Li

Dylan Jhong <[email protected]> 於 2023年3月13日 週一 下午12:29寫道:

Quoted 8 lines

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

---

Hi Dylan,
Thanks for your patch, if I remember correctly, there was a patch from Alistair Francis did the similar fix. Perhaps we should track that patch to see why it doesn't be merged. Thanks.

http://lists.infradead.org/pipermail/linux-riscv/2022-March/013558.html

Quoted 7 lines
Quoted 7 lines

Hi Zong,
Thanks for the reminder, I didn't notice that Alistair had sent the same patch before.

Hi Palmer, Alistair
http://lists.infradead.org/pipermail/linux-riscv/2022-March/013597.html
This patch does not seem to be cherry-picked back to the released linux kernel, and I have not seen the v4 patch. May I ask how is the follow-up progress of this patch?

Quoted 4 lines

Sorry about that, I guess I left it kind of vague as to who was doing what. It looks like we've ended up with enough includes that v3 actually builds now, it's queued up and should end up in fixes tomorrow morning.

Turns out that one still fails some configs, and the one posted here doesn't. So I've got the new one on fixes.

Thanks!

Quoted 65 lines

Thanks!

Quoted 62 lines
Quoted 4 lines

Best,
Dylan

Hi Palmer,

Ping. Any update on this?

Best,
Dylan

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines

Changes from v2:
- Remove unsued EXPORT_SYMBOL()

---
 arch/riscv/include/asm/tlbflush.h | 2 ++
 arch/riscv/mm/context.c           | 2 +-
 arch/riscv/mm/tlbflush.c          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/tlbflush.h +2 -0
index 907b9efd39a8..597d6d8aec28 100644
@@ -12,6 +12,8 @@
 #include <asm/errata_list.h>
 
 #ifdef CONFIG_MMU
+extern unsigned long asid_mask;
+
 static inline void local_flush_tlb_all(void)
 {
        __asm__ __volatile__ ("sfence.vma" : : : "memory");
arch/riscv/mm/context.c +1 -1
index 80ce9caba8d2..6d1aeb063e81 100644
@@ -22,7 +22,7 @@ DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
 
 static unsigned long asid_bits;
 static unsigned long num_asids;
-static unsigned long asid_mask;
+unsigned long asid_mask;
 
 static atomic_long_t current_version;
 
arch/riscv/mm/tlbflush.c +1 -2
index ce7dfc81bb3f..ba4c27187c95 100644
@@ -27,7 +27,7 @@ static void __sbi_tlb_flush_range(struct mm_struct *mm, unsigned long start,
        /* check if the tlbflush needs to be sent to other CPUs */
        broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
        if (static_branch_unlikely(&use_asid_allocator)) {
-               unsigned long asid = atomic_long_read(&mm->context.id);
+               unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
 
                /*
                 * TLB will be immediately flushed on harts concurrently
--

2.34.1

#7 lore raw
In reply to Dylan Jhong
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 8 lines · On Mon, 13 Mar 2023 11:49:06 +0800 you wrote:

Currently, we pass the CONTEXTID instead of the ASID to the TLB flush function. We should only take the ASID field to prevent from touching the reserved bit field.

Fixes 3f1e782998cd ("riscv: add ASID-based tlbflushing methods")

[...]

Here is the summary with links:
- [v2] riscv: mm: Fix incorrect ASID argument when flushing TLB

    https://git.kernel.org/riscv/c/9a801afd3eb9

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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