From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8389428385 for ; Wed, 12 Jun 2024 22:31:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718231464; cv=none; b=hOKs2S7nRefojCzK8TQXhDsAPtHCg6pIPURBU9L3hvI6Pk9Q07LblY7Q9hMns0zixtqLzyKgfsLpXRAdMtVJriXzb48LcbmnDLJUTrPRRTSZTU35Q9HJieAKf7U/nqQG7tOpAl2ukE2Q11ejyymO7evCzqaAvlKpiqDY0SM4AF8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718231464; c=relaxed/simple; bh=jnIZy7v6gRkoBg8xknS0zEjcBUhw4V/Kkf/pzr9ZLKk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=S9mxJj8xywFa63PbgzZIpzMe91aMe0A74eAmkO55M8agceNmz5zZPQcJ67IlCLHTgyHXgd4PNlf/fmhiny6taJ7A6Ah+HRM6UIpXjDLraBm+fRBSF56Z3KJtsZFnp4LW4idRXIbRW7/GlFSkUVY4ie8n3j2a9mk4Rrj+VaxWnrA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IMqxS9Rt; arc=none smtp.client-ip=209.85.222.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IMqxS9Rt" Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7979198531fso21445585a.0 for ; Wed, 12 Jun 2024 15:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718231461; x=1718836261; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=HaKXS0ZZbil+kikiY7Uq0U6QIK8rKqbj9kgNRJOfwg0=; b=IMqxS9Rtla5Bfp/SuSUn7yM+DsYFK0jyOtubighSQZ6ryQjOLgQPzf2Y7Huja9vqE4 6KRjWLVkkXcd79vC8oDh/U2P6y/pJ7vpbcoCKvY44EuWB/FVrjEYDGX5d7TvG2Jz7e6/ echnUc4wBYetBYeeBu0kVdpzDv+VOANZBhfBs7+vGvzPB4LJ8NZ4VtNo/0t1VKcAm1y5 efq8NrM7DA8Jlf2M68W4lPjDLQGy8kGbLQggVg4av0HFGbnBPeLP7HXszADRWzM73v8S S8iyl1wiQ4ZD4HQbXpA+++rIm9gUU7SFya7mDyGHddq6kVA1cXXbzIAZGmRP2Qzzgrjq nKfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718231461; x=1718836261; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:feedback-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HaKXS0ZZbil+kikiY7Uq0U6QIK8rKqbj9kgNRJOfwg0=; b=EYQIdBo/73z5YVo87rKXyeRki8A0f44kfqjmfiHdwJ1rfthuQqKEXJicyCG14DT1bN VCLmZqX2joLoEKoiLVfoBwplrEmskQgV1C9XRCdXfD/u8LlDs/tGLFASrQD18vQc+XWd bhAbYHol51WyxjT2H2UOZzSAREHmNPu+OzfX6JMygjoHrjMLMy63iDfQSS3V9USZtXwV 8Ja5KavwLEU+Q7t+fFERAcm1TY2YlBnQ/vaip8iAfLnVQddDXfs8dmRQqyd63KiW9mZp ryNEzIRYYBOdVbdihIlK0vrtUcwUKearsqkAGkqEAb/M3amjiBFlP/1Cp1SNeORfU0Um tvhg== X-Forwarded-Encrypted: i=1; AJvYcCXj37Y8Tw1+6ETqGXgwdWGlPrjDau/kqzCF8QhY0h7ea+kexqjGWIKC2QuoJ5HXPNhr1XcgC7E1imt34LcFIBISr9D2Rg== X-Gm-Message-State: AOJu0YxWq6Y/RbjqwR6scvs/e9leb6Y1+UFMEq1pQj9eigW62mHoYP2z 7udUMfDVLRE/QEHhi0dPY3owDCj5l4SHadpvA21wf9hciL7RpPSc X-Google-Smtp-Source: AGHT+IE24GnzXvXHctahz7LCplx76fc3qiNipMvkxpU+kgWUnnS/78AStzuLW5X4k22oyxY3TY7sSg== X-Received: by 2002:a05:6214:3a81:b0:6b0:7a61:c69a with SMTP id 6a1803df08f44-6b1b5c07c5dmr41069396d6.53.1718231461421; Wed, 12 Jun 2024 15:31:01 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b2a5bf2321sm482416d6.6.2024.06.12.15.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 15:31:00 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfauth.nyi.internal (Postfix) with ESMTP id C53A11200068; Wed, 12 Jun 2024 18:30:59 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 12 Jun 2024 18:30:59 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfeduhedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeejtdffueetfeetgfelvdetueeltefgieeiuedttdehuddtleeiheekfeeghfdv veenucffohhmrghinhepghhithhhuhgsrdgtohhmpdhkvghrnhgvlhdrohhrghenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhm vghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekhe ehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghm vg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 12 Jun 2024 18:30:58 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , torvalds@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, Trevor Gross , dakr@redhat.com Subject: [RFC 0/2] Initial LKMM atomics support in Rust Date: Wed, 12 Jun 2024 15:30:23 -0700 Message-ID: <20240612223025.1158537-1-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Hi, This is a follow-up of [1]. Thanks for all the inputs from that thread. I use Mark's outline atomic scripts, but make them specific for atomics in Rust. The reason is that I want to use Gary's work [2], and inline atomics if possible in Rust code. My local test can confirm it works: With RUST_LTO_HELPERS=n 224edc: 52800180 mov w0, #0xc // #12 224ee0: 94000000 bl 219640 With RUST_LTO_HELPERS=y 222fd4: 52800189 mov w9, #0xc // #12 222fd8: b8290108 ldadd w9, w8, [x8] Only AtomicI32 (atomic_t) and AtomicI64 (atomic64_t) are added, and for AtomicPtr (atomic pointers) I plan to implement with compile-time selection on either of these two. You can find a branch contains this series and Gray's patchset at: https://github.com/fbq/linux.git dev/rust/atomic-rfc For testing, I randomly picked up some function and inspected the generated code, plus Rust code can use the function document as tests, so I added two tests there. Ideally we should have something similar to lib/atomic64_test.c, but I want to get some feedback on the implementation part first, plus it's just using C functions, so as long as C code passes the tests, it should be fine (famous last words). ARM64 maintainers, I use the following to simulate cases where LSE is configured but not available from hardware: diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 48e7029f1054..99e6e2b2867f 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1601,6 +1601,8 @@ static bool has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope) { u64 val = read_scoped_sysreg(entry, scope); + if (entry->capability == ARM64_HAS_LSE_ATOMICS) + return false; return feature_matches(val, entry); } and my tests in a qemu emulated VM passed for both RUST_LTO_HELPERS=n and =y cases. Let me know what I can also do to test this. Notes for people who are working on Rust code and need atomics, my target of this set of APIs is 6.11 or 6.12 (will work hard on it, but no guarantee ;-)). If you are currently using Rust own atomics, it's OK, we can always clean up quickly after this merged. Regards, Boqun [1]: https://lore.kernel.org/rust-for-linux/20240322233838.868874-1-boqun.feng@gmail.com/ [2]: https://lore.kernel.org/rust-for-linux/20240529202817.3641974-1-gary@garyguo.net/ Boqun Feng (2): rust: Introduce atomic API helpers rust: sync: Add atomic support MAINTAINERS | 4 +- arch/arm64/kernel/cpufeature.c | 2 + rust/atomic_helpers.h | 1035 ++++++++++++++++ rust/helpers.c | 2 + rust/kernel/sync.rs | 1 + rust/kernel/sync/atomic.rs | 63 + rust/kernel/sync/atomic/impl.rs | 1375 +++++++++++++++++++++ scripts/atomic/gen-atomics.sh | 2 + scripts/atomic/gen-rust-atomic-helpers.sh | 64 + scripts/atomic/gen-rust-atomic.sh | 136 ++ 10 files changed, 2683 insertions(+), 1 deletion(-) create mode 100644 rust/atomic_helpers.h create mode 100644 rust/kernel/sync/atomic.rs create mode 100644 rust/kernel/sync/atomic/impl.rs create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh create mode 100755 scripts/atomic/gen-rust-atomic.sh -- 2.45.2