Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2727777ybi; Mon, 1 Jul 2019 17:50:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzDYQ8F8lIFxNbQ2TkkEVxzZFpfC0EDWkaltshMWQs8HSW2S5yIpcoPmz4VYme1cOhrdun X-Received: by 2002:a17:902:a607:: with SMTP id u7mr32433920plq.43.1562028645194; Mon, 01 Jul 2019 17:50:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562028645; cv=none; d=google.com; s=arc-20160816; b=sJQ8GYs63lxt6gWW4M3aTpugg9B5t+Eic55p8DC7lOgM46O0BHEIDyCGoi3bMNI32x Vb1BCeBbIEhpu3Fo+91Bzk0TPwmIqDxn6wxbqDqXby3vcmSGTb+JZKgFNDXLLKoJ/Vsj 5mVXuEDmoWjrYIDKXmeSSinhhfKOpb6Kx7zsA8Cr+vFICEWk31KMwmUY9cqBku6tHhGM TDfOWKiASfeiEKg0bM8aOTDbaxuTbQqAzk+0GXJTFey86eIEHBOLWv73W9Xq14RwASQK LMtAhcP0DSxa+19TBv3FD3449ox64yB9GgOgtK8cke6kWmcHN1gri66umwBVE2vwADoh ymig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=j4A0VBbQxqWAJn4h2BC6mIE2ciny+oMQFxJSAqNPL6M=; b=XxM6cwdSUS2bpJR2ZRcsNmhH++YkmqdQO/TqgWp06lmLWV5UhaTCvKkA5Jv57mZ1Nq Ns/exinjaiJPRkKDsHPph0sD+91fsdUN0glDF5/MVINxl1dcll/Q9NRfVLTjRVBL375G WWPax63hTq/Y1TssdmA9ddfGfYK5JDrXuu6g17EODiSthu27jEonLImtaSVnP7Cy+SEO S6JJV7i3SIyyE10turWmrN5NXIJ+6Pw0j0rrGkkaULJmnwLIdOg7qIWbFH0PFkEWlqb9 jmCyzAFXKZZs/ANe2nSfW4/2kCJPMQEjnDQwRfgZV93NYu/X0LJFTlKW9hCaw3h41MLf 0DOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mmWGo6hz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a190si5539364pge.4.2019.07.01.17.50.30; Mon, 01 Jul 2019 17:50:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mmWGo6hz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727175AbfGBAsb (ORCPT + 99 others); Mon, 1 Jul 2019 20:48:31 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:49279 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727144AbfGBAs3 (ORCPT ); Mon, 1 Jul 2019 20:48:29 -0400 Received: by mail-pf1-f202.google.com with SMTP id 145so3391924pfw.16 for ; Mon, 01 Jul 2019 17:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=j4A0VBbQxqWAJn4h2BC6mIE2ciny+oMQFxJSAqNPL6M=; b=mmWGo6hzzFOyZcPEBUIyroUD5ywVZX1TjYqjSFFVBD8+zJpHjjz30v1UAYkdLkgit5 HhNU/YsmznAE3hbVi5YOrDRF5gpDdL+5ubm/iXbdfrZ7rqVX2D3O91C/J+uRXccHxbPl vzRWTre6s2tx2SWe2V8KR9qnNphbfedZl34625sTb8UpSOqEcsw6HVV0nAIg+Hgs8Pa4 +WP7dEM/0H5KASFWyAk1kswIBLMcYXC7Tvx9jqL8U1DeYus4u+lfzzH7KDzGby2Q4cuD BBUaGIMmmrhsU99brZU0O8n16IOIgxbJ3iSdRFE7EoAlhZHlSCbavq2rwixzbrGfQjMF Hi+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=j4A0VBbQxqWAJn4h2BC6mIE2ciny+oMQFxJSAqNPL6M=; b=c+CSOG27JrYxsw6PQ0uKo3ZYdAGUjF2mY28kB9O0sbjG0/v5/jbWrlkBTsalGixw5r umdsxqWeQeyAZYgzQnOkUEw16Doj3ArTXCeVjKY6pfsPU88L48Nc+fV84HEFvWoX5yXT mw6oqODyWCekSwjCMmSmPHO4D4n15YoJ//Us2lHw6ayxCG7KGZoyhRggsA9a7Gq5R2D4 nSTCwH53ROH9k0KpBnsPP4Tw8bPJivwjkBDnAxnegyCqTv/usxZh98LCLWEMf2oTp4Tx YRjIp03qycDOwC/8+E9R6+O0zKZYWPLUA7Lvrpw1WcSNkbZ9zHXN6y0HZnt/stgaw8hh VSAw== X-Gm-Message-State: APjAAAVP7rM4Fwan1wgu8pxVva3OmU0xYgAA80/yp3rhTCF3EE5pwdy8 54kRJ3qWsifCP0U6fNb6d2x3FgmZRMKDp70= X-Received: by 2002:a63:1f47:: with SMTP id q7mr8133589pgm.264.1562028508630; Mon, 01 Jul 2019 17:48:28 -0700 (PDT) Date: Mon, 1 Jul 2019 17:48:11 -0700 In-Reply-To: <20190702004811.136450-1-saravanak@google.com> Message-Id: <20190702004811.136450-5-saravanak@google.com> Mime-Version: 1.0 References: <20190702004811.136450-1-saravanak@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH v3 4/4] driver core: Add edit_links() callback for drivers From: Saravana Kannan To: Rob Herring , Mark Rutland , Greg Kroah-Hartman , "Rafael J. Wysocki" , Frank Rowand Cc: Saravana Kannan , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, David Collins , kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The driver core/bus adding dependencies by default makes sure that suppliers don't sync the hardware state with software state before all the consumers have their drivers loaded (if they are modules) and are probed. However, when the bus incorrectly adds dependencies that it shouldn't have added, the devices might never probe. For example, if device-C is a consumer of device-S and they have phandles to each other in DT, the following could happen: 1. Device-S get added first. 2. The bus add_links() callback will (incorrectly) try to link it as a consumer of device-C. 3. Since device-C isn't present, device-S will be put in "waiting-for-supplier" list. 4. Device-C gets added next. 5. All devices in "waiting-for-supplier" list are retried for linking. 6. Device-S gets linked as consumer to Device-C. 7. The bus add_links() callback will (correctly) try to link it as a consumer of device-S. 8. This isn't allowed because it would create a cyclic device links. So neither devices will get probed since the supplier is dependent on a consumer that'll never probe (because it can't get resources from the supplier). Without this patch, things stay in this broken state. However, with this patch, the execution will continue like this: 9. Device-C's driver is loaded. 10. Device-C's driver removes Device-S as a consumer of Device-C. 11. Device-C's driver adds Device-C as a consumer of Device-S. 12. Device-S probes. 13. Device-S sync_state() isn't called because Device-C hasn't probed yet. 14. Device-C probes. 15. Device-S's sync_state() callback is called. Signed-off-by: Saravana Kannan --- drivers/base/core.c | 24 ++++++++++++++++++++++-- drivers/base/dd.c | 29 +++++++++++++++++++++++++++++ include/linux/device.h | 18 ++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 8b8b812d26f1..dce97b5f3536 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -423,6 +423,19 @@ static void device_link_wait_for_supplier(struct device *consumer) mutex_unlock(&wfs_lock); } +/** + * device_link_remove_from_wfs - Unmark device as waiting for supplier + * @consumer: Consumer device + * + * Unmark the consumer device as waiting for suppliers to become available. + */ +void device_link_remove_from_wfs(struct device *consumer) +{ + mutex_lock(&wfs_lock); + list_del_init(&consumer->links.needs_suppliers); + mutex_unlock(&wfs_lock); +} + /** * device_link_check_waiting_consumers - Try to unmark waiting consumers * @@ -440,12 +453,19 @@ static void device_link_wait_for_supplier(struct device *consumer) static void device_link_check_waiting_consumers(void) { struct device *dev, *tmp; + int ret; mutex_lock(&wfs_lock); list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, - links.needs_suppliers) - if (!dev->bus->add_links(dev)) + links.needs_suppliers) { + ret = 0; + if (dev->has_edit_links) + ret = driver_edit_links(dev); + else if (dev->bus->add_links) + ret = dev->bus->add_links(dev); + if (!ret) list_del_init(&dev->links.needs_suppliers); + } mutex_unlock(&wfs_lock); } diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 0df9b4461766..842fc7b704f9 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -659,6 +659,12 @@ int driver_probe_device(struct device_driver *drv, struct device *dev) pr_debug("bus: '%s': %s: matched device %s with driver %s\n", drv->bus->name, __func__, dev_name(dev), drv->name); + if (drv->edit_links) { + if (drv->edit_links(dev)) + dev->has_edit_links = true; + else + device_link_remove_from_wfs(dev); + } pm_runtime_get_suppliers(dev); if (dev->parent) pm_runtime_get_sync(dev->parent); @@ -747,6 +753,29 @@ struct device_attach_data { bool have_async; }; +static int __driver_edit_links(struct device_driver *drv, void *data) +{ + struct device *dev = data; + + if (!drv->edit_links) + return 0; + + if (driver_match_device(drv, dev) <= 0) + return 0; + + return drv->edit_links(dev); +} + +int driver_edit_links(struct device *dev) +{ + int ret; + + device_lock(dev); + ret = bus_for_each_drv(dev->bus, NULL, dev, __driver_edit_links); + device_unlock(dev); + return ret; +} + static int __device_attach_driver(struct device_driver *drv, void *_data) { struct device_attach_data *data = _data; diff --git a/include/linux/device.h b/include/linux/device.h index 4a0db34ae650..d3c9e70052d8 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -262,6 +262,20 @@ enum probe_type { * @probe_type: Type of the probe (synchronous or asynchronous) to use. * @of_match_table: The open firmware table. * @acpi_match_table: The ACPI match table. + * @edit_links: Called to allow a matched driver to edit the device links the + * bus might have added incorrectly. This will be useful to handle + * cases where the bus incorrectly adds functional dependencies + * that aren't true or tries to create cyclic dependencies. But + * doesn't correctly handle functional dependencies that are + * missed by the bus as the supplier's sync_state might get to + * execute before the driver for a missing consumer is loaded and + * gets to edit the device links for the consumer. + * + * This function might be called multiple times after a new device + * is added. The function is expected to create all the device + * links for the new device and return 0 if it was completed + * successfully or return an error if it needs to be reattempted + * in the future. * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device. @@ -308,6 +322,7 @@ struct device_driver { const struct of_device_id *of_match_table; const struct acpi_device_id *acpi_match_table; + int (*edit_links)(struct device *dev); int (*probe) (struct device *dev); void (*sync_state)(struct device *dev); int (*remove) (struct device *dev); @@ -1082,6 +1097,7 @@ struct device { bool offline:1; bool of_node_reused:1; bool state_synced:1; + bool has_edit_links:1; #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) @@ -1331,6 +1347,7 @@ extern int __must_check device_attach(struct device *dev); extern int __must_check driver_attach(struct device_driver *drv); extern void device_initial_probe(struct device *dev); extern int __must_check device_reprobe(struct device *dev); +extern int driver_edit_links(struct device *dev); extern bool device_is_bound(struct device *dev); @@ -1423,6 +1440,7 @@ void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); int device_links_supplier_sync_state(struct device *dev, void *data); void device_links_supplier_sync_state_enable(void); +void device_link_remove_from_wfs(struct device *consumer); #ifndef dev_fmt #define dev_fmt(fmt) fmt -- 2.22.0.410.gd8fdbe21b5-goog