From mboxrd@z Thu Jan 1 00:00:00 1970 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D3674CD6E52 for ; Sun, 31 May 2026 13:58:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0B4B8112B4E; Sun, 31 May 2026 13:58:33 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; secure) header.d=posteo.de header.i=@posteo.de header.b="Fu4RwBSb"; dkim-atps=neutral Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8412E10E338 for ; Sat, 30 May 2026 13:37:32 +0000 (UTC) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id DB544240101 for ; Sat, 30 May 2026 15:37:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1780148250; bh=SXr32rjKJbNyYUQ0Dql4qeVjTj4pPxg4GyHhz4KKolU=; h=Message-ID:Subject:From:To:Cc:Date:Autocrypt:Content-Type: MIME-Version:OpenPGP:From; b=Fu4RwBSbBi08AqYeH7dM6yfW3BxPXIAY/3uBqbZh6Ta9vs2/wjxiz/6qvnvnMK/fC MvLlpdyGSgZSgfp9utRx29qNpEg4s/rzxtlmIBSK1qaY/B0uOWAcrOxABlHB5cJcYZ jYEAXwHPShhrCOQw4Z/qaQkKlVZm80umHxzgJwfNM0dONJr+4NQcosWOg9yfCgJ/0I YCwXJDZUDtotM1hWhOvElDm8WqressVkJRfgSmySMruXK+G6SEL3wfcQ9DEbeJrJpG gim3+6yh0jJbZ3ob/GBlpccoYZoxdiDNvTNIEhiOwaaNyFE1hY3LyfPrYd1I6PZ8YR yZEM0kwMnN1GA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4gSLr75bylz9rxF; Sat, 30 May 2026 15:37:27 +0200 (CEST) Message-ID: <4638946fc49a38797b716ea173c93327eb751479.camel@posteo.de> Subject: Re: [PATCH v8 3/5] rust: add basic serial device bus abstractions From: Markus Probst To: Danilo Krummrich , Markus Probst via B4 Relay Cc: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?ISO-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Kari Argillander , "Rafael J. Wysocki" , Viresh Kumar , Boqun Feng , David Airlie , Simona Vetter , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pm@vger.kernel.org, driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org Date: Sat, 30 May 2026 13:37:30 +0000 In-Reply-To: References: <20260530-rust_serdev-v8-0-2a95f1da22a7@posteo.de> <20260530-rust_serdev-v8-3-2a95f1da22a7@posteo.de> Autocrypt: addr=markus.probst@posteo.de; prefer-encrypt=mutual; keydata=mQINBGiDvXgBEADAXUceKafpl46S35UmDh2wRvvx+UfZbcTjeQOlSwKP7YVJ4JOZrVs93 qReNLkOWguIqPBxR9blQ4nyYrqSCV+MMw/3ifyXIm6Pw2YRUDg+WTEOjTixRCoWDgUj1nOsvJ9tVA m76Ww+/pAnepVRafMID0rqEfD9oGv1YrfpeFJhyE2zUw3SyyNLIKWD6QeLRhKQRbSnsXhGLFBXCqt 9k5JARhgQof9zvztcCVlT5KVvuyfC4H+HzeGmu9201BVyihJwKdcKPq+n/aY5FUVxNTgtI9f8wIbm fAjaoT1pjXSp+dszakA98fhONM98pOq723o/1ZGMZukyXFfsDGtA3BB79HoopHKujLGWAGskzClwT jRQxBqxh/U/lL1p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kIC wIEFgIDAQIeBwIXgAAKCRA0dh/4561D0gKJD/9uOQKYlsDoQX65Gd0LiMT0C+5vXgr3VI0PHDOwcv 51fJ3A1vNyPZRFPGrz8+mDEXUQOF/INfnz5Tu1QHwf+iYcWcTGAN/FHgVR6ET6VBNU2hJaKhu+Ggo kjYyJTOvyX+3yNRUfSny0GjTjIPuPTErjqmHF+BtjXslpgwqnNMznf3lRIuUjRORupos6p3k1DndE 5vzUTmXSvMyXyOD2KhBl/kL76k0bHYyAQytZPag12pltrtFbA/r2phDGN2si8PooDT99bSTJjaM45 MTAAHbHKJfvgfK41bNFD5mMtpWpL195XRtS0Nrxdg3PaYBxN5gtTG0RyZfpYRlkdEhm+jj/8RxuSG i/qdhRdbiI7K2IELWeQVHSNDi9JabR/UzlR4NSnhfAjRIVlRM+eFbUl8XwxwVrAkojF5IraH2qRvg VCmuFsHUW07FUlrDrzpjXsD73cKppoFGDCdDR0BHJepXbFLS9+AqkT+guRJlnCTg2p+TQtnbwPgKp Vj98JixovCl99zRYTsL2bRNU5+q8iET65VMJ1ydyNanvLd5vI/NqDkXhlXLsGmdaDTtu4R21PkToX dQNGrZ91M9nlIBKw8Y7c7xZ4098qX2b8JX/CxD+gC1r4C8vuA3GkhFLx+KlkON7LyiJPkrePp6Qky jfGillcaQOqFZ3WwVqyzG1BUfTow== Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-mbJrtKM2BegUzHyWsn7m" MIME-Version: 1.0 OpenPGP: url=https://posteo.de/keys/markus.probst@posteo.de.asc; preference=encrypt X-Mailman-Approved-At: Sun, 31 May 2026 13:58:31 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" --=-mbJrtKM2BegUzHyWsn7m Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, 2026-05-30 at 15:10 +0200, Danilo Krummrich wrote: > On Sat May 30, 2026 at 3:13 AM CEST, Markus Probst via B4 Relay wrote: > > + extern "C" fn probe_callback(sdev: *mut bindings::serdev_device) -= > kernel::ffi::c_int { > > + // SAFETY: The serial device bus only ever calls the probe cal= lback with a valid pointer to > > + // a `struct serdev_device`. > > + // > > + // INVARIANT: `sdev` is valid for the duration of `probe_callb= ack()`. > > + let sdev =3D unsafe { &*sdev.cast::>>() }; > > + let info =3D ::id_info(sdev.as_ref())= ; > > + > > + from_result(|| { > > + let private_data =3D devres::register( > > + sdev.as_ref(), > > + try_pin_init!(PrivateData { > > + probe_complete <- Completion::new(), > > + error: false.into(), > > + }), > > + GFP_KERNEL, > > + )?; > > + > > + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid poi= nter to `serdev_device`. > > + unsafe { > > + (*sdev.as_raw()).rust_private_data =3D > > + (&raw const *private_data).cast::().cast_m= ut() > > + }; > > + > > + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid poi= nter to `serdev_device`. > > + unsafe { bindings::serdev_device_set_client_ops(sdev.as_ra= w(), Self::OPS) }; > > + > > + // SAFETY: The serial device bus only ever calls the probe= callback with a valid pointer > > + // to a `serdev_device`. > > + to_result(unsafe { > > + bindings::devm_serdev_device_open(sdev.as_ref().as_raw= (), sdev.as_raw()) > > + })?; >=20 > The bus device private data drops before devres callbacks run, thus you c= an't > use the devm helper here. I suggest to use serdev_device_open() and call > serdev_device_close() from remove_callback() instead. >=20 > You may also want to consider whether you potentially want T::unbind() to= allow > to have concurrent transmits in flight regardless. IOW, you may want to c= all > serdev_device_close() before T::unbind() anyway. `serdev::Device` will be passed as argument to unbind, i.e. set_baudrate, set_flow_control etc. calls are possible. If we close the serdev device before unbind, calling those would result in a null pointer dereference. Maybe a driver even wants to send a message here, so it should stay open. If I think about it, I can't even close it inside remove_callback after `T::unbind`. With the driver lifetimes, the driver could store the `serdev::Device` pointer in its DriverData and could still make calls to the device. Any suggestions? >=20 > > + > > + let data =3D T::probe(sdev, info); > > + let result =3D sdev.as_ref().set_drvdata(data); > > + > > + // SAFETY: We have exclusive access to `private_data.error= `. > > + unsafe { *private_data.error.get() =3D result.is_err() }; > > + > > + private_data.probe_complete.complete_all(); > > + > > + result.map(|()| 0) > > + }) > > + } > > + > > + extern "C" fn remove_callback(sdev: *mut bindings::serdev_device) = { > > + // SAFETY: The serial device bus only ever calls the remove ca= llback with a valid pointer > > + // to a `struct serdev_device`. > > + // > > + // INVARIANT: `sdev` is valid for the duration of `remove_call= back()`. > > + let sdev =3D unsafe { &*sdev.cast::>>() }; > > + > > + // SAFETY: `remove_callback` is only ever called after a succe= ssful call to > > + // `probe_callback`, hence it's guaranteed that `Device::set_d= rvdata()` has been called > > + // and stored a `Pin>>`. > > + let data =3D unsafe { sdev.as_ref().drvdata_borrow::>() }; > > + > > + T::unbind(sdev, data); > > + } > > + > > + extern "C" fn receive_buf_callback( > > + sdev: *mut bindings::serdev_device, > > + buf: *const u8, > > + length: usize, > > + ) -> usize { > > + // SAFETY: The serial device bus only ever calls the receive b= uf callback with a valid > > + // pointer to a `struct serdev_device`. > > + // > > + // INVARIANT: `sdev` is valid for the duration of `receive_buf= _callback()`. > > + let sdev =3D unsafe { &*sdev.cast::>>() }; >=20 > CoreInternal dereferences to Core, which is technically unsound within th= is > callback. >=20 > I think you want CoreInternal for drvdata_borrow(), so we should add a > BoundInternal type state for this; I will send a patch for this. >=20 > With this and the above fixed, >=20 > Reviewed-by: Danilo Krummrich >=20 > from the driver-core side of things. >=20 > > + // SAFETY: > > + // - The serial device bus only ever calls the receive buf cal= lback with a valid pointer to > > + // a `struct serdev_device`. > > + // - `receive_buf_callback` is only ever called after a succes= sful call to > > + // `probe_callback`, hence it's guaranteed that `sdev.privat= e_data` is a pointer > > + // to a valid `PrivateData`. > > + let private_data =3D unsafe { &*(*sdev.as_raw()).rust_private_= data.cast::() }; > > + > > + private_data.probe_complete.wait_for_completion(); > > + > > + // SAFETY: No one has exclusive access to `private_data.error`= . > > + if unsafe { *private_data.error.get() } { > > + return length; > > + } > > + > > + // SAFETY: `receive_buf_callback` is only ever called after a = successful call to > > + // `probe_callback`, hence it's guaranteed that `Device::set_d= rvdata()` has been called > > + // and stored a `Pin>>`. > > + let data =3D unsafe { sdev.as_ref().drvdata_borrow::>() }; > > + > > + // SAFETY: `buf` is guaranteed to be non-null and has the size= of `length`. > > + let buf =3D unsafe { core::slice::from_raw_parts(buf, length) = }; > > + > > + T::receive(sdev, data, buf) > > + } > > +} Thanks - Markus Probst --=-mbJrtKM2BegUzHyWsn7m Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- iQJPBAABCAA5FiEEgnQYxPSsWOdyMMRzNHYf+OetQ9IFAmoa6BYbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyAAoJEDR2H/jnrUPS+LUP/0Jxc80EuLpjY2G+uAqc WpowXEcHOtta/A+Qn721xtuJ+vxEGhbtXU9YmjzX4op7muKMFC8puoYIVZLKpfxq ndGzvOeZaFJacPKbKREvYpY26uMxxF1+TZ51MzYH9/Hetc2eKTBDWis7vBjPvaXu uNyb5CbX6BJu0N+JFYv6u4jxb5frq/0RDVVqeutapi/srSX/p+S7R2WMdO8y9flr H7Vt3+YZUcWKBAZpY6wTQz2XTU5lGMNEcj5HM7g4/ghcBkW8LJUVGXKI1miZPVGg uFzFtfIOZnTd99XtpAmep7SoJTN97KqRG2AYSuFDLuBjDXPPsz+Ubx+W7Q3Piu5f agzQzS1nBNNNEFOex0TyER+eVS728c/3LVAKbPJWKG4FiBs6eeDaoerJVdmLx/DR 4+ifHvfCWPa5rDwfHfINeYzqGDTyGKUgCcKCTESv1gTlAl/QmSOR7MEc5u4b5x+9 7ebt8+ueewqPTWu6+oYu68mmzysJX63wwgNDjhPKUxxwMfhcTyFciVSsJCffYE8X RYrwlKK+0TPhzW1qKYFqVkNlFfaFu4JA13KcwIpfCt/sVdaYPNT0APCbfktyPN7c yHqb1e6AiPWc+C+tHqz0cFVeN6I3FL8LMENOJ0Bq+Tog6282EFgiDZpz7dDDmizi X9ylbe+R2PRnxVTjAv0ITPBS =Bo67 -----END PGP SIGNATURE----- --=-mbJrtKM2BegUzHyWsn7m--