Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'regulator-fix-v7.0-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator

Pull regulator fix from Mark Brown:
"A fix from Alice for the rust bindings, they didn't handle the stub
implementation of the C API used when CONFIG_REGULATOR is disabled
leading to undefined behaviour"

* tag 'regulator-fix-v7.0-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator:
rust: regulator: do not assume that regulator_get() returns non-null

+18 -15
+18 -15
rust/kernel/regulator.rs
··· 23 23 prelude::*, 24 24 }; 25 25 26 - use core::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull}; 26 + use core::{ 27 + marker::PhantomData, 28 + mem::ManuallyDrop, // 29 + }; 27 30 28 31 mod private { 29 32 pub trait Sealed {} ··· 232 229 /// 233 230 /// # Invariants 234 231 /// 235 - /// - `inner` is a non-null wrapper over a pointer to a `struct 236 - /// regulator` obtained from [`regulator_get()`]. 232 + /// - `inner` is a pointer obtained from a successful call to 233 + /// [`regulator_get()`]. It is treated as an opaque token that may only be 234 + /// accessed using C API methods (e.g., it may be `NULL` if the C API returns 235 + /// `NULL`). 237 236 /// 238 237 /// [`regulator_get()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_get 239 238 pub struct Regulator<State> 240 239 where 241 240 State: RegulatorState, 242 241 { 243 - inner: NonNull<bindings::regulator>, 242 + inner: *mut bindings::regulator, 244 243 _phantom: PhantomData<State>, 245 244 } 246 245 ··· 254 249 // SAFETY: Safe as per the type invariants of `Regulator`. 255 250 to_result(unsafe { 256 251 bindings::regulator_set_voltage( 257 - self.inner.as_ptr(), 252 + self.inner, 258 253 min_voltage.as_microvolts(), 259 254 max_voltage.as_microvolts(), 260 255 ) ··· 264 259 /// Gets the current voltage of the regulator. 265 260 pub fn get_voltage(&self) -> Result<Voltage> { 266 261 // SAFETY: Safe as per the type invariants of `Regulator`. 267 - let voltage = unsafe { bindings::regulator_get_voltage(self.inner.as_ptr()) }; 262 + let voltage = unsafe { bindings::regulator_get_voltage(self.inner) }; 268 263 269 264 to_result(voltage).map(|()| Voltage::from_microvolts(voltage)) 270 265 } ··· 275 270 // received from the C code. 276 271 from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_char_ptr()) })?; 277 272 278 - // SAFETY: We can safely trust `inner` to be a pointer to a valid 279 - // regulator if `ERR_PTR` was not returned. 280 - let inner = unsafe { NonNull::new_unchecked(inner) }; 281 - 273 + // INVARIANT: `inner` is a pointer obtained from `regulator_get()`, and 274 + // the call was successful. 282 275 Ok(Self { 283 276 inner, 284 277 _phantom: PhantomData, ··· 285 282 286 283 fn enable_internal(&self) -> Result { 287 284 // SAFETY: Safe as per the type invariants of `Regulator`. 288 - to_result(unsafe { bindings::regulator_enable(self.inner.as_ptr()) }) 285 + to_result(unsafe { bindings::regulator_enable(self.inner) }) 289 286 } 290 287 291 288 fn disable_internal(&self) -> Result { 292 289 // SAFETY: Safe as per the type invariants of `Regulator`. 293 - to_result(unsafe { bindings::regulator_disable(self.inner.as_ptr()) }) 290 + to_result(unsafe { bindings::regulator_disable(self.inner) }) 294 291 } 295 292 } 296 293 ··· 352 349 /// Checks if the regulator is enabled. 353 350 pub fn is_enabled(&self) -> bool { 354 351 // SAFETY: Safe as per the type invariants of `Regulator`. 355 - unsafe { bindings::regulator_is_enabled(self.inner.as_ptr()) != 0 } 352 + unsafe { bindings::regulator_is_enabled(self.inner) != 0 } 356 353 } 357 354 } 358 355 ··· 362 359 // SAFETY: By the type invariants, we know that `self` owns a 363 360 // reference on the enabled refcount, so it is safe to relinquish it 364 361 // now. 365 - unsafe { bindings::regulator_disable(self.inner.as_ptr()) }; 362 + unsafe { bindings::regulator_disable(self.inner) }; 366 363 } 367 364 // SAFETY: By the type invariants, we know that `self` owns a reference, 368 365 // so it is safe to relinquish it now. 369 - unsafe { bindings::regulator_put(self.inner.as_ptr()) }; 366 + unsafe { bindings::regulator_put(self.inner) }; 370 367 } 371 368 } 372 369