From patchwork Fri Jul 7 15:10:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 117197 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3363213vqx; Fri, 7 Jul 2023 08:52:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlGuAfJ3Y18S/PlUEV5wcGHBPs7xfeNPQVhQdaGKKbERPjxpLHNhacHDJjCNoFG5yuKaZ6HF X-Received: by 2002:a17:906:72d9:b0:993:7594:99ae with SMTP id m25-20020a17090672d900b00993759499aemr4693487ejl.0.1688745121848; Fri, 07 Jul 2023 08:52:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688745121; cv=none; d=google.com; s=arc-20160816; b=V1zYjXRG0dajt61AIi5aLnIPr8C5zCCXDXipFj40+Blem3NfWZBGtaoJbdsY5Lt8Ee JBIkVb1BBYjJzUlKjGIwxz6PCLcmlOM1QSc6bXxgP04rNCQrugs7dd6DWDrygMsyrqMv BcbGBL+NoUA5zupt/pNCVEEPePp37DETJwRRGtY8ovHveROABktxsQbRQWH7enxxcrKU DBJVHekZEhMXLjZvQ0pEHlqLVANiV6UoVC6ThybfOllpmvWYmukH9niyrfLu66aRcGPv 67hjXKuKUAmQaIxdJErLtbaUbLhRJ947GaWQMjcAY7VBsAGdvbZhgIvbsG8W+BAK6Sgu 392g== 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=/vpgghRxsUnXsE3t1IVpXcHF3Kir76KUPEpQodsytdc=; fh=EkXd9mulaEwhN/gYQbLN171OqS+TkA74SfbkC4a6Z8Y=; b=xL0R0qYXrvnLm3xp635iACMkkSBaRtV863jBu0JjIbgM2Ms3aeqhFIae+v5oK12Hc7 15lAS8RWtu8bxvw/hX/1AIBA9Jp4bq4f9DeUA/zdMCj8esh6q/6aSVBzP7ow5tSvWD5d iTMut6PViykJdQSS9nRtZykiyHV67EpcxKgCKDK7JW2rAUXNg+wqqH4zZczQvdrrfzI/ KAVM6Xks9sbJwY/JHRpW09KBxy8pDqiA/RH8dZb1F6KXBIpxovJcGa8ScO7LTWTsXRHm tqiLzL3gul3TzOFywRYqYV2ddJaSNDqPAoG8J/QrfQAQayWDlMR4hzTNVtdjicy9fgQy 6uYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c+nLXn4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a1709061c0900b0099327546526si883091ejg.506.2023.07.07.08.51.38; Fri, 07 Jul 2023 08:52:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c+nLXn4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233364AbjGGPMZ (ORCPT + 99 others); Fri, 7 Jul 2023 11:12:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbjGGPMT (ORCPT ); Fri, 7 Jul 2023 11:12:19 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8199A212C for ; Fri, 7 Jul 2023 08:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688742688; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=/vpgghRxsUnXsE3t1IVpXcHF3Kir76KUPEpQodsytdc=; b=c+nLXn4OZLR9JopyYhq/kLKcu5hVDcMJgVZSqqvUmMU9sSkNL6kfZhp26EcF8NDrZIe0wi l0MWnOpFXfPNaOUuB2C1NUvJ3AZ1VW6xwfD6/Ztxvn7VL05I1e2pmxNXbXFZ9qB83+YAgF sD5KsFJ5eqPjy9XcVtxU7Ied6RcbTIU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-425-huiRPxb7MyqWJbXufgI7Jw-1; Fri, 07 Jul 2023 11:11:27 -0400 X-MC-Unique: huiRPxb7MyqWJbXufgI7Jw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D28691C060C9; Fri, 7 Jul 2023 15:11:26 +0000 (UTC) Received: from omen.home.shazbot.org (unknown [10.22.17.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id 70FCAC09A09; Fri, 7 Jul 2023 15:11:26 +0000 (UTC) From: Alex Williamson To: bhelgaas@google.com Cc: Alex Williamson , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, eric.auger@redhat.com Subject: [PATCH] PCI/VPD: Add runtime power management to sysfs interface Date: Fri, 7 Jul 2023 09:10:44 -0600 Message-Id: <20230707151044.1311544-1-alex.williamson@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770777604928309301?= X-GMAIL-MSGID: =?utf-8?q?1770777604928309301?= Unlike default access to config space through sysfs, the vpd read and write function don't actively manage the runtime power management state of the device during access. Since commit 7ab5e10eda02 ("vfio/pci: Move the unused device into low power state with runtime PM"), the vfio-pci driver will use runtime power management and release unused devices to make use of low power states. Attempting to access VPD information in this low power state can result in incorrect information or kernel crashes depending on the system behavior. Wrap the vpd read/write bin attribute handlers in runtime PM and take into account the potential quirk to select the correct device to wake. Signed-off-by: Alex Williamson --- drivers/pci/vpd.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c index a4fc4d0690fe..81217dd4789f 100644 --- a/drivers/pci/vpd.c +++ b/drivers/pci/vpd.c @@ -275,8 +275,23 @@ static ssize_t vpd_read(struct file *filp, struct kobject *kobj, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); + struct pci_dev *vpd_dev = dev; + ssize_t ret; + + if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) { + vpd_dev = pci_get_func0_dev(dev); + if (!vpd_dev) + return -ENODEV; + } + + pci_config_pm_runtime_get(vpd_dev); + ret = pci_read_vpd(vpd_dev, off, count, buf); + pci_config_pm_runtime_put(vpd_dev); + + if (dev != vpd_dev) + pci_dev_put(vpd_dev); - return pci_read_vpd(dev, off, count, buf); + return ret; } static ssize_t vpd_write(struct file *filp, struct kobject *kobj, @@ -284,8 +299,23 @@ static ssize_t vpd_write(struct file *filp, struct kobject *kobj, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); + struct pci_dev *vpd_dev = dev; + ssize_t ret; + + if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) { + vpd_dev = pci_get_func0_dev(dev); + if (!vpd_dev) + return -ENODEV; + } + + pci_config_pm_runtime_get(vpd_dev); + ret = pci_write_vpd(vpd_dev, off, count, buf); + pci_config_pm_runtime_put(vpd_dev); + + if (dev != vpd_dev) + pci_dev_put(vpd_dev); - return pci_write_vpd(dev, off, count, buf); + return ret; } static BIN_ATTR(vpd, 0600, vpd_read, vpd_write, 0);