Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp145100iob; Tue, 17 May 2022 21:46:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsHvRh8YK7vXxnlvLPklN/d/1a+ZbHxd3DbObpM/dMlXQMdVKv4nyI4rNlRSHv4+M4Oao7 X-Received: by 2002:a17:90b:4f4e:b0:1dc:cafb:d48e with SMTP id pj14-20020a17090b4f4e00b001dccafbd48emr39783347pjb.202.1652849198637; Tue, 17 May 2022 21:46:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652849198; cv=none; d=google.com; s=arc-20160816; b=LLUPgAChdHTHBHSH8pVny5exzzeQmxXn5xCifafEZDeXEIZxkP/iHAQpUkpTaGvlqD mEQdwLqAKbwbNpuwtcotpKS8e/C1pTaPRb8HBA/FzcvInmW92juvIU8FE8hdo+C/hU0r qF+i79ZfOZ+pyYppBAfn/8PUwI8xh4J3WHaKp5bW8cwgkGjo7q/9NobrXm+Z5uhg3Wvh txsxK/UuKk2cKtpHV2n6XOEfVQ8sPTIrTmXzlSqC693xRLw31ey8NN+Mcln4DBYqUQq9 uWZ3ocpsgSQdHs4tPQZcYMfNFk5No/qgVJv7o0yD+VDT23VDt3d1GtLGhtTVeZ7b37Z/ LMBg== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=808kPoCVZ9QBXQQ50NY9PebUEFZ1B+K2Bsp/en8vgGY=; b=upPTs2gK/vCyzVSvkHfDudrmikCpRfRelJ83u6jPzFJszTwKrqswkWSbVyxlC0me4J Ixt/8kJglidvH8Vb2VKDafDN5AVsHhxugu2zjocLlbJ0IHdDaFp4of3WV+LCFje8K67b igye40AU5SSYSLXkIsu4EaQ+MnpZxL70AA8og1hiCr31XA86w7DVNMir/BciDR1GDYHD oxPWbFdVfKtvuObOQAXohmh2N1uRJZXgHGwW4/GEMHpUva59T8cXHfsAZRXMUKqfnwUU 6DurF8fKPScRBlUsc2/gttpnGz2XyPEfRMqt7bZ4yaS4z5TjZ8gk4V7urzPzKaylSUoe ko9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="hIpYi/sG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q2-20020a63e202000000b003daed425f7esi1097633pgh.420.2022.05.17.21.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 21:46:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="hIpYi/sG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3DE34104C9C; Tue, 17 May 2022 20:58:35 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351932AbiEQS1V (ORCPT + 99 others); Tue, 17 May 2022 14:27:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346758AbiEQS1S (ORCPT ); Tue, 17 May 2022 14:27:18 -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 ESMTP id CC3993879E for ; Tue, 17 May 2022 11:27:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652812035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=808kPoCVZ9QBXQQ50NY9PebUEFZ1B+K2Bsp/en8vgGY=; b=hIpYi/sGN2DecSuoV+aN+/5uy97WasBMnQL/n4kbh3XyBo3J2E4ufENMgixlxA7jfbwPyq NSG5GKRT4CCuW2QxV837+nuhLlAjJdFkVPD56gWrlx0tybTB4tsQsuIPvEc3mpfEifh+1m 79SrGJrgyTzkl3S1Q8OSGSJEmuOjDy0= Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-83-5bZm3BkdPXupJUXC7AJ6Vg-1; Tue, 17 May 2022 14:27:14 -0400 X-MC-Unique: 5bZm3BkdPXupJUXC7AJ6Vg-1 Received: by mail-il1-f198.google.com with SMTP id m9-20020a056e021c2900b002d13627892eso1960268ilh.20 for ; Tue, 17 May 2022 11:27:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:organization:mime-version:content-transfer-encoding; bh=808kPoCVZ9QBXQQ50NY9PebUEFZ1B+K2Bsp/en8vgGY=; b=gx+2SooNn5476SaYQBJq8Ck6DfpEKlo7FPZvlolUPw7IPdlqnP0CnT71e+XNapi0ot wk+mNaH8m9EfknGnEBIheI2HsOkHRyqLXs/H8k+gLrKHnQ/u0K3uwi9GWO8QDpn2pela mDvQRSiXdXSZEF6w4VbdZpschBqOh6gi74vE1SfGq2Qrkn8EcjVXLCxrhUkDs7Eyzxyj E7Nax4yW++TzINuZ4Bjbr5IwZ6HvYawD7AbTUsd6ll8nyc1RHWFCnNTo2rZ5rlc3WmRF arAiH3/e6H62Db3W0cn36PDpkYQ49OHY5PR4D0V33uZifUHwlbSjMnHkiY1i7bJRFdT4 p/ng== X-Gm-Message-State: AOAM532WGsQXLZD4RS+KkDiVT25BjM8PybwMYEvi4RH2ZRr28A/Oi2mh fpNUyJn4Ed5rhaUe5sbyWBK17DiUYOgGKBSH3ac6XuFZV6Zc01RBY+ksdPOMGaXCGd/e5f3FgJQ 51YrD+z5R7n2NbIlU/kX6yHmU X-Received: by 2002:a05:6638:140d:b0:32b:c643:e334 with SMTP id k13-20020a056638140d00b0032bc643e334mr13045263jad.125.1652812033716; Tue, 17 May 2022 11:27:13 -0700 (PDT) X-Received: by 2002:a05:6638:140d:b0:32b:c643:e334 with SMTP id k13-20020a056638140d00b0032bc643e334mr13045242jad.125.1652812033478; Tue, 17 May 2022 11:27:13 -0700 (PDT) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id r4-20020a02c844000000b0032e2dce10aesm1885083jao.160.2022.05.17.11.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 11:27:12 -0700 (PDT) Date: Tue, 17 May 2022 12:27:10 -0600 From: Alex Williamson To: Abhishek Sahu Cc: Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" , Max Gurtovoy , Bjorn Helgaas , , , , Subject: Re: [PATCH v4 2/4] vfio/pci: Change the PF power state to D0 before enabling VFs Message-ID: <20220517122710.093c9c19.alex.williamson@redhat.com> In-Reply-To: <20220517100219.15146-3-abhsahu@nvidia.com> References: <20220517100219.15146-1-abhsahu@nvidia.com> <20220517100219.15146-3-abhsahu@nvidia.com> Organization: Red Hat MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Tue, 17 May 2022 15:32:17 +0530 Abhishek Sahu wrote: > According to [PCIe v5 9.6.2] for PF Device Power Management States > > "The PF's power management state (D-state) has global impact on its > associated VFs. If a VF does not implement the Power Management > Capability, then it behaves as if it is in an equivalent > power state of its associated PF. > > If a VF implements the Power Management Capability, the Device behavior > is undefined if the PF is placed in a lower power state than the VF. > Software should avoid this situation by placing all VFs in lower power > state before lowering their associated PF's power state." > > From the vfio driver side, user can enable SR-IOV when the PF is in D3hot > state. If VF does not implement the Power Management Capability, then > the VF will be actually in D3hot state and then the VF BAR access will > fail. If VF implements the Power Management Capability, then VF will > assume that its current power state is D0 when the PF is D3hot and > in this case, the behavior is undefined. > > To support PF power management, we need to create power management > dependency between PF and its VF's. The runtime power management support > may help with this where power management dependencies are supported > through device links. But till we have such support in place, we can > disallow the PF to go into low power state, if PF has VF enabled. > There can be a case, where user first enables the VF's and then > disables the VF's. If there is no user of PF, then the PF can put into > D3hot state again. But with this patch, the PF will still be in D0 > state after disabling VF's since detecting this case inside > vfio_pci_core_sriov_configure() requires access to > struct vfio_device::open_count along with its locks. But the subsequent > patches related to runtime PM will handle this case since runtime PM > maintains its own usage count. > > Also, vfio_pci_core_sriov_configure() can be called at any time > (with and without vfio pci device user), so the power state change > needs to be protected with the required locks. > > Signed-off-by: Abhishek Sahu > --- > drivers/vfio/pci/vfio_pci_core.c | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c > index b9f222ca48cf..4fe9a4efc751 100644 > --- a/drivers/vfio/pci/vfio_pci_core.c > +++ b/drivers/vfio/pci/vfio_pci_core.c > @@ -217,6 +217,10 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat > bool needs_restore = false, needs_save = false; > int ret; > > + /* Prevent changing power state for PFs with VFs enabled */ > + if (pci_num_vf(pdev) && state > PCI_D0) > + return -EBUSY; > + > if (vdev->needs_pm_restore) { > if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) { > pci_save_state(pdev); > @@ -1960,6 +1964,13 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, > } > list_add_tail(&vdev->sriov_pfs_item, &vfio_pci_sriov_pfs); > mutex_unlock(&vfio_pci_sriov_pfs_mutex); > + > + /* > + * The PF power state should always be higher than the VF power > + * state. If PF is in the low power state, then change the > + * power state to D0 first before enabling SR-IOV. > + */ > + vfio_pci_lock_and_set_power_state(vdev, PCI_D0); But we need to hold memory_lock across the next function or else userspace could race a write to the PM register to set D3 before pci_num_vf() can protect us. Thanks, Alex > ret = pci_enable_sriov(pdev, nr_virtfn); > if (ret) > goto out_del;