Skip to contentSkip to conversation

rust: workqueue: add ScopedQueue for lifetime bound items

PATCH In review Onur Özkan 2 participants 3 messages Jun 17 Rust

lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

v2 cover rust: workqueue: add ScopedQueue for lifetime bound items

Changes since v1:
- Removed *with_lt namings.
- Detailed safety comment inside ScopedQueue::enqueue.
- Added Suggested-by tag.

v1: https://lore.kernel.org/all/[email protected]

Onur Özkan (1):
rust: workqueue: add ScopedQueue for lifetime bound items

 rust/kernel/workqueue/mod.rs          |   3 +
 rust/kernel/workqueue/scoped_queue.rs | 187 ++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100644 rust/kernel/workqueue/scoped_queue.rs
Signature · 1 line
2.51.2
author reviewer
#1 lore raw

PATCH v2 1/1 rust: workqueue: add ScopedQueue for lifetime bound items

Add a workqueue wrapper for work items that are not 'static.

Tyr reset work is queued from a handle that owns a Controller<'bound> where the work item holds references tied to the lifetime of the bound device and its mapped IO state. The existing API only accepts 'static work items which cannot express that relationship.

Introduce ScopedQueue for this case. It owns the underlying workqueue and ties enqueued work to the queue lifetime so borrowed state cannot outlive the queue that may still run it.

Construction is unsafe because the queue must not be leaked.

compile_fail doc-tests are ignored for now as KUnit doesn't support that. Enabling those tests as regular code block would raise this error:

    ERROR:root:error[E0597]: `data` does not live long enough
	 --> rust/doctests_kernel_generated.rs:22029:44
	  |
    22027 |         let data = ();
	  |             ---- binding `data` declared here
    22028 |         // SAFETY: Queue is not leaked.
    22029 |         queue = unsafe { new_queue(&data)? };
	  |                                            ^^^^^ borrowed value does not live long enough
    22030 |     }
	  |     - `data` dropped here while still borrowed
    ...
    22034 | }
	  | - borrow might be used here, when `queue` is dropped and runs the `Drop` code for type `ScopedQueue`
	  |
	  = note: values in a scope are dropped in the opposite order they are defined

which is exactly the constraint ScopedQueue is meant to enforce.

This series is based on Alice's "Creation of workqueues in Rust" [1] series.

---
 rust/kernel/workqueue/mod.rs          |   3 +
 rust/kernel/workqueue/scoped_queue.rs | 187 ++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100644 rust/kernel/workqueue/scoped_queue.rs
rust/kernel/workqueue/mod.rs +3 -0
index e30c21214a81..5e2baa84c143 100644
@@ -212,6 +212,9 @@
 mod builder;
 pub use self::builder::Builder;
 
+mod scoped_queue;
+pub use self::scoped_queue::ScopedQueue;
+
 /// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
 #[macro_export]
 macro_rules! new_work {
rust/kernel/workqueue/scoped_queue.rs new file +187 -0

187 lines not on this page · raw message

Signature · 1 line
2.51.2
reviewer Reviewed-by on v2 1/1
#2 lore raw
Quoted 32 lines · Onur Özkan

Add a workqueue wrapper for work items that are not 'static.

Tyr reset work is queued from a handle that owns a Controller<'bound> where the work item holds references tied to the lifetime of the bound device and its mapped IO state. The existing API only accepts 'static work items which cannot express that relationship.

Introduce ScopedQueue for this case. It owns the underlying workqueue and ties enqueued work to the queue lifetime so borrowed state cannot outlive the queue that may still run it.

Construction is unsafe because the queue must not be leaked.

compile_fail doc-tests are ignored for now as KUnit doesn't support that. Enabling those tests as regular code block would raise this error:

    ERROR:root:error[E0597]: `data` does not live long enough
	 --> rust/doctests_kernel_generated.rs:22029:44
	  |
    22027 |         let data = ();
	  |             ---- binding `data` declared here
    22028 |         // SAFETY: Queue is not leaked.
    22029 |         queue = unsafe { new_queue(&data)? };
	  |                                            ^^^^^ borrowed value does not live long enough
    22030 |     }
	  |     - `data` dropped here while still borrowed
    ...
    22034 | }
	  | - borrow might be used here, when `queue` is dropped and runs the `Drop` code for type `ScopedQueue`
	  |
	  = note: values in a scope are dropped in the opposite order they are defined
Quoted 8 lines

which is exactly the constraint ScopedQueue is meant to enforce.

This series is based on Alice's "Creation of workqueues in Rust" [1] series.