Skip to contentSkip to conversation

rust: serdev: Mitigate race conditions

PATCH Superseded Markus Probst 3 participants 3 messages Sep 5

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Markus Probst

author maintainer
#0 lore raw

PATCH v3 1/1 rust: serdev: Mitigate race conditions

There are currently 2 race conditions:
- in probe if Driver::probe returns Err
- in unbind
. In those cases the driver data will be set to NULL before the serdev device was closed. If data is received while the driver data is dropped, the receive_buf_callback might try to access the active mutex on a null pointer. The race conditions can only occur if Driver::receive is implemented.

The issue cannot be cleanly fixed without rewriting some logic, which might introduce new regressions.

Temporarily disable the use of Driver::receive, until fixed in the next release.

---
I will submit a patch (for the next merge cycle) soon, which will
address this issue and make the probe and unbind code less convoluted.
---
Changes in v3:
- mitigate it
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- also fix race condition on unbind
- Link to v1: https://patch.msgid.link/[email protected]

---
 rust/kernel/serdev.rs              | 7 ++++++-
 samples/rust/rust_driver_serdev.rs | 6 +++---
 2 files changed, 9 insertions(+), 4 deletions(-)
rust/kernel/serdev.rs +6 -1
index 17ca504b7f8d..63b489404558 100644
@@ -148,10 +148,14 @@ fn drop(self: Pin<&mut Self>) {
 
 impl<T: Driver> Adapter<T> {
     const OPS: &'static bindings::serdev_device_ops = &bindings::serdev_device_ops {
-        receive_buf: if T::HAS_RECEIVE {
+        /* receive_buf: if T::HAS_RECEIVE {
             Some(Self::receive_buf_callback)
         } else {
             None
+        }, */
+        receive_buf: {
+            const_assert!(!T::HAS_RECEIVE);
+            None
         },
         write_wakeup: Some(bindings::serdev_device_write_wakeup),
     };
@@ -233,6 +237,7 @@ extern "C" fn remove_callback(sdev: *mut bindings::serdev_device) {
         T::unbind(sdev, data_pinned);
     }
 
+    #[expect(dead_code)]
     extern "C" fn receive_buf_callback(
         sdev: *mut bindings::serdev_device,
         buf: *const u8,
samples/rust/rust_driver_serdev.rs +3 -3
index 51b4898cd855..863db75d3b5a 100644
@@ -5,7 +5,7 @@
 use kernel::{
     acpi,
     device::{
-        Bound,
+        // Bound,
         Core, //
     },
     of,
@@ -61,13 +61,13 @@ fn probe<'bound>(
         Ok(Self { sdev: sdev.into() })
     }
 
-    fn receive<'bound>(
+    /*fn receive<'bound>(
         sdev: &'bound serdev::Device<Bound>,
         _this: Pin<&Self>,
         data: &[u8],
     ) -> usize {
         sdev.write(data).unwrap_or_default() as usize
-    }
+    }*/
 }
 
 impl Drop for SampleDriver {

---
base-commit: e5e04726cdd043e309677071ab1b65a4b18f422b
change-id: 20260904-rust_serdev_fix-be3ff9c8a5e8

#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

There are currently 2 race conditions:
- in probe if Driver::probe returns Err
- in unbind
. In those cases the driver data will be set to NULL before the serdev device was closed. If data is received while the driver data is dropped, the receive_buf_callback might try to access the active mutex on a null pointer. The race conditions can only occur if Driver::receive is implemented.

The issue cannot be cleanly fixed without rewriting some logic, which might introduce new regressions.

Quoted 8 lines

Temporarily disable the use of Driver::receive, until fixed in the next release.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Markus Probst
Quoted 7 lines · Markus Probst
(quoted diff) +2 -0
+        /* receive_buf: if T::HAS_RECEIVE {
             Some(Self::receive_buf_callback)
         } else {
             None
+        }, */
 

Review

Quoted 10 lines
(quoted diff) +3 -1
+        // Bound,
 
+    /*fn receive<'bound>(
         sdev: &'bound serdev::Device<Bound>,
         _this: Pin<&Self>,
         data: &[u8],
     ) -> usize {
         sdev.write(data).unwrap_or_default() as usize
-    }
+    }*/

Please don't comment code out -- it is fine to just remove it even if it is a temporary measure. The code will still be there in Git.

Thanks!

Cheers,
Miguel