Skip to content

csky targets' atomic RMW is not lock-free and may cause data races between load/store and it #117306

Description

@taiki-e

In csky, LLVM always generates libcalls (llvm/llvm-project@ec2de74), and atomic implementations are provided by libatomic.

late_link_args_static: TargetOptions::link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-l:libatomic.a"]),

However, as mentioned in #115577 (comment), 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:

  • Fix libatomic to make the RMW implementation lock-free.
  • Fix LLVM to generate atomic instructions instead of libcalls.

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-atomicArea: Atomics, barriers, and sync primitives
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 28, 2023
  2. Dirreke commented on Oct 28, 2023

    @Dirreke
    Contributor

    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.

  3. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 28, 2023
  4. bjorn3 commented on Oct 28, 2023

    @bjorn3
    Member

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

  5. RalfJung commented on Oct 28, 2023

    @RalfJung
    Member

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

  6. Dirreke commented on Oct 28, 2023

    @Dirreke
    Contributor

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

    and the csky-unknown-linux-gnuabiv2hf

  7. apiraino commented on Oct 30, 2023

    @apiraino
    Contributor

    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

  8. added
    P-lowLow priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 30, 2023
  9. Dirreke commented on Oct 30, 2023

    @Dirreke
    Contributor

    @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.

  10. taiki-e commented on Jul 2, 2025

    @taiki-e
    MemberAuthor

    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.

    Btw, taiki-e/atomic-maybe-uninit#32 (not merged) has an experimental inline assembly based implementation. (However, I have not tested that, because the emulator described in the platform documentation always crashes.)

  11. taiki-e commented on Oct 4, 2025

    @taiki-e
    MemberAuthor
    • Fix LLVM to generate atomic instructions instead of libcalls.

    The results of the investigation in the atomic-maybe-uninit PR mentioned above indicate that this approach is likely insufficient for the soft float target (csky-unknown-linux-gnuabiv2).

    In C-SKY, the ldex/stex instructions required for RMW are only available on certain CPUs (ck860*) and cannot be used in the baseline for the soft float target1. (See the second part of taiki-e/atomic-maybe-uninit#32 (comment) for details.) The hard-float target (csky-unknown-linux-gnuabiv2hf)'s baseline2 is okay.

    This is a similar situation to pre-v6 Arm, and on pre-v6 Arm we implement atomics using helpers provided by the kernel.

    C-SKY also seems to have a similar helper, but it doesn't seem to be exposed, so we probably can't adopt the same approach at this point.

    Raising the baseline would make this approach okay, but that's may not the wanted way here. In that case, I think we'd need to expose kernel helpers like Arm does, but considering that it won't work on older kernels, I suspect it will take quite some time before the kernel helper approach can be used.

    Footnotes

    1. Likely ck810, considering linker warnings when target-cpu=ck860 passed: "arch flag ck860 conflict with target ck810,set target arch flag to ck860" ↩

    2. ck860fv ↩

  12. changed the title [-]csky targets' atomic RMW is not lock-free[/-] [+]csky targets' atomic RMW is not lock-free and may cause data races between load/store [/+] on Jul 19, 2026
  13. changed the title [-]csky targets' atomic RMW is not lock-free and may cause data races between load/store [/-] [+]csky targets' atomic RMW is not lock-free and may cause data races between load/store and it[/+] on Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-atomicArea: Atomics, barriers, and sync primitivesC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-cskyTarget: glaCSKY above covers over me~P-lowLow priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions