KCSAN: data-race in __alloc_file / __alloc_file
Subsystems (2)
syzbot
syzbotHello,
syzbot found the following crash on:
HEAD commit: 05f22368 x86, kcsan: Enable KCSAN for x86 git tree: https://github.com/google/ktsan.git kcsan console output: https://syzkaller.appspot.com/x/log.txt?x=10d7fd88e00000 kernel config: https://syzkaller.appspot.com/x/.config?x=87d111955f40591f dashboard link: https://syzkaller.appspot.com/bug?extid=3ef049d50587836c0606 compiler: gcc (GCC) 9.0.0 20181231 (experimental)
Unfortunately, I don't have any reproducer for this crash yet.
IMPORTANT: if you fix the bug, please add the following tag to the commit:
==================================================================
BUG: KCSAN: data-race in __alloc_file / __alloc_file
write to 0xffff8880bb157398 of 4 bytes by task 10993 on cpu 0:
get_cred include/linux/cred.h:253 [inline] __alloc_file+0x74/0x210 fs/file_table.c:105 alloc_empty_file+0x8f/0x180 fs/file_table.c:151 alloc_file+0x4e/0x2b0 fs/file_table.c:193 alloc_file_pseudo+0x11c/0x1b0 fs/file_table.c:232 anon_inode_getfile fs/anon_inodes.c:91 [inline] anon_inode_getfile+0x103/0x1d0 fs/anon_inodes.c:74 __do_sys_perf_event_open+0xd32/0x1ac0 kernel/events/core.c:11100 __se_sys_perf_event_open kernel/events/core.c:10867 [inline] __x64_sys_perf_event_open+0x70/0x90 kernel/events/core.c:10867 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9
write to 0xffff8880bb157398 of 4 bytes by task 11004 on cpu 1:
get_cred include/linux/cred.h:253 [inline] __alloc_file+0x74/0x210 fs/file_table.c:105 alloc_empty_file+0x8f/0x180 fs/file_table.c:151 path_openat+0x74/0x36e0 fs/namei.c:3514 do_filp_open+0x11e/0x1b0 fs/namei.c:3555 do_sys_open+0x3b3/0x4f0 fs/open.c:1097 __do_sys_open fs/open.c:1115 [inline] __se_sys_open fs/open.c:1110 [inline] __x64_sys_open+0x55/0x70 fs/open.c:1110 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 11004 Comm: syz-executor.5 Not tainted 5.4.0-rc3+ #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 ==================================================================
---
This bug is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].
syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
Quoted 52 lines · syzbot
Hello,
syzbot found the following crash on:
HEAD commit:
05f22368x86, kcsan: Enable KCSAN for x86
git tree: https://github.com/google/ktsan.git kcsan console output: https://syzkaller.appspot.com/x/log.txt?x=10d7fd88e00000
kernel config: https://syzkaller.appspot.com/x/.config?x=87d111955f40591f
dashboard link: https://syzkaller.appspot.com/bug?extid=3ef049d50587836c0606
compiler: gcc (GCC) 9.0.0 20181231 (experimental)Unfortunately, I don't have any reproducer for this crash yet.
IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by [email protected]==================================================================
BUG: KCSAN: data-race in __alloc_file / __alloc_filewrite to
0xffff8880bb157398of 4 bytes by task 10993 on cpu 0:get_cred include/linux/cred.h:253 [inline] __alloc_file+0x74/0x210 fs/file_table.c:105 alloc_empty_file+0x8f/0x180 fs/file_table.c:151 alloc_file+0x4e/0x2b0 fs/file_table.c:193 alloc_file_pseudo+0x11c/0x1b0 fs/file_table.c:232 anon_inode_getfile fs/anon_inodes.c:91 [inline] anon_inode_getfile+0x103/0x1d0 fs/anon_inodes.c:74 __do_sys_perf_event_open+0xd32/0x1ac0 kernel/events/core.c:11100 __se_sys_perf_event_open kernel/events/core.c:10867 [inline] __x64_sys_perf_event_open+0x70/0x90 kernel/events/core.c:10867 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9write to
0xffff8880bb157398of 4 bytes by task 11004 on cpu 1:get_cred include/linux/cred.h:253 [inline] __alloc_file+0x74/0x210 fs/file_table.c:105 alloc_empty_file+0x8f/0x180 fs/file_table.c:151 path_openat+0x74/0x36e0 fs/namei.c:3514 do_filp_open+0x11e/0x1b0 fs/namei.c:3555 do_sys_open+0x3b3/0x4f0 fs/open.c:1097 __do_sys_open fs/open.c:1115 [inline] __se_sys_open fs/open.c:1110 [inline] __x64_sys_open+0x55/0x70 fs/open.c:1110 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 11004 Comm: syz-executor.5 Not tainted 5.4.0-rc3+ #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 ==================================================================
Quoted 7 lines
---
This bug is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
Linus, what do you think of the following fix ?
I also took the opportunity avoiding dirtying a cache line if this was possible.
include/linux/cred.h +16 -2
@@ -250,7 +250,14 @@ static inline const struct cred *get_cred(const struct cred *cred)if (!cred)return cred;validate_creds (cred); - nonconst_cred->non_rcu =0 ; + +/* +* Avoid dirtying one cache line. The WRITE_ONCE() also pairs +* with itself, since we run without protection of a lock. +*/ +if (READ_ONCE (nonconst_cred->non_rcu)) +WRITE_ONCE (nonconst_cred->non_rcu,0 ); +return get_new_cred (nonconst_cred); }
@@ -262,7 +269,14 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred)if (!atomic_inc_not_zero (&nonconst_cred->usage))return NULL ;validate_creds (cred); - nonconst_cred->non_rcu =0 ; + +/* +* Avoid dirtying one cache line. The WRITE_ONCE() also pairs +* with itself, since we run without protection of a lock. +*/ +if (READ_ONCE (nonconst_cred->non_rcu)) +WRITE_ONCE (nonconst_cred->non_rcu,0 ); +return cred; }
Quoted 2 lines · Eric Dumazet
Linus, what do you think of the following fix ?
I think it's incredibly ugly.
I realize that avoiding the cacheline dirtying might be worth it, but I'd like to see some indication that it actually matters and helps from a performance angle. We've already dirtied memory fairly close, even if it might not share a cacheline (that structure is randomized, we've touched - or will touch - 'cred->usage') too.
Honestly, I don't think get_cred() is even in a hotpath. Most cred use just use the current cred that doesn't need the 'get'. So the optimization looks somewhat questionable - for all we know it just makes things worse.
I also don't like using a "WRITE_ONCE()" without a reason for it. In this case, the only "reason" is that KCSAN special-cases that thing. I'd much rather have some other way to mark it.
So it just looks hacky to me.
I like that people are looking at KCSAN, but I get a very strong feeling that right now the workarounds for KCSAN false-positives are incredibly ugly, and not always appropriate.
There is absolutely zero need for a WRITE_ONCE() in this case. The code would work fine if the compiler did the zero write fifty times, and re-ordered it wildly. We have a flag that starts out set, and we clear it. There's really no "write-once" about it.
Linus
Quoted 24 lines · Linus Torvalds
Quoted 2 lines · Eric Dumazet
Linus, what do you think of the following fix ?
I think it's incredibly ugly.
I realize that avoiding the cacheline dirtying might be worth it, but I'd like to see some indication that it actually matters and helps from a performance angle. We've already dirtied memory fairly close, even if it might not share a cacheline (that structure is randomized, we've touched - or will touch - '
cred->usage') too.Honestly, I don't think
get_cred()is even in a hotpath. Most cred use just use the current cred that doesn't need the 'get'. So the optimization looks somewhat questionable - for all we know it just makes things worse.I also don't like using a "
WRITE_ONCE()" without a reason for it. In this case, the only "reason" is that KCSAN special-cases that thing. I'd much rather have some other way to mark it.So it just looks hacky to me.
Quoted 9 lines
I like that people are looking at KCSAN, but I get a very strong feeling that right now the workarounds for KCSAN false-positives are incredibly ugly, and not always appropriate.
There is absolutely zero need for a
WRITE_ONCE()in this case. The code would work fine if the compiler did the zero write fifty times, and re-ordered it wildly. We have a flag that starts out set, and we clear it. There's really no "write-once" about it.
Ok, so what do you suggest next ?
Declare KCSAN useless because too many false positives ?
Quoted 4 lines · Eric Dumazet
Ok, so what do you suggest next ?
Declare KCSAN useless because too many false positives ?
I'd hope that there is some way to mark the cases we know about where we just have a flag. I'm not sure what KCSAN uses right now - is it just the "volatile" that makes KCSAN ignore it, or are there other ways to do it?
"volatile" has huge problems with code generation for gcc. It would probably be fine for "not_rcu" in this case, but I'd like to avoid it in general otherwise, which is why I wonder if there are other options.
But worst comes to worst, I'd be ok with a WRITE_ONCE() and a comment about why (and the reason being KCSAN, not the questionable optimization).
Linus
Quoted 4 lines · Linus Torvalds
I'd hope that there is some way to mark the cases we know about where we just have a flag. I'm not sure what KCSAN uses right now - is it just the "volatile" that makes KCSAN ignore it, or are there other ways to do it?
I dunno, Marco will comment on this.
I personally like WRITE_ONCE() since it adds zero overhead on generated code, and is the facto accessor we used for many years (before KCSAN was conceived)
Quoted 9 lines
"volatile" has huge problems with code generation for gcc. It would probably be fine for "
not_rcu" in this case, but I'd like to avoid it in general otherwise, which is why I wonder if there are other options.But worst comes to worst, I'd be ok with a
WRITE_ONCE()and a comment about why (and the reason being KCSAN, not the questionable optimization).
Ok for a single WRITE_ONCE() with a comment.
Hmm, which questionable optimization are you referring to?
Quoted 9 lines · Eric Dumazet
Quoted 4 lines · Linus Torvalds
I'd hope that there is some way to mark the cases we know about where we just have a flag. I'm not sure what KCSAN uses right now - is it just the "volatile" that makes KCSAN ignore it, or are there other ways to do it?
Quoted 5 lines
I dunno, Marco will comment on this.
I personally like
WRITE_ONCE()since it adds zero overhead on generated code, and is the facto accessor we used for many years (before KCSAN was conceived)
BTW, I would love an efficient ADD_ONCE(variable, value)
Using WRITE_ONCE(variable, variable + value) is not good, since it can not use the
optimized instructions operating directly on memory.
Quoted 17 lines · Eric Dumazet
Quoted 9 lines · Eric Dumazet
Quoted 4 lines · Linus Torvalds
I'd hope that there is some way to mark the cases we know about where we just have a flag. I'm not sure what KCSAN uses right now - is it just the "volatile" that makes KCSAN ignore it, or are there other ways to do it?
Quoted 5 lines
I dunno, Marco will comment on this.
I personally like
WRITE_ONCE()since it adds zero overhead on generated code, and is the facto accessor we used for many years (before KCSAN was conceived)
Quoted 5 lines
BTW, I would love an efficient
ADD_ONCE(variable, value)Using
WRITE_ONCE(variable, variable + value)is not good, since it can not use the
optimized instructions operating directly on memory.
Another interesting KCSAN report :
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{
s64 ret = fbc->count; // data-race ....
barrier(); /* Prevent reloads of fbc->count */
if (ret >= 0)
return ret;
return 0;
}How was this code supposed to work at all on 32bit arches ???
Using READ_ONCE(fbc->count) alone will not help.
BUG: KCSAN: data-race in ip6_dst_gc / ip6_dst_gc
read to 0xffff88811dd6298c of 4 bytes by task 10060 on cpu 1:
dst_entries_get_fast include/net/dst_ops.h:47 [inline] ip6_dst_gc+0xf6/0x220 net/ipv6/route.c:3167 dst_alloc+0x104/0x149 net/core/dst.c:85 ip6_dst_alloc+0x3d/0x80 net/ipv6/route.c:353 ip6_rt_cache_alloc+0x8d/0x340 net/ipv6/route.c:1338 ip6_pol_route+0x4ec/0x5c0 net/ipv6/route.c:2217 ip6_pol_route_output+0x48/0x60 net/ipv6/route.c:2452 fib6_rule_lookup+0x95/0x470 net/ipv6/fib6_rules.c:113 ip6_route_output_flags_noref+0x16b/0x230 net/ipv6/route.c:2484 ip6_route_output_flags+0x50/0x1a0 net/ipv6/route.c:2497 ip6_dst_lookup_tail+0x25d/0xc30 net/ipv6/ip6_output.c:1052 ip6_dst_lookup_flow+0x68/0x120 net/ipv6/ip6_output.c:1153 rawv6_sendmsg+0x82c/0x21e0 net/ipv6/raw.c:928 inet_sendmsg+0x6d/0x90 net/ipv4/af_inet.c:807 sock_sendmsg_nosec net/socket.c:637 [inline] sock_sendmsg+0x9f/0xc0 net/socket.c:657 kernel_sendmsg+0x4d/0x70 net/socket.c:677 sock_no_sendpage+0xda/0x110 net/core/sock.c:2742 kernel_sendpage+0x7b/0xc0 net/socket.c:3682 sock_sendpage+0x6c/0x90 net/socket.c:935 pipe_to_sendpage+0x102/0x180 fs/splice.c:449 splice_from_pipe_feed fs/splice.c:500 [inline] __splice_from_pipe+0x248/0x480 fs/splice.c:624 splice_from_pipe+0xbb/0x100 fs/splice.c:659 generic_splice_sendpage+0x45/0x60 fs/splice.c:829 do_splice_from fs/splice.c:848 [inline] direct_splice_actor+0xa0/0xc0 fs/splice.c:1020 splice_direct_to_actor+0x215/0x510 fs/splice.c:975 do_splice_direct+0x161/0x1e0 fs/splice.c:1063 do_sendfile+0x384/0x7f0 fs/read_write.c:1464 __do_sys_sendfile64 fs/read_write.c:1525 [inline] __se_sys_sendfile64 fs/read_write.c:1511 [inline] __x64_sys_sendfile64+0x12a/0x140 fs/read_write.c:1511 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 6 lines · Eric Dumazet
Another interesting KCSAN report :
static inline s64
percpu_counter_read_positive(struct percpu_counter *fbc){ s64 ret = fbc->count; // data-race ....
Yeah, I think that's fundamentally broken on 32-bit. It might need a sequence lock instead of that raw_spinlock_t to be sanely done properly on 32-bit.
Or we just admit that 32-bit doesn't really matter any more in the long run, and that this is not a problem in practice because the 32-bit overflow basically never happens on small machines anyway.
Linus
Quoted 5 lines · Eric Dumazet
BTW, I would love an efficient
ADD_ONCE(variable, value)Using
WRITE_ONCE(variable, variable + value)is not good, since it can not use the optimized instructions operating directly on memory.
So I'm having a hard time seeing how this could possibly ever be valid.
Is this a "writer is locked, readers are unlocked" case or something?
Because we don't really have any sane way to do that any more efficiently, unless we'd have to add new architecture-specific functions for it (like we do have fo the percpu ops).
Anyway, if you have a really hot case you care about, maybe you could convince the gcc people to just add it as a peephole optimization? Right now, gcc ends up doing some strange things with volatiles, and basically disables a lot of stuff over them. But with a test-case, maybe you can convince somebody that certain optimizations are still fine. A "read+add+write" really does the exact same accesses as an add-to-memory instruction, but gcc has some logic to disable that instruction fusion.
Linus
Quoted 11 lines · Linus Torvalds
Quoted 5 lines · Eric Dumazet
BTW, I would love an efficient
ADD_ONCE(variable, value)Using
WRITE_ONCE(variable, variable + value)is not good, since it can not use the optimized instructions operating directly on memory.So I'm having a hard time seeing how this could possibly ever be valid.
Is this a "writer is locked, readers are unlocked" case or something?
per cpu SNMP counters mostly, with no IRQ safety requirements.
Note that this could be implemented using local{64}_add() on arches like x86_64, while others might have to fallback to WRITE_ONCE(variable, variable + add)
Quoted 15 lines
Because we don't really have any sane way to do that any more efficiently, unless we'd have to add new architecture-specific functions for it (like we do have fo the percpu ops).
Anyway, if you have a really hot case you care about, maybe you could convince the gcc people to just add it as a peephole optimization? Right now, gcc ends up doing some strange things with volatiles, and basically disables a lot of stuff over them. But with a test-case, maybe you can convince somebody that certain optimizations are still fine. A "read+add+write" really does the exact same accesses as an add-to-memory instruction, but gcc has some logic to disable that instruction fusion.
Linus
Quoted 5 lines · Eric Dumazet
per cpu SNMP counters mostly, with no IRQ safety requirements.
Note that this could be implemented using local{64}_add() on arches like
x86_64, while others might have to fallback toWRITE_ONCE(variable, variable + add)
raw_cpu_add()?
We already use those for vm_counters where we intentionally accept races.
Linus
Quoted 3 lines · Eric Dumazet
I personally like
WRITE_ONCE()since it adds zero overhead on generated code, and is the facto accessor we used for many years (before KCSAN was conceived)
So I generally prefer WRITE_ONCE() over adding "volatile" to random data structure members.
Because volatile *does* have potentially absolutely horrendous overhead on generated code. It just happens to be ok for the simple case of writing once to a variable.
In fact, you bring that up yourself in your next email when you ask for "ADD_ONCE()". Exactly because gcc generates absolutely horrendous garbage for volatiles, for no actual good reason. Gcc *could* generate a single add-to-memory instruction. But no, that's not at all what gcc does.
So for the kernel, we've generally had the rule to avoid 'volatile' data structures as much as humanly possible, because it actually does something much worse than it could do, and the source code _looks_ simple when the volatile is hidden in the data structures.
Which is why we have READ_ONCE/WRITE_ONCE - it puts the volatile in the code, and makes it clear not only what is going on, but also the impact it has on code generation.
But at the same time, I don't love WRITE_ONCE() when it's not actually about writing once. It might be better to have another way to show "this variable is a flag that we set to a single value". Even if maybe the implementation is then the same (ie we use a 'volatile' assignment to make KCSAN happy).
Quoted 1 line
Hmm, which questionable optimization are you referring to?
The "avoid dirty cacheline" one by adding a read and a conditional. Yes, it can be an optimization. And it might not be.
Linus
Quoted 28 lines · Linus Torvalds
Quoted 3 lines · Eric Dumazet
I personally like
WRITE_ONCE()since it adds zero overhead on generated code, and is the facto accessor we used for many years (before KCSAN was conceived)So I generally prefer
WRITE_ONCE()over adding "volatile" to random data structure members.Because volatile *does* have potentially absolutely horrendous overhead on generated code. It just happens to be ok for the simple case of writing once to a variable.
In fact, you bring that up yourself in your next email when you ask for "
ADD_ONCE()". Exactly because gcc generates absolutely horrendous garbage for volatiles, for no actual good reason. Gcc *could* generate a single add-to-memory instruction. But no, that's not at all what gcc does.So for the kernel, we've generally had the rule to avoid 'volatile' data structures as much as humanly possible, because it actually does something much worse than it could do, and the source code _looks_ simple when the volatile is hidden in the data structures.
Which is why we have READ_ONCE/WRITE_ONCE - it puts the volatile in the code, and makes it clear not only what is going on, but also the impact it has on code generation.
Quoted 5 lines
But at the same time, I don't love
WRITE_ONCE()when it's not actually about writing once. It might be better to have another way to show "this variable is a flag that we set to a single value". Even if maybe the implementation is then the same (ie we use a 'volatile' assignment to make KCSAN happy).
(+some LKMM folks, in case I missed something on what the LKMM defines as data race.)
KCSAN does not use volatile to distinguish accesses. Right now READ_ONCE, WRITE_ONCE, atomic bitops, atomic_t (+ some arch specific primitives) are treated as marked atomic operations.
The goal is to cover all primitives that the LKMM declares as marked/atomic. A data race is then detected for concurrent conflicting accesses where at least one is plain unmarked. In the end the LKMM should decide what KCSAN determines as a data race. As far as I can tell, none of the reported data races so far are false positives in that sense.
Many thanks,
-- Marco
Quoted 4 lines · Marco Elver
KCSAN does not use volatile to distinguish accesses. Right now
READ_ONCE,WRITE_ONCE, atomic bitops,atomic_t(+ some arch specific primitives) are treated as marked atomic operations.
Ok, so we'd have to do this in terms of ATOMIC_WRITE().
One alternative might be KCSAN enhancement, where you notice the following pattern:
- a field is initialized before the data structure gets exposed (I
presume KCSAN already must understand about this issue - initializations are different and not atomic)
- while the field is live, there are operations that write the same
(let's call it "idempotent") value to the field under certain circumstances
- at release time, after all the reference counts are gone, the field
is read for whether that situation happened. I'm assuming KCSAN already understands about this case too?
So it would only be the "idempotent writes" thing that would be something KCSAN would have to realize do not involve a race - because it simply doesn't matter if two writes of the same value race against each other.
But I guess we could also just do
#define WRITE_IDEMPOTENT(x,y) WRITE_ONCE(x,y)
and use that in the kernel to annotate these things. And if we have that kind of annotation, we could then possibly change it to
#define WRITE_IDEMPOTENT(x,y) \
if READ_ONCE(x)!=y WRITE_ONCE(x,y)if we have numbers that that actually helps (that macro written to be intentionally invalid C - it obviously needs statement protection and protection against evaluating the arguments multiple times etc).
Linus
Quoted 16 lines · Linus Torvalds
Quoted 4 lines · Marco Elver
KCSAN does not use volatile to distinguish accesses. Right now
READ_ONCE,WRITE_ONCE, atomic bitops,atomic_t(+ some arch specific primitives) are treated as marked atomic operations.Ok, so we'd have to do this in terms of
ATOMIC_WRITE().One alternative might be KCSAN enhancement, where you notice the following pattern:
- a field is initialized before the data structure gets exposed (I
presume KCSAN already must understand about this issue - initializations are different and not atomic)
Quoted 7 lines
- while the field is live, there are operations that write the same
(let's call it "idempotent") value to the field under certain circumstances- at release time, after all the reference counts are gone, the field
is read for whether that situation happened. I'm assuming KCSAN already understands about this case too?
It's not explicitly aware of initialization or release. We rely on compiler instrumentation for all memory accesses; KCSAN then sets up "watchpoints" for sampled memory accesses, delaying execution, and checking if a concurrent access is observed.
We already have an option (currently disabled on syzbot) where KCSAN infers a data race not because another instrumented accesses happened concurrently, but because the data value changed during a watchpoint's lifetime (e.g. due to uninstrumented write, device write etc.).
This same approach could be used to ignore "idempotent writes" where we would otherwise report a data race; i.e. if there was a concurrent write, but the data value did not change, do not report the race. I'm happy to add this feature if this should always be ignored.
Quoted 12 lines
So it would only be the "idempotent writes" thing that would be something KCSAN would have to realize do not involve a race - because it simply doesn't matter if two writes of the same value race against each other.
But I guess we could also just do
#define WRITE_IDEMPOTENT(x,y) WRITE_ONCE(x,y)and use that in the kernel to annotate these things. And if we have that kind of annotation, we could then possibly change it to
Quoted 8 lines
#define WRITE_IDEMPOTENT(x,y) \ if READ_ONCE(x)!=y WRITE_ONCE(x,y)if we have numbers that that actually helps (that macro written to be intentionally invalid C - it obviously needs statement protection and protection against evaluating the arguments multiple times etc).
Linus
Thanks,
-- Marco
Quoted 5 lines · Marco Elver
It's not explicitly aware of initialization or release. We rely on compiler instrumentation for all memory accesses; KCSAN then sets up "watchpoints" for sampled memory accesses, delaying execution, and checking if a concurrent access is observed.
Ok.
Quoted 4 lines
This same approach could be used to ignore "idempotent writes" where we would otherwise report a data race; i.e. if there was a concurrent write, but the data value did not change, do not report the race. I'm happy to add this feature if this should always be ignored.
Hmm. I don't think it's valid in general, but it might be useful enough in practice, at least as an option to lower the false positives.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Marco Elver
It's not explicitly aware of initialization or release. We rely on compiler instrumentation for all memory accesses; KCSAN then sets up "watchpoints" for sampled memory accesses, delaying execution, and checking if a concurrent access is observed.
Ok.
Quoted 8 lines
Quoted 4 lines
This same approach could be used to ignore "idempotent writes" where we would otherwise report a data race; i.e. if there was a concurrent write, but the data value did not change, do not report the race. I'm happy to add this feature if this should always be ignored.
Hmm. I don't think it's valid in general, but it might be useful enough in practice, at least as an option to lower the false positives.
Minor point...
Can we please agree to call these writes something other than "idempotent"? After all, any write to normal memory is idempotent in the sense that doing it twice has the same effect as doing it once (ignoring possible races, of course).
A better name would be "write-if-different" or "write-if-changed" (and I bet people can come up with something even better if they try). This at least gets across the main idea, and using
WRITE_IF_CHANGED(x, y);
to mean
if (READ_ONCE(x) != y) WRITE_ONCE(x, y);
is a lot clearer than using WRITE_IDEMPOTENT(x, y).
Alan Stern
Quoted 5 lines · Alan Stern
Can we please agree to call these writes something other than "idempotent"? After all, any write to normal memory is idempotent in the sense that doing it twice has the same effect as doing it once (ignoring possible races, of course).
No!
You're completely missing the point.
Two writes to normal memory are *not* idempotent if they write different values. The ordering very much matters, and it's racy and a tool should complain.
But the point of WRITE_IDEMPOTENT() is that it *always* writes the same value, so it doesn't matter if two different writers race on it.
So it really is about being idempotent.
Quoted 1 line
A better name would be "write-if-different" or "write-if-changed"
No.
Again, you're totally missing the point.
It's not about "write-if-different".
It's about idempotent writes.
But if you do know that all the possible racing writes are idempotent, then AS A POSSIBLE CACHE OPTIMIZATION, you can then say "only do this write if somebody else didn't already do it".
But that's a side effect of being idempotent, not the basic rule! And it's not necessarily obviously an optimization at all, because the cacheline may already be dirty, and checking the old value and having a conditional on it may be much more expensive than just writing the new value./
The point is that certain writes DO NOT CARE ABOUT ORDERING, because they may be setting a sticky flag (or stickily clearing a flag, as in this case). The ordering doesn't matter, because the operation is idempotent.
That's what "idempotent" means. You can do it once, or a hundred times, and the end result is the same (but is different from not doing it at all).
And no, not all writes are idempotent. That's just crazy talk. Writes have values.
Linus
Quoted 7 lines · Linus Torvalds
Quoted 5 lines · Alan Stern
Can we please agree to call these writes something other than "idempotent"? After all, any write to normal memory is idempotent in the sense that doing it twice has the same effect as doing it once (ignoring possible races, of course).
Quoted 7 lines
No!
You're completely missing the point.
Two writes to normal memory are *not* idempotent if they write different values. The ordering very much matters, and it's racy and a tool should complain.
What you have written strongly indicates that either you think the word "idempotent" means something different from its actual meaning or else you are misusing the word in a very confusing way.
Your text seems to say that two operations are idempotent if they have the same effect. Compare that to the definition from Wikipedia (not the best in the world, perhaps, but adequate for this discussion):
Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
For example, writing 5 to x is idempotent because performing that write multiple times will have the same result as performing it only once. Likewise for any write to normal memory.
Therefore talking about idempotent writes as somehow being distinct from other writes does not make sense. Nor does it make sense to say "Two writes to normal memory are *not* idempotent if they write different values", because idempotence is a property of individual operations, not of pairs of operations.
You should use a different word, because what you mean is different from what "idempotent" actually means.
Quoted 4 lines
But the point of
WRITE_IDEMPOTENT()is that it *always* writes the same value, so it doesn't matter if two different writers race on it.So it really is about being idempotent.
Okay, I agree that I did completely miss the point of what you originally meant. But what you meant wasn't "being idempotent".
Quoted 9 lines
Quoted 1 line
A better name would be "write-if-different" or "write-if-changed"
No.
Again, you're totally missing the point.
It's not about "write-if-different".
It's about idempotent writes.
Assuming you really are talking about writes (presumably in different threads) that store the same value to the same location: Yes, two such writes do not in practice race with each other -- even though they may satisfy the formal definition of a data race.
On the other hand, they may each race with other accesses.
Quoted 3 lines
But if you do know that all the possible racing writes are idempotent, then AS A POSSIBLE CACHE OPTIMIZATION, you can then say "only do this write if somebody else didn't already do it".
In fact, you can _always_ perform that possible optimization. Whether it will be worthwhile is a separate matter...
Quoted 5 lines
But that's a side effect of being idempotent, not the basic rule! And it's not necessarily obviously an optimization at all, because the cacheline may already be dirty, and checking the old value and having a conditional on it may be much more expensive than just writing the new value./
Agreed.
Quoted 4 lines
The point is that certain writes DO NOT CARE ABOUT ORDERING, because they may be setting a sticky flag (or stickily clearing a flag, as in this case). The ordering doesn't matter, because the operation is idempotent.
Ah, here you use the word correctly.
Quoted 3 lines
That's what "idempotent" means. You can do it once, or a hundred times, and the end result is the same (but is different from not doing it at all).
Precisely the point I make above.
Quoted 2 lines
And no, not all writes are idempotent. That's just crazy talk. Writes have values.
By "writes have values", do you mean that writes store values? Of course they do. But it is clear from what you wrote just above that all writes _are_ idempotent, because doing a write once or a hundred times will yield the same end result.
On a more productive note, do you think we should change the LKMM so that it won't consider two writes to race with each other if they store the same value?
Alan Stern