From patchwork Wed Feb 1 23:22:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 51607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp568087wrn; Wed, 1 Feb 2023 15:30:16 -0800 (PST) X-Google-Smtp-Source: AK7set9jJrtBRgios0wrN3CnGAgfg6v2JmQ0MGJDCAggAiwW5FMWrHxP9GasEd44UnMSiwjK5H6V X-Received: by 2002:aa7:c74a:0:b0:4a2:5c3f:133d with SMTP id c10-20020aa7c74a000000b004a25c3f133dmr4673379eds.3.1675294215877; Wed, 01 Feb 2023 15:30:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675294215; cv=none; d=google.com; s=arc-20160816; b=uQzrAfQaPuF/tNnxK7UYTiKSxvp1wdLAOkoK0fZFJJAOrzCYNtqF1AkYLHaOGSsRVC EaAHhoRvJxeWpptLH62uJfplAficR1uWRGjcaLDXZPiyjtZhyn9boeMzlwGlVewv8IP/ 5LzwCzZNlNWgSlZBynCTT1yrkahQDR3ozIPFbUKavdA9Nm5i+lA5MJX9SgjLkrM4r87D kaETIVlOMwEFdfJTvWW9lX3T4oh5LQQ8XkKXPA2nmtDIhvR01EgsYY9CBI5NRq6+NQZx aHeKFlkcSD9p0CRjzAVsX6Px7Rkrb9QLEZSJniFYtRx3atNc3syGj2J7wqLym8Au/IsG 5yMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature; bh=xDOpfFKpqkig/ifJO3DdK44tzsI39GfXAo3DZPsupvI=; b=jXzv/gcZrAvFZHYLBYALeBTimgshSoVZSC9QwoYfzrGllv5Q3ePRRoHwfT+x10xpZ9 t+As3eA96UoVqBgZ0qbSdXdgTJMjRmN6iq/fy1TWn2IM+JUgDTi/witHNiivIuCysEaa Xg/t1zikyiihFQm0py+mu7iSzmIfXM6qL0v+wLNAXxwbMk+8uUm5MclFc3cKZ0NxduSB kwjQrzNxrHn2NaOwogK7wSdX+SKmFK6eLZb2S4vVF2yLTxF9dMC5xR7XHVdc3r3ATDdK m6PhgVg8d1v8RzCmDeD1sR9fYIXn6AgsreXD3MIRMhsLB20wqeBP4A8iyT5BikdhxWR+ yWCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SWIFInt+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id em5-20020a056402364500b004a0916f3e12si26576774edb.218.2023.02.01.15.29.51; Wed, 01 Feb 2023 15:30:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SWIFInt+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229935AbjBAXW6 (ORCPT + 99 others); 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-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_Roy_Baron?= , 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756673307563130008?= X-GMAIL-MSGID: =?utf-8?q?1756673307563130008?= 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: Gary Guo Reviewed-by: Björn Roy Baron Reviewed-by: Finn Behrens --- 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) + } +} From patchwork Wed Feb 1 23:22:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 51609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp568386wrn; Wed, 1 Feb 2023 15:30:55 -0800 (PST) X-Google-Smtp-Source: AK7set8Ij8+HepSTnICv+yRiZ2zUKg8PQyFRFpEEXud4epYmTAiFG7gBqKHMXPjJLp0LduQkCspf X-Received: by 2002:a17:907:2ce4:b0:888:6294:a1fa with SMTP id hz4-20020a1709072ce400b008886294a1famr4387346ejc.14.1675294255743; Wed, 01 Feb 2023 15:30:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675294255; cv=none; d=google.com; s=arc-20160816; b=flyfuVAmZ5pWWBF4AcY/0CzMANFTJ79iUGUvsYZZfisCOC3UHyFH7BP3y4OZhp05AJ gwSws0TPhpREy37OaoiogozgDKFKLmoEIepQ/I6039rLwdx9tKuxgEpjdTaJhoCGa3L6 Nzf+eofBxAAWSrxu7ecye1TzK3UyRTYyEEMo6u8uZ/a4ZSuHflpJOhYChon3pIiP0WuU q1eXcJt9TjF8A/LZn6Bv70YreV1lXnBFkgBM61LZzI4uZ52S9gQG5PD5k5Tm5Vot6NOc Zq/HNjTvj4W17ic1Pq0t/bbm4i889+1J0IXniCiXS2FVXX5hqFEUktzLLvY1Tgxx9LIo 1Ghw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature; bh=kSFjnS2TzF6MjNVz0CrkTRdBZTFROoWEM1aHOLGjobA=; b=ZyhlXIeInjWNIp/wCxCW9cns16SGrcxG27RaoopPZaswIskzRYeXMXgDrldyV1KdRs yuHa6qMfghq8kzcv2KGzPa1Te75LUyQLU7Hhi9PW6bUq2uM27zA9mehs1nbsshYiIyCR Xs6U7QaMvPF7SU+vC3Znv8laXDIiRVxI/+EVqGKdtw49sX2aoXfnKrfhDUvuZGCVUVy/ 31/PemopkTnOUkzUr9SxjghtWOg6GV5VYBXvRefjQ/LxU1Prpf1P+u3F2WToLkicZWiS 6aqCfpvzUXFxMC+S6V44G/m6pcISXe93dJcCaKU1b0/vFSW9+wyIS8l0ecgugVDrT+KU mfeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SwqscVMH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 9-20020a170906020900b0088a161c2342si11015731ejd.192.2023.02.01.15.30.25; Wed, 01 Feb 2023 15:30:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SwqscVMH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231977AbjBAXXB (ORCPT + 99 others); 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-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_Roy_Baron?= , 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756673349674253714?= X-GMAIL-MSGID: =?utf-8?q?1756673349674253714?= 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); +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` type * 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, } +impl ArcInner { + /// Returns the current reference count of [`ArcInner`]. + fn count(&self) -> u32 { + // SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a + // normal atomic read (i.e. no data race) only requiring on the address 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 {} From patchwork Wed Feb 1 23:22:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 51608 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp568387wrn; Wed, 1 Feb 2023 15:30:55 -0800 (PST) X-Google-Smtp-Source: AK7set/tA5PrXfYzMUVKsln7/Ap4Uc0EnBF5dlv0AGZiPyBpZrMj08I02zlkXf4mxEeQ+MI/wy5a X-Received: by 2002:aa7:c552:0:b0:4a0:8bcc:3cbc with SMTP id s18-20020aa7c552000000b004a08bcc3cbcmr4107551edr.25.1675294255742; Wed, 01 Feb 2023 15:30:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675294255; cv=none; d=google.com; s=arc-20160816; b=ktp8jAifLCT4CxLmFDTy+4vYHm8G0SboB6o/BP6/KDgRfFvcJsW1udW1i4nAEvBeib +mxSX1grFQK+PvkUk5dmStLUt/C8sDR7IfLJwljj6i9H3fMRBCLT/wRGirh/rkGKI2p/ 2cf/fwmQDU+hud3jHJ+n5R7SdaHoFjxdCify+kVyPsXbJ7L5SkRGcfjXdvbXqXLAg8EM J37UmWoer9K7vyfFCXO6xXOTSO09/jofNicGBD4CyhE/4iY7zesvt028blwOFuysvxid o+hHFwK6F94s6EiG4Fb2FoO8pS5uWBHmN7kSAm/al9zn1aAvPbvVUA0tFJwOquxlC4cZ KgmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature; bh=aHPo6+AwM9yD1jStn/Ea4v3g6F0MxEwpwB3GOsSYmsQ=; b=mxpQyktY8uHzsdVkz3DsJrcb63uRm0XlidjaNz/O39Yg74IfAyTwHUBjWJSH8xbTDF TH1IEa/vZZRCZU/N0vdQqNE0hjOCyhMaJpnZSBuRLFKshcVTay71iVxIdZG1g3QKCzIT JlG0xTe/RqmL3U6KFZVe0RqGE9rjAGZzFp4jEjcFynVoFXmXtGk99pndAdG1T729MX8F NxWNVEE4yyQ3Ayr8rdNIZL/lWigrs0CHtUGPNv5Exh+XfMswLD8cGxcv5Rj8ClWOK/Vy 1GJ2Gv10PsMVMKcCsxLS0Eb9LR7SBdbqpAm3QbX2mKrfBXXilF+EG4NPkmkFiDhWi+qN 0YRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=coBzvkNW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j25-20020a50d019000000b004a24ed3c147si10531707edf.438.2023.02.01.15.30.26; Wed, 01 Feb 2023 15:30:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=coBzvkNW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232007AbjBAXXE (ORCPT + 99 others); Wed, 1 Feb 2023 18:23:04 -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-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_Roy_Baron?= , 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756673349911445417?= X-GMAIL-MSGID: =?utf-8?q?1756673349911445417?= 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: Gary Guo Reviewed-by: Björn Roy Baron --- 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() } + } } impl ForeignOwnable for Arc { @@ -233,9 +240,7 @@ impl Deref for Arc { type Target = T; 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 } } @@ -244,7 +249,7 @@ impl Clone for Arc { // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot 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()) }; // 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 { 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 because 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 = unsafe { self.ptr.as_ref() }.refcount.get(); + let refcount = self.get_inner().refcount.get(); // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and // this instance is being dropped, so the broken invariant is not observable. From patchwork Wed Feb 1 23:22:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 51610 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp569364wrn; Wed, 1 Feb 2023 15:33:18 -0800 (PST) X-Google-Smtp-Source: AK7set+C1HDudPxivnjCk8QFILY50pHaS7EcjzbL7Kbmj7lboULAiVXU4KpLu+eXLObT4SSgsLIl X-Received: by 2002:a17:907:3e82:b0:878:5bce:291a with SMTP id hs2-20020a1709073e8200b008785bce291amr6161552ejc.36.1675294398572; Wed, 01 Feb 2023 15:33:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675294398; cv=none; d=google.com; s=arc-20160816; b=naPkte0e+RKQDsk+uXWH2ZzOKU/3v+Zm44ROOpmKoZO52VF+OOX4rqiUfajI+eJg1s z2U1rWVmAjZJsJa7NQCzU7/JGZraJ+dQ9vufzWJIxTptjnuyYfnzSMfBC3MPY845NfAL sgWXBKeK4xyvZD5HQrazIM/zMQrMEvf4ZhiW3UgCQ6flNdxTvV5r11/x3oR2xf2qLUFZ 7ne2AWYCc0sdXRfgvfY8MuYbWBuUdo9HROqXDVg9VWK5Y1NQ0ODWGRME3etrEJx6fSqQ EylLVlgfcSzAhhP9Vipy8cbGSoswsm0w7c/TC4KLRntRN93pxvqcNKMjgtZdk1cVwgso vq0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature; bh=RJ2eqJLEVXk0Nl1is9sUWLnCG7hj77zSsF65ucYjqnM=; b=IOLuRsoZcLuHT0C4KJYfj7KIdCIsf8Jw+x2IWkUV6YHvIbiVr2E6ScEizXHE2MUvzo i24S2546FPsNBPtlKL/ybTgroX/nJ+tUYoT19Fibat0sQtLnpC8a9q/vhCTnY5Taccnr mRzk8v62mSCNrYpflJbThBGwoC0qz2baAb/5WM6Qe5zps2e6sVh5Ygc/dMp7x1Dp27n+ glOTvVzyBFgwWi+3tpWCEAnxmxKDyoxwruhrnXIGwDhG33uBMaaa8Hd1mG+JlrCSDXAX 6qCznEAKDkrQ51eqHX9eiBWUzRzDAfFBg4zs4q5gW7cMcmMqEUVYY61VLa7Ushqhp2aI +WUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=i4yh+W+B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y14-20020a17090668ce00b008718874c66asi26115098ejr.626.2023.02.01.15.32.54; Wed, 01 Feb 2023 15:33:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=i4yh+W+B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231661AbjBAXXJ (ORCPT + 99 others); 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-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_Roy_Baron?= , 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756673499655871927?= X-GMAIL-MSGID: =?utf-8?q?1756673499655871927?= 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() + } +} From patchwork Wed Feb 1 23:22:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 51611 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp569404wrn; Wed, 1 Feb 2023 15:33:29 -0800 (PST) X-Google-Smtp-Source: AK7set8WXgjsHfMsNCqsiaCGjMohrLuGIzu7pJhSi4zVx9uQkfRRP7h4W0hn9dcpViv5zMRg0VtV X-Received: by 2002:aa7:d98d:0:b0:4a2:1b8c:5b58 with SMTP id u13-20020aa7d98d000000b004a21b8c5b58mr4329196eds.34.1675294409098; Wed, 01 Feb 2023 15:33:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675294409; cv=none; d=google.com; s=arc-20160816; b=TiHoi0feV35751cB08ibUTTstjhGkb7dC2KcZqbxTkPmdMf1NmRI82aLtxsGr/hLhY LaSTsk7sOPiLMEJOXaAMGBy+2+nrTXq7VqvSlUYfzCBkhgw6feshgSykdDiZoVAJ20te eGvvDhr75ES+GcuQDkLQAz03YCqjCsusJ6KSvMeLYN7rvFchhSB1YunZ0M5QWqR/c3+N o8+SkfIpmeChf/UIPuh8tXaJsU88Ky/CAc0EsWz/Ap+0W+I+ei5se4gXK5ilglJXgclN N8WcKfOj5RABKqLOF9E0mNLJ8JHZ1QMOZQ9PmXdLF8hpKBcedU374PbMF2QClqUL3yyA 7DYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature; bh=WRf7FsW0svSQ5uvgmd0AHIX9qix6pe5dIuAX0+vgnjk=; b=O8SzVBrRMxlPyZyFgmYMNtSs5HKawHxd/KZy/GR1yxN3AR5pOnm4czPIUjIaHT2J3J JqbdTy9cAGrudimx2mbG6XYlMZp5XF/hKqgTbR8gFmPgRB9y+1VTvrJKb0cX6s8vbDmm 71dLuAdqvuAXfRPNlfjX3gT1ogHceuM+25Uws06KH3b7B4Szxxz8acBUZUU/u4JQLRal u65DvIbcgV7w+egw5bmfVl6a7ES2sFfftzQBrv7WyOUf6CtYf7Vn85p7qlSurvwPhkxr yy89lywI9nP8gTMd+06VlQAyfvFV1J+TSdCQfk2Vdx/UJbR6F1Sigl16bkR2hWPfFCKC kxLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AaGH4xIR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k12-20020a50ce4c000000b0049e1bc53bd3si28297309edj.486.2023.02.01.15.33.04; Wed, 01 Feb 2023 15:33:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AaGH4xIR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230226AbjBAXXL (ORCPT + 99 others); Wed, 1 Feb 2023 18:23:11 -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-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_Roy_Baron?= , 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756673510187186808?= X-GMAIL-MSGID: =?utf-8?q?1756673510187186808?= 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örn 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! { struct RustPrint; +fn arc_print() -> Result { + use kernel::sync::*; + + let a = Arc::try_new(1)?; + let b = 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> = b.into(); + let c = 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"); + arc_print()?; + Ok(RustPrint) } }