From patchwork Fri Mar 22 23:38:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 13600499 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6BF8C54E64 for ; Fri, 22 Mar 2024 23:39:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=UVkoLhHFaQPae5QIWT55R8SBJGoTnFw32KtbTxB+0kg=; b=Iv6d5Ncc9B+iTt CAK1yW8j2nSRqQknAqe1XaGpQYPn9lfz1pprXkvy41qPcgREPkpM23UsdRpb7GNx1goaQ2BMUNrB+ gPNJzpVT+OkNbdQK4j8AQ9x6M8i6dIU8IPErXrHC5rV9F1olX80z4hAFOWSyFPEAkSib90+95pxlr 2pODrYaA9D8AJIy1VyKhCd8t+PSGukLPSBxI4fWUVxXuoP97YbfJBY8xNWFLCF1mcESR2l+erpH5i w3RlHmXgypVdPiFvfejy9jO7FFZgWTybauqrBYtIrMnWPVq3Uh8MevZuOI2P3ACt5UB7jXyUq0lA8 iRKzo3VIcJzk3//gOD6w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rnoTr-00000008vwY-39aG; Fri, 22 Mar 2024 23:39:23 +0000 Received: from mail-vk1-xa33.google.com ([2607:f8b0:4864:20::a33]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rnoTj-00000008vqw-0vAs for linux-arm-kernel@lists.infradead.org; Fri, 22 Mar 2024 23:39:17 +0000 Received: by mail-vk1-xa33.google.com with SMTP id 71dfb90a1353d-4d44beade3cso1164648e0c.1 for ; Fri, 22 Mar 2024 16:39:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711150753; x=1711755553; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=QZ2rrwnnSPuoyWFM6/wbqSx+XEHGNK8//eAAMbn2SLU=; b=FKAMGyB4NkEBDrAYB4o8INfQT5yg6OTFR23Qc5jZO5kMTrjGptmAn9KfW8ZVoTYxe8 PJNzd/sLLtwCrfv24X15RjNcC/PMHf/sML8MfD33uPO60Tf4jxDDhZSf85gQZt5XzQ6h PyqMB/n3tNQZrjToErTT9NvTWS9+JIy/VZjQAqTjeXR4vbHCJrEDWjbHTLFAgT4jD4Nt yuz4iwKP3zl/4OX70dcP2IGLom0xE0r6WyOZIqMQ6Z7G6PmQY2NPA3hHyq4tqmAZHZWi H68qq3bBHs5C2TpWMipbKvUcXrn5hdS25Qw+nexOqwU5kp/2Qo/RwKw/hFOAVhkcIgNF M2hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711150753; x=1711755553; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QZ2rrwnnSPuoyWFM6/wbqSx+XEHGNK8//eAAMbn2SLU=; b=sm6wcUJ3su1O95mGaFIFx/W2MC72MemLglwFNuF5zrSnCM/comywq2ymMFPYE/BD7K 9QydyRVnD2ySp3xum28wV2dCT3mT51SVoIiuQAhDQxjBnNx73BDEfX4ylX10MY4p9DB+ CKX/mJU0jlJ7y2naZmmJJJ1WYzcSxgttbZ6cpc2WVvvk0CJP7iCyfZcyCUPULzT6rGea f1U+lDHLcVDJCwJ1hzxRE0nfdHla8zrCTJYRIqpArMwllVgUrbLKRIImDM78Fow3WUFL gaXNEP7+6nOV0t3nZAzGol47j2JfC3sMszR919Q3IHAISkKnwULEh1UwryNpRpLoRHqr lZ0w== X-Forwarded-Encrypted: i=1; AJvYcCWykcQsGXZGZ8yD6YyjlZIiwLPzEU1IQpred0gkXw/bXCdWiFTRlO3AGg2i4FyM8214z38CN5Frol0ujM0i+O1ISaADNh93eaxqLxQSCkXfjaQYRew= X-Gm-Message-State: AOJu0Yw7Fs3bbw42Z25svCjj6JKiY60zTvmiZsUpBj0kAZlRLfBmHPFb DK7qfmQTWq7ch1H4JbpWz/pa/bDscgjEpC1yypO1cT5K30DfUq5X X-Google-Smtp-Source: AGHT+IEaYQaJaeCZmztaSPVyvy8V/Vxeh4oBqEj4kwmvUweyQop95DXCqfqCPm+ZQJ2lS/7MtVUgAg== X-Received: by 2002:a05:6122:c9f:b0:4cb:fc25:7caa with SMTP id ba31-20020a0561220c9f00b004cbfc257caamr1351039vkb.14.1711150752858; Fri, 22 Mar 2024 16:39:12 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id dd7-20020ad45807000000b0069675909982sm456994qvb.70.2024.03.22.16.39.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 16:39:12 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfauth.nyi.internal (Postfix) with ESMTP id 98CB91200032; Fri, 22 Mar 2024 19:39:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 22 Mar 2024 19:39:11 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtfedgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueeh geeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 22 Mar 2024 19:39:10 -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_Roy_Baron?= , 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 Subject: [WIP 3/3] rust: atomic: Add fetch_sub_release() Date: Fri, 22 Mar 2024 16:38:38 -0700 Message-ID: <20240322233838.868874-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240322233838.868874-1-boqun.feng@gmail.com> References: <20240322233838.868874-1-boqun.feng@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240322_163915_351299_F68CF8B5 X-CRM114-Status: GOOD ( 13.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Signed-off-by: Boqun Feng --- rust/kernel/sync/atomic.rs | 23 +++++++++++++++++++++++ rust/kernel/sync/atomic/arch/arm64.rs | 20 ++++++++++++++++++++ rust/kernel/sync/atomic/arch/x86.rs | 5 +++++ 3 files changed, 48 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 280040705fb0..c3cae0d25e88 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -39,4 +39,27 @@ pub fn new(v: i32) -> Self { pub fn fetch_add_relaxed(&self, i: i32) -> i32 { arch::i32_fetch_add_relaxed(&self.0, i) } + + /// Subs `i` to the atomic variable with RELEASE ordering. + /// + /// Returns the old value before the sub. + /// + /// # Example + /// + /// ```rust + /// use kernel::sync::atomic::AtomicI32; + /// + /// let a = AtomicI32::new(1); + /// let b = a.fetch_sub_release(1); + /// let c = a.fetch_sub_release(2); + /// let d = a.fetch_sub_release(3); + /// let e = a.fetch_sub_release(core::i32::MIN); + /// + /// assert_eq!(b, 1); + /// assert_eq!(c, 0); + /// assert_eq!(d, -2); + /// ``` + pub fn fetch_sub_release(&self, i: i32) -> i32 { + arch::i32_fetch_sub_release(&self.0, i) + } } diff --git a/rust/kernel/sync/atomic/arch/arm64.rs b/rust/kernel/sync/atomic/arch/arm64.rs index 438f37cf7df6..beea77ecdb20 100644 --- a/rust/kernel/sync/atomic/arch/arm64.rs +++ b/rust/kernel/sync/atomic/arch/arm64.rs @@ -24,3 +24,23 @@ pub(crate) fn i32_fetch_add_relaxed(v: &UnsafeCell, i: i32) -> i32 { result } + +pub(crate) fn i32_fetch_sub_release(v: &UnsafeCell, i: i32) -> i32 { + let mut result; + unsafe { + asm!( + "prfm pstl1strm, [{v}]", + "1: ldxr {result:w}, [{v}]", + "sub {val:w}, {result:w}, {i:w}", + "stlxr {tmp:w}, {val:w}, [{v}]", + "cbnz {tmp:w}, 1b", + result = out(reg) result, + tmp = out(reg) _, + val = out(reg) _, + v = in(reg) v.get(), + i = in(reg) i, + ) + } + + result +} diff --git a/rust/kernel/sync/atomic/arch/x86.rs b/rust/kernel/sync/atomic/arch/x86.rs index 2d715f740b22..7f764cde4576 100644 --- a/rust/kernel/sync/atomic/arch/x86.rs +++ b/rust/kernel/sync/atomic/arch/x86.rs @@ -41,3 +41,8 @@ pub(crate) fn i32_fetch_add_relaxed(v: &UnsafeCell, i: i32) -> i32 { // SAFETY: `v.get()` points to a valid `i32`. unsafe { i32_xadd(v.get(), i) } } + +pub(crate) fn i32_fetch_sub_release(v: &UnsafeCell, i: i32) -> i32 { + // SAFETY: `v.get()` points to a valid `i32`. + unsafe { i32_xadd(v.get(), i.wrapping_neg()) } +}