From nobody Mon May 25 05:54:38 2026 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 EE1DF3264C1 for ; Mon, 18 May 2026 04:12:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779077533; cv=none; b=dpj32Oaz+b5JtDfqGSToPTAJ314DOf92Y7bAoIAHlJ3v9OslyPPXQ6BeQ2qr2MdMBkFdcMwozOaQxoUoAQLpJjRVd4EFuH7DklgiBP48iXuWAJL5WREnpDW/lsjNu6xvCex3pDIblBY5PvUsSl11WrdkZc4ZjsLLOGXN+MwuYLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779077533; c=relaxed/simple; bh=94BYK/meaWRlBFsub8cZ3gCJ1k2jJpAZpuOvXBpdB1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OInbRjXAYEdok+h1y/4ebZgXqCB70odLru4qYtf+xz44eJFKuxfXU388mo8Du2gofCe40RwEVqBcPJYNEaXKSY+mhlzFhPmalQyaqpR8Z3RCe5ueNZvBHZRXR6m9yYWCMmVeRv+ZQUFahEbsXD122pTQX+pnSk2NnU4bIIDyJZQ= 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=IOuwuOJv; arc=none smtp.client-ip=209.85.218.47 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="IOuwuOJv" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-bce57c132b2so581775266b.0 for ; Sun, 17 May 2026 21:12:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779077530; x=1779682330; 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; bh=l+3nxpGKAAJVujTKbl/ibTWmnRef3XHrcqZengS5y84=; b=IOuwuOJvqGKfzbDtCmnFJ6yaIhKia6s6UQTYJ6yZ6si52XlWDoKYX4deZdg5mEX0dz xiNk3zqZQXWRqhjsYHmQKp2sTLAjmpFJVLmDIgUgzBpkD9sNTelFSxRZzX8bmLXKh19Z Exn2BlzpM5w07mIYx+UXjNiyxf6bVPLiAccbJ61kBTJf8ai/XtZHqVCe7cKmQLZy/+90 yU897ievA6kQHEE1qDs2sdARDEH4MEKfouYoxfaamzkrrH/nZY7YIYbCfP3aCs1kNQVF s44Jy5ML3j49tgB0QUXP9wK9jvgpMCR9RwKiayZsZ51bO5+pMEDCHWYs71Eep1YNAc6M 5U1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779077530; x=1779682330; 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; bh=l+3nxpGKAAJVujTKbl/ibTWmnRef3XHrcqZengS5y84=; b=PxRp4cfW1GQmbyw5qhE8QOUdZaYZjwnDl1f0NxlvIaefBrEir4lkPi4cZZbk/CHVAj wObsjjVxyerzxTXF+ILSoSIcQdhYYN2I8edjbv1nrAcBr26TCkW2YYKTa71epKxmGswc V2X4YxaxqMNF8Gu+uIfIjTzahu43Axbnetwq5MuTdEoG075U37K9p0+RrpirOzWxlr1i UJQ3faFYjdU5hO7sHXT0qBDBvFc/TkEw1q3cwwR9a7Ze5NvU2ZgqPnYIAyho2x3jKzXK Q+XPu7946lkgkrHnyhhSRDOdiTUsXGspCdZNK1C9cvt8F5GX60691c3UFxEzhA/7RgZx I8hg== X-Gm-Message-State: AOJu0YxzgCjrZulpQKQaCSUtxMnA1DReOx+VQYOUk9toFTN3ImBCnPVs 9s8DGf8EtB/fiqnjPJl22kUnYXEHKT17ZlrJbjvDbWChaiSTu4ecQ+4L X-Gm-Gg: Acq92OFQx9lLCK2zRZAm/ZKVJfDm2le+5xCCEjKGK6q7fX4cmmoGrgJezNXaVUQ1Px4 0ATYPTnASDbUYsEiNkJq+ompY0x4mNhVap4vO0FXgvQZHRhpGzkxvQA/Iqx4vBYC0NrK/kw5MAF Ebt2pj0M8Tztkz+pEW64vdGbzJHi/9Bh1UneDdajHByiuUUYKzzf5qQ/KGPP38yP1ShkDuZvny8 /4uL6Hk+RfknMbCkRYjxP+g+78E2KppshhV/loC2jFqJE9AG7eJrs5osqOtSXMsbqQz21RiS58U z9xQKiORuFByUTcGo17SDYI7jQ7V545EB0HOEphhyZYL47rJ9qEzSptnOZu9ESAkPqPQuIr/bih /NqzPGm8AVv0fuowJwE54E0RJdfyYd9m4X7H8F17OSaZQTkysDoKX5hBw3meJ8P1XUshFdwVbf5 hX6m8mq0/CfzaWMswIxvvXoS0rkOJiuWKhTs3NKaIsClLo7yisb0ovpW30 X-Received: by 2002:a17:907:8707:b0:bd5:7c2:7616 with SMTP id a640c23a62f3a-bd51553bee6mr446184166b.23.1779077530143; Sun, 17 May 2026 21:12:10 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2026 21:12:09 -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:11:55 +0300 Message-ID: <20260518041156.93133-2-salloummoayad4@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260518041156.93133-1-salloummoayad4@gmail.com> References: <20260518041156.93133-1-salloummoayad4@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: 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