From nobody Mon May 25 09:57:54 2026 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.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 1434772631 for ; Mon, 18 May 2026 04:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779078263; cv=none; b=GxVvNDB05/+zsjkKG0yQQDCVQ+HvCKcXKh8cZ7qok6S7yYpFyezOA1K329CDXS28me+Cvj2crnjNMZnVM4mYy21PixAZi3Hpe87b9p3JPRb+Zkli5bDPI3L/l4WHl4Ga7Pz46twK0vdRFj2scdR3XsUV/uP4QSKPVDcREm3Bw78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779078263; c=relaxed/simple; bh=94BYK/meaWRlBFsub8cZ3gCJ1k2jJpAZpuOvXBpdB1Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JrBfDEXKg3GnikHeQpWArwEaiOTxPBdctrdipcPTfpGL/OhhBoWn+9WjtdeTxcAjZllA3z5jR2Tx9gwLXXheUWUxT5NPzX1/OtiGXHdEYbEIDsqjlSXHKbnBRWnVM7B2v2WI/KNVlBe/pINMTw2jlBJrQBaaH/YxQHAEBVN6YjU= 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=MrHycDwN; arc=none smtp.client-ip=209.85.208.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="MrHycDwN" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-6746d0b2b4aso3171668a12.3 for ; Sun, 17 May 2026 21:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779078260; x=1779683060; 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=l+3nxpGKAAJVujTKbl/ibTWmnRef3XHrcqZengS5y84=; b=MrHycDwN874dq20Mic5qC4uAUKrt+oKX+hrf+KQdr3VXAtNIw9LdQJI8GQcYnm6oSF LX8DsW2hioue5GNqHWLF6Xb0oWcYzmBXu3Ex5mpmxwYBiX4muTp7+dhXj7CNx9c63kyM 4V0rySae3Q75MYhyh8lUYaT/kuDN9NFTZBd7CpD/Isdx/HBf7CQ2/IgiuXd0o8eH5kPC AaoWVD0KDmPVk3aupvzq4QsO+n68aV2ngfO5nSTFP4xEDzXHHoK7mdI840yDtGsj/x3n FvYyuVJDjeDZBsDGWNSBmAmBG/tFLViTCgIOpUAM4z6ZasUYQHgptUOfMGnnnzY8oB6V afqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779078260; x=1779683060; 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=l+3nxpGKAAJVujTKbl/ibTWmnRef3XHrcqZengS5y84=; b=iPrsIYXWQrQgQhIVi5igE+2dxy/u6I8ZbolDueBFhqPQxvBowTCjSor5HkuQFbqigy FJzZDMda2LfylPdZXU9EHnQgc+o6ZjDy5c88zjZZNmAPFP6Ber2hDWLAh0177W8xs9Fj 9famGmHi/aajc1hgJ6RhwcXxB4YyCQ1GrI6TjyQXViALxM4e9o8UPB52G23NAZG3SLi2 QzLUkTgfTot+21gfRXnEo5Kb4DTClgbeJw7l42mjERK0Tzhn+22jojw/BuIhgTN8GXzp yw6Vqg8/cZ5ekg85FznXeY3jpBK/EUtkCWdD8QdzjGYsq46bhbb7wb5zNxMgBPK8kQ0b yTVg== X-Gm-Message-State: AOJu0Yw72uq3NhyO/LWBj/PIDZlfEsln47gHI8k40OZS9jCX0PCjWD7f YFhk71Emu2f4e9BgnOI/GuBx9YcIQNizw2D2SNYVSVZoFkU4DTujirJR X-Gm-Gg: Acq92OEl6vdzkTA5TfBk7K0BuEFtnRmZ10YWrLHBqq5ZbhkHyUSKlBGhsrQXTht6yZT 82pi91cPNCKtCphSVbhqbOPQA9xp7ACz1TRa0WBUTltBE5Sa26169htiMk4nW0uwaG4Eg/UenNd JtoQ7aLwVOWR3aKWls4eSxfTQGiTqiopGKQb2jI533Nd1zepdU7S72uofF/nbQNppxfaCZTp14v mv+9HzJLQEPQxwvfegNealeInBTzyOlaxIbKpLAluGasAJXBhajyt+0FICUsMawa6DwHUYS0ytN 6xcT5BdUNos/mYg+LnygrTND2DdW4Ue4ytf2oacFkGCDCMQyF33MJSN4D5ZirPmxEXalCf9U1O6 axGtgO4oG85477EHAdF2qoPFaR8Bpw3DoHnlPqFSyY9qi1P9aIz2Nc2lkDJVl3e3JK0cG2kvf+/ 5SRkAs0MdPs+mhS9qU6G6+HhOg1j0711hhN41KAfpTWDcmC4nRkmEf2QlU X-Received: by 2002:a17:907:3cc1:b0:bd5:7c3:69d with SMTP id a640c23a62f3a-bd517abc6d7mr654993766b.48.1779078260197; Sun, 17 May 2026 21:24:20 -0700 (PDT) Received: from moayad-Dell-G15-5515.. ([185.192.162.194]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-bd4f4c313f7sm525936666b.21.2026.05.17.21.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2026 21:24:19 -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] rust: workqueue: add cancel_work for Arc Date: Mon, 18 May 2026 07:23:47 +0300 Message-ID: <20260518042347.95213-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: quoted-printable Content-Type: text/plain; charset="utf-8" Add a safe cancel_work() function for work items owned by Arc. In the existing API, enqueueing an Arc calls Arc::into_raw() to leak the Arc reference into the workqueue. WorkItemPointer::run() is the only place that reclaims it via Arc::from_raw(). Without a cancel function, drivers had no safe way to cancel pending work, and any attempt to do so by calling bindings::cancel_work() directly would leak the Arc reference since run() would never be called. cancel_work() handles this by calling Arc::from_raw() when cancel succeeds, reclaiming the leaked reference that __enqueue() left behind. Signed-off-by: Moayad Salloum --- rust/kernel/workqueue.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 6d665418b..daff9040a 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -871,6 +871,35 @@ unsafe fn __enqueue(self, queue_work_on: F) -> Self= ::EnqueueOutput } } =20 +pub fn cancel_work(item: &Arc) -> bool +where + T: WorkItem>, + T: HasWork, +{ + let ptr =3D Arc::as_ptr(item); + // SAFETY: `ptr` comes from `Arc::as_ptr` which is a valid non-danglin= g pointer to `T`. + let work_ptr =3D unsafe { T::raw_get_work(ptr.cast_mut()) }; + // SAFETY: `raw_get_work` returns a pointer to a valid `Work` f= ield. + let work_ptr_struct =3D unsafe { Work::raw_get(work_ptr) }; + // SAFETY: The `Arc` keeps the allocation alive, so `work_ptr_struct` = is valid for + // the duration of this call. + let cancel_res =3D unsafe { bindings::cancel_work(work_ptr_struct) }; + + if cancel_res { + // SAFETY: `cancel_work` returned true, meaning the work was pendi= ng and has been + // removed from the queue. The workqueue will not call `run`, so w= e are responsible + // for reclaiming the `Arc` reference that was leaked in `__enqueu= e` via + // `Arc::into_raw`. We use `work_container_of` to recover the orig= inal `*const T` + // pointer from the `Work` field pointer, then reconstruct = the `Arc` with + // `Arc::from_raw` and drop it to decrement the ref count. + let item_ptr =3D unsafe { T::work_container_of(work_ptr) }; + drop(unsafe { Arc::from_raw(item_ptr) }); + true + } else { + false + } +} + // SAFETY: By the safety requirements of `HasDelayedWork`, the `work_struc= t` returned by methods in // `HasWork` provides a `work_struct` that is the `work` field of a `delay= ed_work`, and the rest of // the `delayed_work` has the same access rules as its `work` field. --=20 2.43.0