Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp1037406lqs; Wed, 6 Mar 2024 04:44:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXg91w4zcjCS5JltfaBA8I/l9tmXolWMKyYgug915xGoQMrHovFEXsSlAH4DEkj/QxamkW5EU7y+o/3VhwseztTjVrYcYczL2r4UXHRug== X-Google-Smtp-Source: AGHT+IEf34zX8Sgo5X8FaWWm+9A3MmO+K9reOeATccXjB5iEry1BjeXhSuZoBJC2aukKoeuLR0DO X-Received: by 2002:a05:6214:849:b0:68f:6f92:d291 with SMTP id dg9-20020a056214084900b0068f6f92d291mr4454896qvb.4.1709729041736; Wed, 06 Mar 2024 04:44:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709729041; cv=pass; d=google.com; s=arc-20160816; b=Jn9Fqe4j9Q9TmEVniMlf3jJ5NdhX7VE3v6ANy2DhVLaQ68X/U61BeWXugHnO4VNeTZ kjzNCwO8dPLvO9TZtYMjM8LNMaqwTijMY355eFjg1+PKYZmuaiXYg7hA8WD2EPeHlbHP EYG0EN7gfSBt8Oq2u/ZqDhxFDm4xlO3TWvolPc2QTo/NI/MGBC0Jks/X76yl7LFzIj/z OCROsq3COipGhx+U6GmsWTDsdFAe2ziHydDfRaqwoWbkByDw7xPd0+Gd8iHmwWMR+xYB A3mZRLJJ6jPToyFLhUhDz/ivxmzHWe0KzpjCcag4o0P5lSB9U/cPGhPFQNICl6G8MsYh DIFQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=Apz5AiwuERNKhLmawgUaC5BdiOhLZhpn7eFMH23CW+k=; fh=Z9OxwEoraVjablDxL0xmDDCjhojj6K7KNXPO/9sdJGM=; b=fohMmxJxS/gOPjTQ0q+z5a1l5NidlPredWFvHp8wIBsbuHQU7gvaWxPG7ByV1feCqE Nb5D6VpjBgWLQwla4h+xsmeE2V/pzvO76LRdOeyjeMxZdiHE35FJzQmotuk0mNLDNzdr dppcBpNngFf/eq6CgPNKFHdwYAPTtRfhB1vmWTqPD00yVehNFGg7jVqtJsnY9hAe0SYi tRB0KdlOyvMS6zdA7yerV4UJBDx3SnUmv6CddGLDYTY4fZ1HipYcBXF1VMyPsvZ6a6oY vaAle73AmZ1aU91VEkRz3g8bpbR1sSXtNgmEBU0dymZBI2fvzs+zq2FK8+fNbWIpa+9J dl+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-93914-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93914-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id fq8-20020a056214258800b0068ce8312e16si14391652qvb.327.2024.03.06.04.44.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 04:44:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93914-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-93914-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93914-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0E8711C2265A for ; Wed, 6 Mar 2024 12:44:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 12140126F1C; Wed, 6 Mar 2024 12:43:52 +0000 (UTC) Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 DFF491DA4C; Wed, 6 Mar 2024 12:43:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709729031; cv=none; b=J4SlD1FrpYJQqcrIFfkMC8XJh0APcqTc75z5swoBufM6Ha0VhCld1L//ErKMvpfocrrF4/PViMkgEgFes9TH4eTq0gAOuZqNEfUP2f3xdGnS8EX3lZRAcK3tGHVR/RwOI9L1qkTLrrul2YmP9/TZawYb/S41l0zTjnFWsWnXhqU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709729031; c=relaxed/simple; bh=o5CYraV7yOFyyWAXdnfgODZ0zbgO7oV1ojCeLS2bet0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=dxlYGXZNeevv9yGp2nDd1mT9ubmy7HYL7LgH4dJjhrTsgwx0BCpN0kTEKK1xQuxYB8t2ETqhEdleylBJ1xBDWrSYZbIJ5uCAFrpLwovnLc0cuoGqcOD5FHW5svSmSeQHwjX7uX2lKfAeTib/BIY+m0l/aCsCVQN55qiFeAXz8gs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.167.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3c21e709953so82651b6e.0; Wed, 06 Mar 2024 04:43:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709729029; x=1710333829; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Apz5AiwuERNKhLmawgUaC5BdiOhLZhpn7eFMH23CW+k=; b=jKBGlwiRekBOx5VjcocyoqMgPdyngzL8SbCLP9YS/U9xvtSHq0GSB3Qmeo3DYn79LV KZvmpF21qncHpNoho+oXiPaByswOxJlgm0pxCAZkTSt052XdXhODH13hJSJfv3l6iXzg 2NpBN+VpvNvxgYHTOtlivBdQS5IlS5MlGP8qYH4ElpqLcbr+5WErK3qWUWTaLhXv7PJL ZECq67t+T/zfXNW7zyWy7ik9gY3Sgqt+XY7CY6VjZBLGj3EAYY6Ptp2Eiubh8G7CZcFG 4im0d+sN/VT3SR0+6PfQudPWoDvvy/NGJKa5P8Es/32TgXE7WxQCZmSA7RSDooj+wh2z xzZQ== X-Forwarded-Encrypted: i=1; AJvYcCVTyHKjehM2ItbTeXcns1U+BXUn5xiERgarMWbPQINCZjffm60qxXYBFtx1alriCr8ISb4wGjtVhv0nUE+n43L4yMEwuwPDiLu1LW7BnIiz+oKRKd+/SAwpWT8l6dRKo1+NaJPaTqsLP0TWYijQfDOb15YNrH0LY9gbVihd45lCKA== X-Gm-Message-State: AOJu0Yy4UMdS6o1jemYPX+kQJj4c2LLOThYyj2BOxG7bkfiyCvVXpIu2 i9xMhX/EssHqoan8v7p5qLvEinfBZLrRrvUA0kdfGkzJduCHA19YJAqWI2WgyfKoZFmjY7SW7Jv IMrtBymmH2qbEoC2zR3OHkVcXSy2NrUd4lks= X-Received: by 2002:a05:6871:3418:b0:221:3a21:5b37 with SMTP id nh24-20020a056871341800b002213a215b37mr3725623oac.4.1709729028877; Wed, 06 Mar 2024 04:43:48 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240306085007.169771-1-herve.codina@bootlin.com> <20240306085007.169771-2-herve.codina@bootlin.com> <1fff8742a13c28dd7e1dda47ad2d6fa8e21e421e.camel@gmail.com> In-Reply-To: <1fff8742a13c28dd7e1dda47ad2d6fa8e21e421e.camel@gmail.com> From: "Rafael J. Wysocki" Date: Wed, 6 Mar 2024 13:43:37 +0100 Message-ID: Subject: Re: [PATCH v4 1/2] driver core: Introduce device_link_wait_removal() To: =?UTF-8?B?TnVubyBTw6E=?= Cc: Herve Codina , Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Frank Rowand , Saravana Kannan , 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 , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 6, 2024 at 10:17=E2=80=AFAM Nuno S=C3=A1 wrote: > > On Wed, 2024-03-06 at 09:50 +0100, Herve Codina wrote: > > The commit 80dd33cf72d1 ("drivers: base: Fix device link removal") > > introduces a workqueue to release the consumer and supplier devices use= d > > 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 remove= d > > (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 rais= e > > 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. > > > > Fixes: 80dd33cf72d1 ("drivers: base: Fix device link removal") > > Cc: stable@vger.kernel.org > > Signed-off-by: Herve Codina > > --- > > With the below addressed: > > Reviewed-by: Nuno Sa > > > 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 d5f4e4aac09b..48b28c59c592 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; > > > > /** > > * __fwnode_link_add - Create a link between two fwnode_handles. > > @@ -532,12 +533,26 @@ static void devlink_dev_release(struct device *de= v) > > /* > > * It may take a while to complete this work because of the SRCU > > * synchronization in device_link_release_fn() and if the consume= r 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); > > } > > > > +/** > > + * device_link_wait_removal - Wait for ongoing devlink removal jobs to > > terminate > > + */ > > +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 a= ny > > + * 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, > > @@ -4099,9 +4114,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; > > > > I can't still agree with this. Why not doing it in devlink_class_init()? = This is > devlink specific so it makes complete sense to me. If you do that in devlink_class_init() and it fails, you essentially cause the creation of every device link to fail. IOW, you try to live without device links and pretend that it is all OK. That won't get you very far, especially on systems where DT is used. Doing it here, if it fails, you prevent the driver model from working at all (because one of its necessary components is unavailable), which arguably is a better choice.