From patchwork Mon Aug 29 11:48:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 601056 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 5B4EBECAAD5 for ; Mon, 29 Aug 2022 12:17:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229651AbiH2MRx (ORCPT ); Mon, 29 Aug 2022 08:17:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231171AbiH2MRg (ORCPT ); Mon, 29 Aug 2022 08:17:36 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E934797D51; Mon, 29 Aug 2022 05:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b5F/5vzaSUBJZP3lF7WQ/wJ+EwJjYGzQlgS6BQ+mPFpU3kZUrT7yW2hFz4lzj4ULqc1pV+7fOkTntoldli1P7mRT87IZJFNJ7hxkc9ij1UUm6tXHUjMbu+QnIcPEX2T3hqSVYoD9rkTE8I3Pux0yP8P49RuuO4ALAGAiIs16qjhe3aM3yuXBFOhjgIs5yeiSpYiwUwA1BD4JGzx5F7hrks4mx9Ps1cHVKjVoFegojV4fsl5OVvzx1DGIXx078a2Ti8kDVEvR1Gq5hpWGZ8YRwy03HH0+dxC/P5wNsTpxLG/Cpgu/Q3wkBx/107QCYkra/JI28/+U5eSLVMkYNZ+uzA== 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=GS+0AX+1mjY+BsvJVY/oyr/YmF59D85FHAMSBSUwoQA=; b=cqoWdP6MrbgiEceQtRdVFM+t/yLmta5Rc+xWGjB/31VMUtU8+petaOV5NacyyhIEmmxFR6ztWYQ2zJLNKAk1cx+kcxmhQQxvkEZZ57sRSneYlE4hiukrBSgfVQAwh4Ukft3GmXlhWiKjnk4WYpWzWsXECnimB6dcvURnj9RNKmAtTBRMb4SndIegAxcB0s53r1yTwHgf+ar6H9vdal8KU8sOTLsDOKA4BTxzJV8hsNxpTqBWnrtTR+uqrh9zc4757wfoOAW+Awo0Amo1WSTAfeBkUNv90mlZc3iI8WhwRsIuJVj3v7/ksTl2jyMQCCi6JYqvFj8xrC1W+ICVyDO2dg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GS+0AX+1mjY+BsvJVY/oyr/YmF59D85FHAMSBSUwoQA=; b=OtWa3k8vy8pc9gfmo9cHVGjnrdVzft8z4uHduvRV0MpbaJbBYMKTx5cHlOVbGezCSi4W6CEgglh3p0YhqXzss0Mq2WJ7yBHflA/OJ0rZ3SA8fFii165J4G/lJyA4vN/44Oc0RTFz2GVyBqzAEcXi6hQc42sYAicwJ9+/KYRhEu0FG9uqxEYty33+k035rwsQk4WgBXsfcK3RGu+uAcwJ5lBHMiinNoed2p4HZtZA1s+XbTFm1gohtMQL0Hhr2y/UEZV6bR46/XMNHes2WmqrAXItcYNodUfPGCSmpfAyTUPRyE+wWBuujaqekCJqVwtZHxbyFVKkH6XVzzRD2Y4h1Q== Received: from MW4PR02CA0019.namprd02.prod.outlook.com (2603:10b6:303:16d::23) by BYAPR12MB2613.namprd12.prod.outlook.com (2603:10b6:a03:69::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.19; Mon, 29 Aug 2022 11:49:07 +0000 Received: from CO1NAM11FT026.eop-nam11.prod.protection.outlook.com (2603:10b6:303:16d:cafe::a9) by MW4PR02CA0019.outlook.office365.com (2603:10b6:303:16d::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by CO1NAM11FT026.mail.protection.outlook.com (10.13.175.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:07 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Mon, 29 Aug 2022 11:49:06 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 29 Aug 2022 04:49:05 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Mon, 29 Aug 2022 04:49:01 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v7 1/5] vfio: Add the device features for the low power entry and exit Date: Mon, 29 Aug 2022 17:18:46 +0530 Message-ID: <20220829114850.4341-2-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220829114850.4341-1-abhsahu@nvidia.com> References: <20220829114850.4341-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 627e0062-9520-4432-7454-08da89b47ac4 X-MS-TrafficTypeDiagnostic: BYAPR12MB2613:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: e8AH5/rcP8SKIufgF3FaWGhyCTfKrvU7PGjTL7G4GScnfU6A5X5yJM4+z3htfi7uj2PWQNbnQDbqUmnbdcs2sviQRxq9nHP1Ju7wYnXSA1JVBQ2rfEB3sG4087bJR5/eqITAL+jh6xEyv+UztkLkcdWWUzE8SGvntUs1wnO7+SOJgqoSkX5wk5VbugLbSC825ycDAgcmYEdX6UliQa2m88oPtVGos3kSJ0x4Kzhzmlj0GnmhEQNnmm2O8D3MVgECtWGVllCV/VlafWV1eCC0uxGOaB3VgxfjB0TEPJM9/X3+iVW/HrK6GZkCX5tU4qGN6+leBHIvvVc6QI394R8CDMvVzEEySO627QVgpuqgjQK0f4Unw+ig1tiB2zjOqrz4Osy3B6nY2vA1JHi4vbJ+Ap8hMMf90oPAjUPyoYsHYSI/JoQxrVL6tJ974WpeTbNrF2CfwmMhaI1UAwikodTzrN6eLZC+nVLw/iEEmn5qXV9GEwrfdrWIweAVOvKnMR5t+IGqxT0m0I9PqBwMVph0UyuKt+4cTqxgfd2wxZZYTeN32BWfnwhYNTRsXlCDoXiUbY2XYWWmy8/n9Rl3qvQU1MbjGFYezGaM4kj2PjifTNWEtqR9YSck11yESK7Mau/xyxolSCNkYyN8s/eQuKwyHi43KlszDbLelqlKkAbLiSCkcUkZOk7X7cLUXrmdK3K3CDnERCHFlmZFVJyiLreaLQiA0414OEF1T4Qeo/7IlcHrwnupEw075rK185MyBK/NDXQA5szqdNJVIJJqXFqYqbnHh0+Miccq4DLbsRZl8PWrh8GdE7gZPkzdzGJp3W4L X-Forefront-Antispam-Report: CIP:12.22.5.236; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230016)(4636009)(136003)(39860400002)(376002)(396003)(346002)(36840700001)(40470700004)(46966006)(4326008)(70206006)(110136005)(316002)(70586007)(54906003)(82310400005)(8676002)(40460700003)(40480700001)(36860700001)(7416002)(8936002)(5660300002)(2906002)(81166007)(356005)(82740400003)(36756003)(6666004)(7696005)(26005)(86362001)(107886003)(478600001)(41300700001)(1076003)(2616005)(186003)(426003)(83380400001)(336012)(47076005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2022 11:49:07.2066 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 627e0062-9520-4432-7454-08da89b47ac4 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.236]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT026.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2613 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This patch adds the following new device features for the low power entry and exit in the header file. The implementation for the same will be added in the subsequent patches. - VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY - VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP - VFIO_DEVICE_FEATURE_LOW_POWER_EXIT For vfio-pci based devices, with the standard PCI PM registers, all power states cannot be achieved. The platform-based power management needs to be involved to go into the lowest power state. For doing low power entry and exit with platform-based power management, these device features can be used. The entry device feature has two variants. These two variants are mainly to support the different behaviour for the low power entry. If there is any access for the VFIO device on the host side, then the device will be moved out of the low power state without the user's guest driver involvement. Some devices (for example NVIDIA VGA or 3D controller) require the user's guest driver involvement for each low-power entry. In the first variant, the host can return the device to low power automatically. The device will continue to attempt to reach low power until the low power exit feature is called. In the second variant, if the device exits low power due to an access, the host kernel will signal the user via the provided eventfd and will not return the device to low power without a subsequent call to one of the low power entry features. A call to the low power exit feature is optional if the user provided eventfd is signaled. These device features only support VFIO_DEVICE_FEATURE_SET and VFIO_DEVICE_FEATURE_PROBE operations. Signed-off-by: Abhishek Sahu --- include/uapi/linux/vfio.h | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 733a1cddde30..76a173f973de 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -986,6 +986,62 @@ enum vfio_device_mig_state { VFIO_DEVICE_STATE_RUNNING_P2P = 5, }; +/* + * Upon VFIO_DEVICE_FEATURE_SET, allow the device to be moved into a low power + * state with the platform-based power management. Device use of lower power + * states depends on factors managed by the runtime power management core, + * including system level support and coordinating support among dependent + * devices. Enabling device low power entry does not guarantee lower power + * usage by the device, nor is a mechanism provided through this feature to + * know the current power state of the device. If any device access happens + * (either from the host or through the vfio uAPI) when the device is in the + * low power state, then the host will move the device out of the low power + * state as necessary prior to the access. Once the access is completed, the + * device may re-enter the low power state. For single shot low power support + * with wake-up notification, see + * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP below. Access to mmap'd + * device regions is disabled on LOW_POWER_ENTRY and may only be resumed after + * calling LOW_POWER_EXIT. + */ +#define VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY 3 + +/* + * This device feature has the same behavior as + * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY with the exception that the user + * provides an eventfd for wake-up notification. When the device moves out of + * the low power state for the wake-up, the host will not allow the device to + * re-enter a low power state without a subsequent user call to one of the low + * power entry device feature IOCTLs. Access to mmap'd device regions is + * disabled on LOW_POWER_ENTRY_WITH_WAKEUP and may only be resumed after the + * low power exit. The low power exit can happen either through LOW_POWER_EXIT + * or through any other access (where the wake-up notification has been + * generated). The access to mmap'd device regions will not trigger low power + * exit. + * + * The notification through the provided eventfd will be generated only when + * the device has entered and is resumed from a low power state after + * calling this device feature IOCTL. A device that has not entered low power + * state, as managed through the runtime power management core, will not + * generate a notification through the provided eventfd on access. Calling the + * LOW_POWER_EXIT feature is optional in the case where notification has been + * signaled on the provided eventfd that a resume from low power has occurred. + */ +struct vfio_device_low_power_entry_with_wakeup { + __s32 wakeup_eventfd; + __u32 reserved; +}; + +#define VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP 4 + +/* + * Upon VFIO_DEVICE_FEATURE_SET, disallow use of device low power states as + * previously enabled via VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY or + * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP device features. + * This device feature IOCTL may itself generate a wakeup eventfd notification + * in the latter case if the device had previously entered a low power state. + */ +#define VFIO_DEVICE_FEATURE_LOW_POWER_EXIT 5 + /* -------- API for Type1 VFIO IOMMU -------- */ /** From patchwork Mon Aug 29 11:48:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 602132 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 6FADCECAAD5 for ; Mon, 29 Aug 2022 12:12:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231615AbiH2MMK (ORCPT ); Mon, 29 Aug 2022 08:12:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231200AbiH2MLh (ORCPT ); Mon, 29 Aug 2022 08:11:37 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2075.outbound.protection.outlook.com [40.107.243.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80E719352D; Mon, 29 Aug 2022 04:56:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eCviOnbaemJzcRlO0nPYS0RLqDdUq09raK8zbpErb28uyP11jmdm9AX6yd0oheFZSA7V3hkSejeeCh/flNM7prAgdyiSBlO55HFO4IpWojX/ku4+Zgih/YrRAWIuKzk8TBar4QFpkGixk+oeNFETTo/T5qUycuqmwgY45T7BKzlkqdAWqz5jSiRuYcho1ab6JFb3wQzq9F3tqRsQKU0vqo4hXLDXrkscT9NoEuPTGo9ZTAIycSNdyuoLw2sQIg4R1aWsmQtiQT1a5ulH/vaFLXSwvzsqqjHI7yskpjwKXYwq22oWnLF5wX9ru5sk6GFPXY8InbiyGAfE52THMrF3qw== 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=X5SFija1pmKheybbWx7egP4rc5NVxvIwNaiRI/uQyj8=; b=N97U2VDS7hCZRR1r6JNuuUSWLLeAYqTF66GCeLdpmSdlkJyL5HcJlDdp2kKPNGCDNWQgHAF3xS0mh2MYlyuxlK6ZSebOC4WVO9pQtdn3giHmCzb8Qp618D7pEfH55w2Z3112b6Ubfpx/J/uYak1bwNSiKGy8DtyLd+ZP7tre2Gc7MNA3nDNAWyCtmvUuQZYQydiw+JLXGcsWitQ+grSAO266/Jwq1yRvkkjjON+nPsyQIStpJA92JZUApXdVopnO2B62lrkZXlbEE5384qxSKZkqt7Rv4XzMs9Qhr7YZcy4AYgtY/9p1YOdEr5u2NPaoN20GpMqj4kOcXFHKIXtRfQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=X5SFija1pmKheybbWx7egP4rc5NVxvIwNaiRI/uQyj8=; b=PrMSQJcmL/sr3WrVhrGKzqQtzrCEesDNQ4PtUN1dvh167Osq65cP7wt7lve7I7Cnl+cy2cVdWvue3ROa/i9/Y7LM65wCeAhSKnFXxPFi2YqZSjhf/9p3GluCrWEOX9QlIEw4Y2BLfnxBj2UADRVfqDiRzosrXSr1ydRFaIkR+bSnF3e9mDSD5wyGzCw33R63ADxasciZf1iLro4bkz0JPs1eDryIlnoSi5ydbbn1pyin6OGTmWyIyrcew9kdmEdiZdcvzlyyKuOAGblan7Ts1OrL9PHm0YrXm/kkzEFg+qeSa602U4CXsG5XF9waozx+ucmdC2qE3RSF4dC0s8Y56A== Received: from MW4PR03CA0106.namprd03.prod.outlook.com (2603:10b6:303:b7::21) by DM5PR12MB1481.namprd12.prod.outlook.com (2603:10b6:4:e::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.19; Mon, 29 Aug 2022 11:49:13 +0000 Received: from CO1NAM11FT112.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b7:cafe::18) by MW4PR03CA0106.outlook.office365.com (2603:10b6:303:b7::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.19 via Frontend Transport; Mon, 29 Aug 2022 11:49:13 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.238) by CO1NAM11FT112.mail.protection.outlook.com (10.13.174.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:12 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Mon, 29 Aug 2022 11:49:12 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 29 Aug 2022 04:49:11 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Mon, 29 Aug 2022 04:49:06 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v7 2/5] vfio: Increment the runtime PM usage count during IOCTL call Date: Mon, 29 Aug 2022 17:18:47 +0530 Message-ID: <20220829114850.4341-3-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220829114850.4341-1-abhsahu@nvidia.com> References: <20220829114850.4341-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b6f273b9-a010-4273-d1f4-08da89b47e2b X-MS-TrafficTypeDiagnostic: DM5PR12MB1481:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Cjpb2VWc1ZAjQUDcIrs/67Z+4TYZI/R5A2WnxvAt7hq7+ieSrME4XfGdg6LgOJw9woG03xwwfNfJ9F+Ru5SXLZKNg36o9yxzViWr0T6R1oERYpclfF5u+/TLoMCNEB1JGwX3NNQZetpgPKOaMH8fc/e8V24ecL2ztpPVEQzQ7YZOVSLVNkQMjZFKc7/lQPKDgpvI0w6QtLJyaDx3tVG4X4ZhsQ++dgx/xRW+3j8sfNejtVflxQhayL3bf2BYqhCjchq5VGCQNpjUMEFhYFydkrjmalUYmjyr7Z4JGvlZlqiiRtmOEGjIOGeNsIhZ9KmnlJuaKbkowYXTcu7m5OJxNX5baXHy/c7EJ0G+VEXwb0q5VPnovIP1bCjOHLl+ANVb1B239LTKRdVs/V9wwWi/snthVEv3/DHCMKUFk34hLBwozk78EwFVQGWm2BUxElH9ed9kqlez9DISMVgygsf6AcW6HhTFqOZh4x4ZErqzu6+Vyt9tT1LeEBRHUGxStQSho91GnWx3xGkGHdnt+9vhFHZkEDGU6DhXTmPE0Wiwuz7tGN2SVAXyU7lMr39YzJ+6xVQYDWj8sZ9bDWQOZ78uJJZOKzZK8B70uXke6zMpw//VeBCdztLoaIBDgdx1lF5ZscYc+A90GsPXnPehT45cWtTTn712aHP6o1WBiHnjbdVj9Xm3JD+XXwKFX6Z4S68fbUAiINqs+VeKlcPuwH5llyAuMy82ZmoNfEH0QoHaXK0006juLwZdRBhwRbbFC6T7KY3Lq2RZJxolaIWGDh1dDhC7okY5RhpHRpygBMDT9vfD4BaRjuRpAIf50IEHUxAu X-Forefront-Antispam-Report: CIP:12.22.5.238; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230016)(4636009)(396003)(136003)(376002)(39860400002)(346002)(40470700004)(36840700001)(46966006)(107886003)(26005)(7696005)(41300700001)(478600001)(1076003)(6666004)(83380400001)(426003)(47076005)(186003)(336012)(2906002)(2616005)(8936002)(5660300002)(110136005)(40460700003)(40480700001)(82310400005)(316002)(54906003)(4326008)(8676002)(70586007)(70206006)(356005)(7416002)(81166007)(86362001)(82740400003)(36756003)(36860700001)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2022 11:49:12.9172 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b6f273b9-a010-4273-d1f4-08da89b47e2b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.238]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT112.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1481 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The vfio-pci based drivers will have runtime power management support where the user can put the device into the low power state and then PCI devices can go into the D3cold state. If the device is in the low power state and the user issues any IOCTL, then the device should be moved out of the low power state first. Once the IOCTL is serviced, then it can go into the low power state again. The runtime PM framework manages this with help of usage count. One option was to add the runtime PM related API's inside vfio-pci driver but some IOCTL (like VFIO_DEVICE_FEATURE) can follow a different path and more IOCTL can be added in the future. Also, the runtime PM will be added for vfio-pci based drivers variant currently, but the other VFIO based drivers can use the same in the future. So, this patch adds the runtime calls runtime-related API in the top-level IOCTL function itself. For the VFIO drivers which do not have runtime power management support currently, the runtime PM API's won't be invoked. Only for vfio-pci based drivers currently, the runtime PM API's will be invoked to increment and decrement the usage count. In the vfio-pci drivers also, the variant drivers can opt-out by incrementing the usage count during device-open. The pm_runtime_resume_and_get() checks the device current status and will return early if the device is already in the ACTIVE state. Taking this usage count incremented while servicing IOCTL will make sure that the user won't put the device into the low power state when any other IOCTL is being serviced in parallel. Let's consider the following scenario: 1. Some other IOCTL is called. 2. The user has opened another device instance and called the IOCTL for low power entry. 3. The low power entry IOCTL moves the device into the low power state. 4. The other IOCTL finishes. If we don't keep the usage count incremented then the device access will happen between step 3 and 4 while the device has already gone into the low power state. The pm_runtime_resume_and_get() will be the first call so its error should not be propagated to user space directly. For example, if pm_runtime_resume_and_get() can return -EINVAL for the cases where the user has passed the correct argument. So the pm_runtime_resume_and_get() errors have been masked behind -EIO. Signed-off-by: Abhishek Sahu --- drivers/vfio/vfio_main.c | 52 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 6f96e6d07a5e..ee189ff015a4 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "vfio.h" #define DRIVER_VERSION "0.3" @@ -1348,6 +1349,39 @@ static const struct file_operations vfio_group_fops = { .release = vfio_group_fops_release, }; +/* + * Wrapper around pm_runtime_resume_and_get(). + * Return error code on failure or 0 on success. + */ +static inline int vfio_device_pm_runtime_get(struct vfio_device *device) +{ + struct device *dev = device->dev; + + if (dev->driver && dev->driver->pm) { + int ret; + + ret = pm_runtime_resume_and_get(dev); + if (ret) { + dev_info_ratelimited(dev, + "vfio: runtime resume failed %d\n", ret); + return -EIO; + } + } + + return 0; +} + +/* + * Wrapper around pm_runtime_put(). + */ +static inline void vfio_device_pm_runtime_put(struct vfio_device *device) +{ + struct device *dev = device->dev; + + if (dev->driver && dev->driver->pm) + pm_runtime_put(dev); +} + /* * VFIO Device fd */ @@ -1668,15 +1702,27 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { struct vfio_device *device = filep->private_data; + int ret; + + ret = vfio_device_pm_runtime_get(device); + if (ret) + return ret; switch (cmd) { case VFIO_DEVICE_FEATURE: - return vfio_ioctl_device_feature(device, (void __user *)arg); + ret = vfio_ioctl_device_feature(device, (void __user *)arg); + break; + default: if (unlikely(!device->ops->ioctl)) - return -EINVAL; - return device->ops->ioctl(device, cmd, arg); + ret = -EINVAL; + else + ret = device->ops->ioctl(device, cmd, arg); + break; } + + vfio_device_pm_runtime_put(device); + return ret; } static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, From patchwork Mon Aug 29 11:48:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 601057 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 32A39ECAAD4 for ; Mon, 29 Aug 2022 12:09:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230527AbiH2MJk (ORCPT ); Mon, 29 Aug 2022 08:09:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231361AbiH2MIV (ORCPT ); Mon, 29 Aug 2022 08:08:21 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062a.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe5b::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8E9A8E446; Mon, 29 Aug 2022 04:53:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HriMKK0cog9eS5QzxT0w2ro2rB1b1ZNFxQ/jLbmuQz9CcCgna3ZNcK2hiAJbCJu+cv902Gvq57MeXNXH1KwiMWLaIwLF4hOgG9nZvMRLmD4pOtg1d8HOsbMlAnk7oUSGAc4/0fcYYhMaZ4de2I1O+g8IgRHBLA29rQy6Bat7adW/WCBlYku8EbRAmRP0/yypLMyzNX7sAlMbtgEdY2L1Mtbj+jcY/txyUqnAdnT47SGleylEGeD5xIOpEyoBnwW/e2n2rvoOHqTHaZuBNmPyGfl8CIacKLTczlPnhH+yZ0QG3uOayMeprmZv621HNgmVPG0imN62YxukXLQp4gF/IA== 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=LwkuhZRni3VilS+fViO2KXJWi5cSptkV4Lmh2gWdXSk=; b=In7BEeKWuV3WMjLjnUOiV6DoY1E3C6BJKLcOmogoDj39byZq/buFsnEY9ScI1Viwa6ffihSXebhPanxvoUBmkfefAMM/KJCpbDsu4IWyF9Z2CU/cg9+kPerSXP1LShipE8bCISfJy17cBu9aObr1zbP4o/llCOca9YlLWLOfaHPMr8wywNdnbRjJskZL6Z1ZCAQbH7dTroJeinFwIBZAhSQG9pQjTRjE3z6ltNSF0nuaY2mAR4AVrafIqDc1FPxOAfrEPjBqUI9Hg50niDMNN95BwgvVilHh8IiIUOewaQBS+goU3U8MsX/ecsWxgRIDsys3U0mRWGR48KrRjFogWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LwkuhZRni3VilS+fViO2KXJWi5cSptkV4Lmh2gWdXSk=; b=okBY/xTu+JHLWVJtstSC1kWlJVqbMv06yxgXmn8PP7mfUm2JS+S1nsZNWOA5uBVEhH5bTexRhFOWwlwDU0xbvW2Y8JzuduS+PqPJdPhqFlSK3BGjPGGlolxxLUt9vA0EPaXj3eWOqqgO90rd8epRcK/wOcYkJ88gGyoVri4nR9QRBUQQ6/lkJfHf6sF0ek8sNn4dER6vTJpjwVsRD3Ka9EtGJg1RfcWTGhsSub+2rmMgu+mInhGPK692qDaUK+YuDObTM7Ukf/Ioata0IZZWiSk+TDOmjZhi2WSFDVb2BfM+KUA6f7R0vP9DXmBsBdLch9c4faWeMB7lrkU5iI0nig== Received: from DS7PR03CA0233.namprd03.prod.outlook.com (2603:10b6:5:3ba::28) by CH2PR12MB3831.namprd12.prod.outlook.com (2603:10b6:610:29::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.16; Mon, 29 Aug 2022 11:49:18 +0000 Received: from DM6NAM11FT009.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ba:cafe::24) by DS7PR03CA0233.outlook.office365.com (2603:10b6:5:3ba::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.18 via Frontend Transport; Mon, 29 Aug 2022 11:49:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by DM6NAM11FT009.mail.protection.outlook.com (10.13.173.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:18 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Mon, 29 Aug 2022 11:49:18 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 29 Aug 2022 04:49:17 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Mon, 29 Aug 2022 04:49:12 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v7 3/5] vfio/pci: Mask INTx during runtime suspend Date: Mon, 29 Aug 2022 17:18:48 +0530 Message-ID: <20220829114850.4341-4-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220829114850.4341-1-abhsahu@nvidia.com> References: <20220829114850.4341-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 45480123-51aa-4dcc-9728-08da89b48191 X-MS-TrafficTypeDiagnostic: CH2PR12MB3831:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zixo/UeLymTEDZ1HwKxXsS86kb4JmB0h+OZgmQx/3r8QiorffFmGzVwpwf9C4OyHKIhSmu0Ug9e4BZH1RgGmnK9ru1DelV9+9MoTT4T2pytVxYNcRlelNi0cE2E40sWvHFN/DkkjSc/DdTBZ0bLT6EE+xr3ctECo+rPJdPxSNZspc5IxHhGKX/EWIvlk2ICXEF4OyPQgWzQ7pfvr/3E8igv1w4wEWbow0NRPGNKNPJwFi4yFqkEwtJeuA+ry+7fh/u+hw6W6NXb7ch5F+E4AOcajL/1EJuAIbJT7svQ85/hxPTJUo099/Tnw/w2TjuDMILbdFhC19JId5mWsosVJW0fI1x7ghweVU037P1T1MdilIBmEozU4T7dOapoVxSerSOMQWuvvnBqQQDVL8+imrDuEhLXBIEsiykti0gYcW6TND6Wq9N0uSRGxlGRSy/edmn4fwnSMdUJUqDDpAXIEdA5UXrUZ1YFDxRS8DuUR3aovzT22m8pp+500iXtKSTbQCH88ktLfXMLWAXzkC7y3mGu+NTBD4gXhzmMJLglCtWIBwJWVCB7cC1hZ0eABLWyrOO/KBHl+EtCB/R6G52JXZSin7guZ/0og0DG2C4u9+0MTX3JbvfRjHnE3U2KsZuHsLxI6vja6xqTvsh7ZuseHuPKEWSliqTWHoOHPNCPw9oVHOkAt5M9clQQJGyh7c3B5+OJfmZD1/Read8QGJAwkgA0E1ZdRnrOQaEczwYlgcnWHMDcP3hjHhCflmJXfXuGU6LFpz/G8ubro/wlxKhd9ZStMZ1F6hkGrr+tTLcSduW2KdsQI2z2bdOSjVmNBbbS3 X-Forefront-Antispam-Report: CIP:12.22.5.234; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230016)(4636009)(396003)(136003)(39860400002)(346002)(376002)(46966006)(40470700004)(36840700001)(36860700001)(1076003)(2616005)(336012)(186003)(426003)(2906002)(47076005)(83380400001)(110136005)(316002)(54906003)(70586007)(70206006)(4326008)(8676002)(5660300002)(26005)(107886003)(7696005)(6666004)(478600001)(15650500001)(7416002)(41300700001)(8936002)(81166007)(36756003)(40460700003)(40480700001)(86362001)(82310400005)(82740400003)(356005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2022 11:49:18.6017 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 45480123-51aa-4dcc-9728-08da89b48191 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.234]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT009.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB3831 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This patch adds INTx handling during runtime suspend/resume. All the suspend/resume related code for the user to put the device into the low power state will be added in subsequent patches. The INTx lines may be shared among devices. Whenever any INTx interrupt comes for the VFIO devices, then vfio_intx_handler() will be called for each device sharing the interrupt. Inside vfio_intx_handler(), it calls pci_check_and_mask_intx() and checks if the interrupt has been generated for the current device. Now, if the device is already in the D3cold state, then the config space can not be read. Attempt to read config space in D3cold state can cause system unresponsiveness in a few systems. To prevent this, mask INTx in runtime suspend callback, and unmask the same in runtime resume callback. If INTx has been already masked, then no handling is needed in runtime suspend/resume callbacks. 'pm_intx_masked' tracks this, and vfio_pci_intx_mask() has been updated to return true if the INTx vfio_pci_irq_ctx.masked value is changed inside this function. For the runtime suspend which is triggered for the no user of VFIO device, the 'irq_type' will be VFIO_PCI_NUM_IRQS and these callbacks won't do anything. The MSI/MSI-X are not shared so similar handling should not be needed for MSI/MSI-X. vfio_msihandler() triggers eventfd_signal() without doing any device-specific config access. When the user performs any config access or IOCTL after receiving the eventfd notification, then the device will be moved to the D0 state first before servicing any request. Another option was to check this flag 'pm_intx_masked' inside vfio_intx_handler() instead of masking the interrupts. This flag is being set inside the runtime_suspend callback but the device can be in non-D3cold state (for example, if the user has disabled D3cold explicitly by sysfs, the D3cold is not supported in the platform, etc.). Also, in D3cold supported case, the device will be in D0 till the PCI core moves the device into D3cold. In this case, there is a possibility that the device can generate an interrupt. Adding check in the IRQ handler will not clear the IRQ status and the interrupt line will still be asserted. This can cause interrupt flooding. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_core.c | 38 +++++++++++++++++++++++++++---- drivers/vfio/pci/vfio_pci_intrs.c | 6 ++++- drivers/vfio/pci/vfio_pci_priv.h | 2 +- include/linux/vfio_pci_core.h | 1 + 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 9273f1ffd0dd..207ede189c2a 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -277,16 +277,46 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat return ret; } +#ifdef CONFIG_PM +static int vfio_pci_core_runtime_suspend(struct device *dev) +{ + struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); + + /* + * If INTx is enabled, then mask INTx before going into the runtime + * suspended state and unmask the same in the runtime resume. + * If INTx has already been masked by the user, then + * vfio_pci_intx_mask() will return false and in that case, INTx + * should not be unmasked in the runtime resume. + */ + vdev->pm_intx_masked = ((vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX) && + vfio_pci_intx_mask(vdev)); + + return 0; +} + +static int vfio_pci_core_runtime_resume(struct device *dev) +{ + struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); + + if (vdev->pm_intx_masked) + vfio_pci_intx_unmask(vdev); + + return 0; +} +#endif /* CONFIG_PM */ + /* - * The dev_pm_ops needs to be provided to make pci-driver runtime PM working, - * so use structure without any callbacks. - * * The pci-driver core runtime PM routines always save the device state * before going into suspended state. If the device is going into low power * state with only with runtime PM ops, then no explicit handling is needed * for the devices which have NoSoftRst-. */ -static const struct dev_pm_ops vfio_pci_core_pm_ops = { }; +static const struct dev_pm_ops vfio_pci_core_pm_ops = { + SET_RUNTIME_PM_OPS(vfio_pci_core_runtime_suspend, + vfio_pci_core_runtime_resume, + NULL) +}; int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) { diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c index 8cb987ef3c47..40c3d7cf163f 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -59,10 +59,12 @@ static void vfio_send_intx_eventfd(void *opaque, void *unused) eventfd_signal(vdev->ctx[0].trigger, 1); } -void vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) +/* Returns true if the INTx vfio_pci_irq_ctx.masked value is changed. */ +bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; unsigned long flags; + bool masked_changed = false; spin_lock_irqsave(&vdev->irqlock, flags); @@ -86,9 +88,11 @@ void vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) disable_irq_nosync(pdev->irq); vdev->ctx[0].masked = true; + masked_changed = true; } spin_unlock_irqrestore(&vdev->irqlock, flags); + return masked_changed; } /* diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h index 58b8d34c162c..5e4fa69aee16 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -23,7 +23,7 @@ struct vfio_pci_ioeventfd { bool test_mem; }; -void vfio_pci_intx_mask(struct vfio_pci_core_device *vdev); +bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev); void vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev); int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags, diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index e5cf0d3313a6..a0f1f36e42a2 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -78,6 +78,7 @@ struct vfio_pci_core_device { bool needs_reset; bool nointx; bool needs_pm_restore; + bool pm_intx_masked; struct pci_saved_state *pci_saved_state; struct pci_saved_state *pm_save; int ioeventfds_nr; From patchwork Mon Aug 29 11:48:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 601058 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 4C90AECAAD4 for ; Mon, 29 Aug 2022 12:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231438AbiH2MHR (ORCPT ); Mon, 29 Aug 2022 08:07:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbiH2MG4 (ORCPT ); Mon, 29 Aug 2022 08:06:56 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2080.outbound.protection.outlook.com [40.107.244.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8731A50043; Mon, 29 Aug 2022 04:52:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CUYs9ml4qHlJL4khliJQ4/DM/02as3vYY6N+s+Lv7Dr2djcCMJDEKYX3pVpyF2c9OVC3n0W+2xsAZH/NON3sMeOAnZgROLPqd4NnN4FSRfDBGSwcTB1Sq9V3FFAkAwctpaWJBrBHbXqElVJQl57gKZ2sGQbxVHlLf33T2f/JilPpzE7mioIrcfF4+vsHuK+QGkQzJsiv28WLY5G7E/4bP9EXh/3EP0PZH6TyavCqzJ9XUM+m6XL1f7ZJvUw85njh6XG8HiAZqVYTSZ1SYc7KPcH1aUeFIkCQLjZgNHscHqzduezCoGQIJGfb2YQ4qkA0IVrvLS9rr3sWOwe8uRV/gw== 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=ncZ35M0nguXZoNQSMNSfbDKf5bJhkw1FpmPxzv5CEOw=; b=K5sCGqcQR+PD91b3ZnFIfKj+ng3BTng+2GOVabM5ICdxte93/ULbMPw7IA+js+iIZscvq9JlND0DJogznPdSyCknsSoUsdL/+z8OObNL5GWAKcaX9nbtUfcYnyrvwcSpenVpHZkJfWIJ0Pjf9736JDs26qTlXmmuM0kNtBj+IF+8X2tF3lB2Zcjr2VIixLLLFH6L/+VAMvYktubbOfb8xKNvmDWRqsRx0iTDbXJN+Ydm0AzBOFch+1exDFf6gsT7u56JBBvL3dtGD9jcm/RvLfDLI3rnZa/SywEwuIrEml5iaQQMn1SsYbZm7KZugM/s9MM5SoN9OhJkCATxDI7MBQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ncZ35M0nguXZoNQSMNSfbDKf5bJhkw1FpmPxzv5CEOw=; b=YutcZk/z02JxpvCGvhHCMs6aGot6PIuNV3Ngx35bkR5K9u4Bm47rOxtCaBj03VkvGuDsj5Mrj/+oCFh0xRoA2UrwJVnpRroqifCro63gQ0EYCPxkdAVvIEn6pAg7JALhsdPBLFBQi6ejZcgoTqOCE6t7WNVoPTvoMhYdBqUO59vId8rEp+OWHZqM1B/3I7bCeFM8YokfXz/Qt6Ts+CZJpWR2D6KFWzJejfRE/g6folW6yRCQRWCBa2ubi82gzVExz4iSVPTevxL9rsFStX1bN7F+SgPmw5AKp4GFxNKkEdUPIqxFFXHu/6TV0gcaiOtVQtEf7X8kT2NGE1CqiWMjbg== Received: from MW3PR06CA0006.namprd06.prod.outlook.com (2603:10b6:303:2a::11) by DM6PR12MB4780.namprd12.prod.outlook.com (2603:10b6:5:168::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.14; Mon, 29 Aug 2022 11:49:24 +0000 Received: from CO1NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2a:cafe::94) by MW3PR06CA0006.outlook.office365.com (2603:10b6:303:2a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.14 via Frontend Transport; Mon, 29 Aug 2022 11:49:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by CO1NAM11FT003.mail.protection.outlook.com (10.13.175.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:24 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Mon, 29 Aug 2022 11:49:23 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 29 Aug 2022 04:49:22 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Mon, 29 Aug 2022 04:49:17 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v7 4/5] vfio/pci: Implement VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY/EXIT Date: Mon, 29 Aug 2022 17:18:49 +0530 Message-ID: <20220829114850.4341-5-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220829114850.4341-1-abhsahu@nvidia.com> References: <20220829114850.4341-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d7797540-0c20-419e-2b8f-08da89b484cc X-MS-TrafficTypeDiagnostic: DM6PR12MB4780:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dfVte2bCoNOWl7tB6S1k3W9VqetQkbm41IMLsbrbMMpNTmDbb0Vn5b6wgMMtqbdLM4eZgw5q4jagT0FjR66Gtx//5e8V/2QZnLF3Ppf/OKm4R+W+gtwpGuFVxtBgyjf+sLmf03ESbEVRi0AktPCKuhm2xgkqE4fo1k5uAIhfd8S+jIUbMY6h+4AAG2ip+dInD7sADA1MRtTz0ZCansD9puvU6QNfvPiJBVxUw0V7u9zNbk2bYAdDiM7PdovcpQ7k4yyr/AaPnPOx1xWG2WscmvBgajuF0JcUEaqoQFL1a8xzG1CDdPlpCH9pxkhn+Q5O8dBMYnehznZBG7eTZJAt7v/4VX1v9D1xfpStcWxOySDp2Z0JPAfthJ7lob8G3D1O+1fpHH7NSOORXap8qbwnodt46nCgxsBbRSMlOEBTKBRk5m0IAV39Qo4ES3APF2k0TXR/pZfJpbxC/KoUM4FMvyFscghRGH6wSEIQqYNwgQ4m4d0c4r5ntPI9CR/aFl+FZ0tSstj0t1MHvfAK2xJijGqqAW3CMy9oVJ/q447bbWOekEf8ZHGT8ckEKbE6ql9WkewjQyBxCKcBuLmjtk2TvBq52tYJraMEmEp7EAstKsyN/yTqW0GBB3L1BmBdQkLsTEnzGhQiTA7lYbCyRneBsVCqzyQg7WVcGg3hkKhsTPLA3b6alAbPVIEb+cvWZJem5FSWz/F0Y1XpeceEe2V9q2kUmmsmVjR3Ik2WZy99TrRL/kcclELXmyP9tXUcK4Wb1cxIkPll3qRjNfeAnfnx0CTCgqrIzOOP6jsS7AcV9LlibL6pv8XKGyt0XPcXrJBE X-Forefront-Antispam-Report: CIP:12.22.5.236; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230016)(4636009)(39860400002)(346002)(136003)(376002)(396003)(36840700001)(46966006)(40470700004)(82310400005)(7696005)(2906002)(107886003)(6666004)(30864003)(2616005)(82740400003)(336012)(40480700001)(83380400001)(186003)(26005)(426003)(36860700001)(47076005)(316002)(8676002)(4326008)(86362001)(70206006)(70586007)(40460700003)(1076003)(54906003)(110136005)(81166007)(36756003)(356005)(7416002)(41300700001)(8936002)(478600001)(5660300002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2022 11:49:24.0031 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d7797540-0c20-419e-2b8f-08da89b484cc X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.236]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4780 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Currently, if the runtime power management is enabled for vfio-pci based devices in the guest OS, then the guest OS will do the register write for PCI_PM_CTRL register. This write request will be handled in vfio_pm_config_write() where it will do the actual register write of PCI_PM_CTRL register. With this, the maximum D3hot state can be achieved for low power. If we can use the runtime PM framework, then we can achieve the D3cold state (on the supported systems) which will help in saving maximum power. 1. D3cold state can't be achieved by writing PCI standard PM config registers. This patch implements the following newly added low power related device features: - VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY - VFIO_DEVICE_FEATURE_LOW_POWER_EXIT The VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY feature will allow the device to make use of low power platform states on the host while the VFIO_DEVICE_FEATURE_LOW_POWER_EXIT will prevent further use of those power states. 2. The vfio-pci driver uses runtime PM framework for low power entry and exit. On the platforms where D3cold state is supported, the runtime PM framework will put the device into D3cold otherwise, D3hot or some other power state will be used. There are various cases where the device will not go into the runtime suspended state. For example, - The runtime power management is disabled on the host side for the device. - The user keeps the device busy after calling LOW_POWER_ENTRY. - There are dependent devices that are still in runtime active state. For these cases, the device will be in the same power state that has been configured by the user through PCI_PM_CTRL register. 3. The hypervisors can implement virtual ACPI methods. For example, in guest linux OS if PCI device ACPI node has _PR3 and _PR0 power resources with _ON/_OFF method, then guest linux OS invokes the _OFF method during D3cold transition and then _ON during D0 transition. The hypervisor can tap these virtual ACPI calls and then call the low power device feature IOCTL. 4. The 'pm_runtime_engaged' flag tracks the entry and exit to runtime PM. This flag is protected with 'memory_lock' semaphore. 5. All the config and other region access are wrapped under pm_runtime_resume_and_get() and pm_runtime_put(). So, if any device access happens while the device is in the runtime suspended state, then the device will be resumed first before access. Once the access has been finished, then the device will again go into the runtime suspended state. 6. The memory region access through mmap will not be allowed in the low power state. Since __vfio_pci_memory_enabled() is a common function, so check for 'pm_runtime_engaged' has been added explicitly in vfio_pci_mmap_fault() to block only mmap'ed access. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_core.c | 153 +++++++++++++++++++++++++++++-- include/linux/vfio_pci_core.h | 1 + 2 files changed, 146 insertions(+), 8 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 207ede189c2a..9c612162653f 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -277,11 +277,100 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat return ret; } +static int vfio_pci_runtime_pm_entry(struct vfio_pci_core_device *vdev) +{ + /* + * The vdev power related flags are protected with 'memory_lock' + * semaphore. + */ + vfio_pci_zap_and_down_write_memory_lock(vdev); + if (vdev->pm_runtime_engaged) { + up_write(&vdev->memory_lock); + return -EINVAL; + } + + vdev->pm_runtime_engaged = true; + pm_runtime_put_noidle(&vdev->pdev->dev); + up_write(&vdev->memory_lock); + + return 0; +} + +static int vfio_pci_core_pm_entry(struct vfio_device *device, u32 flags, + void __user *arg, size_t argsz) +{ + struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 0); + if (ret != 1) + return ret; + + /* + * Inside vfio_pci_runtime_pm_entry(), only the runtime PM usage count + * will be decremented. The pm_runtime_put() will be invoked again + * while returning from the ioctl and then the device can go into + * runtime suspended state. + */ + return vfio_pci_runtime_pm_entry(vdev); +} + +static void __vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) +{ + if (vdev->pm_runtime_engaged) { + vdev->pm_runtime_engaged = false; + pm_runtime_get_noresume(&vdev->pdev->dev); + } +} + +static void vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) +{ + /* + * The vdev power related flags are protected with 'memory_lock' + * semaphore. + */ + down_write(&vdev->memory_lock); + __vfio_pci_runtime_pm_exit(vdev); + up_write(&vdev->memory_lock); +} + +static int vfio_pci_core_pm_exit(struct vfio_device *device, u32 flags, + void __user *arg, size_t argsz) +{ + struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 0); + if (ret != 1) + return ret; + + /* + * The device is always in the active state here due to pm wrappers + * around ioctls. + */ + vfio_pci_runtime_pm_exit(vdev); + return 0; +} + #ifdef CONFIG_PM static int vfio_pci_core_runtime_suspend(struct device *dev) { struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); + down_write(&vdev->memory_lock); + /* + * The user can move the device into D3hot state before invoking + * power management IOCTL. Move the device into D0 state here and then + * the pci-driver core runtime PM suspend function will move the device + * into the low power state. Also, for the devices which have + * NoSoftRst-, it will help in restoring the original state + * (saved locally in 'vdev->pm_save'). + */ + vfio_pci_set_power_state(vdev, PCI_D0); + up_write(&vdev->memory_lock); + /* * If INTx is enabled, then mask INTx before going into the runtime * suspended state and unmask the same in the runtime resume. @@ -418,6 +507,18 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) /* * This function can be invoked while the power state is non-D0. + * This non-D0 power state can be with or without runtime PM. + * vfio_pci_runtime_pm_exit() will internally increment the usage + * count corresponding to pm_runtime_put() called during low power + * feature entry and then pm_runtime_resume() will wake up the device, + * if the device has already gone into the suspended state. Otherwise, + * the vfio_pci_set_power_state() will change the device power state + * to D0. + */ + vfio_pci_runtime_pm_exit(vdev); + pm_runtime_resume(&pdev->dev); + + /* * This function calls __pci_reset_function_locked() which internally * can use pci_pm_reset() for the function reset. pci_pm_reset() will * fail if the power state is non-D0. Also, for the devices which @@ -1273,6 +1374,10 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz) { switch (flags & VFIO_DEVICE_FEATURE_MASK) { + case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY: + return vfio_pci_core_pm_entry(device, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_LOW_POWER_EXIT: + return vfio_pci_core_pm_exit(device, flags, arg, argsz); case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: return vfio_pci_core_feature_token(device, flags, arg, argsz); default: @@ -1285,31 +1390,47 @@ static ssize_t vfio_pci_rw(struct vfio_pci_core_device *vdev, char __user *buf, size_t count, loff_t *ppos, bool iswrite) { unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); + int ret; if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) return -EINVAL; + ret = pm_runtime_resume_and_get(&vdev->pdev->dev); + if (ret) { + pci_info_ratelimited(vdev->pdev, "runtime resume failed %d\n", + ret); + return -EIO; + } + switch (index) { case VFIO_PCI_CONFIG_REGION_INDEX: - return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); + ret = vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); + break; case VFIO_PCI_ROM_REGION_INDEX: if (iswrite) - return -EINVAL; - return vfio_pci_bar_rw(vdev, buf, count, ppos, false); + ret = -EINVAL; + else + ret = vfio_pci_bar_rw(vdev, buf, count, ppos, false); + break; case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: - return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); + ret = vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); + break; case VFIO_PCI_VGA_REGION_INDEX: - return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); + ret = vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); + break; + default: index -= VFIO_PCI_NUM_REGIONS; - return vdev->region[index].ops->rw(vdev, buf, + ret = vdev->region[index].ops->rw(vdev, buf, count, ppos, iswrite); + break; } - return -EINVAL; + pm_runtime_put(&vdev->pdev->dev); + return ret; } ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf, @@ -1504,7 +1625,11 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) mutex_lock(&vdev->vma_lock); down_read(&vdev->memory_lock); - if (!__vfio_pci_memory_enabled(vdev)) { + /* + * Memory region cannot be accessed if the low power feature is engaged + * or memory access is disabled. + */ + if (vdev->pm_runtime_engaged || !__vfio_pci_memory_enabled(vdev)) { ret = VM_FAULT_SIGBUS; goto up_out; } @@ -2219,6 +2344,15 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, goto err_unlock; } + /* + * Some of the devices in the dev_set can be in the runtime suspended + * state. Increment the usage count for all the devices in the dev_set + * before reset and decrement the same after reset. + */ + ret = vfio_pci_dev_set_pm_runtime_get(dev_set); + if (ret) + goto err_unlock; + list_for_each_entry(cur_vma, &dev_set->device_list, vdev.dev_set_list) { /* * Test whether all the affected devices are contained by the @@ -2274,6 +2408,9 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, else mutex_unlock(&cur->vma_lock); } + + list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) + pm_runtime_put(&cur->pdev->dev); err_unlock: mutex_unlock(&dev_set->lock); return ret; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index a0f1f36e42a2..1025d53fde0b 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -79,6 +79,7 @@ struct vfio_pci_core_device { bool nointx; bool needs_pm_restore; bool pm_intx_masked; + bool pm_runtime_engaged; struct pci_saved_state *pci_saved_state; struct pci_saved_state *pm_save; int ioeventfds_nr; From patchwork Mon Aug 29 11:48:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 602134 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 E5B54ECAAD2 for ; Mon, 29 Aug 2022 12:06:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231134AbiH2MGX (ORCPT ); Mon, 29 Aug 2022 08:06:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229494AbiH2MGF (ORCPT ); Mon, 29 Aug 2022 08:06:05 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2051.outbound.protection.outlook.com [40.107.95.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83DD58C02E; Mon, 29 Aug 2022 04:51:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eccUwO146pp9KMpFXqLXs7E1QjqzJupJIU8rHZrH+hyxtp8D3N31wqFVvlyyWiYletUohBrQK8HRMFOPgVs1lSlhkTLLuHpa2tDPksD835tgBKZjUljcjI7ylUoYItIvObrwpYpojJJvLewgZ5rCaxho6Owgfl42UlG7AHKkae/4TQ8re6a9hIc7u/DqipqKHbouTZ717GRHgMQ38IIru9IE4WZdYGMnf8SAnowaDRLtGQylOMusGjE/xerRXQ+4gLiN7mKhoEDjFHLlCFJfWTZPmRFtJmQQCxzAb92zrHGvV8hM8EHVt/FiYNq1Zm5KyyOzbrw6RbkCcnqptQT2gQ== 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=VZ/zwXj4y/6VfaxgkC+vhiLKBJuygzeOHfNXVpjH/eY=; b=nSyvqoUE5uBGWh40Pyb3q1Gx8tJDAJ080Na0KF/GNS7E/YLKPAXT3kGiUTbeNeJuzwyPqSzkRJtgDqJwma98ZxuBKKzDQBzwlE5NblPzPZ6KKhkVMAWyAs/8W2k9Z76sz4TtQoto1L1kWrI2zJHyI1bguEy0tkpQNFqS4AHwneGj76dLh0qCgWG1ffwQlUISRzqZrO8oGCc3Oz2+wlcm6BYKyPuunOt4FL64uUBU57o5PKRESQxMC9ULNDSvC4FuQPUbB6BbOaGlF8CuQFEpOXb9avFmbd9e2C0a3Z0vD4B7Zb7EYfbFnN7L7VZVw4bzWT8bq/fhSwQiwFpJa43JyQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VZ/zwXj4y/6VfaxgkC+vhiLKBJuygzeOHfNXVpjH/eY=; b=t9CEtW7aCqKa0ZRa2Fu3TsUBST7e7SJwgyftSCUyjMA0O3Yx9pFopItB5nqRzR8U75wDwEfLsNWRiECICeYuh48Z/Wq5tyGyEB9PF7K7Jv1x53tksmVF8/PmPrdOXzekjLqk9iDj2rrBfclwXzJKQfJ9jEbOdmA97CuNgI4syFyYWt2qMVzjY9bDphNWj+Igf+W9K+xY3b9EuTJmUWVHTrR2OLs/WNjsDVd1J+e2zLuCMPHpuP4CBl8UhKF4Nztr3labTqvYHtajw9/qzkky+KXLTPDTEhlMtpjfUXTOZ+jzAIHc6PE/ATLVr02CHG/KOEyhZU/YnbzuX1CospdJIQ== Received: from BN9PR03CA0854.namprd03.prod.outlook.com (2603:10b6:408:13d::19) by CH2PR12MB4876.namprd12.prod.outlook.com (2603:10b6:610:67::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15; Mon, 29 Aug 2022 11:49:31 +0000 Received: from BN8NAM11FT030.eop-nam11.prod.protection.outlook.com (2603:10b6:408:13d:cafe::e5) by BN9PR03CA0854.outlook.office365.com (2603:10b6:408:13d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:31 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.235) by BN8NAM11FT030.mail.protection.outlook.com (10.13.177.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5566.15 via Frontend Transport; Mon, 29 Aug 2022 11:49:30 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Mon, 29 Aug 2022 11:49:29 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 29 Aug 2022 04:49:28 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Mon, 29 Aug 2022 04:49:23 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v7 5/5] vfio/pci: Implement VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP Date: Mon, 29 Aug 2022 17:18:50 +0530 Message-ID: <20220829114850.4341-6-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220829114850.4341-1-abhsahu@nvidia.com> References: <20220829114850.4341-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a5cdc856-2fae-428e-ad24-08da89b488e9 X-MS-TrafficTypeDiagnostic: CH2PR12MB4876:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: J8utr+yg0pZi7JCGTaZlDK2oRfp35QtiWFg8Tb3TxFFaLOgBzU/YfqHP6Np7WWfdmRgrRpGRadlLipUliTo6lW+xabS1istGLxvzfZdqd2YjvLJW5EpdNddBiXuaWK/UjuGjZn/whdGdHqgxoWYL3NILCKHwZxWX6VdnR8LN8hoqOBAw1m7j+dUONwGrr6rpiey/eEgQuvI8raIGZ9YUnHObKEV2k229EgvIcgiPWS+ZMsvxhnsMmjlpfACljLovF0T7OSJFr1xNexaINmvEBWBs6JExkY0+bEhACErmvXGMg/C3eovZrJ8yGe4R3LZJIrBMXJg7KB5AxrlliSZ7X5svbwVJb3QLD8AByY+vWriaSFdg1XJwCyOei0b/FihSxOVQX2CBeibceacP0AgpoRZw8+AlIiRtksLALBNrmVVMNH5zVPkCiwxHpVFYE1G7kFqctM4Cax15JeOL3Gm63H1mWYpy7zhuvOF3a/c3T+u9zUVgy/UJBS4tgiKIjY5x4FaCe0USWuzd/4BCMaWHJzNhiAUJVuYhwTopOtsQZWuYRAT0eOqfg0lFABDiP/ZC0hTUNAQWnWp5XNnnlH8V2k6nfjocmHEYzIWZMGXr2sjT37FCC+Yr3vWMIkCVYrO7P4Dp/wZPsctVptW2VgqbwyAuqmKZrcuMX8KeG/DQ+x1JMqaMOXyXie8q/L9pJHuxnhus03BNbmYMvVXt39h87QZrPnrcMhMXe30C8fOrpqHbOKK/4zDLak9oFXzVuCZxwsw2/FNA8sQKUieVuoXT1AwUcspBc4clTHOy+Pz/fQiCJtg4gy2hKQSmbYp+1m6I X-Forefront-Antispam-Report: CIP:12.22.5.235; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230016)(4636009)(346002)(136003)(39860400002)(376002)(396003)(40470700004)(46966006)(36840700001)(2906002)(107886003)(26005)(82740400003)(6666004)(2616005)(82310400005)(426003)(40460700003)(40480700001)(336012)(47076005)(186003)(1076003)(36860700001)(83380400001)(110136005)(8676002)(54906003)(4326008)(70206006)(70586007)(7696005)(41300700001)(7416002)(5660300002)(81166007)(86362001)(8936002)(316002)(356005)(36756003)(478600001)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2022 11:49:30.8762 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a5cdc856-2fae-428e-ad24-08da89b488e9 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.235]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT030.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4876 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This patch implements VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP device feature. In the VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY, if there is any access for the VFIO device on the host side, then the device will be moved out of the low power state without the user's guest driver involvement. Once the device access has been finished, then the host can move the device again into low power state. With the low power entry happened through VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP, the device will not be moved back into the low power state and a notification will be sent to the user by triggering wakeup eventfd. vfio_pci_core_pm_entry() will be called for both the variants of low power feature entry so add an extra argument for wakeup eventfd context and store locally in 'struct vfio_pci_core_device'. For the entry happened without wakeup eventfd, all the exit related handling will be done by the LOW_POWER_EXIT device feature only. When the LOW_POWER_EXIT will be called, then the vfio core layer vfio_device_pm_runtime_get() will increment the usage count and will resume the device. In the driver runtime_resume callback, the 'pm_wake_eventfd_ctx' will be NULL. Then vfio_pci_core_pm_exit() will call vfio_pci_runtime_pm_exit() and all the exit related handling will be done. For the entry happened with wakeup eventfd, in the driver resume callback, eventfd will be triggered and all the exit related handling will be done. When vfio_pci_runtime_pm_exit() will be called by vfio_pci_core_pm_exit(), then it will return early. But if the runtime suspend has not happened on the host side, then all the exit related handling will be done in vfio_pci_core_pm_exit() only. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_core.c | 63 ++++++++++++++++++++++++++++++-- include/linux/vfio_pci_core.h | 1 + 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 9c612162653f..0d4b49f06b14 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -277,7 +277,8 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat return ret; } -static int vfio_pci_runtime_pm_entry(struct vfio_pci_core_device *vdev) +static int vfio_pci_runtime_pm_entry(struct vfio_pci_core_device *vdev, + struct eventfd_ctx *efdctx) { /* * The vdev power related flags are protected with 'memory_lock' @@ -290,6 +291,7 @@ static int vfio_pci_runtime_pm_entry(struct vfio_pci_core_device *vdev) } vdev->pm_runtime_engaged = true; + vdev->pm_wake_eventfd_ctx = efdctx; pm_runtime_put_noidle(&vdev->pdev->dev); up_write(&vdev->memory_lock); @@ -313,7 +315,40 @@ static int vfio_pci_core_pm_entry(struct vfio_device *device, u32 flags, * while returning from the ioctl and then the device can go into * runtime suspended state. */ - return vfio_pci_runtime_pm_entry(vdev); + return vfio_pci_runtime_pm_entry(vdev, NULL); +} + +static int vfio_pci_core_pm_entry_with_wakeup( + struct vfio_device *device, u32 flags, + struct vfio_device_low_power_entry_with_wakeup __user *arg, + size_t argsz) +{ + struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); + struct vfio_device_low_power_entry_with_wakeup entry; + struct eventfd_ctx *efdctx; + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, + sizeof(entry)); + if (ret != 1) + return ret; + + if (copy_from_user(&entry, arg, sizeof(entry))) + return -EFAULT; + + if (entry.wakeup_eventfd < 0) + return -EINVAL; + + efdctx = eventfd_ctx_fdget(entry.wakeup_eventfd); + if (IS_ERR(efdctx)) + return PTR_ERR(efdctx); + + ret = vfio_pci_runtime_pm_entry(vdev, efdctx); + if (ret) + eventfd_ctx_put(efdctx); + + return ret; } static void __vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) @@ -321,6 +356,11 @@ static void __vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) if (vdev->pm_runtime_engaged) { vdev->pm_runtime_engaged = false; pm_runtime_get_noresume(&vdev->pdev->dev); + + if (vdev->pm_wake_eventfd_ctx) { + eventfd_ctx_put(vdev->pm_wake_eventfd_ctx); + vdev->pm_wake_eventfd_ctx = NULL; + } } } @@ -348,7 +388,10 @@ static int vfio_pci_core_pm_exit(struct vfio_device *device, u32 flags, /* * The device is always in the active state here due to pm wrappers - * around ioctls. + * around ioctls. If the device had entered a low power state and + * pm_wake_eventfd_ctx is valid, vfio_pci_core_runtime_resume() has + * already signaled the eventfd and exited low power mode itself. + * pm_runtime_engaged protects the redundant call here. */ vfio_pci_runtime_pm_exit(vdev); return 0; @@ -388,6 +431,17 @@ static int vfio_pci_core_runtime_resume(struct device *dev) { struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); + /* + * Resume with a pm_wake_eventfd_ctx signals the eventfd and exit + * low power mode. + */ + down_write(&vdev->memory_lock); + if (vdev->pm_wake_eventfd_ctx) { + eventfd_signal(vdev->pm_wake_eventfd_ctx, 1); + __vfio_pci_runtime_pm_exit(vdev); + } + up_write(&vdev->memory_lock); + if (vdev->pm_intx_masked) vfio_pci_intx_unmask(vdev); @@ -1376,6 +1430,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, switch (flags & VFIO_DEVICE_FEATURE_MASK) { case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY: return vfio_pci_core_pm_entry(device, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP: + return vfio_pci_core_pm_entry_with_wakeup(device, flags, + arg, argsz); case VFIO_DEVICE_FEATURE_LOW_POWER_EXIT: return vfio_pci_core_pm_exit(device, flags, arg, argsz); case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 1025d53fde0b..089b603bcfdc 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -85,6 +85,7 @@ struct vfio_pci_core_device { int ioeventfds_nr; struct eventfd_ctx *err_trigger; struct eventfd_ctx *req_trigger; + struct eventfd_ctx *pm_wake_eventfd_ctx; struct list_head dummy_resources_list; struct mutex ioeventfds_lock; struct list_head ioeventfds_list;