Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp6060pxb; Thu, 7 Jan 2021 17:26:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJxgOOxpautdHEwYyUUj6YIBMmPXOZdF0JMJWzRdy6sVmVVpj1wLkIv/ByXJbkxjqby83GrV X-Received: by 2002:a17:906:add7:: with SMTP id lb23mr1163232ejb.352.1610069196054; Thu, 07 Jan 2021 17:26:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610069196; cv=none; d=google.com; s=arc-20160816; b=s2t1jdLvm8fNZyopBAX/g/n55s8nFRNEbthNelmjkx7+DWaOIKxl5kDSnM/nzAZLda W4AUWmeO8tatzb9ckOYIr8Uw2aMmW2yQYFApikq/+pR1w3GaKCAgIWgUO6hOljzVL2qU DLgktGI61D4ex/1zFSwQj/HXUb0u/PLy1qPauSk9nlDdRTMgLz1mws5iLNcJGArpgFMm bGHxIaoJNWYZkjHxpS0hX9cUX/D7oUs7hdrNjsX0D9ypkOaK0Ek8h69+JPi0k8TzPJ8A u5mP8rlmL/M/f8j2iA7xk8b0MoFCEotaxh2MzIX+qR4rL6UbVUYFFqH5TSLtWQdkX8qW 3THg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :sender:dkim-signature; bh=KOAVDThdKzrh65X/GbWaYtQc60+9XyXrjP6Yz10hBpg=; b=IYw0qdmlKdEGWfrc1dLyQ1nlPfkn/QTWyF8PjotkrkxLn9arPvR6XZuT4vfCknKToS 7LSyAutdf9+c11fzACojQAQlAODCULU9aJRK4YjkYpKvXXnPnXhyIltlc18G6723oAjl SQdtR14+AnW/2hORz07onFku1p0CxTt9oo8HGw1oovn4Ru2/XxZu6+nLLaHaAelCPHld 7MBhGcU96ExP9BSmMYbGMoCIoANCaZPdpFvg0E3xb8zu3ic8RRnAPilzgHG/MElfrwlO e/32nJqFB9a4s/BE970ZihSiAj+kmp2hD+Hixofbnv8lTylvdl5xxJaruX9DnfjM5Lki yxZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RBGLN+zZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id cx27si2812294edb.264.2021.01.07.17.26.11; Thu, 07 Jan 2021 17:26:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RBGLN+zZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729637AbhAHBZM (ORCPT + 99 others); Thu, 7 Jan 2021 20:25:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727634AbhAHBZM (ORCPT ); Thu, 7 Jan 2021 20:25:12 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 294A0C0612F4 for ; Thu, 7 Jan 2021 17:24:32 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id l8so13506970ybj.16 for ; Thu, 07 Jan 2021 17:24:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=KOAVDThdKzrh65X/GbWaYtQc60+9XyXrjP6Yz10hBpg=; b=RBGLN+zZzbIyfD+Sx/8r3MreHegdReAedekFKpvtaPNNaqDbxkXYUj2VDgIfMU2A+a 0gSrIzpImGQ7lN+roVMUsqqZ/U6UKXglPQAKcLOxtuXz5XCZrSBzdnRyeyAZ6P7rAP2+ p20EzqAtITf3uOy8l+7o8K8aplevxQgr8mcMGEN/YLXWuDLt8A/lOi6vRoJ24r/ltKVD kyQB7m52qcSKNuifYv9rjJ5XWDxv3vyUrdczvAB0Al217clWckEOomaFHNDcQuiAkhs2 wb9lWUU5U2EJa53S5Bd1MeIOxKCQDs1WnLO2WDPO0iabFn/+7S/upoYubbaTArj+IHqn XzIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=KOAVDThdKzrh65X/GbWaYtQc60+9XyXrjP6Yz10hBpg=; b=YpVxufkrXyP14gpni7OuF+BQt2qmqHVSWXjTzPrBHsuba2+jQLG1wm2pobwWcGQ+cI 6II2328Nbg2II3u8iORif6IoZoq3/snHy6eITqfAC+xiApGkkArLZgvQOtKsWtvz6m+G Fl//0yatqk+2ZIhMu1npcceQvLVITC3s+05QbB5gUGgvmgDuTuqY0XdeNvSGMmBiiS+7 6EyXBx15KIUEAJ0qIqsIZY61FSbuXnhcJ09FWIAb3UGgqP6RudsP5qtVSs8mQBjDt4wb mrzseREi8BLfEQHivkdakCm5TZ6N3FFqw17phPlwKWqM2pTNZIyVVP6QAYHxt30bQDSl JE/Q== X-Gm-Message-State: AOAM530hva19lj/vi5MDDvg3nETpLAtQU+9ToQYjpi35PRpT1CjN87Z9 14FKU5k+VaSk5gPhEAduf9+AbzrIAJ1fk3U= Sender: "saravanak via sendgmr" X-Received: from saravanak.san.corp.google.com ([2620:15c:2d:3:7220:84ff:fe09:fedc]) (user=saravanak job=sendgmr) by 2002:a25:c095:: with SMTP id c143mr2229233ybf.119.1610069071305; Thu, 07 Jan 2021 17:24:31 -0800 (PST) Date: Thu, 7 Jan 2021 17:24:26 -0800 Message-Id: <20210108012427.766318-1-saravanak@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog Subject: [PATCH v3] driver core: Fix device link device name collision From: Saravana Kannan To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Saravana Kannan Cc: stable@vger.kernel.org, Michael Walle , kernel-team@android.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The device link device's name was of the form: -- This can cause name collision as reported here [1] as device names are not globally unique. Since device names have to be unique within the bus/class, add the bus/class name as a prefix to the device names used to construct the device link device name. So the devuce link device's name will be of the form: :--: [1] - https://lore.kernel.org/lkml/20201229033440.32142-1-michael@walle.cc/ Cc: stable@vger.kernel.org Fixes: 287905e68dd2 ("driver core: Expose device link details in sysfs") Reported-by: Michael Walle Signed-off-by: Saravana Kannan --- Documentation/ABI/testing/sysfs-class-devlink | 4 ++-- .../ABI/testing/sysfs-devices-consumer | 5 +++-- .../ABI/testing/sysfs-devices-supplier | 5 +++-- drivers/base/core.c | 17 +++++++++-------- include/linux/device.h | 12 ++++++++++++ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-class-devlink b/Documentation/ABI/testing/sysfs-class-devlink index b662f747c83e..8a21ce515f61 100644 --- a/Documentation/ABI/testing/sysfs-class-devlink +++ b/Documentation/ABI/testing/sysfs-class-devlink @@ -5,8 +5,8 @@ Description: Provide a place in sysfs for the device link objects in the kernel at any given time. The name of a device link directory, denoted as ... above, is of the form -- - where is the supplier device name and is - the consumer device name. + where is the supplier bus:device name and + is the consumer bus:device name. What: /sys/class/devlink/.../auto_remove_on Date: May 2020 diff --git a/Documentation/ABI/testing/sysfs-devices-consumer b/Documentation/ABI/testing/sysfs-devices-consumer index 1f06d74d1c3c..0809fda092e6 100644 --- a/Documentation/ABI/testing/sysfs-devices-consumer +++ b/Documentation/ABI/testing/sysfs-devices-consumer @@ -4,5 +4,6 @@ Contact: Saravana Kannan Description: The /sys/devices/.../consumer: are symlinks to device links where this device is the supplier. denotes the - name of the consumer in that device link. There can be zero or - more of these symlinks for a given device. + name of the consumer in that device link and is of the form + bus:device name. There can be zero or more of these symlinks + for a given device. diff --git a/Documentation/ABI/testing/sysfs-devices-supplier b/Documentation/ABI/testing/sysfs-devices-supplier index a919e0db5e90..207f5972e98d 100644 --- a/Documentation/ABI/testing/sysfs-devices-supplier +++ b/Documentation/ABI/testing/sysfs-devices-supplier @@ -4,5 +4,6 @@ Contact: Saravana Kannan Description: The /sys/devices/.../supplier: are symlinks to device links where this device is the consumer. denotes the - name of the supplier in that device link. There can be zero or - more of these symlinks for a given device. + name of the supplier in that device link and is of the form + bus:device name. There can be zero or more of these symlinks + for a given device. diff --git a/drivers/base/core.c b/drivers/base/core.c index 25e08e5f40bd..4140a69dfe18 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -456,7 +456,9 @@ static int devlink_add_symlinks(struct device *dev, struct device *con = link->consumer; char *buf; - len = max(strlen(dev_name(sup)), strlen(dev_name(con))); + len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), + strlen(dev_bus_name(con)) + strlen(dev_name(con))); + len += strlen(":"); len += strlen("supplier:") + 1; buf = kzalloc(len, GFP_KERNEL); if (!buf) @@ -470,12 +472,12 @@ static int devlink_add_symlinks(struct device *dev, if (ret) goto err_con; - snprintf(buf, len, "consumer:%s", dev_name(con)); + snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); if (ret) goto err_con_dev; - snprintf(buf, len, "supplier:%s", dev_name(sup)); + snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); if (ret) goto err_sup_dev; @@ -737,8 +739,9 @@ struct device_link *device_link_add(struct device *consumer, link->link_dev.class = &devlink_class; device_set_pm_not_required(&link->link_dev); - dev_set_name(&link->link_dev, "%s--%s", - dev_name(supplier), dev_name(consumer)); + dev_set_name(&link->link_dev, "%s:%s--%s:%s", + dev_bus_name(supplier), dev_name(supplier), + dev_bus_name(consumer), dev_name(consumer)); if (device_register(&link->link_dev)) { put_device(consumer); put_device(supplier); @@ -1808,9 +1811,7 @@ const char *dev_driver_string(const struct device *dev) * never change once they are set, so they don't need special care. */ drv = READ_ONCE(dev->driver); - return drv ? drv->name : - (dev->bus ? dev->bus->name : - (dev->class ? dev->class->name : "")); + return drv ? drv->name : dev_bus_name(dev); } EXPORT_SYMBOL(dev_driver_string); diff --git a/include/linux/device.h b/include/linux/device.h index 89bb8b84173e..1779f90eeb4c 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -609,6 +609,18 @@ static inline const char *dev_name(const struct device *dev) return kobject_name(&dev->kobj); } +/** + * dev_bus_name - Return a device's bus/class name, if at all possible + * @dev: struct device to get the bus/class name of + * + * Will return the name of the bus/class the device is attached to. If it is + * not attached to a bus/class, an empty string will be returned. + */ +static inline const char *dev_bus_name(const struct device *dev) +{ + return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : ""); +} + __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...); #ifdef CONFIG_NUMA -- 2.29.2.729.g45daf8777d-goog