From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (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 BE87B186E2E; Thu, 26 Jun 2025 04:13:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750911209; cv=none; b=Ph8QdE9PV0vpB+3YNdJpHOTDAyhuznRJKzu2MMRUnx7xZGIZf5we6xbXzOPS1ec8WOkoXpT2K4/zAkbP1wFYN4iSHYCdh3dqTXUjjgtRaBYt7kfjbgOueobxP7+Dv3fR0kRzc6PlyypaebjHchsl3JWFIrKr1lWfxQykuPQnWjE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750911209; c=relaxed/simple; bh=HBCEEJz/CPA+TBfOtmhfTxmJ2gUIJttlEr9jJMcBJRk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Dj6BgnIHCDuh8Ovo/mKuXkm1b8+YrvUXwtVca/zOOU1kUW3IL+a3hDVCX6b/wvhr8fuZe1nJWoUhu+C3uOlyiXqBHaWu5Vxp6Hb47hkjjnivM3yFSHlq4Z+VmuX0nbqptn0Qa+hY49bddX2/02mplkGlA76Uag72iCZm10hA1SI= 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=KFwpRieB; arc=none smtp.client-ip=209.85.219.47 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KFwpRieB" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6fd0a3cd326so8458626d6.1; Wed, 25 Jun 2025 21:13:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750911206; x=1751516006; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=cB89kIOPc2zsNiciblqiuc+ZzwafSNwodC7kwMuppfQ=; b=KFwpRieB9N1DxzqFLmRb3R+X1uDQlQ3Z3XX+wOu/nLmSI2HHCsN5IAemZ3CNVVJmeL iZJ1AKryqPVa5oxlMtW3+kGKYCANILUDqnDMFITsQilAyylzYOgp5xxun9qwLHM9VGpX 1pU9NpTmvtuPZQLbfglvpkOuLkc9u1UZZsrlso14Tm+BcIknOSMH7EIzBTB9eWihg9rf HFRP7+cCpNsydVrUJRVLmqAj/t4iVpi6izfLk0io/Lu7jGjo64I9JS55JR4kaEZX4P1Y 8P5RMRERzhLEQDE2IsgGKHalxH8a6oepfyF0y/ptl6yNkp1QPLN2om5VUfbm+hTAdOH6 RiqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750911206; x=1751516006; h=in-reply-to: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=cB89kIOPc2zsNiciblqiuc+ZzwafSNwodC7kwMuppfQ=; b=Guf2NjNIVnxwUKqv21NuXWXJiB7CdNGr2KHP0UjsSRrY/ky8i+7CdcTwmdohxyogWF UsIhZci3hf1pK1zYsvzvrvQQQeIP/I7HkyK9ZjSrEGMxLCw4q2m6dZjdabwPLJB7LkZE CDPkIAfYw8jSYnw6OcpjPu/wgaNr/kvi7mAG3aiI6CxTMemz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/N/PL+WpfaA2TZVJgo4mdX1mTLcLC6ilJONaUQ9y3KxymHOe4xQKG0/A== X-Received: by 2002:a05:6214:570a:b0:6ed:19d1:212f with SMTP id 6a1803df08f44-6fd5ef2fdadmr95362836d6.5.1750911206526; Wed, 25 Jun 2025 21:13:26 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3f99efaa9sm681441585a.58.2025.06.25.21.13.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jun 2025 21:13:25 -0700 (PDT) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfauth.phl.internal (Postfix) with ESMTP id 60FCFF40068; Thu, 26 Jun 2025 00:13:25 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Thu, 26 Jun 2025 00:13:25 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddvgddvgeeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveeiudffiedv necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqh hunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddu jeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvg drnhgrmhgvpdhnsggprhgtphhtthhopedvtddpmhhouggvpehsmhhtphhouhhtpdhrtghp thhtohepuggrkhhrsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehgrhgvghhkhheslh hinhhugihfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopehrrghfrggvlheskhgv rhhnvghlrdhorhhgpdhrtghpthhtohepohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtg hpthhtoheprghlvgigrdhgrgihnhhorhesghhmrghilhdrtghomhdprhgtphhtthhopehg rghrhiesghgrrhihghhuohdrnhgvthdprhgtphhtthhopegsjhhorhhnfegpghhhsehprh hothhonhhmrghilhdrtghomhdprhgtphhtthhopehlohhsshhinheskhgvrhhnvghlrdho rhhgpdhrtghpthhtoheprgdrhhhinhgusghorhhgsehkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 26 Jun 2025 00:13:24 -0400 (EDT) Date: Wed, 25 Jun 2025 21:13:24 -0700 From: Boqun Feng To: Danilo Krummrich Cc: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: Re: [PATCH v3 3/4] rust: devres: get rid of Devres' inner Arc Message-ID: References: <20250624215600.221167-1-dakr@kernel.org> <20250624215600.221167-4-dakr@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250624215600.221167-4-dakr@kernel.org> On Tue, Jun 24, 2025 at 11:54:01PM +0200, Danilo Krummrich wrote: [...] > +#[pin_data(PinnedDrop)] > +pub struct Devres { It makes me realize: I think we need to make `T` being `Send`? Because the devm callback can happen on a different thread other than `Devres::new()` and the callback may drop `T` because of revoke(), so we are essientially sending `T`. Alternatively we can make `Devres::new()` and its friend require `T` being `Send`. If it's true, we need a separate patch that "Fixes" this. (Imagine a Devres) > + dev: ARef, > + /// Pointer to [`Self::devres_callback`]. > + /// > + /// Has to be stored, since Rust does not guarantee to always return the same address for a > + /// function. However, the C API uses the address as a key. > + callback: unsafe extern "C" fn(*mut c_void), > + /// Contains all the fields shared with [`Self::callback`]. > + // TODO: Replace with `UnsafePinned`, once available. nit: Maybe also reference the `drop_in_place()` in Devres::drop() as well, because once we use `UnsafePinned`, we don't need that `drop_in_place()`. But not a big deal, just trying to help the people who would handle that "TODO" ;-) > + #[pin] > + inner: Opaque>, > +} > + [...] > +// SAFETY: `Devres` can be send to any task, if `T: Send`. > +unsafe impl Send for Devres {} > + > +// SAFETY: `Devres` can be shared with any task, if `T: Sync`. > +unsafe impl Sync for Devres {} `T` also need to be `Send` for `Devres` to be `Sync` because that's what `Revocable` requires. (Unless we want `T` always being `Send` because of the issue I mentioned above) The rest looks good to me. Regards, Boqun > + > +#[pinned_drop] > +impl PinnedDrop for Devres { > + fn drop(self: Pin<&mut Self>) { > // SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data > // anymore, hence it is safe not to wait for the grace period to finish. > - if unsafe { self.0.data.revoke_nosync() } { > - // We revoked `self.0.data` before the devres action did, hence try to remove it. > - if !DevresInner::remove_action(&self.0) { > + if unsafe { self.data().revoke_nosync() } { > + // We revoked `self.data` before the devres action did, hence try to remove it. > + if !self.remove_action() { > // We could not remove the devres action, which means that it now runs concurrently, > - // hence signal that `self.0.data` has been revoked successfully. > - self.0.revoke.complete_all(); > + // hence signal that `self.data` has been revoked by us successfully. > + self.inner().revoke.complete_all(); > + > + // Wait for `Self::devres_callback` to be done using this object. > + self.inner().devm.wait_for_completion(); > } > + } else { > + // `Self::devres_callback` revokes `self.data` for us, hence wait for it to be done > + // using this object. > + self.inner().devm.wait_for_completion(); > } > + > + // INVARIANT: At this point it is guaranteed that `inner` can't be accessed any more. > + // > + // SAFETY: `inner` is valid for dropping. > + unsafe { core::ptr::drop_in_place(self.inner.get()) }; > } > } > [...]