From nobody Fri Dec 19 04:18:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3779C05027 for ; Wed, 1 Feb 2023 23:23:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231946AbjBAXW6 (ORCPT ); Wed, 1 Feb 2023 18:22:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231897AbjBAXW4 (ORCPT ); Wed, 1 Feb 2023 18:22:56 -0500 Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 482971BDC; Wed, 1 Feb 2023 15:22:55 -0800 (PST) Received: by mail-qv1-xf34.google.com with SMTP id k18so27141qvm.7; Wed, 01 Feb 2023 15:22:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=xDOpfFKpqkig/ifJO3DdK44tzsI39GfXAo3DZPsupvI=; b=SWIFInt+tnHdNOuNDsy1BmdUYNLVTjdcAJto1P0JfMykBg4bI8D7W4yZy00i0n3QXa JBfOskH/f31gPO6cNFvcqf+tBkaFPJYVi7wfEU3pbtdziSU++uBtuBWE92S3riwEDAd1 VpOMQq21Z5UeO5g2jynQuBhCZNhOXBnvzYQroZtJBI8bfKy3Akv4JtqdUCO7i3zkdkAM JcviuUA6fPEYBJBkhnRpIHPfg3cwXqpQoJwIiiDibalvZWsQKHGVp7lrLKnBBrnhYT8Z EE67zWuUSL79pSt5lmytovjpoJFJXh/HU3aQ2fhHqFfq/Qg0y13TOAaiEDq3FB4Rf8et H+QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xDOpfFKpqkig/ifJO3DdK44tzsI39GfXAo3DZPsupvI=; b=fqpFzhAxybaN09pbv0syxdkp9OPhd6ofEOPri84W6ucaDfHum8497Kicakh0lINqUR 5yL7VPrneDAcwc+PBs7aU7wO+j8/anFoFL3l566QBYnH1Z8JTDQvC5nTXxFaB8deUGUR UCIIpnfVnoTwJ+A4tyRw8hdmr5UsEmr/x0aC31ZtsEJtfiVmWniDCNnwkXs2jUehJRb8 NkildCetlQKOBmGjyL0CzpO1Bv++7PT8G6rZ62HdQaqGiedQCPSgdiVJv5UtKh3Tm0VF HAsAiD/JGvYq6BoOC6OwfP55fsQQMuqKnO3+Qb1IrSCUuI3fVejRuNAZ4ys76EA4SiOV saoA== X-Gm-Message-State: AO0yUKX/brJf1O2j5X86/1U+Gzx9CTCGYBXzTBkeNxOAyyqNtgSEqSq3 XNMcjRKxwi2XqENUWY2HNdvX4PFHG6A= X-Google-Smtp-Source: AK7set9a6IxO3IXNd54tsP2BmFYctJnxgkrmxnm4fG+vw1gzaPtFWoaaQhoRP8o0OPSgdQJfcqPFVg== X-Received: by 2002:ad4:5009:0:b0:534:7371:ab01 with SMTP id s9-20020ad45009000000b005347371ab01mr6384503qvo.7.1675293774237; Wed, 01 Feb 2023 15:22:54 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id pi48-20020a05620a37b000b006e16dcf99c8sm13181950qkn.71.2023.02.01.15.22.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:22:53 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id D1FCB27C005A; Wed, 1 Feb 2023 18:22:52 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 01 Feb 2023 18:22:52 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudefjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehfedvtdekjedtleffkefgtdehueegfeffkeeggffhjefhudeikeejgeeh tedvfeenucffohhmrghinhepphhtrhdrrghsnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhs ohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnh hgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Feb 2023 18:22:52 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo Subject: [RFC 1/5] rust: sync: impl Display for {Unique,}Arc Date: Wed, 1 Feb 2023 15:22:40 -0800 Message-Id: <20230201232244.212908-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201232244.212908-1-boqun.feng@gmail.com> References: <20230201232244.212908-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows printing the inner data of `Arc` and its friends if the inner data implements `Display`. It's useful for logging and debugging purpose. Signed-off-by: Boqun Feng Reviewed-by: Bj=C3=B6rn Roy Baron Reviewed-by: Finn Behrens Reviewed-by: Gary Guo --- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 519a6ec43644..fc680a4a795c 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -22,6 +22,7 @@ use crate::{ }; use alloc::boxed::Box; use core::{ + fmt, marker::{PhantomData, Unsize}, mem::{ManuallyDrop, MaybeUninit}, ops::{Deref, DerefMut}, @@ -522,3 +523,15 @@ impl DerefMut for UniqueArc { unsafe { &mut self.inner.ptr.as_mut().data } } } + +impl fmt::Display for UniqueArc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.deref(), f) + } +} + +impl fmt::Display for Arc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.deref(), f) + } +} --=20 2.39.1 From nobody Fri Dec 19 04:18:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65C68C05027 for ; Wed, 1 Feb 2023 23:23:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231897AbjBAXXB (ORCPT ); Wed, 1 Feb 2023 18:23:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231921AbjBAXW5 (ORCPT ); Wed, 1 Feb 2023 18:22:57 -0500 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 000FC5BAF; Wed, 1 Feb 2023 15:22:55 -0800 (PST) Received: by mail-qt1-x833.google.com with SMTP id f10so219456qtv.1; Wed, 01 Feb 2023 15:22:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=kSFjnS2TzF6MjNVz0CrkTRdBZTFROoWEM1aHOLGjobA=; b=SwqscVMHJz5NcEj21qpRyEo1QIbrC+LPEotlDBBxSqoKopoc8AzZXPFRFhZe5U8iqj wCL4hHbdf45/O4cXuJ+klxnSwp/LLlLtY5IqX2JNhqLnJkBnr5BBIGWXUonK+kBiL/JT Ur0cJS5JPDr+rVZ6ZqmXJhcua1Ts57CPpyJYqYflbyOpqPU61IEl2ubn3zmy471D5iiu p+/LsRcdE9tKEFkD4VUZ5LW4tlM8QM7WFON4lJJFL84x56tQJ+rQnSbmBFeyo7vhhN/P aE4ipQyBqkY51MfK6KdoxWkkyfCNjcMn284BYBGk5bxC3ZCN7ULsIMV5NzJWUWa66Njh IpFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=kSFjnS2TzF6MjNVz0CrkTRdBZTFROoWEM1aHOLGjobA=; b=tT4kk90pOfGQdxftNyUV7wyZZLiLaeBOXpuq97F9AYNSe0o32SG7UDA9jp0WrHVcQa FJPHvxZYFT1+sehcvz3nYf/rU3+wewqcH35Zl/jdOtqU/BP6Y/894lqyQt39LGqqYA+F 0QwQtARwMxnVY9B6BA7kwW+WuZa0k4KqsfbsqQL6l5Kju65FYGf0nvmvjkHIPubb8Nde sqL6yyCGuMuDCfthPk3JL5ovSj98P/MD+OACJQ9SOp83IoVbTlORzNOBmY9lP1IWcFCb y5kSOLizUE6SXoXOqcmwOhHvSWZf5CWcwDd5uEUpPPobc8B6BIFeAxzVp6FfqPK5nOOu QSvQ== X-Gm-Message-State: AO0yUKV4ese1lD+1WrY12y3DLLRQuPMhVl+RA62bj0mWeDYeyzKrt1zu 1BMTahtqfuL7J6q9Q7+5hzM= X-Google-Smtp-Source: AK7set/4+AitiQHzdVQUCv4bLv3awO1NHV/wYA7hZ4FtS4ATErjNK3bqBiPZH68Xu5XpClclB1byig== X-Received: by 2002:ac8:5e4f:0:b0:3b9:b761:b0a0 with SMTP id i15-20020ac85e4f000000b003b9b761b0a0mr8546936qtx.12.1675293775646; Wed, 01 Feb 2023 15:22:55 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id v3-20020ac87283000000b003b62e9c82ebsm12661001qto.48.2023.02.01.15.22.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:22:54 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailauth.nyi.internal (Postfix) with ESMTP id 4043827C0054; Wed, 1 Feb 2023 18:22:54 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 01 Feb 2023 18:22:54 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudefjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueeh geeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Feb 2023 18:22:53 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo Subject: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count() Date: Wed, 1 Feb 2023 15:22:41 -0800 Message-Id: <20230201232244.212908-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201232244.212908-1-boqun.feng@gmail.com> References: <20230201232244.212908-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows reading the current count of a refcount in an `ArcInner`. Signed-off-by: Boqun Feng Reviewed-by: Gary Guo --- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 09a4d93f9d62..afc5f1a39fef 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) } EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); =20 +unsigned int rust_helper_refcount_read(refcount_t *r) +{ + return refcount_read(r); +} +EXPORT_SYMBOL_GPL(rust_helper_refcount_read); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` ty= pe * as the Rust `usize` type, so we can use it in contexts where Rust diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index fc680a4a795c..fbfceaa3096e 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -127,6 +127,15 @@ struct ArcInner { data: T, } =20 +impl ArcInner { + /// Returns the current reference count of [`ArcInner`]. + fn count(&self) -> u32 { + // SAFETY: `self.refcount.get()` is always a valid pointer, and `r= efcount_read()` is a + // normal atomic read (i.e. no data race) only requiring on the ad= dress is valid. + unsafe { bindings::refcount_read(self.refcount.get()) } + } +} + // This is to allow [`Arc`] (and variants) to be used as the type of `self= `. impl core::ops::Receiver for Arc {} =20 --=20 2.39.1 From nobody Fri Dec 19 04:18:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B35CC636D3 for ; Wed, 1 Feb 2023 23:23:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229616AbjBAXXG (ORCPT ); Wed, 1 Feb 2023 18:23:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbjBAXXA (ORCPT ); Wed, 1 Feb 2023 18:23:00 -0500 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF2644680; Wed, 1 Feb 2023 15:22:57 -0800 (PST) Received: by mail-qt1-x829.google.com with SMTP id z5so179120qtn.8; Wed, 01 Feb 2023 15:22:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=aHPo6+AwM9yD1jStn/Ea4v3g6F0MxEwpwB3GOsSYmsQ=; b=coBzvkNWO16ubnVJFhsQLkJKuN9MBdDiq1fLJ4nGhdNLQYX4cfG7oGiORemm8jizjt y9c5k39cFqghMA3rXgtIEklnCshDDJaUHGbpWe3yjw+RTyLGWEcF3ij5SA6x9lcjniv4 hDfkWY4nLrSeiFG5heMYed0UPizPfTgOSL3clXMb6TVBokvO0ieKTCaax3+yXVOdwXZv GiHvO218gYxLcpqjr0W80Lr6EFJVrY/wJnO/HaUgzX9EWip2nUHdCygRoMdARpAZesfG KPPsx1Z/OMIKJnTfVTK0Y3QCnKgu6hRiS55NZSXYe5WKpXiIjNpyLCEgcJBNr07pmFPO +nuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=aHPo6+AwM9yD1jStn/Ea4v3g6F0MxEwpwB3GOsSYmsQ=; b=grsjgInOxnj18VJcmIOHTAJ919hOw8kIz/8848JXoFNIlu61gpjkan4pZGEs4kdeRc pOUBAym3r3o19hkQzNHYsbwGWZaE4wdjPYZt3gMVAEsFP+g5v48CUx7icwA5OK08sA+1 bq//qVXdhPcAf2Q/FJozgk3woxx79AsfUBk96X82hdYJHwK3AIeHQNULY7x+hiDwD78Q 7eLZiRquc8shE86YWnP5xxnfzBwynsklgPBHEr1ZVPhAMg06YDFEWC1MZaemLtiE+oUy sIKD0Q20fkB4Hj5sfzYOEaKbqEqHLr2iZJj5hlPL0wgDyrrHFQuqi9jXw0OLE1ubt+GT LS2g== X-Gm-Message-State: AO0yUKU2IrrT24FfTOC6YtfVTEw70y+s9+y8ccMjQPkGZgu0NchaZZyN hlFuXEST/4IIfp87TRVbGYc= X-Google-Smtp-Source: AK7set8e2QsqCZCuq1Et5y4/QaCWYeoawUWEscDSM7aOiQ8oEljU9DDNG4UnIrlXdysT+8gZHz+qKg== X-Received: by 2002:ac8:4e54:0:b0:3b9:bd1f:1de6 with SMTP id e20-20020ac84e54000000b003b9bd1f1de6mr4876552qtw.40.1675293777095; Wed, 01 Feb 2023 15:22:57 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id eq14-20020a05622a5e0e00b003b9b8ae2c8dsm2252376qtb.93.2023.02.01.15.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:22:56 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id B047327C005A; Wed, 1 Feb 2023 18:22:55 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 01 Feb 2023 18:22:55 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudefjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehfedvtdekjedtleffkefgtdehueegfeffkeeggffhjefhudeikeejgeeh tedvfeenucffohhmrghinhepphhtrhdrrghsnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhs ohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnh hgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Feb 2023 18:22:55 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo Subject: [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper Date: Wed, 1 Feb 2023 15:22:42 -0800 Message-Id: <20230201232244.212908-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201232244.212908-1-boqun.feng@gmail.com> References: <20230201232244.212908-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Getting a `&ArcInner` should always be safe from a `Arc`, therefore add this helper function to avoid duplicate unsafe `ptr.as_ref()`. Signed-off-by: Boqun Feng Reviewed-by: Bj=C3=B6rn Roy Baron Reviewed-by: Gary Guo --- rust/kernel/sync/arc.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index fbfceaa3096e..4d8de20c996f 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -201,6 +201,13 @@ impl Arc { // reference can be created. unsafe { ArcBorrow::new(self.ptr) } } + + /// Returns a reference to the internal [`ArcInner`]. + fn get_inner(&self) -> &ArcInner { + // SAFETY: By the type invariant, there is necessarily a reference= to the object, so it is + // safe to dereference it. + unsafe { self.ptr.as_ref() } + } } =20 impl ForeignOwnable for Arc { @@ -233,9 +240,7 @@ impl Deref for Arc { type Target =3D T; =20 fn deref(&self) -> &Self::Target { - // SAFETY: By the type invariant, there is necessarily a reference= to the object, so it is - // safe to dereference it. - unsafe { &self.ptr.as_ref().data } + &self.get_inner().data } } =20 @@ -244,7 +249,7 @@ impl Clone for Arc { // INVARIANT: C `refcount_inc` saturates the refcount, so it canno= t overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference= to the object, so it is // safe to increment the refcount. - unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) = }; + unsafe { bindings::refcount_inc(self.get_inner().refcount.get()) }; =20 // SAFETY: We just incremented the refcount. This increment is now= owned by the new `Arc`. unsafe { Self::from_inner(self.ptr) } @@ -253,11 +258,7 @@ impl Clone for Arc { =20 impl Drop for Arc { fn drop(&mut self) { - // SAFETY: By the type invariant, there is necessarily a reference= to the object. We cannot - // touch `refcount` after it's decremented to a non-zero value bec= ause another thread/CPU - // may concurrently decrement it to zero and free it. It is ok to = have a raw pointer to - // freed/invalid memory as long as it is never dereferenced. - let refcount =3D unsafe { self.ptr.as_ref() }.refcount.get(); + let refcount =3D self.get_inner().refcount.get(); =20 // INVARIANT: If the refcount reaches zero, there are no other ins= tances of `Arc`, and // this instance is being dropped, so the broken invariant is not = observable. --=20 2.39.1 From nobody Fri Dec 19 04:18:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64849C63797 for ; Wed, 1 Feb 2023 23:23:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231520AbjBAXXJ (ORCPT ); Wed, 1 Feb 2023 18:23:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231970AbjBAXXA (ORCPT ); Wed, 1 Feb 2023 18:23:00 -0500 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AA9510E8; Wed, 1 Feb 2023 15:22:59 -0800 (PST) Received: by mail-qt1-x82d.google.com with SMTP id bb40so210851qtb.2; Wed, 01 Feb 2023 15:22:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=RJ2eqJLEVXk0Nl1is9sUWLnCG7hj77zSsF65ucYjqnM=; b=i4yh+W+BrM8RY8XP1P/LnT/bYfNWNsm8jzEkujYQ5E28NinCeb/zJ33eelJMgMCCja zoXTfnMmpxYc2/lU8Iv2RVB1Xc3XegWyc+QtaOaVyKNbLwPkWKiO3ga41LQbfKh/zhy5 cYLSKjDhX1fgFTcGU5RSnCvlDfPgd/UO+P8KW0Z/S4lnnGXLmDV7EeckFig4+SWyBbwa pMRa7j3i1/fwL0wRwbdyA7DzNp6KVGoGoALEgLhEtuj1Iu1C8tSWTmhGyVO9/SW387w6 EEI/dvZnSsmgJZZJBK6wsx0A5p8lpEdGjT7OpwVMtElTIkQuwH6cXAU7YCe/ERlXv6WQ rO3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=RJ2eqJLEVXk0Nl1is9sUWLnCG7hj77zSsF65ucYjqnM=; b=xe5kuZKzcWYBGhDr4gjDJcdMUBnA2nl0RmcF4AvKnKcW3sBSaG30Bc3cHSoa0Wu1j1 tcbnvmly6Q1qOvV9UxXgyzqXrXuG/G10uG8xzo2q7n5m3BNL/5Rakq3t5nfVRN6if5dQ +28qR5w4abUdy4vqIRVLWhlmoSiyariZyz61SgidaUqFHJ7BBbJ6U130ngC2CigTyXzC du2PGTSRQH4keJRjR2TOE79b0GP9AbUVP6usjeDqNM0pEyDeX+eWrejgOoohOw50oh4e tTflRqAWjsmVSroVhyipzwCjkak/iNP90A4BPn4HNHBPrUOO6Dad7RXWhbwWKp/LcGuL iC1A== X-Gm-Message-State: AO0yUKVhglYOGHIZPXPrjdVMXGUOaM41jVIDtL5+a3Al9tREKPb7HM+y 0F2SFVqrEET5Ue6DnNrBLn21rXW2vCk= X-Google-Smtp-Source: AK7set8MH15M8ebrTvc95LvO8zsJP+EmB0ENiE3zoR7Td84Jayz3huuk8xDs0cpChxcOfs/NubPEsQ== X-Received: by 2002:a05:622a:152:b0:3b8:6bc1:155b with SMTP id v18-20020a05622a015200b003b86bc1155bmr8312505qtw.45.1675293778224; Wed, 01 Feb 2023 15:22:58 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id o2-20020a375a02000000b007055dce4cecsm13076809qkb.97.2023.02.01.15.22.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:22:57 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailauth.nyi.internal (Postfix) with ESMTP id 1383A27C0054; Wed, 1 Feb 2023 18:22:57 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 01 Feb 2023 18:22:57 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudefjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueeh geeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Feb 2023 18:22:56 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo Subject: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc Date: Wed, 1 Feb 2023 15:22:43 -0800 Message-Id: <20230201232244.212908-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201232244.212908-1-boqun.feng@gmail.com> References: <20230201232244.212908-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows printing macros to print the data and the refcount nubmer of these struct for debugging purposes. Signed-off-by: Boqun Feng --- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 4d8de20c996f..f143d8305c36 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -474,6 +474,7 @@ impl Deref for ArcBorrow<'_, T> { /// /// # test().unwrap(); /// ``` +#[derive(Debug)] pub struct UniqueArc { inner: Arc, } @@ -545,3 +546,15 @@ impl fmt::Display for Arc= { fmt::Display::fmt(self.deref(), f) } } + +impl fmt::Debug for Arc { + /// Formats debug output for [`Arc`]. + /// + /// Refcount is also printed for debugging purpose. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Arc") + .field("refcount", &self.get_inner().count()) + .field("data", &self.deref()) + .finish() + } +} --=20 2.39.1 From nobody Fri Dec 19 04:18:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F12EDC05027 for ; Wed, 1 Feb 2023 23:23:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231463AbjBAXXM (ORCPT ); Wed, 1 Feb 2023 18:23:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232001AbjBAXXC (ORCPT ); Wed, 1 Feb 2023 18:23:02 -0500 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A927A264; Wed, 1 Feb 2023 15:23:00 -0800 (PST) Received: by mail-qt1-x830.google.com with SMTP id m12so201226qth.4; Wed, 01 Feb 2023 15:23:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=WRf7FsW0svSQ5uvgmd0AHIX9qix6pe5dIuAX0+vgnjk=; b=AaGH4xIRJHiGzFMV++RFKatLGmRKIlREolVmZNjG58XdbhhZh+dfTsMyJYEO+70aem tBqEufwqM5laAYG07ZQPu8n6RGmg+qrsVt5Zqo6b3ISlGXUwbys8dTWui33h1xrvKKA5 n1CihKRhSpTe+48nBn480C5vwmB2HicalOCh4HRM7mt6aqy+/iCOMgYHhHUIr5HHjlb+ RTvpnbVpRzvWisvXms2l3y4e2SiNqRVZuutfDxnXhU20zU+YdCNjwQK+lRq6PAmCYORR GUZoSPcR+/cRSEgsWhh9yJQuWrTv2e0f9ucLgKuoOFB0FpBtRudXBfgQgpQfmUXiFefQ 5bBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=WRf7FsW0svSQ5uvgmd0AHIX9qix6pe5dIuAX0+vgnjk=; b=U37R2xk4+D4anO1F0nsjAloxJMblkiq7mMafhdrkrMXPRp67OS6Cx4Z8iRn1u3d1v/ C1JxTnnHxqBHge2pcXJaYUHu73iCYL/LHhaIIXQHR2o2+ZpD4YeepVVRnraG4HO1ZKK8 y8XTmYiYNl/a5Yssk0rVk7w5Aycf3qPMTb/Gvrot9qY/qn5WpAU9jTqLvR3EQYoUCB8Z btVvkCRYCZzUd77VsM3IAxtDUl4VYLOqIzcbBlAADbOqSdu1b76LGa9tY15pFWrZtE7L 3I3S0ECWgUj3VU3H51S/Xj/fgjOWfN/o9KGH3MG7KkXo40rJhrxCfJlfCwCWCcPtscr6 Y2WA== X-Gm-Message-State: AO0yUKXTNLQM17qKt4uRunEa3KHfc/k4efigedRgZTSKtg2lJkHBENFd EK2FRHa8Y7uJzoM2jpxu80U= X-Google-Smtp-Source: AK7set8a4O590xcb1r76E6BdTkcepn8WXa7kzVn5DCamhrt0fB+pXN3hlNimrLvuOLFWYKwV+G0Z+Q== X-Received: by 2002:ac8:5886:0:b0:3b9:b297:5ec5 with SMTP id t6-20020ac85886000000b003b9b2975ec5mr7535406qta.13.1675293779687; Wed, 01 Feb 2023 15:22:59 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id s39-20020a05622a1aa700b003a7e38055c9sm12735406qtc.63.2023.02.01.15.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:22:58 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailauth.nyi.internal (Postfix) with ESMTP id 71F4527C0054; Wed, 1 Feb 2023 18:22:58 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 01 Feb 2023 18:22:58 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudefjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueeh geeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Feb 2023 18:22:57 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo Subject: [RFC 5/5] sample: rust: print: Add sampe code for Arc printing Date: Wed, 1 Feb 2023 15:22:44 -0800 Message-Id: <20230201232244.212908-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201232244.212908-1-boqun.feng@gmail.com> References: <20230201232244.212908-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This both demonstrates the usage of different print format in Rust and serves as a selftest for the `Display` and `Debug` implementation of `Arc` and its friends. Signed-off-by: Boqun Feng Reviewed-by: Bj=C3=B6rn Roy Baron Reviewed-by: Finn Behrens --- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index 8b39d9cef6d1..165a8d7b1c07 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -15,6 +15,30 @@ module! { =20 struct RustPrint; =20 +fn arc_print() -> Result { + use kernel::sync::*; + + let a =3D Arc::try_new(1)?; + let b =3D UniqueArc::try_new("hello, world")?; + + // Prints the value of data in `a`. + pr_info!("{}", a); + + // Uses ":?" to print debug fmt of `b`. + pr_info!("{:?}", b); + + let a: Arc<&str> =3D b.into(); + let c =3D a.clone(); + + // Uses `dbg` to print, will move `c`. + dbg!(c); + + // Prints debug fmt with pretty-print "#" and number-in-hex "x". + pr_info!("{:#x?}", a); + + Ok(()) +} + impl kernel::Module for RustPrint { fn init(_module: &'static ThisModule) -> Result { pr_info!("Rust printing macros sample (init)\n"); @@ -43,6 +67,8 @@ impl kernel::Module for RustPrint { pr_cont!(" is {}", "continued"); pr_cont!(" with {}\n", "args"); =20 + arc_print()?; + Ok(RustPrint) } } --=20 2.39.1