From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (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 5040A33987E for ; Thu, 5 Mar 2026 21:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772747180; cv=none; b=V2/BOTV9SMuRrX+/rrlrHt+eDep83N0ETAhWtOKcGvBsjSzWZUOqywvVhZPyTI4ek+upzCvxNH5Lcj85aL0ZfrBmFN72zH9DrHlY5oj1JowRPARc1KK3qWA9FtSeD1NQLxHLjH3hZBnEi69eTv7VL3mFpnLG27ObxH33cgA22dM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772747180; c=relaxed/simple; bh=iyX0qmfaG81Rj41H7X2QPhit2JQzbYOp/KdP09pUD9I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cJqbOA9r1hhNJRwJje4namTGjl/bcwwRMdFNePL31KPwEr1Sxuj0iphM6SGPABgbtlhKJVkJUFxQrkqW+n2rjrQKqp0iEpOapjlqc8OLJVXcleRMKfvKokrx0un7mNQTZCQ4mx5EbrCRGbg1M1VRMen190F2/0GQAjB23OJGYAY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=nNchSM+9; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=XE8n7s4i; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="nNchSM+9"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="XE8n7s4i" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 625Knlt13141211 for ; Thu, 5 Mar 2026 21:46:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= w6k8+DCPmvVS0VzBd1DlzrWskZqznfWtGoXnFeE7JRQ=; b=nNchSM+9zrXqc//l I9qvK5eJ8PrIe7ZtPyZPSreumwlr6NNn12k4VvWKWATdQJ4gO08lvE92ccQotyKu /4GTyvQeyB6iFe2Kwb5j+IkAo93aq/k5Puobc1IH9/BIPzqkh0sT9EpwSc26y6It 3cl+xfxtYfsfXnFo/SdQNO5bsRVzEyGXMElogOpQo4IAjqmKqrx4IY/ZjUrqC2K2 xnpBmMq48g6kdDpE1HfWyqI6JH9/w94Qyd3cRgjJs5qfQL5+UwlOY2qIZQ4kh26O V33uBXs1pbuT5oDwZuk91ZQ2XXwxR/cvm0GcZNXP9bpB06v/cLCwKZBYsGsw5mut cDvlsw== Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4cq1pk3hvh-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 05 Mar 2026 21:46:17 +0000 (GMT) Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-899bef1ea49so380130276d6.1 for ; Thu, 05 Mar 2026 13:46:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1772747177; x=1773351977; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=w6k8+DCPmvVS0VzBd1DlzrWskZqznfWtGoXnFeE7JRQ=; b=XE8n7s4iC4gB37HqsZDo3523SXgMArVsUrvUpkENuaOitYynePFWDAkRxrLIsonUPx WEP5k3vYLYBK9sRR6lnhKZtJeJzaQ9/W8oY6gXmSQXdVN9S4WrcBxn6uV/P6eQow/Y+U eb1FOa+4HmkOOjfsepgg6q2EWvi2NmzLZZSfLLSkucrgi1tRkH2EoweRIkSqodTyyuBA NoNJgvL+LSX60mLNaiDKx6PNdfJ0g9P7ddLw+1Loi7gsz5dh2ZcF8bFhzSUcFOnPFold f2n6VfIM2GMz8Y+dg0MApN5WHLQ4pZyficw3QzAhJeHpQvZ8q1XuVN9ku0o+CMMssYcS vt6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772747177; x=1773351977; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=w6k8+DCPmvVS0VzBd1DlzrWskZqznfWtGoXnFeE7JRQ=; b=w7uZVceqY1idoC19vBCtB//OZTtShVNfz4XrwrDBCHqYoc/jMrc/szh+6XtMcQOySV qsXmyTOB7A0M61WABplkwzm6wpH6Eh/gbRNCJi55N86cuXDokh4m8QpLykRCmjXpSWjv joeNzdfXuYCYmVKL6sA+CFcxh5GjCVk67H5Wec/IGYkLqmEs8klfWzinmOIiBry6NLTR hkQeRaW6HiN6Z0e1B0+rQK0rCq/hf+Be75BkRFPUkSDu4zybHjQOGdWLSTqWefMpg1q2 dYYIr3tP5aK1fGJQK+84BZY03IY/YttffqMwYu9n/uMVV34k5jAh19jMDwoj1agZZd99 cu7g== X-Forwarded-Encrypted: i=1; AJvYcCWfp1VjLI6ynXkI0tWkaQa2sgoPHNwOtcW+vAjCA5yyRTExFxtrFLxsbPSDRSH82GbCHrZkrWc1e8u7cAM=@vger.kernel.org X-Gm-Message-State: AOJu0Yxq2/kYJaILVGlReB4qkYQLRLa/lfKPEZvMbttxe45z++/QoWrU 4bv8z+gc2/MBq/gr+BMSeBEH96LW1KNuBraDpQ3hMF70iHD56G35wmOr1S/Zf9meXMPt+R7sixS X6M0o3viPxzhvcOK0DRZyzY+BGRk+1OEF1UNcwAkJ7UFdhYJzue32jJLY61u6WxFVHIA= X-Gm-Gg: ATEYQzx2kGwVS+TPjt63VBPQmKa4REF2RBsTSqo3l0NO+lT8uuc3EsUamw3bRs1Jlmh yTBfrXtbqn6tjiKXb2X2mOP90pS62pYniE2d4e43aPx/bh4kqz2d4teM7xhzHQefC7s2hrmrOEr sPZSqKmtyoBv0HwBi2XU9ic4Yvxiq25EpIZFh3RLrNB4gefuHzaOkXtI4ceWAuUDMOC47Y0Jipf 09Mwkd0pQD56eB0afsklfUBY9dv4fyonTTsTnsrRsa4gECfVyhpLpWkxEpVuHLUz7Qm5S0W3YQx c3YHseAFnR6Cj9hrjBJmEG9sjVZc3CBdY5MQ3iHTaYfKYVWxKZt0bPhbZ41AQtuv7b9l/uTpIsw GdH6kH3qKHmvy2o3gIi9snCsm2g7YN8iuPW4yLhIo6WxN X-Received: by 2002:a05:620a:4688:b0:8ca:d5cb:6839 with SMTP id af79cd13be357-8cd6d4eb0b2mr853685a.65.1772747176630; Thu, 05 Mar 2026 13:46:16 -0800 (PST) X-Received: by 2002:a05:620a:4688:b0:8ca:d5cb:6839 with SMTP id af79cd13be357-8cd6d4eb0b2mr849485a.65.1772747176172; Thu, 05 Mar 2026 13:46:16 -0800 (PST) Received: from [127.0.1.1] ([178.197.219.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439ba2a58dasm28571575f8f.27.2026.03.05.13.46.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Mar 2026 13:46:15 -0800 (PST) From: Krzysztof Kozlowski Date: Thu, 05 Mar 2026 22:45:40 +0100 Subject: [PATCH v2 01/10] workqueue: devres: Add device-managed allocate workqueue 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: 7bit Message-Id: <20260305-workqueue-devm-v2-1-66a38741c652@oss.qualcomm.com> References: <20260305-workqueue-devm-v2-0-66a38741c652@oss.qualcomm.com> In-Reply-To: <20260305-workqueue-devm-v2-0-66a38741c652@oss.qualcomm.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Jonathan Corbet , Shuah Khan , Tejun Heo , Lai Jiangshan , Tobias Schrammm , Sebastian Reichel , Andy Shevchenko , Dan Carpenter , Krzysztof Kozlowski , Lee Jones , Dzmitry Sankouski , Matthias Brugger , AngeloGioacchino Del Regno , Benson Leung , Tzung-Bi Shih Cc: Matti Vaittinen , driver-core@lists.linux.dev, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Sebastian Reichel , linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, chrome-platform@lists.linux.dev, Krzysztof Kozlowski X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5216; i=krzysztof.kozlowski@oss.qualcomm.com; h=from:subject:message-id; bh=iyX0qmfaG81Rj41H7X2QPhit2JQzbYOp/KdP09pUD9I=; b=owEBbQKS/ZANAwAKAcE3ZuaGi4PXAcsmYgBpqfmZqlqPt/O4g+aJVCPVwYCdNiMJivVoKJNv+ nfTlOEuLkCJAjMEAAEKAB0WIQTd0mIoPREbIztuuKjBN2bmhouD1wUCaan5mQAKCRDBN2bmhouD 12BuEACE9UGRO8qQNKFX1PzUCZ/Fq7vepWE4JCBJk0nfeFyl0KogKdTFRFTSJsUiy3dJS0tzFJW 7WNmnOlxd7SbvQyXqVrx+RdQC5wbgOlI+Sol+ecKeyExdYtDYLCU6Owm28wDGtgEkTE9mJfiM9o mUyXPEsHEcoqBmimZTT+FwLmUGknfHEglow4Am76QGjLX7IsZ3LhKVzM7Rj5iFP939x9p3Fk11w b+DQjHaOHIrzD9Wa5iyoaD5SyIhn2ixaHFTdqx+km8ivIAkp2A0vJTGfQAcmeqXOmlcJSzg3fSv yBMQ5Xz30FMatrz/Zrj9+z3clMPhYokj8lrGHYK0+YKKKrfu6VJaN9XKe9p1JJpWLxQ7M1cWPyt rMMSWtOdvNnAqcUcfCchdEQeCIz4WkpMrfcQv/qQcaLUAyED27AZRfrVc+YMK0f3FZz8q4bzJMk hqqq995Ee3ptNIv/y1mnunKPueHyuf8L7BiWhF6YA7c4fth+txTnuvKup5geymqojDZqRmq2WK7 H7jl8/lUbJeUYr4tXJpgcnKQOwns3MJFViLP5yUd38U6Wq0fz/CcCbhh5hF4ga3EPuYgPZjILVl fkIsr48Wel9gwyvGlG14kKHUQ4hQe95Gr7l3enhfn4+YMk73kivz3PW3tYXfn3xR7L4J5bS1n+d uBhhIvG9s2KLDyA== X-Developer-Key: i=krzysztof.kozlowski@oss.qualcomm.com; a=openpgp; fpr=9BD07E0E0C51F8D59677B7541B93437D3B41629B X-Proofpoint-ORIG-GUID: Kk9AM4-RIG6AHtvpqiK2wsB6Of03JMxq X-Authority-Analysis: v=2.4 cv=Gu9PO01C c=1 sm=1 tr=0 ts=69a9f9a9 cx=c_pps a=UgVkIMxJMSkC9lv97toC5g==:117 a=gOEeR9iKwsj33Yj5oN/cWg==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=EUspDBNiAAAA:8 a=7xz5_SZBKAcHatMQKAYA:9 a=QEXdDO2ut3YA:10 a=1HOtulTD9v-eNWfpl4qZ:22 X-Proofpoint-GUID: Kk9AM4-RIG6AHtvpqiK2wsB6Of03JMxq X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzA1MDE4NSBTYWx0ZWRfX+kaTj0zR/Mta xPBCSJrjJj9RaR4prV9CwIcILjKkSsNKblf50ShLonDpQ17JAxAbyw7DYiY3X1t6Fkucvxyvz3P gV3ukClYSIRNj5hlOboTK1juTEN1l7HTrG0OjTxK3fiqbx5ebmhdzwgrzVVLBR19KRjN91Z2BiJ DrAAz2e7sMGK1DiBkMVyWZ4hQ6Nz8+5J4dffAHJnXU2LmkiI//L9odVbPZVVR7D8WUay6DOw/dR ztrdmKNzgXE+JMsAq2fPXY6sV3zaeUS6B3dnswIJx5hlHN0NjDzdbYiN41og8H4Xbpjn5HQm1VD 1NDgMtV15zQu2MgU9EaDgDryjm8qCjcHxEnUPPR1/m2RBBtibu0r7fdc2I2xnc+1HSnj6PTM71G 8y/Vyuq6AbmmL9rPNo8ZYv1I+FlvubG36wzI54ZlzlD/xtsdnWeNK5Yro2UpAggQ3bMGrFN7yeY /UFK5Y04Dc5kgku3Uyg== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-03-05_06,2026-03-04_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 lowpriorityscore=0 suspectscore=0 phishscore=0 adultscore=0 impostorscore=0 clxscore=1015 priorityscore=1501 bulkscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2602130000 definitions=main-2603050185 Add a Resource-managed version of alloc_workqueue() to fix common problem of drivers mixing devm() calls with destroy_workqueue. Such naive and discouraged driver approach leads to difficult to debug bugs when the driver: 1. Allocates workqueue in standard way and destroys it in driver remove() callback, 2. Sets work struct with devm_work_autocancel(), 3. Registers interrupt handler with devm_request_threaded_irq(). Which leads to following unbind/removal path: 1. destroy_workqueue() via driver remove(), Any interrupt coming now would still execute the interrupt handler, which queues work on destroyed workqueue. 2. devm_irq_release(), 3. devm_work_drop() -> cancel_work_sync() on destroyed workqueue. devm_alloc_workqueue() has two benefits: 1. Solves above problem of mix-and-match devres and non-devres code in driver, 2. Simplify any sane drivers which were correctly using alloc_workqueue() + devm_add_action_or_reset(). Signed-off-by: Krzysztof Kozlowski --- All further patches depend on this one. Changes in v2: 1. Drop devm_create_workqueue(), devm_create_freezable_workqueue() and devm_create_singlethread_workqueue() 2. Simplify with devm_add_action_or_reset() 3. Do not export devm_destroy_workqueue() 4. I did not move the declarations to devm-helpers.h because consensus was not reached and I think it would not be accurate place. The main alloc_workqueue() is here, so should be the devm- interface. --- Documentation/driver-api/driver-model/devres.rst | 4 ++++ include/linux/workqueue.h | 22 +++++++++++++++++++ kernel/workqueue.c | 28 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst index 7d2b897d66fa..017fb155a5bc 100644 --- a/Documentation/driver-api/driver-model/devres.rst +++ b/Documentation/driver-api/driver-model/devres.rst @@ -464,3 +464,7 @@ SPI WATCHDOG devm_watchdog_register_device() + +WORKQUEUE + devm_alloc_workqueue() + devm_alloc_ordered_workqueue() diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h index fc5744402a66..66a94c171b0b 100644 --- a/include/linux/workqueue.h +++ b/include/linux/workqueue.h @@ -512,6 +512,26 @@ __printf(1, 4) struct workqueue_struct * alloc_workqueue_noprof(const char *fmt, unsigned int flags, int max_active, ...); #define alloc_workqueue(...) alloc_hooks(alloc_workqueue_noprof(__VA_ARGS__)) +/** + * devm_alloc_workqueue - Resource-managed allocate a workqueue + * @dev: Device to allocate workqueue for + * @fmt: printf format for the name of the workqueue + * @flags: WQ_* flags + * @max_active: max in-flight work items, 0 for default + * @...: args for @fmt + * + * Resource managed workqueue, see alloc_workqueue() for details. + * + * The workqueue will be automatically destroyed on driver detach. Typically + * this should be used in drivers already relying on devm interafaces. + * + * RETURNS: + * Pointer to the allocated workqueue on success, %NULL on failure. + */ +__printf(2, 5) struct workqueue_struct * +devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags, + int max_active, ...); + #ifdef CONFIG_LOCKDEP /** * alloc_workqueue_lockdep_map - allocate a workqueue with user-defined lockdep_map @@ -568,6 +588,8 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active, */ #define alloc_ordered_workqueue(fmt, flags, args...) \ alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args) +#define devm_alloc_ordered_workqueue(dev, fmt, flags, args...) \ + devm_alloc_workqueue(dev, fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args) #define create_workqueue(name) \ alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_PERCPU, 1, (name)) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index a1bfabeaef41..5cc5e6a400c9 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -5909,6 +5910,33 @@ struct workqueue_struct *alloc_workqueue_noprof(const char *fmt, } EXPORT_SYMBOL_GPL(alloc_workqueue_noprof); +static void devm_workqueue_release(void *res) +{ + destroy_workqueue(res); +} + +__printf(2, 5) struct workqueue_struct * +devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags, + int max_active, ...) +{ + struct workqueue_struct *wq; + va_list args; + int ret; + + va_start(args, max_active); + wq = alloc_workqueue(fmt, flags, max_active, args); + va_end(args); + if (!wq) + return NULL; + + ret = devm_add_action_or_reset(dev, devm_workqueue_release, wq); + if (ret) + return NULL; + + return wq; +} +EXPORT_SYMBOL_GPL(devm_alloc_workqueue); + #ifdef CONFIG_LOCKDEP __printf(1, 5) struct workqueue_struct * -- 2.51.0