summaryrefslogtreecommitdiff
path: root/rust
AgeCommit message (Collapse)Author
2025-11-05rust: uaccess: add UserSliceWriter::write_slice_partial()Danilo Krummrich
The existing write_slice() method is a wrapper around copy_to_user() and expects the user buffer to be larger than the source buffer. However, userspace may split up reads in multiple partial operations providing an offset into the source buffer and a smaller user buffer. In order to support this common case, provide a helper for partial writes. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Matthew Maurer <mmaurer@google.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Acked-by: Miguel Ojeda <ojeda@kernel.org> [ Replace map_or() with let-else; use saturating_add(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-05rust: uaccess: add UserSliceReader::read_slice_file()Danilo Krummrich
Add UserSliceReader::read_slice_file(), which is the same as UserSliceReader::read_slice_partial() but updates the given file::Offset by the number of bytes read. This is equivalent to C's `simple_write_to_buffer()` and useful when dealing with file offsets from file operations. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Acked-by: Miguel Ojeda <ojeda@kernel.org> [ Replace saturating_add() with the raw operator and a corresponding OVERFLOW comment. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-05rust: uaccess: add UserSliceReader::read_slice_partial()Danilo Krummrich
The existing read_slice() method is a wrapper around copy_from_user() and expects the user buffer to be larger than the destination buffer. However, userspace may split up writes in multiple partial operations providing an offset into the destination buffer and a smaller user buffer. In order to support this common case, provide a helper for partial reads. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Matthew Maurer <mmaurer@google.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Acked-by: Miguel Ojeda <ojeda@kernel.org> [ Replace map_or() with let-else; use saturating_add(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-05rust: fs: add file::Offset type aliasDanilo Krummrich
Add a type alias for file offsets, i.e. bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64. Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Christian Brauner <brauner@kernel.org> Cc: Jan Kara <jack@suse.cz> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Christian Brauner <brauner@kernel.org> Link: https://patch.msgid.link/20251020222722.240473-2-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-04rust: Add read_poll_timeout_atomic functionFUJITA Tomonori
Add read_poll_timeout_atomic function which polls periodically until a condition is met, an error occurs, or the attempt limit is reached. The C's read_poll_timeout_atomic() is used for the similar purpose. In atomic context the timekeeping infrastructure is unavailable, so reliable time-based timeouts cannot be implemented. So instead, the helper accepts a maximum number of attempts and busy-waits (udelay + cpu_relax) between tries. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Link: https://patch.msgid.link/20251103112958.2961517-3-fujita.tomonori@gmail.com [ Adjust imports to use "kernel vertical" style. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-04rust: add udelay() functionFUJITA Tomonori
Add udelay() function, inserts a delay based on microseconds with busy waiting, in preparation for supporting read_poll_timeout_atomic(). Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://patch.msgid.link/20251103112958.2961517-2-fujita.tomonori@gmail.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-04rust: usb: fix broken call to T::disconnect()Danilo Krummrich
A refactoring of Device::drvdata_obtain() broke T::disconnect() in the USB abstractions. """ error[E0599]: no method named `data` found for struct `core::pin::Pin<kbox::Box<T, Kmalloc>>` in the current scope --> rust/kernel/usb.rs:92:34 | 92 | T::disconnect(intf, data.data()); | ^^^^ method not found in `core::pin::Pin<kbox::Box<T, Kmalloc>>` error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0599`. make[2]: *** [rust/Makefile:553: rust/kernel.o] Error 1 make[1]: *** [/builddir/build/BUILD/kernel-6.18.0-build/kernel-next-20251103/linux-6.18.0-0.0.next.20251103.436.vanilla.fc44.x86_64/Makefile:1316: prepare] Error 2 make: *** [Makefile:256: __sub-make] Error 2 """ This slipped through, since the USB abstractions are globally disabled. However, the USB tree recently enabled them, hence it showed up in linux-next. Reported-by: Thorsten Leemhuis <linux@leemhuis.info> Closes: https://lore.kernel.org/all/1c8afbc0-e888-4702-9e4e-fa8aef0f97ae@leemhuis.info/ Fixes: 6bbaa93912bf ("rust: device: narrow the generic of drvdata_obtain()") Reviewed-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Thorsten Leemhuis <linux@leemhuis.info> Link: https://patch.msgid.link/20251103110115.1925072-1-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-11-04rust: kbuild: workaround `rustdoc` doctests modifier bugMiguel Ojeda
The `rustdoc` modifiers bug [1] was fixed in Rust 1.90.0 [2], for which we added a workaround in commit abbf9a449441 ("rust: workaround `rustdoc` target modifiers bug"). However, `rustdoc`'s doctest generation still has a similar issue [3], being fixed at [4], which does not affect us because we apply the workaround to both, and now, starting with Rust 1.91.0 (released 2025-10-30), `-Zsanitizer` is a target modifier too [5], which means we fail with: RUSTDOC TK rust/kernel/lib.rs error: mixing `-Zsanitizer` will cause an ABI mismatch in crate `kernel` --> rust/kernel/lib.rs:3:1 | 3 | //! The `kernel` crate. | ^ | = help: the `-Zsanitizer` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely = note: unset `-Zsanitizer` in this crate is incompatible with `-Zsanitizer=kernel-address` in dependency `core` = help: set `-Zsanitizer=kernel-address` in this crate or unset `-Zsanitizer` in `core` = help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=sanitizer` to silence this error A simple way around is to add the sanitizer to the list in the existing workaround (especially if we had not started to pass the sanitizer flags in the previous commit, since in that case that would not be necessary). However, that still applies the workaround in more cases than necessary. Instead, only modify the doctests flags to ignore the check for sanitizers, so that it is more local (and thus the compiler keeps checking it for us in the normal `rustdoc` calls). Since the previous commit already treated the `rustdoc` calls as kernel objects, this should allow us in the future to easily remove this workaround when the time comes. By the way, the `-Cunsafe-allow-abi-mismatch` flag overwrites previous ones rather than appending, so it needs to be all done in the same flag. Moreover, unknown modifiers are rejected, and thus we have to gate based on the version too. Finally, `-Zsanitizer-cfi-normalize-integers` is not affected (in Rust 1.91.0), so it is not needed in the workaround for the moment. Cc: stable@vger.kernel.org # Needed in 6.12.y and later (Rust is pinned in older LTSs). Link: https://github.com/rust-lang/rust/issues/144521 [1] Link: https://github.com/rust-lang/rust/pull/144523 [2] Link: https://github.com/rust-lang/rust/issues/146465 [3] Link: https://github.com/rust-lang/rust/pull/148068 [4] Link: https://github.com/rust-lang/rust/pull/138736 [5] Reviewed-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Justin M. Forbes <jforbes@fedoraproject.org> Link: https://patch.msgid.link/20251102212853.1505384-2-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-11-04rust: kbuild: treat `build_error` and `rustdoc` as kernel objectsMiguel Ojeda
Even if normally `build_error` isn't a kernel object, it should still be treated as such so that we pass the same flags. Similarly, `rustdoc` targets are never kernel objects, but we need to treat them as such. Otherwise, starting with Rust 1.91.0 (released 2025-10-30), `rustc` will complain about missing sanitizer flags since `-Zsanitizer` is a target modifier too [1]: error: mixing `-Zsanitizer` will cause an ABI mismatch in crate `build_error` --> rust/build_error.rs:3:1 | 3 | //! Build-time error. | ^ | = help: the `-Zsanitizer` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely = note: unset `-Zsanitizer` in this crate is incompatible with `-Zsanitizer=kernel-address` in dependency `core` = help: set `-Zsanitizer=kernel-address` in this crate or unset `-Zsanitizer` in `core` = help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=sanitizer` to silence this error Thus explicitly mark them as kernel objects. Cc: stable@vger.kernel.org # Needed in 6.12.y and later (Rust is pinned in older LTSs). Link: https://github.com/rust-lang/rust/pull/138736 [1] Reviewed-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Justin M. Forbes <jforbes@fedoraproject.org> Link: https://patch.msgid.link/20251102212853.1505384-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-11-03rust: module: update the module macro with module parameter supportAndreas Hindborg
Allow module parameters to be declared in the rust `module!` macro. Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Tested-by: Daniel Gomez <da.gomez@samsung.com> Signed-off-by: Daniel Gomez <da.gomez@kernel.org>
2025-11-03rust: module: use a reference in macros::module::moduleAndreas Hindborg
When we add parameter support to the module macro, we want to be able to pass a reference to `ModuleInfo` to a helper function. That is not possible when we move out of the local `modinfo`. So change the function to access the local via reference rather than value. Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Tested-by: Daniel Gomez <da.gomez@samsung.com> Signed-off-by: Daniel Gomez <da.gomez@kernel.org>
2025-11-03rust: introduce module_param moduleAndreas Hindborg
Add types and traits for interfacing the C moduleparam API. Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Tested-by: Daniel Gomez <da.gomez@samsung.com> Signed-off-by: Daniel Gomez <da.gomez@kernel.org>
2025-11-03rust: str: add radix prefixed integer parsing functionsAndreas Hindborg
Add the trait `ParseInt` for parsing string representations of integers where the string representations are optionally prefixed by a radix specifier. Implement the trait for the primitive integer types. Suggested-by: Benno Lossin <benno.lossin@proton.me> Tested-by: Daniel Gomez <da.gomez@samsung.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Signed-off-by: Daniel Gomez <da.gomez@kernel.org>
2025-11-03rust: sync: add `SetOnce`Andreas Hindborg
Introduce the `SetOnce` type, a container that can only be written once. The container uses an internal atomic to synchronize writes to the internal value. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Tested-by: Daniel Gomez <da.gomez@samsung.com> Signed-off-by: Daniel Gomez <da.gomez@kernel.org>
2025-11-02rust: condvar: fix broken intra-doc linkMiguel Ojeda
The future move of pin-init to `syn` uncovers the following broken intra-doc link: error: unresolved link to `crate::pin_init` --> rust/kernel/sync/condvar.rs:39:40 | 39 | /// instances is with the [`pin_init`](crate::pin_init!) and [`new_condvar`] macros. | ^^^^^^^^^^^^^^^^ no item named `pin_init` in module `kernel` | = note: `-D rustdoc::broken-intra-doc-links` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(rustdoc::broken_intra_doc_links)]` Currently, when rendered, the link points to a literal `crate::pin_init!` URL. Thus fix it. Cc: stable@vger.kernel.org Fixes: 129e97be8e28 ("rust: pin-init: fix documentation links") Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://patch.msgid.link/20251029073344.349341-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-11-02rust: devres: fix private intra-doc linkMiguel Ojeda
The future move of pin-init to `syn` uncovers the following private intra-doc link: error: public documentation for `Devres` links to private item `Self::inner` --> rust/kernel/devres.rs:106:7 | 106 | /// [`Self::inner`] is guaranteed to be initialized and is always accessed read-only. | ^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `-D rustdoc::private-intra-doc-links` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(rustdoc::private_intra_doc_links)]` Currently, when rendered, the link points to "nowhere" (an inexistent anchor for a "method"). Thus fix it. Cc: stable@vger.kernel.org Fixes: f5d3ef25d238 ("rust: devres: get rid of Devres' inner Arc") Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20251029071406.324511-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-29rust: auxiliary: implement parent() for Device<Bound>Danilo Krummrich
Take advantage of the fact that if the auxiliary device is bound the parent is guaranteed to be bound as well and implement a separate parent() method for auxiliary::Device<Bound>. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-29rust: auxiliary: move parent() to impl DeviceDanilo Krummrich
Currently, the parent method is implemented for any Device<Ctx>, i.e. any device context and returns a &device::Device<Normal>. However, a subsequent patch will introduce impl Device<Bound> { pub fn parent() -> device::Device<Bound> { ... } } which takes advantage of the fact that if the auxiliary device is bound the parent is guaranteed to be bound as well. I.e. the behavior we want is that all device contexts that dereference to Bound, will use the implementation above, whereas the old implementation should only be implemented for Device<Normal>. Hence, move the current implementation. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-29rust: auxiliary: unregister on parent device unbindDanilo Krummrich
Guarantee that an auxiliary driver will be unbound before its parent is unbound; there is no point in operating an auxiliary device whose parent has been unbound. In practice, this guarantee allows us to assume that for a bound auxiliary device, also the parent device is bound. This is useful when an auxiliary driver calls into its parent, since it allows the parent to directly access device resources and its device private data due to the guaranteed bound device context. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-29rust: auxiliary: consider auxiliary devices always have a parentDanilo Krummrich
An auxiliary device is guaranteed to always have a parent device (both in C and Rust), hence don't return an Option<&auxiliary::Device> in auxiliary::Device::parent(). Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-29rust: device: introduce Device::drvdata()Danilo Krummrich
In C dev_get_drvdata() has specific requirements under which it is valid to access the returned pointer. That is, drivers have to ensure that (1) for the duration the returned pointer is accessed the driver is bound and remains to be bound to the corresponding device, (2) the returned void * is treated according to the driver's private data type, i.e. according to what has been passed to dev_set_drvdata(). In Rust, (1) can be ensured by simply requiring the Bound device context, i.e. provide the drvdata() method for Device<Bound> only. For (2) we would usually make the device type generic over the driver type, e.g. Device<T: Driver>, where <T as Driver>::Data is the type of the driver's private data. However, a device does not have a driver type known at compile time and may be bound to multiple drivers throughout its lifetime. Hence, in order to be able to provide a safe accessor for the driver's device private data, we have to do the type check on runtime. This is achieved by letting a driver assert the expected type, which is then compared to a type hash stored in struct device_private when dev_set_drvdata() is called. Example: // `dev` is a `&Device<Bound>`. let data = dev.drvdata::<SampleDriver>()?; There are two aspects to note: (1) Technically, the same check could be achieved by comparing the struct device_driver pointer of struct device with the struct device_driver pointer of the driver struct (e.g. struct pci_driver). However, this would - in addition the pointer comparison - require to tie back the private driver data type to the struct device_driver pointer of the driver struct to prove correctness. Besides that, accessing the driver struct (stored in the module structure) isn't trivial and would result into horrible code and API ergonomics. (2) Having a direct accessor to the driver's private data is not commonly required (at least in Rust): Bus callback methods already provide access to the driver's device private data through a &self argument, while other driver entry points such as IRQs, workqueues, timers, IOCTLs, etc. have their own private data with separate ownership and lifetime. In other words, a driver's device private data is only relevant for driver model contexts (such a file private is only relevant for file contexts). Having that said, the motivation for accessing the driver's device private data with Device<Bound>::drvdata() are interactions between drivers. For instance, when an auxiliary driver calls back into its parent, the parent has to be capable to derive its private data from the corresponding device (i.e. the parent of the auxiliary device). Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> [ * Remove unnecessary `const _: ()` block, * rename type_id_{store,match}() to {set,match}_type_id(), * assert size_of::<bindings::driver_type>() >= size_of::<TypeId>(), * add missing check in case Device::drvdata() is called from probe(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-29rust: device: narrow the generic of drvdata_obtain()Danilo Krummrich
Let T be the actual private driver data type without the surrounding box, as it leaves less room for potential bugs. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-28rust: usb: fix formattingMiguel Ojeda
We do our best to keep the repository `rustfmt`-clean, thus run the tool to fix the formatting issue. Link: https://docs.kernel.org/rust/coding-guidelines.html#style-formatting Link: https://rust-for-linux.com/contributing#submit-checklist-addendum Fixes: 9a55e0079258 ("Revert "USB: disable rust bindings from the build for now"") Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Link: https://patch.msgid.link/20251016231350.1418501-1-ojeda@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-10-27rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`Siyuan Huang
All types in `bindings` implement `Zeroable` if they can, so use `pin_init::zeroed` instead of relying on `unsafe` code. If this ends up not compiling in the future, something in bindgen or on the C side changed and is most likely incorrect. Link: https://github.com/Rust-for-Linux/linux/issues/1189 Suggested-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Acked-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Kunwu Chan <chentao@kylinos.cn> Link: https://patch.msgid.link/20251020031204.78917-1-huangsiyuan@kylinos.cn Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2025-10-27Merge 6.18-rc3 into usb-nextGreg Kroah-Hartman
We need the USB fixes in here as well to build on top of. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-10-27Merge 6.18-rc3 into driver-core-nextGreg Kroah-Hartman
We need the driver core fixes in here as well to build on top of. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-10-26rust: simplify read_poll_timeout's example codeFUJITA Tomonori
- Drop unnecessary Result's '<()>' - Use '?' instead of match Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-25Merge tag 'driver-core-6.18-rc3' of ↵Linus Torvalds
git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core Pull driver core fixes from Danilo Krummrich: - In Device::parent(), do not make any assumptions on the device context of the parent device - Check visibility before changing ownership of a sysfs attribute group - In topology_parse_cpu_capacity(), replace an incorrect usage of PTR_ERR_OR_ZERO() with IS_ERR_OR_NULL() - In devcoredump, fix a circular locking dependency between struct devcd_entry::mutex and kernfs - Do not warn about a pending fw_devlink sync state * tag 'driver-core-6.18-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core: arch_topology: Fix incorrect error check in topology_parse_cpu_capacity() rust: device: fix device context of Device::parent() sysfs: check visibility before changing group attribute ownership devcoredump: Fix circular locking dependency with devcd->mutex. driver core: fw_devlink: Don't warn about sync_state() pending
2025-10-23rust: drm/gem: Remove Object.devLyude Paul
I noticed by chance that there's actually already a pointer to this in struct drm_gem_object. So, no use in carrying this around! Signed-off-by: Lyude Paul <lyude@redhat.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20251021172220.252558-1-lyude@redhat.com
2025-10-23rust: pci: normalise spelling of PCI BARPeter Colberg
Consistently refer to PCI base address register as PCI BAR. Fix spelling mistake "Mapps" -> "Maps". Link: https://lore.kernel.org/rust-for-linux/20251015225827.GA960157@bhelgaas/ Link: https://github.com/Rust-for-Linux/linux/issues/1196 Suggested-by: Bjorn Helgaas <helgaas@kernel.org> Signed-off-by: Peter Colberg <pcolberg@redhat.com> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-23rust: pci: refer to legacy as INTx interruptsPeter Colberg
Consistently use INTx, as in the description of IrqType::Intx, to refer to the four legacy PCI interrupts, INTA#, INTB#, INTC#, and INTD#. Link: https://lore.kernel.org/rust-for-linux/20251015230209.GA960343@bhelgaas/ Link: https://github.com/Rust-for-Linux/linux/issues/1196 Suggested-by: Bjorn Helgaas <helgaas@kernel.org> Signed-off-by: Peter Colberg <pcolberg@redhat.com> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-23rust: opp: simplify callers of `to_c_str_array`Tamir Duberstein
Use `Option` combinators to make this a bit less noisy. Wrap the `dev_pm_opp_set_config` operation in a closure and use type ascription to leverage the compiler to check for use after free. Signed-off-by: Tamir Duberstein <tamird@kernel.org> Tested-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2025-10-22rust: debugfs: Implement Reader for Mutex<T> only when T is UnpinBoqun Feng
Since we are going to make `Mutex<T>` structurally pin the data (i.e. `T`), therefore `.lock()` function only returns a `Guard` that can dereference a mutable reference to `T` if only `T` is `Unpin`, therefore restrict the impl `Reader` block of `Mutex<T>` to that. Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20251022034237.70431-1-boqun.feng@gmail.com
2025-10-22rust: replace `CStr` with `core::ffi::CStr`Tamir Duberstein
`kernel::ffi::CStr` was introduced in commit d126d2380131 ("rust: str: add `CStr` type") in November 2022 as an upstreaming of earlier work that was done in May 2021[0]. That earlier work, having predated the inclusion of `CStr` in `core`, largely duplicated the implementation of `std::ffi::CStr`. `std::ffi::CStr` was moved to `core::ffi::CStr` in Rust 1.64 in September 2022. Hence replace `kernel::str::CStr` with `core::ffi::CStr` to reduce our custom code footprint, and retain needed custom functionality through an extension trait. Add `CStr` to `ffi` and the kernel prelude. Link: https://github.com/Rust-for-Linux/linux/commit/faa3cbcca03d0dec8f8e43f1d8d5c0860d98a23f [0] Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://patch.msgid.link/20251018-cstr-core-v18-16-9378a54385f8@gmail.com [ Removed assert that would now depend on the Rust version. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: support formatting of foreign typesTamir Duberstein
Introduce a `fmt!` macro which wraps all arguments in `kernel::fmt::Adapter` and a `kernel::fmt::Display` trait. This enables formatting of foreign types (like `core::ffi::CStr`) that do not implement `core::fmt::Display` due to concerns around lossy conversions which do not apply in the kernel. Suggested-by: Alice Ryhl <aliceryhl@google.com> Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Custom.20formatting/with/516476467 Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://patch.msgid.link/20251018-cstr-core-v18-15-9378a54385f8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: clk: use `CStr::as_char_ptr`Tamir Duberstein
Replace the use of `as_ptr` which works through `<CStr as Deref<Target=&[u8]>::deref()` in preparation for replacing `kernel::str::CStr` with `core::ffi::CStr` as the latter does not implement `Deref<Target=&[u8]>`. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Link: https://patch.msgid.link/20251018-cstr-core-v18-14-9378a54385f8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: regulator: use `CStr::as_char_ptr`Tamir Duberstein
Replace the use of `as_ptr` which works through `<CStr as Deref<Target=&[u8]>::deref()` in preparation for replacing `kernel::str::CStr` with `core::ffi::CStr` as the latter does not implement `Deref<Target=&[u8]>`. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: Mark Brown <broonie@kernel.org> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Link: https://patch.msgid.link/20251018-cstr-core-v18-13-9378a54385f8@gmail.com [ Move safety comment below to support older Clippy. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: configfs: use `CStr::as_char_ptr`Tamir Duberstein
Replace the use of `as_ptr` which works through `<CStr as Deref<Target=&[u8]>::deref()` in preparation for replacing `kernel::str::CStr` with `core::ffi::CStr` as the latter does not implement `Deref<Target=&[u8]>`. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://patch.msgid.link/20251018-cstr-core-v18-12-9378a54385f8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: opp: use `CStr::as_char_ptr`Tamir Duberstein
Replace the use of `as_ptr` which works through `<CStr as Deref<Target=&[u8]>::deref()` in preparation for replacing `kernel::str::CStr` with `core::ffi::CStr` as the latter does not implement `Deref<Target=&[u8]>`. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Link: https://patch.msgid.link/20251018-cstr-core-v18-10-9378a54385f8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2025-10-22rust: opp: fix broken rustdoc linkTamir Duberstein
Correct the spelling of "CString" to make the link work. Fixes: ce32e2d47ce6 ("rust: opp: Add abstractions for the configuration options") Signed-off-by: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2025-10-21Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem ↵Lyude Paul
objects automatically" Currently in order to implement AlwaysRefCounted for gem objects, we use a blanket implementation: unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T { … } While this technically works, it comes with the rather unfortunate downside that attempting to create a similar blanket implementation in any other kernel crate will now fail in a rather confusing way. Using an example from the (not yet upstream) rust DRM KMS bindings, if we were to add: unsafe impl<T: RcModeObject> AlwaysRefCounted for T { … } Then the moment that both blanket implementations are present in the same kernel tree, compilation fails with the following: error[E0119]: conflicting implementations of trait `types::AlwaysRefCounted` --> rust/kernel/drm/kms.rs:504:1 | 504 | unsafe impl<T: RcModeObject> AlwaysRefCounted for T { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation | ::: rust/kernel/drm/gem/mod.rs:97:1 | 97 | unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T { | ---------------------------------------------------- first implementation here So, revert these changes for now. The proper fix for this is to introduce a macro for copy/pasting the same implementation of AlwaysRefCounted around. This reverts commit 38cb08c3fcd3f3b1d0225dcec8ae50fab5751549. Signed-off-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20251016210955.2813186-2-lyude@redhat.com
2025-10-21rust: driver: let probe() return impl PinInit<Self, Error>Danilo Krummrich
The driver model defines the lifetime of the private data stored in (and owned by) a bus device to be valid from when the driver is bound to a device (i.e. from successful probe()) until the driver is unbound from the device. This is already taken care of by the Rust implementation of the driver model. However, we still ask drivers to return a Result<Pin<KBox<Self>>> from probe(). Unlike in C, where we do not have the concept of initializers, but rather deal with uninitialized memory, drivers can just return an impl PinInit<Self, Error> instead. This contributes to more clarity to the fact that a driver returns it's device private data in probe() and the Rust driver model owns the data, manages the lifetime and - considering the lifetime - provides (safe) accessors for the driver. Hence, let probe() functions return an impl PinInit<Self, Error> instead of Result<Pin<KBox<Self>>>. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-21rust: lock: Add a Pin<&mut T> accessorDaniel Almeida
In order for callers to be able to access the inner T safely if T: !Unpin, there needs to be a way to get a Pin<&mut T>. Add this accessor and a corresponding example to tell users how it works. This requires the pin projection functionality [1] for better ergonomic. [boqun: Apply Daniel's fix to the code example, add the reference to pin projection patch and remove out-of-date part in the commit log] Suggested-by: Benno Lossin <lossin@kernel.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://github.com/Rust-for-Linux/linux/issues/1181 Link: https://lore.kernel.org/rust-for-linux/20250912174148.373530-1-lossin@kernel.org/ [1]
2025-10-21rust: lock: Pin the inner dataDaniel Almeida
In preparation to support Lock<T> where T is pinned, the first thing that needs to be done is to structurally pin the 'data' member. This switches the 't' parameter in Lock<T>::new() to take in an impl PinInit<T> instead of a plain T. This in turn uses the blanket implementation "impl PinInit<T> for T". Subsequent patches will touch on Guard<T>. Suggested-by: Benno Lossin <lossin@kernel.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://github.com/Rust-for-Linux/linux/issues/1181
2025-10-21rust: lock: guard: Add T: Unpin bound to DerefMutDaniel Almeida
A core property of pinned types is not handing a mutable reference to the inner data in safe code, as this trivially allows that data to be moved. Enforce this condition by adding a bound on lock::Guard's DerefMut implementation, so that it's only implemented for pinning-agnostic types. Suggested-by: Benno Lossin <lossin@kernel.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://github.com/Rust-for-Linux/linux/issues/1181
2025-10-21add `[pin_]init_scope` to execute code before creating an initializerBenno Lossin
In more complex cases, initializers need to run arbitrary code before assigning initializers to fields. While this is possible using the underscore codeblock feature (`_: {}`), values returned by such functions cannot be used from later field initializers. The two new functions `[pin_]init_scope` allow users to first run some fallible code and then return an initializer which the function turns into a single initializer. This permits using the same value multiple times by different fields. Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> [ Fix typo in commit message: s/functinos/functions/. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-20rust: pci: move IRQ infrastructure to separate fileDanilo Krummrich
Move the PCI interrupt infrastructure to a separate sub-module in order to keep things organized. Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-20rust: pci: move I/O infrastructure to separate fileDanilo Krummrich
Move the PCI I/O infrastructure to a separate sub-module in order to keep things organized. Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-20rust: pci: implement TryInto<IrqRequest<'a>> for IrqVector<'a>Danilo Krummrich
Implement TryInto<IrqRequest<'a>> for IrqVector<'a> to directly convert a pci::IrqVector into a generic IrqRequest, instead of taking the indirection via an unrelated pci::Device method. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-10-20rust: remove spurious `use core::fmt::Debug`Tamir Duberstein
We want folks to use `kernel::fmt` but this is only used for `derive` so can be removed entirely. This backslid in commit ea60cea07d8c ("rust: add `Alignment` type"). Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://patch.msgid.link/20251018-cstr-core-v18-9-9378a54385f8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>