From patchwork Wed Sep 20 18:59:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: stuart hayes X-Patchwork-Id: 142576 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp4395233vqi; Wed, 20 Sep 2023 13:07:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYw31hbO+4ekD8EKnq3sTaDQMJJACgTxi+VEkJBnIh22jJyc0MFI0TWbKZML4k7abkqGGm X-Received: by 2002:a17:902:dac5:b0:1c4:3ae6:381a with SMTP id q5-20020a170902dac500b001c43ae6381amr3666917plx.0.1695240453032; Wed, 20 Sep 2023 13:07:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695240453; cv=none; d=google.com; s=arc-20160816; b=iNdc3rQ7tW0chI5/KxWHuzn5xV11R2GISdkv2SVTfgx3AjIrtH7+reVb4RlaSEXzXE SuskYxIMY/rFDSPKGwWXGZROaFFl8mkaYGODtiitiHCaXAxRpyHgXUrWAmf0SNB/0SzN njsTV1eHanE9D5qUUaVUka5QD7K5oEsVSZTbAAbXEh+6t7p6AanlPguZpuJmOAu/mCNS qW3RJrpjOdg+2JcCqJoq5L7sqazArHsGyoRx2+wvu8mgEeN51eLTnFXz8y3Qo9sgBxEW p7pw5cWDdMr/wnZSkByE+jkRXAtIEzIt4NLRtjf/ymkKJbCblW02RXTOOxNmJiq7g55J Tv0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=eTsWnCm4WBdqltgR3xxPn5PFi60U+G+FBv6+XPeoNEg=; fh=4kcKpPpu8ta55eY4DoPd4YM50LeMY+jLeP5YtAqVdoE=; b=ZUgEqrETiXdkjiYJok2lAydMGfaNU3ricni5sxn1jvciWF+2juQCEhezW8ZJ/cIbBo kKoeoFpzs2vVqxpWRuwcR8OQDwn1FzdWwcE6qB0qqj1k5XmK30pbGGnJdnSWR2v65Nwv e/nryGKfQrjIIClbUJOL6CTb/x/0wtIqLJ5xxN+95c42tVj3zgmDelfhPAlMhAsN8doz 0eCy3aL2/r+s5URYDFaUdWANQSyGR4ZYRjAsOc7xdCBUa6DpV0yBGknuOSNfK167+Ud+ +l3/qidjE410BRJGWq1HOBC0L33PTSaFkkOCunveS22ffraV7voE0bZcfd0iPjnvUQZP D+AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=V5vq+oEA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id d10-20020a170902654a00b001c364978a9csi11851433pln.362.2023.09.20.13.07.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 13:07:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=V5vq+oEA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 241F48263776; Wed, 20 Sep 2023 12:00:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229842AbjITS75 (ORCPT + 27 others); Wed, 20 Sep 2023 14:59:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229825AbjITS7z (ORCPT ); Wed, 20 Sep 2023 14:59:55 -0400 Received: from mail-oo1-xc2d.google.com (mail-oo1-xc2d.google.com [IPv6:2607:f8b0:4864:20::c2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D82AC9 for ; Wed, 20 Sep 2023 11:59:49 -0700 (PDT) Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5738949f62cso123200eaf.0 for ; Wed, 20 Sep 2023 11:59:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695236388; x=1695841188; 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=eTsWnCm4WBdqltgR3xxPn5PFi60U+G+FBv6+XPeoNEg=; b=V5vq+oEAkikwTjjjscIYLkbJcxM9d1PhjozZM1AhYltycpP56KIJbvq8HewYSnU+p9 VmYbuhY/OjR3HoL7C0PwRHZ144zjzI7t+LrDWX4dEeVHbeB5tAG8ZTlzvyYpQDEiaUEo AlwTRa6s7nfUt8BQDcFle5CafWlw/qbDfI7cGXcS2rE4cwPNkeyGwStFgag0qkCPlBam ypLATPytWe8eRJMlHYGBDhWvEjXulzpFk9/frZsp5uxf8C8eCCrcKOnPXUJr2tseY5T2 j1DBXqUiPPjN30ixj+C9fvo+FRwj8uDx0/zJW+3qmWtrD6GVCMOFKFXINbq2r/L9Io5p MzFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695236388; x=1695841188; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eTsWnCm4WBdqltgR3xxPn5PFi60U+G+FBv6+XPeoNEg=; b=vfPnbpXLVb+SHcdZkGJvTbRMZEbCoPNeiX1JiYpCL6+099kfE+1uUfETlovw+g9ZFO zFIRoYno7tQp1+Eg9H6cVRuTnlXekSj+97uCmDYB8tXHAPojYYkQTQcLTr74NlPiy0tW g0nKSYDdVybAvW8OQfk6h7djHkiSuAfhQFFp6AENJ7/DlxVKVApGFQJQlaazlpGYKGJA Rw1dJHDgo56A+x2MQgyL7hUUsSiVQy9O6NcDYyWl7UrSHV/mEnr07x3MAypcgnugnMGR pyhmQfcY7gdPZD2S1GJdtNcGV9XHyInlVeqfQSurr+1DAmgF/kAYhEWFUoQ+rL1sSsuj o9Sg== X-Gm-Message-State: AOJu0YwpH3Zq2guWRuXBg1mjsQYhI2FJWiMP6fP7ZRysmjddrvO6Xvxh ZIMQAf2V8iKrEqEBz31RGWNl3AFk4mQ= X-Received: by 2002:a4a:3c54:0:b0:56d:c55b:4792 with SMTP id p20-20020a4a3c54000000b0056dc55b4792mr4070515oof.6.1695236387785; Wed, 20 Sep 2023 11:59:47 -0700 (PDT) Received: from localhost.localdomain ([143.166.81.254]) by smtp.gmail.com with ESMTPSA id v17-20020a4a9751000000b00573f5173a57sm7022440ooi.23.2023.09.20.11.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 11:59:47 -0700 (PDT) From: Stuart Hayes To: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , "Rafael J . Wysocki" , Tanjore Suresh , Martin Belanger , Oliver O'Halloran , Daniel Wagner , Keith Busch , Lukas Wunner Cc: Stuart Hayes Subject: [PATCH v3] driver core: shut down devices asynchronously Date: Wed, 20 Sep 2023 13:59:23 -0500 Message-Id: <20230920185923.3422-1-stuart.w.hayes@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 20 Sep 2023 12:00:08 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777588452989822626 X-GMAIL-MSGID: 1777588452989822626 Shut down devices asynchronously, ensuring that each device is shut down before its parents. This can dramatically reduce system shutdown/reboot time on systems that have devices that take many seconds to shut down, such as some NVMe drives. On one system tested, the shutdown time went from 11 minutes without this patch to 55 seconds with the patch. Signed-off-by: Stuart Hayes --- v1->v2: rewritten using kernel async code (suggested by Lukas Wunner) v2->v3: removed recursive functions to schedule children to be shutdown before parents, since existing device_shutdown loop will already do this --- drivers/base/base.h | 3 ++ drivers/base/core.c | 102 ++++++++++++++++++++++++++---------------- include/linux/async.h | 6 +++ 3 files changed, 73 insertions(+), 38 deletions(-) diff --git a/drivers/base/base.h b/drivers/base/base.h index eb4c0ace9242..9b9d80e575ca 100644 --- a/drivers/base/base.h +++ b/drivers/base/base.h @@ -11,6 +11,7 @@ * */ #include +#include /** * struct subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure. @@ -97,6 +98,7 @@ struct driver_private { * the device; typically because it depends on another driver getting * probed first. * @async_driver - pointer to device driver awaiting probe via async_probe + * @child_domain - domain for async shutdown work of children * @device - pointer back to the struct device that this structure is * associated with. * @dead - This device is currently either in the process of or has been @@ -114,6 +116,7 @@ struct device_private { struct list_head deferred_probe; struct device_driver *async_driver; char *deferred_probe_reason; + struct async_domain child_domain; struct device *device; u8 dead:1; }; diff --git a/drivers/base/core.c b/drivers/base/core.c index 4d8b315c48a1..b0f71e64125c 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -3473,6 +3474,7 @@ static int device_private_init(struct device *dev) klist_init(&dev->p->klist_children, klist_children_get, klist_children_put); INIT_LIST_HEAD(&dev->p->deferred_probe); + INIT_ASYNC_DOMAIN_EXCLUSIVE(&dev->p->child_domain); return 0; } @@ -4718,11 +4720,68 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) } EXPORT_SYMBOL_GPL(device_change_owner); +/** + * shutdown_device - shutdown one device + * @data: the pointer to the struct device to be shutdown + * @cookie: not used + * + * This shuts down one device, after waiting for children to finish + * shutdown. This should be scheduled for any children first. + */ +static void shutdown_device(void *data, async_cookie_t cookie) +{ + struct device *dev = data; + + /* + * wait for shutdown work of children to finish + */ + async_synchronize_full_domain(&dev->p->child_domain); + + /* + * Make sure the device is off the kset list, in the + * event that dev->*->shutdown() doesn't remove it. + */ + spin_lock(&devices_kset->list_lock); + list_del_init(&dev->kobj.entry); + spin_unlock(&devices_kset->list_lock); + + /* hold lock to avoid race with probe/release */ + if (dev->parent) + device_lock(dev->parent); + device_lock(dev); + + /* Don't allow any more runtime suspends */ + pm_runtime_get_noresume(dev); + pm_runtime_barrier(dev); + + if (dev->class && dev->class->shutdown_pre) { + if (initcall_debug) + dev_info(dev, "shutdown_pre\n"); + dev->class->shutdown_pre(dev); + } + if (dev->bus && dev->bus->shutdown) { + if (initcall_debug) + dev_info(dev, "shutdown\n"); + dev->bus->shutdown(dev); + } else if (dev->driver && dev->driver->shutdown) { + if (initcall_debug) + dev_info(dev, "shutdown\n"); + dev->driver->shutdown(dev); + } + + device_unlock(dev); + if (dev->parent) + device_unlock(dev->parent); + + put_device(dev); +} + /** * device_shutdown - call ->shutdown() on each device to shutdown. */ void device_shutdown(void) { + ASYNC_DOMAIN_EXCLUSIVE(top_domain); struct device *dev, *parent; wait_for_device_probe(); @@ -4732,20 +4791,14 @@ void device_shutdown(void) spin_lock(&devices_kset->list_lock); /* - * Walk the devices list backward, shutting down each in turn. - * Beware that device unplug events may also start pulling + * Walk the devices list backward, scheduling shutdown of each in + * turn. Beware that device unplug events may also start pulling * devices offline, even as the system is shutting down. */ while (!list_empty(&devices_kset->list)) { dev = list_entry(devices_kset->list.prev, struct device, kobj.entry); - /* - * hold reference count of device's parent to - * prevent it from being freed because parent's - * lock is to be held - */ - parent = get_device(dev->parent); get_device(dev); /* * Make sure the device is off the kset list, in the @@ -4754,40 +4807,13 @@ void device_shutdown(void) list_del_init(&dev->kobj.entry); spin_unlock(&devices_kset->list_lock); - /* hold lock to avoid race with probe/release */ - if (parent) - device_lock(parent); - device_lock(dev); - - /* Don't allow any more runtime suspends */ - pm_runtime_get_noresume(dev); - pm_runtime_barrier(dev); - - if (dev->class && dev->class->shutdown_pre) { - if (initcall_debug) - dev_info(dev, "shutdown_pre\n"); - dev->class->shutdown_pre(dev); - } - if (dev->bus && dev->bus->shutdown) { - if (initcall_debug) - dev_info(dev, "shutdown\n"); - dev->bus->shutdown(dev); - } else if (dev->driver && dev->driver->shutdown) { - if (initcall_debug) - dev_info(dev, "shutdown\n"); - dev->driver->shutdown(dev); - } - - device_unlock(dev); - if (parent) - device_unlock(parent); - - put_device(dev); - put_device(parent); + async_schedule_domain(shutdown_device, dev, + dev->parent ? &parent->p->child_domain : &top_domain); spin_lock(&devices_kset->list_lock); } spin_unlock(&devices_kset->list_lock); + async_synchronize_full_domain(&top_domain); } /* diff --git a/include/linux/async.h b/include/linux/async.h index cce4ad31e8fc..ab62402452f8 100644 --- a/include/linux/async.h +++ b/include/linux/async.h @@ -35,6 +35,12 @@ struct async_domain { struct async_domain _name = { .pending = LIST_HEAD_INIT(_name.pending), \ .registered = 0 } +static inline void INIT_ASYNC_DOMAIN_EXCLUSIVE(struct async_domain *domain) +{ + INIT_LIST_HEAD(&domain->pending); + domain->registered = 0; +} + async_cookie_t async_schedule_node(async_func_t func, void *data, int node); async_cookie_t async_schedule_node_domain(async_func_t func, void *data,