Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp514079lql; Mon, 11 Mar 2024 09:09:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXpOOpOI1IlEq3ronAMxvUK0g217VaeCJXhsIZPHsInXVjet28z+2SwSBr1jVogaxGll0nJWYXAs5kM4NZ8Rej+e2RtUCwCU3gUF/hBtg== X-Google-Smtp-Source: AGHT+IEs0YzHxbX//W2HDQljqJTPvK6SCN1KYCSrQum9q+BcItN28X3AuBicUBHJHp23zPlZQ6Ie X-Received: by 2002:a50:d614:0:b0:567:48b9:e9e6 with SMTP id x20-20020a50d614000000b0056748b9e9e6mr4178551edi.42.1710173352535; Mon, 11 Mar 2024 09:09:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710173352; cv=pass; d=google.com; s=arc-20160816; b=TYLT/AMUWpMi2frIx8Knr2tfKfkzGkHwzd/bxuYmpLU2Gd1++RTT8NSlU68lcHEey9 FrWadVASBP8MxBy11g3/ZBtCFBlEUhaXTBqmSS/lQ9NavOvyhIjDAfJ6mfmPgWeLydWK 1FaPJgtxk/K42Lt4rWGL+EDzIyfHyK272imiBFpwth9o6KVcX+vNp7Cch5EvRBAJSzcS 3N0ZXNGsAdaJ/8RpMTpZbaf3OgMPshTRKiCWalFa8hw6RH4U9oiPKtHIz0U5qJ36B01o Youu8fseTsePu84MEOLSF7jtZ9p7RnkpazzYOjBqmvSAOJQO8NQQUbM4+5mcGTTx7x2Q 8zuA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=9adI/6of+7s/Nl97N1SnFVT87VG5FRlJB2WqXNujaws=; fh=Grbf2XJV+5BTwO08rfaijWmcpr8ELgrvDk+9cAcEx0I=; b=k5lt39JGe4G3tDUMjAz6IvKoEEXBy4AT0/HSIEUghO+SreUH37xIJQezZCc51g8Wiv yS1HadHahmgjXz1D8khsSScyd092r6+Q1UEKuduKD+CWrA5Lshnu0msOYvYOyha5a0Er qKXfAy+534OPCZcyGKPdUfv0+O+wNMY3zvALkyGJlwh/Nw+UUrZGw7AKkFpR0A9xI1Wr kZb0eVSnNsk6V2I0J/yhxCzreUAzKVffAzVxZVg0/34LgOWqf/nQ1FA7X/eT0xwEYog5 ZRb5ftYLumVQqNNWr2UZAyUZNrhQrKdcXfqxev7nH7642T60QQ7iiwHw6h2TDheQkdGj dEGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IhD3P0vG; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-99188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t20-20020aa7d714000000b005682919cfd8si2577259edq.401.2024.03.11.09.09.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 09:09:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IhD3P0vG; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-99188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4695D1F21FF0 for ; Mon, 11 Mar 2024 16:09:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 076964CB5B; Mon, 11 Mar 2024 16:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IhD3P0vG" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 8BFDE4AEC9 for ; Mon, 11 Mar 2024 16:08:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710173316; cv=none; b=GPglrGl6rxI+7rb/co0yhptxrOl1UYPM2iMBDnUx3m5KMw1LhNFzoIsbcF3pfpsipVgC++5Twpie4Ly4TV3WB3M3cI6bAmrEFegxbLhz8dJYGwpru0YszqDxID1srqa+zlw+vvbgh+9WRZ1UayS+DvlElxT+u1NroKIexpvoft8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710173316; c=relaxed/simple; bh=dTDELu132/83Pc9WB78HCpiSQH+OEDNxB/Dz/kqP3EE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ui61fPZ8C2CuPs9E268kDRo679bpixk7cGtF2RSlLXT2bo8enpcWpJpEKjx/ms6O0n+17Vvfc9TlSgSLurXma3t5wtRPd1SsxokKELPywTSy1OhiRWNiNutfuae8JUuVQhiLYbyUyzRYnGq+jXTGv0GN7646QxnQF1ztJ3UB410= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IhD3P0vG; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a0815e3f9so43432857b3.2 for ; Mon, 11 Mar 2024 09:08:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710173313; x=1710778113; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9adI/6of+7s/Nl97N1SnFVT87VG5FRlJB2WqXNujaws=; b=IhD3P0vGZ09yXmlWGILPApF5DxaLVd5YhGycri+F8PrAATWU1J8R7OAP83wwXDPMOm tdj/D3WfqMeljVWmhrTubS5NB1NGWJs9uBSQ7TxV+j8Ai3nMXrNXGCt2PWQ9XTGs1FTh Xd3MYoDcoNLaAE9cjIzRir6UhRGIgU8jObxWMsN8OO/tyqksOi6EW6bHIrUMLJG2xnTR cdL2N4krejGbrDUWvwtpBKPdbbaIA+k0UDBUstfFyLipAbvR7BIizQznx/x5aOSwMFth vLMykL61baJlaLDMz/AoPVRZr05liwyMEBVCZrbPZcMG2qnfXpzmbkZ5z0aZEGvqkYr7 aibQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710173313; x=1710778113; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9adI/6of+7s/Nl97N1SnFVT87VG5FRlJB2WqXNujaws=; b=RWcv0ZnyGKQ7hP8dlR14XjVpWIAKaT3yTuMmWLAGKyVuYJeRGKUHw48ohRjwQvdRN/ LSvDDPt2kw8hxCyYjBXlilyDt3XgFR8ZebpxJ4q0LwkEH4CXof7ZZSsE6t9Sq732ra04 CKTAMz93ltk8GXbZXKN7DkHfm8i0IqBm2P1lZTGyJuRd11Rlrf3WKuER1XpFTPZMVH76 FPjxUEv6qJINMVVsPHt9lhlW/ysTWndHWc6VdikqP3riEW3gdCJCqD7WkC1mC9snkBwP YbzqHfzkV7mz+3UTJ7OMWeXs8HnxOrvhJrDdm7cnL+vzX6RegA1yquiOfyVC4fzitcBM /sXA== X-Forwarded-Encrypted: i=1; AJvYcCWCXHopyZZgF041wHYBauVUfEJFsD4XHM6XaBU/U6nKyGRRp7fODDYGG8aLITyHA+DpKgi5MIALN9C1Wf6dXJWlCbBr0c8lMraT2dII X-Gm-Message-State: AOJu0YwqLmxmbyplKip1y3mVD518zWDq704COJ/yKiIJyfXZLWLK3nZE fX5ykAaUeveLNg02YTQ2Worv8RPYIlssuMyPiLL+i+OfAwLs5fFDOSyghZPfvJE0Pu38ADia6+m vQvqv6Yyg+/vB5g== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a0d:d5c6:0:b0:608:398c:33bf with SMTP id x189-20020a0dd5c6000000b00608398c33bfmr146783ywd.8.1710173313704; Mon, 11 Mar 2024 09:08:33 -0700 (PDT) Date: Mon, 11 Mar 2024 16:08:19 +0000 In-Reply-To: <20240311-arc-for-list-v3-0-cba1883c62eb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240311-arc-for-list-v3-0-cba1883c62eb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2578; i=aliceryhl@google.com; h=from:subject:message-id; bh=dTDELu132/83Pc9WB78HCpiSQH+OEDNxB/Dz/kqP3EE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBl7yx4oLnnXqRdNuQvjnTuAOP/so9X0itV5HLJl I/VeRdBlgqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZe8seAAKCRAEWL7uWMY5 RgIWD/9X+MWLobKJpl8KfMnRimQ3J1q3W6wzBE14KOjhM34LNyL9dlkAm/gkD6shGT+FQcg+yko 3Nnd8YYpR3smNN3GNNvi4baNHCOEi6Jzuf9vJXOSxkYxnP3xLwgVgwSL7L6z5dzpIBHjkKPMKYU FCYB55UlxmTiNemYtIMGjZaRX5K68VxTMmUZ34Ra2aOGelVynHFyQaxcoD4tSqD0dGxnt/p6L7d X9/i5lLEJ4hCLJY+gD3vLwmBMLAvmVg4LJwohpkRLle3xKmKlyeaG8vwlo5rAlH0rcRD6KRbbu1 XZxiCwP5EAR9DkyVE0Bj2kaDqvA6YgIhWgxcVK6PBlqfTAHL5S9qdOEdKSg+k9t7SwaIFNBLtdQ duoYjplnNLrHdEkFHtfxZVQerD5M92rFHyERyCqUk6lnh2F8Gu/1rY4YNO1DSqvQay5dMB5fZef 1yFxpgeBoz7X3rL5Ddod7rd87/53OB444EfVPQ33TUp6YnWMjWxVKD3JNr451zc0+pHoGuH3pSA 2ouHu/53HSkcNOBJt0C461LD2A88FYAbL0h/6ucyjKGjCFOTcyNXISfT5p+j+ZexgAHarUUAK/t JH1b56hWiyf7sxpZuhUfUfHb8WGbKqQVtigtZEdcZ0okFtkzEBGfSnMvSKiYk7ds8QB68rGFVkh +ocS26/k/mz51ow== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240311-arc-for-list-v3-2-cba1883c62eb@google.com> Subject: [PATCH v3 2/2] rust: sync: add `Arc::into_unique_or_drop` From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Decrement the refcount of an `Arc`, but handle the case where it hits zero by taking ownership of the now-unique `Arc`, instead of destroying and deallocating it. This is a dependency of the linked list that Rust Binder uses. The linked list uses this method as part of its `ListArc` abstraction. Signed-off-by: Alice Ryhl --- rust/kernel/sync/arc.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 53addb8876c2..ef8b520f65a8 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -290,6 +290,36 @@ pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { pub fn ptr_eq(this: &Self, other: &Self) -> bool { core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr()) } + + /// Converts this [`Arc`] into a [`UniqueArc`], or destroys it if it is not unique. + /// + /// When this destroys the `Arc`, it does so while properly avoiding races. This means that + /// this method will never call the destructor of the value. + pub fn into_unique_or_drop(self) -> Option>> { + // We will manually manage the refcount in this method, so we disable the destructor. + let me = ManuallyDrop::new(self); + // SAFETY: We own a refcount, so the pointer is still valid. + let refcount = unsafe { me.ptr.as_ref() }.refcount.get(); + + // If the refcount reaches a non-zero value, then we have destroyed this `Arc` and will + // return without further touching the `Arc`. If the refcount reaches zero, then there are + // no other arcs, and we can create a `UniqueArc`. + // + // SAFETY: We own a refcount, so the pointer is not dangling. + let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) }; + if is_zero { + // SAFETY: We have exclusive access to the arc, so we can perform unsynchronized + // accesses to the refcount. + unsafe { core::ptr::write(refcount, bindings::REFCOUNT_INIT(1)) }; + + // INVARIANT: We own the only refcount to this arc, so we may create a `UniqueArc`. We + // must pin the `UniqueArc` because the values was previously in an `Arc`, and they pin + // their values. + Some(Pin::from(UniqueArc { inner: ManuallyDrop::into_inner(me) })) + } else { + None + } + } } impl ForeignOwnable for Arc { -- 2.44.0.278.ge034bb2e1d-goog