rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
94.86k stars 12.23k forks source link

csky targets' atomic RMW is not lock-free #117306

Open taiki-e opened 8 months ago

taiki-e commented 8 months ago

In csky, LLVM always generates libcalls (https://github.com/llvm/llvm-project/commit/ec2de7490813a7593dad59f210c7ec41f1a29002), and atomic implementations are provided by libatomic.

https://github.com/rust-lang/rust/blob/c7224e3c954ce54853364c950608c1f0ed47ea85/compiler/rustc_target/src/spec/csky_unknown_linux_gnuabiv2.rs#L15

However, as mentioned in https://github.com/rust-lang/rust/pull/115577#issuecomment-1732597305, the atomic RMW implementation provided in libatomic.a is not lock-free.

And it violates what the standard library is intended to guarantee.

https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#portability

All atomic types in this module are guaranteed to be lock-free if they’re available. This means they don’t internally acquire a global mutex.

Also, mixing lock-free load/store and non-lock-free RMW can cause data races.

To fix this, we would need to do one of the following:

It is not impossible to fix this on our end, but it is not very realistic as it would require writing a lot of inline assembly.

cc @Dirreke (mentioned because you are target maintainer)


@rustbot label +A-atomic +I-unsound

Dirreke commented 8 months ago

Thanks!

I think to fix LLVM to generate atomic instructions instead of libcalls is a better idea.

The linux system and the gcc-toolchain of csky is not well maintained and the newest version of them is not open-source. Besides, there's many exsited devices with the system which is not suitable for upgrade.

Therefore, I think to fix LLVM is a more realizable and practical way.

bjorn3 commented 8 months ago

(We don't have a CSky target label?)

RalfJung commented 8 months ago

I assume this is about the csky-unknown-linux-gnuabiv2 target.

Dirreke commented 8 months ago

I assume this is about the csky-unknown-linux-gnuabiv2 target.

and the csky-unknown-linux-gnuabiv2hf

apiraino commented 8 months ago

WG-prioritization assigning priority (Zulip discussion).

@Dirreke AFAICS the compiler target csky-unknown-linux-gnuabiv2hf is not mapped on our Tier support list. Do you think it should be updated?

@rustbot label -I-prioritize +P-low

Dirreke commented 8 months ago

@Dirreke AFAICS the compiler target csky-unknown-linux-gnuabiv2hf is not mapped on our Tier support list. Do you think it should be updated?

csky-unknown-linux-gnuabiv2hf is introduced into rust at #117049 . The target csky-unknown-linux-gnuabiv2hf and the related docs will be released in the next version.