Received: by 2002:ab2:715a:0:b0:1fd:c064:50c with SMTP id l26csp30912lqm; Mon, 10 Jun 2024 11:40:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVrI6FRM2czRDHUacLz5E2B8DdyM2EdSFf8SYrgAOqPd2bfYgJlpl4FGixgRKkJHhb4jzthJUoRmNrknN3rWqdGz6sJDRqWZX4O29u04w== X-Google-Smtp-Source: AGHT+IFvvCii+Dj1U2veMisxzsZbRvWttCof9u1luffVYylrQTstsHbZHrLRUOPEYSlqgeIlaTWc X-Received: by 2002:a05:620a:46a5:b0:795:be11:c626 with SMTP id af79cd13be357-797c2dd4bffmr97315285a.26.1718044845041; Mon, 10 Jun 2024 11:40:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718044845; cv=pass; d=google.com; s=arc-20160816; b=MoGycbEPMD+wBcpZVU9s/eh4CIgtcvA07ajGZkZnlWsZUiVHjRQCp1f6WwVdOzcvEm PbWb4ZZ+GlQrkam0Ykwztqf3E2CgXxLnz0FffZ6v1ce72nNvSMDn68Mcb4UeSoPsDpT3 D4g8lpEbQBp/GjVEiiz82JcRbPJs+RDNJaFcDNv8aAJCKMfE3sakk583GP9gIbrsVy/W VL9sMeRJIeyRqfGN70D+77SE9WVK8697ppNrwzPwND8K8oyTIrAs2wzG1uMbVqe7v0Wm Zrxss6KQOsma83RmBBUJQYb5iLmDQdMtJ6bTSnTnQm4KKeu6ZolRTQY8rIy5eSwe7ZI9 K0vw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=OywUQ9KGRGgplnGy/1tiQUpY5rZ67qOaYLzLaaTA3N0=; fh=r9QPZHmqdjviGre1LMIqrSWENfSRIuvZcRrpmUMhVLk=; b=v6f0saDnn3exjpE2CEe9O6DXIXNXZm/f/mRU/mpcqfYT3sSVxCilxWQ1D9PtF46myE 2wuYJPL4ufoowpMQ6BihY3+7LIB6sEntrNzRNAcDncHnntVuBTHdP4UirZdVwd5Md0QC GTkFJHqC5pU74RfgmNs3Ovr4eX/Ve+CaTVMtvo0ETFwYlWwtWLNyKdpYSggN+iMLKJCq vypU53T04zwKUxQDsqUW3wh08S2KbUfNra24W34KrPnomFiRCAnG85bcxuTL62wqUJ0Z eBx94Uvayij607JBML/QZgSXfqmoofS2UT0O5QVeaWkbIb+ZrB1Fgemtt+Rq2xxcp/gE EdaA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SBTj2nsi; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-208761-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208761-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-7954e814487si722490385a.495.2024.06.10.11.40.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 11:40:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208761-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SBTj2nsi; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-208761-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208761-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B37841C22525 for ; Mon, 10 Jun 2024 18:40:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33BD714F9E0; Mon, 10 Jun 2024 18:38:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SBTj2nsi" Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (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 98E29142E79; Mon, 10 Jun 2024 18:38:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718044714; cv=none; b=kR9fQFdPkD4lBoBRWCE3zHgivNojIoiehPUxKDb48ykoATfao5Sgo0ZraQJeHHl75OJtM5GeWSieaop5D7pmz1lFDYf6wtRPGBUp2MPmBVkQr7QqXBbMuIvlJ8RX4uOx+xQuhlmYWlZVmEPro457COtxFEqxOKVtcc7BGaPSNQE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718044714; c=relaxed/simple; bh=ejGHWAb9GefhywC3TAx8q6a9ruEZKc6g1Y+QeyzGYzE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dGI7sIfsjo2K/PSAVQmNC0PKTb5/69GplKqoKH+owO36jJEgI8K+bdtdfwzGipjUIzdS4eTS+c4g1z7HDMOxHWYq0BAr++WZU/ACF4uNZLitbIdigwuR1ucPlb6LLYWMWuXqAYmmkC6l1jEmUK59BGMFGYWVSIxAp+5ocKal8JQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SBTj2nsi; arc=none smtp.client-ip=209.85.219.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6af6c2bee7bso40652616d6.1; Mon, 10 Jun 2024 11:38:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718044711; x=1718649511; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=OywUQ9KGRGgplnGy/1tiQUpY5rZ67qOaYLzLaaTA3N0=; b=SBTj2nsi+HAY1prdDL8+ohDur879oF5zQqNM3I3/5VZop22vEaW2xMfxKQl93VtRh8 L0sGnWMjkfwMcG+2Cti1oSYz1HRrlvu+h/k8UdVP00SZOcRL+bSh5UaUSrdkFZKhOjwL zf05saBphpVl1OIHMR+c+kDs4sFvksn/iMlZnSH51le/KD5B6y4RRfmL+SwzRLSQPTqg 9ml6RlixA8Kx3Y0GmAspizBbxk4W0XeNrPHnvKhEI+XWsTZeNGAJxgccA9n2SGFxEWS2 N6PbWr2DoWW501Ohbpp6+u5j19ltr6NNGDg/L0CsZSgt1bOv4aFQ+6DZmRUzXTWd1PK9 nrUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718044711; x=1718649511; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OywUQ9KGRGgplnGy/1tiQUpY5rZ67qOaYLzLaaTA3N0=; b=ZVCP6kiNRaBcnV+oSz2C6R7xlz7y1PyPZ88cR+b9xffet6omEAs2V5Mul0qzyN/atY Zh+8MKIbsRwxVMBZuMKvVyPfdRRllSrBZn4mojAUg7rGXdGnnl9MJI+rCJUXRqBJWf0x HeDGqGiB6BGMr6WX+Xg74HrL9glPT8OTVJPLWif2Wj+GH+WlhTHDxt3RLGcj+NCykvsv oevj7KgNkcv/eaG4JwWrMls0Ksh8Ap5rFkFKBtXlcguzqpAsNobnps6EbM+pqH8q42dX HgsZa9soM0iGzMYQpthgT+/BYeIPYxjiFJJvoTLnSClI8pninX7EAfoyNvoiRNU0PeOn hhKw== X-Forwarded-Encrypted: i=1; AJvYcCX3bxVD8qdWc65298TiXL4DHxTdOfteBe0CulC3Pv71z5THM6tpsXvbxcFt1QfeEWf53fgu4B/d6awXV7wg1q41dkjTxzLMXkcUN7G5/T5uG3ZydGjA+GkY47OfnUbBncwUHOUxh3Ueq1VJR54= X-Gm-Message-State: AOJu0Yx0WpgvTrmVSE3FS1F7rNr/4cOrQ3uvD6synT1yP2KZ3HrYskyM xN9TlC9YvdEMV7XPcwk/g85u1gQuREmQjbF3kxdF8KnJDihInk0Z X-Received: by 2002:a05:6214:21ef:b0:6b0:5e5d:8d5 with SMTP id 6a1803df08f44-6b089ebd1c2mr8691706d6.8.1718044711332; Mon, 10 Jun 2024 11:38:31 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b064df1b70sm33675046d6.128.2024.06.10.11.38.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 11:38:30 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfauth.nyi.internal (Postfix) with ESMTP id 480641200043; Mon, 10 Jun 2024 14:38:29 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 10 Jun 2024 14:38:29 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedutddguddviecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtugfgjgesthekredttddtjeenucfhrhhomhepueho qhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtf frrghtthgvrhhnpeevgffhueevkedutefgveduuedujeefledthffgheegkeekiefgudek hffggeelfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeeh tdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmse hfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 10 Jun 2024 14:38:28 -0400 (EDT) Date: Mon, 10 Jun 2024 11:38:07 -0700 From: Boqun Feng To: Danilo Krummrich Cc: gregkh@linuxfoundation.org, rafael@kernel.org, mcgrof@kernel.org, russell.h.weight@intel.com, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, airlied@gmail.com, fujita.tomonori@gmail.com, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] rust: add abstraction for struct device Message-ID: References: <20240610180318.72152-1-dakr@redhat.com> <20240610180318.72152-2-dakr@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240610180318.72152-2-dakr@redhat.com> On Mon, Jun 10, 2024 at 08:02:27PM +0200, Danilo Krummrich wrote: [...] > +/// A reference-counted device. > +/// > +/// This structure represents the Rust abstraction for a C `struct device`. This implementation > +/// abstracts the usage of an already existing C `struct device` within Rust code that we get > +/// passed from the C side. > +/// > +/// An instance of this abstraction can be obtained temporarily or permanent. > +/// > +/// A temporary one is bound to the lifetime of the C `struct device` pointer used for creation. > +/// A permanent instance is always reference-counted and hence not restricted by any lifetime > +/// boundaries. > +/// > +/// For subsystems it is recommended to create a permanent instance to wrap into a subsystem > +/// specifc device structure (e.g. `pci::Device`). This is useful for passing it to drivers in > +/// `T::probe()`, such that a driver can store the `ARef` (equivalent to storing a > +/// `struct device` pointer in a C driver) for arbitrary purposes, e.g. allocating DMA coherent > +/// memory. > +/// > +/// # Invariants > +/// > +/// The pointer stored in `Self` is non-null and valid for the lifetime of the `ARef` instance. In > +/// particular, the `ARef` instance owns an increment on the underlying object’s reference count. > +#[repr(transparent)] > +pub struct Device(Opaque); > + [...] > + > +// SAFETY: `Device` only holds a pointer to a C `struct device`, which is safe to be used from any > +// thread. > +unsafe impl Send for Device {} > + > +// SAFETY: `Device` only holds a pointer to a C `struct device`, references to which are safe to be > +// used from any thread. > +unsafe impl Sync for Device {} These comments need some rework, `Device` is not a pointer to `struct device` anymore. For the `Sync` one, how about: // SAFETY: `Device` can be shared among threads because all immutable // methods are protected by the synchronization in `struct device`. unsafe impl Sync for Device {} and for `Send`, I actually don't think we can easily say the generic `Device` is `Send`: you can create a `struct device` where `->release` requires to be run on the same thread that creates the `device`, and nothing is wrong about it, I think (e.g. making a thread be the sole owner of some special devices). Unless, in the #Invariants of `Device`, and the #safety of `from_ptr`, you mention that `Device` assume its `->release` can be called on any thread. Regards, Boqun > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index fbd91a48ff8b..dd1207f1a873 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -28,6 +28,7 @@ > > pub mod alloc; > mod build_assert; > +pub mod device; > pub mod error; > pub mod init; > pub mod ioctl; > -- > 2.45.1 >