Skip to contentSkip to conversation

x86/mm/pat: silence a data race in cpa_4k_install

PATCH In review Qian Cai 4 participants 13 messages Jan 21, 2020 – Jan 23, 2020

Subsystems (2)
lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH 1/1 x86/mm/pat: silence a data race in cpa_4k_install

Macro Elver mentioned,

"Yes. I was finally able to reproduce this data race on linux-next (my system doesn't crash though, maybe not enough cores?). Here is a trace with line numbers:

read to 0xffffffffaa59a000 of 8 bytes by interrupt on cpu 7:

cpa_inc_4k_install arch/x86/mm/pat/set_memory.c:131 [inline]
__change_page_attr+0x10cf/0x1840 arch/x86/mm/pat/set_memory.c:1514
__change_page_attr_set_clr+0xce/0x490 arch/x86/mm/pat/set_memory.c:1636
__set_pages_np+0xc4/0xf0 arch/x86/mm/pat/set_memory.c:2148
__kernel_map_pages+0xb0/0xc8 arch/x86/mm/pat/set_memory.c:2178
kernel_map_pages include/linux/mm.h:2719 [inline]

<snip>

write to 0xffffffffaa59a000 of 8 bytes by task 1 on cpu 6:

cpa_inc_4k_install arch/x86/mm/pat/set_memory.c:131 [inline]
__change_page_attr+0x10ea/0x1840 arch/x86/mm/pat/set_memory.c:1514
__change_page_attr_set_clr+0xce/0x490 arch/x86/mm/pat/set_memory.c:1636
__set_pages_p+0xc4/0xf0 arch/x86/mm/pat/set_memory.c:2129
__kernel_map_pages+0x2e/0xc8 arch/x86/mm/pat/set_memory.c:2176
kernel_map_pages include/linux/mm.h:2719 [inline]

<snip>

Both accesses are due to the same "cpa_4k_install++" in cpa_inc_4k_install. Now you can see that a data race here could be potentially undesirable: depending on compiler optimizations or how x86 executes a non-LOCK'd increment, you may lose increments, corrupt the counter, etc. Since this counter only seems to be used for printing some stats, this data race itself is unlikely to cause harm to the system though."

This will generate a lot of noise on a debug kernel with debug_pagealloc with KCSAN enabled which could render the system unusable. Silence it by using the data_race() macro.

---
 arch/x86/mm/pat/set_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +1 -1
index 20823392f4f2..a5c35e57905e 100644
@@ -128,7 +128,7 @@ static inline void cpa_inc_2m_checked(void)
 
 static inline void cpa_inc_4k_install(void)
 {
-	cpa_4k_install++;
+	data_race(cpa_4k_install++);
 }
 
 static inline void cpa_inc_lp_sameprot(int level)
Signature · 1 line
2.21.0 (Apple Git-122.2)
#1 lore raw
Quoted 25 lines · Qian Cai

Macro Elver mentioned,

"Yes. I was finally able to reproduce this data race on linux-next (my system doesn't crash though, maybe not enough cores?). Here is a trace with line numbers:

read to 0xffffffffaa59a000 of 8 bytes by interrupt on cpu 7:

cpa_inc_4k_install arch/x86/mm/pat/set_memory.c:131 [inline]
__change_page_attr+0x10cf/0x1840 arch/x86/mm/pat/set_memory.c:1514
__change_page_attr_set_clr+0xce/0x490 arch/x86/mm/pat/set_memory.c:1636
__set_pages_np+0xc4/0xf0 arch/x86/mm/pat/set_memory.c:2148
__kernel_map_pages+0xb0/0xc8 arch/x86/mm/pat/set_memory.c:2178
kernel_map_pages include/linux/mm.h:2719 [inline]

<snip>

write to 0xffffffffaa59a000 of 8 bytes by task 1 on cpu 6:

cpa_inc_4k_install arch/x86/mm/pat/set_memory.c:131 [inline]
__change_page_attr+0x10ea/0x1840 arch/x86/mm/pat/set_memory.c:1514
__change_page_attr_set_clr+0xce/0x490 arch/x86/mm/pat/set_memory.c:1636
__set_pages_p+0xc4/0xf0 arch/x86/mm/pat/set_memory.c:2129
__kernel_map_pages+0x2e/0xc8 arch/x86/mm/pat/set_memory.c:2176
kernel_map_pages include/linux/mm.h:2719 [inline]

<snip>

Quoted 7 lines

Both accesses are due to the same "cpa_4k_install++" in cpa_inc_4k_install. Now you can see that a data race here could be potentially undesirable: depending on compiler optimizations or how x86 executes a non-LOCK'd increment, you may lose increments, corrupt the counter, etc. Since this counter only seems to be used for printing some stats, this data race itself is unlikely to cause harm to the system though."

Thank you for the patch!

Could you remove the verbatim copy of my email? Maybe something like:

