From patchwork Sat Apr 22 01:06:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brett Creeley X-Patchwork-Id: 13220860 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42B23C77B7F for ; Sat, 22 Apr 2023 01:07:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232902AbjDVBHo (ORCPT ); Fri, 21 Apr 2023 21:07:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234121AbjDVBHg (ORCPT ); Fri, 21 Apr 2023 21:07:36 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2061.outbound.protection.outlook.com [40.107.94.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED9102719; Fri, 21 Apr 2023 18:07:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DUMq6QZYQrTA/930FbEzgbZILft2ZjHvHB6ZUOlmJ7gbpIHcyE4jp/wv36T7fd084RKZ1dz2Vej/SZu9keyit4LdDXfaifHIFe0eX6ZS9W3/zVD3Cxdj7b6yUGahGHkDykPg+6k8jw1rtgefNiUSbNovUsjveku/os8dfvPc+1nc9dhSVqI9SaOFPuw06VyZzDZyrlqDDlCR6A+tpmUN98bbbQMMZ5duhMnOBBR17KCh+ssdS5Dtq1bLX7wyxQ+Wo1JD1bXaMqOLxCq0FcNc88cTogW8HLlxSd4GAbph3mJ527KoGXzE2p9Yhhdoc4rVSOjyx420ucpfDoWkRAyT8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hxtxk1ZpvyrKOOhVoQcB2qJ5YUIOTUPC/7VpoUhu5iU=; b=DvDbdvUIPdE7VFnVfZmY9LeywWa7ktrpWvcua7w8B3m8/ZC7cUGxl1m9QrxBvglFVCD24/GkcGIeMBfc95UMgwWIk0TsHoV2FIqKgRCqrKExcXlsp5MGjiU1Qdp7ppF1+Ds1TTqk59qyxenqb42Ru0I+m6FRbkpengvEk0ceNB3yhG+Rz6kG5UhNZx0/0G7pw7D/IDRQYW6juj14d/5ILVyvNVzW0kiiILfKKsQ6u4rzQwf1WWa6B5e38+ZQOqPGv2HZT7yCZDJwOXH74Z4tIlmgTKfzkx4MUFAA8ViTHkOEdbKfu0m8zJUgpYSlJxWcGlh/WzvORCuKpnQc8/AaCg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hxtxk1ZpvyrKOOhVoQcB2qJ5YUIOTUPC/7VpoUhu5iU=; b=J/rxG/y7o82dnuOq/N2F04wwbz7Plg1icIyZPrBsvEEYB9HOQfCxujKdshZfWfeexh1/ayEmDHgmWvhjEycY9eZaTAx3NGFO8+EhI5d/lnBIIrNJ1/k+WO+5qYPtkUIK3fHRue9xKKWAKREAIm8dyZhdb2c/e9IHk6W7rc5j2F0= Received: from BN9PR03CA0764.namprd03.prod.outlook.com (2603:10b6:408:13a::19) by CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Sat, 22 Apr 2023 01:07:19 +0000 Received: from BN8NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:408:13a:cafe::3a) by BN9PR03CA0764.outlook.office365.com (2603:10b6:408:13a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.27 via Frontend Transport; Sat, 22 Apr 2023 01:07:19 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT003.mail.protection.outlook.com (10.13.177.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6319.25 via Frontend Transport; Sat, 22 Apr 2023 01:07:19 +0000 Received: from driver-dev1.pensando.io (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Fri, 21 Apr 2023 20:07:18 -0500 From: Brett Creeley To: , , , , , , CC: , , Subject: [PATCH v9 vfio 6/7] vfio/pds: Add support for firmware recovery Date: Fri, 21 Apr 2023 18:06:41 -0700 Message-ID: <20230422010642.60720-7-brett.creeley@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230422010642.60720-1-brett.creeley@amd.com> References: <20230422010642.60720-1-brett.creeley@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT003:EE_|CH2PR12MB4133:EE_ X-MS-Office365-Filtering-Correlation-Id: 97040736-11c1-4960-4033-08db42cdebf9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Sgb7E5jAdbnEIejpwW+6dkc21fuLXRm8qWxVFhCYJ4zvpj6msc9woChj/BtjUKePK4O/6+FbpRNPZkT67KqP5oxPWqNqbY3Z2SGQPZhq4ZYbREMOM6SwvVh1S5jQeJKSd0zMwiyq5FC9GeTpX4soWPiu75HcSW0+ZGSypt3LhHs3IvLeEI0KlJBbaKDw/lg2POzef9ZOp1A/4v61ZE21ico87HmJVhK7paKS6jmgIKIGVNc1NdbM+J+jaUuWgCBwGDTToZgc/CN8hq/J67d6BIu77j7l2kaSTTN4ISjdj/LcxuPvXjY8SdfkMXt14e+kREV/N5q4TiefJjWMOE41k57VIwJAACKWsYAlkb/DtZn/m3PkY1fdVim1L2gr5vm4fPA5TCxmDNwb+/gqYjpF+uCTdkhyz3hJmPL0+m2IXngb8dtQ58CFyB7h6I8aKqmtvDAt8U4dAjsNLGPGPt2tgLmcIWTtg/r6DgRRvz6bdbAhnxSjY/jyGh/F2U+fez71xTkJLPhDmtFV716hEjzH66u43WqmbFu2BYQTXUMsivmjMr2QbIxQG2meF1BhfFWUy5WH+y5BLF32vO4fA5ztFebbjBlg3qyln4Sg/Fg9R+Kn6l5JzBb63hylOoj9kTwu7Kbty9avPs/5qGGDXaM5yUFXW3V+Shi5h9utqY9tjW1QKCJjckFMaryDsvypePszKc5yAlriC02n9Yp7DMSDJFOFPFrUTjvN6MOFqhYinMM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(346002)(39860400002)(136003)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(478600001)(356005)(81166007)(6666004)(1076003)(82740400003)(82310400005)(2616005)(186003)(16526019)(26005)(426003)(83380400001)(336012)(36860700001)(47076005)(86362001)(4326008)(54906003)(110136005)(36756003)(40480700001)(70586007)(41300700001)(8676002)(8936002)(70206006)(316002)(2906002)(44832011)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2023 01:07:19.7297 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 97040736-11c1-4960-4033-08db42cdebf9 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4133 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org It's possible that the device firmware crashes and is able to recover due to some configuration and/or other issue. If a live migration is in progress while the firmware crashes, the live migration will fail. However, the VF PCI device should still be functional post crash recovery and subsequent migrations should go through as expected. When the pds_core device notices that firmware crashes it sends an event to all its client drivers. When the pds_vfio driver receives this event while migration is in progress it will request a deferred reset on the next migration state transition. This state transition will report failure as well as any subsequent state transition requests from the VMM/VFIO. Based on uapi/vfio.h the only way out of VFIO_DEVICE_STATE_ERROR is by issuing VFIO_DEVICE_RESET. Once this reset is done, the migration state will be reset to VFIO_DEVICE_STATE_RUNNING and migration can be performed. If the event is received while no migration is in progress (i.e. the VM is in normal operating mode), then no actions are taken and the migration state remains VFIO_DEVICE_STATE_RUNNING. Signed-off-by: Brett Creeley Signed-off-by: Shannon Nelson --- drivers/vfio/pci/pds/pci_drv.c | 108 ++++++++++++++++++++++++++++++++ drivers/vfio/pci/pds/vfio_dev.c | 29 ++++++++- drivers/vfio/pci/pds/vfio_dev.h | 4 ++ 3 files changed, 139 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/pds/pci_drv.c b/drivers/vfio/pci/pds/pci_drv.c index 91d9a66d847a..b64c2e492688 100644 --- a/drivers/vfio/pci/pds/pci_drv.c +++ b/drivers/vfio/pci/pds/pci_drv.c @@ -19,6 +19,107 @@ #define PDS_VFIO_DRV_DESCRIPTION "AMD/Pensando VFIO Device Driver" #define PCI_VENDOR_ID_PENSANDO 0x1dd8 +static void +pds_vfio_recovery(struct pds_vfio_pci_device *pds_vfio) +{ + bool deferred_reset_needed = false; + + /* + * Documentation states that the kernel migration driver must not + * generate asynchronous device state transitions outside of + * manipulation by the user or the VFIO_DEVICE_RESET ioctl. + * + * Since recovery is an asynchronous event received from the device, + * initiate a deferred reset. Only issue the deferred reset if a + * migration is in progress, which will cause the next step of the + * migration to fail. Also, if the device is in a state that will + * be set to VFIO_DEVICE_STATE_RUNNING on the next action (i.e. VM is + * shutdown and device is in VFIO_DEVICE_STATE_STOP) as that will clear + * the VFIO_DEVICE_STATE_ERROR when the VM starts back up. + */ + mutex_lock(&pds_vfio->state_mutex); + if ((pds_vfio->state != VFIO_DEVICE_STATE_RUNNING && + pds_vfio->state != VFIO_DEVICE_STATE_ERROR) || + (pds_vfio->state == VFIO_DEVICE_STATE_RUNNING && + pds_vfio_dirty_is_enabled(pds_vfio))) + deferred_reset_needed = true; + mutex_unlock(&pds_vfio->state_mutex); + + /* + * On the next user initiated state transition, the device will + * transition to the VFIO_DEVICE_STATE_ERROR. At this point it's the user's + * responsibility to reset the device. + * + * If a VFIO_DEVICE_RESET is requested post recovery and before the next + * state transition, then the deferred reset state will be set to + * VFIO_DEVICE_STATE_RUNNING. + */ + if (deferred_reset_needed) + pds_vfio_deferred_reset(pds_vfio, VFIO_DEVICE_STATE_ERROR); +} + +static int +pds_vfio_pci_notify_handler(struct notifier_block *nb, + unsigned long ecode, + void *data) +{ + struct pds_vfio_pci_device *pds_vfio = container_of(nb, + struct pds_vfio_pci_device, + nb); + struct device *dev = pds_vfio_to_dev(pds_vfio); + union pds_core_notifyq_comp *event = data; + + dev_dbg(dev, "%s: event code %lu\n", __func__, ecode); + + /* + * We don't need to do anything for RESET state==0 as there is no notify + * or feedback mechanism available, and it is possible that we won't + * even see a state==0 event. + * + * Any requests from VFIO while state==0 will fail, which will return + * error and may cause migration to fail. + */ + if (ecode == PDS_EVENT_RESET) { + dev_info(dev, "%s: PDS_EVENT_RESET event received, state==%d\n", + __func__, event->reset.state); + if (event->reset.state == 1) + pds_vfio_recovery(pds_vfio); + } + + return 0; +} + +static int +pds_vfio_pci_register_event_handler(struct pds_vfio_pci_device *pds_vfio) +{ + struct device *dev = pds_vfio_to_dev(pds_vfio); + struct notifier_block *nb = &pds_vfio->nb; + int err; + + if (!nb->notifier_call) { + nb->notifier_call = pds_vfio_pci_notify_handler; + err = pdsc_register_notify(nb); + if (err) { + nb->notifier_call = NULL; + dev_err(dev, "failed to register pds event handler: %pe\n", + ERR_PTR(err)); + return -EINVAL; + } + dev_dbg(dev, "pds event handler registered\n"); + } + + return 0; +} + +static void +pds_vfio_pci_unregister_event_handler(struct pds_vfio_pci_device *pds_vfio) +{ + if (pds_vfio->nb.notifier_call) { + pdsc_unregister_notify(&pds_vfio->nb); + pds_vfio->nb.notifier_call = NULL; + } +} + static int pds_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) @@ -49,8 +150,14 @@ pds_vfio_pci_probe(struct pci_dev *pdev, goto out_unregister_coredev; } + err = pds_vfio_pci_register_event_handler(pds_vfio); + if (err) + goto out_unregister_client; + return 0; +out_unregister_client: + pds_vfio_unregister_client_cmd(pds_vfio); out_unregister_coredev: vfio_pci_core_unregister_device(&pds_vfio->vfio_coredev); out_put_vdev: @@ -63,6 +170,7 @@ pds_vfio_pci_remove(struct pci_dev *pdev) { struct pds_vfio_pci_device *pds_vfio = pds_vfio_pci_drvdata(pdev); + pds_vfio_pci_unregister_event_handler(pds_vfio); pds_vfio_unregister_client_cmd(pds_vfio); vfio_pci_core_unregister_device(&pds_vfio->vfio_coredev); vfio_put_device(&pds_vfio->vfio_coredev.vdev); diff --git a/drivers/vfio/pci/pds/vfio_dev.c b/drivers/vfio/pci/pds/vfio_dev.c index 1adb00ea0114..0ae9f9250083 100644 --- a/drivers/vfio/pci/pds/vfio_dev.c +++ b/drivers/vfio/pci/pds/vfio_dev.c @@ -37,10 +37,13 @@ pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio) if (pds_vfio->deferred_reset) { pds_vfio->deferred_reset = false; if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) { - pds_vfio->state = VFIO_DEVICE_STATE_RUNNING; + pds_vfio->state = pds_vfio->deferred_reset_state; pds_vfio_put_restore_file(pds_vfio); pds_vfio_put_save_file(pds_vfio); + } else if (pds_vfio->deferred_reset_state == VFIO_DEVICE_STATE_ERROR) { + pds_vfio->state = VFIO_DEVICE_STATE_ERROR; } + pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING; spin_unlock(&pds_vfio->reset_lock); goto again; } @@ -53,6 +56,7 @@ pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio) { spin_lock(&pds_vfio->reset_lock); pds_vfio->deferred_reset = true; + pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING; if (!mutex_trylock(&pds_vfio->state_mutex)) { spin_unlock(&pds_vfio->reset_lock); return; @@ -61,6 +65,16 @@ pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio) pds_vfio_state_mutex_unlock(pds_vfio); } +void +pds_vfio_deferred_reset(struct pds_vfio_pci_device *pds_vfio, + enum vfio_device_mig_state reset_state) +{ + spin_lock(&pds_vfio->reset_lock); + pds_vfio->deferred_reset = true; + pds_vfio->deferred_reset_state = reset_state; + spin_unlock(&pds_vfio->reset_lock); +} + static struct file * pds_vfio_set_device_state(struct vfio_device *vdev, enum vfio_device_mig_state new_state) @@ -71,7 +85,14 @@ pds_vfio_set_device_state(struct vfio_device *vdev, struct file *res = NULL; mutex_lock(&pds_vfio->state_mutex); - while (new_state != pds_vfio->state) { + /* + * only way to transition out of VFIO_DEVICE_STATE_ERROR is via + * VFIO_DEVICE_RESET, so prevent the state machine from running since + * vfio_mig_get_next_state() will throw a WARN_ON() when transitioning + * from VFIO_DEVICE_STATE_ERROR to any other state + */ + while (pds_vfio->state != VFIO_DEVICE_STATE_ERROR && + new_state != pds_vfio->state) { enum vfio_device_mig_state next_state; int err = vfio_mig_get_next_state(vdev, pds_vfio->state, @@ -93,6 +114,9 @@ pds_vfio_set_device_state(struct vfio_device *vdev, } } pds_vfio_state_mutex_unlock(pds_vfio); + /* still waiting on a deferred_reset */ + if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) + res = ERR_PTR(-EIO); return res; } @@ -175,6 +199,7 @@ pds_vfio_open_device(struct vfio_device *vdev) mutex_init(&pds_vfio->state_mutex); pds_vfio->state = VFIO_DEVICE_STATE_RUNNING; + pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING; vfio_pci_core_finish_enable(&pds_vfio->vfio_coredev); diff --git a/drivers/vfio/pci/pds/vfio_dev.h b/drivers/vfio/pci/pds/vfio_dev.h index 2271f6af2a85..d494aabfce1f 100644 --- a/drivers/vfio/pci/pds/vfio_dev.h +++ b/drivers/vfio/pci/pds/vfio_dev.h @@ -23,6 +23,8 @@ struct pds_vfio_pci_device { enum vfio_device_mig_state state; spinlock_t reset_lock; /* protect reset_done flow */ u8 deferred_reset; + enum vfio_device_mig_state deferred_reset_state; + struct notifier_block nb; int vf_id; int pci_id; @@ -35,6 +37,8 @@ pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio); const struct vfio_device_ops *pds_vfio_ops_info(void); struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev); void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio); +void pds_vfio_deferred_reset(struct pds_vfio_pci_device *pds_vfio, + enum vfio_device_mig_state reset_state); struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio); struct device *pds_vfio_to_dev(struct pds_vfio_pci_device *pds_vfio);