From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 755FC2356BE for ; Mon, 18 May 2026 04:12:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779077526; cv=none; b=eZHyUqvaVfpTGcrT2QfogXU7bn6dY/+wdum8KxeQj+iEQf+JhlsE9GjZWHH8tnfZUXS0KAn3AZMWBaEpBjaybqvYtnU0uHUVLVa6JoKaJ42QbcYMPu80EyKAdPkXuKt/5+3sl2fUOU/e45KZWgMHB4Z+QVt3NVY7Q8fjtUIqjuc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779077526; c=relaxed/simple; bh=tV9y9h8tyQclWteeIaMREPy8u22ENHoAcERe5+G8hQM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=STLyLYFFnOxaTGbmuvJfdPNFIr/Co9dzvXZtZU5HVCspAC8p2NY3iyyhFJnh8503/SWhGRCjjxa4mO35of5cj+UOPgTAQ1VZinkAF+6VElODtlXaqPKt0JFHQC1LYxjUaCC+mtjGE9aHR8KsiG+teNoyTXSUmbXROfRadiVmMy4= 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=hIzT1Yau; arc=none smtp.client-ip=209.85.208.48 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="hIzT1Yau" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-6763cc8775cso6310348a12.0 for ; Sun, 17 May 2026 21:12:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779077524; x=1779682324; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xgeeVSI8Dv/viF+Q8OmPFj9IAqT9Fzi8XwuORCK6h7o=; b=hIzT1Yau+xV1naP9ILK07XM4ykuulPT0I+e4lnXbVsXi3cBCx7qyDfWRxppGW8Eoxm Ecnm6IyVkWvpWwkw33PkMKkdTtO2xpGVgLSx8P8mMu8OZDiwLSC8awx6olx0IF+R9TPE DnDlbLZx9/RC9qkID+aNbKEQg5Cg3UjURJtAJ7YcxtXdV2AP5098tJ0kRzH367FrZj84 zBlZ+R86BTYh+0dD9vuXT8YWiFd6TgqXG+pM1+u0JLDzaAqs+j5DOGVucuUZlrigFkfK zvotq7LF61A9FoeLfImkDsR8LwxwHb1oceMWsRPff1tI0fdUaZtBpkmkl0QVEEBCDxqw Vv4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779077524; x=1779682324; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xgeeVSI8Dv/viF+Q8OmPFj9IAqT9Fzi8XwuORCK6h7o=; b=Jg/pIyirAc1Q5C/sanrezbUbG6PdDX/3dhBUqbQZBMCeFa/dN6Gysm1GX/gwxPkOMK nz0ActWWOvutpKfpI8XnMzTSSX99Tu9SZ2HjhhBcN21Lrj+i78RQJpGuG7+d4l05CHH+ NbHF1K4RjlxqBlXq6zr4pIkS8APBM/Z7HHfpfhox/GB0tGNrWqmqflZ2MxuB79nx55b4 9P30qJR5niTHM3FwFi4cz+3LoNkikYEXP1EkS8ppH5XDy3Ee/TVO4LLF3aIU1kf18xNZ gdBXeU3xiuAeY9O7tq6KliEM01hR86QNv1ugClm7fm6Br626v6GWD/iEod/OY0DxaCrD 76EA== X-Gm-Message-State: AOJu0YwHLyBbQDLbml+6Bra2qnnx2meAnwiXkxt2uJE6T27wfxa6MXI9 9XDeWv/BSTr5yXRwALNlflVIIlevDEcrloh/g/cjWEhUQqa4UD6cl0XO X-Gm-Gg: Acq92OHwwai346y+0bL/ni6CC32UxC0zGsye8hNTQHSlh69PNtXbFhP1+S+0odnHPxq S5toVgeqqaFEIKeCNN9n24PDtdAROzQPyiS4zMbmRVbeafZXLnwULkObRAoF9Gj0dNEe550zW6n z7pl6azObwIp4gWb8XZwsH36dIgE4yRhbNSU1miZZbi5JAF6FfND+PltN+FmamNW3Vcuv5IVUOT 3M5hGJFA0Mwo6lavn/urcLYRCvxZs8zVBl/cVp7UGe8+XZXCxmUDy7aY69k6LNviFLoamlOd+Yh cD1ti3Luzp/VpoGdAiyhYV26h3FBEWosHikH5yBNZc/b5xi+Yjp3VgeWMH+Skol0TYc5Mleh/fz CO3/a63F6S6TSIRNtjv5qAHGyxd51c7smdYMcvESvgT6KhF+E9MX3jQkV+UWgMXB+PLTx2C3OUr SU0ix4N9j9uExL0opIYFtvWA8IZ7j3iHPBFGArrGNm1m59a4ancKTSJXfgDd/EL84vt0Y= X-Received: by 2002:a17:907:3f10:b0:bd6:462b:1d6c with SMTP id a640c23a62f3a-bd6462b292dmr268170766b.0.1779077523468; Sun, 17 May 2026 21:12:03 -0700 (PDT) Received: from moayad-Dell-G15-5515.. ([185.192.162.194]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-bd4f4c620cbsm513915166b.27.2026.05.17.21.12.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2026 21:12:02 -0700 (PDT) From: Moayad Salloum To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, ojeda@kernel.org, aliceryhl@google.com, lossin@kernel.org, a.hindborg@kernel.org, Moayad Salloum Subject: [PATCH 1/2] rust: workqueue: add SAFETY comments for Pin> impls Date: Mon, 18 May 2026 07:11:54 +0300 Message-ID: <20260518041156.93133-1-salloummoayad4@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Fill in the two SAFETY: TODO placeholders for the WorkItemPointer and RawWorkItem implementations of Pin>. For WorkItemPointer: __enqueue uses a work_struct initialized with this trait's run function because Work::new stores T::Pointer::run via init_work_with_key, and the ID const generic ensures the correct Work field and WorkItemPointer impl are selected. For RawWorkItem: the work_struct pointer remains valid for the closure's duration because KBox::into_raw leaks the allocation without dropping it. If queue_work_on returns true, the memory is only reclaimed in WorkItemPointer::run via KBox::from_raw. queue_work_on cannot return false because Pin> provides exclusive ownership, so the item cannot already be in a workqueue. Signed-off-by: Moayad Salloum --- rust/kernel/workqueue.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 7e253b6f2..6d665418b 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -881,7 +881,15 @@ 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 way to create a `Work` object is through `Work::new`. +// - `Work::new` makes sure that `T::Pointer::run` is passed 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 ensures that `T::raw_get_work` uses the correct +// offset for the `Work` field, and `Work::new` picks the correct implementation of +// `WorkItemPointer` for `Pin>`. unsafe impl WorkItemPointer for Pin> where T: WorkItem, @@ -901,7 +909,14 @@ unsafe impl WorkItemPointer for Pin> } } -// SAFETY: TODO. +// SAFETY: The `work_struct` raw pointer is guaranteed to be valid for the duration of the call to +// the closure because we call `KBox::into_raw` before the closure, which leaks the allocation +// without dropping it, so the pointer remains live. If `queue_work_on` returns true, it is further +// guaranteed to be valid until a call to the function pointer in the `work_struct` because the +// leaked memory is only reclaimed in `WorkItemPointer::run` (via `KBox::from_raw`), which is what +// the function pointer in the `work_struct` must be pointing to, according to the safety +// requirements of `WorkItemPointer`. Note that `queue_work_on` can never return false here: +// `Pin>` provides exclusive ownership, so the work item cannot already be in a workqueue. unsafe impl RawWorkItem for Pin> where T: WorkItem, -- 2.43.0