Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2622664lqo; Mon, 20 May 2024 11:02:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVr9jnWNj2ppLjMlcUInuMl82HxFG9/J/m2GylBR5ihQRJcak5iV4Sxil+283ZIO2jl0hc/xxG4ME6YfAenqLUH35MM6wWUmbK3gEY8FA== X-Google-Smtp-Source: AGHT+IHruxV2n7CpPjPKkM4A1ZK5xxFJlFwVghyTjIxJ8Mv4/HaKnS9zDGuyaRTtLEIcy1YyDAwj X-Received: by 2002:a05:6512:b8d:b0:51c:c7ad:329b with SMTP id 2adb3069b0e04-5242200a10emr6845842e87.30.1716228178226; Mon, 20 May 2024 11:02:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716228178; cv=pass; d=google.com; s=arc-20160816; b=kRgvxvxtGE6bdwFPcsAjd5KnK2x6/rMZgLeQC7emR2YFuSOSHpSmbGnFgfHBdkd8xa EE0C4FBTM6aGIEEVZ8dFaJGZsg4Jd8WYljhQZVRp0bO9otBFWhN1/WKMclrhNuDFdUFS KXRJ5C/L12CzYH+nxt+TjwB3kvcy7Zw3LXXhlGLi5A4b+o/8r7WZ/anxo4tXNNvdINWl /bzuhjDcHn5ilGs2eSNxqXicLUkpqT6uOfcyw0/0WN1lpO18R6bYVa3+i6CVXuNCaYjd TTRTxOW3KF8KKWFf04baw2MjeJ8VJRAz97kljUyz/fFhOntXtpozl06Ouv9N/EWqs4aI XRIw== 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:dkim-signature; bh=8Q/smVXC3ZkbBmRd41PLI8VlBmYysGIL0Bb/5qcoV2Y=; fh=vTJwzQHUAkPnFukvGUcruuk8IB4DpQwWLUm9b+NALt4=; b=TH3SVTZypOdfMmoKGPFXyexT5odOt2WRuz1XF6Qjoe/oY+C6/5SHa6iUjvaX189VRr yu6TB1w0aNTmohFGuZ8SoNrKQu9UtCtAtpIQUwg64Tkt+6szyb0iDnC68g7LhCcCLs6A JAAOmuzTewaOF9XAnu80/fSbKDxyh4mEMzFmfqs1CjDUkjjg1WpFymV4OVTeFF2kFQWG o+NCvf3UE/OlPVoJupLd260QIXvuF6+6HfKpAqqg2sRzQQe2jaECEsUrVxWWDcM9FG5G MRBon6FX5dUOEdyzC1ICKzvO3Jv3Leg5cvrvPNizUcwFBKHCiqjNhjDnjnv93h60lF32 5UEw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Nz2jfv0X; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-184042-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184042-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5ce2ec3d9bsi544817466b.282.2024.05.20.11.02.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 11:02:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184042-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=@linuxfoundation.org header.s=korg header.b=Nz2jfv0X; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-184042-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184042-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org 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 EC2B71F21531 for ; Mon, 20 May 2024 18:02:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 364F71386AA; Mon, 20 May 2024 18:00:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Nz2jfv0X" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3457834CDE; Mon, 20 May 2024 18:00:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716228027; cv=none; b=XaA/X4nZo4fjWj8xqFWrfgaoe5jjDmk0MqvtOJ3FmmX47INlu6+Pje1+RttxDJ3nmoWSDJbd39hv6pqgkwyzNmrn24c3IhPn5YUAOplNU+lcFJW7TJStJqyhwMhu2f2d9K4ZkanIwXwvX1pY372TKODOdNV9L48JSfWURyv+7LE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716228027; c=relaxed/simple; bh=vVR9EAvK+2jMSL8py9fcIjHGGS71cFk9eRgtsSMxJ3I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=o67AfKGW5QNsyKHU5N/kCD6foa8qgXYsQsHC4+L0R6mealxSMMD3p710zzalSUou0HOm7vj+ScqKTmx7Stu+P8uhw6LcR9D1OWNCNH1Gyq1rH8mUJCvCCySJrMbrnJ056cs7ee3Ogml5uw8dKNKBWrQSlEwq7UHqPUonQnPY89g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Nz2jfv0X; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E9C4C32789; Mon, 20 May 2024 18:00:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1716228026; bh=vVR9EAvK+2jMSL8py9fcIjHGGS71cFk9eRgtsSMxJ3I=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Nz2jfv0XEg7U5RPgEE4Kz/1YSq4wbFkS4OHP9RSGruDTZQc0+oLvMOO+zq4xzYpHm RndQzzc9nJyljmchgLvsxsAAlNS/T+ZEWoTvi1cNWPiGBi+7J1pv7GdUCWflZp98Ew ZT30w6wE3rwuXw6J7WwjDELCgIhHK7/Qe9rzrDQg= Date: Mon, 20 May 2024 20:00:23 +0200 From: Greg KH To: Danilo Krummrich Cc: rafael@kernel.org, bhelgaas@google.com, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@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, lina@asahilina.net, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: Re: [RFC PATCH 01/11] rust: add abstraction for struct device Message-ID: <2024052038-deviancy-criteria-e4fe@gregkh> References: <20240520172554.182094-1-dakr@redhat.com> <20240520172554.182094-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: <20240520172554.182094-2-dakr@redhat.com> On Mon, May 20, 2024 at 07:25:38PM +0200, Danilo Krummrich wrote: > Add an (always) reference counted abstraction for a generic struct > device. This abstraction encapsulates existing struct device instances > and manages its reference count. > > Subsystems may use this abstraction as a base to abstract subsystem > specific device instances based on a generic struct device. > > Co-developed-by: Wedson Almeida Filho > Signed-off-by: Wedson Almeida Filho > Signed-off-by: Danilo Krummrich > --- > rust/helpers.c | 1 + > rust/kernel/device.rs | 76 +++++++++++++++++++++++++++++++++++++++++++ What's the status of moving .rs files next to their respective .c files in the build system? Keeping them separate like this just isn't going to work, sorry. > --- /dev/null > +++ b/rust/kernel/device.rs > @@ -0,0 +1,76 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Generic devices that are part of the kernel's driver model. > +//! > +//! C header: [`include/linux/device.h`](../../../../include/linux/device.h) relative paths for a common "include + > +use crate::{ > + bindings, > + types::{ARef, Opaque}, > +}; > +use core::ptr; > + > +/// A ref-counted device. > +/// > +/// # 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 underlying object’s reference count. > +#[repr(transparent)] > +pub struct Device(Opaque); > + > +impl Device { > + /// Creates a new ref-counted instance of an existing device pointer. > + /// > + /// # Safety > + /// > + /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count. Callers NEVER care about the reference count of a struct device, anyone poking in that is asking for trouble. And why non-NULL? Can't you check for that here? Shouldn't you check for that here? Many driver core functions can handle a NULL pointer just fine (i.e. get/put_device() can), why should Rust code assume that a pointer passed to it from the C layer is going to have stricter rules than the C layer can provide? > + pub unsafe fn from_raw(ptr: *mut bindings::device) -> ARef { > + // SAFETY: By the safety requirements, ptr is valid. > + // Initially increase the reference count by one to compensate for the final decrement once > + // this newly created `ARef` instance is dropped. > + unsafe { bindings::get_device(ptr) }; > + > + // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`. > + let ptr = ptr.cast::(); > + > + // SAFETY: By the safety requirements, ptr is valid. > + unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr)) } > + } > + > + /// Obtain the raw `struct device *`. > + pub(crate) fn as_raw(&self) -> *mut bindings::device { > + self.0.get() > + } > + > + /// Convert a raw `struct device` pointer to a `&Device`. > + /// > + /// # Safety > + /// > + /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count for > + /// the entire duration when the returned reference exists. Again, non-NULL might not be true, and reference counts are never tracked by any user EXCEPT to increment/decrement it, you never know if it is 0 or not, all you know is that if a pointer is given to you by the driver core to a 'struct device' for a function that it is a valid reference at that point in time, or maybe NULL, until your function returns. Anything after that can not be counted on. thanks, greg k-h