Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4875598pxj; Wed, 12 May 2021 15:24:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygp1XtedFV9qapsDE2vDYyIQODu/BjDBEWiZ7/qHcOB8cdsH+5WIhNNdzZxF6X4Hxxjdkn X-Received: by 2002:a05:6402:c8:: with SMTP id i8mr46992148edu.57.1620858270544; Wed, 12 May 2021 15:24:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620858270; cv=none; d=google.com; s=arc-20160816; b=KXkSYayBJC0CM03NmpeS8wzNf4m1EX7SZGUBZWDEEp4AiGgOGx7xOW+Dif8CN91rbS ji6Fs9Ttwg5UqPxn8KCbbjCcKiTMohVDOSYSf+T+kBdvyfQUhmTV3t9QwCdrPimMmpj+ Wo0ypeHQl0G45GDVPkrvSTUQ/ZFBNppwavWPlNLMyikMpML6QkdCtFL84ZxnbpzZAH7W 1Z4fWpSgGejOcDatDlGfUwSQXiIuCdD88dcfUynuFhG6Qv+VgHjzvBzvw1JpTQJY8TrQ kraSky8fDQmIIj+gZKDo51bISLcRIjn09rffRB2BwkdawwGUYzSiH/si4Ik7t9YSAsG0 GWPQ== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=O4prwy0dt076IdQhcTAcUnDE5VADW8jC/nkDuCrMp/E=; b=HwFsyBGOhlyP4SaHbImQ7PiFhEkD9VAqS+/mkALi3LoqeMSMREBqx3KqG5bhZElwp/ 4Z5OUKFPWMrkGXlIOhH0uGgysNS21dLPwTTPOCPhhQHXu4PSkEwYYU3fuEdhF0fO7TNK kfm5ZtRwGn/ocQbNIkYkAS/sgXAGt6xCYG6EU6OGbv19P2OB8EL0R6vZIUqitLDrNlLd nZEji+lNoCHYQsPexNUYnMi/jBgQNIjvuxTpSxX0P1Cngf3H+b1Lt+mk790h4CJJBuaI ExeNXQfdBFz0ei1wTxD3UZ43yL+lvaXvbMZ72qjZwCWiYhk5LzBQvUjECEiqWrt/6PP1 VZyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cg5jrRhY; 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 bl1si1193022ejb.408.2021.05.12.15.24.06; Wed, 12 May 2021 15:24:30 -0700 (PDT) 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=cg5jrRhY; 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 S1443192AbhELWQz (ORCPT + 99 others); Wed, 12 May 2021 18:16:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347332AbhELVqW (ORCPT ); Wed, 12 May 2021 17:46:22 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 195F1C06134F for ; Wed, 12 May 2021 14:35:06 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id u7-20020a259b470000b02904dca50820c2so29639250ybo.11 for ; Wed, 12 May 2021 14:35:06 -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=O4prwy0dt076IdQhcTAcUnDE5VADW8jC/nkDuCrMp/E=; b=cg5jrRhYrcGnbAPJsnJ/5EYIxyKKBKikA6TdWTgM6hM+NftKs1GDRfa0rm+EChPCuZ t0L4Tq5/946AM60F0m7nlK2FWPIyXEp1S+RxTnYQBnWwKIMQ8XaeZTAh6YXklnGxXmG2 APCBf4UhSgHVeUqOQrA9ua0wH3PgO48v7agpbCJ1WztS5T0fe/eI/qqjaqIymxSRSplF qZO865O4FLo0EzxPr/oLNQWgbxq7Yxh90O0YiLR1TevdUqpziX5IGY1u+ZKDuvs3YpS3 Q5NEueKOa+IPrzmQkst2OXlVbH2/mXDVqVYkHFPu6P5nlpruN6MUDj9x9esFSpc9btKT zCag== 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=O4prwy0dt076IdQhcTAcUnDE5VADW8jC/nkDuCrMp/E=; b=J+R2TuEUuNvPPGZsTeriXO6nPvC8EpvcXTKy4Twtv0KoC+3QjG/w5NgRwiQSXc3LhU 4qbXJ82AJuupiWVs898EkjCB+EDv4MkK6tmp0VqOGEOzaV3/yaEwa9akAaUAn+osO10R ownKuWO9jBtKsnGxYeO5PRmguiH2zJl7akzEqEb1CtIocOhvqjCcmId/nsje9bad7liI 3g6zmEBo0rdNAZr0njqSrsWIpaU9dEtqhcOPs/GP6xxYrFGFb8iDapC1rXd9r3Vjy3JK 1+rF01aBYHX8NKl075q+A7OjtaiP+0FDJUJAeeREuQzUfOBXr1lqA/dPC+dF3xtycxIj KFxA== X-Gm-Message-State: AOAM530uN3VEJQIARyfmCuKVi99bcuJ2T3i888DohTG9TdyvIJ/ptfH9 pOruJtx/H9PH6fryXg1fjUj1LJ5IF/aI X-Received: from rajat2.mtv.corp.google.com ([2620:15c:202:201:45af:4b75:ef28:4f6d]) (user=rajatja job=sendgmr) by 2002:a25:dc89:: with SMTP id y131mr48833854ybe.493.1620855305337; Wed, 12 May 2021 14:35:05 -0700 (PDT) Date: Wed, 12 May 2021 14:34:57 -0700 In-Reply-To: <20210512213457.1310774-1-rajatja@google.com> Message-Id: <20210512213457.1310774-2-rajatja@google.com> Mime-Version: 1.0 References: <20210512213457.1310774-1-rajatja@google.com> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog Subject: [PATCH v3 2/2] PCI: Add sysfs "removable" attribute From: Rajat Jain To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Bjorn Helgaas , Alan Stern , Rajat Jain , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-usb@vger.kernel.org, helgaas@kernel.org, Oliver Neukum , David Laight , "=?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?=" Cc: rajatxjain@gmail.com, jsbarnes@google.com, dtor@google.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A PCI device is "external_facing" if it's a Root Port with the ACPI "ExternalFacingPort" property or if it has the DT "external-facing" property. We consider everything downstream from such a device to be removable by user. We're mainly concerned with consumer platforms with user accessible thunderbolt ports that are vulnerable to DMA attacks, and we expect those ports to be identified as "ExternalFacingPort". Devices in traditional hotplug slots can technically be removed, but the expectation is that unless the port is marked with "ExternalFacingPort", such devices are less accessible to user / may not be removed by end user, and thus not exposed as "removable" to userspace. Set pci_dev_type.supports_removable so the device core exposes the "removable" file in sysfs, and tell the device core about removable devices. This can be used by userspace to implment any policies it wants to, tailored specifically for user removable devices. Eg usage: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2591812 https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2795038 (code uses such an attribute to remove external PCI devicces or disable features on them as needed by the policy desired) Signed-off-by: Rajat Jain --- v3: - commit log updated - Rename set_pci_dev_removable() -> pci_set_removable() - Call it after applying early PCI quirks. v2: Add documentation Documentation/ABI/testing/sysfs-devices-removable | 3 ++- drivers/pci/pci-sysfs.c | 1 + drivers/pci/probe.c | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-devices-removable b/Documentation/ABI/testing/sysfs-devices-removable index 9dabcad7cdcd..ec0b243f5db4 100644 --- a/Documentation/ABI/testing/sysfs-devices-removable +++ b/Documentation/ABI/testing/sysfs-devices-removable @@ -14,4 +14,5 @@ Description: Currently this is only supported by USB (which infers the information from a combination of hub descriptor bits and - platform-specific data such as ACPI). + platform-specific data such as ACPI) and PCI (which gets this + from ACPI / device tree). diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index beb8d1f4fafe..38b3259ba333 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1541,4 +1541,5 @@ static const struct attribute_group *pci_dev_attr_groups[] = { const struct device_type pci_dev_type = { .groups = pci_dev_attr_groups, + .supports_removable = true, }; diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 3a62d09b8869..3515afeeaba8 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -1575,6 +1575,16 @@ static void set_pcie_untrusted(struct pci_dev *dev) dev->untrusted = true; } +static void pci_set_removable(struct pci_dev *dev) +{ + struct pci_dev *parent = pci_upstream_bridge(dev); + if (parent && + (parent->external_facing || dev_is_removable(&parent->dev))) + dev_set_removable(&dev->dev, DEVICE_REMOVABLE); + else + dev_set_removable(&dev->dev, DEVICE_FIXED); +} + /** * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config? * @dev: PCI device @@ -1822,6 +1832,8 @@ int pci_setup_device(struct pci_dev *dev) /* Early fixups, before probing the BARs */ pci_fixup_device(pci_fixup_early, dev); + pci_set_removable(dev); + pci_info(dev, "[%04x:%04x] type %02x class %#08x\n", dev->vendor, dev->device, dev->hdr_type, dev->class); -- 2.31.1.607.g51e8a6a459-goog