From patchwork Thu Apr 20 07:47:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 85768 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp158690vqo; Thu, 20 Apr 2023 01:08:12 -0700 (PDT) X-Google-Smtp-Source: AKy350am7Lf1HsEBO/NPqqtp7mFGwtZKjzagb3pbV6XuI0ThAIqMfkvGlbBoh3DSwprDtbTjiY1p X-Received: by 2002:a05:6a20:8f21:b0:ef:7d7b:432f with SMTP id b33-20020a056a208f2100b000ef7d7b432fmr1230159pzk.21.1681978092087; Thu, 20 Apr 2023 01:08:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1681978092; cv=pass; d=google.com; s=arc-20160816; b=u7g/hqBGunJHvsnE4NFqkWTuwfZ7jHAyBtCyiv1rtV+xkIQPKqgxDv3VtsuLZ2aUY6 JL50YAALXmEmOlxEEZeYy84311f06MeHfSTXK5mYWD2GRECytZklD/sbp1eafTssg3I0 q5o418/60O4IQBhrDgoEbEgH4/M8Wc5eojIE7TCwdmdu+LUN3Gyi79ZJTKg0K0BEZ3GQ MyZEU2J0Kiy3XBckTSLYcKzKl699rl2Nb6CUROmHaeuVrHNI4XhIWdfHd6b67YwqVi7N 59qnKKXzrvA1q/mbLRadDJBJzpo5ddfmzpv728Fi/pGFkz8FFsv2qtylrJqCzYCLwk0Z OgLw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SB5IaaRV6DtyBHNYRvQgKfnE6ElEe6c/CS9kqoQICfg=; b=Q/zMWD9uWEJViHGx0G3AvKgt/L1pJd62gJM6T8B1nw5Mvy6pIrID6rvGQwi6N1fR/K SyAYyrW7QngCqBUXDXvJbpHzyNKK1wstU8C0YAkI9XOGM6Z99kchL+Sjle1lbILj1vhI CpiHBABRPdB3X7BmAvllLw3iXGunr6m0vDfkAPUq8SqVdEDT6HIBqqbYlwRbVJTzy98s yXqJK3Tv4zCxQc+xng9Aci6rdJwWzK/xQCdGHFuUEjhi6UQ7deidINgz4u/Jdqu+ov3V CmJ5t97grtA4I+1UeDkkUmH4rk7/ZNqxY/qjFQLiiT9B2vNYY11D6feWKRLKUQeQtCUb Iyag== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Y+K0fNSb; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y4-20020a655b04000000b0051936c543c8si1031041pgq.212.2023.04.20.01.07.59; Thu, 20 Apr 2023 01:08:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Y+K0fNSb; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234179AbjDTHtD (ORCPT + 99 others); Thu, 20 Apr 2023 03:49:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232726AbjDTHsf (ORCPT ); Thu, 20 Apr 2023 03:48:35 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A57246A4; Thu, 20 Apr 2023 00:48:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YrgLksOJBA+x4rIK5bVZoZrPW9V8HHBerPKKPgJ4xFvmDfz+tjC9a+XPBSMgLpR1ZnKzOWB5fxPrc8bh3lcG2rCiN2DPw0I7kPrDMk11vzzdkBntD69JKaBJ8od6a34P8tXKrqb4aTEHPPnjAuAWDFy1hpvFSWcJPavZvCW/Wnc+qmYNb4ExrJ+4hfeXoPNis0YIkI+eyqgYoi1yfi3YgPfW/Op3O0RGB60BUIx4ret6tDoQzYmkTO3NXjfzyXpWA6mZuTRupG0VumprO7ZTVHvk5qnVll6Mwz0+Qc9m+lfgG8Jg1szCZISWlspIxpnj81ceJ5Y1c+jXg4nsAcXlDw== 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=SB5IaaRV6DtyBHNYRvQgKfnE6ElEe6c/CS9kqoQICfg=; b=bkwCjBd4Tbmoxv9Vqhfc23g7gVgSazTVCfbYJvolixVjCrrtwu5Bo/iD8LhW2y/1OGkVV3Eqq8xz21deRsymNryIf4XpR/L+1edUHcTE6aSX4XaMHkz+3UJ0e9+07V2wMoeb2nkPW3pLcl1W9e0OM5nnPDkQjttA22sld72tbKEQtN6LbI/7sxpx7DkxDOuSgex9zb7bRPW52Dc66d7pwNl6OkovKE9OE9s7h1g/YQFMizmG+SkJe+nQVr95NseIGBmQwcegJehenxrvur3fL1pbzTGWEbH0JhZ23p7Ra1eYas3o1cmGAHSCigyzsdjI1zEeHo/1IaY0Zmztf1Pa+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com 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=SB5IaaRV6DtyBHNYRvQgKfnE6ElEe6c/CS9kqoQICfg=; b=Y+K0fNSbGmKNOTJ2LSfunrGdJkpVcyFlyY+VDs1qOjOAPQpHmZzC51jFLzYd+QrSy3lQ88suJN1yMvNzfUgOtQnfPUGa0K9dREDRt4AtnLQ30f25RGWeUhF2ROTAGGM78uA0y3C/QYlEFBsVzmzDC2mK8AdV7du21d2Ht4W3dEq1FhGdNPkSbeGzCDomA9gmM2ZGwZiEI5Tl6Q8FUjS0c5OAnIRdJjfDHs9qXxnE8Uw6sJZZjdf42YhfQnWHMoAv8k/PEegqrnKz+pF76pY5aBBpLd/ApX/0KE/R+uF8chY2ctTehP0nFjAOcmJqiZTgUJjIq745Q1Wr1/Ednc1nLg== Received: from DM5PR07CA0063.namprd07.prod.outlook.com (2603:10b6:4:ad::28) by MW4PR12MB7166.namprd12.prod.outlook.com (2603:10b6:303:224::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.31; Thu, 20 Apr 2023 07:48:30 +0000 Received: from DM6NAM11FT022.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ad:cafe::43) by DM5PR07CA0063.outlook.office365.com (2603:10b6:4:ad::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.24 via Frontend Transport; Thu, 20 Apr 2023 07:48:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DM6NAM11FT022.mail.protection.outlook.com (10.13.172.210) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.25 via Frontend Transport; Thu, 20 Apr 2023 07:48:30 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 20 Apr 2023 00:48:19 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 20 Apr 2023 00:48:19 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 20 Apr 2023 00:48:18 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH RFC v2 3/3] vfio: Add dev_data_len/uptr in struct vfio_device_bind_iommufd Date: Thu, 20 Apr 2023 00:47:47 -0700 Message-ID: <8232c5ea073d793d277f45036a6e61e0849b4202.1681976394.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT022:EE_|MW4PR12MB7166:EE_ X-MS-Office365-Filtering-Correlation-Id: 3bdcb85b-4a6b-438e-3f31-08db4173a25e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vdJFSWdmjtZWRXhvKqQpXGrqN3rXQ4di2zuNB+hsd+Ej4WHJuKymMckpBC8j5NeYKBFxJeKFAfHieLBFxrEpPSL7PmddvFkWi/GqlAnnAtCTBEIg6ruYhftZhPijgDjHHMNYTur1vqPeiwlsqULC1/ExAISPDFrqF0lwHY1NnDio1HRvWT5zmaU+mQ5beJP62GL4Xc67u13dl0scY5/JTAkGnfjFuuETVugBlKKp4yfyA2ak1vJ11SSkdTMt84B7751tEeiC+FQpv+LOjr1siIC4oYyHwl0xWzXZCtrh2j/EbU900uBBkAPh/6Ob5PmHmnvBTp+FTyu7a6g5lhomdojheBcg1mzWyUUKpQJruSMpkwhyrEIoAoHDx/sdC7TOkFJdGHKVIUE9xHDOZy0acMKEyIx+TExyAEFkdAf87bZdMt3FAPlXWJ8N4GEUTg01Hw0p4c2unYm5861TwDND7TfRtsOPNAM8dG8KN8H1g3LNZYSU/RR9Q8pstfz3Ya5JmoxJyrni35fZlwjXQD8tK6kIOcEo6D6cjgIryJnmqN0i7D7iWFr23pj1zGsCMnX6dc5heB9+EFkgF/aLyTA/GaQEtK3DEVgjCXXIc4ysO4drfZYslB4WEepoGlPV8TMCusLf9nGb0mVQH973Ka8OUyVbdYM/OkpklPFtOZWSlOwfw029S3WPl2bngKKGX3y08FBujzTK+I+50NmhpDfIsxgYjxdhlautexde8nST+tEGt1qY/8jPuHM/h8pzIvaDiXyXnlDjdz0bJID3+zbYWNuBlfq8oHg0ZfjdO09XuPI= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(46966006)(36840700001)(40470700004)(36756003)(186003)(26005)(34020700004)(36860700001)(40460700003)(478600001)(82310400005)(6666004)(40480700001)(110136005)(4326008)(54906003)(8676002)(86362001)(316002)(7696005)(7636003)(70586007)(8936002)(70206006)(356005)(82740400003)(7416002)(41300700001)(2616005)(47076005)(2906002)(83380400001)(426003)(336012)(5660300002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:48:30.2781 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3bdcb85b-4a6b-438e-3f31-08db4173a25e X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT022.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7166 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763681859919080959?= X-GMAIL-MSGID: =?utf-8?q?1763681859919080959?= This allows user space to pass in an iommu specific device data with the VFIO_DEVICE_BIND_IOMMUFD ioctl. The data is not mandatory. But it must be provided if a non-zero data_len is passed along. Signed-off-by: Nicolin Chen --- drivers/vfio/device_cdev.c | 19 +++++++++++++++++-- drivers/vfio/iommufd.c | 13 +++++++++++++ include/linux/vfio.h | 2 ++ include/uapi/linux/vfio.h | 13 +++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index b5824179cd48..70241d9c0fa9 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -104,9 +104,10 @@ static struct iommufd_ctx *vfio_get_iommufd_from_fd(int fd) long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, struct vfio_device_bind_iommufd __user *arg) { + uint32_t mask = VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA; struct vfio_device *device = df->device; struct vfio_device_bind_iommufd bind; - unsigned long minsz; + unsigned long minsz, datasz; bool is_noiommu; int ret; @@ -117,9 +118,23 @@ long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, if (copy_from_user(&bind, arg, minsz)) return -EFAULT; - if (bind.argsz < minsz || bind.flags) + if (bind.argsz < minsz || bind.flags & ~mask) return -EINVAL; + if (bind.flags & VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA) { + datasz = offsetofend(struct vfio_device_bind_iommufd, + dev_data_len); + if (bind.argsz < datasz) + return -EINVAL; + if (copy_from_user((void *)&bind + minsz, + (void __user *)arg + minsz, datasz - minsz)) + return -EFAULT; + if (!bind.dev_data_uptr ^ !bind.dev_data_len) + return -EINVAL; + device->user_data = u64_to_user_ptr(bind.dev_data_uptr); + device->user_data_len = bind.dev_data_len; + } + /* BIND_IOMMUFD only allowed for cdev fds */ if (df->group) return -EINVAL; diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 16d6aac06180..0b985ccffcbe 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -91,6 +91,16 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, idev = iommufd_device_bind(ictx, vdev->dev, out_device_id); if (IS_ERR(idev)) return PTR_ERR(idev); + + if (vdev->user_data) { + int rc = iommufd_device_set_data(idev, vdev->user_data, + vdev->user_data_len); + if (rc) { + iommufd_device_unbind(idev); + return rc; + } + } + vdev->iommufd_device = idev; return 0; } @@ -104,8 +114,11 @@ void vfio_iommufd_physical_unbind(struct vfio_device *vdev) iommufd_device_detach(vdev->iommufd_device); vdev->iommufd_attached = false; } + iommufd_device_unset_data(vdev->iommufd_device); iommufd_device_unbind(vdev->iommufd_device); vdev->iommufd_device = NULL; + vdev->user_data = NULL; + vdev->user_data_len = 0; } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 46b313f8bfaf..e4bb63801472 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -68,6 +68,8 @@ struct vfio_device { #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_device *iommufd_device; bool iommufd_attached; + void *user_data; + u32 user_data_len; #endif bool noiommu; }; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 17c5a1dadd08..9cec823c2829 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -212,15 +212,28 @@ struct vfio_group_status { * as long as the input @iommufd is valid. Otherwise, it is * meaningless. devid is a handle for this device and can be * used in IOMMUFD commands. + * @dev_data_uptr: User pointer of the device user data. + * @dev_data_len: Length of the device user data. + * + * A device user data is an iommu specific structure that must be defined in + * the include/uapi/linux/iommufd.h file. On some platform enabling the iommu + * nested translation configuration, a device behind the iommu, while working + * in a guest VM, needs to provide the host kernel a certain virtual ID in the + * guest VM. For example, ARM SMMUv3 requires a virtual Stream ID to sanity a + * cache invalidation command from the user space. User space wanting to pass a + * user data must set VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA flag. * * Return: 0 on success, -errno on failure. */ struct vfio_device_bind_iommufd { __u32 argsz; __u32 flags; +#define VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA (1 << 0) __s32 iommufd; #define VFIO_NOIOMMU_FD (-1) __u32 out_devid; + __aligned_u64 dev_data_uptr; + __u32 dev_data_len; }; #define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 19)