"Increments to cpa_4k_install may happen concurrently, as detected by KCSAN:

<....... the stack traces ......>

Since the counter is only used to count stats, a data race will not be harmful, thus we mark it as an intentional data race with the 'data_race()' macro.

Otherwise, this may generate a lot of noise on a debug kernel with debug_pagealloc
with KCSAN enabled which could render the system unusable."

Thanks,
-- Marco

Quoted 26 lines

This will generate a lot of noise on a debug kernel with debug_pagealloc with KCSAN enabled which could render the system unusable. Silence it by using the data_race() macro.

---
 arch/x86/mm/pat/set_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +1 -2
index 20823392f4f2..a5c35e57905e 100644
@@ -128,7 +128,7 @@ static inline void cpa_inc_2m_checked(void)
 
 static inline void cpa_inc_4k_install(void)
 {
-       cpa_4k_install++;
+       data_race(cpa_4k_install++);
 }
 
 static inline void cpa_inc_lp_sameprot(int level)
--

2.21.0 (Apple Git-122.2)

maintainer
#2 lore raw
Quoted 5 lines · Marco Elver

Could you remove the verbatim copy of my email? Maybe something like:

"Increments to cpa_4k_install may happen concurrently, as detected by KCSAN:

<....... the stack traces ......>

... and drop the stack traces and fix your subject to say what you're actually "fixing".

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#3 lore raw
Quoted 13 lines · Borislav Petkov
Quoted 5 lines · Marco Elver

Could you remove the verbatim copy of my email? Maybe something like:

"Increments to cpa_4k_install may happen concurrently, as detected by KCSAN:

<....... the stack traces ......>

... and drop the stack traces and fix your subject to say what you're actually "fixing".

Does this title work for you?

x86/mm/pat: silence an data race for KCSAN

#4 lore raw
Quoted 10 lines · Qian Cai
Quoted 10 lines · Borislav Petkov
Quoted 4 lines · Marco Elver

Could you remove the verbatim copy of my email? Maybe something like:

"Increments to cpa_4k_install may happen concurrently, as detected by KCSAN:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

<....... the stack traces ......>

... and drop the stack traces and fix your subject to say what you're actually "fixing".

Does this title work for you?

x86/mm/pat: silence an data race for KCSAN

Isn't the intent "x86/mm/pat: Mark intentional data race" ? The fact that KCSAN no longer shows the warning is a side-effect. At least that's how I see it.

Thanks,
-- Marco

maintainer
#5 lore raw
Quoted 3 lines · Marco Elver

Isn't the intent "x86/mm/pat: Mark intentional data race" ? The fact that KCSAN no longer shows the warning is a side-effect. At least that's how I see it.

Perhaps because you've been dealing with KCSAN for so long. :-)

The main angle here, IMO, is that this "fix" is being done solely for KCSAN. Or is there another reason to "fix" intentional data races? At least I don't see one. And the text says

"This will generate a lot of noise on a debug kernel with debug_pagealloc with KCSAN enabled which could render the system unusable."

So yes, I think it should say something about making KCSAN happy.

Oh, and while at it I'd prefer it if it did the __no_kcsan function annotation instead of the data_race() thing.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#6 lore raw
Quoted 15 lines · Borislav Petkov
Quoted 3 lines · Marco Elver

Isn't the intent "x86/mm/pat: Mark intentional data race" ? The fact that KCSAN no longer shows the warning is a side-effect. At least that's how I see it.

Perhaps because you've been dealing with KCSAN for so long. :-)

The main angle here, IMO, is that this "fix" is being done solely for KCSAN. Or is there another reason to "fix" intentional data races? At least I don't see one. And the text says

Quoted 8 lines

"This will generate a lot of noise on a debug kernel with debug_pagealloc with KCSAN enabled which could render the system unusable."

So yes, I think it should say something about making KCSAN happy.

Oh, and while at it I'd prefer it if it did the __no_kcsan function annotation instead of the data_race() thing.

Actually "__no_kcsan" does not work because I have CONFIG_OPTIMIZE_INLINING=y (GCC 8.3.1) here, so it has to be,

arch/x86/mm/pat/set_memory.c +1 -1
index 20823392f4f2..fabbf8a33b7f 100644
@@ -126,7 +126,7 @@ static inline void cpa_inc_2m_checked(void)
        cpa_2m_checked++;
 }
 
-static inline void cpa_inc_4k_install(void)
+static inline void __no_kcsan_or_inline cpa_inc_4k_install(void)
 {
        cpa_4k_install++;
 }

Are you fine with it or data_race() looks better?

maintainer
#7 lore raw
Quoted 1 line · Qian Cai

Actually "__no_kcsan" does not work because I have

Why, because KCSAN conflicts with inlining? I'm looking at the comment over __no_kasan_or_inline.

Quoted 16 lines

