From nobody Tue Dec 16 16:35:34 2025 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) (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 78A75DF49; Mon, 25 Mar 2024 15:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711380121; cv=none; b=DftLYD6jKmH+H0PL5pYWJyPh2RZKRsqUGU2F7v0MLHA/+slIAVcxT2qziOC1S03YxgbnFAAdgd9o3WLxuYrpGD+FlP7uomF/lOb6kDxc8Y5CklR1ekrenUn4mDZlbJn3JVUMZ9Qg2c8RyuyREmaSxOQaMAwFJd6GsmlVilXD9d0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711380121; c=relaxed/simple; bh=WKicl9RpGDu1gJ6HqIzRdJp8b9byCb1GpL96zLyvR+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PAtAJeKBXPWbOsZjsakCb1dsCeIOQwYW01XBoUsxWKbTegEPX603VjSwJ04vrvXaGRHXNOgjLn/fhaQ+iE4nxg+HPqDGcS7nDBHYdazKJLa7pQ16RCnkUsanZSOmNumkIF3vbCZ/HzPrT2QUaHHNNuelMxZXDuVbhbYY2FZNSGM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=W6nC0Pe6; arc=none smtp.client-ip=217.70.183.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="W6nC0Pe6" Received: by mail.gandi.net (Postfix) with ESMTPA id CBE0AFF80F; Mon, 25 Mar 2024 15:21:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1711380116; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UpWR4WCDuNDYnnhlODtqNwmnfIhRuBZf4UaQrvurOwU=; b=W6nC0Pe6py8mNJRQ5DN90Xo92lsaTVdsFAtCuxacVR5zoQEXtgQ5CKY9b1PvN2UIVjn50/ asyf2aSpS/JBeQTgfqQNFux2wwum1gxmjP2hpC43U5PoxTvor5QxMpw0nPuWwRLw4hz1VG /v8SRnlpoL/DGg8vaVJ31nsxkgvcO+q1K5S8vAxK21FIenNqhfH7ab6nPItUuvhP4gsf59 xL6IFtmEbmeSAro6/reBkPlTdu6f9lIgH5Xj2m28Gr49FOIYbPN/Wj95qyw8GgXaHhG6Ip 8jIHq9KfcnMaDsHGqrQh/ZeKr7qyXkF6/vk/2fGvZJsf3TsVi0trVrjicAvxwA== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Frank Rowand , Saravana Kannan Cc: Lizhi Hou , Max Zhen , Sonal Santan , Stefano Stabellini , Jonathan Cameron , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Luca Ceresoli , Nuno Sa , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v6 1/2] driver core: Introduce device_link_wait_removal() Date: Mon, 25 Mar 2024 16:21:25 +0100 Message-ID: <20240325152140.198219-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240325152140.198219-1-herve.codina@bootlin.com> References: <20240325152140.198219-1-herve.codina@bootlin.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 X-GND-Sasl: herve.codina@bootlin.com Content-Type: text/plain; charset="utf-8" The commit 80dd33cf72d1 ("drivers: base: Fix device link removal") introduces a workqueue to release the consumer and supplier devices used in the devlink. In the job queued, devices are release and in turn, when all the references to these devices are dropped, the release function of the device itself is called. Nothing is present to provide some synchronisation with this workqueue in order to ensure that all ongoing releasing operations are done and so, some other operations can be started safely. For instance, in the following sequence: 1) of_platform_depopulate() 2) of_overlay_remove() During the step 1, devices are released and related devlinks are removed (jobs pushed in the workqueue). During the step 2, OF nodes are destroyed but, without any synchronisation with devlink removal jobs, of_overlay_remove() can raise warnings related to missing of_node_put(): ERROR: memory leak, expected refcount 1 instead of 2 Indeed, the missing of_node_put() call is going to be done, too late, from the workqueue job execution. Introduce device_link_wait_removal() to offer a way to synchronize operations waiting for the end of devlink removals (i.e. end of workqueue jobs). Also, as a flushing operation is done on the workqueue, the workqueue used is moved from a system-wide workqueue to a local one. Cc: stable@vger.kernel.org Signed-off-by: Herve Codina Tested-by: Luca Ceresoli Reviewed-by: Nuno Sa Reviewed-by: Saravana Kannan Acked-by: Greg Kroah-Hartman --- drivers/base/core.c | 26 +++++++++++++++++++++++--- include/linux/device.h | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 7e3af0ad770a..f2242aadffb0 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -44,6 +44,7 @@ static bool fw_devlink_is_permissive(void); static void __fw_devlink_link_to_consumers(struct device *dev); static bool fw_devlink_drv_reg_done; static bool fw_devlink_best_effort; +static struct workqueue_struct *device_link_wq; =20 /** * __fwnode_link_add - Create a link between two fwnode_handles. @@ -533,12 +534,26 @@ static void devlink_dev_release(struct device *dev) /* * It may take a while to complete this work because of the SRCU * synchronization in device_link_release_fn() and if the consumer or - * supplier devices get deleted when it runs, so put it into the "long" - * workqueue. + * supplier devices get deleted when it runs, so put it into the + * dedicated workqueue. */ - queue_work(system_long_wq, &link->rm_work); + queue_work(device_link_wq, &link->rm_work); } =20 +/** + * device_link_wait_removal - Wait for ongoing devlink removal jobs to ter= minate + */ +void device_link_wait_removal(void) +{ + /* + * devlink removal jobs are queued in the dedicated work queue. + * To be sure that all removal jobs are terminated, ensure that any + * scheduled work has run to completion. + */ + flush_workqueue(device_link_wq); +} +EXPORT_SYMBOL_GPL(device_link_wait_removal); + static struct class devlink_class =3D { .name =3D "devlink", .dev_groups =3D devlink_groups, @@ -4165,9 +4180,14 @@ int __init devices_init(void) sysfs_dev_char_kobj =3D kobject_create_and_add("char", dev_kobj); if (!sysfs_dev_char_kobj) goto char_kobj_err; + device_link_wq =3D alloc_workqueue("device_link_wq", 0, 0); + if (!device_link_wq) + goto wq_err; =20 return 0; =20 + wq_err: + kobject_put(sysfs_dev_char_kobj); char_kobj_err: kobject_put(sysfs_dev_block_kobj); block_kobj_err: diff --git a/include/linux/device.h b/include/linux/device.h index 1795121dee9a..d7d8305a72e8 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1249,6 +1249,7 @@ void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); void device_links_supplier_sync_state_pause(void); void device_links_supplier_sync_state_resume(void); +void device_link_wait_removal(void); =20 /* Create alias, so I can be autoloaded. */ #define MODULE_ALIAS_CHARDEV(major,minor) \ --=20 2.44.0 From nobody Tue Dec 16 16:35:34 2025 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) (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 4952C13CA97; Mon, 25 Mar 2024 15:21:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711380122; cv=none; b=lGTjzmQQ76d94PVUoT5+5hRfxJhjLGOeAyC6Dq0Lfilmautq88YEzKFoI3v381rM6D/KEUMBFvDo0reAs9BazweZ5lk6IrkkBXoP2yi05s+E7zJuYJ63RwDYgeh/26Kp/rjtFT3z4W73+mU2RzTVN0SU+OaaRRwqESlZzdQ9OCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711380122; c=relaxed/simple; bh=irAAEUcrV7xB4jx4nOrL/PVBhqWchwYDIeJsAg7Sbj0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Yrw9RuFBaZTvLYW4MS37YCN8PTtFvFpa7vKb8AcqoKsKlqgvWYDuvgfwvGNRBTox4dHmKDDay3VPzc9U/BsdRGIntoxwKI3BoGrvmA/gmE+0T9fTuuEFiKq28MYFO0OXKvV3yDywpl2wSg6Vq/VHzVQR0XAidlSldf9iPTEYSzU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=pXf3FPYp; arc=none smtp.client-ip=217.70.183.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="pXf3FPYp" Received: by mail.gandi.net (Postfix) with ESMTPA id BA4F2FF80C; Mon, 25 Mar 2024 15:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1711380118; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FQr/k0s/kJSMD8sx4HhB7DuZAab9W+v4kyea1IFYzds=; b=pXf3FPYpsieWAC0uHiLlYblaEjvpxd0eCYtiKvgub243wBLecvqcb3NLlGPKHG3oW2kRYl W8Vat7B94mhIYuEjvjrFI3slIFbUSWN2q7msRTsyRP/whf3Nqc3dlLx9/ghJP/usPBKi5n MyBWc03pJkDKzh1ocH37lFDAa1E93RpTFVM+SZN7CPiz8FKi0KDrSXVdwVoONJzHUBjsph 9gv1Ev6C/tGOyTbVhkiE1zAxotBQonTXin0kXmURg/jG65FoSNtXLIXapElOEGuIP6Knuv pz5q33xYLlybq1+61DAZAvijHap9fgYKw5nYzo99gf2NfqcLgq3MGbhOWwVI3w== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Frank Rowand , Saravana Kannan Cc: Lizhi Hou , Max Zhen , Sonal Santan , Stefano Stabellini , Jonathan Cameron , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Luca Ceresoli , Nuno Sa , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v6 2/2] of: dynamic: Synchronize of_changeset_destroy() with the devlink removals Date: Mon, 25 Mar 2024 16:21:26 +0100 Message-ID: <20240325152140.198219-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240325152140.198219-1-herve.codina@bootlin.com> References: <20240325152140.198219-1-herve.codina@bootlin.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 X-GND-Sasl: herve.codina@bootlin.com Content-Type: text/plain; charset="utf-8" In the following sequence: 1) of_platform_depopulate() 2) of_overlay_remove() During the step 1, devices are destroyed and devlinks are removed. During the step 2, OF nodes are destroyed but __of_changeset_entry_destroy() can raise warnings related to missing of_node_put(): ERROR: memory leak, expected refcount 1 instead of 2 ... Indeed, during the devlink removals performed at step 1, the removal itself releasing the device (and the attached of_node) is done by a job queued in a workqueue and so, it is done asynchronously with respect to function calls. When the warning is present, of_node_put() will be called but wrongly too late from the workqueue job. In order to be sure that any ongoing devlink removals are done before the of_node destruction, synchronize the of_changeset_destroy() with the devlink removals. Fixes: 80dd33cf72d1 ("drivers: base: Fix device link removal") Cc: stable@vger.kernel.org Signed-off-by: Herve Codina Reviewed-by: Saravana Kannan Tested-by: Luca Ceresoli Reviewed-by: Nuno Sa --- drivers/of/dynamic.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index 3bf27052832f..4d57a4e34105 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -9,6 +9,7 @@ =20 #define pr_fmt(fmt) "OF: " fmt =20 +#include #include #include #include @@ -667,6 +668,17 @@ void of_changeset_destroy(struct of_changeset *ocs) { struct of_changeset_entry *ce, *cen; =20 + /* + * When a device is deleted, the device links to/from it are also queued + * for deletion. Until these device links are freed, the devices + * themselves aren't freed. If the device being deleted is due to an + * overlay change, this device might be holding a reference to a device + * node that will be freed. So, wait until all already pending device + * links are deleted before freeing a device node. This ensures we don't + * free any device node that has a non-zero reference count. + */ + device_link_wait_removal(); + list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node) __of_changeset_entry_destroy(ce); } --=20 2.44.0