Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B4AD47605A for ; Fri, 24 Jul 2026 23:38:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784936327; cv=none; b=NTHCL5Wk+7YqLEmiL2IuuRnQOEDrvEeosbs+ZBkgWLPeTlYohPLqt7xWTggEQXu9dthTOWuAlxt2iQTyFFHVyzl8ohG7V2xElj6AD+hTJvbRjz3RMWNoY3xshsl1WPcSt6RgnUEdQGppn9mdPfFWc91kPr3O5uTIaUkC6EGU3OY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784936327; c=relaxed/simple; bh=52qK/YudMp2ThqOHUaB4gro4ZauViPO4HJFIpihKe48=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MDLxsrqHnATPJagkFz1JdCU0bQ2X0Brric8Sn3hOIF8t5x8No5FwiG5D7z2DyACeSRFXLclk5wkshgoPc1kyT1N8iI9CXQnuQ1jvhbs+8dnM7UOlqjUon62L8GJvXegwT6UpzbrqQRy9q3y0SJAF+vYAtssiiHux0R27V0s8xZg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CugTZW0m; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CugTZW0m" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-495437bb891so8688605e9.1 for ; Fri, 24 Jul 2026 16:38:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784936323; x=1785541123; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=38rdguqgiGdbvLnKonEzw8VooCAv+0ry5WIpDB9PR/0=; b=CugTZW0mV18Ctv+vlq9x7ykIAoIBxAuSGC0pGIdHcRNmhIzYHrYqNryisxz2f4oZCX J3hyKl/iHf8vvlHjAlJ9vQ+eM/S1KaApAzWqkmdOH6Cnk3W+sbQmfIN3DQps4tZphw5q 70jw0I1SdTOFk7nBf7+4ax9O70ZPl6Fc0/KAeQDsG+JajOjXVLl2NZkGNNFM6E3yhiLa VSYqiuxcgtbkje53NBxbwuv10xOOQ6Aj5q91YtzClRn6YSedpxPmD2pvBhnU2CwFlxoE xqtD7NthRrPr7Jl6OZtAU1u9djkAJX3Fn1d8tdymBUxKlnpNAKMmeK4liUn+O1GiNhFA 12nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784936323; x=1785541123; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=38rdguqgiGdbvLnKonEzw8VooCAv+0ry5WIpDB9PR/0=; b=aiB9v55jD21Vv8tPRPJI7D0s4tWpolvUnM3sWffQ9m7aq105R5JFSXguhO92pM58n7 bWpQ/Oa1ASzxG9VZoEXGuU/6BGKZtHdbc9voHMFLopFKfr260nk83oHk+0mXrWq3KHyQ BfxELGBUrwQZYHnXk2cjSGlczm7rVpNNYDDleCR7fTMEHTLhenTBQ1bfS+tFs4TBk562 kgMe5NXDe/6wFMYC4TF1ZgH5dioKln7oFVEgdBatCBPpUzwjdGoJUluqv+TgfiByEaIj Us/6Ad8HX16pEOsXSpKXWLq+ejRxTQfJlJN1GMqu9VxPgv71sG+0REvDTlWWwr+748ed ZX8g== X-Forwarded-Encrypted: i=1; AHgh+Rqb9qO82nLot8J2XYYRGGSPZAYX4b4ERy0vbUhYmuH8xILM84htDO8pOyxXvHjHHSRDRBGtkrzclvUP310=@vger.kernel.org X-Gm-Message-State: AOJu0YwS34poFW8LB6OkF2pgyHukRT/GWJr410x2TZ3DIoe0My1jmaLw 3eDMbHmxjpHmYszW1bwxqqfvIQtqE9kb7lF2nxvSszs5fbo1xkQ2g3wk X-Gm-Gg: AR+sD1076HOGLdWbiv3/C3bAP5JYu3DHr3pKv4lUBdPP6c3L28bI+LR02FNDrVpOMAk UWhWyO/5gnepHSUfn1b/m+NcZvb1LfYC+6D393fIqhp/mHVPwcXFmOFAWHdD0R4NmmRQmkwOp9t 1j/2EccWJtFtIMzetL3PAmNRo6cp4et68fx8au2ZmZN4J9/a8tzilCf37uw3z7MmC3uh0MXPu7m B4B/HKV1S6O6We53R6Os8YIpZrxhMzZH4t6ktAd2gOHsehqWJ77VyZjgoxayssuKfj+9UCVtOtP qmZwUJG3KzbM6CPEwUZllSjWdEdFK3hrpsBdLkUAzRClDmYbtLM2G3e/FqYrDps2nqCXMIE0t+K WNE4JdAxBoCGIEFqZ2DWK4hQveoiJ3Z1gKXKQi6V2saemh58pSxujy1TYD8fymIsjIvifl5ZAyF R6Y+b8aol077OBfVNDhdc0RtkPiBzhukwwFSeStOt/+Nb6g3WSK2sAAAYIprO1fIOoQdQ= X-Received: by 2002:a05:600c:4f09:b0:495:3a52:71b1 with SMTP id 5b1f17b1804b1-496b5b438f3mr2190285e9.5.1784936323508; Fri, 24 Jul 2026 16:38:43 -0700 (PDT) Received: from Cian.taile2c8f4.ts.net ([2a02:8084:d03:fa00:819d:8e1d:c966:142d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4957b5f35a1sm72226775e9.0.2026.07.24.16.38.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2026 16:38:43 -0700 (PDT) From: Cian McGuire To: Miguel Ojeda Cc: Boqun Feng , Gary Guo , Alice Ryhl , Lyude Paul , Daniel Almeida , =?UTF-8?q?Onur=20=C3=96zkan?= , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Alexandre Courbot , FUJITA Tomonori , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Cian McGuire Subject: [PATCH v2 4/4] rust: workqueue: document safety of Pin> work item impls Date: Sat, 25 Jul 2026 00:38:25 +0100 Message-ID: <20260724233825.1321247-4-cianmcguire167@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260724233825.1321247-1-cianmcguire167@gmail.com> References: <20260724233825.1321247-1-cianmcguire167@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The `WorkItemPointer` and `RawWorkItem` impls for `Pin>` had their SAFETY comments left as "TODO.". Both traits are already implemented for `Arc` earlier in this file with real justifications; these two impls follow the same shape, adapted for `KBox`. `WorkItemPointer`'s contract requires that `__enqueue` use a `work_struct` initialized with this impl's `run` as the function pointer. This holds regardless of which smart pointer type is involved: the only safe ways to produce that `work_struct` are `Work::new` and `DelayedWork::new` (reached via `HasDelayedWork`'s `raw_as_work`), and both are generic over any `T: WorkItem` and always bake in `T::Pointer::run`, so for a `T` bound by `WorkItem` either one bakes in exactly this impl's `run`. The argument is identical to the existing `Arc` comment and does not depend on anything Arc-specific. `RawWorkItem`'s contract requires that pointers passed to the `queue_work_on` closure remain valid for the duration `__enqueue` guarantees. This one is not a direct copy of the `Arc` case, because the two `__enqueue` implementations differ: `__enqueue` here leaks the box via `KBox::into_raw` before calling the closure, so the pointer is valid for the call. If the closure returns true, the pointer remains valid until reclaimed in `WorkItemPointer::run` via a matching `KBox::from_raw`. If the closure returns false, `__enqueue`'s own safety requirement only permits that when the `work_struct` is already enqueued elsewhere -- but `self` is an exclusively-owned `Pin>`, and ownership is only ever given up by enqueuing it, so it cannot already be enqueued while this call still holds it. That branch is therefore genuinely unreachable, matching the `unreachable_unchecked` call. Suggested-by: Miguel Ojeda Link: https://github.com/Rust-for-Linux/linux/issues/351 Signed-off-by: Cian McGuire --- v2: - The `WorkItemPointer` comment claimed `Work::new` was the only safe way to construct the backing `work_struct`; it now also credits `DelayedWork::new` (via `HasDelayedWork`'s `raw_as_work`) as a second valid path, and updates the closing sentence to match. rust/kernel/workqueue.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 7e253b6f299c..2a6bf0b9b83c 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -881,7 +881,16 @@ unsafe impl RawDelayedWorkItem for Arc { } -// SAFETY: TODO. +// SAFETY: The `__enqueue` implementation in `RawWorkItem` uses a `work_struct` initialized +// with the `run` method of this trait as the function pointer because: +// - `__enqueue` gets the `work_struct` from the `Work` field, using `T::raw_get_work`. +// - The only safe ways to produce that `work_struct` are `Work::new` and `DelayedWork::new` +// (via `HasDelayedWork`'s `raw_as_work`), and both pass `T::Pointer::run` to +// `init_work_with_key`. +// - Finally `Work` and `RawWorkItem` guarantee that the correct `Work` field will be used +// because of the ID const generic bound. This makes sure that `T::raw_get_work` uses the +// correct offset for the `Work` field, and the matching constructor picks the correct +// implementation of `WorkItemPointer` for `Pin>`. unsafe impl WorkItemPointer for Pin> where T: WorkItem, @@ -901,7 +910,14 @@ unsafe impl WorkItemPointer for Pin> } } -// SAFETY: TODO. +// SAFETY: `__enqueue` leaks the box via `KBox::into_raw` before calling `queue_work_on`, so +// the pointer is valid for the duration of that call. If the closure returns true, the +// pointer remains valid until reclaimed in `WorkItemPointer::run`, which reconstructs the box +// with a matching `KBox::from_raw`. If the closure returns false, `__enqueue`'s own safety +// requirement only permits that when the `work_struct` is already in a workqueue -- but +// `self` is an exclusively-owned `Pin>`, and ownership is only ever given up by +// enqueuing it, so it cannot already be enqueued elsewhere while we still hold it here. That +// branch is therefore unreachable, matching the `unreachable_unchecked` call. unsafe impl RawWorkItem for Pin> where T: WorkItem, -- 2.55.0