From nobody Thu Apr 9 10:52:49 2026 Received: from forward205b.mail.yandex.net (forward205b.mail.yandex.net [178.154.239.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C668E2EBDD0 for ; Mon, 9 Mar 2026 21:01:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773090082; cv=none; b=u2vn8zVerJuY7DY+kNqUj7UxXQtOQfQmr6d7Q58oWL6ewxjldzY7vLlrrbes4NloYKl4ja9kxIQcGxXOUJfaYUoQTb8l9HaI0ghStXUCgKMI3mk4YjXMBFAXZx1U8xpH2E99FQ6IcCwQZ36n5jzq6d4SfiwTStgxP9sh9t6I+dw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773090082; c=relaxed/simple; bh=l2zVABx0ukHupTK6cqX0MYpdDRqgh2ztr5w6jsiRguQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=i6HXmo+68gomh+UVCJiatZSTSu3YE07IfgGZYEkZt7bsj7J6dy2UHtCJlqUeRDOeN/37mMh9W74jRfjzTxtntP4IBOjjvr0vveBXbxxs/lNjXyJ1ahktbeICyP4SQTajuuLrtECVH7ONAQYU1IeVSDJ0/9st5r2533bZqMWFVW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=SQ+Auh5r; arc=none smtp.client-ip=178.154.239.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="SQ+Auh5r" Received: from forward101b.mail.yandex.net (forward101b.mail.yandex.net [IPv6:2a02:6b8:c02:900:1:45:d181:d101]) by forward205b.mail.yandex.net (Yandex) with ESMTPS id B0A138616D for ; Mon, 09 Mar 2026 23:55:06 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-89.sas.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-89.sas.yp-c.yandex.net [IPv6:2a02:6b8:c08:ec27:0:640:5ac1:0]) by forward101b.mail.yandex.net (Yandex) with ESMTPS id 43580C0079; Mon, 09 Mar 2026 23:54:58 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-89.sas.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id DsL9phmGm0U0-UTh50ljr; Mon, 09 Mar 2026 23:54:57 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1773089697; bh=tdqZdIhYVneZTBKoqZpd0WXEPrwKrA6bsUPN0bxfmEQ=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=SQ+Auh5rZLuuOOgWcnB13A0f7pd3PJ5QPijAbhz3npDInU5pBXiBRMIIeNUDBuE42 exswIHxI70WWx+qa2N4ZndZiuSDUuaAcJvrfXT4UI5nmj6Ooqly3rLoVtOqMqxWM1j o0msEhF0R+MFo7BR/wK+Yr+OaEzSx9GJi6xt2H3c= Authentication-Results: mail-nwsmtp-smtp-production-main-89.sas.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: linux-kernel@vger.kernel.org Cc: dakr@kernel.org, aliceryhl@google.com, daniel.almeida@collabora.com, airlied@gmail.com, simona@ffwll.ch, =?UTF-8?q?Onur=20=C3=96zkan?= , Deborah Brouwer Subject: [PATCH v1 3/4] rust: add ordered workqueue wrapper Date: Mon, 9 Mar 2026 23:53:18 +0300 Message-ID: <20260309205403.34390-4-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260309205403.34390-1-work@onurozkan.dev> References: <20260309205403.34390-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add an owned OrderedQueue wrapper for alloc_ordered_workqueue() and destroy_workqueue(). This gives Rust drivers a simple way to create and own an ordered workqueue with automatic cleanup in Drop. Tested-by: Deborah Brouwer Signed-off-by: Onur =C3=96zkan --- rust/helpers/workqueue.c | 6 +++++ rust/kernel/workqueue.rs | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/rust/helpers/workqueue.c b/rust/helpers/workqueue.c index ce1c3a5b2150..7cd3b000a5b6 100644 --- a/rust/helpers/workqueue.c +++ b/rust/helpers/workqueue.c @@ -14,3 +14,9 @@ __rust_helper void rust_helper_init_work_with_key(struct = work_struct *work, INIT_LIST_HEAD(&work->entry); work->func =3D func; } + +__rust_helper struct workqueue_struct * +rust_helper_alloc_ordered_workqueue(const char *name, unsigned int flags) +{ + return alloc_ordered_workqueue("%s", flags, name); +} diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 6acc7b5ba31c..d5aa61a5ef93 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -195,6 +195,7 @@ types::Opaque, }; use core::marker::PhantomData; +use core::ptr::NonNull; =20 /// Creates a [`Work`] initialiser with the given name and a newly-created= lock class. #[macro_export] @@ -346,6 +347,52 @@ pub fn try_spawn( } } =20 +/// A kernel work queue that allocates and owns an ordered `workqueue_stru= ct`. +/// +/// Unlike [`Queue`], [`OrderedQueue`] takes ownership of the underlying C +/// workqueue and automatically destroys it when dropped. +pub struct OrderedQueue(NonNull); + +// SAFETY: Workqueue objects are thread-safe to share and use concurrently. +unsafe impl Send for OrderedQueue {} +// SAFETY: Workqueue objects are thread-safe to share and use concurrently. +unsafe impl Sync for OrderedQueue {} + +impl OrderedQueue { + /// Allocates an ordered workqueue. + /// + /// It is equivalent to C's `alloc_ordered_workqueue()`. + pub fn new(name: &'static CStr, flags: u32) -> Result { + // SAFETY: `name` is a `&'static CStr`, guaranteeing a valid, null= -terminated C + // string pointer for the duration of this call. + let ptr =3D unsafe { bindings::alloc_ordered_workqueue(name.as_cha= r_ptr(), flags) }; + let ptr =3D NonNull::new(ptr).ok_or(ENOMEM)?; + Ok(Self(ptr)) + } + + /// Enqueues a work item. + /// + /// This may fail if the work item is already enqueued in a workqueue. + /// + /// The work item will be submitted using `WORK_CPU_UNBOUND`. + pub fn enqueue(&self, w: W) -> W::EnqueueOutput + where + W: RawWorkItem + Send + 'static, + { + // SAFETY: `self.0` is valid while `self` is alive. + unsafe { Queue::from_raw(self.0.as_ptr()) }.enqueue(w) + } +} + +impl Drop for OrderedQueue { + fn drop(&mut self) { + // SAFETY: + // - Pointer comes from `alloc_ordered_workqueue()` and is owned b= y `self`. + // - `OrderedQueue` does not expose delayed scheduling API. + unsafe { bindings::destroy_workqueue(self.0.as_ptr()) }; + } +} + /// A helper type used in [`try_spawn`]. /// /// [`try_spawn`]: Queue::try_spawn --=20 2.51.2