CONFIG_OPTIMIZE_INLINING=y (GCC 8.3.1) here, so it has to be,

arch/x86/mm/pat/set_memory.c +1 -1
index 20823392f4f2..fabbf8a33b7f 100644
@@ -126,7 +126,7 @@ static inline void cpa_inc_2m_checked(void)
        cpa_2m_checked++;
 }
 
-static inline void cpa_inc_4k_install(void)
+static inline void __no_kcsan_or_inline cpa_inc_4k_install(void)
 {
        cpa_4k_install++;
 }
Quoted 1 line

Are you fine with it or data_race() looks better?

This one looks marginally better because the annotation is still outside of the function, so to speak.

Btw, looking at the other "inc" CPA statistics functions there, does it mean that for KCSAN they all need to be annotated now too?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Qian Cai

Actually "__no_kcsan" does not work because I have

Why, because KCSAN conflicts with inlining? I'm looking at the comment over __no_kasan_or_inline.

Rather a bug in GCC. AFAIK it is GCC <9 ignoring __attribute__((no_sanitize_*)) for functions that it decides to inline. Somewhere GCC loses the attribute when inlined, resulting in still emitting instrumentation calls. __no_kcsan_or_inline works around it by making such functions noinline. GCC also emits an error if you try to combine __always_inline and __attribute__((no_sanitize_*)).

For these reasons we sadly need __no_kcsan_or_inline (similarly we need __no_kasan_or_inline for KASAN for the same reasons). I hope this will look a bit nicer once we move past GCC<9.

Quoted 10 lines
Quoted 10 lines

CONFIG_OPTIMIZE_INLINING=y (GCC 8.3.1) here, so it has to be,

arch/x86/mm/pat/set_memory.c +0 -0
index 20823392f4f2..fabbf8a33b7f 100644
@@ -126,7 +126,7 @@ static inline void cpa_inc_2m_checked(void)
        cpa_2m_checked++;
 }
 
Quoted 2 lines

arch/x86/mm/pat/set_memory.c static inline void cpa_inc_2m_checked(void) · line 129

Quoted 2 lines
arch/x86/mm/pat/set_memory.c +1 -1
-static inline void cpa_inc_4k_install(void)
+static inline void __no_kcsan_or_inline cpa_inc_4k_install(void)

It should be 'static __no_kcsan_or_inline void cpa_inc_4k_install(void)', since __no_kcsan_or_inline provides __always_inline on non-KCSAN builds.

Thanks,
-- Marco

Quoted 17 lines
Quoted 5 lines
arch/x86/mm/pat/set_memory.c +0 -0
 {
        cpa_4k_install++;
 }

Are you fine with it or data_race() looks better?

This one looks marginally better because the annotation is still outside of the function, so to speak.

Btw, looking at the other "inc" CPA statistics functions there, does it mean that for KCSAN they all need to be annotated now too?

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

author
#9 lore raw
In reply to Borislav Petkov
Quoted 6 lines · Borislav Petkov

Btw, looking at the other "inc" CPA statistics functions there, does it mean that for KCSAN they all need to be annotated now too?

I don’t know. KCSAN never trigger any warnings for other CPA places yet.

maintainer
#10 lore raw
In reply to Borislav Petkov
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Marco Elver

Isn't the intent "x86/mm/pat: Mark intentional data race" ? The fact that KCSAN no longer shows the warning is a side-effect. At least that's how I see it.

Perhaps because you've been dealing with KCSAN for so long. :-)

The main angle here, IMO, is that this "fix" is being done solely for KCSAN. Or is there another reason to "fix" intentional data races? At least I don't see one. And the text says

Documentation. It is a clear and concise marker of intent. Unintended data races are bad.

Also, we've been adding annotations to the kernel source forever, sparse, lockdep, etc.. now KCSAN. All we have to do is make sure they're minimally invasive, and in that regard the date_race() marker is spot on IMO.

author
#11 lore raw
Quoted 11 lines · Peter Zijlstra

Documentation. It is a clear and concise marker of intent. Unintended data races are bad.

Also, we've been adding annotations to the kernel source forever, sparse, lockdep, etc.. now KCSAN. All we have to do is make sure they're minimally invasive, and in that regard the date_race() marker is spot on IMO.

Okay, so which way should we move forward with this then? Borislav liked __no_kasan_or_inline and Peter liked data_race(). I personally like data_race() more because it has nothing to do with the GCC bug, but I realized my opinion has little weight here.

maintainer
#12 lore raw
Quoted 4 lines · Qian Cai

Okay, so which way should we move forward with this then? Borislav liked __no_kasan_or_inline and Peter liked data_race(). I personally like data_race() more because it has nothing to do with the GCC bug, but I realized my opinion has little weight here.

Do the data_race() thing, pls.

But do send after the merge window because latter should open next week and we all will be busy and not review/apply new stuff.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette