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 C27F8CD6E5E for ; Sun, 31 May 2026 14:00:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D45E1112B76; Sun, 31 May 2026 13:59:49 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; secure) header.d=posteo.de header.i=@posteo.de header.b="reHg+A3c"; dkim-atps=neutral Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6857210E368 for ; Sat, 30 May 2026 14:00:17 +0000 (UTC) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id E7D2B240105 for ; Sat, 30 May 2026 16:00:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1780149615; bh=G/i0TO9GdgjX59jtswmOC3XKG8URzr+7FHgsFwNYnWs=; h=Message-ID:Subject:From:To:Cc:Date:Autocrypt:Content-Type: MIME-Version:OpenPGP:From; b=reHg+A3cW9xGrGiM6zb69u7+Z5fo86S5grty0slumLuRzedsdIcmr+876O9WJeI4a wRuTil7JMuDYw4tuscf8RVUM3SwNWQmf4nIV3UECJbwWcHNeWXRhA+4806cbDOdiA+ WSnWtsJG4EtW+AaEh4fKfhAp4LqifzLvlGz+VSJneqJUkLw4WY5YSwqhi6O8eQu4op YDu/9ZQnUJ1rU9PHg+IumWLmXji5/sCtDa4mibPSmRThQ1x+A7LyzgCs9iNOpDohRd hA5RFTAhcnAvZiUqx4LWsN+9MLhapLH1rECFAaQWVvLjxIwMOWNIEIL8G0EVjW2Stg IcwGfqbq75EWQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4gSMLN19Frz6tsb; Sat, 30 May 2026 16:00:12 +0200 (CEST) Message-ID: <888dc39c52bb6ddac1a1eed7876c4573bdbef002.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 14:00:15 +0000 In-Reply-To: References: <20260530-rust_serdev-v8-0-2a95f1da22a7@posteo.de> <20260530-rust_serdev-v8-3-2a95f1da22a7@posteo.de> <4638946fc49a38797b716ea173c93327eb751479.camel@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/1ZGMZukyXFfsDGtA3BB79HoopHKujL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DIsMgIbAwULCQgHAgIiAgYVCgkIC 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="=-fVOfyae3Wn6pl54P3Xax" 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" --=-fVOfyae3Wn6pl54P3Xax Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, 2026-05-30 at 15:53 +0200, Markus Probst wrote: > On Sat, 2026-05-30 at 15:37 +0200, Markus Probst wrote: > > 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_devic= e) -> kernel::ffi::c_int { > > > > + // SAFETY: The serial device bus only ever calls the probe= callback with a valid pointer to > > > > + // a `struct serdev_device`. > > > > + // > > > > + // INVARIANT: `sdev` is valid for the duration of `probe_c= allback()`. > > > > + let sdev =3D unsafe { &*sdev.cast::>>() }; > > > > + let info =3D ::id_info(sdev.as_re= f()); > > > > + > > > > + 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= pointer to `serdev_device`. > > > > + unsafe { > > > > + (*sdev.as_raw()).rust_private_data =3D > > > > + (&raw const *private_data).cast::().ca= st_mut() > > > > + }; > > > > + > > > > + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid= pointer to `serdev_device`. > > > > + unsafe { bindings::serdev_device_set_client_ops(sdev.a= s_raw(), Self::OPS) }; > > > > + > > > > + // SAFETY: The serial device bus only ever calls the p= robe 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 y= ou can't > > > use the devm helper here. I suggest to use serdev_device_open() and c= all > > > 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 call > > > 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. > >=20 > > 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. > >=20 > > Any suggestions? > I could keep calling devm, but close it before unbind and reopen it > without the receive ops. The devm will close it then again. >=20 > This way it stays open until the last devres callback has run, but > doesn't call the receive_buf_callback while the drvdata has been > dropped. >=20 > I will test it and see if it works. Ok, forget it. Only works safely in theory. `serdev_device_open` could fail and that would be unsound again. Thanks - Markus Probst >=20 > Thanks > - Markus Probst >=20 > >=20 > > >=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.e= rror`. > > > > + 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_devi= ce) { > > > > + // SAFETY: The serial device bus only ever calls the remov= e callback with a valid pointer > > > > + // to a `struct serdev_device`. > > > > + // > > > > + // INVARIANT: `sdev` is valid for the duration of `remove_= callback()`. > > > > + let sdev =3D unsafe { &*sdev.cast::>>() }; > > > > + > > > > + // SAFETY: `remove_callback` is only ever called after a s= uccessful call to > > > > + // `probe_callback`, hence it's guaranteed that `Device::s= et_drvdata()` 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 recei= ve buf 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 withi= n this > > > 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= callback with a valid pointer to > > > > + // a `struct serdev_device`. > > > > + // - `receive_buf_callback` is only ever called after a su= ccessful call to > > > > + // `probe_callback`, hence it's guaranteed that `sdev.pr= ivate_data` is a pointer > > > > + // to a valid `PrivateData`. > > > > + let private_data =3D unsafe { &*(*sdev.as_raw()).rust_priv= ate_data.cast::() }; > > > > + > > > > + private_data.probe_complete.wait_for_completion(); > > > > + > > > > + // SAFETY: No one has exclusive access to `private_data.er= ror`. > > > > + if unsafe { *private_data.error.get() } { > > > > + return length; > > > > + } > > > > + > > > > + // SAFETY: `receive_buf_callback` is only ever called afte= r a successful call to > > > > + // `probe_callback`, hence it's guaranteed that `Device::s= et_drvdata()` 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, leng= th) }; > > > > + > > > > + T::receive(sdev, data, buf) > > > > + } > > > > +} > >=20 > > Thanks > > - Markus Probst --=-fVOfyae3Wn6pl54P3Xax Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- iQJPBAABCAA5FiEEgnQYxPSsWOdyMMRzNHYf+OetQ9IFAmoa7WsbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyAAoJEDR2H/jnrUPShr8P/2iPeyrmnfRRTKwVPu8p H8qPCGjyu5PW4jTLBph30XOvJT+pTCo/VEE+14aVs6MwbbCwGTck+Wqa3b2v65PV Sd8V6RBN8Eo7WZxHuQJ/o21bYPCDBrISGbAetQR/od8n7KN2dSt4RpNTfnordgms UaeAmYtCb7dNIoRt2yX3ZnzibModA6bNXHdUEwQg05XpnuQ3XCCf8q3XhdZ7AC34 mjZw/2Ya5LD4Fm1R7Zb2L8jLbxB0FZ3B3mUgNIAiswl5IZXh7D2AOnmjf/sPSE9f uAOETcVWWReV2TMHQfB1Fo7nwHS6qgDL8Qbb8t3jUpMM+BatkmbUPfI1B+x5RFzR 0Vd87IQ4Sc5XksYjZurxXd96Qm4tHZPfml7bnxXtSIbfpFuhd167eQxw0jnMlhuc wBNknOd5abBBqlAeLdzmEy49jPIgsTJ69aBaD/KvmBxMwedaG6mtY6PO9LWSi+6+ ljLf1SW1Q70K7VnZwLlN94256aUCsm1RfTYXSJ2Q0o3yRd7mKZeEEuuuH+tM9lA0 OQwBjCoE+r5RbEH57yq0vIzZmMYuZ8/gEcH/FcxYVDE+cxK9fLvxqOaOKNzERZrS 5jSBdaSWNjIq/Za5/8jML5j0Jg5HOCs0wtGUxqxZe2REnI5JxPy52U/7DtP/YVHy w2wgg3FqKHPCpl0XTkXbSPDs =AbU4 -----END PGP SIGNATURE----- --=-fVOfyae3Wn6pl54P3Xax--